I am new to git, so I will post it here as well it fails after 3
insertions:
---------------------
package org.fusesource.hawtdb.api;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Collections;
import java.util.List;
import org.fusesource.hawtbuf.Buffer;
import org.fusesource.hawtbuf.codec.IntegerCodec;
import org.fusesource.hawtbuf.codec.LongCodec;
import org.fusesource.hawtdb.api.Paged.SliceType;
import org.junit.Test;
public class MixedUseTest {
public static final Buffer MAGIC = new Buffer(new byte[] {'c'});
private TxPageFile m_pageFile;
private IndexFactory<Long, Integer> m_indexFactory;
private short m_pageSize;
private short m_dataSize;
private IndexDataAccessor m_dataAccessor = new IndexDataAccessor();
private static IndexFactory<Long, Integer> createIndexFactory() {
HashIndexFactory<Long, Integer> indexFactory = new
HashIndexFactory<Long, Integer>();
indexFactory.setDeferredEncoding(false);
indexFactory.setKeyCodec(LongCodec.INSTANCE);
indexFactory.setValueCodec(IntegerCodec.INSTANCE);
return indexFactory;
}
private static TxPageFile createFileFactory(File path, short
pageSize) throws IOException {
TxPageFileFactory factory = new TxPageFileFactory();
factory.setFile(path);
factory.setPageSize(pageSize);
factory.setSync(false);
factory.open();
return factory.getTxPageFile();
}
public void setUp(short pageSize) throws IOException {
File path = new File("test-mixed");
path.delete();
m_pageSize = pageSize;
m_dataSize = (short)(pageSize - (MAGIC.length + Long.SIZE/8));
m_pageFile = createFileFactory(path, pageSize);
m_indexFactory = createIndexFactory();
// create the index
Transaction tx = m_pageFile.tx();
try {
Index<Long, Integer> index = m_indexFactory.create(tx);
// seed the index with some metadata about the test
index.put(-2L, (int)m_pageSize);
index.put(-3L, (int)m_dataSize);
index.put(-4L, MAGIC.length);
}
finally {
tx.commit();
}
m_pageFile.flush();
}
private class CustomObject {
private long id;
private byte[] data;
public CustomObject(long id) {
this.id = id;
this.data = new byte[m_dataSize];
}
}
private class IndexDataAccessor implements
PagedAccessor<CustomObject> {
public IndexDataAccessor() {
}
public CustomObject load(Paged p, int page) {
ByteBuffer bb = p.slice(SliceType.READ, page, 1);
try {
// check the magic
Buffer magicBuffer = new Buffer(MAGIC.length);
bb.get(magicBuffer.data);
if (!MAGIC.equals(magicBuffer))
throw new IllegalArgumentException("unknown page type");
CustomObject co = new CustomObject(bb.getLong());
bb.get(co.data);
return co;
}
finally {
p.unslice(bb);
}
}
@Override
public List<Integer> store(Paged p, int page, CustomObject value) {
ByteBuffer bb = p.slice(SliceType.WRITE, page, 1);
try {
bb.put(MAGIC.data);
bb.putLong(
value.id);
bb.put(value.data);
}
finally {
p.unslice(bb);
}
return Collections.EMPTY_LIST;
}
@Override
public List<Integer> pagesLinked(Paged paged, int page) {
return Collections.EMPTY_LIST;
}
}
private void store(CustomObject c) {
Transaction tx = m_pageFile.tx();
try {
Index<Long, Integer> idx = m_indexFactory.open(tx);
Integer pageNumber = idx.get(
c.id);
CustomObject obj;
// see if the index contains this object
if (pageNumber == null) {
pageNumber = tx.alloc();
obj = c;
}
else {
// read the previous object
obj = tx.get(m_dataAccessor, pageNumber);
}
// data update
obj.data = c.data;
// add this person to the index
idx.putIfAbsent(
c.id, pageNumber);
// write the data to the page
m_dataAccessor.store(tx, pageNumber, obj);
tx.commit();
}
finally {
tx.close();
}
}
@Test
public void testSingleIndex() throws IOException {
setUp((short)(1024*29));
System.out.println("building db");
for (int i = 0; i < 10000; i++) {
System.out.println("creating: " + i);
CustomObject c = new CustomObject(i);
store(c);
if (i % 10 == 0)
m_pageFile.flush();
}
}
}