leveldb-haskell-0.6.5: Haskell bindings to LevelDB

Copyright(c) 2012-2013 The leveldb-haskell Authors
LicenseBSD3
Maintainerkim.altintop@gmail.com
Stabilityexperimental
Portabilitynon-portable
Safe HaskellNone
LanguageHaskell2010

Database.LevelDB.MonadResource

Contents

Description

 

Synopsis

Exported Types

data DB Source #

Database handle

Instances

Eq DB Source # 

Methods

(==) :: DB -> DB -> Bool #

(/=) :: DB -> DB -> Bool #

data BatchOp Source #

Batch operation

newtype Comparator Source #

User-defined comparator

data Options Source #

Options when opening a database

Constructors

Options 

Fields

  • blockRestartInterval :: !Int

    Number of keys between restart points for delta encoding of keys.

    This parameter can be changed dynamically. Most clients should leave this parameter alone.

    Default: 16

  • blockSize :: !Int

    Approximate size of user data packed per block.

    Note that the block size specified here corresponds to uncompressed data. The actual size of the unit read from disk may be smaller if compression is enabled.

    This parameter can be changed dynamically.

    Default: 4k

  • cacheSize :: !Int

    Control over blocks (user data is stored in a set of blocks, and a block is the unit of reading from disk).

    If > 0, use the specified cache (in bytes) for blocks. If 0, leveldb will automatically create and use an 8MB internal cache.

    Default: 0

  • comparator :: !(Maybe Comparator)

    Comparator used to defined the order of keys in the table.

    If Nothing, the default comparator is used, which uses lexicographic bytes-wise ordering.

    NOTE: the client must ensure that the comparator supplied here has the same name and orders keys exactly the same as the comparator provided to previous open calls on the same DB.

    Default: Nothing

  • compression :: !Compression

    Compress blocks using the specified compression algorithm.

    This parameter can be changed dynamically.

    Default: Snappy

  • createIfMissing :: !Bool

    If true, the database will be created if it is missing.

    Default: False

  • errorIfExists :: !Bool

    It true, an error is raised if the database already exists.

    Default: False

  • maxOpenFiles :: !Int

    Number of open files that can be used by the DB.

    You may need to increase this if your database has a large working set (budget one open file per 2MB of working set).

    Default: 1000

  • paranoidChecks :: !Bool

    If true, the implementation will do aggressive checking of the data it is processing and will stop early if it detects any errors.

    This may have unforeseen ramifications: for example, a corruption of one DB entry may cause a large number of entries to become unreadable or for the entire DB to become unopenable.

    Default: False

  • writeBufferSize :: !Int

    Amount of data to build up in memory (backed by an unsorted log on disk) before converting to a sorted on-disk file.

    Larger values increase performance, especially during bulk loads. Up to to write buffers may be held in memory at the same time, so you may with to adjust this parameter to control memory usage. Also, a larger write buffer will result in a longer recovery time the next time the database is opened.

    Default: 4MB

  • filterPolicy :: !(Maybe (Either BloomFilter FilterPolicy))
     

Instances

data ReadOptions Source #

Options for read operations

Constructors

ReadOptions 

Fields

  • verifyCheckSums :: !Bool

    If true, all data read from underlying storage will be verified against corresponding checksums.

    Default: False

  • fillCache :: !Bool

    Should the data read for this iteration be cached in memory? Callers may with to set this field to false for bulk scans.

    Default: True

  • useSnapshot :: !(Maybe Snapshot)

    If Just, read as of the supplied snapshot (which must belong to the DB that is being read and which must not have been released). If Nothing, use an implicit snapshot of the state at the beginning of this read operation.

    Default: Nothing

data Snapshot Source #

Snapshot handle

Instances

data WriteOptions Source #

Options for write operations

Constructors

WriteOptions 

Fields

  • sync :: !Bool

    If true, the write will be flushed from the operating system buffer cache (by calling WritableFile::Sync()) before the write is considered complete. If this flag is true, writes will be slower.

    If this flag is false, and the machine crashes, some recent writes may be lost. Note that if it is just the process that crashes (i.e., the machine does not reboot), no writes will be lost even if sync==false.

    In other words, a DB write with sync==false has similar crash semantics as the "write()" system call. A DB write with sync==true has similar crash semantics to a "write()" system call followed by "fsync()".

    Default: False

