Package org.iq80.leveldb

Examples of org.iq80.leveldb.DB


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

      deleteIterator.close();
    }
  }

  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

        return instance;
    }

    public void close(String aDirectoryPath) {
        synchronized (this.databases) {
            DB db = this.databases.get(aDirectoryPath);

            if (db != null) {
                this.databases.remove(aDirectoryPath);

                try {
                    db.close();
                } catch (IOException e) {
                    throw new IllegalStateException(
                            "Cannot completely close LevelDB database: "
                                + aDirectoryPath
                                + " because: "
View Full Code Here

            this.close(directoryPath);
        }
    }

    public DB databaseFrom(String aDirectoryPath) {
        DB db = null;

        synchronized (this.databases) {
            db = this.databases.get(aDirectoryPath);

            if (db == null) {
View Full Code Here

            Options options = new Options();

            options.createIfMissing(true);

            DB db = factory.open(new File(aDirectoryPath), options);

            return db;

        } catch (Throwable t) {
            throw new IllegalStateException(
View Full Code Here

                // better safe than sorry, for store data?
                .verifyChecksums(true)
                .cacheSize(NODE_STATE_CACHE_SIZE)
                ;
       
        DB nodeStateDB;
        try {
            nodeStateDB = factory.open(nodeStateDir, options);
        } catch (IOException e) {
            throw new IllegalStateException("Failed to open Remote Node state LevelDB (id '"+secondaryId
                    +"'): "+e.getMessage(), e);
View Full Code Here

                .createIfMissing(canCreate)
                .logger(_ldbLogger)
                .verifyChecksums(false)
                ;
       
        DB dataDB;
        try {
            options = options.cacheSize(_levelDBConfig.dataCacheSize.getNumberOfBytes());
            dataDB = factory.open(dataDir, options);
        } catch (IOException e) {
            throw new IllegalStateException("Failed to open main data LevelDB: "+e.getMessage(), e);
        }
        DB indexDB;
        try {
            options = options.cacheSize(_levelDBConfig.dataCacheSize.getNumberOfBytes());
            indexDB = factory.open(lastModDir, options);
        } catch (IOException e) {
            throw new IllegalStateException("Failed to open last-mod index LevelDB: "+e.getMessage(), e);
View Full Code Here

                // better safe than sorry, for store data?
                .verifyChecksums(true)
                .cacheSize(NODE_STATE_CACHE_SIZE)
                ;
       
        DB nodeStateDB;
        try {
            nodeStateDB = factory.open(nodeStateDir, options);
        } catch (IOException e) {
            throw new IllegalStateException("Failed to open Node state LevelDB: "+e.getMessage(), e);
        }
View Full Code Here

            throws IOException, DBException
    {
        Options options = new Options().createIfMissing(true);

        File path = getTestDirectory(getClass().getName() + "_" + NEXT_ID.incrementAndGet());
        DB db = firstFactory.open(path, options);

        WriteOptions wo = new WriteOptions().sync(false);
        ReadOptions ro = new ReadOptions().fillCache(true).verifyChecksums(true);
        db.put(bytes("Tampa"), bytes("green"));
        db.put(bytes("London"), bytes("red"));
        db.put(bytes("New York"), bytes("blue"));

        db.close();
        db = secondFactory.open(path, options);

        assertEquals(db.get(bytes("Tampa"), ro), bytes("green"));
        assertEquals(db.get(bytes("London"), ro), bytes("red"));
        assertEquals(db.get(bytes("New York"), ro), bytes("blue"));

        db.delete(bytes("New York"), wo);

        assertEquals(db.get(bytes("Tampa"), ro), bytes("green"));
        assertEquals(db.get(bytes("London"), ro), bytes("red"));
        assertNull(db.get(bytes("New York"), ro));

        db.close();
        db = firstFactory.open(path, options);

        assertEquals(db.get(bytes("Tampa"), ro), bytes("green"));
        assertEquals(db.get(bytes("London"), ro), bytes("red"));
        assertNull(db.get(bytes("New York"), ro));

        db.close();
    }
View Full Code Here

TOP

Related Classes of org.iq80.leveldb.DB

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.