Skip to content

Commit be67a35

Browse files
committed
Fix errors not releasing memory.
- Refactor some files for clarity - Bump LTS Haskell
1 parent f1709ef commit be67a35

File tree

10 files changed

+48
-56
lines changed

10 files changed

+48
-56
lines changed

Diff for: CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.1.5
2+
3+
* Fix errors not releasing memory.
4+
15
## 2.1.4
26

37
* Add ResourceT support for snapshots and iterators.

Diff for: hie.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
cradle:
2+
stack:

Diff for: package.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: rocksdb-haskell-jprupp
2-
version: 2.1.4
2+
version: 2.1.5
33
synopsis: Haskell bindings for RocksDB
44
description: See README at <https://github.com/jprupp/rocksdb-haskell#readme>
55
maintainer: Jean-Pierre Rupp <[email protected]>

Diff for: rocksdb-haskell-jprupp.cabal

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
cabal-version: 1.12
22

3-
-- This file has been generated from package.yaml by hpack version 0.34.4.
3+
-- This file has been generated from package.yaml by hpack version 0.35.2.
44
--
55
-- see: https://github.com/sol/hpack
66
--
7-
-- hash: 48b49def634c00156ec5f8bc2f4a8c92caf0aca334fca3cef6507f51febe9867
7+
-- hash: 7e9a6be0b5ec766b502a4a03665778ca9fd0e9b17df4f5da3f902cb7631111be
88

99
name: rocksdb-haskell-jprupp
10-
version: 2.1.4
10+
version: 2.1.5
1111
synopsis: Haskell bindings for RocksDB
1212
description: See README at <https://github.com/jprupp/rocksdb-haskell#readme>
1313
category: Database, FFI

Diff for: src/Database/RocksDB/Base.hs

+7-7
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ withDBCF path config cf_cfgs f =
146146
write_opts = liftIO $ do
147147
when (createIfMissing config) $
148148
createDirectoryIfMissing True path
149-
null <$> listDirectory path >>= \case
150-
True -> create_new cf_names cf_opts opts_ptr read_opts write_opts
151-
False -> withCString path $ \path_ptr ->
149+
listDirectory path >>= \case
150+
[] -> create_new cf_names cf_opts opts_ptr read_opts write_opts
151+
_ -> withCString path $ \path_ptr ->
152152
withCString "default" $ \cf_deflt_name -> do
153153
pokeArray cf_names_array (cf_deflt_name : cf_names)
154154
pokeArray cf_opts_array (opts_ptr : cf_opts)
@@ -280,13 +280,13 @@ getCommon DB{rocksDB = db_ptr, readOpts = read_opts} mcf key = liftIO $
280280
Nothing -> c_rocksdb_get
281281
db_ptr read_opts
282282
key_ptr (intToCSize klen) vlen_ptr
283-
vlen <- peek vlen_ptr
284283
if val_ptr == nullPtr
285284
then return Nothing
286285
else do
287-
res' <- Just <$> BS.packCStringLen (val_ptr, cSizeToInt vlen)
286+
vlen <- peek vlen_ptr
287+
res <- Just <$> BS.packCStringLen (val_ptr, cSizeToInt vlen)
288288
freeCString val_ptr
289-
return res'
289+
return res
290290

291291
delete :: MonadIO m => DB -> ByteString -> m ()
292292
delete db = deleteCommon db Nothing
@@ -312,7 +312,7 @@ write DB{rocksDB = db_ptr, writeOpts = write_opts} batch = liftIO $
312312
throwIfErr "write" $ c_rocksdb_write db_ptr write_opts batch_ptr
313313
-- ensure @ByteString@s (and respective shared @CStringLen@s) aren't
314314
-- GC'ed until here
315-
mapM_ (liftIO . touch) batch
315+
mapM_ touch batch
316316
where
317317
batchAdd batch_ptr (Put key val) =
318318
BU.unsafeUseAsCStringLen key $ \(key_ptr, klen) ->

Diff for: src/Database/RocksDB/C.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ type RocksDB = Ptr LRocksDB
109109
type ColumnFamily = Ptr LColumnFamily
110110
type Options = Ptr LOptions
111111
type WriteBatch = Ptr LWriteBatch
112-
type SliceTransform = Ptr LSliceTransform
112+
type SliceTransform = Ptr LSliceTransform
113113
type ReadOpts = Ptr LReadOpts
114114
type WriteOpts = Ptr LWriteOpts
115115
type Snapshot = Ptr LSnapshot

Diff for: src/Database/RocksDB/Internal.hs

+23-37
Original file line numberDiff line numberDiff line change
@@ -59,45 +59,30 @@ instance Default Config where
5959
}
6060

