mirror of https://github.com/poanetwork/rox.git
feat(): add auto-encoding of non-binaries
This commit is contained in:
parent
0cf7b74375
commit
b5a7465f1b
|
@ -14,3 +14,9 @@ If [available in Hex](https://hex.pm/docs/publish), the package can be installed
|
|||
[{:rox, "~> 0.1.0"}]
|
||||
end
|
||||
```
|
||||
|
||||
## Features
|
||||
|
||||
* String friendly wrapping around erlang char lists
|
||||
* Auto encoding of non-binary types (tuples, maps, lists, etc) via
|
||||
`:erlang.term_to_binary/1`. (Use `decode: true` on `get`)
|
||||
|
|
24
lib/rox.ex
24
lib/rox.ex
|
@ -143,30 +143,48 @@ defmodule Rox do
|
|||
:erocksdb.open(to_charlist(path), sanitize_opts(db_opts), sanitize_opts(cf_opts))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Close the RocksDB with the specifed `db_handle`
|
||||
"""
|
||||
|
||||
@spec close(db_handle) :: :ok | {:error, any}
|
||||
def close(db), do:
|
||||
:erocksdb.close(db)
|
||||
|
||||
@doc """
|
||||
Put a key/value pair into the default column family handle
|
||||
"""
|
||||
|
||||
@spec put(db_handle, key, value) :: :ok | {:error, any}
|
||||
def put(db, key, value), do:
|
||||
def put(db, key, value) when is_binary(value), do:
|
||||
:erocksdb.put(db, key, value, [])
|
||||
|
||||
def put(db, key, value), do:
|
||||
:erocksdb.put(db, key, :erlang.term_to_binary(value), [])
|
||||
|
||||
@doc """
|
||||
Put a key/value pair into the default column family handle with the provided
|
||||
write options
|
||||
"""
|
||||
|
||||
@spec put(db_handle, key, value, write_options) :: :ok | {:error, any}
|
||||
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) and is_binary(value), do:
|
||||
:erocksdb.put(db, key, value, write_opts)
|
||||
|
||||
def put(db, key, value, write_opts) when is_list(write_opts), do:
|
||||
:erocksdb.put(db, key, :erlang.term_to_binary(value), write_opts)
|
||||
|
||||
@doc """
|
||||
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}
|
||||
def put(db, cf, key, value, write_opts \\ []), do:
|
||||
def put(db, cf, key, value, write_opts \\ [])
|
||||
def put(db, cf, key, value, write_opts) when is_binary(value), do:
|
||||
:erocksdb.put(db, cf, key, value, write_opts)
|
||||
|
||||
def put(db, cf, key, value, write_opts), do:
|
||||
:erocksdb.put(db, cf, key, :erlang.term_to_binary(value), write_opts)
|
||||
|
||||
defp sanitize_opts(opts) do
|
||||
[ raw, rest ] = Keyword.split(opts, @opts_to_convert_to_bitlists)
|
||||
converted = Enum.map(raw, fn {k, val} -> {k, to_charlist(val)} end)
|
||||
|
|
Loading…
Reference in New Issue