-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTestRedis.java
More file actions
311 lines (262 loc) · 12 KB
/
TestRedis.java
File metadata and controls
311 lines (262 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package com.cosmian.findex;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import com.cosmian.TestUtils;
import com.cosmian.jna.findex.Findex;
import com.cosmian.jna.findex.Interrupt;
import com.cosmian.jna.findex.ffi.KeywordSet;
import com.cosmian.jna.findex.ffi.SearchResults;
import com.cosmian.jna.findex.structs.IndexedValue;
import com.cosmian.jna.findex.structs.Keyword;
import com.cosmian.jna.findex.structs.Location;
import com.cosmian.utils.CloudproofException;
import redis.clients.jedis.Jedis;
public class TestRedis {
@BeforeAll
public static void before_all() {
TestUtils.initLogging();
}
@Test
public void testUpsertAndSearchRedis() throws Exception {
if (TestUtils.portAvailable(RedisEntryTable.redisHostname(), 6379)) {
throw new RuntimeException("Redis is down");
}
System.out.println("");
System.out.println("---------------------------------------");
System.out.println("Findex Upsert Redis");
System.out.println("---------------------------------------");
System.out.println("");
//
// Recover key and label
//
byte[] key = IndexUtils.generateKey();
assertEquals(16, key.length);
String label = IndexUtils.loadLabel();
//
// Recover test vectors
//
Set<Long> expectedDbLocations = IndexUtils.loadExpectedDBLocations();
//
// Build dataset with DB uids and words
//
UsersDataset[] testFindexDataset = IndexUtils.loadDatasets();
//
// Prepare Redis tables and users
//
try (RedisUserDb db = new RedisUserDb();
RedisEntryTable entryTable = new RedisEntryTable();
RedisChainTable chainTable = new RedisChainTable();) {
db.flush();
entryTable.flush();
chainTable.flush();
db.insertUsers(testFindexDataset);
System.out.println("After insertion: data_table size: " + chainTable.getAllKeys().size());
Findex findex = new Findex(key, label, entryTable, chainTable);
//
// Upsert
//
Map<IndexedValue, Set<Keyword>> indexedValuesAndWords = IndexUtils.index(testFindexDataset);
KeywordSet res = findex.add(indexedValuesAndWords);
int entryTableLength = entryTable.getAllKeys().size();
int chainTableLength = chainTable.getAllKeys().size();
assertEquals(583, res.getResults().size(), "wrong number of new upserted keywords");
assertEquals(583, entryTableLength, "wrong Entry Table length");
assertEquals(618, chainTableLength, "wrong Entry Table length");
System.out.println("After insertion: entry_table size: " + entryTableLength);
System.out.println("After insertion: chain_table size: " + chainTableLength);
//
// Upsert a new keyword
//
HashMap<IndexedValue, Set<Keyword>> newIndexedKeyword = new HashMap<>();
Set<Keyword> expectedKeywords = new HashSet<>();
expectedKeywords.add(new Keyword("test"));
newIndexedKeyword.put(new IndexedValue(new Location("ici")), expectedKeywords);
// It is returned the first time it is added.
Set<Keyword> newKeywords = findex.add(newIndexedKeyword).getResults();
assertEquals(expectedKeywords, newKeywords, "new keyword is not returned");
// It is *not* returned the second time it is added.
newKeywords = findex.add(newIndexedKeyword).getResults();
assert (newKeywords.isEmpty());
//
// Search
//
System.out.println("");
System.out.println("---------------------------------------");
System.out.println("Findex Search Redis");
System.out.println("---------------------------------------");
System.out.println("");
{
SearchResults searchResults = findex.search(new String[] {"France"});
assertEquals(expectedDbLocations, searchResults.getNumbers());
System.out.println("<== successfully found all original French locations");
}
// This compact should do nothing except changing the label since the users
// table didn't change.
findex.compact(key, "NewLabel");
System.out
.println("After first compact: entry_table size: " + entryTable.getAllKeys().size());
System.out
.println("After first compact: chain_table size: " + chainTable.getAllKeys().size());
{
// Search with new label and without user changes
SearchResults searchResults = findex.search(new String[] {"France"});
assertEquals(expectedDbLocations, searchResults.getNumbers());
System.out.println("<== successfully found all French locations with the new label");
}
//
// Compact
//
System.out.println("");
System.out.println("---------------------------------------");
System.out.println("Findex Re-Compact Sqlite");
System.out.println("---------------------------------------");
System.out.println("");
// Delete the user n°17 to test the compact indexes
db.deleteUser(17);
expectedDbLocations.remove(17l);
findex.compact(key, "NewLabel2", db);
{
// Search should return everyone but n°17
SearchResults searchResults = findex.search(new String[] {"France"});
assertEquals(expectedDbLocations, searchResults.getNumbers());
System.out
.println("<== successfully found all French locations after removing one and compacting");
}
}
}
@Test
public void testExceptions() throws Exception {
if (TestUtils.portAvailable(RedisEntryTable.redisHostname(), 6379)) {
throw new RuntimeException("Redis is down");
}
System.out.println("");
System.out.println("---------------------------------------");
System.out.println("Findex Exceptions in Redis");
System.out.println("---------------------------------------");
System.out.println("");
//
// Recover key and label
//
byte[] key = IndexUtils.generateKey();
assertEquals(16, key.length);
String label = IndexUtils.loadLabel();
//
// Build dataset with DB uids and words
//
UsersDataset[] testFindexDataset = IndexUtils.loadDatasets();
//
// Prepare Redis tables and users
//
try (RedisUserDb db = new RedisUserDb();
RedisEntryTable entryTable = new RedisEntryTable();
RedisChainTable chainTable = new RedisChainTable();) {
db.flush();
entryTable.flush();
chainTable.flush();
db.insertUsers(testFindexDataset);
Findex findex = new Findex(key, label, entryTable, chainTable);
Map<IndexedValue, Set<Keyword>> indexedValuesAndWords = IndexUtils.index(testFindexDataset);
findex.add(indexedValuesAndWords);
entryTable.shouldThrowInsideFetchEntries = true;
try {
findex.search(new String[] {"John"});
} catch (CloudproofException e) {
assertEquals("Should throw inside fetch entries", e.getMessage());
return;
}
throw new Exception("Should have throw");
}
}
@Test
public void testGraphUpsertAndSearchRedis() throws Exception {
if (TestUtils.portAvailable(RedisEntryTable.redisHostname(), 6379)) {
throw new RuntimeException("Redis is down");
}
System.out.println("");
System.out.println("---------------------------------------");
System.out.println("Findex Graph Upsert Redis");
System.out.println("---------------------------------------");
System.out.println("");
//
// Recover key and label
//
byte[] key = IndexUtils.generateKey();
assertEquals(16, key.length);
String label = IndexUtils.loadLabel();
//
// Build dataset with DB uids and words
//
UsersDataset[] testFindexDataset = IndexUtils.loadDatasets();
Map<IndexedValue, Set<Keyword>> additions = IndexUtils.index(testFindexDataset);
// add auto-completion for keywords 'Martin', 'Martena'
additions.put(new Keyword("Mart").toIndexedValue(), new HashSet<>(
Arrays.asList(new Keyword("Mar"))));
additions.put(new Keyword("Marti").toIndexedValue(), new HashSet<>(
Arrays.asList(new Keyword("Mart"))));
additions.put(new Keyword("Marte").toIndexedValue(), new HashSet<>(
Arrays.asList(new Keyword("Mart"))));
additions.put(new Keyword("Martin").toIndexedValue(), new HashSet<>(
Arrays.asList(new Keyword("Marti"))));
additions.put(new Keyword("Marten").toIndexedValue(), new HashSet<>(
Arrays.asList(new Keyword("Marte"))));
additions.put(new Keyword("Martena").toIndexedValue(), new HashSet<>(
Arrays.asList(new Keyword("Marten"))));
//
// Prepare Redis tables and users
//
try (RedisUserDb db = new RedisUserDb();
RedisEntryTable entryTable = new RedisEntryTable();
RedisChainTable chainTable = new RedisChainTable();) {
db.flush();
entryTable.flush();
chainTable.flush();
db.insertUsers(testFindexDataset);
System.out.println("After insertion: data_table size: " + db.getAllKeys().size());
Findex findex = new Findex(key, label, entryTable, chainTable);
//
// Upsert
//
findex.add(additions);
System.out.println("After insertion: entry_table size: " + entryTable.getAllKeys().size());
System.out.println("After insertion: chain_table size: " + chainTable.getAllKeys().size());
//
// Search
//
System.out.println("");
System.out.println("---------------------------------------");
System.out.println("Findex Graph Search Redis");
System.out.println("---------------------------------------");
System.out.println("");
{
SearchResults searchResults = findex.search(new String[] {"Mar"}, new Interrupt() {
@Override
public boolean interrupt(Map<Keyword, Set<IndexedValue>> results) throws CloudproofException {
Keyword key_marti = new Keyword("Marti");
Keyword key_marte = new Keyword("Marte");
if (results.containsKey(key_marti)) {
IndexedValue iv = results.get(key_marti).iterator().next();
assertEquals(new Keyword("Martin"), iv.getKeyword());
}
if (results.containsKey(key_marte)) {
IndexedValue iv = results.get(key_marte).iterator().next();
assertEquals(new Keyword("Marten"), iv.getKeyword());
}
return true;
}
});
assertEquals(3, searchResults.numberOfUniqueLocations());
System.out.println("<== successfully found all original French locations");
}
// delete all items
try (Jedis jedis = db.connect()) {
jedis.flushAll();
}
}
}
}