Search in sources :

Example 51 with ByteIterator

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

the class ArangoDBClient method update.

/**
 * Update a record in the database. Any field/value pairs in the specified
 * values HashMap will be written into the record with the specified record
 * key, overwriting any existing values with the same field name.
 *
 * @param table
 *      The name of the table
 * @param key
 *      The record key of the record to write.
 * @param values
 *      A HashMap of field/value pairs to update in the record
 * @return Zero on success, a non-zero error code on error. See this class's
 *     description for a discussion of error codes.
 */
@Override
public Status update(String table, String key, Map<String, ByteIterator> values) {
    try {
        if (!transactionUpdate) {
            BaseDocument updateDoc = new BaseDocument();
            for (Entry<String, ByteIterator> field : values.entrySet()) {
                updateDoc.addAttribute(field.getKey(), byteIteratorToString(field.getValue()));
            }
            arangoDB.db(databaseName).collection(table).updateDocument(key, updateDoc);
            return Status.OK;
        } else {
            // id for documentHandle
            String transactionAction = "function (id) {" + // use internal database functions
            "var db = require('internal').db;" + // collection.update(document, data, overwrite, keepNull, waitForSync)
            String.format("db._update(id, %s, true, false, %s);}", mapToJson(values), Boolean.toString(waitForSync).toLowerCase());
            TransactionOptions options = new TransactionOptions();
            options.writeCollections(table);
            options.params(createDocumentHandle(table, key));
            arangoDB.db(databaseName).transaction(transactionAction, Void.class, options);
            return Status.OK;
        }
    } catch (ArangoDBException e) {
        logger.error("Exception while trying update {} {} with ex {}", table, key, e.toString());
    }
    return Status.ERROR;
}
Also used : ByteIterator(site.ycsb.ByteIterator) StringByteIterator(site.ycsb.StringByteIterator) BaseDocument(com.arangodb.entity.BaseDocument) TransactionOptions(com.arangodb.model.TransactionOptions) ArangoDBException(com.arangodb.ArangoDBException)

Example 52 with ByteIterator

use of site.ycsb.ByteIterator 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 53 with ByteIterator

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

the class AsyncHBaseTest method testReadMissingRow.

@Test
public void testReadMissingRow() throws Exception {
    final HashMap<String, ByteIterator> result = new HashMap<String, ByteIterator>();
    final Status status = client.read(tableName, "Missing row", null, result);
    assertEquals(Status.NOT_FOUND, status);
    assertEquals(0, result.size());
}
Also used : Status(site.ycsb.Status) ByteIterator(site.ycsb.ByteIterator) StringByteIterator(site.ycsb.StringByteIterator) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 54 with ByteIterator

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

the class AsyncHBaseTest method testScan.

@Test
public void testScan() throws Exception {
    // Fill with data
    final String colStr = "row_number";
    final byte[] col = Bytes.toBytes(colStr);
    final int n = 10;
    final List<Put> puts = new ArrayList<Put>(n);
    for (int i = 0; i < n; i++) {
        final byte[] key = Bytes.toBytes(String.format("%05d", i));
        final byte[] value = java.nio.ByteBuffer.allocate(4).putInt(i).array();
        final Put p = new Put(key);
        p.addColumn(Bytes.toBytes(COLUMN_FAMILY), col, value);
        puts.add(p);
    }
    table.put(puts);
    // Test
    final Vector<HashMap<String, ByteIterator>> result = new Vector<HashMap<String, ByteIterator>>();
    // Scan 5 records, skipping the first
    client.scan(tableName, "00001", 5, null, result);
    assertEquals(5, result.size());
    for (int i = 0; i < 5; i++) {
        final HashMap<String, ByteIterator> row = result.get(i);
        assertEquals(1, row.size());
        assertTrue(row.containsKey(colStr));
        final byte[] bytes = row.get(colStr).toArray();
        final ByteBuffer buf = ByteBuffer.wrap(bytes);
        final int rowNum = buf.getInt();
        assertEquals(i + 1, rowNum);
    }
}
Also used : ByteIterator(site.ycsb.ByteIterator) StringByteIterator(site.ycsb.StringByteIterator) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Vector(java.util.Vector) ByteBuffer(java.nio.ByteBuffer) Put(org.apache.hadoop.hbase.client.Put) Test(org.junit.Test)

Example 55 with ByteIterator

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

the class AzureCosmosClient method update.

/**
 * Update a record in the database. Any field/value pairs in the specified
 * values HashMap will be written into the record with the specified record key,
 * overwriting any existing values with the same field name.
 *
 * @param table  The name of the table
 * @param key    The record key of the record to write.
 * @param values A HashMap of field/value pairs to update in the record
 * @return Zero on success, a non-zero error code on error
 */
@Override
public Status update(String table, String key, Map<String, ByteIterator> values) {
    String readEtag = "";
    // that will be a future improvement.
    for (int attempt = 0; attempt < NUM_UPDATE_ATTEMPTS; attempt++) {
        try {
            CosmosContainer container = AzureCosmosClient.containerCache.get(table);
            if (container == null) {
                container = AzureCosmosClient.database.getContainer(table);
                AzureCosmosClient.containerCache.put(table, container);
            }
            CosmosItemResponse<ObjectNode> response = container.readItem(key, new PartitionKey(key), ObjectNode.class);
            readEtag = response.getETag();
            ObjectNode node = response.getItem();
            for (Entry<String, ByteIterator> pair : values.entrySet()) {
                node.put(pair.getKey(), pair.getValue().toString());
            }
            CosmosItemRequestOptions requestOptions = new CosmosItemRequestOptions();
            requestOptions.setIfMatchETag(readEtag);
            PartitionKey pk = new PartitionKey(key);
            container.replaceItem(node, key, pk, requestOptions);
            return Status.OK;
        } catch (CosmosException e) {
            if (!AzureCosmosClient.includeExceptionStackInLog) {
                e = null;
            }
            LOGGER.error("Failed to update key {} to collection {} in database {} on attempt {}", key, table, AzureCosmosClient.databaseName, attempt, e);
        }
    }
    return Status.ERROR;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ByteIterator(site.ycsb.ByteIterator) StringByteIterator(site.ycsb.StringByteIterator) CosmosItemRequestOptions(com.azure.cosmos.models.CosmosItemRequestOptions) CosmosContainer(com.azure.cosmos.CosmosContainer) PartitionKey(com.azure.cosmos.models.PartitionKey) CosmosException(com.azure.cosmos.CosmosException)

Aggregations

ByteIterator (site.ycsb.ByteIterator)131 HashMap (java.util.HashMap)98 StringByteIterator (site.ycsb.StringByteIterator)92 Status (site.ycsb.Status)62 Test (org.junit.Test)53 ByteArrayByteIterator (site.ycsb.ByteArrayByteIterator)34 DBException (site.ycsb.DBException)30 Map (java.util.Map)20 IOException (java.io.IOException)10 Put (org.apache.hadoop.hbase.client.Put)8 ArrayList (java.util.ArrayList)7 Vector (java.util.Vector)7 ByteBuffer (java.nio.ByteBuffer)6 HashSet (java.util.HashSet)6 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6 NumericByteIterator (site.ycsb.NumericByteIterator)5 EntityProperty (com.microsoft.azure.storage.table.EntityProperty)4 Properties (java.util.Properties)4 Assume.assumeNoException (org.junit.Assume.assumeNoException)4 DB (site.ycsb.DB)4