Пример #1
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;
  }