mirror of https://github.com/poanetwork/rox.git
refactor(): formatting and styling
This commit is contained in:
parent
ed025c569e
commit
bafb36bb49
48
lib/rox.ex
48
lib/rox.ex
|
@ -1,4 +1,9 @@
|
||||||
defmodule Rox do
|
defmodule Rox do
|
||||||
|
@moduledoc """
|
||||||
|
Elixir wrapper for RocksDB.
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
@opts_to_convert_to_bitlists [:db_log_dir, :wal_dir]
|
@opts_to_convert_to_bitlists [:db_log_dir, :wal_dir]
|
||||||
|
|
||||||
@type compaction_style :: :level | :universal | :fifo | :none
|
@type compaction_style :: :level | :universal | :fifo | :none
|
||||||
|
@ -136,25 +141,27 @@ defmodule Rox do
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Open a RocksDB with the specified database options and column family options
|
Open a RocksDB with the specified database options and column family options
|
||||||
"""
|
|
||||||
|
|
||||||
|
"""
|
||||||
@spec open(path :: file_path, db_opts :: db_options, cf_opts :: cf_options) :: {:ok, db_handle} | {:error, any}
|
@spec open(path :: file_path, db_opts :: db_options, cf_opts :: cf_options) :: {:ok, db_handle} | {:error, any}
|
||||||
def open(path, db_opts \\ [], cf_opts \\ []) do
|
def open(path, db_opts \\ [], cf_opts \\ []) do
|
||||||
:erocksdb.open(to_charlist(path), sanitize_opts(db_opts), sanitize_opts(cf_opts))
|
:erocksdb.open(to_charlist(path), sanitize_opts(db_opts), sanitize_opts(cf_opts))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Close the RocksDB with the specifed `db_handle`
|
Close the RocksDB with the specifed `db_handle`
|
||||||
"""
|
|
||||||
|
|
||||||
|
"""
|
||||||
@spec close(db_handle) :: :ok | {:error, any}
|
@spec close(db_handle) :: :ok | {:error, any}
|
||||||
def close(db), do:
|
def close(db), do:
|
||||||
:erocksdb.close(db)
|
:erocksdb.close(db)
|
||||||
|
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Put a key/value pair into the default column family handle
|
Put a key/value pair into the default column family handle
|
||||||
"""
|
|
||||||
|
|
||||||
|
"""
|
||||||
@spec put(db_handle, key, value) :: :ok | {:error, any}
|
@spec put(db_handle, key, value) :: :ok | {:error, any}
|
||||||
def put(db, key, value) when is_binary(value), do:
|
def put(db, key, value) when is_binary(value), do:
|
||||||
:erocksdb.put(db, key, value, [])
|
:erocksdb.put(db, key, value, [])
|
||||||
|
@ -162,11 +169,12 @@ defmodule Rox do
|
||||||
def put(db, key, value), do:
|
def put(db, key, value), do:
|
||||||
:erocksdb.put(db, key, :erlang.term_to_binary(value), [])
|
:erocksdb.put(db, key, :erlang.term_to_binary(value), [])
|
||||||
|
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Put a key/value pair into the default column family handle with the provided
|
Put a key/value pair into the default column family handle with the provided
|
||||||
write options
|
write options
|
||||||
"""
|
|
||||||
|
|
||||||
|
"""
|
||||||
@spec put(db_handle, key, value, write_options) :: :ok | {:error, any}
|
@spec put(db_handle, key, value, write_options) :: :ok | {:error, any}
|
||||||
def put(db, key, value, write_opts) when is_list(write_opts) and is_binary(value), do:
|
def put(db, key, value, write_opts) when is_list(write_opts) and is_binary(value), do:
|
||||||
:erocksdb.put(db, key, value, write_opts)
|
:erocksdb.put(db, key, value, write_opts)
|
||||||
|
@ -174,27 +182,30 @@ defmodule Rox do
|
||||||
def put(db, key, value, write_opts) when is_list(write_opts), do:
|
def put(db, key, value, write_opts) when is_list(write_opts), do:
|
||||||
:erocksdb.put(db, key, :erlang.term_to_binary(value), write_opts)
|
:erocksdb.put(db, key, :erlang.term_to_binary(value), write_opts)
|
||||||
|
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Put a key/value pair into the specified column family with optional `write_options`
|
Put a key/value pair into the specified column family with optional `write_options`
|
||||||
|
|
||||||
"""
|
"""
|
||||||
@spec put(db_handle, cf_handle, key, value, write_options) :: :ok | {:error, any}
|
@spec put(db_handle, cf_handle, key, value, write_options) :: :ok | {:error, any}
|
||||||
def put(db, cf, key, value, write_opts \\ [])
|
def put(db, cf, key, value, write_opts \\ [])
|
||||||
|
|
||||||
def put(db, cf, key, value, write_opts) when is_binary(value), do:
|
def put(db, cf, key, value, write_opts) when is_binary(value), do:
|
||||||
:erocksdb.put(db, cf, key, value, write_opts)
|
:erocksdb.put(db, cf, key, value, write_opts)
|
||||||
|
|
||||||
def put(db, cf, key, value, write_opts), do:
|
def put(db, cf, key, value, write_opts), do:
|
||||||
:erocksdb.put(db, cf, key, :erlang.term_to_binary(value), write_opts)
|
:erocksdb.put(db, cf, key, :erlang.term_to_binary(value), write_opts)
|
||||||
|
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Retrieve a key/value pair in the default column family
|
Retrieve a key/value pair in the default column family
|
||||||
|
|
||||||
For non binary terms, you may use `decode: true` to automatically decode the binary back into the term.
|
For non binary terms, you may use `decode: true` to automatically decode the binary back into the term.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@spec get(db_handle, key, read_options) :: {:ok, binary} | {:ok, value} | :not_found | {:error, any}
|
@spec get(db_handle, key, read_options) :: {:ok, binary} | {:ok, value} | :not_found | {:error, any}
|
||||||
def get(db, key, read_opts \\ []) do
|
def get(db, key, read_opts \\ []) do
|
||||||
{ auto_decode, read_opts } = Keyword.pop(read_opts, :decode)
|
{auto_decode, read_opts} = Keyword.pop(read_opts, :decode)
|
||||||
with {:ok, val } <- :erocksdb.get(db, key, read_opts) do
|
with {:ok, val} <- :erocksdb.get(db, key, read_opts) do
|
||||||
if auto_decode do
|
if auto_decode do
|
||||||
{:ok, :erlang.binary_to_term(val)}
|
{:ok, :erlang.binary_to_term(val)}
|
||||||
else
|
else
|
||||||
|
@ -203,14 +214,17 @@ defmodule Rox do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Creates an Elixir stream of the keys within the `db_handle`.
|
Creates an Elixir stream of the keys within the `db_handle`.
|
||||||
"""
|
|
||||||
|
|
||||||
|
"""
|
||||||
@spec stream_keys(db_handle, read_options) :: Enumerable.t
|
@spec stream_keys(db_handle, read_options) :: Enumerable.t
|
||||||
def stream_keys(db, read_opts \\ []) do
|
def stream_keys(db, read_opts \\ []) do
|
||||||
Stream.resource(fn ->
|
Stream.resource(fn ->
|
||||||
{:ok, iter } = :erocksdb.iterator(db, read_opts, :keys_only)
|
{:ok, iter} =
|
||||||
|
:erocksdb.iterator(db, read_opts, :keys_only)
|
||||||
|
|
||||||
{iter, :first}
|
{iter, :first}
|
||||||
end, fn {iter, dir} ->
|
end, fn {iter, dir} ->
|
||||||
case :erocksdb.iterator_move(iter, dir) do
|
case :erocksdb.iterator_move(iter, dir) do
|
||||||
|
@ -223,7 +237,8 @@ defmodule Rox do
|
||||||
end
|
end
|
||||||
|
|
||||||
def stream(db, read_opts \\ []) do
|
def stream(db, read_opts \\ []) do
|
||||||
{auto_decode, read_opts } = Keyword.pop(read_opts, :decode)
|
{auto_decode, read_opts} =
|
||||||
|
Keyword.pop(read_opts, :decode)
|
||||||
|
|
||||||
scan = fn {iter, dir} ->
|
scan = fn {iter, dir} ->
|
||||||
case :erocksdb.iterator_move(iter, dir) do
|
case :erocksdb.iterator_move(iter, dir) do
|
||||||
|
@ -234,7 +249,7 @@ defmodule Rox do
|
||||||
|
|
||||||
scan_or_decode = if auto_decode do
|
scan_or_decode = if auto_decode do
|
||||||
fn arg ->
|
fn arg ->
|
||||||
with {[{key, val}], acc } <-scan.(arg) do
|
with {[{key, val}], acc} <- scan.(arg) do
|
||||||
val = :erlang.binary_to_term(val)
|
val = :erlang.binary_to_term(val)
|
||||||
{[{key, val}], acc}
|
{[{key, val}], acc}
|
||||||
end
|
end
|
||||||
|
@ -244,7 +259,8 @@ defmodule Rox do
|
||||||
end
|
end
|
||||||
|
|
||||||
Stream.resource(fn ->
|
Stream.resource(fn ->
|
||||||
{:ok, iter } = :erocksdb.iterator(db, read_opts)
|
{:ok, iter} =
|
||||||
|
:erocksdb.iterator(db, read_opts)
|
||||||
{iter, :first}
|
{iter, :first}
|
||||||
end, scan_or_decode, fn {iter, _dir} ->
|
end, scan_or_decode, fn {iter, _dir} ->
|
||||||
:erocksdb.iterator_close(iter)
|
:erocksdb.iterator_close(iter)
|
||||||
|
@ -252,8 +268,12 @@ defmodule Rox do
|
||||||
end
|
end
|
||||||
|
|
||||||
defp sanitize_opts(opts) do
|
defp sanitize_opts(opts) do
|
||||||
{ raw, rest } = Keyword.split(opts, @opts_to_convert_to_bitlists)
|
{raw, rest} =
|
||||||
converted = Enum.map(raw, fn {k, val} -> {k, to_charlist(val)} end)
|
Keyword.split(opts, @opts_to_convert_to_bitlists)
|
||||||
|
|
||||||
|
converted =
|
||||||
|
Enum.map(raw, fn {k, val} -> {k, to_charlist(val)} end)
|
||||||
|
|
||||||
Keyword.merge(rest, converted)
|
Keyword.merge(rest, converted)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue