package leveldb

  1. Overview
  2. Docs

Module LevelDBSource

Access to leveldb databases.

Usage in a concurrent setting

* * A database may only be opened by one process at a time; this is enforced * by LevelDB by using a file lock. Within a process, operations on a value * of type db (such as get, put, iterator, Iterator.make or * Snapshot.make) can be performed concurrently in different threads. * Values of type iterator, writebatch, snapshot and read_access must * not be used simultaneously from two different threads, so external * synchronization (e.g. using Mutex) is required. * * As an exception to the above rule, it is possible to close a db with * iterator, snapshot or read_access values in use. Values of these * types can also be released/closed in a thread while they are being used in * another. In both cases, the thread that is releasing/closing the value * will wait until the current operation is finished and invalidate the value * so that any further operations on it will fail.

Exceptions

Sourceexception Error of string

Errors (apart from Not_found) are notified with Error s exceptions.

Types

Sourcetype db

Database

Sourcetype iterator

Database iterators.

Sourcetype writebatch

Batch write operations.

Sourcetype snapshot

Immutable database snapshots.

Sourcetype read_access

Read-only access to the DB or a snapshot.

Sourcetype comparator

Type that represents a const Comparator* pointer (refer to * LevelDB's comparator.h). If you want to define your own, * use an external function of type unit -> comparator * returning the pointer.

Sourcetype env

Type that represents a const Env* pointer (refer to * LevelDB's options.h). If you want to define your own, * use an external function of type unit -> env * returning the pointer.

Database maintenance

Sourceval destroy : string -> bool

Destroy the contents of the database in the given directory. *

  • returns

    true if the operation succeeded.

Sourceval repair : string -> bool

If a DB cannot be opened, you may attempt to call this method to resurrect * as much of the contents of the database as possible. Some data may be * lost, so be careful when calling this function on a database that contains * important information. *

  • returns

    true if the operation succeeded.

Database operations

Sourceval default_env : env
Sourceval lexicographic_comparator : comparator
Sourceval open_db : ?write_buffer_size:int -> ?max_open_files:int -> ?block_size:int -> ?block_restart_interval:int -> ?comparator:comparator -> ?cache_size:int -> ?env:env -> string -> db

Open a leveldb database in the given directory. *

  • parameter cache_size

    size of LRU cache in MB (no cache if not given) *

Sourceval close : db -> unit

Close the database. All further operations on it will fail. * Existing snapshots, read_access values and iterators are released and * invalidated. If such values are being used in a concurrent thread, * the current thread will wait until the operation is finished and then * proceed to invalidate the value, making any further uses on it fail, and * release it. * Note that the database is closed automatically in the finalizer if you * don't close it manually.

Sourceval read_access : db -> read_access

Read-only access to the DB.

Sourceval iterator : db -> iterator

Return a new iterator. Refer to Iterator.make.

Sourceval get_approximate_size : db -> string -> string -> Int64.t

get_approximate_size from_key to_key returns the approximate size * on disk of the range comprised between from_key and to_key.

Sourceval get_property : db -> string -> string option

Return the specified property, if existent.

Sourceval compact_range : db -> from_key:string option -> to_key:string option -> unit

Compact specified range. None is treated as a key before (resp. after) * all keys in the database; therefore compact_range db None None will * compact the whole DB.

Read/write

Note that in the following functions the contents of the key will be * copied to the stack, so exceedingly large keys could cause a stack * overflow.

Sourceval get : db -> string -> string option

Retrieve a value.

Sourceval get_exn : db -> string -> string

Retrieve a value, raising Not_found if missing.

Sourceval mem : db -> string -> bool

mem db key returns true iff key is present in db.

Sourceval put : db -> ?sync:bool -> string -> string -> unit

put ?sync key value adds (or replaces) a binding to the database. *

  • parameter sync

    whether to write synchronously (default: false)

Sourceval delete : db -> ?sync:bool -> string -> unit

delete ?sync key deletes the binding for the given key. *

  • parameter sync

    whether to write synchronously (default: false)

Iteration

Sourceval iter : (string -> string -> bool) -> db -> unit

iter f db applies f to all the bindings in db until it returns * false, i.e. runs f key value for all the bindings in lexicographic * key order.

Sourceval rev_iter : (string -> string -> bool) -> db -> unit

Like iter, but proceed in reverse lexicographic order.

Sourceval iter_from : (string -> string -> bool) -> db -> string -> unit

iter_from f db start applies f key value for all the bindings after * start (inclusive) until it returns false.

Sourceval rev_iter_from : (string -> string -> bool) -> db -> string -> unit

iter_from f db start applies f key value for all the bindings before * start (inclusive) in reverse lexicographic order until f returns * false..

Batch operations

Sourcemodule Batch : sig ... end

Batch operations applied atomically.

Iterators

Sourcemodule Iterator : sig ... end

Iteration over bindings in a database.

Snapshots

Sourcemodule Snapshot : sig ... end

Access to database snapshots. * Note that the functions that accept a key will copy its contents to the * stack, so exceedingly large keys could cause a stack overflow.

Abstract read-only access

Sourcemodule Read_access : sig ... end

Read-only access to databases and snapshots. * Note that the functions that accept a key will copy its contents to the * stack, so exceedingly large keys could cause a stack overflow.

OCaml

Innovation. Community. Security.