rocksdb-haskell 0.1.0 → 1.0.0
raw patch · 11 files changed
+335/−521 lines, 11 filesdep +QuickCheckdep +binarydep +hspecdep ~bytestringdep ~resourcetnew-uploader
Dependencies added: QuickCheck, binary, hspec, hspec-expectations, process, rocksdb-haskell, temporary
Dependency ranges changed: bytestring, resourcet
Files
- README.md +47/−0
- Readme.md +0/−47
- rocksdb-haskell.cabal +27/−10
- src/Database/RocksDB.hs +2/−2
- src/Database/RocksDB/Base.hs +82/−18
- src/Database/RocksDB/C.hsc +70/−67
- src/Database/RocksDB/Internal.hs +12/−40
- src/Database/RocksDB/Iterator.hs +47/−18
- src/Database/RocksDB/MonadResource.hs +0/−279
- src/Database/RocksDB/Types.hs +14/−40
- tests/tests.hs +34/−0
+ README.md view
@@ -0,0 +1,47 @@+This library provides Haskell bindings to+[RocksDB](http://rocksdb.org)++[](https://travis-ci.org/serokell/rocksdb-haskell)+[](https://ci.appveyor.com/project/jagajaga/rocksdb-haskell)++## History++Version 0.1.0:++* initial release of this fork.++## Installation++Prerequisites:++* [GHC 8.*](http://www.haskell.org/ghc)+* [Cabal](http://www.haskell.org/cabal), version 1.3 or higher+* [RocksDB](http://rocksdb.org)+* Optional: [Snappy](http://code.google.com/p/snappy),+ if compression support is desired++To install the latest version from hackage:++```shell+$ cabal install rocksdb-haskell+```++To install from checked-out source:++```shell+$ cabal install+```++## Notes++This library is in very early stage and has seen very limited testing. Comments+and contributions are welcome.++## Bugs and Contributing++Please report issues via http://github.com/serokell/rocksdb-haskell/issues.<br />+Patches are best submitted as pull requests.++## License++BSD 3, see LICENSE file.
− Readme.md
@@ -1,47 +0,0 @@-This library provides Haskell bindings to-[RocksDB](http://rocksdb.org)--[](https://drone.io/github.com/agrafix/rocksdb-haskell/latest)--## History--Version 0.1.x:--* initial fork of leveldb-haskell--## Installation--Prerequisites:--* [GHC 7.*](http://www.haskell.org/ghc)-* [Cabal](http://www.haskell.org/cabal), version 1.3 or higher-* [RocksDB](http://rocksdb.org)-* Optional: [Snappy](http://code.google.com/p/snappy),- if compression support is desired--To install the latest version from hackage:--```shell-$ cabal install rocksdb-haskell-```--To install from checked-out source:--```shell-$ cabal install-```--## Notes--This library is in very early stage and has seen very limited testing. Comments-and contributions are welcome.--## Bugs and Contributing--Please report issues via http://github.com/agrafix/rocksdb-haskell/issues.<br />-Patches are best submitted as pull requests, or via email-(mail@agrafix.net).--## License--BSD 3, see LICENSE file.
rocksdb-haskell.cabal view
@@ -1,29 +1,29 @@ name: rocksdb-haskell-version: 0.1.0+version: 1.0.0 synopsis: Haskell bindings to RocksDB-homepage: http://github.com/agrafix/rocksdb-haskell-bug-reports: http://github.com/agrafix/rocksdb-haskell/issues+homepage: http://github.com/serokell/rocksdb-haskell+bug-reports: http://github.com/serokell/rocksdb-haskell/issues license: BSD3 license-file: LICENSE author: Kim Altintop, Alexander Thiemann et.al. (see AUTHORS file)-maintainer: mail@agrafix.net+maintainer: Serokell <hi@serokell.io> copyright: Copyright (c) 2012-2014 The leveldb-haskell Authors, Copyright (c) 2014 The rocksdb-haskell Authors category: Database, FFI stability: Experimental build-type: Simple cabal-version: >=1.10-tested-with: GHC == 7.2.2, GHC == 7.4.1+tested-with: GHC == 8.0.1 description: From <http://rocksdb.org>: . RocksDB is an embeddable persistent key-value store for fast storage. RocksDB can also be the foundation for a client-server database but our current focus is on embedded workloads. . RocksDB builds on LevelDB to be scalable to run on servers with many CPU cores, to efficiently use fast storage, to support IO-bound, in-memory and write-once workloads, and to be flexible to allow for innovation.-extra-source-files: Readme.md, AUTHORS+extra-source-files: README.md, AUTHORS source-repository head type: git- location: git://github.com/agrafix/rocksdb-haskell.git+ location: git://github.com/serokell/rocksdb-haskell.git library exposed-modules: Database.RocksDB@@ -31,7 +31,6 @@ , Database.RocksDB.C , Database.RocksDB.Internal , Database.RocksDB.Iterator- , Database.RocksDB.MonadResource , Database.RocksDB.Types default-language: Haskell2010@@ -41,15 +40,33 @@ , RecordWildCards build-depends: base >= 3 && < 5+ , binary , bytestring , data-default , filepath , resourcet > 0.3.2 , transformers - ghc-options: -Wall -rtsopts -funbox-strict-fields- ghc-prof-options: -prof -auto-all+ ghc-options: -Wall -funbox-strict-fields hs-source-dirs: src extra-libraries: rocksdb++test-suite rocksdb-tests+ ghc-options: -Wall+ main-is: tests.hs+ type: exitcode-stdio-1.0+ hs-source-dirs: tests+ build-depends: base >= 3 && < 5+ , rocksdb-haskell+ , hspec >= 1.8+ , process >= 1.1.0.2+ , bytestring >= 0.10.4.0+ , data-default+ , resourcet+ , transformers+ , temporary+ , hspec-expectations+ , QuickCheck+ default-language: Haskell2010
src/Database/RocksDB.hs view
@@ -12,6 +12,6 @@ -- The API closely follows the C-API of RocksDB. -- For more information, see: <http://rocksdb.org> -module Database.RocksDB (module R) where+module Database.RocksDB (module Base) where -import Database.RocksDB.MonadResource as R+import Database.RocksDB.Base as Base
src/Database/RocksDB/Base.hs view
@@ -32,12 +32,18 @@ -- * Basic Database Manipulations , open+ , openBracket , close , put+ , putBinaryVal+ , putBinary , delete , write , get+ , getBinary+ , getBinaryVal , withSnapshot+ , withSnapshotBracket , createSnapshot , releaseSnapshot @@ -46,6 +52,7 @@ , BloomFilter , createBloomFilter , releaseBloomFilter+ , bloomFilter -- * Administrative Functions , Property (..), getProperty@@ -55,34 +62,74 @@ -- * Iteration , module Database.RocksDB.Iterator- )-where+ ) where -import Control.Applicative ((<$>))-import Control.Exception (bracket, bracketOnError, finally)-import Control.Monad (liftM)-import Control.Monad.IO.Class (MonadIO (liftIO))-import Data.ByteString (ByteString)-import Data.ByteString.Internal (ByteString (..))+import Control.Applicative ((<$>))+import Control.Exception (bracket, bracketOnError, finally)+import Control.Monad (liftM)++import Control.Monad.IO.Class (MonadIO (liftIO))+import Control.Monad.Trans.Resource (MonadResource (..), ReleaseKey, allocate,+ release)+import Data.Binary (Binary)+import qualified Data.Binary as Binary+import Data.ByteString (ByteString)+import Data.ByteString.Internal (ByteString (..))+import qualified Data.ByteString.Lazy as BSL import Foreign-import Foreign.C.String (withCString)+import Foreign.C.String (withCString) import Database.RocksDB.C import Database.RocksDB.Internal import Database.RocksDB.Iterator import Database.RocksDB.Types -import qualified Data.ByteString as BS-import qualified Data.ByteString.Unsafe as BU+import qualified Data.ByteString as BS+import qualified Data.ByteString.Unsafe as BU +-- | Create a 'BloomFilter'+bloomFilter :: MonadResource m => Int -> m BloomFilter+bloomFilter i =+ snd <$> allocate (createBloomFilter i)+ releaseBloomFilter +-- | Open a database+--+-- The returned handle will automatically be released when the enclosing+-- 'runResourceT' terminates.+openBracket :: MonadResource m => FilePath -> Options -> m (ReleaseKey, DB)+openBracket path opts = allocate (open path opts) close+{-# INLINE openBracket #-}++-- | 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.+withSnapshotBracket :: MonadResource m => DB -> (Snapshot -> m a) -> m a+withSnapshotBracket db f = do+ (rk, snap) <- createSnapshotBracket db+ res <- f snap+ release rk+ return res++-- | 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.+-- Can be released early.+createSnapshotBracket :: MonadResource m => DB -> m (ReleaseKey, Snapshot)+createSnapshotBracket db = allocate (createSnapshot db) (releaseSnapshot db)+ -- | Open a database. -- -- The returned handle should be released with 'close'. open :: MonadIO m => FilePath -> Options -> m DB open path opts = liftIO $ bracketOnError (mkOpts opts) freeOpts mkDB where- mkDB opts'@(Options' opts_ptr _ _ _) =+ mkDB opts'@(Options' opts_ptr _ _) = withCString path $ \path_ptr -> liftM (`DB` opts') $ throwIfErr "open"@@ -125,18 +172,18 @@ if val_ptr == nullPtr then return Nothing else do res <- Just <$> BS.packCString val_ptr- free val_ptr+ freeCString val_ptr return res where prop (NumFilesAtLevel i) = "rocksdb.num-files-at-level" ++ show i- prop Stats = "rocksdb.stats"- prop SSTables = "rocksdb.sstables"+ prop Stats = "rocksdb.stats"+ prop SSTables = "rocksdb.sstables" -- | Destroy the given RocksDB database. destroy :: MonadIO m => FilePath -> Options -> m () destroy path opts = liftIO $ bracket (mkOpts opts) freeOpts destroy' where- destroy' (Options' opts_ptr _ _ _) =+ destroy' (Options' opts_ptr _ _) = withCString path $ \path_ptr -> throwIfErr "destroy" $ c_rocksdb_destroy_db opts_ptr path_ptr @@ -144,7 +191,7 @@ repair :: MonadIO m => FilePath -> Options -> m () repair path opts = liftIO $ bracket (mkOpts opts) freeOpts repair' where- repair' (Options' opts_ptr _ _ _) =+ repair' (Options' opts_ptr _ _) = withCString path $ \path_ptr -> throwIfErr "repair" $ c_rocksdb_repair_db opts_ptr path_ptr @@ -171,7 +218,12 @@ where toInt64 = return . fromIntegral +putBinaryVal :: (MonadIO m, Binary v) => DB -> WriteOptions -> ByteString -> v -> m ()+putBinaryVal db wopts key val = put db wopts key (binaryToBS val) +putBinary :: (MonadIO m, Binary k, Binary v) => DB -> WriteOptions -> k -> v -> m ()+putBinary db wopts key val = put db wopts (binaryToBS key) (binaryToBS val)+ -- | Write a key/value pair. put :: MonadIO m => DB -> WriteOptions -> ByteString -> ByteString -> m () put (DB db_ptr _) opts key value = liftIO $ withCWriteOpts opts $ \opts_ptr ->@@ -182,6 +234,12 @@ key_ptr (intToCSize klen) val_ptr (intToCSize vlen) +getBinaryVal :: (Binary v, MonadIO m) => DB -> ReadOptions -> ByteString -> m (Maybe v)+getBinaryVal db ropts key = fmap bsToBinary <$> get db ropts key++getBinary :: (MonadIO m, Binary k, Binary v) => DB -> ReadOptions -> k -> m (Maybe v)+getBinary db ropts key = fmap bsToBinary <$> get db ropts (binaryToBS key)+ -- | Read a value by key. get :: MonadIO m => DB -> ReadOptions -> ByteString -> m (Maybe ByteString) get (DB db_ptr _) opts key = liftIO $ withCReadOpts opts $ \opts_ptr ->@@ -194,7 +252,7 @@ then return Nothing else do res' <- Just <$> BS.packCStringLen (val_ptr, cSizeToInt vlen)- free val_ptr+ freeCString val_ptr return res' -- | Delete a key/value pair.@@ -243,3 +301,9 @@ releaseBloomFilter :: MonadIO m => BloomFilter -> m () releaseBloomFilter (BloomFilter fp) = liftIO $ c_rocksdb_filterpolicy_destroy fp++binaryToBS :: Binary v => v -> ByteString+binaryToBS x = BSL.toStrict (Binary.encode x)++bsToBinary :: Binary v => ByteString -> v+bsToBinary x = Binary.decode (BSL.fromStrict x)
src/Database/RocksDB/C.hsc view
@@ -14,7 +14,11 @@ import Foreign.C.Types import Foreign.C.String +#ifdef mingw32_HOST_OS+#include <rocksdb\c.h>+#else #include <rocksdb/c.h>+#endif data RocksDB data LCache@@ -49,18 +53,22 @@ deriving (Eq, Show) #{enum CompressionOpt, CompressionOpt , noCompression = 0- , enableCompression = 1+ , snappyCompression = 1+ , zlibCompression = 2+ , bz2Compression = 3+ , lz4Compression = 4+ , lz4hcCompression = 5 } -foreign import ccall safe "rocksdb/c.h rocksdb_open"+foreign import ccall safe "rocksdb\\c.h rocksdb_open" c_rocksdb_open :: OptionsPtr -> DBName -> ErrPtr -> IO RocksDBPtr -foreign import ccall safe "rocksdb/c.h rocksdb_close"+foreign import ccall safe "rocksdb\\c.h rocksdb_close" c_rocksdb_close :: RocksDBPtr -> IO () -foreign import ccall safe "rocksdb/c.h rocksdb_put"+foreign import ccall safe "rocksdb\\c.h rocksdb_put" c_rocksdb_put :: RocksDBPtr -> WriteOptionsPtr -> Key -> CSize@@ -68,14 +76,14 @@ -> ErrPtr -> IO () -foreign import ccall safe "rocksdb/c.h rocksdb_delete"+foreign import ccall safe "rocksdb\\c.h rocksdb_delete" c_rocksdb_delete :: RocksDBPtr -> WriteOptionsPtr -> Key -> CSize -> ErrPtr -> IO () -foreign import ccall safe "rocksdb/c.h rocksdb_write"+foreign import ccall safe "rocksdb\\c.h rocksdb_write" c_rocksdb_write :: RocksDBPtr -> WriteOptionsPtr -> WriteBatchPtr@@ -84,7 +92,7 @@ -- | Returns NULL if not found. A malloc()ed array otherwise. Stores the length -- of the array in *vallen.-foreign import ccall safe "rocksdb/c.h rocksdb_get"+foreign import ccall safe "rocksdb\\c.h rocksdb_get" c_rocksdb_get :: RocksDBPtr -> ReadOptionsPtr -> Key -> CSize@@ -92,18 +100,18 @@ -> ErrPtr -> IO CString -foreign import ccall safe "rocksdb/c.h rocksdb_create_snapshot"+foreign import ccall safe "rocksdb\\c.h rocksdb_create_snapshot" c_rocksdb_create_snapshot :: RocksDBPtr -> IO SnapshotPtr -foreign import ccall safe "rocksdb/c.h rocksdb_release_snapshot"+foreign import ccall safe "rocksdb\\c.h rocksdb_release_snapshot" c_rocksdb_release_snapshot :: RocksDBPtr -> SnapshotPtr -> IO () -- | Returns NULL if property name is unknown. Else returns a pointer to a -- malloc()-ed null-terminated value.-foreign import ccall safe "rocksdb/c.h rocksdb_property_value"+foreign import ccall safe "rocksdb\\c.h rocksdb_property_value" c_rocksdb_property_value :: RocksDBPtr -> CString -> IO CString -foreign import ccall safe "rocksdb/c.h rocksdb_approximate_sizes"+foreign import ccall safe "rocksdb\\c.h rocksdb_approximate_sizes" c_rocksdb_approximate_sizes :: RocksDBPtr -> CInt -- ^ num ranges -> Ptr CString -> Ptr CSize -- ^ range start keys (array)@@ -111,10 +119,10 @@ -> Ptr Word64 -- ^ array of approx. sizes of ranges -> IO () -foreign import ccall safe "rocksdb/c.h rocksdb_destroy_db"+foreign import ccall safe "rocksdb\\c.h rocksdb_destroy_db" c_rocksdb_destroy_db :: OptionsPtr -> DBName -> ErrPtr -> IO () -foreign import ccall safe "rocksdb/c.h rocksdb_repair_db"+foreign import ccall safe "rocksdb\\c.h rocksdb_repair_db" c_rocksdb_repair_db :: OptionsPtr -> DBName -> ErrPtr -> IO () @@ -122,37 +130,37 @@ -- Iterator -- -foreign import ccall safe "rocksdb/c.h rocksdb_create_iterator"+foreign import ccall safe "rocksdb\\c.h rocksdb_create_iterator" c_rocksdb_create_iterator :: RocksDBPtr -> ReadOptionsPtr -> IO IteratorPtr -foreign import ccall safe "rocksdb/c.h rocksdb_iter_destroy"+foreign import ccall safe "rocksdb\\c.h rocksdb_iter_destroy" c_rocksdb_iter_destroy :: IteratorPtr -> IO () -foreign import ccall safe "rocksdb/c.h rocksdb_iter_valid"+foreign import ccall safe "rocksdb\\c.h rocksdb_iter_valid" c_rocksdb_iter_valid :: IteratorPtr -> IO CUChar -foreign import ccall safe "rocksdb/c.h rocksdb_iter_seek_to_first"+foreign import ccall safe "rocksdb\\c.h rocksdb_iter_seek_to_first" c_rocksdb_iter_seek_to_first :: IteratorPtr -> IO () -foreign import ccall safe "rocksdb/c.h rocksdb_iter_seek_to_last"+foreign import ccall safe "rocksdb\\c.h rocksdb_iter_seek_to_last" c_rocksdb_iter_seek_to_last :: IteratorPtr -> IO () -foreign import ccall safe "rocksdb/c.h rocksdb_iter_seek"+foreign import ccall safe "rocksdb\\c.h rocksdb_iter_seek" c_rocksdb_iter_seek :: IteratorPtr -> Key -> CSize -> IO () -foreign import ccall safe "rocksdb/c.h rocksdb_iter_next"+foreign import ccall safe "rocksdb\\c.h rocksdb_iter_next" c_rocksdb_iter_next :: IteratorPtr -> IO () -foreign import ccall safe "rocksdb/c.h rocksdb_iter_prev"+foreign import ccall safe "rocksdb\\c.h rocksdb_iter_prev" c_rocksdb_iter_prev :: IteratorPtr -> IO () -foreign import ccall safe "rocksdb/c.h rocksdb_iter_key"+foreign import ccall safe "rocksdb\\c.h rocksdb_iter_key" c_rocksdb_iter_key :: IteratorPtr -> Ptr CSize -> IO Key -foreign import ccall safe "rocksdb/c.h rocksdb_iter_value"+foreign import ccall safe "rocksdb\\c.h rocksdb_iter_value" c_rocksdb_iter_value :: IteratorPtr -> Ptr CSize -> IO Val -foreign import ccall safe "rocksdb/c.h rocksdb_iter_get_error"+foreign import ccall safe "rocksdb\\c.h rocksdb_iter_get_error" c_rocksdb_iter_get_error :: IteratorPtr -> ErrPtr -> IO () @@ -160,25 +168,25 @@ -- Write batch -- -foreign import ccall safe "rocksdb/c.h rocksdb_writebatch_create"+foreign import ccall safe "rocksdb\\c.h rocksdb_writebatch_create" c_rocksdb_writebatch_create :: IO WriteBatchPtr -foreign import ccall safe "rocksdb/c.h rocksdb_writebatch_destroy"+foreign import ccall safe "rocksdb\\c.h rocksdb_writebatch_destroy" c_rocksdb_writebatch_destroy :: WriteBatchPtr -> IO () -foreign import ccall safe "rocksdb/c.h rocksdb_writebatch_clear"+foreign import ccall safe "rocksdb\\c.h rocksdb_writebatch_clear" c_rocksdb_writebatch_clear :: WriteBatchPtr -> IO () -foreign import ccall safe "rocksdb/c.h rocksdb_writebatch_put"+foreign import ccall safe "rocksdb\\c.h rocksdb_writebatch_put" c_rocksdb_writebatch_put :: WriteBatchPtr -> Key -> CSize -> Val -> CSize -> IO () -foreign import ccall safe "rocksdb/c.h rocksdb_writebatch_delete"+foreign import ccall safe "rocksdb\\c.h rocksdb_writebatch_delete" c_rocksdb_writebatch_delete :: WriteBatchPtr -> Key -> CSize -> IO () -foreign import ccall safe "rocksdb/c.h rocksdb_writebatch_iterate"+foreign import ccall safe "rocksdb\\c.h rocksdb_writebatch_iterate" c_rocksdb_writebatch_iterate :: WriteBatchPtr -> Ptr () -- ^ state -> FunPtr (Ptr () -> Key -> CSize -> Val -> CSize) -- ^ put@@ -190,49 +198,37 @@ -- Options -- -foreign import ccall safe "rocksdb/c.h rocksdb_options_create"+foreign import ccall safe "rocksdb\\c.h rocksdb_options_create" c_rocksdb_options_create :: IO OptionsPtr -foreign import ccall safe "rocksdb/c.h rocksdb_options_destroy"+foreign import ccall safe "rocksdb\\c.h rocksdb_options_destroy" c_rocksdb_options_destroy :: OptionsPtr -> IO () -foreign import ccall safe "rocksdb/c.h rocksdb_options_set_comparator"+foreign import ccall safe "rocksdb\\c.h rocksdb_options_set_comparator" c_rocksdb_options_set_comparator :: OptionsPtr -> ComparatorPtr -> IO () -foreign import ccall safe "rocksdb/c.h rocksdb_options_set_filter_policy"- c_rocksdb_options_set_filter_policy :: OptionsPtr -> FilterPolicyPtr -> IO ()--foreign import ccall safe "rocksdb/c.h rocksdb_options_set_create_if_missing"+foreign import ccall safe "rocksdb\\c.h rocksdb_options_set_create_if_missing" c_rocksdb_options_set_create_if_missing :: OptionsPtr -> CUChar -> IO () -foreign import ccall safe "rocksdb/c.h rocksdb_options_set_error_if_exists"+foreign import ccall safe "rocksdb\\c.h rocksdb_options_set_error_if_exists" c_rocksdb_options_set_error_if_exists :: OptionsPtr -> CUChar -> IO () -foreign import ccall safe "rocksdb/c.h rocksdb_options_set_paranoid_checks"+foreign import ccall safe "rocksdb\\c.h rocksdb_options_set_paranoid_checks" c_rocksdb_options_set_paranoid_checks :: OptionsPtr -> CUChar -> IO () -foreign import ccall safe "rocksdb/c.h rocksdb_options_set_info_log"+foreign import ccall safe "rocksdb\\c.h rocksdb_options_set_info_log" c_rocksdb_options_set_info_log :: OptionsPtr -> LoggerPtr -> IO () -foreign import ccall safe "rocksdb/c.h rocksdb_options_set_write_buffer_size"+foreign import ccall safe "rocksdb\\c.h rocksdb_options_set_write_buffer_size" c_rocksdb_options_set_write_buffer_size :: OptionsPtr -> CSize -> IO () -foreign import ccall safe "rocksdb/c.h rocksdb_options_set_max_open_files"+foreign import ccall safe "rocksdb\\c.h rocksdb_options_set_max_open_files" c_rocksdb_options_set_max_open_files :: OptionsPtr -> CInt -> IO () -foreign import ccall safe "rocksdb/c.h rocksdb_options_set_block_size"- c_rocksdb_options_set_block_size :: OptionsPtr -> CSize -> IO ()--foreign import ccall safe "rocksdb/c.h rocksdb_options_set_block_restart_interval"- c_rocksdb_options_set_block_restart_interval :: OptionsPtr -> CInt -> IO ()--foreign import ccall safe "rocksdb/c.h rocksdb_options_set_compression"+foreign import ccall safe "rocksdb\\c.h rocksdb_options_set_compression" c_rocksdb_options_set_compression :: OptionsPtr -> CompressionOpt -> IO () -foreign import ccall safe "rocksdb/c.h rocksdb_options_set_cache"- c_rocksdb_options_set_cache :: OptionsPtr -> CachePtr -> IO () - -- -- Comparator --@@ -251,14 +247,14 @@ -- | Make a name FunPtr foreign import ccall "wrapper" mkName :: NameFun -> IO (FunPtr NameFun) -foreign import ccall safe "rocksdb/c.h rocksdb_comparator_create"+foreign import ccall safe "rocksdb\\c.h rocksdb_comparator_create" c_rocksdb_comparator_create :: StatePtr -> FunPtr Destructor -> FunPtr CompareFun -> FunPtr NameFun -> IO ComparatorPtr -foreign import ccall safe "rocksdb/c.h rocksdb_comparator_destroy"+foreign import ccall safe "rocksdb\\c.h rocksdb_comparator_destroy" c_rocksdb_comparator_destroy :: ComparatorPtr -> IO () @@ -285,7 +281,7 @@ -- | Make a FunPtr to a user-defined key_may_match function foreign import ccall "wrapper" mkKMM :: KeyMayMatchFun -> IO (FunPtr KeyMayMatchFun) -foreign import ccall safe "rocksdb/c.h rocksdb_filterpolicy_create"+foreign import ccall safe "rocksdb\\c.h rocksdb_filterpolicy_create" c_rocksdb_filterpolicy_create :: StatePtr -> FunPtr Destructor -> FunPtr CreateFilterFun@@ -293,29 +289,29 @@ -> FunPtr NameFun -> IO FilterPolicyPtr -foreign import ccall safe "rocksdb/c.h rocksdb_filterpolicy_destroy"+foreign import ccall safe "rocksdb\\c.h rocksdb_filterpolicy_destroy" c_rocksdb_filterpolicy_destroy :: FilterPolicyPtr -> IO () -foreign import ccall safe "rocksdb/c.h rocksdb_filterpolicy_create_bloom"+foreign import ccall safe "rocksdb\\c.h rocksdb_filterpolicy_create_bloom" c_rocksdb_filterpolicy_create_bloom :: CInt -> IO FilterPolicyPtr -- -- Read options -- -foreign import ccall safe "rocksdb/c.h rocksdb_readoptions_create"+foreign import ccall safe "rocksdb\\c.h rocksdb_readoptions_create" c_rocksdb_readoptions_create :: IO ReadOptionsPtr -foreign import ccall safe "rocksdb/c.h rocksdb_readoptions_destroy"+foreign import ccall safe "rocksdb\\c.h rocksdb_readoptions_destroy" c_rocksdb_readoptions_destroy :: ReadOptionsPtr -> IO () -foreign import ccall safe "rocksdb/c.h rocksdb_readoptions_set_verify_checksums"+foreign import ccall safe "rocksdb\\c.h rocksdb_readoptions_set_verify_checksums" c_rocksdb_readoptions_set_verify_checksums :: ReadOptionsPtr -> CUChar -> IO () -foreign import ccall safe "rocksdb/c.h rocksdb_readoptions_set_fill_cache"+foreign import ccall safe "rocksdb\\c.h rocksdb_readoptions_set_fill_cache" c_rocksdb_readoptions_set_fill_cache :: ReadOptionsPtr -> CUChar -> IO () -foreign import ccall safe "rocksdb/c.h rocksdb_readoptions_set_snapshot"+foreign import ccall safe "rocksdb\\c.h rocksdb_readoptions_set_snapshot" c_rocksdb_readoptions_set_snapshot :: ReadOptionsPtr -> SnapshotPtr -> IO () @@ -323,13 +319,13 @@ -- Write options -- -foreign import ccall safe "rocksdb/c.h rocksdb_writeoptions_create"+foreign import ccall safe "rocksdb\\c.h rocksdb_writeoptions_create" c_rocksdb_writeoptions_create :: IO WriteOptionsPtr -foreign import ccall safe "rocksdb/c.h rocksdb_writeoptions_destroy"+foreign import ccall safe "rocksdb\\c.h rocksdb_writeoptions_destroy" c_rocksdb_writeoptions_destroy :: WriteOptionsPtr -> IO () -foreign import ccall safe "rocksdb/c.h rocksdb_writeoptions_set_sync"+foreign import ccall safe "rocksdb\\c.h rocksdb_writeoptions_set_sync" c_rocksdb_writeoptions_set_sync :: WriteOptionsPtr -> CUChar -> IO () @@ -337,8 +333,15 @@ -- Cache -- -foreign import ccall safe "rocksdb/c.h rocksdb_cache_create_lru"+foreign import ccall safe "rocksdb\\c.h rocksdb_cache_create_lru" c_rocksdb_cache_create_lru :: CSize -> IO CachePtr -foreign import ccall safe "rocksdb/c.h rocksdb_cache_destroy"+foreign import ccall safe "rocksdb\\c.h rocksdb_cache_destroy" c_rocksdb_cache_destroy :: CachePtr -> IO ()++----------------------------------------------------------------------------+-- Free+----------------------------------------------------------------------------++foreign import ccall safe "rocksdb\\c.h rocksdb_free"+ c_rocksdb_free :: CString -> IO ()
src/Database/RocksDB/Internal.hs view
@@ -21,6 +21,7 @@ , freeComparator , freeFilterPolicy , freeOpts+ , freeCString , mkCReadOpts , mkComparator , mkCompareFun@@ -48,7 +49,7 @@ import Control.Monad (when) import Data.ByteString (ByteString) import Foreign-import Foreign.C.String (peekCString, withCString)+import Foreign.C.String (CString, peekCString, withCString) import Foreign.C.Types (CInt, CSize) import Database.RocksDB.C@@ -81,7 +82,6 @@ { _optsPtr :: !OptionsPtr , _cachePtr :: !(Maybe CachePtr) , _comp :: !(Maybe Comparator')- , _fpPtr :: !(Maybe (Either FilterPolicyPtr FilterPolicy')) } @@ -89,10 +89,6 @@ mkOpts Options{..} = do opts_ptr <- c_rocksdb_options_create - c_rocksdb_options_set_block_restart_interval opts_ptr- $ intToCInt blockRestartInterval- c_rocksdb_options_set_block_size opts_ptr- $ intToCSize blockSize c_rocksdb_options_set_compression opts_ptr $ ccompression compression c_rocksdb_options_set_create_if_missing opts_ptr@@ -106,30 +102,21 @@ c_rocksdb_options_set_write_buffer_size opts_ptr $ intToCSize writeBufferSize - cache <- maybeSetCache opts_ptr cacheSize cmp <- maybeSetCmp opts_ptr comparator- fp <- maybeSetFilterPolicy opts_ptr filterPolicy - return (Options' opts_ptr cache cmp fp)+ return (Options' opts_ptr Nothing cmp) where ccompression NoCompression = noCompression- ccompression EnableCompression =- enableCompression-- maybeSetCache :: OptionsPtr -> Int -> IO (Maybe CachePtr)- maybeSetCache opts_ptr size =- if size <= 0- then return Nothing- else do- cache_ptr <- c_rocksdb_cache_create_lru $ intToCSize size- c_rocksdb_options_set_cache opts_ptr cache_ptr- return . Just $ cache_ptr+ ccompression SnappyCompression =+ snappyCompression+ ccompression ZlibCompression =+ zlibCompression maybeSetCmp :: OptionsPtr -> Maybe Comparator -> IO (Maybe Comparator') maybeSetCmp opts_ptr (Just mcmp) = Just <$> setcmp opts_ptr mcmp- maybeSetCmp _ Nothing = return Nothing+ maybeSetCmp _ Nothing = return Nothing setcmp :: OptionsPtr -> Comparator -> IO Comparator' setcmp opts_ptr (Comparator cmp) = do@@ -137,30 +124,12 @@ c_rocksdb_options_set_comparator opts_ptr cmp_ptr return cmp' - maybeSetFilterPolicy :: OptionsPtr- -> Maybe (Either BloomFilter FilterPolicy)- -> IO (Maybe (Either FilterPolicyPtr FilterPolicy'))- maybeSetFilterPolicy _ Nothing =- return Nothing- maybeSetFilterPolicy opts_ptr (Just (Left (BloomFilter bloom_ptr))) = do- c_rocksdb_options_set_filter_policy opts_ptr bloom_ptr- return Nothing -- bloom filter is freed automatically- maybeSetFilterPolicy opts_ptr (Just (Right fp)) = do- fp'@(FilterPolicy' _ _ _ _ fp_ptr) <- mkFilterPolicy fp- c_rocksdb_options_set_filter_policy opts_ptr fp_ptr- return . Just . Right $ fp'- freeOpts :: Options' -> IO ()-freeOpts (Options' opts_ptr mcache_ptr mcmp_ptr mfp) = do+freeOpts (Options' opts_ptr mcache_ptr mcmp_ptr ) = do c_rocksdb_options_destroy opts_ptr maybe (return ()) c_rocksdb_cache_destroy mcache_ptr maybe (return ()) freeComparator mcmp_ptr- maybe (return ())- (either c_rocksdb_filterpolicy_destroy freeFilterPolicy)- mfp - return ()- withCWriteOpts :: WriteOptions -> (WriteOptionsPtr -> IO a) -> IO a withCWriteOpts WriteOptions{..} = bracket mkCWriteOpts freeCWriteOpts where@@ -258,6 +227,9 @@ freeCReadOpts :: ReadOptionsPtr -> IO () freeCReadOpts = c_rocksdb_readoptions_destroy++freeCString :: CString -> IO ()+freeCString = c_rocksdb_free withCReadOpts :: ReadOptions -> (ReadOptionsPtr -> IO a) -> IO a withCReadOpts opts = bracket (mkCReadOpts opts) freeCReadOpts
src/Database/RocksDB/Iterator.hs view
@@ -29,27 +29,31 @@ , mapIter , releaseIter , withIter- )-where+ , withIterator+ , iterOpenBracket+ , iterOpen+ ) where -import Control.Applicative ((<$>), (<*>))-import Control.Exception (bracket, finally, onException)-import Control.Monad (when)-import Control.Monad.IO.Class (MonadIO (liftIO))-import Data.ByteString (ByteString)-import Data.Maybe (catMaybes)+import Control.Applicative ((<$>), (<*>))+import Control.Exception (bracket, finally, onException)+import Control.Monad (when)+import Control.Monad.IO.Class (MonadIO (liftIO))+import Control.Monad.Trans.Resource (MonadResource (..), ReleaseKey, allocate,+ release)+import Data.ByteString (ByteString)+import Data.Maybe (catMaybes) import Foreign-import Foreign.C.Error (throwErrnoIfNull)-import Foreign.C.String (CString, peekCString)-import Foreign.C.Types (CSize)+import Foreign.C.Error (throwErrnoIfNull)+import Foreign.C.String (CString, peekCString)+import Foreign.C.Types (CSize) import Database.RocksDB.C import Database.RocksDB.Internal import Database.RocksDB.Types -import qualified Data.ByteString as BS-import qualified Data.ByteString.Char8 as BC-import qualified Data.ByteString.Unsafe as BU+import qualified Data.ByteString as BS+import qualified Data.ByteString.Char8 as BC+import qualified Data.ByteString.Unsafe as BU -- | Iterator handle --@@ -58,8 +62,34 @@ -- @examples/iterforkio.hs@ for a simple example of how to do that. data Iterator = Iterator !IteratorPtr !ReadOptionsPtr deriving (Eq) +-- | 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.+withIterator :: MonadResource m => DB -> ReadOptions -> (Iterator -> m a) -> m a+withIterator db opts f = do+ (rk, iter) <- iterOpenBracket db opts+ res <- f iter+ release rk+ return res+ -- | 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 Iterator+iterOpen db opts = snd <$> iterOpenBracket db opts++-- | Create an 'Iterator' which can be released early.+iterOpenBracket :: MonadResource m => DB -> ReadOptions -> m (ReleaseKey, Iterator)+iterOpenBracket db opts = allocate (createIter db opts) releaseIter++-- | Create an 'Iterator'.+-- -- The iterator should be released with 'releaseIter'. -- -- Note that an 'Iterator' creates a snapshot of the database implicitly, so@@ -190,23 +220,22 @@ -- should be put in the right position prior to calling this with the iterator. -- -- See strictness remarks on 'mapIter'.-iterItems :: (Functor m, MonadIO m) => Iterator -> m [(ByteString, ByteString)]+iterItems :: MonadIO m => Iterator -> m [(ByteString, ByteString)] iterItems iter = catMaybes <$> mapIter iterEntry iter -- | Return a list of key from an iterator. The iterator should be put -- in the right position prior to calling this with the iterator. -- -- See strictness remarks on 'mapIter'-iterKeys :: (Functor m, MonadIO m) => Iterator -> m [ByteString]+iterKeys :: MonadIO m => Iterator -> m [ByteString] iterKeys iter = catMaybes <$> mapIter iterKey iter -- | Return a list of values from an iterator. The iterator should be put -- in the right position prior to calling this with the iterator. -- -- See strictness remarks on 'mapIter'-iterValues :: (Functor m, MonadIO m) => Iterator -> m [ByteString]+iterValues :: MonadIO m => Iterator -> m [ByteString] iterValues iter = catMaybes <$> mapIter iterValue iter- -- -- Internal
− src/Database/RocksDB/MonadResource.hs
@@ -1,279 +0,0 @@--- |--- Module : Database.RocksDB.MonadResource--- Copyright : (c) 2012-2013 The leveldb-haskell Authors--- (c) 2014 The rocksdb-haskell Authors--- License : BSD3--- Maintainer : mail@agrafix.net--- Stability : experimental--- Portability : non-portable-----module Database.RocksDB.MonadResource- ( -- * Exported Types- DB- , BatchOp(..)- , Comparator(..)- , Compression(..)- , Options(..)- , ReadOptions(..)- , Snapshot- , WriteBatch- , WriteOptions(..)- , Range-- -- * Defaults- , defaultOptions- , defaultWriteOptions- , defaultReadOptions-- -- * Basic Database Manipulation- , withSnapshot- , open- , put- , delete- , write- , get- , createSnapshot- , createSnapshot'-- -- * Filter Policy / Bloom Filter- , FilterPolicy(..)- , bloomFilter-- -- * Administrative Functions- , Property(..), getProperty- , destroy- , repair- , approximateSize-- -- * Iteration- , Iterator- , withIterator- , iterOpen- , iterOpen'- , iterValid- , iterSeek- , iterFirst- , iterLast- , iterNext- , iterPrev- , iterKey- , iterValue- , iterGetError- , mapIter- , iterItems- , iterKeys- , iterValues-- -- * Re-exports- , MonadResource (..)- , runResourceT- , resourceForkIO- )-where--import Control.Applicative ((<$>))-import Control.Monad.Trans.Resource-import Data.ByteString (ByteString)-import Data.Int (Int64)--import Database.RocksDB.Base (BatchOp, BloomFilter, Comparator,- Compression, DB, FilterPolicy,- Iterator, Options, Property,- Range, ReadOptions, Snapshot,- WriteBatch, WriteOptions,- defaultOptions,- defaultReadOptions,- defaultWriteOptions)-import qualified Database.RocksDB.Base as Base----- | Create a 'BloomFilter'-bloomFilter :: MonadResource m => Int -> m BloomFilter-bloomFilter i =- snd <$> allocate (Base.createBloomFilter i)- Base.releaseBloomFilter---- | Open a database------ The returned handle will automatically be released when the enclosing--- 'runResourceT' terminates.-open :: MonadResource m => FilePath -> Options -> m DB-open path opts = snd <$> open' path opts--open' :: MonadResource m => FilePath -> Options -> m (ReleaseKey, DB)-open' path opts = allocate (Base.open path opts) Base.close-{-# INLINE open' #-}---- | 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.-withSnapshot :: MonadResource m => DB -> (Snapshot -> m a) -> m a-withSnapshot db f = do- (rk, snap) <- createSnapshot' db- res <- f snap- release rk- return res---- | 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 Snapshot-createSnapshot db = snd <$> createSnapshot' db---- | Create a snapshot of the database which can (and should) be released early.-createSnapshot' :: MonadResource m => DB -> m (ReleaseKey, Snapshot)-createSnapshot' db = allocate (Base.createSnapshot db) (Base.releaseSnapshot db)---- | Get a DB property-getProperty :: MonadResource m => DB -> Property -> m (Maybe ByteString)-getProperty = Base.getProperty---- | Destroy the given rocksdb database.-destroy :: MonadResource m => FilePath -> Options -> m ()-destroy = Base.destroy---- | Repair the given rocksdb database.-repair :: MonadResource m => FilePath -> Options -> m ()-repair = Base.repair----- | Inspect the approximate sizes of the different levels-approximateSize :: MonadResource m => DB -> Range -> m Int64-approximateSize = Base.approximateSize---- | Write a key/value pair-put :: MonadResource m => DB -> WriteOptions -> ByteString -> ByteString -> m ()-put = Base.put---- | Read a value by key-get :: MonadResource m => DB -> ReadOptions -> ByteString -> m (Maybe ByteString)-get = Base.get---- | Delete a key/value pair-delete :: MonadResource m => DB -> WriteOptions -> ByteString -> m ()-delete = Base.delete---- | Perform a batch mutation-write :: MonadResource m => DB -> WriteOptions -> WriteBatch -> m ()-write = Base.write----- | 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.-withIterator :: MonadResource m => DB -> ReadOptions -> (Iterator -> m a) -> m a-withIterator db opts f = do- (rk, iter) <- iterOpen' db opts- res <- f iter- release rk- return res---- | 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 Iterator-iterOpen db opts = snd <$> iterOpen' db opts---- | Create an 'Iterator' which can be released early.-iterOpen' :: MonadResource m => DB -> ReadOptions -> m (ReleaseKey, Iterator)-iterOpen' db opts = allocate (Base.createIter db opts) Base.releaseIter---- | An iterator is either positioned at a key/value pair, or not valid. This--- function returns /true/ iff the iterator is valid.-iterValid :: MonadResource m => Iterator -> m Bool-iterValid = Base.iterValid---- | 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.-iterSeek :: MonadResource m => Iterator -> ByteString -> m ()-iterSeek = Base.iterSeek---- | Position at the first key in the source. The iterator is /valid/ after this--- call iff the source is not empty.-iterFirst :: MonadResource m => Iterator -> m ()-iterFirst = Base.iterFirst---- | Position at the last key in the source. The iterator is /valid/ after this--- call iff the source is not empty.-iterLast :: MonadResource m => Iterator -> m ()-iterLast = Base.iterLast---- | 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.-iterNext :: MonadResource m => Iterator -> m ()-iterNext = Base.iterNext---- | 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.-iterPrev :: MonadResource m => Iterator -> m ()-iterPrev = Base.iterPrev---- | Return the key for the current entry if the iterator is currently--- positioned at an entry, ie. 'iterValid'.-iterKey :: MonadResource m => Iterator -> m (Maybe ByteString)-iterKey = Base.iterKey---- | Return the value for the current entry if the iterator is currently--- positioned at an entry, ie. 'iterValid'.-iterValue :: MonadResource m => Iterator -> m (Maybe ByteString)-iterValue = Base.iterValue---- | Check for errors------ Note that this captures somewhat severe errors such as a corrupted database.-iterGetError :: MonadResource m => Iterator -> m (Maybe ByteString)-iterGetError = Base.iterGetError----- | Map a function over an iterator, advancing the iterator forward and--- returning the value. The iterator should be put in the right position prior--- to calling the function.------ Note that this function accumulates the result strictly, ie. it reads all--- values into memory until the iterator is exhausted. This is most likely not--- what you want for large ranges. You may consider using conduits instead, for--- an example see: <https://gist.github.com/adc8ec348f03483446a5>-mapIter :: MonadResource m => (Iterator -> m a) -> Iterator -> m [a]-mapIter = Base.mapIter---- | Return a list of key and value tuples from an iterator. The iterator--- should be put in the right position prior to calling this with the iterator.------ See strictness remarks on 'mapIter'.-iterItems :: MonadResource m => Iterator -> m [(ByteString, ByteString)]-iterItems = Base.iterItems---- | Return a list of key from an iterator. The iterator should be put--- in the right position prior to calling this with the iterator.------ See strictness remarks on 'mapIter'-iterKeys :: MonadResource m => Iterator -> m [ByteString]-iterKeys = Base.iterKeys---- | Return a list of values from an iterator. The iterator should be put--- in the right position prior to calling this with the iterator.------ See strictness remarks on 'mapIter'-iterValues :: MonadResource m => Iterator -> m [ByteString]-iterValues = Base.iterValues
src/Database/RocksDB/Types.hs view
@@ -37,7 +37,11 @@ newtype Snapshot = Snapshot SnapshotPtr deriving (Eq) -- | Compression setting-data Compression = NoCompression | EnableCompression deriving (Eq, Show)+data Compression+ = NoCompression+ | SnappyCompression+ | ZlibCompression+ deriving (Eq, Show) -- | User-defined comparator newtype Comparator = Comparator (ByteString -> ByteString -> Ordering)@@ -54,32 +58,7 @@ -- | Options when opening a database data Options = Options- { 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, rocksdb- -- will automatically create and use an 8MB internal cache.- --- -- Default: 0- , comparator :: !(Maybe Comparator)+ { comparator :: !(Maybe Comparator) -- ^ Comparator used to defined the order of keys in the table. -- -- If 'Nothing', the default comparator is used, which uses lexicographic@@ -90,28 +69,28 @@ -- to previous open calls on the same DB. -- -- Default: Nothing- , compression :: !Compression+ , compression :: !Compression -- ^ Compress blocks using the specified compression algorithm. -- -- This parameter can be changed dynamically. -- -- Default: 'EnableCompression'- , createIfMissing :: !Bool+ , createIfMissing :: !Bool -- ^ If true, the database will be created if it is missing. -- -- Default: False- , errorIfExists :: !Bool+ , errorIfExists :: !Bool -- ^ It true, an error is raised if the database already exists. -- -- Default: False- , maxOpenFiles :: !Int+ , 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+ , 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. --@@ -120,7 +99,7 @@ -- or for the entire DB to become unopenable. -- -- Default: False- , writeBufferSize :: !Int+ , 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. --@@ -131,22 +110,17 @@ -- database is opened. -- -- Default: 4MB- , filterPolicy :: !(Maybe (Either BloomFilter FilterPolicy)) } defaultOptions :: Options defaultOptions = Options- { blockRestartInterval = 16- , blockSize = 4096- , cacheSize = 0- , comparator = Nothing- , compression = EnableCompression+ { comparator = Nothing+ , compression = SnappyCompression , createIfMissing = False , errorIfExists = False , maxOpenFiles = 1000 , paranoidChecks = False , writeBufferSize = 4 `shift` 20- , filterPolicy = Nothing } instance Default Options where
+ tests/tests.hs view
@@ -0,0 +1,34 @@+{-# LANGUAGE OverloadedStrings #-}++module Main where++import Control.Monad.IO.Class (MonadIO (liftIO))+import Control.Monad.Trans.Resource (MonadResource, runResourceT)+import Data.Default+import System.Process (system)+import System.IO.Temp (withSystemTempDirectory)+import Test.Hspec+import Test.Hspec.Expectations+import Test.Hspec.QuickCheck (prop)+import Test.QuickCheck++import Database.RocksDB++initializeDB :: MonadResource m => FilePath -> m DB+initializeDB path =+ open+ path+ defaultOptions+ {createIfMissing = True, compression = NoCompression}++main :: IO ()+main = hspec $ do++ describe "Basic DB Functionality" $ do+ it "should put items into the database and retrieve them" $ do+ runResourceT $ withSystemTempDirectory "rocksdb" $ \path -> do+ db <- initializeDB path++ put db def "zzz" "zzz"+ get db def "zzz"+ `shouldReturn` (Just "zzz")