Search in sources :

Example 6 with ByteArrayByteIterator

use of site.ycsb.ByteArrayByteIterator in project YCSB by brianfrankcooper.

the class ScyllaCQLClient method read.

/**
 * Read a record from the database. Each field/value pair from the result will
 * be stored in a HashMap.
 *
 * @param table
 *          The name of the table
 * @param key
 *          The record key of the record to read.
 * @param fields
 *          The list of fields to read, or null for all of them
 * @param result
 *          A HashMap of field/value pairs for the result
 * @return Zero on success, a non-zero error code on error
 */
@Override
public Status read(String table, String key, Set<String> fields, Map<String, ByteIterator> result) {
    try {
        PreparedStatement stmt = (fields == null) ? READ_ALL_STMT.get() : READ_STMTS.get(fields);
        // Prepare statement on demand
        if (stmt == null) {
            Select.Builder selectBuilder;
            if (fields == null) {
                selectBuilder = QueryBuilder.select().all();
            } else {
                selectBuilder = QueryBuilder.select();
                for (String col : fields) {
                    ((Select.Selection) selectBuilder).column(col);
                }
            }
            stmt = session.prepare(selectBuilder.from(table).where(QueryBuilder.eq(YCSB_KEY, QueryBuilder.bindMarker())).limit(1));
            stmt.setConsistencyLevel(readConsistencyLevel);
            if (trace) {
                stmt.enableTracing();
            }
            PreparedStatement prevStmt = (fields == null) ? READ_ALL_STMT.getAndSet(stmt) : READ_STMTS.putIfAbsent(new HashSet<>(fields), stmt);
            if (prevStmt != null) {
                stmt = prevStmt;
            }
        }
        LOGGER.debug(stmt.getQueryString());
        LOGGER.debug("key = {}", key);
        ResultSet rs = session.execute(stmt.bind(key));
        if (rs.isExhausted()) {
            return Status.NOT_FOUND;
        }
        // Should be only 1 row
        Row row = rs.one();
        ColumnDefinitions cd = row.getColumnDefinitions();
        for (ColumnDefinitions.Definition def : cd) {
            ByteBuffer val = row.getBytesUnsafe(def.getName());
            if (val != null) {
                result.put(def.getName(), new ByteArrayByteIterator(val.array()));
            } else {
                result.put(def.getName(), null);
            }
        }
        return Status.OK;
    } catch (Exception e) {
        LOGGER.error(MessageFormatter.format("Error reading key: {}", key).getMessage(), e);
        return Status.ERROR;
    }
}
Also used : ByteBuffer(java.nio.ByteBuffer) DBException(site.ycsb.DBException) ByteArrayByteIterator(site.ycsb.ByteArrayByteIterator) HashSet(java.util.HashSet)

Example 7 with ByteArrayByteIterator

use of site.ycsb.ByteArrayByteIterator in project YCSB by brianfrankcooper.

the class ScyllaCQLClient method scan.

/**
 * Perform a range scan for a set of records in the database. Each field/value
 * pair from the result will be stored in a HashMap.
 *
 * scylla CQL uses "token" method for range scan which doesn't always yield
 * intuitive results.
 *
 * @param table
 *          The name of the table
 * @param startkey
 *          The record key of the first record to read.
 * @param recordcount
 *          The number of records to read
 * @param fields
 *          The list of fields to read, or null for all of them
 * @param result
 *          A Vector of HashMaps, where each HashMap is a set field/value
 *          pairs for one record
 * @return Zero on success, a non-zero error code on error
 */
