Skip to content

ActionKV function find() not working properly?  #114

Description

@ladislaff93

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?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions