Search in sources :

Example 11 with ByteArrayByteIterator

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

the class AbstractDBTestCases method testInsertReadUpdate.

/**
   * Test method for {@link DB#insert}, {@link DB#read}, and {@link DB#update} .
   */
@Test
public void testInsertReadUpdate() {
    DB client = getDB();
    final String table = getClass().getSimpleName();
    final String id = "update";
    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());
    }
    HashMap<String, ByteIterator> updated = new HashMap<String, ByteIterator>();
    updated.put("a", new ByteArrayByteIterator(new byte[] { 5, 6, 7, 8 }));
    result = client.update(table, id, updated);
    assertThat("Update did not return success (0).", result, is(Status.OK));
    read.clear();
    result = client.read(table, id, null, read);
    assertThat("Read, after update, 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) 5)));
        assertTrue(iter.hasNext());
        assertThat(iter.nextByte(), is(Byte.valueOf((byte) 6)));
        assertTrue(iter.hasNext());
        assertThat(iter.nextByte(), is(Byte.valueOf((byte) 7)));
        assertTrue(iter.hasNext());
        assertThat(iter.nextByte(), is(Byte.valueOf((byte) 8)));
        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) DB(com.yahoo.ycsb.DB) Test(org.junit.Test)

Example 12 with ByteArrayByteIterator

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

the class S3Client method readFromStorage.

/**
  * Download an object from S3.
  *
  * @param bucket
  *            The name of the bucket
  * @param key
  *            The file key of the object to upload/update.
  * @param result
  *            The Hash map where data from the object are written
  *
  */
protected Status readFromStorage(String bucket, String key, HashMap<String, ByteIterator> result, SSECustomerKey ssecLocal) {
    try {
        Map.Entry<S3Object, ObjectMetadata> objectAndMetadata = getS3ObjectAndMetadata(bucket, key, ssecLocal);
        //consuming the stream
        InputStream objectData = objectAndMetadata.getKey().getObjectContent();
        // writing the stream to bytes and to results
        int sizeOfFile = (int) objectAndMetadata.getValue().getContentLength();
        byte[] inputStreamToByte = new byte[sizeOfFile];
        objectData.read(inputStreamToByte, 0, sizeOfFile);
        result.put(key, new ByteArrayByteIterator(inputStreamToByte));
        objectData.close();
        objectAndMetadata.getKey().close();
    } catch (Exception e) {
        System.err.println("Not possible to get the object " + key);
        e.printStackTrace();
        return Status.ERROR;
    }
    return Status.OK;
}
Also used : ByteArrayByteIterator(com.yahoo.ycsb.ByteArrayByteIterator) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) S3Object(com.amazonaws.services.s3.model.S3Object) HashMap(java.util.HashMap) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) DBException(com.yahoo.ycsb.DBException)

Example 13 with ByteArrayByteIterator

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

the class AzureClient method readEntity.

private Status readEntity(String key, HashMap<String, ByteIterator> result) {
    try {
        // firstly, retrieve the entity to be deleted
        TableOperation retrieveOp = TableOperation.retrieve(partitionKey, key, DynamicTableEntity.class);
        DynamicTableEntity entity = cloudTable.execute(retrieveOp).getResultAsType();
        HashMap<String, EntityProperty> properties = entity.getProperties();
        for (Entry<String, EntityProperty> entry : properties.entrySet()) {
            String fieldName = entry.getKey();
            ByteIterator fieldVal = new ByteArrayByteIterator(entry.getValue().getValueAsByteArray());
            result.put(fieldName, fieldVal);
        }
        return Status.OK;
    } catch (Exception e) {
        return Status.ERROR;
    }
}
Also used : 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) TableOperation(com.microsoft.azure.storage.table.TableOperation) DBException(com.yahoo.ycsb.DBException)

Example 14 with ByteArrayByteIterator

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

the class AzureClient method readSubset.

/*
   * Read subset of properties instead of full fields with projection.
   */
public Status readSubset(String key, Set<String> fields, HashMap<String, ByteIterator> result) {
    String whereStr = String.format("RowKey eq '%s'", key);
    TableQuery<TableServiceEntity> projectionQuery = TableQuery.from(TableServiceEntity.class).where(whereStr).select(fields.toArray(new String[0]));
    EntityResolver<HashMap<String, ByteIterator>> resolver = new EntityResolver<HashMap<String, ByteIterator>>() {

        public HashMap<String, ByteIterator> resolve(String partitionkey, String rowKey, Date timeStamp, HashMap<String, EntityProperty> properties, String etag) {
            HashMap<String, ByteIterator> tmp = new HashMap<String, ByteIterator>();
            for (Entry<String, EntityProperty> entry : properties.entrySet()) {
                String key = entry.getKey();
                ByteIterator val = new ByteArrayByteIterator(entry.getValue().getValueAsByteArray());
                tmp.put(key, val);
            }
            return tmp;
        }
    };
    try {
        for (HashMap<String, ByteIterator> tmp : cloudTable.execute(projectionQuery, resolver)) {
            for (Entry<String, ByteIterator> entry : tmp.entrySet()) {
                String fieldName = entry.getKey();
                ByteIterator fieldVal = entry.getValue();
                result.put(fieldName, fieldVal);
            }
        }
        return Status.OK;
    } catch (Exception e) {
        return Status.ERROR;
    }
}
Also used : HashMap(java.util.HashMap) EntityResolver(com.microsoft.azure.storage.table.EntityResolver) TableServiceEntity(com.microsoft.azure.storage.table.TableServiceEntity) Date(java.util.Date) DBException(com.yahoo.ycsb.DBException) EntityProperty(com.microsoft.azure.storage.table.EntityProperty) ByteArrayByteIterator(com.yahoo.ycsb.ByteArrayByteIterator) ByteArrayByteIterator(com.yahoo.ycsb.ByteArrayByteIterator) ByteIterator(com.yahoo.ycsb.ByteIterator)

Example 15 with ByteArrayByteIterator

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

the class CassandraCQLClient 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, 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).where(QueryBuilder.eq(YCSB_KEY, key)).limit(1);
        stmt.setConsistencyLevel(readConsistencyLevel);
        if (debug) {
            System.out.println(stmt.toString());
        }
        if (trace) {
            stmt.enableTracing();
        }
        ResultSet rs = session.execute(stmt);
        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) {
        e.printStackTrace();
        System.out.println("Error reading key: " + key);
        return Status.ERROR;
    }
}
Also used : ColumnDefinitions(com.datastax.driver.core.ColumnDefinitions) ByteArrayByteIterator(com.yahoo.ycsb.ByteArrayByteIterator) SimpleStatement(com.datastax.driver.core.SimpleStatement) Statement(com.datastax.driver.core.Statement) Select(com.datastax.driver.core.querybuilder.Select) ResultSet(com.datastax.driver.core.ResultSet) Row(com.datastax.driver.core.Row) ByteBuffer(java.nio.ByteBuffer) DBException(com.yahoo.ycsb.DBException)

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