diff --git a/README.md b/README.md index 8a91c37..239668b 100644 --- a/README.md +++ b/README.md @@ -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`) diff --git a/lib/rox.ex b/lib/rox.ex index 105a7d9..0099692 100644 --- a/lib/rox.ex +++ b/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)