use of org.apache.cassandra.index.sasi.utils.CombinedTerm in project cassandra by apache.
the class OnDiskIndexTest method testCombiningOfThePartitionedSA.
@Test
public void testCombiningOfThePartitionedSA() throws Exception {
OnDiskIndexBuilder builderA = new OnDiskIndexBuilder(UTF8Type.instance, LongType.instance, OnDiskIndexBuilder.Mode.PREFIX);
OnDiskIndexBuilder builderB = new OnDiskIndexBuilder(UTF8Type.instance, LongType.instance, OnDiskIndexBuilder.Mode.PREFIX);
TreeMap<Long, TreeMap<Long, LongSet>> expected = new TreeMap<>();
for (long i = 0; i <= 100; i++) {
TreeMap<Long, LongSet> offsets = expected.get(i);
if (offsets == null)
expected.put(i, (offsets = new TreeMap<>()));
builderA.add(LongType.instance.decompose(i), keyAt(i), i);
putAll(offsets, keyBuilder(i));
}
for (long i = 50; i < 100; i++) {
TreeMap<Long, LongSet> offsets = expected.get(i);
if (offsets == null)
expected.put(i, (offsets = new TreeMap<>()));
long position = 100L + i;
builderB.add(LongType.instance.decompose(i), keyAt(position), position);
putAll(offsets, keyBuilder(100L + i));
}
File indexA = File.createTempFile("on-disk-sa-partition-a", ".db");
indexA.deleteOnExit();
File indexB = File.createTempFile("on-disk-sa-partition-b", ".db");
indexB.deleteOnExit();
builderA.finish(indexA);
builderB.finish(indexB);
OnDiskIndex a = new OnDiskIndex(indexA, LongType.instance, new KeyConverter());
OnDiskIndex b = new OnDiskIndex(indexB, LongType.instance, new KeyConverter());
RangeIterator<OnDiskIndex.DataTerm, CombinedTerm> union = OnDiskIndexIterator.union(a, b);
TreeMap<Long, TreeMap<Long, LongSet>> actual = new TreeMap<>();
while (union.hasNext()) {
CombinedTerm term = union.next();
Long composedTerm = LongType.instance.compose(term.getTerm());
TreeMap<Long, LongSet> offsets = actual.get(composedTerm);
if (offsets == null)
actual.put(composedTerm, (offsets = new TreeMap<>()));
putAll(offsets, term.getTokenTreeBuilder());
}
Assert.assertEquals(actual, expected);
File indexC = File.createTempFile("on-disk-sa-partition-final", ".db");
indexC.deleteOnExit();
OnDiskIndexBuilder combined = new OnDiskIndexBuilder(UTF8Type.instance, LongType.instance, OnDiskIndexBuilder.Mode.PREFIX);
combined.finish(Pair.create(keyAt(0).getKey(), keyAt(100).getKey()), indexC, new CombinedTermIterator(a, b));
OnDiskIndex c = new OnDiskIndex(indexC, LongType.instance, new KeyConverter());
union = OnDiskIndexIterator.union(c);
actual.clear();
while (union.hasNext()) {
CombinedTerm term = union.next();
Long composedTerm = LongType.instance.compose(term.getTerm());
TreeMap<Long, LongSet> offsets = actual.get(composedTerm);
if (offsets == null)
actual.put(composedTerm, (offsets = new TreeMap<>()));
putAll(offsets, term.getTokenTreeBuilder());
}
Assert.assertEquals(actual, expected);
a.close();
b.close();
}
use of org.apache.cassandra.index.sasi.utils.CombinedTerm in project cassandra by apache.
the class TokenTreeTest method testMergingOfEqualTokenTrees.
public void testMergingOfEqualTokenTrees(SortedMap<Long, LongSet> tokensMap) throws Exception {
TokenTreeBuilder tokensA = new DynamicTokenTreeBuilder(tokensMap);
TokenTreeBuilder tokensB = new DynamicTokenTreeBuilder(tokensMap);
TokenTree a = buildTree(tokensA);
TokenTree b = buildTree(tokensB);
TokenTreeBuilder tokensC = new StaticTokenTreeBuilder(new CombinedTerm(null, null) {
public RangeIterator<Long, Token> getTokenIterator() {
RangeIterator.Builder<Long, Token> union = RangeUnionIterator.builder();
union.add(a.iterator(new KeyConverter()));
union.add(b.iterator(new KeyConverter()));
return union.build();
}
});
TokenTree c = buildTree(tokensC);
Assert.assertEquals(tokensMap.size(), c.getCount());
Iterator<Token> tokenIterator = c.iterator(KEY_CONVERTER);
Iterator<Map.Entry<Long, LongSet>> listIterator = tokensMap.entrySet().iterator();
while (tokenIterator.hasNext() && listIterator.hasNext()) {
Token treeNext = tokenIterator.next();
Map.Entry<Long, LongSet> listNext = listIterator.next();
Assert.assertEquals(listNext.getKey(), treeNext.get());
Assert.assertEquals(convert(listNext.getValue()), convert(treeNext));
}
for (Map.Entry<Long, LongSet> entry : tokensMap.entrySet()) {
TokenTree.OnDiskToken result = c.get(entry.getKey(), KEY_CONVERTER);
Assert.assertNotNull("failed to find object for token " + entry.getKey(), result);
LongSet found = result.getOffsets();
Assert.assertEquals(entry.getValue(), found);
}
}
Aggregations