Skip to content
This repository
  • 5 commits
  • 2 files changed
  • 0 comments
  • 1 contributor
BIN  rebar
Binary file not shown
92  src/riak_kv_eleveldb_backend.erl
@@ -54,6 +54,7 @@
54 54
 
55 55
 -record(state, {ref :: reference(),
56 56
                 data_root :: string(),
  57
+                open_opts = [],
57 58
                 config :: config(),
58 59
                 read_opts = [],
59 60
                 write_opts = [],
@@ -93,14 +94,12 @@ start(Partition, Config) ->
93 94
     %% Get the data root directory
94 95
     DataDir = filename:join(app_helper:get_prop_or_env(data_root, Config, eleveldb),
95 96
                             integer_to_list(Partition)),
96  
-    case open_db(DataDir, Config) of
97  
-        {ok, Ref} ->
98  
-            {ok, #state { ref = Ref,
99  
-                          data_root = DataDir,
100  
-                          read_opts = config_value(read_options, Config, []),
101  
-                          write_opts = config_value(write_options, Config, []),
102  
-                          fold_opts = config_value(fold_options, Config, [{fill_cache, false}]),
103  
-                          config = Config }};
  97
+
  98
+    %% Initialize state
  99
+    S0 = init_state(DataDir, Config),
  100
+    case open_db(S0) of
  101
+        {ok, State} ->
  102
+            {ok, State};
104 103
         {error, Reason} ->
105 104
             {error, Reason}
106 105
     end.
@@ -277,17 +276,17 @@ fold_objects(FoldObjectsFun, Acc, Opts, #state{fold_opts=FoldOpts,
277 276
 %% @doc Delete all objects from this eleveldb backend
278 277
 %% and return a fresh reference.
279 278
 -spec drop(state()) -> {ok, state()} | {error, term(), state()}.
280  
-drop(#state{data_root=DataRoot}=State) ->
281  
-    case eleveldb:destroy(DataRoot, []) of
  279
+drop(State0) ->
  280
+    case eleveldb:destroy(State0#state.data_root, []) of
282 281
         ok ->
283  
-            case open_db(DataRoot, State#state.config) of
284  
-                {ok, Ref} ->
285  
-                    {ok, State#state { ref = Ref }};
  282
+            case open_db(State0) of
  283
+                {ok, State} ->
  284
+                    {ok, State};
286 285
                 {error, Reason} ->
287  
-                    {error, Reason, State}
  286
+                    {error, Reason, State0}
288 287
             end;
289 288
         {error, Reason} ->
290  
-            {error, Reason, State}
  289
+            {error, Reason, State0}
291 290
     end.
292 291
 
293 292
 %% @doc Returns true if this eleveldb backend contains any
@@ -312,40 +311,61 @@ callback(_Ref, _Msg, State) ->
312 311
 %% ===================================================================
313 312
 
314 313
 %% @private
315  
-open_db(DataRoot, Config) ->
  314
+init_state(DataRoot, Config) ->
316 315
     %% Get the data root directory
317 316
     filelib:ensure_dir(filename:join(DataRoot, "dummy")),
318 317
 
  318
+    %% Merge the proplist passed in from Config with any values specified by the
  319
+    %% eleveldb app level; precedence is given to the Config.
  320
+    MergedConfig = orddict:merge(fun(_K, VLocal, _VGlobal) -> VLocal end,
  321
+                                 orddict:from_list(Config), % Local
  322
+                                 orddict:from_list(application:get_all_env(eleveldb))), % Global
  323
+
319 324
     %% Use a variable write buffer size in order to reduce the number
320 325
     %% of vnodes that try to kick off compaction at the same time
321 326
     %% under heavy uniform load...
322  
-    WriteBufferMin = config_value(write_buffer_size_min, Config, 3 * 1024 * 1024),
323  
-    WriteBufferMax = config_value(write_buffer_size_max, Config, 6 * 1024 * 1024),
  327
+    WriteBufferMin = config_value(write_buffer_size_min, MergedConfig, 3 * 1024 * 1024),
  328
+    WriteBufferMax = config_value(write_buffer_size_max, MergedConfig, 6 * 1024 * 1024),
324 329
     WriteBufferSize = WriteBufferMin + random:uniform(1 + WriteBufferMax - WriteBufferMin),
325 330
 
326  
-    %% Assemble options...
327  
-    Options = [
328  
-               {create_if_missing, true},
329  
-               {write_buffer_size, WriteBufferSize},
330  
-               {max_open_files, config_value(max_open_files, Config)},
331  
-               {cache_size, config_value(cache_size, Config)},
332  
-               {paranoid_checks, config_value(paranoid_checks, Config)}
333  
-              ],
334  
-
335  
-    lager:debug("Opening LevelDB in ~s with options: ~p\n", [DataRoot, Options]),
336  
-    eleveldb:open(DataRoot, Options).
337  
-
  331
+    %% Update the write buffer size in the merged config and make sure create_if_missing is set
  332
+    %% to true
  333
+    FinalConfig = orddict:store(write_buffer_size, WriteBufferSize,
  334
+                                orddict:store(create_if_missing, true, MergedConfig)),
  335
+
  336
+    %% Parse out the open/read/write options
  337
+    {OpenOpts, _BadOpenOpts} = eleveldb:validate_options(open, FinalConfig),
  338
+    {ReadOpts, _BadReadOpts} = eleveldb:validate_options(read, FinalConfig),
  339
+    {WriteOpts, _BadWriteOpts} = eleveldb:validate_options(write, FinalConfig),
  340
+
  341
+    %% Use read options for folding, but FORCE fill_cache to false
  342
+    FoldOpts = lists:keystore(fill_cache, 1, ReadOpts, {fill_cache, false}),
  343
+
  344
+    %% Generate a debug message with the options we'll use for each operation
  345
+    lager:debug("Datadir ~s options for LevelDB: ~p\n",
  346
+                [DataRoot, [{open, OpenOpts}, {read, ReadOpts}, {write, WriteOpts}, {fold, FoldOpts}]]),
  347
+    #state { data_root = DataRoot,
  348
+             open_opts = OpenOpts,
  349
+             read_opts = ReadOpts,
  350
+             write_opts = WriteOpts,
  351
+             fold_opts = FoldOpts,
  352
+             config = FinalConfig }.
338 353
 
339 354
 %% @private
340  
-config_value(Key, Config) ->
341  
-    config_value(Key, Config, undefined).
  355
+open_db(State0) ->
  356
+    case eleveldb:open(State0#state.data_root, State0#state.open_opts) of
  357
+        {ok, Ref} ->
  358
+            {ok, State0#state { ref = Ref }};
  359
+        {error, Reason} ->
  360
+            {error, Reason}
  361
+    end.
342 362
 
343 363
 %% @private
344 364
 config_value(Key, Config, Default) ->
345  
-    case proplists:get_value(Key, Config) of
346  
-        undefined ->
347  
-            app_helper:get_env(eleveldb, Key, Default);
348  
-        Value ->
  365
+    case orddict:find(Key, Config) of
  366
+        error ->
  367
+            Default;
  368
+        {ok, Value} ->
349 369
             Value
350 370
     end.
351 371
 

No commit comments for this range

Something went wrong with that request. Please try again.