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 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;
  }