Skip to content

Commit

Permalink
Added scan with support for reverse
Browse files Browse the repository at this point in the history
  • Loading branch information
wolf4ood committed Jul 24, 2019
1 parent ce01659 commit 1652a1c
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 12 deletions.
6 changes: 5 additions & 1 deletion lib/sled.ex
Expand Up @@ -22,9 +22,13 @@ defmodule Sled do
end

def scan(%{db: db, codec: codec}, key, opts \\ []) do
with {:ok, cursor} <- Sled.Native.scan(db, codec.encode(key), opts),
with {:ok, cursor} <- Sled.Native.scan(db, codec.encode(key), to_map(opts)),
handle <- Sled.Cursor.wrap(cursor, codec) do
{:ok, handle}
end
end

defp to_map(map) when is_map(map), do: map
defp to_map([]), do: %{}
defp to_map(enum), do: Enum.into(enum, %{})
end
1 change: 1 addition & 0 deletions native/sled_ex/src/atoms.rs
Expand Up @@ -3,6 +3,7 @@ rustler_atoms! {
atom error;
atom not_found;
atom done;
atom reverse;
//atom __true__ = "true";
//atom __false__ = "false";
}
43 changes: 32 additions & 11 deletions native/sled_ex/src/db.rs
Expand Up @@ -40,12 +40,20 @@ impl DBHandle {
Ok(())
}

fn scan(&self, key: Binary) -> SledExResult<Cursor> {
fn scan(&self, key: Binary, reverse: bool) -> SledExResult<Cursor> {
let iter = self.0.scan(key.as_slice());

let eternal_iter: Iter<'static> = unsafe { std::mem::transmute(iter) };

Ok(Cursor(Mutex::new(eternal_iter)))
match reverse {
true => {
let eternal_iter: std::iter::Rev<Iter<'static>> =
unsafe { std::mem::transmute(iter.rev()) };
Ok(Cursor::RevIter(Mutex::new(eternal_iter)))
}
false => {
let eternal_iter: Iter<'static> = unsafe { std::mem::transmute(iter) };
Ok(Cursor::Iter(Mutex::new(eternal_iter)))
}
}
}
}

Expand Down Expand Up @@ -92,7 +100,10 @@ pub fn scan<'a>(env: Env<'a>, args: &[Term<'a>]) -> NifResult<Term<'a>> {

let key: Binary = args[1].decode()?;

let cursor = db.scan(key)?;
let cursor = match args[2].map_get(atoms::reverse().to_term(env)) {
Ok(e) => db.scan(key, e.decode()?)?,
_ => db.scan(key, false)?,
};

let resource = ResourceArc::new(cursor);

Expand All @@ -118,15 +129,25 @@ pub fn iter_next<'a>(env: Env<'a>, args: &[Term<'a>]) -> NifResult<Term<'a>> {
}
}

pub struct Cursor(Mutex<sled::Iter<'static>>);
pub enum Cursor {
Iter(Mutex<sled::Iter<'static>>),
RevIter(Mutex<std::iter::Rev<sled::Iter<'static>>>),
}

impl Cursor {
fn next(&self) -> Option<SledExResult<(Vec<u8>, IVec)>> {
self.0
.lock()
.unwrap()
.next()
.map(|v| v.map_err(SledExError::from))
match self {
Cursor::Iter(i) => i
.lock()
.unwrap()
.next()
.map(|v| v.map_err(SledExError::from)),
Cursor::RevIter(i) => i
.lock()
.unwrap()
.next()
.map(|v| v.map_err(SledExError::from)),
}
}
}

Expand Down
12 changes: 12 additions & 0 deletions test/sled_test.exs
Expand Up @@ -51,4 +51,16 @@ defmodule SledTest do
assert Enum.to_list(1..9) |> Enum.map(fn x -> {to_string(x), to_string(x)} end) ==
cursor |> Enum.into([])
end


test "should scan keys reverse", %{db: db} do
Enum.each(1..9, &(:ok = Sled.set(db, to_string(&1), to_string(&1))))

assert {:ok, "1"} == Sled.get(db, "1")

assert {:ok, cursor} = Sled.scan(db, "2",[reverse: true])

assert Enum.to_list(1..2) |> Enum.map(fn x -> {to_string(x), to_string(x)} end) |> Enum.reverse() ==
cursor |> Enum.into([])
end
end
13 changes: 13 additions & 0 deletions test/sled_with_codec_test.exs
Expand Up @@ -63,4 +63,17 @@ defmodule SledTestWithCodec do
assert Enum.to_list(1..9) |> Enum.map(fn x -> {x, x} end) ==
cursor |> Enum.into([])
end


test "should scan keys reverse", %{db: db} do
Enum.each(1..9, &(:ok = Sled.set(db, &1, &1)))

assert {:ok, 1} == Sled.get(db, 1)

assert {:ok, cursor} = Sled.scan(db, 2, [reverse: true] )

assert Enum.to_list(1..2) |> Enum.map(fn x -> {x, x} end) |> Enum.reverse() ==
cursor |> Enum.into([])
end

end

0 comments on commit 1652a1c

Please sign in to comment.