-
Notifications
You must be signed in to change notification settings - Fork 174
Add support for snappy compression in Bitcask backend #108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
36794a9
f44bc6f
19dc7bf
f90c66f
b6d273c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -217,7 +217,8 @@ get(Ref, Key, TryNum) -> | |
| {ok, _Key, ?TOMBSTONE} -> | ||
| not_found; | ||
| {ok, _Key, Value} -> | ||
| {ok, Value}; | ||
| Val = decompress(Value), | ||
| {ok, Val}; | ||
| {error, eof} -> | ||
| not_found; | ||
| {error, _} = Err -> | ||
|
|
@@ -1222,9 +1223,11 @@ do_put(Key, Value, #bc_state{write_file = WriteFile} = State, Retries, _LastErr) | |
| end, | ||
|
|
||
| Tstamp = bitcask_time:tstamp(), | ||
|
|
||
| CValue = compress(Value, State#bc_state.opts), | ||
| {ok, WriteFile2, Offset, Size} = bitcask_fileops:write( | ||
| State2#bc_state.write_file, | ||
| Key, Value, Tstamp), | ||
| Key, CValue, Tstamp), | ||
| case bitcask_nifs:keydir_put(State2#bc_state.keydir, Key, | ||
| bitcask_fileops:file_tstamp(WriteFile2), | ||
| Size, Offset, Tstamp, true) of | ||
|
|
@@ -1333,6 +1336,53 @@ expiry_merge([File | Files], LiveKeyDir, Acc0) -> | |
| end, | ||
| expiry_merge(Files, LiveKeyDir, Acc). | ||
|
|
||
| enable_compression(Opts) -> | ||
| case get_opt(enable_compression, Opts) of | ||
| true -> | ||
| true; | ||
| _ -> | ||
| false | ||
| end. | ||
|
|
||
| compression_threshold(Opts) -> | ||
| case get_opt(compression_threshold, Opts) of | ||
| T when is_float(T) -> | ||
| T; | ||
| _ -> | ||
| 0.8 | ||
| end. | ||
|
|
||
| check_compression_threshold(Value, Compressed, Opts) -> | ||
| Threshold = compression_threshold(Opts), | ||
| case (byte_size(Compressed) / byte_size(Value)) of | ||
| Ratio when Ratio < Threshold -> | ||
| Compressed; | ||
| _ -> | ||
| Value | ||
| end. | ||
|
|
||
| compress(Value, Opts) -> | ||
| case enable_compression(Opts) of | ||
| true -> | ||
| case snappy:compress(Value) of | ||
| {ok, Compressed} -> | ||
| check_compression_threshold(Value, Compressed, Opts); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems to me to be a lot of extra work for a non-adaptive setting. I suspect that the better thing to do is to have a simple size threshold below which you don't bother to compress the data. |
||
| _ -> | ||
| Value | ||
| end; | ||
| false -> | ||
| Value | ||
| end. | ||
|
|
||
| decompress(Value) -> | ||
| case snappy:is_valid(Value) of | ||
| true -> | ||
| {ok, Val} = snappy:decompress(Value), | ||
| Val; | ||
| false -> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. may want to change |
||
| Value | ||
| end. | ||
|
|
||
| %% =================================================================== | ||
| %% EUnit tests | ||
| %% =================================================================== | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer these to be named maybe_compress and maybe_decompress to more clearly state intent.