@@ -78,6 +78,51 @@ defmodule Electric.ShapeCache.FileStorage.CompactionTest do
7878 |> length == 4
7979 end
8080
81+ test "compacts a log file with replica mode full" , % { tmp_dir: tmp_dir } do
82+ log_file_path = Path . join ( tmp_dir , "log_file" )
83+
84+ log_stream =
85+ [
86+ ins ( offset: { 1 , 1 } , rec: [ id: "key1" , value: "value1" ] ) ,
87+ upd ( offset: { 2 , 1 } , rec: [ id: "key1" , value: { "value1" , "value2" } ] ) ,
88+ ins ( offset: { 3 , 1 } , rec: [ id: "key2" , value: "value3" ] ) ,
89+ upd ( offset: { 4 , 1 } , rec: [ id: "key1" , value: { "value2" , "value new 1" } ] ) ,
90+ upd ( offset: { 5 , 1 } , rec: [ id: "key1" , value: { "value new 1" , "value new 2" } ] ) ,
91+ upd ( offset: { 6 , 1 } , rec: [ id: "key1" , value: { "value new 2" , "value new 3" } ] ) ,
92+ upd ( offset: { 7 , 1 } , rec: [ id: "key1" , value: { "value new 3" , "value new 4" } ] ) ,
93+ upd ( offset: { 8 , 1 } , rec: [ id: "key1" , value: { "value new 4" , "value new 5" } ] ) ,
94+ del ( offset: { 9 , 1 } , rec: [ id: "key2" , value: "value" ] )
95+ ]
96+ |> TestUtils . changes_to_log_items ( replica: :full )
97+
98+ paths = LogFile . write_log_file ( log_stream , log_file_path )
99+
100+ assert LogFile . read_chunk ( paths , % LogOffset { tx_offset: 0 , op_offset: 0 } )
101+ |> Enum . to_list ( )
102+ |> length == 9
103+
104+ assert { log_file_path , chunk_index_path , key_index_path } =
105+ Compaction . compact_in_place ( paths , 1_000_000 )
106+
107+ assert File . exists? ( log_file_path )
108+ assert File . exists? ( chunk_index_path )
109+ assert File . exists? ( key_index_path )
110+
111+ assert [
112+ % { "headers" => % { "operation" => "insert" } } ,
113+ % { "headers" => % { "operation" => "insert" } } ,
114+ % {
115+ "headers" => % { "operation" => "update" } ,
116+ "value" => % { "id" => "key1" , "value" => "value new 5" } ,
117+ "old_value" => % { "value" => "value1" }
118+ } ,
119+ % { "headers" => % { "operation" => "delete" } }
120+ ] =
121+ LogFile . read_chunk ( paths , % LogOffset { tx_offset: 0 , op_offset: 0 } )
122+ |> Enum . to_list ( )
123+ |> Enum . map ( & Jason . decode! / 1 )
124+ end
125+
81126 test "compacts a large enough log file full of updates (failing property)" , % {
82127 tmp_dir: tmp_dir
83128 } do
0 commit comments