Search in sources :

Example 1 with RawMessageTableEntry

use of co.cask.cdap.messaging.store.RawMessageTableEntry in project cdap by caskdata.

the class LevelDBMessageTable method persist.

@Override
protected void persist(Iterator<RawMessageTableEntry> entries) throws IOException {
    try (WriteBatch writeBatch = levelDB.createWriteBatch()) {
        while (entries.hasNext()) {
            RawMessageTableEntry entry = entries.next();
            byte[] rowKey = entry.getKey();
            // LevelDB doesn't make copies, and since we reuse RawMessageTableEntry object, we need to create copies.
            writeBatch.put(Arrays.copyOf(rowKey, rowKey.length), encodeValue(entry.getTxPtr(), entry.getPayload()));
        }
        levelDB.write(writeBatch, WRITE_OPTIONS);
    } catch (DBException ex) {
        throw new IOException(ex);
    }
}
Also used : DBException(org.iq80.leveldb.DBException) RawMessageTableEntry(co.cask.cdap.messaging.store.RawMessageTableEntry) IOException(java.io.IOException) WriteBatch(org.iq80.leveldb.WriteBatch)

Example 2 with RawMessageTableEntry

use of co.cask.cdap.messaging.store.RawMessageTableEntry in project cdap by caskdata.

the class LevelDBMessageTable method read.

@Override
protected CloseableIterator<RawMessageTableEntry> read(byte[] startRow, byte[] stopRow) throws IOException {
    final DBScanIterator iterator = new DBScanIterator(levelDB, startRow, stopRow);
    final RawMessageTableEntry tableEntry = new RawMessageTableEntry();
    return new AbstractCloseableIterator<RawMessageTableEntry>() {

        private boolean closed = false;

        @Override
        protected RawMessageTableEntry computeNext() {
            if (closed || (!iterator.hasNext())) {
                return endOfData();
            }
            Map.Entry<byte[], byte[]> row = iterator.next();
            Map<String, byte[]> columns = decodeValue(row.getValue());
            return tableEntry.set(row.getKey(), columns.get(TX_COL), columns.get(PAYLOAD_COL));
        }

        @Override
        public void close() {
            try {
                iterator.close();
            } finally {
                endOfData();
                closed = true;
            }
        }
    };
}
Also used : AbstractCloseableIterator(co.cask.cdap.api.dataset.lib.AbstractCloseableIterator) RawMessageTableEntry(co.cask.cdap.messaging.store.RawMessageTableEntry) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with RawMessageTableEntry

use of co.cask.cdap.messaging.store.RawMessageTableEntry in project cdap by caskdata.

the class HBaseMessageTable method persist.

@Override
protected void persist(Iterator<RawMessageTableEntry> entries) throws IOException {
    List<Put> batchPuts = new ArrayList<>();
    while (entries.hasNext()) {
        RawMessageTableEntry entry = entries.next();
        PutBuilder putBuilder = tableUtil.buildPut(rowKeyDistributor.getDistributedKey(entry.getKey()));
        if (entry.getTxPtr() != null) {
            putBuilder.add(columnFamily, TX_COL, entry.getTxPtr());
        }
        if (entry.getPayload() != null) {
            putBuilder.add(columnFamily, PAYLOAD_COL, entry.getPayload());
        }
        batchPuts.add(putBuilder.build());
    }
    try {
        if (!batchPuts.isEmpty()) {
            hTable.put(batchPuts);
            if (!hTable.isAutoFlush()) {
                hTable.flushCommits();
            }
        }
    } catch (IOException e) {
        throw exceptionHandler.handle(e);
    }
}
Also used : PutBuilder(co.cask.cdap.data2.util.hbase.PutBuilder) ArrayList(java.util.ArrayList) RawMessageTableEntry(co.cask.cdap.messaging.store.RawMessageTableEntry) IOException(java.io.IOException) Put(org.apache.hadoop.hbase.client.Put)

Example 4 with RawMessageTableEntry

use of co.cask.cdap.messaging.store.RawMessageTableEntry in project cdap by caskdata.

the class HBaseMessageTable method read.

@Override
protected CloseableIterator<RawMessageTableEntry> read(byte[] startRow, byte[] stopRow) throws IOException {
    Scan scan = tableUtil.buildScan().setStartRow(startRow).setStopRow(stopRow).setCaching(scanCacheRows).build();
    try {
        final ResultScanner scanner = DistributedScanner.create(hTable, scan, rowKeyDistributor, scanExecutor);
        final RawMessageTableEntry tableEntry = new RawMessageTableEntry();
        return new AbstractCloseableIterator<RawMessageTableEntry>() {

            private boolean closed = false;

            @Override
            protected RawMessageTableEntry computeNext() {
                if (closed) {
                    return endOfData();
                }
                Result result;
                try {
                    result = scanner.next();
                } catch (IOException e) {
                    throw exceptionHandler.handleAndWrap(e);
                }
                if (result == null) {
                    return endOfData();
                }
                return tableEntry.set(rowKeyDistributor.getOriginalKey(result.getRow()), result.getValue(columnFamily, TX_COL), result.getValue(columnFamily, PAYLOAD_COL));
            }

            @Override
            public void close() {
                try {
                    scanner.close();
                } finally {
                    endOfData();
                    closed = true;
                }
            }
        };
    } catch (IOException e) {
        throw exceptionHandler.handle(e);
    }
}
Also used : ResultScanner(org.apache.hadoop.hbase.client.ResultScanner) AbstractCloseableIterator(co.cask.cdap.api.dataset.lib.AbstractCloseableIterator) RawMessageTableEntry(co.cask.cdap.messaging.store.RawMessageTableEntry) Scan(org.apache.hadoop.hbase.client.Scan) IOException(java.io.IOException) Result(org.apache.hadoop.hbase.client.Result)

Aggregations

RawMessageTableEntry (co.cask.cdap.messaging.store.RawMessageTableEntry)4 IOException (java.io.IOException)3 AbstractCloseableIterator (co.cask.cdap.api.dataset.lib.AbstractCloseableIterator)2 PutBuilder (co.cask.cdap.data2.util.hbase.PutBuilder)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Put (org.apache.hadoop.hbase.client.Put)1 Result (org.apache.hadoop.hbase.client.Result)1 ResultScanner (org.apache.hadoop.hbase.client.ResultScanner)1 Scan (org.apache.hadoop.hbase.client.Scan)1 DBException (org.iq80.leveldb.DBException)1 WriteBatch (org.iq80.leveldb.WriteBatch)1