Example #1
0
  @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;
  }
Example #2
0
 @Override
 public synchronized void close() throws IOException {
   try {
     _bytesDB.close();
     _index.close();
   } catch (IOException ioe) {
     _index.close();
     throw ioe;
   }
 }
Example #3
0
  @Override
  public IndexedIterator<Entry<byte[], byte[]>> iterator() {
    if (isOpen()) {
      return new IndexedDataStoreIterator(_index.iterator());
    }

    throw new StoreClosedException();
  }
Example #4
0
  @Override
  public IndexedIterator<byte[]> keyIterator() {
    if (isOpen()) {
      return _index.keyIterator();
    }

    throw new StoreClosedException();
  }
Example #5
0
  @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());
  }
Example #6
0
  @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;
  }
Example #7
0
 @Override
 public boolean isOpen() {
   return _index.isOpen();
 }
Example #8
0
 @Override
 public synchronized void sync() throws IOException {
   _bytesDB.sync();
   _index.sync();
 }
Example #9
0
 @Override
 public synchronized void persist() throws IOException {
   _bytesDB.persist();
   _index.persist();
 }
Example #10
0
 @Override
 public synchronized void clear() throws IOException {
   _bytesDB.clear();
   _index.clear();
 }
Example #11
0
 @Override
 public final int capacity() {
   return _index.capacity();
 }