Search in sources :

Example 1 with RecordExistsException

use of edu.umass.cs.gnscommon.exceptions.server.RecordExistsException in project GNS by MobilityFirst.

the class NoSQLRecordsThroughputTest method testlookupMultipleSystemAndUserFields.

private static void testlookupMultipleSystemAndUserFields(String node, String guid, String field) {
    //NoSQLRecords instance = new MongoRecords<String>(node);
    NoSQLRecords instance = new DiskMapRecords(node);
    GNSRecordMap<String> recordMap = new GNSRecordMap<String>(instance, COLLECTION_NAME);
    JSONObject json = new JSONObject();
    try {
        json.put(field, "some value");
    } catch (JSONException e) {
        System.out.println("Problem creating json " + e);
    }
    ValuesMap valuesMap = new ValuesMap(json);
    NameRecord nameRecord = new NameRecord(recordMap, guid, valuesMap);
    try {
        instance.insert(COLLECTION_NAME, guid, nameRecord.toJSONObject());
    } catch (JSONException e) {
        System.out.println("Problem writing json " + e);
    } catch (FailedDBOperationException e) {
        System.out.println("Problem adding " + json.toString() + " as value of " + guid + ": " + e);
    } catch (RecordExistsException e) {
        System.out.println(guid + " record already exists in database. Try something else." + e);
    }
    // and try to read it as fast as possible
    try {
        ArrayList<ColumnField> userFields = new ArrayList<>(Arrays.asList(new ColumnField(field, ColumnFieldType.USER_JSON)));
        int frequency = 10000;
        reset();
        do {
            Map<ColumnField, Object> map = instance.lookupSomeFields(COLLECTION_NAME, guid, NameRecord.NAME, NameRecord.VALUES_MAP, userFields);
            if (incrCount() % frequency == 0) {
                System.out.println(map);
                System.out.println(DelayProfiler.getStats());
                System.out.println("op/s = " + Format.formatTime(getCount() * 1000.0 / (System.currentTimeMillis() - initTime)));
                if (getCount() > frequency * 20) {
                    System.out.println("**********************resetting************************");
                    reset();
                }
            }
        } while (true);
    } catch (FailedDBOperationException | RecordNotFoundException e) {
        System.out.println("Lookup failed: " + e);
    }
}
Also used : RecordExistsException(edu.umass.cs.gnscommon.exceptions.server.RecordExistsException) ValuesMap(edu.umass.cs.gnsserver.utils.ValuesMap) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException) GNSRecordMap(edu.umass.cs.gnsserver.gnsapp.recordmap.GNSRecordMap) RecordNotFoundException(edu.umass.cs.gnscommon.exceptions.server.RecordNotFoundException) NameRecord(edu.umass.cs.gnsserver.gnsapp.recordmap.NameRecord) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject)

Example 2 with RecordExistsException

use of edu.umass.cs.gnscommon.exceptions.server.RecordExistsException in project GNS by MobilityFirst.

the class MongoRecords method bulkUpdate.

/**
   *
   * @param collectionName
   * @param values
   * @throws FailedDBOperationException
   * @throws RecordExistsException
   */
public void bulkUpdate(String collectionName, Map<String, JSONObject> values) throws FailedDBOperationException, RecordExistsException {
    //String primaryKey = mongoCollectionSpecs.getCollectionSpec(collectionName).getPrimaryKey().getName();
    DBCollection collection = db.getCollection(collectionName);
    String primaryKey = mongoCollectionSpecs.getCollectionSpec(collectionName).getPrimaryKey().getName();
    db.requestEnsureConnection();
    BulkWriteOperation unordered = collection.initializeUnorderedBulkOperation();
    for (Map.Entry<String, JSONObject> entry : values.entrySet()) {
        BasicDBObject query = new BasicDBObject(primaryKey, entry.getKey());
        JSONObject value = entry.getValue();
        if (value != null) {
            DBObject document;
            try {
                document = (DBObject) JSON.parse(value.toString());
            } catch (Exception e) {
                throw new FailedDBOperationException(collectionName, "bulkUpdate", "Unable to parse json" + e.getMessage());
            }
            unordered.find(query).upsert().replaceOne(document);
        } else {
            unordered.find(query).removeOne();
        }
    }
    // Maybe check the result?
    BulkWriteResult result = unordered.execute();
}
Also used : BulkWriteOperation(com.mongodb.BulkWriteOperation) DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) Map(java.util.Map) ValuesMap(edu.umass.cs.gnsserver.utils.ValuesMap) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) DuplicateKeyException(com.mongodb.DuplicateKeyException) RecordNotFoundException(edu.umass.cs.gnscommon.exceptions.server.RecordNotFoundException) RecordExistsException(edu.umass.cs.gnscommon.exceptions.server.RecordExistsException) JSONException(org.json.JSONException) MongoException(com.mongodb.MongoException) UnknownHostException(java.net.UnknownHostException) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException) BulkWriteResult(com.mongodb.BulkWriteResult)

Example 3 with RecordExistsException

use of edu.umass.cs.gnscommon.exceptions.server.RecordExistsException in project GNS by MobilityFirst.

the class NoSQLTest method test_01_Insert.

/**
   *
   */
@Test
public void test_01_Insert() {
    JSONObject json = new JSONObject();
    try {
        json.put(field, "some value");
    } catch (JSONException e) {
        fail("Problem creating json " + e);
    }
    ValuesMap valuesMap = new ValuesMap(json);
    NameRecord nameRecord = new NameRecord(recordMap, guid, valuesMap);
    try {
        instance.insert(collection, guid, nameRecord.toJSONObject());
    } catch (FailedDBOperationException | JSONException | RecordExistsException e) {
        fail("Problem during insert " + e);
    }
}
Also used : RecordExistsException(edu.umass.cs.gnscommon.exceptions.server.RecordExistsException) NameRecord(edu.umass.cs.gnsserver.gnsapp.recordmap.NameRecord) JSONObject(org.json.JSONObject) ValuesMap(edu.umass.cs.gnsserver.utils.ValuesMap) JSONException(org.json.JSONException) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException) Test(org.junit.Test)

Example 4 with RecordExistsException

use of edu.umass.cs.gnscommon.exceptions.server.RecordExistsException in project GNS by MobilityFirst.

the class NoSQLTest method test_20_InsertRecord.

/**
   *
   */
@Test
public void test_20_InsertRecord() {
    JSONObject json = new JSONObject();
    try {
        json.put(field, "some value");
    } catch (JSONException e) {
        fail("Problem creating json " + e);
    }
    ValuesMap valuesMap = new ValuesMap(json);
    NameRecord nameRecord = new NameRecord(recordMap, guid2, valuesMap);
    try {
        instance.insert(collection, guid2, nameRecord.toJSONObject());
    } catch (FailedDBOperationException | JSONException | RecordExistsException e) {
        fail("Problem during insert " + e);
    }
}
Also used : RecordExistsException(edu.umass.cs.gnscommon.exceptions.server.RecordExistsException) NameRecord(edu.umass.cs.gnsserver.gnsapp.recordmap.NameRecord) JSONObject(org.json.JSONObject) ValuesMap(edu.umass.cs.gnsserver.utils.ValuesMap) JSONException(org.json.JSONException) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException) Test(org.junit.Test)

Example 5 with RecordExistsException

use of edu.umass.cs.gnscommon.exceptions.server.RecordExistsException in project GNS by MobilityFirst.

the class MongoRecords method insert.

@Override
public void insert(String collectionName, String guid, JSONObject value) throws FailedDBOperationException, RecordExistsException {
    db.requestStart();
    try {
        db.requestEnsureConnection();
        DBCollection collection = db.getCollection(collectionName);
        DBObject dbObject;
        try {
            dbObject = (DBObject) JSON.parse(value.toString());
        } catch (Exception e) {
            throw new FailedDBOperationException(collectionName, guid, "Unable to parse json" + e.getMessage());
        }
        try {
            collection.insert(dbObject);
        } catch (DuplicateKeyException e) {
            throw new RecordExistsException(collectionName, guid);
        } catch (MongoException e) {
            DatabaseConfig.getLogger().log(Level.FINE, "{0} insert failed: {1}", new Object[] { dbName, e.getMessage() });
            throw new FailedDBOperationException(collectionName, dbObject.toString(), "Original mongo exception:" + e.getMessage());
        }
    } finally {
        db.requestDone();
    }
}
Also used : RecordExistsException(edu.umass.cs.gnscommon.exceptions.server.RecordExistsException) DBCollection(com.mongodb.DBCollection) MongoException(com.mongodb.MongoException) JSONObject(org.json.JSONObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) DuplicateKeyException(com.mongodb.DuplicateKeyException) RecordNotFoundException(edu.umass.cs.gnscommon.exceptions.server.RecordNotFoundException) RecordExistsException(edu.umass.cs.gnscommon.exceptions.server.RecordExistsException) JSONException(org.json.JSONException) MongoException(com.mongodb.MongoException) UnknownHostException(java.net.UnknownHostException) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException) DuplicateKeyException(com.mongodb.DuplicateKeyException)

Aggregations

FailedDBOperationException (edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)5 RecordExistsException (edu.umass.cs.gnscommon.exceptions.server.RecordExistsException)5 JSONException (org.json.JSONException)5 JSONObject (org.json.JSONObject)5 ValuesMap (edu.umass.cs.gnsserver.utils.ValuesMap)4 RecordNotFoundException (edu.umass.cs.gnscommon.exceptions.server.RecordNotFoundException)3 NameRecord (edu.umass.cs.gnsserver.gnsapp.recordmap.NameRecord)3 BasicDBObject (com.mongodb.BasicDBObject)2 DBCollection (com.mongodb.DBCollection)2 DBObject (com.mongodb.DBObject)2 DuplicateKeyException (com.mongodb.DuplicateKeyException)2 MongoException (com.mongodb.MongoException)2 UnknownHostException (java.net.UnknownHostException)2 Test (org.junit.Test)2 BulkWriteOperation (com.mongodb.BulkWriteOperation)1 BulkWriteResult (com.mongodb.BulkWriteResult)1 GNSRecordMap (edu.umass.cs.gnsserver.gnsapp.recordmap.GNSRecordMap)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1