Package org.iq80.leveldb

Examples of org.iq80.leveldb.DBIterator


            logger.error("Failed to find the db file on the close: {} ", name);
        }
    }

  public List<ByteArrayWrapper> dumpKeys() {
    DBIterator iterator = getDb().iterator();
    ArrayList<ByteArrayWrapper> keys = new ArrayList<>();

    while (iterator.hasNext()) {
      ByteArrayWrapper key = new ByteArrayWrapper(iterator.next().getKey());
      keys.add(key);
    }
    Collections.sort((List<ByteArrayWrapper>) keys);
    return keys;
  }
View Full Code Here


      }
      columns = Arrays.copyOf(columns, columns.length);
      Arrays.sort(columns, Bytes.BYTES_COMPARATOR);
    }

    DBIterator iterator = getDB().iterator();
    seekToStart(iterator, startRow);
    byte[] endKey = stopRow == null ? null : createEndKey(stopRow);
    return new LevelDBScanner(iterator, endKey, filter, columns, tx);
  }
View Full Code Here

      Arrays.sort(columns, Bytes.BYTES_COMPARATOR);
    }

    byte[] startKey = createStartKey(row, columns == null ? startCol : columns[0]);
    byte[] endKey = createEndKey(row, columns == null ? stopCol : upperBound(columns[columns.length - 1]));
    DBIterator iterator = getDB().iterator();
    try {
      iterator.seek(startKey);
      return getRow(iterator, endKey, tx, false, columns, limit).getSecond();
    } finally {
      iterator.close();
    }
  }
View Full Code Here

  public void deleteRows(byte[] prefix) throws IOException {
    Preconditions.checkNotNull(prefix, "prefix must not be null");
    DB db = getDB();
    WriteBatch batch = db.createWriteBatch();
    DBIterator iterator = db.iterator();
    try {
      iterator.seek(createStartKey(prefix));
      while (iterator.hasNext()) {
        Map.Entry<byte[], byte[]> entry = iterator.next();
        if (!Bytes.startsWith(KeyValue.fromKey(entry.getKey()).getRow(), prefix)) {
          // iterator is past prefix
          break;
        }
        batch.delete(entry.getKey());
      }
      db.write(batch);
    } finally {
      iterator.close();
    }
  }
View Full Code Here

    // find first row to delete and first entry in the DB to examine
    Iterator<byte[]> rows = toDelete.iterator();
    byte[] currentRow = rows.next();
    byte[] startKey = createStartKey(currentRow);
    DB db = getDB();
    DBIterator iterator = db.iterator();
    WriteBatch batch = db.createWriteBatch();
    try {
      iterator.seek(startKey);
      if (!iterator.hasNext()) {
        return; // nothing in the db to delete
      }
      Map.Entry<byte[], byte[]> entry = iterator.next();

      // iterate over the database and the rows to delete, collecting (raw) keys to delete
      while (entry != null && currentRow != null) {
        KeyValue kv = KeyValue.fromKey(entry.getKey());
        int comp = Bytes.compareTo(kv.getRow(), currentRow);
        if (comp == 0) {
          // same row -> delete
          batch.delete(entry.getKey());
          entry = iterator.hasNext() ? iterator.next() : null;
        } else if (comp > 0) {
          // read past current row -> move to next row
          currentRow = rows.hasNext() ? rows.next() : null;
        } else if (comp < 0) {
          // iterator must seek to current row
          iterator.seek(createStartKey(currentRow));
          entry = iterator.hasNext() ? iterator.next() : null;
        }
      }
    } finally {
      iterator.close();
    }
    // delete all the entries that were found
    db.write(batch, getWriteOptions());
  }
View Full Code Here

      columns = Arrays.copyOf(columns, columns.length);
      Arrays.sort(columns, Bytes.BYTES_COMPARATOR);
    }

    DB db = getDB();
    DBIterator iterator = db.iterator();
    seekToStart(iterator, startRow);
    byte[] endKey = stopRow == null ? null : createEndKey(stopRow);
    Scanner scanner = new LevelDBScanner(iterator, endKey, filter, columns, null);

    DBIterator deleteIterator = db.iterator();
    seekToStart(deleteIterator, startRow);
    final int deletesPerRound = 1024; // todo make configurable
    try {
      Row rowValues;
      WriteBatch batch = db.createWriteBatch();
      int deletesInBatch = 0;

      // go through all matching cells and delete them in batches.
      while ((rowValues = scanner.next()) != null) {
        byte[] row = rowValues.getRow();
        for (byte[] column : rowValues.getColumns().keySet()) {
          addToDeleteBatch(batch, deleteIterator, row, column);
          deletesInBatch++;

          // perform the deletes when we have built up a batch.
          if (deletesInBatch >= deletesPerRound) {
            // delete all the entries that were found
            db.write(batch, getWriteOptions());
            batch = db.createWriteBatch();
            deletesInBatch = 0;
          }
        }
      }

      // perform any outstanding deletes
      if (deletesInBatch > 0) {
        db.write(batch, getWriteOptions());
      }
    } finally {
      scanner.close();
      deleteIterator.close();
    }
  }
View Full Code Here

  }

  private void deleteColumn(byte[] row, byte[] column) throws IOException {
    DB db = getDB();
    WriteBatch batch = db.createWriteBatch();
    DBIterator iterator = db.iterator();
    try {
      addToDeleteBatch(batch, iterator, row, column);
      db.write(batch);
    } finally {
      iterator.close();
    }
  }
View Full Code Here

  }

  @Override
  protected void clearLockSafe() throws CacheLoaderException {
    long count = 0;
    DBIterator it = db.iterator(new ReadOptions().fillCache(false));
    boolean destroyDatabase = false;

    if (config.getClearThreshold() <= 0) {
      try {
        for (it.seekToFirst(); it.hasNext();) {
          Map.Entry<byte[], byte[]> entry = it.next();
          db.delete(entry.getKey());
          count++;

          if (count > config.clearThreshold) {
            destroyDatabase = true;
            break;
          }
        }
      } finally {
        try {
               it.close();
            } catch (IOException e) {
               log.warnUnableToCloseDbIterator(e);
            }
      }
    } else {
View Full Code Here

  @Override
  protected Set<InternalCacheEntry> loadAllLockSafe()
      throws CacheLoaderException {
    Set<InternalCacheEntry> entries = new HashSet<InternalCacheEntry>();

    DBIterator it = db.iterator(new ReadOptions().fillCache(false));
    try {
      for (it.seekToFirst(); it.hasNext();) {
        Map.Entry<byte[], byte[]> entry = it.next();
        entries.add(unmarshall(entry));
      }
    } catch (Exception e) {
      throw new CacheLoaderException(e);
    } finally {
      try {
            it.close();
         } catch (IOException e) {
            log.warnUnableToCloseDbIterator(e);
         }
    }
View Full Code Here

    if (maxEntries <= 0)
      return Collections.emptySet();

    Set<InternalCacheEntry> entries = new HashSet<InternalCacheEntry>();

    DBIterator it = db.iterator(new ReadOptions().fillCache(false));
    try {
      it.seekToFirst();
      for (int i = 0; it.hasNext() && i < maxEntries; i++) {
        Map.Entry<byte[], byte[]> entry = it.next();
        entries.add(unmarshall(entry));
      }
    } catch (Exception e) {
      throw new CacheLoaderException(e);
    } finally {
      try {
            it.close();
         } catch (IOException e) {
            log.warnUnableToCloseDbIterator(e);
         }
    }
View Full Code Here

TOP

Related Classes of org.iq80.leveldb.DBIterator

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.