Search in sources :

Example 6 with ByteArrayByteIterator

use of com.yahoo.ycsb.ByteArrayByteIterator in project YCSB by brianfrankcooper.

the class AsyncHBaseClient method read.

@Override
public Status read(String table, String key, Set<String> fields, HashMap<String, ByteIterator> result) {
    setTable(table);
    final GetRequest get = new GetRequest(lastTableBytes, key.getBytes(), columnFamilyBytes);
    if (fields != null) {
        get.qualifiers(getQualifierList(fields));
    }
    try {
        if (debug) {
            System.out.println("Doing read from HBase columnfamily " + Bytes.pretty(columnFamilyBytes));
            System.out.println("Doing read for key: " + key);
        }
        final ArrayList<KeyValue> row = client.get(get).join(joinTimeout);
        if (row == null || row.isEmpty()) {
            return Status.NOT_FOUND;
        }
        // got something so populate the results
        for (final KeyValue column : row) {
            result.put(new String(column.qualifier()), // be GC'd.
            new ByteArrayByteIterator(column.value()));
            if (debug) {
                System.out.println("Result for field: " + Bytes.pretty(column.qualifier()) + " is: " + Bytes.pretty(column.value()));
            }
        }
        return Status.OK;
    } catch (InterruptedException e) {
        System.err.println("Thread interrupted");
        Thread.currentThread().interrupt();
    } catch (Exception e) {
        System.err.println("Failure reading from row with key " + key + ": " + e.getMessage());
        return Status.ERROR;
    }
    return Status.ERROR;
}
Also used : ByteArrayByteIterator(com.yahoo.ycsb.ByteArrayByteIterator) KeyValue(org.hbase.async.KeyValue) GetRequest(org.hbase.async.GetRequest) DBException(com.yahoo.ycsb.DBException) IOException(java.io.IOException)

Example 7 with ByteArrayByteIterator

use of com.yahoo.ycsb.ByteArrayByteIterator in project YCSB by brianfrankcooper.

the class AzureClient method scan.

@Override
public Status scan(String table, String startkey, int recordcount, Set<String> fields, Vector<HashMap<String, ByteIterator>> result) {
    try {
        String whereStr = String.format("(PartitionKey eq '%s') and (RowKey ge '%s')", partitionKey, startkey);
        TableQuery<DynamicTableEntity> scanQuery = new TableQuery<DynamicTableEntity>(DynamicTableEntity.class).where(whereStr).take(recordcount);
        int cnt = 0;
        for (DynamicTableEntity entity : cloudTable.execute(scanQuery)) {
            HashMap<String, EntityProperty> properties = entity.getProperties();
            HashMap<String, ByteIterator> cur = new HashMap<String, ByteIterator>();
            for (Entry<String, EntityProperty> entry : properties.entrySet()) {
                String fieldName = entry.getKey();
                ByteIterator fieldVal = new ByteArrayByteIterator(entry.getValue().getValueAsByteArray());
                if (fields == null || fields.contains(fieldName)) {
                    cur.put(fieldName, fieldVal);
                }
            }
            result.add(cur);
            if (++cnt == recordcount) {
                break;
            }
        }
        return Status.OK;
    } catch (Exception e) {
        return Status.ERROR;
    }
}
Also used : HashMap(java.util.HashMap) DBException(com.yahoo.ycsb.DBException) DynamicTableEntity(com.microsoft.azure.storage.table.DynamicTableEntity) EntityProperty(com.microsoft.azure.storage.table.EntityProperty) ByteArrayByteIterator(com.yahoo.ycsb.ByteArrayByteIterator) ByteArrayByteIterator(com.yahoo.ycsb.ByteArrayByteIterator) ByteIterator(com.yahoo.ycsb.ByteIterator) TableQuery(com.microsoft.azure.storage.table.TableQuery)

Example 8 with ByteArrayByteIterator

use of com.yahoo.ycsb.ByteArrayByteIterator in project YCSB by brianfrankcooper.

