Only compress if compressed size is smaller

This commit is contained in:
Kresten Krab Thorup 2012-04-23 04:23:52 +02:00
parent d37b227936
commit 25b4099eec
2 changed files with 20 additions and 8 deletions

View file

@ -49,23 +49,30 @@ estimate_node_size_increment(_KVList,Key,Value) ->
-define(SNAPPY_COMPRESSION, 1).
-define(GZIP_COMPRESSION, 2).
encode_index_node(Level, KVList, Compress) ->
encode_index_node(KVList, Compress) ->
TermData = erlang:term_to_binary(KVList),
case Compress of
snappy ->
{ok, Snappied} = snappy:compress(TermData),
CompressedData = [?SNAPPY_COMPRESSION|Snappied];
if byte_size(Snappied) > byte_size(TermData) ->
OutData = [?NO_COMPRESSION|TermData];
true ->
OutData = [?SNAPPY_COMPRESSION|Snappied]
end;
gzip ->
CompressedData = [?GZIP_COMPRESSION|zlib:gzip(TermData)];
GZipData = zlib:gzip(TermData);
if byte_size(GZipData) > byte_size(TermData) ->
OutData = [?NO_COMPRESSION|TermData];
true ->
OutData = [?GZIP_COMPRESSION|GZipData]
end;
_ ->
CompressedData = [?NO_COMPRESSION|TermData]
OutData = [?NO_COMPRESSION|TermData]
end,
Size = erlang:iolist_size(CompressedData),
{ok, Size+6, [ <<(Size+2):32/unsigned, Level:16/unsigned>> | CompressedData ] }.
{ok, OutData}.
decode_index_node(Level, <<Tag, Data/binary>>) ->

View file

@ -187,8 +187,13 @@ add_record(Level, Key, Value, #state{ nodes=Nodes }=State) ->
close_node(#state{nodes=[#node{ level=Level, members=NodeMembers }|RestNodes], compress=Compress} = State) ->
OrderedMembers = lists:reverse(NodeMembers),
{ok, DataSize, Data} = hanoi_util:encode_index_node(Level, OrderedMembers, Compress),
{ok, BlockData} = hanoi_util:encode_index_node(OrderedMembers, Compress),
NodePos = State#state.index_file_pos,
BlockSize = byte_size(BlockData),
Data = [ <<(BlockSize+2):32/unsigned, Level:16/unsigned>> | BlockData ],
DataSize = BlockData + 6,
ok = file:write(State#state.index_file, Data),
{FirstKey, _} = hd(OrderedMembers),