1+ use std:: collections:: BTreeMap ;
2+
13use auto_compressor:: {
2- manager:: run_compressor_on_room_chunk,
4+ manager:: { compress_chunks_of_database , run_compressor_on_room_chunk} ,
35 state_saving:: { connect_to_database, create_tables_if_needed} ,
46} ;
57use compressor_integration_tests:: {
68 add_contents_to_database, clear_compressor_state, database_collapsed_states_match_map,
79 database_structure_matches_map, empty_database,
8- map_builder:: { compressed_3_3_from_0_to_13_with_state, line_segments_with_state} ,
10+ map_builder:: {
11+ compressed_3_3_from_0_to_13_with_state, line_segments_with_state,
12+ structure_from_edges_with_state,
13+ } ,
914 setup_logger, DB_URL ,
1015} ;
1116use serial_test:: serial;
@@ -31,7 +36,7 @@ fn run_compressor_on_room_chunk_works() {
3136 clear_compressor_state ( ) ;
3237
3338 // compress in 3,3 level sizes by default
34- let default_levels = vec ! [ Level :: restore ( 3 , 0 , None ) , Level :: restore ( 3 , 0 , None ) ] ;
39+ let default_levels = vec ! [ Level :: new ( 3 ) , Level :: new ( 3 ) ] ;
3540
3641 // compress the first 7 groups in the room
3742 // structure should be the following afterwards
@@ -63,3 +68,163 @@ fn run_compressor_on_room_chunk_works() {
6368 // Check that the structure of the database matches the expected structure
6469 assert ! ( database_structure_matches_map( & expected) ) ;
6570}
71+
72+ #[ test]
73+ #[ serial( db) ]
74+ fn compress_chunks_of_database_compresses_multiple_rooms ( ) {
75+ setup_logger ( ) ;
76+ // This creates 2 with the following structure
77+ //
78+ // 0-1-2 3-4-5 6-7-8 9-10-11 12-13
79+ // (with room2's numbers shifted up 14)
80+ //
81+ // Each group i has state:
82+ // ('node','is', i)
83+ // ('group', j, 'seen') - for all j less than i in that room
84+ let initial1 = line_segments_with_state ( 0 , 13 ) ;
85+ let initial2 = line_segments_with_state ( 14 , 27 ) ;
86+
87+ empty_database ( ) ;
88+ add_contents_to_database ( "room1" , & initial1) ;
89+ add_contents_to_database ( "room2" , & initial2) ;
90+
91+ let mut client = connect_to_database ( DB_URL ) . unwrap ( ) ;
92+ create_tables_if_needed ( & mut client) . unwrap ( ) ;
93+ clear_compressor_state ( ) ;
94+
95+ // compress in 3,3 level sizes by default
96+ let default_levels = vec ! [ Level :: new( 3 ) , Level :: new( 3 ) ] ;
97+
98+ // Compress 4 chunks of size 8.
99+ // The first two should compress room1 and the second two should compress room2
100+ compress_chunks_of_database ( DB_URL , 8 , & default_levels, 4 ) . unwrap ( ) ;
101+
102+ // We are aiming for the following structure in the database for room1
103+ // i.e. groups 6 and 9 should have changed from initial map
104+ // N.B. this saves 11 rows
105+ //
106+ // 0 3\ 12
107+ // 1 4 6\ 13
108+ // 2 5 7 9
109+ // 8 10
110+ // 11
111+ //
112+ // Where each group i has state:
113+ // ('node','is', i)
114+ // ('group', j, 'seen') - for all j less than i
115+ let expected1 = compressed_3_3_from_0_to_13_with_state ( ) ;
116+
117+ // Check that the database still gives correct states for each group in room1
118+ assert ! ( database_collapsed_states_match_map( & initial1) ) ;
119+
120+ // Check that the structure of the database matches the expected structure for room1
121+ assert ! ( database_structure_matches_map( & expected1) ) ;
122+
123+ // room 2 should have the same structure but will all numbers shifted up by 14
124+ let expected_edges: BTreeMap < i64 , i64 > = vec ! [
125+ ( 15 , 14 ) ,
126+ ( 16 , 15 ) ,
127+ ( 18 , 17 ) ,
128+ ( 19 , 18 ) ,
129+ ( 20 , 17 ) ,
130+ ( 21 , 20 ) ,
131+ ( 22 , 21 ) ,
132+ ( 23 , 20 ) ,
133+ ( 24 , 23 ) ,
134+ ( 25 , 24 ) ,
135+ ( 27 , 26 ) ,
136+ ]
137+ . into_iter ( )
138+ . collect ( ) ;
139+
140+ let expected2 = structure_from_edges_with_state ( expected_edges, 14 , 27 ) ;
141+
142+ // Check that the database still gives correct states for each group in room2
143+ assert ! ( database_collapsed_states_match_map( & initial2) ) ;
144+
145+ // Check that the structure of the database matches the expected structure for room2
146+ assert ! ( database_structure_matches_map( & expected2) ) ;
147+ }
148+
149+ #[ test]
150+ #[ serial( db) ]
151+ fn compress_chunks_of_database_continues_where_it_left_off ( ) {
152+ setup_logger ( ) ;
153+ // This creates 2 with the following structure
154+ //
155+ // 0-1-2 3-4-5 6-7-8 9-10-11 12-13
156+ // (with room2's numbers shifted up 14)
157+ //
158+ // Each group i has state:
159+ // ('node','is', i)
160+ // ('group', j, 'seen') - for all j less than i in that room
161+ let initial1 = line_segments_with_state ( 0 , 13 ) ;
162+ let initial2 = line_segments_with_state ( 14 , 27 ) ;
163+
164+ empty_database ( ) ;
165+ add_contents_to_database ( "room1" , & initial1) ;
166+ add_contents_to_database ( "room2" , & initial2) ;
167+
168+ let mut client = connect_to_database ( DB_URL ) . unwrap ( ) ;
169+ create_tables_if_needed ( & mut client) . unwrap ( ) ;
170+ clear_compressor_state ( ) ;
171+
172+ // compress in 3,3 level sizes by default
173+ let default_levels = vec ! [ Level :: new( 3 ) , Level :: new( 3 ) ] ;
174+
175+ // Compress chunks of various sizes:
176+ //
177+ // These two should compress room1
178+ compress_chunks_of_database ( DB_URL , 8 , & default_levels, 1 ) . unwrap ( ) ;
179+ compress_chunks_of_database ( DB_URL , 100 , & default_levels, 1 ) . unwrap ( ) ;
180+ // These three should compress room2
181+ compress_chunks_of_database ( DB_URL , 1 , & default_levels, 2 ) . unwrap ( ) ;
182+ compress_chunks_of_database ( DB_URL , 5 , & default_levels, 1 ) . unwrap ( ) ;
183+ compress_chunks_of_database ( DB_URL , 5 , & default_levels, 1 ) . unwrap ( ) ;
184+
185+ // We are aiming for the following structure in the database for room1
186+ // i.e. groups 6 and 9 should have changed from initial map
187+ // N.B. this saves 11 rows
188+ //
189+ // 0 3\ 12
190+ // 1 4 6\ 13
191+ // 2 5 7 9
192+ // 8 10
193+ // 11
194+ //
195+ // Where each group i has state:
196+ // ('node','is', i)
197+ // ('group', j, 'seen') - for all j less than i
198+ let expected1 = compressed_3_3_from_0_to_13_with_state ( ) ;
199+
200+ // Check that the database still gives correct states for each group in room1
201+ assert ! ( database_collapsed_states_match_map( & initial1) ) ;
202+
203+ // Check that the structure of the database matches the expected structure for room1
204+ assert ! ( database_structure_matches_map( & expected1) ) ;
205+
206+ // room 2 should have the same structure but will all numbers shifted up by 14
207+ let expected_edges: BTreeMap < i64 , i64 > = vec ! [
208+ ( 15 , 14 ) ,
209+ ( 16 , 15 ) ,
210+ ( 18 , 17 ) ,
211+ ( 19 , 18 ) ,
212+ ( 20 , 17 ) ,
213+ ( 21 , 20 ) ,
214+ ( 22 , 21 ) ,
215+ ( 23 , 20 ) ,
216+ ( 24 , 23 ) ,
217+ ( 25 , 24 ) ,
218+ ( 27 , 26 ) ,
219+ ]
220+ . into_iter ( )
221+ . collect ( ) ;
222+
223+ let expected2 = structure_from_edges_with_state ( expected_edges, 14 , 27 ) ;
224+
225+ // Check that the database still gives correct states for each group in room2
226+ assert ! ( database_collapsed_states_match_map( & initial2) ) ;
227+
228+ // Check that the structure of the database matches the expected structure for room2
229+ assert ! ( database_structure_matches_map( & expected2) ) ;
230+ }
0 commit comments