| Copyright | (c) 2014 Kim Altintop |
|---|---|
| License | BSD3 |
| Maintainer | kim.altintop@gmail.com |
| Stability | experimental |
| Portability | non-portable |
| Safe Haskell | None |
| Language | Haskell2010 |
Database.LevelDB.Streaming
Contents
Description
High-level, Data.List-like, streaming interface to Database.LevelDB.Iterator.
This module contains types and functions to construct Streams from
Iterators, and re-exports the functions operating
on Streams from Data.Stream.Monadic.
Note that most of the functions from the latter module are (intentionally) conflicting with the Prelude, it is thus recommended to import this module qualified:
import Database.LevelDB -- or Database.LevelDB.Base import qualified Database.LevelDB.Streaming as S
- data KeyRange
- = KeyRange {
- start :: !ByteString
- end :: ByteString -> Ordering
- | AllKeys
- = KeyRange {
- data Direction
- type Key = ByteString
- type Value = ByteString
- type Entry = (Key, Value)
- keySlice :: (Applicative m, MonadIO m) => Iterator -> KeyRange -> Direction -> Stream m Key
- entrySlice :: (Applicative m, MonadIO m) => Iterator -> KeyRange -> Direction -> Stream m Entry
- module Data.Stream.Monadic
Documentation
Constructors
| KeyRange | |
Fields
| |
| AllKeys | |
type Key = ByteString Source
type Value = ByteString Source
Constructing streams
Caveats:
- remember that
Iterators are not threadsafe - consider that traversing a
Streammutates the underlyingIterator, so the above applies to theStreamas well - because of the destructive update semantics of
Iterators, the following example will (perhaps obviously) not work as expected:
withIter db def $ \ i ->
toList $ zip (keySlice i AllKeys Asc) (keySlice i AllKeys Desc)However, the following will work:
withIter db def $ \ i ->
foldl (+) 0 . map (*2) . map ByteString.length $ keySlice i AllKeys AscHere, fusion ensures the key slice is traversed only once, while the next
example will incur rewinding the Iterator and traversing it a second time:
withIter db def $ \ i ->
let slice = keySlice i AllKeys Asc
count f = foldl' (\ c k -> c + f k) 0
in liftM2 (+) (count ByteString.length slice) (count (const 1) slice)To summarise: it is recommended to always create Streams with their own
exclusive Iterator, and to not share them across threads.
entrySlice :: (Applicative m, MonadIO m) => Iterator -> KeyRange -> Direction -> Stream m Entry Source
Re-exports
module Data.Stream.Monadic