Package co.cask.cdap.api.dataset.table

Examples of co.cask.cdap.api.dataset.table.Scanner


    if (!indexedColumns.contains(column)) {
      throw new IllegalArgumentException("Column " + Bytes.toStringBinary(column) + " is not configured for indexing");
    }
    byte[] rowKeyPrefix = Bytes.concat(column, keyDelimiter, value, keyDelimiter);
    byte[] stopRow = Bytes.stopKeyForPrefix(rowKeyPrefix);
    Scanner indexScan = index.scan(rowKeyPrefix, stopRow);
    return new IndexScanner(indexScan, rowKeyPrefix);
  }
View Full Code Here


   * @param stopRow stop row exclusive. {@code null} means scan all rows to the end of the table
   * @return {@link co.cask.cdap.api.dataset.lib.CloseableIterator} of
   * {@link co.cask.cdap.api.dataset.lib.KeyValue<byte[],byte[]>}
   */
  public CloseableIterator<KeyValue<byte[], byte[]>> scan(byte[] startRow, byte[] stopRow) {
    final Scanner scanner = table.scan(startRow, stopRow);

    return new AbstractCloseableIterator<KeyValue<byte[], byte[]>>() {
      private boolean closed = false;
      @Override
      protected KeyValue<byte[], byte[]> computeNext() {
        Preconditions.checkState(!closed);
        Row next = scanner.next();
        if (next != null) {
          return new KeyValue<byte[], byte[]>(next.getRow(), next.get(KEY_COLUMN));
        }
        close();
        return null;
      }

      @Override
      public void close() {
        scanner.close();
        endOfData();
        closed = true;
      }
    };
  }
View Full Code Here

  }

  @Override
  public void deleteRange(@Nullable byte[] start, @Nullable byte[] stop, @Nullable byte[][] columns,
                          @Nullable FuzzyRowFilter filter) {
    Scanner scanner = this.scan(start, stop, columns, filter);

    try {
      Row rowValues;
      while ((rowValues = scanner.next()) != null) {
        byte[] row = rowValues.getRow();
        for (byte[] column : rowValues.getColumns().keySet()) {
          InMemoryOrderedTableService.deleteColumns(tableName, row, column);
        }
      }
    } finally {
      scanner.close();
    }
  }
View Full Code Here

  // returns first that matches
  @Nullable
  public <T> T get(Key id, Class<T> classOfT) {
    try {
      Scanner scan = table.scan(id.getKey(), Bytes.stopKeyForPrefix(id.getKey()));
      Row row = scan.next();
      if (row == null || row.isEmpty()) {
        return null;
      }

      byte[] value = row.get(COLUMN);
View Full Code Here

    byte[] startKey = startId.getKey();
    byte[] stopKey = stopId == null ? Bytes.stopKeyForPrefix(startKey) : stopId.getKey();

    try {
      List<T> list = Lists.newArrayList();
      Scanner scan = table.scan(startKey, stopKey);
      Row next;
      while ((limit-- > 0) && (next = scan.next()) != null) {
        byte[] columnValue = next.get(COLUMN);
        if (columnValue == null) {
          continue;
        }
        T value = deserialize(columnValue, classOfT);
View Full Code Here

  public <T> void deleteAll(Key id) {
    byte[] prefix = id.getKey();
    byte[] stopKey = Bytes.stopKeyForPrefix(prefix);

    try {
      Scanner scan = table.scan(prefix, stopKey);
      Row next;
      while ((next = scan.next()) != null) {
        String columnValue = next.getString(COLUMN);
        if (columnValue == null) {
          continue;
        }
        table.delete(new Delete(next.getRow()).add(COLUMN));
View Full Code Here

      iterator.close();
    }
  }

  private static Scanner createEmptyScanner() {
    return new Scanner() {
      @Override
      public Row next() {
        return null;
      }
View Full Code Here

    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

  protected final <T> Map<String, T> scan(byte[] prefix, Class<T> classOfT) {
    byte[] stopKey = createStopKey(prefix);

    try {
      Map<String, T> map = Maps.newHashMap();
      Scanner scan = table.scan(prefix, stopKey);
      Row next;
      while ((next = scan.next()) != null) {
        byte[] columnValue = next.get(COLUMN);
        T value = GSON.fromJson(new String(columnValue, Charsets.UTF_8), classOfT);
        String key = new String(next.getRow(), prefix.length, next.getRow().length - prefix.length, Charsets.UTF_8);
        map.put(key, value);
      }
View Full Code Here

  protected final void deleteAll(byte[] prefix) {
    byte[] stopKey = createStopKey(prefix);

    try {
      Scanner scan = table.scan(prefix, stopKey);
      Row next;
      while ((next = scan.next()) != null) {
        table.delete(next.getRow());
      }
    } catch (Exception e) {
      throw Throwables.propagate(e);
    }
View Full Code Here

TOP

Related Classes of co.cask.cdap.api.dataset.table.Scanner

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.