the class CassandraCQLClient 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.
   *
   * Cassandra 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 {
        Statement stmt;
        Select.Builder selectBuilder;
        if (fields == null) {
            selectBuilder = QueryBuilder.select().all();
        } else {
            selectBuilder = QueryBuilder.select();
            for (String col : fields) {
                ((Select.Selection) selectBuilder).column(col);
            }
        }
        stmt = selectBuilder.from(table);
        // The statement builder is not setup right for tokens.
        // So, we need to build it manually.
        String initialStmt = stmt.toString();
        StringBuilder scanStmt = new StringBuilder();
        scanStmt.append(initialStmt.substring(0, initialStmt.length() - 1));
        scanStmt.append(" WHERE ");
        scanStmt.append(QueryBuilder.token(YCSB_KEY));
        scanStmt.append(" >= ");
        scanStmt.append("token('");
        scanStmt.append(startkey);
        scanStmt.append("')");
        scanStmt.append(" LIMIT ");
        scanStmt.append(recordcount);
        stmt = new SimpleStatement(scanStmt.toString());
        stmt.setConsistencyLevel(readConsistencyLevel);
        if (debug) {
            System.out.println(stmt.toString());
        }
        if (trace) {
            stmt.enableTracing();
        }
        ResultSet rs = session.execute(stmt);
        HashMap<String, ByteIterator> tuple;
        while (!rs.isExhausted()) {
            Row row = rs.one();
            tuple = new HashMap<String, ByteIterator>();
            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) {
        e.printStackTrace();
        System.out.println("Error scanning with startkey: " + startkey);
        return Status.ERROR;
    }
}
Also used : ColumnDefinitions(com.datastax.driver.core.ColumnDefinitions) SimpleStatement(com.datastax.driver.core.SimpleStatement) Statement(com.datastax.driver.core.Statement) SimpleStatement(com.datastax.driver.core.SimpleStatement) ByteBuffer(java.nio.ByteBuffer) DBException(com.yahoo.ycsb.DBException) ByteArrayByteIterator(com.yahoo.ycsb.ByteArrayByteIterator) ByteArrayByteIterator(com.yahoo.ycsb.ByteArrayByteIterator) ByteIterator(com.yahoo.ycsb.ByteIterator) Select(com.datastax.driver.core.querybuilder.Select) ResultSet(com.datastax.driver.core.ResultSet) Row(com.datastax.driver.core.Row)

Example 9 with ByteArrayByteIterator

use of com.yahoo.ycsb.ByteArrayByteIterator in project YCSB by brianfrankcooper.

the class AbstractDBTestCases method testScan.

/**
   * Test method for {@link DB#scan}.
   */
@Test
public void testScan() {
    final DB client = getDB();
    final String table = getClass().getSimpleName();
    // Insert a bunch of documents.
    for (int i = 0; i < 100; ++i) {
        HashMap<String, ByteIterator> inserted = new HashMap<String, ByteIterator>();
        inserted.put("a", new ByteArrayByteIterator(new byte[] { (byte) (i & 0xFF), (byte) (i >> 8 & 0xFF), (byte) (i >> 16 & 0xFF), (byte) (i >> 24 & 0xFF) }));
        Status result = client.insert(table, padded(i), inserted);
        assertThat("Insert did not return success (0).", result, is(Status.OK));
    }
    Set<String> keys = Collections.singleton("a");
    Vector<HashMap<String, ByteIterator>> results = new Vector<HashMap<String, ByteIterator>>();
    Status result = client.scan(table, "00050", 5, null, results);
    assertThat("Read did not return success (0).", result, is(Status.OK));
    assertThat(results.size(), is(5));
    for (int i = 0; i < 5; ++i) {
        HashMap<String, ByteIterator> read = results.get(i);
        for (String key : keys) {
            ByteIterator iter = read.get(key);
            assertThat("Did not read the inserted field: " + key, iter, notNullValue());
            assertTrue(iter.hasNext());
            assertThat(iter.nextByte(), is(Byte.valueOf((byte) ((i + 50) & 0xFF))));
            assertTrue(iter.hasNext());
            assertThat(iter.nextByte(), is(Byte.valueOf((byte) ((i + 50) >> 8 & 0xFF))));
            assertTrue(iter.hasNext());
            assertThat(iter.nextByte(), is(Byte.valueOf((byte) ((i + 50) >> 16 & 0xFF))));
            assertTrue(iter.hasNext());
            assertThat(iter.nextByte(), is(Byte.valueOf((byte) ((i + 50) >> 24 & 0xFF))));
            assertFalse(iter.hasNext());
        }
    }
}
Also used : Status(com.yahoo.ycsb.Status) ByteArrayByteIterator(com.yahoo.ycsb.ByteArrayByteIterator) ByteArrayByteIterator(com.yahoo.ycsb.ByteArrayByteIterator) ByteIterator(com.yahoo.ycsb.ByteIterator) HashMap(java.util.HashMap) Vector(java.util.Vector) DB(com.yahoo.ycsb.DB) Test(org.junit.Test)

