BTreeIndex implements Index { @Override public void insertIntoIndex(Key key) throws JasDBStorageException { openIndex(); StatRecord btreeInsertRecord = StatisticsMonitor.createRecord("btree:insert"); resourceLockManager.sharedLock(); lockManager.startLockChain(); lockManager.acquireLock(LockIntentType.LEAVELOCK_OPTIMISTIC, rootBlock); try { LeaveBlock leaveBlock = rootBlock.findLeaveBlock(LockIntentType.LEAVELOCK_OPTIMISTIC, key); if(leaveBlock.size() == persister.getMaxKeys()) { lockManager.releaseLockChain(); lockManager.startLockChain(); lockManager.acquireLock(LockIntentType.WRITE_EXCLUSIVE, rootBlock); leaveBlock = rootBlock.findLeaveBlock(LockIntentType.WRITE_EXCLUSIVE, key); doLeaveBlockInsert(leaveBlock, key); } else { doLeaveBlockInsert(leaveBlock, key); } } finally { lockManager.releaseLockChain(); resourceLockManager.sharedUnlock(); btreeInsertRecord.stop(); } } BTreeIndex(File indexLocation, KeyInfo keyInfo); @Override void configure(Configuration configuration); @Override boolean hasUniqueConstraint(); @Override KeyInfo getKeyInfo(); @Override String getName(); @Override long count(); @Override int getPageSize(); @Override int getIndexType(); @Override int match(Set<String> fields); @Override IndexSearchResultIteratorCollection searchIndex(SearchCondition searchCondition, SearchLimit searchLimit); @Override IndexIterator getIndexIterator(); @Override void insertIntoIndex(Key key); @Override void removeFromIndex(Key key); @Override void updateKey(Key oldKey, Key newKey); @Override void openIndex(); @Override void close(); @Override void removeIndex(); @Override void flushIndex(); @Override IndexScanReport scan(ScanIntent intent, Iterator<IndexableItem> indexableItems); @Override void rebuildIndex(Iterator<IndexableItem> indexableItems); @Override MemoryAware getMemoryManager(); @Override IndexState getState(); @Override String toString();  }
@Test public void testIndexInsertDataKeyAndRead() throws Exception { KeyInfo keyInfo = new KeyInfoImpl(new SimpleIndexField("somekey", new StringKeyType()), new SimpleIndexField("DATA", new DataKeyType())); try (BTreeIndex index = new BTreeIndex(new File(tmpDir, "indexbag_somekey.idx"), keyInfo)) { index.insertIntoIndex(new StringKey("Amsterdam").addKey(keyInfo.getKeyNameMapper(), "DATA", new DataKey("My Great piece of text".getBytes(Charset.forName("utf8"))))); assertEquals("My Great piece of text", loadDataKey(index, "Amsterdam")); assertEquals("My Great piece of text", loadDataKey(index, "Amsterdam")); assertEquals("My Great piece of text", loadDataKey(index, "Amsterdam")); } }