@Override public synchronized boolean delete(byte[] key) throws Exception { if (key == null) return false; // Lookup index meta byte[] metaBytes = _index.lookup(key); if (metaBytes == null) return false; IndexMeta meta = IndexMeta.parse(metaBytes); if (meta == null) return false; // Delete from bytes DB _bytesDB.set(meta.getDataAddr(), null, System.currentTimeMillis()); // Update index _index.update(key, null); _updateCnt++; if (_updateCnt >= _batchSize) { _updateCnt = 0; persist(); } return true; }
@Override public synchronized void close() throws IOException { try { _bytesDB.close(); _index.close(); } catch (IOException ioe) { _index.close(); throw ioe; } }
@Override public IndexedIterator<Entry<byte[], byte[]>> iterator() { if (isOpen()) { return new IndexedDataStoreIterator(_index.iterator()); } throw new StoreClosedException(); }
@Override public IndexedIterator<byte[]> keyIterator() { if (isOpen()) { return _index.keyIterator(); } throw new StoreClosedException(); }
@Override public byte[] get(byte[] key) { if (key == null) return null; byte[] metaBytes = _index.lookup(key); if (metaBytes == null) return null; IndexMeta meta = IndexMeta.parse(metaBytes); if (meta == null) return null; return _bytesDB.get(meta.getDataAddr()); }
@Override public synchronized boolean put(byte[] key, byte[] value) throws Exception { if (value == null) return delete(key); if (key == null) return false; // Lookup index meta IndexMeta meta = null; byte[] metaBytes = _index.lookup(key); if (metaBytes != null) { meta = IndexMeta.parse(metaBytes); } // Update index if needed if (meta == null) { // Add to bytes DB int index = _bytesDB.add(value, System.currentTimeMillis()); metaBytes = IndexMeta.build(index); // Update hashIndex _index.update(key, metaBytes); } else { // Update bytes DB int index = meta.getDataAddr(); _bytesDB.set(index, value, System.currentTimeMillis()); // No need to update hashIndex } _updateCnt++; if (_updateCnt >= _batchSize) { _updateCnt = 0; persist(); } return true; }
@Override public boolean isOpen() { return _index.isOpen(); }
@Override public synchronized void sync() throws IOException { _bytesDB.sync(); _index.sync(); }
@Override public synchronized void persist() throws IOException { _bytesDB.persist(); _index.persist(); }
@Override public synchronized void clear() throws IOException { _bytesDB.clear(); _index.clear(); }
@Override public final int capacity() { return _index.capacity(); }