Use backwards compatible term_to_binary (#408)

* Use backwards compatible term_to_binary

So that where we have hashed term_to_binary output in OTP25 or earlier, that has will be matched in OTP 26.

* Test reliability

If all keys are put in order, the max_slots may not be used, as the driver at L0 is penciller cache size, and merge to new files (managed by the parameter) only occurs when there are overlapping files the level below
This commit is contained in:
Martin Sumner 2023-10-05 10:33:20 +01:00 committed by GitHub
parent c4a32366df
commit b96518c32a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 32 additions and 16 deletions

View file

@ -11,6 +11,7 @@
integer_now/0,
integer_time/1,
magic_hash/1,
t2b/1,
safe_rename/4]).
-define(WRITE_OPS, [binary, raw, read, write]).
@ -53,7 +54,7 @@ magic_hash({binary, BinaryKey}) ->
H = 5381,
hash1(H, BinaryKey) band 16#FFFFFFFF;
magic_hash(AnyKey) ->
BK = term_to_binary(AnyKey),
BK = t2b(AnyKey),
magic_hash({binary, BK}).
hash1(H, <<>>) ->
@ -64,6 +65,17 @@ hash1(H, <<B:8/integer, Rest/bytes>>) ->
hash1(H2, Rest).
-spec t2b(term()) -> binary().
%% @doc
%% term_to_binary with options necessary to ensure backwards compatability
%% in the handling of atoms (within OTP 26).
%% See https://github.com/martinsumner/leveled/issues/407
%% If the binary() which is outputted is to be hashed for comparison, then
%% this must be used.
t2b(Term) ->
term_to_binary(Term, [{minor_version, 1}]).
-spec safe_rename(string(), string(), binary(), boolean()) -> ok.
%% @doc
%% Write a file, sync it and rename it (and for super-safe mode read it back)