Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
546 lines (321 sloc) 14.8 KB

Module cowdb

Behaviours: gen_server.

Data Types

compression_method()


compression_method() = snappy | lz4 | gzip | {deflate, Level::integer()} | none

cow_mfa()


cow_mfa() = {local, Name::atom()} | {global, GlobalName::term()} | {via, ViaName::term()}

db()


db() = #db{} | pid()

fold_options()


fold_options() = [{dir, fwd | rev} | {start_key, term()} | {end_key, term()} | {end_key_gt, term()} | {key_group_fun, function()}]

fsync_options()


fsync_options() = [before_header | after_header | on_file_open]

open_options()


open_options() = [{compression, compression_method()} | {fsync_options, fsync_options()} | auto_compact | {auto_compact, boolean()} | {compact_limit, integer()} | {reduce, function()} | {less, function()} | {init_func, function()}]

transact_fn()


transact_fn() = {module(), function(), [any()]} | {module(), function()} | function()

transact_id()


transact_id() = integer() | tx_end

transact_ops()


transact_ops() = [{add, term(), any()} | {remove, term()} | {fn, transact_fn()}]

Function Index

cancel_compact/1cancel compaction.
close/1Close the file.
compact/1compact the database file.
count/1get the number of objects stored in the database.
data_size/1get the total size of the objects stored in the database.
database_info/1returns database info.
delete/2delete one object from the store.
drop_db/1delete a database.
drop_db/2delete a database asynchronously or not.
fold/3fold all objects form the database.
fold/4fold all objects form the database with range options.
fold_reduce/4fold the reduce function over the results.
full_reduce/1return the full reduced value.
get/2get an object by the specified key.
get_snapshot/2get a snapshot of the database at some point.
log/4fold the transaction log.
log/5fold the transaction log Args:
  • Db: the db value (in transaction function) or pid
  • StartT: transaction ID to start from
  • EndT: transaction ID to stop
  • Fun: function collection log result:
      fun({TransactId, Op, {K,V}, Ts}, Acc) ->
           {ok, Acc2} | {stop, Acc2}
       end
    where TransactId is the transaction ID Transactid where the OP (add or remove) on the Key/Value pair {K, V} has been run on the unix time Ts.
  • Acc: initial value to pass to the function.
The function return the total number of transactions in the range and the values collected during folding.
lookup/2deprecated: use mget/2 instead.
mdelete/2delete multiple object at once.
mget/2get a list of objects by the specified key.
mput/2add multiple objects to a store.
open/1open a cowdb database, pass a function to initialise the stores and indexes.
open/2open a cowdb database, pass a function to initialise the stores and indexes.
open/3Create or open a cowdb store with a registered name.
open_link/1open a cowdb database as part of the supervision tree, pass a function to initialise the stores and indexes.
open_link/2open a cowdb database as part of the supervision tree.
open_link/3open a cowdb database as part of the supervision tree with a registered name.
put/2add one object to a store.
put/3add one object to a store.
transact/2execute a transaction A transaction received operations to execute as a list:
  • {add, Key, Value} to add an object
  • {remove, Key} to remove a value
  • {fn, Func} a transaction function. A transaction function received the db value like it was at the beginning of the transaction as an argument. It's possible to pass arguments to it. A transaction function return a list of operations and can query/manipulate function. The list of operations returned can also contain a function.
.
transact/3

Function Details

cancel_compact/1


cancel_compact(Ref::db()) -> ok

cancel compaction

close/1


close(DbPid::pid()) -> ok

Close the file.

compact/1


compact(Ref::db()) -> ok | {error, term()}

compact the database file

count/1


count(DbPid::db()) -> {ok, integer()} | {error, term()}

get the number of objects stored in the database.

data_size/1


data_size(DbPid::db()) -> {ok, integer()} | {error, term()}

get the total size of the objects stored in the database.

database_info/1


database_info(DbPid::db()) -> {ok, list()}

returns database info

delete/2


delete(Db::db(), Key::term()) -> {ok, transact_id()} | {error, term()}

delete one object from the store

drop_db/1


drop_db(DbPid::db()) -> ok | {error, term()}

delete a database

drop_db/2


drop_db(DbPid::db(), Async::boolean()) -> ok | {error, term()}

delete a database asynchronously or not

