Skip to content

Commit

Permalink
clean up for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
justin committed Jun 4, 2010
1 parent 2462887 commit a1b4808
Show file tree
Hide file tree
Showing 8 changed files with 908 additions and 694 deletions.
23 changes: 13 additions & 10 deletions apps/luwak/src/luwak_block.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@

-include_lib("luwak/include/luwak.hrl").

-export([create/2, data/1, name/1]).
-export([create/2,
data/1,
name/1]).

create(Riak, Data) ->
Value = [
{data, Data},
{created, now()},
{type, block}],
Obj = riak_object:new(?N_BUCKET, skerl:hexhash(?HASH_LEN, Data), Value),
{Riak:put(Obj,2), Obj}.
Value = [
{data, Data},
{created, now()},
{type, block}
],
Obj = riak_object:new(?N_BUCKET, skerl:hexhash(?HASH_LEN, Data), Value),
{Riak:put(Obj,2), Obj}.

data(Val) when is_list(Val) ->
proplists:get_value(data, Val);
proplists:get_value(data, Val);
data(Object) ->
proplists:get_value(data, riak_object:get_value(Object)).
proplists:get_value(data, riak_object:get_value(Object)).

name(Object) ->
riak_object:key(Object).
riak_object:key(Object).
16 changes: 8 additions & 8 deletions apps/luwak/src/luwak_checksum.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
-export([sha1/2]).

sha1(Riak, File) ->
Length = luwak_file:length(Riak, File),
sha1_int(crypto:sha_init(), luwak_get_stream:start(Riak, File, 0, Length)).
Length = luwak_file:length(Riak, File),
sha1_int(crypto:sha_init(), luwak_get_stream:start(Riak, File, 0, Length)).

sha1_int(Ctx, GetStream) ->
case luwak_get_stream:recv(GetStream, 1000) of
{error, timeout} -> {error, timeout};
eos -> crypto:sha_final(Ctx);
{Data, _} -> sha1_int(crypto:sha_update(Ctx,Data),GetStream)
end.
case luwak_get_stream:recv(GetStream, 1000) of
{error, timeout} -> {error, timeout};
eos -> crypto:sha_final(Ctx);
{Data, _} -> sha1_int(crypto:sha_update(Ctx,Data),GetStream)
end.
199 changes: 108 additions & 91 deletions apps/luwak/src/luwak_file.erl
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
-module(luwak_file).

-export([create/3, create/4, set_attributes/3, get_attributes/1, exists/2,
delete/2, get/2, get_property/2, update_root/3, update_checksum/3,
name/1, length/2]).
-export([create/3,
create/4,
set_attributes/3,
get_attributes/1,
exists/2,
delete/2,
get/2,
get_property/2,
update_root/3,
update_checksum/3,
name/1,
length/2]).

-include_lib("luwak/include/luwak.hrl").

Expand All @@ -12,137 +21,145 @@
%% overwrite an existing file of the same name.
%% @equiv create(Riak, Name, [], Attributes)
create(Riak, Name, Attributes) when is_binary(Name) ->
create(Riak, Name, [], Attributes).
create(Riak, Name, [], Attributes).

%% @spec create(Riak :: riak(), Name :: binary(), Properties :: proplist(), Attributes :: dict())
%% @spec create(Riak :: riak(), Name :: binary(), Properties :: proplist(),
%% Attributes :: dict())
%% -> {ok, File :: luwak_file()}
%% @doc Create a luwak file handle with the given name and attributes.
%% Recognized properties:
%% {block_size, int()} - The maximum size of an individual data chunk in bytes. Default
%% is 1000000.
%% {tree_order, int()} - The maximum number of children for an individual tree node. Default
%% is 250.
%% {checksumming, boolean()} - This controls whether or not checksumming will occur. Checksumming
%% will leave a checksum of the most recent write request as a property
%% of the filehandle.
%% {block_size, int()} - The maximum size of an individual data chunk
%% in bytes. Default is 1000000.
%% {tree_order, int()} - The maximum number of children for an
%% individual tree node. Default is 250.
%% {checksumming, boolean()} - This controls whether or not checksumming
%% will occur. Checksumming will leave a
%% checksum of the most recent write request
%% as a property of the filehandle.
create(Riak, Name, Properties, Attributes) when is_binary(Name) ->
BlockSize = proplists:get_value(block_size, Properties, ?BLOCK_DEFAULT),
Order = proplists:get_value(tree_order, Properties, ?ORDER_DEFAULT),
Checksumming = proplists:get_value(checksumming, Properties, false),
if
Order < 2 -> throw("tree_order cannot be less than 2");
BlockSize < 1 -> throw("block_size cannot be less than 1");
true -> ok
end,
Value = [
{attributes, Attributes},
{block_size, BlockSize},
{created, now()},
{modified, now()},
{checksumming, Checksumming},
{checksum, undefined},
{tree_order, Order},
{ancestors, []},
{root, undefined}
],
Obj = riak_object:new(?O_BUCKET, Name, Value),
ok = Riak:put(Obj, 2),
{ok, Obj}.

