package dovetaildb.bagindex;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import dovetaildb.bytes.ArrayBytes;
import dovetaildb.bytes.Bytes;
import dovetaildb.querynode.QueryNode;
import dovetaildb.querynode.QueryNode.NextStatus;
import dovetaildb.scan.Scanner;
import dovetaildb.util.Pair;
import dovetaildb.util.Util;
import junit.framework.TestCase;
import org.junit.Test;
public abstract class BagIndexTest extends TestCase {
protected abstract BagIndex createIndex();
protected Bytes t0 = new ArrayBytes(new byte[]{});
protected Bytes t1 = new ArrayBytes(new byte[]{'0','1'});
protected Bytes t2 = new ArrayBytes(new byte[]{'0','1','0','1','0','1'});
protected Bytes t34pre = new ArrayBytes(new byte[]{'1','2','3'});
protected Bytes t3 = new ArrayBytes(new byte[]{'1','2','3','4','5','6','7','8','0'});
protected Bytes t4 = new ArrayBytes(new byte[]{'1','2','3','4','5','6','7','8','9','0'});
protected Bytes t5 = new ArrayBytes(new byte[]{'2','4','6','8','0','0','1','1','0','0','1','1','0','0','1','1','0','0'});
protected File tempStoreDir = null;
protected BagIndex index;
@Override
public void setUp() {
index = createIndex();
tempStoreDir = Util.createTempDirectory("BagIndexTest-"+this.getClass().getSimpleName());
index.setHomedir(tempStoreDir.getAbsolutePath());
}
@Override
public void tearDown() {
if (tempStoreDir != null) {
Util.deleteDirectory(tempStoreDir);
}
}
public void checkEmpty(long revNum) {
assertEquals(null,index.getRange(t0, t1, t5, false, false, revNum));
assertEquals(null,index.getRange(t0, t3, t3, true, true, revNum));
assertEquals(null,index.getTerm(t1, revNum));
}
public void checkTwoInserts(long revNum) {
long doc0, doc1, doc2;
QueryNode s;
QueryNode allTerms = index.getRange(ArrayBytes.EMPTY_BYTES, null, null, false, false, revNum);
s = index.getTerm(t1, revNum);
assertTrue(s.next());
doc0 = s.doc();
assertFalse(s.next());
assertTrue(allTerms.skipTo(doc0));
assertEquals(0, allTerms.term().compareTo(t1));
assertEquals(NextStatus.NEXT_TERM, allTerms.nextTerm());
assertEquals(0, allTerms.term().compareTo(t2));
assertEquals(NextStatus.NEXT_TERM, allTerms.nextTerm());
assertEquals(0, allTerms.term().compareTo(t4));
assertEquals(NextStatus.NEXT_TERM, allTerms.nextTerm());
assertEquals(0, allTerms.term().compareTo(t5));
assertEquals(NextStatus.AT_END, allTerms.nextTerm());
s = index.getTerm(t1, revNum);
doc1 = s.doc();
assertTrue(s.next());
doc2 = s.doc();
assertFalse(s.next());
assertTrue(doc1 < doc2);
allTerms = index.getRange(ArrayBytes.EMPTY_BYTES, null, null, false, false, revNum);
assertTrue(allTerms.skipTo(doc1));
assertEquals(0, allTerms.term().compareTo(t1));
assertEquals(NextStatus.NEXT_TERM, allTerms.nextTerm());
assertEquals(0, allTerms.term().compareTo(t3));
assertEquals(NextStatus.NEXT_DOC, allTerms.nextTerm());
s = index.getRange(t0, t3, t4, true, true, revNum);
assertNull(s);
s = index.getRange(t0, t3, t4, true, false, revNum);
assertNotNull(s);
assertFalse(s.next());
s = index.getRange(t0, t3, t4, false, true, revNum);
assertNotNull(s);
assertFalse(s.next());
s = index.getRange(t0, t3, t4, false, false, revNum);
assertNotNull(s);
assertTrue(s.next());
assertFalse(s.next());
}
long initialEmptyRev, twoInsertsRev;
@Test
public void testAll() {
initialEmptyRev = index.getCurrentRevNum();
checkEmpty(initialEmptyRev);
// empty commit
checkEmpty(index.commitNewRev(new ArrayList<EditRec>()));
// test inserts
ArrayList<EditRec> edits = new ArrayList<EditRec>();
edits.add(new EditRec(1, t1));
edits.add(new EditRec(2, t1));
edits.add(new EditRec(2, t2));
edits.add(new EditRec(1, t3));
edits.add(new EditRec(2, t4));
edits.add(new EditRec(2, t5));
twoInsertsRev = index.commitNewRev(edits);
checkTwoInserts(twoInsertsRev);
checkEmpty(initialEmptyRev); // old rev should still appear empty
}
}