@Override
public Status scan(String table, String startkey, int recordcount, Set<String> fields, Vector<HashMap<String, ByteIterator>> result) {
    try {
        PreparedStatement stmt = (fields == null) ? SCAN_ALL_STMT.get() : SCAN_STMTS.get(fields);
        // Prepare statement on demand
        if (stmt == null) {
            Select.Builder selectBuilder;
            if (fields == null) {
                selectBuilder = QueryBuilder.select().all();
            } else {
                selectBuilder = QueryBuilder.select();
                for (String col : fields) {
                    ((Select.Selection) selectBuilder).column(col);
                }
            }
            Select selectStmt = selectBuilder.from(table);
            // The statement builder is not setup right for tokens.
            // So, we need to build it manually.
            String initialStmt = selectStmt.toString();
            String scanStmt = initialStmt.substring(0, initialStmt.length() - 1) + " WHERE " + QueryBuilder.token(YCSB_KEY) + " >= token(" + QueryBuilder.bindMarker() + ")" + " LIMIT " + QueryBuilder.bindMarker();
            stmt = session.prepare(scanStmt);
            stmt.setConsistencyLevel(readConsistencyLevel);
            if (trace) {
                stmt.enableTracing();
            }
            PreparedStatement prevStmt = (fields == null) ? SCAN_ALL_STMT.getAndSet(stmt) : SCAN_STMTS.putIfAbsent(new HashSet<>(fields), stmt);
            if (prevStmt != null) {
                stmt = prevStmt;
            }
        }
        LOGGER.debug(stmt.getQueryString());
        LOGGER.debug("startKey = {}, recordcount = {}", startkey, recordcount);
        ResultSet rs = session.execute(stmt.bind(startkey, recordcount));
        HashMap<String, ByteIterator> tuple;
        while (!rs.isExhausted()) {
            Row row = rs.one();
            tuple = new HashMap<>();
            ColumnDefinitions cd = row.getColumnDefinitions();
            for (ColumnDefinitions.Definition def : cd) {
                ByteBuffer val = row.getBytesUnsafe(def.getName());
                if (val != null) {
                    tuple.put(def.getName(), new ByteArrayByteIterator(val.array()));
                } else {
                    tuple.put(def.getName(), null);
                }
            }
            result.add(tuple);
        }
        return Status.OK;
    } catch (Exception e) {
        LOGGER.error(MessageFormatter.format("Error scanning with startkey: {}", startkey).getMessage(), e);
        return Status.ERROR;
    }
}
Also used : ByteBuffer(java.nio.ByteBuffer) DBException(site.ycsb.DBException) ByteArrayByteIterator(site.ycsb.ByteArrayByteIterator) ByteIterator(site.ycsb.ByteIterator) ByteArrayByteIterator(site.ycsb.ByteArrayByteIterator) HashSet(java.util.HashSet)

Example 8 with ByteArrayByteIterator

use of site.ycsb.ByteArrayByteIterator in project YCSB by brianfrankcooper.

the class RiakUtils method deserializeTable.

/**
 * Deserializes an input byte array, transforming it into a list of (String, ByteIterator) pairs (i.e. a Map).
 *
 * @param aValue    A byte array containing the table to deserialize.
 * @param theResult A Map containing the deserialized table.
 */
private static void deserializeTable(final byte[] aValue, final Map<String, ByteIterator> theResult) {
    final ByteArrayInputStream anInputStream = new ByteArrayInputStream(aValue);
    byte[] aSizeBuffer = new byte[4];
    try {
        while (anInputStream.available() > 0) {
            anInputStream.read(aSizeBuffer);
            final int aColumnNameLength = fromBytes(aSizeBuffer);
            final byte[] aColumnNameBuffer = new byte[aColumnNameLength];
            anInputStream.read(aColumnNameBuffer);
            anInputStream.read(aSizeBuffer);
            final int aColumnValueLength = fromBytes(aSizeBuffer);
            final byte[] aColumnValue = new byte[aColumnValueLength];
            anInputStream.read(aColumnValue);
            theResult.put(new String(aColumnNameBuffer), new ByteArrayByteIterator(aColumnValue));
        }
    } catch (Exception e) {
        throw new IllegalStateException(e);
    } finally {
        close(anInputStream);
    }
}
Also used : ByteArrayByteIterator(site.ycsb.ByteArrayByteIterator)

Example 9 with ByteArrayByteIterator

use of site.ycsb.ByteArrayByteIterator in project YCSB by brianfrankcooper.

the class VoltClient4 method unpackRowDataHashMap.

private HashMap<String, ByteIterator> unpackRowDataHashMap(byte[] rowData, ByteBuffer buf, int nFields, Set<String> fields, HashMap<String, ByteIterator> result) {
    for (int i = 0; i < nFields; i++) {
        int len = buf.getInt();
        int off = buf.position();
        String key = new String(rowData, off, len, UTF8);
        buf.position(off + len);
        len = buf.getInt();
        off = buf.position();
        if (fields == null || fields.contains(key)) {
            result.put(key, new ByteArrayByteIterator(rowData, off, len));
        }
        buf.position(off + len);
    }
    return result;
}
Also used : ByteArrayByteIterator(site.ycsb.ByteArrayByteIterator)

Example 10 with ByteArrayByteIterator

use of site.ycsb.ByteArrayByteIterator in project YCSB by brianfrankcooper.

the class HBaseClient2 method scan.