Example 10 with ByteArrayByteIterator

use of com.yahoo.ycsb.ByteArrayByteIterator in project YCSB by brianfrankcooper.

the class AbstractDBTestCases method testInsertReadDelete.

/**
   * Test method for {@link DB#insert}, {@link DB#read}, and {@link DB#delete} .
   */
@Test
public void testInsertReadDelete() {
    final DB client = getDB();
    final String table = getClass().getSimpleName();
    final String id = "delete";
    HashMap<String, ByteIterator> inserted = new HashMap<String, ByteIterator>();
    inserted.put("a", new ByteArrayByteIterator(new byte[] { 1, 2, 3, 4 }));
    Status result = client.insert(table, id, inserted);
    assertThat("Insert did not return success (0).", result, is(Status.OK));
    HashMap<String, ByteIterator> read = new HashMap<String, ByteIterator>();
    Set<String> keys = Collections.singleton("a");
    result = client.read(table, id, keys, read);
    assertThat("Read did not return success (0).", result, is(Status.OK));
    for (String key : keys) {
        ByteIterator iter = read.get(key);
        assertThat("Did not read the inserted field: " + key, iter, notNullValue());
        assertTrue(iter.hasNext());
        assertThat(iter.nextByte(), is(Byte.valueOf((byte) 1)));
        assertTrue(iter.hasNext());
        assertThat(iter.nextByte(), is(Byte.valueOf((byte) 2)));
        assertTrue(iter.hasNext());
        assertThat(iter.nextByte(), is(Byte.valueOf((byte) 3)));
        assertTrue(iter.hasNext());
        assertThat(iter.nextByte(), is(Byte.valueOf((byte) 4)));
        assertFalse(iter.hasNext());
    }
    result = client.delete(table, id);
    assertThat("Delete did not return success (0).", result, is(Status.OK));
    read.clear();
    result = client.read(table, id, null, read);
    assertThat("Read, after delete, did not return not found (1).", result, is(Status.NOT_FOUND));
    assertThat("Found the deleted fields.", read.size(), is(0));
    result = client.delete(table, id);
    assertThat("Delete did not return not found (1).", result, is(Status.NOT_FOUND));
}
Also used : Status(com.yahoo.ycsb.Status) ByteArrayByteIterator(com.yahoo.ycsb.ByteArrayByteIterator) ByteArrayByteIterator(com.yahoo.ycsb.ByteArrayByteIterator) ByteIterator(com.yahoo.ycsb.ByteIterator) HashMap(java.util.HashMap) DB(com.yahoo.ycsb.DB) Test(org.junit.Test)

Aggregations

ByteArrayByteIterator (com.yahoo.ycsb.ByteArrayByteIterator)25 ByteIterator (com.yahoo.ycsb.ByteIterator)15 HashMap (java.util.HashMap)12 DBException (com.yahoo.ycsb.DBException)9 IOException (java.io.IOException)6 DB (com.yahoo.ycsb.DB)4 Status (com.yahoo.ycsb.Status)4 EntityProperty (com.microsoft.azure.storage.table.EntityProperty)3 Test (org.junit.Test)3 ColumnDefinitions (com.datastax.driver.core.ColumnDefinitions)2 ResultSet (com.datastax.driver.core.ResultSet)2 Row (com.datastax.driver.core.Row)2 SimpleStatement (com.datastax.driver.core.SimpleStatement)2 Statement (com.datastax.driver.core.Statement)2 Select (com.datastax.driver.core.querybuilder.Select)2 ByteString (com.google.bigtable.repackaged.com.google.protobuf.ByteString)2 Column (com.google.bigtable.v1.Column)2 Family (com.google.bigtable.v1.Family)2 DeleteFromRow (com.google.bigtable.v1.Mutation.DeleteFromRow)2 ReadRowsRequest (com.google.bigtable.v1.ReadRowsRequest)2