package io.trygvis.btree; import java.io.Closeable; import java.io.IOException; import java.nio.ByteBuffer; import java.util.Iterator; public class BtreeFile implements Closeable { private final HeapFile heap; private final int keySize; private final int itemsPerPage; public BtreeFile(HeapFile heap, int keySize) throws IOException { this.heap = heap; this.keySize = keySize; itemsPerPage = heap.PAGE_SIZE / keySize; // Make sure we always have a page #0. if (heap.pageCount() == 0) { HeapPage root = heap.blankPage(); heap.writePage(root); } } public void add(byte[] key, byte[] value) throws IOException { HeapPage page = heap.loadPage(0); ByteBuffer k = ByteBuffer.wrap(key); ByteBuffer current; ByteBuffer smaller; Iterator it = page.items().iterator(); int position = 0; while(it.hasNext()) { current = it.next(); int diff = k.compareTo(current); if(diff < 0) { break; } } // page.prepend(); } @Override public void close() throws IOException { heap.close(); } public static class BtreeItem { private final byte[] from; private final int page; public BtreeItem(byte[] from, int page) { this.from = from; this.page = page; } } }