Search in sources :

Example 11 with ByteArrayByteIterator

use of site.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) {
        Map<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(site.ycsb.Status) ByteArrayByteIterator(site.ycsb.ByteArrayByteIterator) ByteIterator(site.ycsb.ByteIterator) ByteArrayByteIterator(site.ycsb.ByteArrayByteIterator) HashMap(java.util.HashMap) Vector(java.util.Vector) DB(site.ycsb.DB) Test(org.junit.Test)

Example 12 with ByteArrayByteIterator

use of site.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(site.ycsb.Status) ByteArrayByteIterator(site.ycsb.ByteArrayByteIterator) ByteIterator(site.ycsb.ByteIterator) ByteArrayByteIterator(site.ycsb.ByteArrayByteIterator) HashMap(java.util.HashMap) DB(site.ycsb.DB) Test(org.junit.Test)

Example 13 with ByteArrayByteIterator

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

the class AsyncHBaseClient method read.

@Override
public Status read(String table, String key, Set<String> fields, Map<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(site.ycsb.ByteArrayByteIterator) KeyValue(org.hbase.async.KeyValue) GetRequest(org.hbase.async.GetRequest) IOException(java.io.IOException) DBException(site.ycsb.DBException)

Example 14 with ByteArrayByteIterator

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

the class AsyncHBaseClient method scan.

@Override
public Status scan(String table, String startkey, int recordcount, Set<String> fields, Vector<HashMap<String, ByteIterator>> result) {
    setTable(table);
    final Scanner scanner = client.newScanner(lastTableBytes);
    scanner.setFamily(columnFamilyBytes);
    scanner.setStartKey(startkey.getBytes(UTF8_CHARSET));
    // No end key... *sniff*
    if (fields != null) {
        scanner.setQualifiers(getQualifierList(fields));
    }
    // no filters? *sniff*
    ArrayList<ArrayList<KeyValue>> rows = null;
    try {
        int numResults = 0;
        while ((rows = scanner.nextRows().join(joinTimeout)) != null) {
            for (final ArrayList<KeyValue> row : rows) {
                final HashMap<String, ByteIterator> rowResult = new HashMap<String, ByteIterator>(row.size());
                for (final KeyValue column : row) {
                    rowResult.put(new String(column.qualifier()), // be GC'd.
                    new ByteArrayByteIterator(column.value()));
                    if (debug) {
                        System.out.println("Got scan result for key: " + Bytes.pretty(column.key()));
                    }
                }
                result.add(rowResult);
                numResults++;
                if (numResults >= recordcount) {
                    // if hit recordcount, bail out
                    break;
                }
            }
        }
        scanner.close().join(joinTimeout);
        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 " + startkey + ": " + e.getMessage());
        return Status.ERROR;
    }
    return Status.ERROR;
}
Also used : Scanner(org.hbase.async.Scanner) KeyValue(org.hbase.async.KeyValue) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IOException(java.io.IOException) DBException(site.ycsb.DBException) ByteArrayByteIterator(site.ycsb.ByteArrayByteIterator) ByteIterator(site.ycsb.ByteIterator) ByteArrayByteIterator(site.ycsb.ByteArrayByteIterator)

Example 15 with ByteArrayByteIterator

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

the class AzureClient method readEntity.

private Status readEntity(String key, Map<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(site.ycsb.ByteArrayByteIterator) ByteIterator(site.ycsb.ByteIterator) ByteArrayByteIterator(site.ycsb.ByteArrayByteIterator) TableOperation(com.microsoft.azure.storage.table.TableOperation) DBException(site.ycsb.DBException)

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