From 74686b1380eecd220eacbbcfe0fb95377d708ec8 Mon Sep 17 00:00:00 2001 From: Kresten Krab Thorup Date: Mon, 30 Apr 2012 21:28:33 +0200 Subject: [PATCH] Implement merge hibernation for tail scan When scanning just one file (because all it's keys are after the ones in the other file), we also can need hibernation to save memory. Especially the bloom filters being built take a lot of mem. --- src/hanoi_merger.erl | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/hanoi_merger.erl b/src/hanoi_merger.erl index 0069b16..ddc7c18 100644 --- a/src/hanoi_merger.erl +++ b/src/hanoi_merger.erl @@ -155,6 +155,16 @@ scan(BT1, BT2, Out, IsLastLevel, [{Key1,Value1}|AT]=AKVs, [{Key2,Value2}|BT]=BKV scan(BT1, BT2, Out2, IsLastLevel, AT, BT, Count+1, step(Step, 2)) end. + +hibernate_scan_only(Keep) -> + erlang:garbage_collect(), + receive + {step, From, HowMany} -> + {BT, OutBin, IsLastLevel, KVs, Count, N} = erlang:binary_to_term( zlib:gunzip( Keep ) ), + scan_only(BT, hanoi_writer:deserialize(OutBin), IsLastLevel, KVs, Count, {N+HowMany, From}) + end. + + scan_only(BT, Out, IsLastLevel, KVs, Count, {N, FromPID}) when N < 1, KVs =/= [] -> case FromPID of none -> @@ -166,6 +176,10 @@ scan_only(BT, Out, IsLastLevel, KVs, Count, {N, FromPID}) when N < 1, KVs =/= [] receive {step, From, HowMany} -> scan_only(BT, Out, IsLastLevel, KVs, Count, {N+HowMany, From}) + after 10000 -> + Args = {BT, hanoi_writer:serialize(Out), IsLastLevel, KVs, Count, N}, + Keep = zlib:gzip ( erlang:term_to_binary( Args ) ), + hibernate_scan_only(Keep); end; scan_only(BT, Out, IsLastLevel, [], Count, {_, FromPID}=Step) ->