iilyak commented on a change in pull request #3766:
URL: https://github.com/apache/couchdb/pull/3766#discussion_r731740470



##########
File path: src/smoosh/src/smoosh_priority_queue.erl
##########
@@ -73,14 +113,53 @@ info(#priority_queue{tree=Tree}=Q) ->
              [{min, Min}, {max, Max}]
      end].
 
+from_list(Orddict, #priority_queue{name=Name}) ->
+    Map = maps:from_list(Orddict),
+    Tree = gb_trees:from_orddict(Orddict),
+    #priority_queue{name=Name, map=Map, tree=Tree}.
+
+to_list(#priority_queue{tree=Tree}) ->
+    gb_trees:to_list(Tree).
+
+file_name(#priority_queue{name=Name}) ->
+    filename:join(".", Name ++ ".queue").
+
 truncate(infinity, Q) ->
     Q;
 truncate(Capacity, Q) when Capacity > 0 ->
     truncate(Capacity, ?MODULE:size(Q), Q).
 
 truncate(Capacity, Size, Q) when Size =< Capacity ->
+    write_to_file(Q),
     Q;
-truncate(Capacity, Size, #priority_queue{dict=Dict, tree=Tree}) when Size > 0 
->
+truncate(Capacity, Size, #priority_queue{name=Name, map=Map, tree=Tree}) when 
Size > 0 ->
     {_, {Key, _}, Tree1} = gb_trees:take_smallest(Tree),
-    Q1 = #priority_queue{dict=dict:erase(Key, Dict), tree=Tree1},
+    Q1 = #priority_queue{name=Name, map=maps:remove(Key, Map), tree=Tree1},
     truncate(Capacity, ?MODULE:size(Q1), Q1).
+
+do_open(FilePath) ->
+    case file:read_file(FilePath) of
+        {ok, Content} ->
+            <<Vsn, Binary/binary>> = Content,
+            try parse_queue(Vsn, ?VSN, Binary) of
+                Bin -> 
+                    file:delete(FilePath),
+                    {ok, Bin}
+            catch
+                error:_ ->
+                    couch_log:notice(
+                        "Invalid queue file. Deleting: ~p...", [FilePath]),
+                    file:delete(FilePath),
+                    {error}
+            end;
+        {error, _} ->
+            couch_log:notice(
+                "Cannot read the queue file. Deleting: ~p...", [FilePath]),

Review comment:
       It would be better to log the reason.




-- 
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]


Reply via email to