Defaults

Basic Database Manipulation

withSnapshot :: MonadResource m => DB -> (Snapshot -> m a) -> m a Source #

Run an action with a snapshot of the database.

The snapshot will be released when the action terminates or throws an exception. Note that this function is provided for convenience and does not prevent the Snapshot handle to escape. It will, however, be invalid after this function returns and should not be used anymore.

open :: MonadResource m => FilePath -> Options -> m DB Source #

Open a database

The returned handle will automatically be released when the enclosing runResourceT terminates.

put :: MonadIO m => DB -> WriteOptions -> ByteString -> ByteString -> m () Source #

Write a key/value pair.

delete :: MonadIO m => DB -> WriteOptions -> ByteString -> m () Source #

Delete a key/value pair.

write :: MonadIO m => DB -> WriteOptions -> WriteBatch -> m () Source #

Perform a batch mutation.

get :: MonadIO m => DB -> ReadOptions -> ByteString -> m (Maybe ByteString) Source #

Read a value by key.

createSnapshot :: MonadResource m => DB -> m Snapshot Source #

Create a snapshot of the database.

The returned Snapshot will be released automatically when the enclosing runResourceT terminates. It is recommended to use createSnapshot' instead and release the resource manually as soon as possible.

createSnapshot' :: MonadResource m => DB -> m (ReleaseKey, Snapshot) Source #

Create a snapshot of the database which can (and should) be released early.

Filter Policy / Bloom Filter

data FilterPolicy Source #

User-defined filter policy

Administrative Functions

data Property Source #

Properties exposed by LevelDB

getProperty :: MonadIO m => DB -> Property -> m (Maybe ByteString) Source #

Get a DB property.

destroy :: MonadIO m => FilePath -> Options -> m () Source #

Destroy the given LevelDB database.

The database must not be in use during this operation.

repair :: MonadIO m => FilePath -> Options -> m () Source #

Repair the given LevelDB database.

approximateSize :: MonadIO m => DB -> Range -> m Int64 Source #

Inspect the approximate sizes of the different levels.

compactRange :: MonadIO m => DB -> Range -> m () Source #

Compact the underlying storage for the given Range. In particular this means discarding deleted and overwritten data as well as rearranging the data to reduce the cost of operations accessing the data.

version :: MonadIO m => m (Int, Int) Source #

Return the runtime version of the underlying LevelDB library as a (major, minor) pair.

Iteration

data Iterator Source #

Iterator handle

Note that an Iterator requires external synchronization if it is shared between multiple threads which mutate it's state. See examples/iterforkio.hs for a simple example of how to do that.

Instances

withIterator :: MonadResource m => DB -> ReadOptions -> (Iterator -> m a) -> m a Source #

Run an action with an Iterator. The iterator will be closed after the action returns or an error is thrown. Thus, the iterator will not be valid after this function terminates.

iterOpen :: MonadResource m => DB -> ReadOptions -> m Iterator Source #

Create an Iterator.

The iterator will be released when the enclosing runResourceT terminates. You may consider to use iterOpen' instead and manually release the iterator as soon as it is no longer needed (alternatively, use withIterator).

Note that an Iterator creates a snapshot of the database implicitly, so updates written after the iterator was created are not visible. You may, however, specify an older Snapshot in the ReadOptions.

iterOpen' :: MonadResource m => DB -> ReadOptions -> m (ReleaseKey, Iterator) Source #

Create an Iterator which can be released early.

iterValid :: MonadIO m => Iterator -> m Bool Source #

An iterator is either positioned at a key/value pair, or not valid. This function returns true iff the iterator is valid.

iterSeek :: MonadIO m => Iterator -> ByteString -> m () Source #

Position at the first key in the source that is at or past target. The iterator is valid after this call iff the source contains an entry that comes at or past target.

iterFirst :: MonadIO m => Iterator -> m () Source #

Position at the first key in the source. The iterator is valid after this call iff the source is not empty.

iterLast :: MonadIO m => Iterator -> m () Source #

Position at the last key in the source. The iterator is valid after this call iff the source is not empty.

iterNext :: MonadIO m => Iterator -> m () Source #

Moves to the next entry in the source. After this call, iterValid is true iff the iterator was not positioned at the last entry in the source.

