Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

ActionKV function find() not working properly? #114

Open
ladislaff93 opened this issue Nov 4, 2023 · 0 comments
Open

ActionKV function find() not working properly? #114

ladislaff93 opened this issue Nov 4, 2023 · 0 comments

Comments

@ladislaff93
Copy link

ladislaff93 commented Nov 4, 2023

I notice that find() in lib.rs is throwing error because the
loop {let position = f.seek(SeekFrom::Current(0))?;...`
always go the end of the bytes and throw becuse cant find any bytes anymore

thread 'tests::test_find' panicked at 'called `Option::unwrap()` on a `None` value', src/lib.rs:266:81
stack backtrace:
   0: rust_begin_unwind
             at /rustc/5680fa18feaa87f3ff04063800aec256c3d4b4be/library/std/src/panicking.rs:593:5
   1: core::panicking::panic_fmt
             at /rustc/5680fa18feaa87f3ff04063800aec256c3d4b4be/library/core/src/panicking.rs:67:14
   2: core::panicking::panic
             at /rustc/5680fa18feaa87f3ff04063800aec256c3d4b4be/library/core/src/panicking.rs:117:5
   3: core::option::Option<T>::unwrap
             at /rustc/5680fa18feaa87f3ff04063800aec256c3d4b4be/library/core/src/option.rs:935:21
   4: libactionkv::tests::test_find
             at ./src/lib.rs:266:26
   5: libactionkv::tests::test_find::{{closure}}
             at ./src/lib.rs:259:20
   6: core::ops::function::FnOnce::call_once
             at /rustc/5680fa18feaa87f3ff04063800aec256c3d4b4be/library/core/src/ops/function.rs:250:5
   7: core::ops::function::FnOnce::call_once
             at /rustc/5680fa18feaa87f3ff04063800aec256c3d4b4be/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
test tests::test_find ... FAILED

i solved by adding
let position = f.seek(SeekFrom::Start(0)... before loop and
position = f.seek(SeekFrom::Current(0))?; at the end of the loop when the target key is found.

let mut position = f.seek(SeekFrom::Start(0))?;
    loop {

      let maybe_kv = ActionKV::process_record(&mut f);
      let kv = match maybe_kv {
        Ok(kv) => kv,
        Err(err) => {
          match err.kind() {
            io::ErrorKind::UnexpectedEof => {
              // "Unexpected" is relative. The application may not have
              // expected it, but we expect files to be finite.
              break;
            }
            _ => return Err(err),
          }
        }
      };

      if kv.key == target {
        found = Some((position, kv.value));
      }

      position = f.seek(SeekFrom::Current(0))?;
      // important to keep looping until the end of the file,
      // in case the key has been overwritten
    }

Am i missing something or dont understand how the SeekFrom work?

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant