Search in sources :

Example 36 with ByteIterator

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

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

the class OrientDBClientTest method updateTest.

@Test
public void updateTest() {
    String preupdateString = "preupdate";
    String user0 = "user0";
    String user1 = "user1";
    String user2 = "user2";
    OPartitionedDatabasePool pool = orientDBClient.getDatabasePool();
    try (ODatabaseDocumentTx db = pool.acquire()) {
        // Manually insert three documents
        for (String key : Arrays.asList(user0, user1, user2)) {
            ODocument doc = new ODocument(CLASS);
            for (int i = 0; i < NUM_FIELDS; i++) {
                doc.field(FIELD_PREFIX + i, preupdateString);
            }
            doc.save();
            ODictionary<ORecord> dictionary = db.getDictionary();
            dictionary.put(key, doc);
        }
    }
    HashMap<String, ByteIterator> updateMap = new HashMap<>();
    for (int i = 0; i < NUM_FIELDS; i++) {
        updateMap.put(FIELD_PREFIX + i, new StringByteIterator(buildDeterministicValue(user1, FIELD_PREFIX + i)));
    }
    orientDBClient.update(CLASS, user1, updateMap);
    try (ODatabaseDocumentTx db = pool.acquire()) {
        ODictionary<ORecord> dictionary = db.getDictionary();
        // Ensure that user0 record was not changed
        ODocument result = dictionary.get(user0);
        for (int i = 0; i < NUM_FIELDS; i++) {
            assertEquals("Assert first row fields contain preupdateString", result.field(FIELD_PREFIX + i), preupdateString);
        }
        // Check that all the columns have expected values for user1 record
        result = dictionary.get(user1);
        for (int i = 0; i < NUM_FIELDS; i++) {
            assertEquals("Assert updated row fields are correct", result.field(FIELD_PREFIX + i), updateMap.get(FIELD_PREFIX + i).toString());
        }
        // Ensure that user2 record was not changed
        result = dictionary.get(user2);
        for (int i = 0; i < NUM_FIELDS; i++) {
            assertEquals("Assert third row fields contain preupdateString", result.field(FIELD_PREFIX + i), preupdateString);
        }
    }
}
Also used : StringByteIterator(site.ycsb.StringByteIterator) ByteIterator(site.ycsb.ByteIterator) OPartitionedDatabasePool(com.orientechnologies.orient.core.db.OPartitionedDatabasePool) ORecord(com.orientechnologies.orient.core.record.ORecord) StringByteIterator(site.ycsb.StringByteIterator) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 38 with ByteIterator

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

the class MongoDbClient 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 {
        MongoCollection<Document> collection = database.getCollection(table);
        Document query = new Document("_id", key);
        Document fieldsToSet = new Document();
        for (Map.Entry<String, ByteIterator> entry : values.entrySet()) {
            fieldsToSet.put(entry.getKey(), entry.getValue().toArray());
        }
        Document update = new Document("$set", fieldsToSet);
        UpdateResult result = collection.updateOne(query, update);
        if (result.wasAcknowledged() && result.getMatchedCount() == 0) {
            System.err.println("Nothing updated for key " + key);
            return Status.NOT_FOUND;
        }
        return Status.OK;
    } catch (Exception e) {
        System.err.println(e.toString());
        return Status.ERROR;
    }
}
Also used : ByteIterator(site.ycsb.ByteIterator) ByteArrayByteIterator(site.ycsb.ByteArrayByteIterator) Document(org.bson.Document) HashMap(java.util.HashMap) Map(java.util.Map) UpdateResult(com.mongodb.client.result.UpdateResult) DBException(site.ycsb.DBException)

Example 39 with ByteIterator

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

the class MongoDbClient 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. See the {@link DB}
 *         class's description for a discussion of error codes.
 */
@Override
public Status scan(String table, String startkey, int recordcount, Set<String> fields, Vector<HashMap<String, ByteIterator>> result) {
    MongoCursor<Document> cursor = null;
    try {
        MongoCollection<Document> collection = database.getCollection(table);
        Document scanRange = new Document("$gte", startkey);
        Document query = new Document("_id", scanRange);
        Document sort = new Document("_id", INCLUDE);
        FindIterable<Document> findIterable = collection.find(query).sort(sort).limit(recordcount);
        if (fields != null) {
            Document projection = new Document();
            for (String fieldName : fields) {
                projection.put(fieldName, INCLUDE);
            }
            findIterable.projection(projection);
        }
        cursor = findIterable.iterator();
        if (!cursor.hasNext()) {
            System.err.println("Nothing found in scan for key " + startkey);
            return Status.ERROR;
        }
        result.ensureCapacity(recordcount);
        while (cursor.hasNext()) {
            HashMap<String, ByteIterator> resultMap = new HashMap<String, ByteIterator>();
            Document obj = cursor.next();
            fillMap(resultMap, obj);
            result.add(resultMap);
        }
        return Status.OK;
    } catch (Exception e) {
        System.err.println(e.toString());
        return Status.ERROR;
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}
Also used : ByteIterator(site.ycsb.ByteIterator) ByteArrayByteIterator(site.ycsb.ByteArrayByteIterator) HashMap(java.util.HashMap) Document(org.bson.Document) DBException(site.ycsb.DBException)

Example 40 with ByteIterator

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

the class MongoDbClient method insert.

/**
 * Insert 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.
 *
 * @param table
 *          The name of the table
 * @param key
 *          The record key of the record to insert.
 * @param values
 *          A HashMap of field/value pairs to insert in the record
 * @return Zero on success, a non-zero error code on error. See the {@link DB}
 *         class's description for a discussion of error codes.
 */
@Override
public Status insert(String table, String key, Map<String, ByteIterator> values) {
    try {
        MongoCollection<Document> collection = database.getCollection(table);
        Document toInsert = new Document("_id", key);
        for (Map.Entry<String, ByteIterator> entry : values.entrySet()) {
            toInsert.put(entry.getKey(), entry.getValue().toArray());
        }
        if (batchSize == 1) {
            if (useUpsert) {
                // this is effectively an insert, but using an upsert instead due
                // to current inability of the framework to clean up after itself
                // between test runs.
                collection.replaceOne(new Document("_id", toInsert.get("_id")), toInsert, UPDATE_WITH_UPSERT);
            } else {
                collection.insertOne(toInsert);
            }
        } else {
            bulkInserts.add(toInsert);
            if (bulkInserts.size() == batchSize) {
                if (useUpsert) {
                    List<UpdateOneModel<Document>> updates = new ArrayList<UpdateOneModel<Document>>(bulkInserts.size());
                    for (Document doc : bulkInserts) {
                        updates.add(new UpdateOneModel<Document>(new Document("_id", doc.get("_id")), doc, UPDATE_WITH_UPSERT));
                    }
                    collection.bulkWrite(updates);
                } else {
                    collection.insertMany(bulkInserts, INSERT_UNORDERED);
                }
                bulkInserts.clear();
            } else {
                return Status.BATCHED_OK;
            }
        }
        return Status.OK;
    } catch (Exception e) {
        System.err.println("Exception while trying bulk insert with " + bulkInserts.size());
        e.printStackTrace();
        return Status.ERROR;
    }
}
Also used : UpdateOneModel(com.mongodb.client.model.UpdateOneModel) ByteIterator(site.ycsb.ByteIterator) ByteArrayByteIterator(site.ycsb.ByteArrayByteIterator) ArrayList(java.util.ArrayList) Document(org.bson.Document) HashMap(java.util.HashMap) Map(java.util.Map) DBException(site.ycsb.DBException)

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