Fix silly oversight with variable names, add better data dir prep.

This commit is contained in:
Gregory Burd 2012-04-15 16:56:19 -04:00
parent 4eaa02ac3f
commit e81a3480ab

View file

@ -90,20 +90,24 @@ start(Partition, Config) ->
ok; ok;
{error, {already_started, _}} -> {error, {already_started, _}} ->
ok; ok;
{error, Reason} -> {error, StartReason} ->
lager:error("Failed to init the lsm_btree backend: ~p", [Reason]), lager:error("Failed to init the lsm_btree backend: ~p", [StartReason]),
{error, Reason} {error, StartReason}
end, end,
case AppStart of case AppStart of
ok -> ok ->
ok = filelib:ensure_dir(filename:join([DataRoot, "x"])), case get_data_dir(DataRoot, integer_to_list(Partition)) of
DbName = filename:join(DataRoot, integer_to_list(Partition)), {ok, DataDir} ->
case lsm_btree:open(DbName) of case lsm_btree:open(DataDir) of
{ok, Tree} -> {ok, Tree} ->
{ok, #state{tree=Tree, partition=Partition}}; {ok, #state{tree=Tree, partition=Partition}};
{error, OpenReason}=OpenError -> {error, OpenReason}=OpenError ->
lager:error("Failed to open lsm_btree: ~p\n", [OpenReason]), lager:error("Failed to open lsm_btree: ~p\n", [OpenReason]),
OpenError OpenError
end;
{error, Reason} ->
lager:error("Failed to start lsm_btree backend: ~p\n", [Reason]),
{error, Reason}
end; end;
Error -> Error ->
Error Error
@ -121,8 +125,8 @@ stop(#state{tree=Tree}) ->
{ok, not_found, state()} | {ok, not_found, state()} |
{error, term(), state()}. {error, term(), state()}.
get(Bucket, Key, #state{tree=Tree}=State) -> get(Bucket, Key, #state{tree=Tree}=State) ->
Key = to_object_key(Bucket, Key), BKey = to_object_key(Bucket, Key),
case lsm_btree:get(Tree, Key) of case lsm_btree:get(Tree, BKey) of
{ok, Value} -> {ok, Value} ->
{ok, Value, State}; {ok, Value, State};
not_found -> not_found ->
@ -136,9 +140,9 @@ get(Bucket, Key, #state{tree=Tree}=State) ->
-spec put(riak_object:bucket(), riak_object:key(), [index_spec()], binary(), state()) -> -spec put(riak_object:bucket(), riak_object:key(), [index_spec()], binary(), state()) ->
{ok, state()} | {ok, state()} |
{error, term(), state()}. {error, term(), state()}.
put(Bucket, PrimaryKey, _IndexSpecs, Val, #state{tree=Tree}=State) -> put(Bucket, Key, _IndexSpecs, Val, #state{tree=Tree}=State) ->
Key = to_object_key(Bucket, PrimaryKey), BKey = to_object_key(Bucket, Key),
ok = lsm_btree:put(Tree, Key, Val), ok = lsm_btree:put(Tree, BKey, Val),
{ok, State}. {ok, State}.
%% @doc Delete an object from the lsm_btree backend %% @doc Delete an object from the lsm_btree backend
@ -146,8 +150,8 @@ put(Bucket, PrimaryKey, _IndexSpecs, Val, #state{tree=Tree}=State) ->
{ok, state()} | {ok, state()} |
{error, term(), state()}. {error, term(), state()}.
delete(Bucket, Key, _IndexSpecs, #state{tree=Tree}=State) -> delete(Bucket, Key, _IndexSpecs, #state{tree=Tree}=State) ->
Key = to_object_key(Bucket, Key), BKey = to_object_key(Bucket, Key),
case lsm_btree:delete(Tree, Key) of case lsm_btree:delete(Tree, BKey) of
ok -> ok ->
{ok, State}; {ok, State};
{error, Reason} -> {error, Reason} ->
@ -271,6 +275,19 @@ callback(_Ref, _Msg, State) ->
%% Internal functions %% Internal functions
%% =================================================================== %% ===================================================================
%% @private
%% Create the directory for this partition's LSM-BTree files
get_data_dir(DataRoot, Partition) ->
PartitionDir = filename:join([DataRoot, Partition]),
case filelib:ensure_dir(filename:join([filename:absname(DataRoot), Partition, "x"])) of
ok ->
{ok, PartitionDir};
{error, Reason} ->
lager:error("Failed to create lsm_btree dir ~s: ~p",
[PartitionDir, Reason]),
{error, Reason}
end.
%% @private %% @private
%% Return a function to fold over the buckets on this backend %% Return a function to fold over the buckets on this backend
fold_buckets_fun(FoldBucketsFun) -> fold_buckets_fun(FoldBucketsFun) ->