String msgBase = StringTools.rightPad("DATA-", msgSize, 'X');
BlockBasedDataStoreTools.create(storeId, new File("target/test"),
200, blockSize, true);
LinkedDataStore store = createStore(storeId, new File("target/test"));
// -------------------------------------------------------------------
// Insert some messages
long startTime = System.currentTimeMillis();
int previous = -1;
for (int n = 0; n < msgCount; n++)
{
byte[] data = (msgBase + n).getBytes();
previous = store.store(data, previous);
if (previous == -1)
throw new IllegalStateException("No space left !");
// System.out.println(store);
}
store.commitChanges();
long endTime = System.currentTimeMillis();
System.out.println("Insertion time " + (endTime - startTime) + " ms");
//System.out.println(store);
store.close();
// -------------------------------------------------------------------
// Reload store
store = createStore(storeId, new File("target/test"));
//System.out.println(store);
int count = 0;
int current = store.first();
while (current != -1)
{
store.retrieve(current);
current = store.next(current);
count++;
}
// System.out.println(store);
assertEquals(msgCount, count);
assertEquals(msgCount, store.size());
// System.out.println(store);
// -------------------------------------------------------------------
// Delete half the messages
count = 0;
current = store.first();
while (current != -1)
{
int next = store.next(current);
if (next != -1)
next = store.next(next);
store.delete(current);
count++;
current = next;
}
store.commitChanges();
assertEquals(msgCount - count, store.size());
// System.out.println(store);
store.close();
// -------------------------------------------------------------------
// Reload store
store = createStore(storeId, new File("target/test"));
assertEquals(msgCount - count, store.size());
//System.out.println(store);
// -------------------------------------------------------------------
// Add back some messages
int pos = -1;
for (int n = 0; n < count; n++)
{
byte[] data = (msgBase + n).getBytes();
previous = store.store(data, pos);
if (previous == -1)
throw new IllegalStateException("No space left !");
pos = store.next(previous);
if (pos != -1)
pos = store.next(pos);
// pos+=2+data.length/BLOCK_SIZE;
}
store.commitChanges();
assertEquals(msgCount, store.size());
System.out.println(store);
store.close();
// -------------------------------------------------------------------
// Reload store
store = createStore(storeId, new File("target/test"));
assertEquals(msgCount, store.size());
System.out.println(store);
count = 0;
current = store.first();
while (current != -1)
{
store.retrieve(current);
current = store.next(current);
count++;
}
assertEquals(msgCount, count);
assertEquals(msgCount, store.size());
store.close();
// -------------------------------------------------------------------
// Delete everything
store = createStore(storeId, new File("target/test"));
assertEquals(msgCount, store.size());
count = 0;
startTime = System.currentTimeMillis();
current = store.first();
while (current != -1)
{
int next = store.next(current);
store.delete(current);
current = next;
count++;
}
store.commitChanges();
endTime = System.currentTimeMillis();
System.out.println("Deletion time " + (endTime - startTime) + " ms");
assertEquals(msgCount, count);
assertEquals(0, store.size());
assertEquals(-1, store.first());
store.close();
// -------------------------------------------------------------------
// Reload store
store = createStore(storeId, new File("target/test"));
assertEquals(0, store.size());
assertEquals(-1, store.first());
store.close();
}