package leveldb

  1. Overview
  2. Docs

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

exception Error of string

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

Types

type db

Database

type iterator

Database iterators.

type writebatch

Batch write operations.

type snapshot

Immutable database snapshots.

type read_access

Read-only access to the DB or a snapshot.

type 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.

type 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

val destroy : string -> bool

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

  • returns

    true if the operation succeeded.

val 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

val default_env : env
val lexicographic_comparator : comparator
val 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) *

val 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.

val read_access : db -> read_access

Read-only access to the DB.

val iterator : db -> iterator

Return a new iterator. Refer to Iterator.make.

val 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.

val get_property : db -> string -> string option

Return the specified property, if existent.

val 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.

val get : db -> string -> string option

Retrieve a value.

val get_exn : db -> string -> string

Retrieve a value, raising Not_found if missing.

val mem : db -> string -> bool

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

val 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)

val 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

val 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.

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

Like iter, but proceed in reverse lexicographic order.

val 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.

val 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

module Batch : sig ... end

Batch operations applied atomically.

Iterators

module Iterator : sig ... end

Iteration over bindings in a database.

Snapshots

module 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

module 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.