fold/3


fold(DbPid::db(), Fun::function(), Acc::any()) -> {ok, any(), any()} | {error, term()}

fold all objects form the database

fold/4


fold(DbPid::db(), Fun::function(), Acc::any(), Options::fold_options()) -> {ok, any()} | {error, term()}

fold all objects form the database with range options

fold_reduce/4

fold_reduce(DbPid, Fun, Acc, Options) -> any()

fold the reduce function over the results.

full_reduce/1


full_reduce(DbPid::db()) -> {ok, any()}

return the full reduced value

get/2


get(Db::db(), Key::any()) -> {ok, any()} | {error, term()}

get an object by the specified key

get_snapshot/2


get_snapshot(DbPid::db(), TransactId::transact_id()) -> {ok, db()} | {error, term()}

get a snapshot of the database at some point.

log/4


log(Db::db(), StartT::transact_id(), Function::function(), Acc::any()) -> {ok, NbTransactions::integer(), Acc2::any()} | {error, term()}

fold the transaction log

log/5


log(Db::db(), StartT::transact_id(), EndT::transact_id(), Function::function(), Acc::any()) -> {ok, NbTransactions::integer(), Acc2::any()} | {error, term()}

fold the transaction log Args:

  • Db: the db value (in transaction function) or pid

  • StartT: transaction ID to start from

  • EndT: transaction ID to stop

  • Fun: function collection log result:

  fun({TransactId, Op, {K,V}, Ts}, Acc) ->
       {ok, Acc2} | {stop, Acc2}
   end

where TransactId is the transaction ID Transactid where the OP (add or remove) on the Key/Value pair {K, V} has been run on the unix time Ts.

  • Acc: initial value to pass to the function.

The function return the total number of transactions in the range and the values collected during folding.

lookup/2


lookup(Db::db(), Keys::[any()]) -> {ok, any()} | {error, term()}

deprecated: use mget/2 instead.

mdelete/2


mdelete(Db::db(), Keys::[term()]) -> {ok, transact_id()} | {error, term()}

delete multiple object at once

mget/2


mget(Db::db(), Keys::[any()]) -> {ok, any()} | {error, term()}

get a list of objects by the specified key

mput/2


mput(Db::db(), KVs::[{term(), any()}]) -> {ok, transact_id()} | {error, term()}

add multiple objects to a store

open/1


open(FilePath::string()) -> {ok, Db::pid()} | {error, term()}

open a cowdb database, pass a function to initialise the stores and indexes.

open/2


open(FilePath::string(), Option::open_options()) -> {ok, Db::pid()} | {error, term()}

open a cowdb database, pass a function to initialise the stores and indexes.

open/3


open(Name::cow_mfa(), FilePath::string(), Option::open_options()) -> {ok, Db::pid()} | {error, term()}

Create or open a cowdb store with a registered name.

open_link/1


open_link(FilePath::string()) -> {ok, Db::pid()} | {error, term()}

open a cowdb database as part of the supervision tree, pass a function to initialise the stores and indexes.

open_link/2


open_link(FilePath::string(), Option::open_options()) -> {ok, Db::pid()} | {error, term()}

open a cowdb database as part of the supervision tree

open_link/3


open_link(Name::cow_mfa(), FilePath::string(), Option::open_options()) -> {ok, Db::pid()} | {error, term()}

open a cowdb database as part of the supervision tree with a registered name

put/2


put(DbPid::db(), X2::{term(), any()}) -> {ok, transact_id()} | {error, term()}

add one object to a store

put/3


put(DbPid::db(), Key::term(), Value::any()) -> {ok, transact_id()} | {error, term()}

add one object to a store

transact/2


transact(Ref::db(), OPs::transact_ops()) -> {ok, transact_id()} | {error, term()}

execute a transaction A transaction received operations to execute as a list:

  • {add, Key, Value} to add an object

  • {remove, Key} to remove a value

  • {fn, Func} a transaction function. A transaction function received the db value like it was at the beginning of the transaction as an argument. It's possible to pass arguments to it. A transaction function return a list of operations and can query/manipulate function. The list of operations returned can also contain a function.

transact/3


transact(Ref::db(), OPs::transact_ops(), Timeout::timeout()) -> {ok, transact_id()} | {error, term()}