iilyak commented on a change in pull request #3766:
URL: https://github.com/apache/couchdb/pull/3766#discussion_r726995629
##########
File path: src/smoosh/src/smoosh_priority_queue.erl
##########
@@ -12,32 +12,68 @@
-module(smoosh_priority_queue).
--export([new/0, last_updated/2, is_key/2, in/4, in/5, out/1, size/1, info/1]).
+-export([new/1]).
+
+-export([open/1, last_updated/2, is_key/2, in/4, in/5, out/1, size/1, info/1]).
+
+-export([flush/1, close/1]).
+
+-export([from_list/3, to_list/1]).
+
+-export([file_name/1, write_to_file/1]).
+
+-define(VSN, 1).
-record(priority_queue, {
- dict=dict:new(),
- tree=gb_trees:empty()
+ name,
+ map,
+ tree
}).
-new() ->
- #priority_queue{}.
+new(Name) ->
+ #priority_queue{name=Name, map=maps:new(), tree=gb_trees:empty()}.
+
+open(#priority_queue{name=Name} = Q) ->
+ case do_open(file_name(Q)) of
+ {ok, Terms} ->
+ Tree = maps:fold(fun(Key, {TreeKey, Value}, TreeAcc) ->
+ gb_trees:enter(TreeKey, {Key, Value}, TreeAcc)
+ end, gb_trees:empty(), Terms),
+ Q#priority_queue{name=Name, map=Terms, tree=Tree};
+ {error} ->
+ Q
+ end.
+
+write_to_file(#priority_queue{map=Map} = Q) ->
+ OnDisk = <<?VSN, (term_to_binary(Map, [compressed, {minor_version,
1}]))/binary>>,
+ FileName = file_name(Q),
+ TmpFileName = FileName ++ ".tmp",
+ file:write_file(TmpFileName, OnDisk),
+ file:rename(TmpFileName, FileName).
Review comment:
do you need to call `file:delete(FileName)` and ignore result before
calling `rename/2`?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]