%% @spec set_attributes(Riak :: riak(), Obj :: luwak_file(), Attributes :: dict())
BlockSize = proplists:get_value(block_size, Properties, ?BLOCK_DEFAULT),
Order = proplists:get_value(tree_order, Properties, ?ORDER_DEFAULT),
Checksumming = proplists:get_value(checksumming, Properties, false),
if
Order < 2 -> throw("tree_order cannot be less than 2");
BlockSize < 1 -> throw("block_size cannot be less than 1");
true -> ok
end,
Value = [
{attributes, Attributes},
{block_size, BlockSize},
{created, now()},
{modified, now()},
{checksumming, Checksumming},
{checksum, undefined},
{tree_order, Order},
{ancestors, []},
{root, undefined}
],
Obj = riak_object:new(?O_BUCKET, Name, Value),
ok = Riak:put(Obj, 2),
{ok, Obj}.

%% @spec set_attributes(Riak :: riak(), Obj :: luwak_file(),
%% Attributes :: dict())
%% -> {ok, NewFile}
%% @doc Sets the new attributes, saves them, and returns a new file handle.
set_attributes(Riak, Obj, Attributes) ->
Value = lists:keyreplace(attributes, 1, riak_object:get_value(Obj), {attributes, Attributes}),
Obj2 = riak_object:apply_updates(riak_object:update_value(Obj, Value)),
ok = Riak:put(Obj2, 2),
{ok, Obj2}.
Value = lists:keyreplace(attributes, 1, riak_object:get_value(Obj),
{attributes, Attributes}),
Obj2 = riak_object:apply_updates(riak_object:update_value(Obj, Value)),
ok = Riak:put(Obj2, 2),
{ok, Obj2}.

%% @spec get_attributes(Obj :: luwak_file()) -> dict()
%% @doc Gets the attribute dictionary from the file handle.
get_attributes(Obj) ->
proplists:get_value(attributes, riak_object:get_value(Obj)).
proplists:get_value(attributes, riak_object:get_value(Obj)).

%% @spec exists(Riak :: riak(), Name :: binary()) -> {ok, true} | {ok, false} | {error, Reason}
%% @spec exists(Riak :: riak(), Name :: binary())
%% -> {ok, true} | {ok, false} | {error, Reason}
%% @doc Checks for the existence of the named file.
exists(Riak, Name) ->
case Riak:get(?O_BUCKET, Name, 2) of
{ok, _Obj} -> {ok, true};
{error, notfound} -> {ok, false};
Err -> Err
end.
case Riak:get(?O_BUCKET, Name, 2) of
{ok, _Obj} -> {ok, true};
{error, notfound} -> {ok, false};
Err -> Err
end.

%% @spec length(Riak :: riak(), File :: luwak_file()) -> Length
%% @doc returns the length in bytes of the file.
length(Riak, File) ->
case get_property(File, root) of
undefined -> 0;
RootName ->
{ok, Node} = luwak_tree:get(Riak, RootName),
case Node of
#n{children=Children} -> luwak_tree_utils:blocklist_length(Children);
Block ->
byte_size(luwak_block:data(Block))
end
end.
case get_property(File, root) of
undefined ->
0;
RootName ->
{ok, Node} = luwak_tree:get(Riak, RootName),
case Node of
#n{children=Children} ->
luwak_tree_utils:blocklist_length(Children);
Block ->
byte_size(luwak_block:data(Block))
end
end.