/**
 * Perform a range scan for a set of records in the database. Each field/value
 * pair from the result will be stored in a HashMap.
 *
 * @param table
 *          The name of the table
 * @param startkey
 *          The record key of the first record to read.
 * @param recordcount
 *          The number of records to read
 * @param fields
 *          The list of fields to read, or null for all of them
 * @param result
 *          A Vector of HashMaps, where each HashMap is a set field/value
 *          pairs for one record
 * @return Zero on success, a non-zero error code on error
 */
@Override
public Status scan(String table, String startkey, int recordcount, Set<String> fields, Vector<HashMap<String, ByteIterator>> result) {
    // if this is a "new" table, init HTable object. Else, use existing one
    if (!tableName.equals(table)) {
        currentTable = null;
        try {
            getHTable(table);
            tableName = table;
        } catch (IOException e) {
            System.err.println("Error accessing HBase table: " + e);
            return Status.ERROR;
        }
    }
    Scan s = new Scan(Bytes.toBytes(startkey));
    // HBase has no record limit. Here, assume recordcount is small enough to
    // bring back in one call.
    // We get back recordcount records
    FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
    s.setCaching(recordcount);
    if (this.usePageFilter) {
        filterList.addFilter(new PageFilter(recordcount));
    }
    // add specified fields or else all fields
    if (fields == null) {
        s.addFamily(columnFamilyBytes);
    } else {
        for (String field : fields) {
            s.addColumn(columnFamilyBytes, Bytes.toBytes(field));
        }
    }
    // define value filter if needed
    if (useScanValueFiltering) {
        filterList.addFilter(new ValueFilter(scanFilterOperator, scanFilterValue));
    }
    s.setFilter(filterList);
    // get results
    ResultScanner scanner = null;
    try {
        scanner = currentTable.getScanner(s);
        int numResults = 0;
        for (Result rr = scanner.next(); rr != null; rr = scanner.next()) {
            // get row key
            String key = Bytes.toString(rr.getRow());
            if (debug) {
                System.out.println("Got scan result for key: " + key);
            }
            HashMap<String, ByteIterator> rowResult = new HashMap<String, ByteIterator>();
            while (rr.advance()) {
                final Cell cell = rr.current();
                rowResult.put(Bytes.toString(CellUtil.cloneQualifier(cell)), new ByteArrayByteIterator(CellUtil.cloneValue(cell)));
            }
            // add rowResult to result vector
            result.add(rowResult);
            numResults++;
            // break is required.
            if (numResults >= recordcount) {
                // if hit recordcount, bail out
                break;
            }
        }
    // done with row
    } catch (IOException e) {
        if (debug) {
            System.out.println("Error in getting/parsing scan result: " + e);
        }
        return Status.ERROR;
    } finally {
        if (scanner != null) {
            scanner.close();
        }
    }
    return Status.OK;
}
Also used : ResultScanner(org.apache.hadoop.hbase.client.ResultScanner) HashMap(java.util.HashMap) FilterList(org.apache.hadoop.hbase.filter.FilterList) IOException(java.io.IOException) Result(org.apache.hadoop.hbase.client.Result) ByteArrayByteIterator(site.ycsb.ByteArrayByteIterator) ByteIterator(site.ycsb.ByteIterator) ByteArrayByteIterator(site.ycsb.ByteArrayByteIterator) Scan(org.apache.hadoop.hbase.client.Scan) PageFilter(org.apache.hadoop.hbase.filter.PageFilter) ValueFilter(org.apache.hadoop.hbase.filter.ValueFilter) Cell(org.apache.hadoop.hbase.Cell)

Aggregations

ByteArrayByteIterator (site.ycsb.ByteArrayByteIterator)29 ByteIterator (site.ycsb.ByteIterator)14 DBException (site.ycsb.DBException)12 HashMap (java.util.HashMap)11 IOException (java.io.IOException)8 Status (site.ycsb.Status)5 ByteBuffer (java.nio.ByteBuffer)4 HashSet (java.util.HashSet)4 Test (org.junit.Test)4 DB (site.ycsb.DB)4 EntityProperty (com.microsoft.azure.storage.table.EntityProperty)3 Cell (org.apache.hadoop.hbase.Cell)3 Result (org.apache.hadoop.hbase.client.Result)3 ColumnDefinitions (com.datastax.driver.core.ColumnDefinitions)2 PreparedStatement (com.datastax.driver.core.PreparedStatement)2 ResultSet (com.datastax.driver.core.ResultSet)2 Row (com.datastax.driver.core.Row)2 Select (com.datastax.driver.core.querybuilder.Select)2 Column (com.google.bigtable.v2.Column)2 Family (com.google.bigtable.v2.Family)2