dermesser / leveldb-rs

A reimplementation of LevelDB in Rust (no bindings).
Other
521 stars 59 forks source link

seek is not working? #43

Closed ItsFunny closed 10 months ago

ItsFunny commented 10 months ago

let mut db = LevelDB::default();

    let key = vec![0, 1, 2];

    {
        db.set(&vec![10, 11], 1u32.to_le_bytes().as_slice())
            .unwrap();
    }

    {
        let mut key1 = key.clone();
        key1.extend_from_slice(&[1, 2]);
        db.set(key1.as_slice(), 1u32.to_le_bytes().as_slice())
            .unwrap();
    }

    {
        let mut key2 = key.clone();
        key2.extend_from_slice(&[3, 4]);
        db.set(key2.as_slice(), 2u32.to_le_bytes().as_slice())
            .unwrap();
    }

    {
        let mut key3 = key.clone();
        key3.extend_from_slice(&[5, 6]);
        db.set(key3.as_slice(), 3u32.to_le_bytes().as_slice())
            .unwrap();
    }

    db.iter_all(key.as_slice(), |v| {
        let v = u32::from_le_bytes(v.as_slice().try_into().unwrap());
        print!("{} ", v);
        v
    })
    .unwrap();
dermesser commented 10 months ago

where's the seek and what's the issue?

ItsFunny commented 10 months ago

this is my iter_all codes, I think i used in wrong way

 fn iter_all<KF, VF, K, V>(
        &mut self,
        prefix: &[u8],
        kf: KF,
        vf: VF,
    ) -> IndexerResult<Vec<(K, V)>>
    where
        KF: Fn(Vec<u8>) -> K,
        VF: Fn(Vec<u8>) -> Option<V>,
    {
        let mut db = self.db.borrow_mut();
        let mut iter = db.new_iter()?;
        iter.seek(prefix);
        let mut ret = vec![];
        let v = current_key_val(&iter);
        if let Some((k, v)) = v {
            let value = vf(v);
            let key = kf(k);
            if value.is_some() {
                ret.push((key, value.unwrap()))
            }
        }
        loop {
            if !iter.valid() {
                return Ok(ret);
            }
            let next = iter.next();
            if next.is_none() {
                return Ok(ret);
            }
            let (k, v) = next.unwrap();
            let key = kf(k);
            let value = vf(v);
            if value.is_some() {
                ret.push((key, value.unwrap()))
            }
        }
    }

@dermesser Hi,please take a look