Пример #1
0
 @Override
 public synchronized void open() throws IOException {
   try {
     _bytesDB.open();
     _index.open();
   } catch (IOException ioe) {
     _index.close();
     _bytesDB.close();
     throw ioe;
   }
 }
Пример #2
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;
  }
Пример #3
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());
  }
Пример #4
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;
  }
Пример #5
0
 @Override
 public synchronized void sync() throws IOException {
   _bytesDB.sync();
   _index.sync();
 }
Пример #6
0
 @Override
 public synchronized void persist() throws IOException {
   _bytesDB.persist();
   _index.persist();
 }
Пример #7
0
 @Override
 public synchronized void clear() throws IOException {
   _bytesDB.clear();
   _index.clear();
 }