If the iterator is not valid, this function does nothing. Note that this is a shortcoming of the C API: an iterPrev might still be possible, but we can't determine if we're at the last or first entry.

iterPrev :: MonadIO m => Iterator -> m () Source #

Moves to the previous entry in the source. After this call, iterValid is true iff the iterator was not positioned at the first entry in the source.

If the iterator is not valid, this function does nothing. Note that this is a shortcoming of the C API: an iterNext might still be possible, but we can't determine if we're at the last or first entry.

iterKey :: MonadIO m => Iterator -> m (Maybe ByteString) Source #

Return the key for the current entry if the iterator is currently positioned at an entry, ie. iterValid.

iterValue :: MonadIO m => Iterator -> m (Maybe ByteString) Source #

Return the value for the current entry if the iterator is currently positioned at an entry, ie. iterValid.

iterGetError :: MonadIO m => Iterator -> m (Maybe ByteString) Source #

Check for errors

Note that this captures somewhat severe errors such as a corrupted database.

Re-exports

class (MonadThrow m, MonadIO m, Applicative m, MonadBase IO m) => MonadResource m where #

A Monad which allows for safe resource allocation. In theory, any monad transformer stack which includes a ResourceT can be an instance of MonadResource.

Note: runResourceT has a requirement for a MonadBaseControl IO m monad, which allows control operations to be lifted. A MonadResource does not have this requirement. This means that transformers such as ContT can be an instance of MonadResource. However, the ContT wrapper will need to be unwrapped before calling runResourceT.

Since 0.3.0

Minimal complete definition

liftResourceT

Methods

liftResourceT :: ResourceT IO a -> m a #

Lift a ResourceT IO action into the current Monad.

Since 0.4.0

Instances

MonadResource m => MonadResource (ListT m) 

Methods

liftResourceT :: ResourceT IO a -> ListT m a #

(MonadThrow m, MonadBase IO m, MonadIO m, Applicative m) => MonadResource (ResourceT m) 

Methods

liftResourceT :: ResourceT IO a -> ResourceT m a #

MonadResource m => MonadResource (MaybeT m) 

Methods

liftResourceT :: ResourceT IO a -> MaybeT m a #

(Error e, MonadResource m) => MonadResource (ErrorT e m) 

Methods

liftResourceT :: ResourceT IO a -> ErrorT e m a #

MonadResource m => MonadResource (ExceptT e m) 

Methods

liftResourceT :: ResourceT IO a -> ExceptT e m a #

MonadResource m => MonadResource (StateT s m) 

Methods

liftResourceT :: ResourceT IO a -> StateT s m a #

MonadResource m => MonadResource (StateT s m) 

Methods

liftResourceT :: ResourceT IO a -> StateT s m a #

(Monoid w, MonadResource m) => MonadResource (WriterT w m) 

Methods

liftResourceT :: ResourceT IO a -> WriterT w m a #

(Monoid w, MonadResource m) => MonadResource (WriterT w m) 

Methods

liftResourceT :: ResourceT IO a -> WriterT w m a #

MonadResource m => MonadResource (IdentityT * m) 

Methods

liftResourceT :: ResourceT IO a -> IdentityT * m a #

MonadResource m => MonadResource (ContT * r m) 

Methods

liftResourceT :: ResourceT IO a -> ContT * r m a #

MonadResource m => MonadResource (ReaderT * r m) 

Methods

liftResourceT :: ResourceT IO a -> ReaderT * r m a #

(Monoid w, MonadResource m) => MonadResource (RWST r w s m) 

Methods

liftResourceT :: ResourceT IO a -> RWST r w s m a #

(Monoid w, MonadResource m) => MonadResource (RWST r w s m) 

Methods

liftResourceT :: ResourceT IO a -> RWST r w s m a #

runResourceT :: MonadBaseControl IO m => ResourceT m a -> m a #

Unwrap a ResourceT transformer, and call all registered release actions.

Note that there is some reference counting involved due to resourceForkIO. If multiple threads are sharing the same collection of resources, only the last call to runResourceT will deallocate the resources.

Since 0.3.0

resourceForkIO :: MonadBaseControl IO m => ResourceT m () -> ResourceT m ThreadId #

Launch a new reference counted resource context using forkIO.

This is defined as resourceForkWith forkIO.

Since: 0.3.0