rocksdb-query 0.1.4 → 0.2.0
raw patch · 4 files changed
+48/−31 lines, 4 filesdep +data-default
Dependencies added: data-default
Files
- CHANGELOG.md +4/−0
- rocksdb-query.cabal +3/−2
- src/Database/RocksDB/Query.hs +36/−25
- test/Spec.hs +5/−4
CHANGELOG.md view
@@ -4,6 +4,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +## 0.2.0+### Changed+- Accept read options instead of snapshot for read operations.+ ## 0.1.4 ### Changed - Documentation improvements.
rocksdb-query.cabal view
@@ -2,10 +2,10 @@ -- -- see: https://github.com/sol/hpack ----- hash: 6a197506c4da781d1961464f87333cda5a6687a1432fa0586d8cf4eadc50b1ed+-- hash: bcc5b994097bfd7c7418a70ade182d3fc5c268d6b4667e658cb0e5de7727a910 name: rocksdb-query-version: 0.1.4+version: 0.2.0 synopsis: RocksDB database querying library for Haskell description: Please see the README on GitHub at <https://github.com/xenog/rocksdb-query#readme> category: Database@@ -52,6 +52,7 @@ build-depends: base >=4.7 && <5 , cereal+ , data-default , hspec , rocksdb-haskell , rocksdb-query
src/Database/RocksDB/Query.hs view
@@ -1,25 +1,37 @@ {-# LANGUAGE LambdaCase #-} {-# LANGUAGE MultiParamTypeClasses #-}+{-|+Module : Database.RocksDB.Query+Copyright : No rights reserved+License : UNLICENSE+Maintainer : xenog@protonmail.com+Stability : experimental+Portability : POSIX++Query functions to make interaction with RocksDB stores easier and safer.+-} module Database.RocksDB.Query where -import qualified Data.ByteString as B import Conduit-import Data.Serialize as S-import Database.RocksDB as R+import qualified Data.ByteString as B+import Data.Serialize as S+import Database.RocksDB as R import UnliftIO +-- | Class for types that are database keys. class Key key++-- | Class for types that are corresponding database key and value. class KeyValue key value -- | Read a value from the database, or 'Nothing' if not found. retrieve :: (MonadIO m, KeyValue key value, Serialize key, Serialize value) => DB- -> Maybe Snapshot+ -> ReadOptions -> key -> m (Maybe value)-retrieve db snapshot key = do- let opts = defaultReadOptions {useSnapshot = snapshot}+retrieve db opts key = R.get db opts (encode key) >>= \case Nothing -> return Nothing Just bytes ->@@ -27,6 +39,7 @@ Left e -> throwString e Right x -> return (Just x) +-- | Internal function for recursively matching a key. matchRecursive :: ( MonadIO m , KeyValue key value@@ -66,7 +79,7 @@ -- > db <- open "test-db" defaultOptions {createIfMissing = True} -- > insert db (FullKey "hello" "world") "despite all my rage" -- > Just record <- runResourceT . runConduit $--- > matching db Nothing (ShortKey "hello") .| headC+-- > matching db def (ShortKey "hello") .| headC -- > print (record :: (MyKey, String)) -- > -- (Fullkey "hello" "world","despite all my rage") --@@ -81,11 +94,10 @@ , Serialize value ) => DB- -> Maybe Snapshot+ -> ReadOptions -> key -> ConduitT () (key, value) m ()-matching db snapshot base = do- let opts = defaultReadOptions {useSnapshot = snapshot}+matching db opts base = withIterator db opts $ \it -> do iterSeek it (encode base) matchRecursive base it@@ -99,12 +111,11 @@ , Serialize value ) => DB- -> Maybe Snapshot+ -> ReadOptions -> key -> key -> ConduitT () (key, value) m ()-matchingSkip db snapshot base start = do- let opts = defaultReadOptions {useSnapshot = snapshot}+matchingSkip db opts base start = withIterator db opts $ \it -> do iterSeek it (encode start) matchRecursive base it@@ -146,11 +157,11 @@ , Serialize value ) => DB- -> Maybe Snapshot+ -> ReadOptions -> key -> m (Maybe (key, value))-firstMatching db snapshot base =- runResourceT . runConduit $ matching db snapshot base .| headC+firstMatching db opts base =+ runResourceT . runConduit $ matching db opts base .| headC -- | Like 'matchingSkip', but return the first element only. firstMatchingSkip ::@@ -160,13 +171,13 @@ , Serialize value ) => DB- -> Maybe Snapshot+ -> ReadOptions -> key -> key -> m (Maybe (key, value))-firstMatchingSkip db snapshot base start =+firstMatchingSkip db opts base start = runResourceT . runConduit $- matchingSkip db snapshot base start .| headC+ matchingSkip db opts base start .| headC -- | Like 'matching' but return a list. matchingAsList ::@@ -176,12 +187,12 @@ , Serialize value ) => DB- -> Maybe Snapshot+ -> ReadOptions -> key -> m [(key, value)]-matchingAsList db snapshot base =+matchingAsList db opts base = runResourceT . runConduit $- matching db snapshot base .| sinkList+ matching db opts base .| sinkList -- | Like 'matchingSkip', but return a list. matchingSkipAsList ::@@ -191,10 +202,10 @@ , Serialize value ) => DB- -> Maybe Snapshot+ -> ReadOptions -> key -> key -> m [(key, value)]-matchingSkipAsList db snapshot base start =+matchingSkipAsList db opts base start = runResourceT . runConduit $- matchingSkip db snapshot base start .| sinkList+ matchingSkip db opts base start .| sinkList
test/Spec.hs view
@@ -2,6 +2,7 @@ {-# LANGUAGE MultiParamTypeClasses #-} import Control.Applicative import Control.Monad+import Data.Default import Data.Serialize as S import Data.Word import Database.RocksDB as R@@ -42,28 +43,28 @@ setup $ \db -> describe "database" $ do it "reads a record" $ do- r <- retrieve db Nothing (KeyTwo 1 2)+ r <- retrieve db def (KeyTwo 1 2) r `shouldBe` Just "Hello First World Again!" it "reads two records at the end" $ do let ls = [ (KeyTwo 2 1, "Hello Second World!") , (KeyTwo 2 2, "Hello Second World Again!") ]- rs <- matchingAsList db Nothing (KeyTwoBase 2)+ rs <- matchingAsList db def (KeyTwoBase 2) rs `shouldBe` ls it "reads two records in the middle" $ do let ls = [ (KeyTwo 1 1, "Hello First World!") , (KeyTwo 1 2, "Hello First World Again!") ]- rs <- matchingAsList db Nothing (KeyTwoBase 1)+ rs <- matchingAsList db def (KeyTwoBase 1) rs `shouldBe` ls it "query and skip" $ do let ex = (KeyTwo 2 2, "Hello Second World Again!") rs <- matchingSkipAsList db- Nothing+ def (KeyTwoBase 2) (KeyTwo 2 2) rs `shouldBe` [ex]