-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
ReverseIndexFullConverterTest2.java
179 lines (138 loc) · 6.07 KB
/
ReverseIndexFullConverterTest2.java
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
package nu.marginalia.index.reverse;
import lombok.SneakyThrows;
import nu.marginalia.array.buffer.LongQueryBuffer;
import nu.marginalia.index.full.ReverseIndexFullConverter;
import nu.marginalia.index.full.ReverseIndexFullReader;
import nu.marginalia.index.journal.model.IndexJournalEntryData;
import nu.marginalia.index.journal.model.IndexJournalEntryHeader;
import nu.marginalia.index.journal.reader.IndexJournalReaderSingleCompressedFile;
import nu.marginalia.index.journal.writer.IndexJournalWriterImpl;
import nu.marginalia.index.journal.writer.IndexJournalWriter;
import nu.marginalia.index.priority.ReverseIndexPriorityParameters;
import nu.marginalia.lexicon.journal.KeywordLexiconJournalMode;
import nu.marginalia.ranking.DomainRankings;
import nu.marginalia.lexicon.KeywordLexicon;
import nu.marginalia.lexicon.journal.KeywordLexiconJournal;
import nu.marginalia.service.control.ServiceHeartbeat;
import nu.marginalia.service.control.ServiceTaskHeartbeat;
import nu.marginalia.test.TestUtil;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import static org.mockito.Mockito.when;
class ReverseIndexFullConverterTest2 {
KeywordLexicon keywordLexicon;
IndexJournalWriter writer;
Path indexFile;
Path wordsFile1;
Path urlsFile1;
Path dictionaryFile;
private final Logger logger = LoggerFactory.getLogger(getClass());
Path dataDir;
private Path wordsFile;
private Path docsFile;
int workSetSize = 8192;
int workSetStart = 8000;
@BeforeEach
@SneakyThrows
void setUp() {
dictionaryFile = Files.createTempFile("tmp", ".dict");
dictionaryFile.toFile().deleteOnExit();
keywordLexicon = new KeywordLexicon(new KeywordLexiconJournal(dictionaryFile.toFile(), KeywordLexiconJournalMode.READ_WRITE));
keywordLexicon.getOrInsert("0");
indexFile = Files.createTempFile("tmp", ".idx");
indexFile.toFile().deleteOnExit();
writer = new IndexJournalWriterImpl(keywordLexicon, indexFile);
wordsFile1 = Files.createTempFile("words1", ".idx");
urlsFile1 = Files.createTempFile("urls1", ".idx");
dataDir = Files.createTempDirectory(getClass().getSimpleName());
for (int i = 1; i < workSetSize; i++) {
if (i < workSetStart) {
keywordLexicon.getOrInsert(Integer.toString(i));
}
else {
createEntry(writer, keywordLexicon, i);
}
}
keywordLexicon.commitToDisk();
Thread.sleep(1000);
writer.close();
var reader = new IndexJournalReaderSingleCompressedFile(indexFile);
wordsFile = dataDir.resolve("words.dat");
docsFile = dataDir.resolve("docs.dat");
}
@AfterEach
public void tearDown() {
TestUtil.clearTempDir(dataDir);
}
public int[] getFactorsI(int id) {
return IntStream.rangeClosed(1, id-1).toArray();
}
public long[] getFactorsL(int id) {
return LongStream.rangeClosed(1, id).filter(v -> (id % v) == 0).toArray();
}
long createId(long url, long domain) {
return (domain << 32) | url;
}
public void createEntry(IndexJournalWriter writer, KeywordLexicon keywordLexicon, int id) {
int[] factors = getFactorsI(id);
var header = new IndexJournalEntryHeader(factors.length, 0, createId(id, id/20), id % 5);
long[] data = new long[factors.length*2];
for (int i = 0; i < factors.length; i++) {
data[2*i] = keywordLexicon.getOrInsert(Integer.toString(factors[i]));
data[2*i + 1] = (i % 21 != 0) ? 0 : -factors[i];
}
writer.put(header, new IndexJournalEntryData(data));
}
@Test
void testRev2() throws IOException {
Path tmpDir = Path.of("/tmp");
// RIP fairies
var serviceHeartbeat = Mockito.mock(ServiceHeartbeat.class);
when(serviceHeartbeat.createServiceTaskHeartbeat(Mockito.any(), Mockito.any()))
.thenReturn(Mockito.mock(ServiceTaskHeartbeat.class));
new ReverseIndexFullConverter(serviceHeartbeat, tmpDir, new IndexJournalReaderSingleCompressedFile(indexFile), new DomainRankings(), wordsFile, docsFile).convert();
var reverseReader = new ReverseIndexFullReader(wordsFile, docsFile);
for (int i = workSetStart; i < workSetSize; i++) {
var es = reverseReader.documents(i);
LongQueryBuffer lqb = new LongQueryBuffer(100);
while (es.hasMore()) {
lqb.reset();
es.read(lqb);
System.out.println(Arrays.toString(Arrays.copyOf(lqb.data, lqb.end)));
}
System.out.println("--");
}
TestUtil.clearTempDir(dataDir);
}
@Test
void testRevP() throws IOException {
Path tmpDir = Path.of("/tmp");
// RIP fairies
var serviceHeartbeat = Mockito.mock(ServiceHeartbeat.class);
when(serviceHeartbeat.createServiceTaskHeartbeat(Mockito.any(), Mockito.any()))
.thenReturn(Mockito.mock(ServiceTaskHeartbeat.class));
new ReverseIndexFullConverter(serviceHeartbeat, tmpDir, new IndexJournalReaderSingleCompressedFile(indexFile, null, ReverseIndexPriorityParameters::filterPriorityRecord), new DomainRankings(), wordsFile, docsFile).convert();
var reverseReader = new ReverseIndexFullReader(wordsFile, docsFile);
for (int i = workSetStart; i < workSetSize; i++) {
var es = reverseReader.documents(i);
LongQueryBuffer lqb = new LongQueryBuffer(100);
while (es.hasMore()) {
lqb.reset();
es.read(lqb);
System.out.println(Arrays.toString(Arrays.copyOf(lqb.data, lqb.end)));
}
System.out.println("--");
}
TestUtil.clearTempDir(dataDir);
}
}