%% @spec delete(Riak :: riak(), Name :: binary()) -> ok | {error, Reason}
%% @doc deletes the named file from luwak. This is a fast operation since
%% the blocks and tree for that file remain untouched. A GC operation
%% (not yet implemented) will be required to clean them up properly.
delete(Riak, Name) ->
Riak:delete(?O_BUCKET, Name, 2).
Riak:delete(?O_BUCKET, Name, 2).

%% @spec get(Riak :: riak(), Name :: binary()) -> {ok, File} | {error, Reason}
%% @doc returns a filehandle for the named file.
get(Riak, Name) ->
Riak:get(?O_BUCKET, Name, 2).
Riak:get(?O_BUCKET, Name, 2).

%% @spec get_property(Obj :: luwak_file(), PropName :: atom()) -> Property
%% @doc retrieves the named property from the filehandle.
get_property(Obj, type) ->
case riak_object:get_value(Obj) of
List when is_list(List) -> proplists:get_value(type, List);
#n{} -> node
end;
case riak_object:get_value(Obj) of
List when is_list(List) -> proplists:get_value(type, List);
#n{} -> node
end;
get_property(Obj, links) ->
case riak_object:get_value(Obj) of
List when is_list(List) -> proplists:get_value(links, List);
#n{children=Children} -> Children
end;
case riak_object:get_value(Obj) of
List when is_list(List) -> proplists:get_value(links, List);
#n{children=Children} -> Children
end;
get_property(Obj, PropName) ->
case riak_object:get_value(Obj) of
List when is_list(List) -> proplists:get_value(PropName, List);
_ -> undefined
end.
case riak_object:get_value(Obj) of
List when is_list(List) -> proplists:get_value(PropName, List);
_ -> undefined
end.

%% @private
update_root(Riak, Obj, NewRoot) ->
ObjVal1 = riak_object:get_value(Obj),
OldRoot = proplists:get_value(root, ObjVal1),
Ancestors = proplists:get_value(ancestors, ObjVal1),
ObjVal2 = lists:keyreplace(ancestors, 1, ObjVal1,
{ancestors, [OldRoot|Ancestors]}),
ObjVal3 = lists:keyreplace(root, 1, ObjVal2, {root, NewRoot}),
Obj2 = riak_object:apply_updates(riak_object:update_value(Obj, ObjVal3)),
ok = Riak:put(Obj2, 2),
{ok, Obj2}.
ObjVal1 = riak_object:get_value(Obj),
OldRoot = proplists:get_value(root, ObjVal1),
Ancestors = proplists:get_value(ancestors, ObjVal1),
ObjVal2 = lists:keyreplace(ancestors, 1, ObjVal1,
{ancestors, [OldRoot|Ancestors]}),
ObjVal3 = lists:keyreplace(root, 1, ObjVal2, {root, NewRoot}),
Obj2 = riak_object:apply_updates(riak_object:update_value(Obj, ObjVal3)),
ok = Riak:put(Obj2, 2),
{ok, Obj2}.

%% @private
update_checksum(Riak, Obj, ChecksumFun) ->
case get_property(Obj, checksumming) of
true ->
ObjVal1 = riak_object:get_value(Obj),
ObjVal2 = lists:keyreplace(checksum, 1, ObjVal1,
{checksum, {sha1, ChecksumFun()}}),
Obj2 = riak_object:apply_updates(riak_object:update_value(Obj, ObjVal2)),
ok = Riak:put(Obj2, 2),
{ok, Obj2};
_ ->
{ok, Obj}
case get_property(Obj, checksumming) of
true ->
ObjVal1 = riak_object:get_value(Obj),
ObjVal2 = lists:keyreplace(checksum, 1, ObjVal1,
{checksum, {sha1, ChecksumFun()}}),
Obj2 = riak_object:apply_updates(riak_object:update_value(Obj,
ObjVal2)),
ok = Riak:put(Obj2, 2),
{ok, Obj2};
_ ->
{ok, Obj}
end.

%% @spec name(Obj :: luwak_file()) -> binary()
%% @doc returns the name of the given file handle.
name(Obj) ->
riak_object:key(Obj).
riak_object:key(Obj).
Loading

0 comments on commit a1b4808

Please sign in to comment.