6161
withOptions :: MonadUnliftIO m => Config -> (Options -> m a) -> m a
62-
withOptions Config {..} f =
63-
with_opts $ \opts -> do
64-
liftIO $ do
65-
slice <- bloom
66-
block_opts opts slice
67-
pfx_extract opts
68-
max_files opts
69-
c_rocksdb_options_set_create_if_missing
70-
opts (boolToCBool createIfMissing)
71-
c_rocksdb_options_set_error_if_exists
72-
opts (boolToCBool errorIfExists)
73-
c_rocksdb_options_set_paranoid_checks
74-
opts (boolToCBool paranoidChecks)
75-
f opts
62+
withOptions Config {..} f = with_opts $ \opts -> do
63+
liftIO $ do
64+
when bloomFilter $ do
65+
fp <- c_rocksdb_filterpolicy_create_bloom_full 10
66+
bo <- c_rocksdb_block_based_options_create
67+
c_rocksdb_block_based_options_set_filter_policy bo fp
68+
c_rocksdb_options_set_block_based_table_factory opts bo
69+
forM_ prefixLength $ \l -> do
70+
t <- c_rocksdb_slicetransform_create_fixed_prefix (intToCSize l)
71+
c_rocksdb_options_set_prefix_extractor opts t
72+
forM_ maxFiles $
73+
c_rocksdb_options_set_max_open_files opts . intToCInt
74+
c_rocksdb_options_set_create_if_missing
75+
opts (boolToCBool createIfMissing)
76+
c_rocksdb_options_set_error_if_exists
77+
opts (boolToCBool errorIfExists)
78+
c_rocksdb_options_set_paranoid_checks
79+
opts (boolToCBool paranoidChecks)
80+
f opts
7681
where
7782
with_opts =
7883
bracket
7984
(liftIO c_rocksdb_options_create)
8085
(liftIO . c_rocksdb_options_destroy)
81-
block_opts _ Nothing = return ()
82-
block_opts opts (Just slice) = liftIO $ do
83-
block <- c_rocksdb_block_based_options_create
84-
c_rocksdb_block_based_options_set_filter_policy block slice
85-
c_rocksdb_options_set_block_based_table_factory opts block
86-
bloom =
87-
if bloomFilter
88-
then Just <$> c_rocksdb_filterpolicy_create_bloom_full 10
89-
else return Nothing
90-
pfx_extract opts =
91-
case prefixLength of
92-
Nothing -> return ()
93-
Just len -> liftIO $ do
94-
p <- c_rocksdb_slicetransform_create_fixed_prefix
95-
(intToCSize len)
96-
c_rocksdb_options_set_prefix_extractor opts p
97-
max_files opts =
98-
case maxFiles of
99-
Nothing -> return ()
100-
Just i -> c_rocksdb_options_set_max_open_files opts (intToCInt i)
10186

10287

10388
withOptionsCF :: MonadUnliftIO m => [Config] -> ([Options] -> m a) -> m a
@@ -131,9 +116,10 @@ throwIfErr :: MonadUnliftIO m => String -> (ErrPtr -> m a) -> m a
131116
throwIfErr s f = alloca $ \err_ptr -> do
132117
liftIO $ poke err_ptr nullPtr
133118
res <- f err_ptr
134-
erra <- liftIO $ peek err_ptr
135-
when (erra /= nullPtr) $ do
136-
err <- liftIO $ peekCString erra
119+
err_cstr <- liftIO $ peek err_ptr
120+
when (err_cstr /= nullPtr) $ do
121+
err <- liftIO $ peekCString err_cstr
122+
liftIO $ free err_cstr
137123
throwIO $ userError $ s ++ ": " ++ err
138124
return res
139125

Diff for: stack.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
resolver: lts-16.5
1+
resolver: lts-21.11
22
nix:
33
packages:
44
- rocksdb

Diff for: stack.yaml.lock

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
packages: []
77
snapshots:
88
- completed:
9-
size: 531707
10-
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/16/5.yaml
11-
sha256: 9751e25e0af5713a53ddcfcc79564b082c71b1b357fadef0d85672a5b5ba3703
12-
original: lts-16.5
9+
sha256: 64d66303f927e87ffe6b8ccf736229bf608731e80d7afdf62bdd63c59f857740
10+
size: 640037
11+
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/21/11.yaml
12+
original: lts-21.11

Diff for: test/tests.hs

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ main = do
3737
it "should store and retrieve items from different column families" $
3838
withSystemTempDirectory "rocksdbcf1" $ \path ->
3939
withTestDBCF path ["two"] $ \db -> do
40-
let [two] = columnFamilies db
40+
let two = head $ columnFamilies db
4141
put db "one" "one"
4242
get db "one" `shouldReturn` Just "one"
4343
getCF db two "one" `shouldReturn` Nothing
@@ -57,5 +57,5 @@ main = do
5757
kvs = zip keys vals
5858
as1 <- mapM (\(k, v) -> async $ put db k v) kvs
5959
mapM_ wait as1
60-
as2 <- mapM (\k -> async $ get db k) keys
60+
as2 <- mapM (async . get db) keys
6161
mapM wait as2 `shouldReturn` map Just vals

0 commit comments

Comments
 (0)