protected int outOfRangeId() {
return elementStore.getIndexStart() + elementStore.capacity() + rand.nextInt(elementStore.capacity());
}
public void testApiBasics() throws Exception {
SimpleElement element;
SimpleElement elementRead;
int elementId = inRangeId();
element = new SimpleElement(elementId);
element.setTimestamp(System.currentTimeMillis());
element.setTerms(new String[] {"simple", "element"});
element.setScore(rand.nextFloat());
long scn = System.currentTimeMillis();
elementStore.setElement(elementId, element, scn);
elementStore.sync();
assertEquals(scn, elementStore.getLWMark());
assertEquals(elementStore.getLWMark(), elementStore.getHWMark());
elementRead = elementStore.getElement(elementId);
assertTrue(elementRead != null);
assertEquals(element.getElementId(), elementRead.getElementId());
assertEquals(element.getTimestamp(), elementRead.getTimestamp());
assertEquals(element.getScore(), elementRead.getScore());
assertTrue(Arrays.equals(element.getTerms(), elementRead.getTerms()));
scn++;
elementStore.deleteElement(elementId, scn);
assertTrue(elementStore.getElement(elementId) == null);
assertEquals(scn, elementStore.getHWMark());
assertTrue(elementStore.getLWMark() < elementStore.getHWMark());
elementStore.persist();
assertEquals(scn, elementStore.getLWMark());
assertEquals(elementStore.getLWMark(), elementStore.getHWMark());
elementId = outOfRangeId();
try {
elementRead = elementStore.getElement(elementId);
assertEquals(null, elementRead);
} catch(Exception e) {
assertEquals(ArrayIndexOutOfBoundsException.class, e.getClass());
}
ElementSerializer<SimpleElement> serializer = createSerializer();
byte[] elementBytes;
// Test delete/setElement/getElementBytes
elementId = inRangeId();
element.setElementId(elementId);
scn++;
elementStore.deleteElement(elementId, scn);
assertEquals(null, elementStore.getElement(elementId));
scn++;
elementStore.setElement(elementId, element, scn);
elementBytes = elementStore.getElementBytes(elementId);
SimpleElement deserializedElement = serializer.deserialize(elementBytes);
assertEquals(element.getElementId(), deserializedElement.getElementId());
assertTrue(Arrays.equals(element.getTerms(), deserializedElement.getTerms()));
// Test delete/setElementBytes/getElement
elementId = inRangeId();
element.setElementId(elementId);