Changed remove to not throw an exception on a missing key. Oops.
authordavid@mel
Sun Apr 27 15:39:44 2008 +0100 (4 months ago)
changeset 50ad2a68f4979
parent 4817654ccb5be
child 64f19cb4fadef
Changed remove to not throw an exception on a missing key. Oops.
--- a/haskell/qdbm/QDBM/Depot.hs Sun Apr 27 13:41:15 2008 +0100
+++ b/haskell/qdbm/QDBM/Depot.hs Sun Apr 27 15:39:44 2008 +0100
@@ -21,8 +21,10 @@ import Data.Bits
import Data.Bits
import Control.Monad
+-- A handle on a database.
type Depot = Ptr CDepot
+-- A mode in which we're accessing a database.
data AccessMode = Read | Write
-- Open the Depot with the given name, creating it if it does not exist,
@@ -43,6 +45,7 @@ closeDepot = check . dpclose
closeDepot = check . dpclose
+-- Get a record from the Depot, returning Just value if its present or Nothing otherwise.
get :: Depot -> ByteString -> IO (Maybe ByteString)
get depot str = unsafeUseAsCStringLen str getCSL
where getCSL :: CStringLen -> IO (Maybe ByteString)
@@ -51,13 +54,15 @@ get depot str = unsafeUseAsCStringLen st
if (result == nullPtr) then return Nothing
else liftM Just $ cStringLenToByteString (result, resultlen)
+--
put :: Depot -> ByteString -> ByteString -> IO ()
put depot key value = unsafeUseAsCStringLen key $ \k -> (unsafeUseAsCStringLen value $ put' k)
where put' :: CStringLen -> CStringLen -> IO ()
put' (ks, kl) (vs, vl) = check $ dpput depot ks kl vs vl 0
-remove :: Depot -> ByteString -> IO ()
-remove depot string = check $ unsafeUseAsCStringLen string (uncurry $ dpout depot)
+remove :: Depot -> ByteString -> IO Bool
+remove depot string = do result <- unsafeUseAsCStringLen string (uncurry $ dpout depot)
+ return (result /= 0)
-- Note that this function is *not* reentrant. Calling it inside itself
-- will break everything. :(