Search in sources :

Example 6 with BasicDBObject

use of com.massivecraft.massivecore.xlib.mongodb.BasicDBObject in project MassiveCore by MassiveCraft.

the class DriverMongo method load.

@Override
public Entry<JsonObject, Long> load(Coll<?> coll, String id) {
    DBCollection dbcoll = fixColl(coll);
    BasicDBObject raw = (BasicDBObject) dbcoll.findOne(new BasicDBObject(ID_FIELD, id));
    return loadRaw(raw);
}
Also used : DBCollection(com.massivecraft.massivecore.xlib.mongodb.DBCollection) BasicDBObject(com.massivecraft.massivecore.xlib.mongodb.BasicDBObject)

Example 7 with BasicDBObject

use of com.massivecraft.massivecore.xlib.mongodb.BasicDBObject in project MassiveCore by MassiveCraft.

the class DriverMongo method containsId.

@Override
public boolean containsId(Coll<?> coll, String id) {
    DBCollection dbcoll = fixColl(coll);
    DBCursor cursor = dbcoll.find(new BasicDBObject(ID_FIELD, id));
    return cursor.count() != 0;
}
Also used : DBCollection(com.massivecraft.massivecore.xlib.mongodb.DBCollection) BasicDBObject(com.massivecraft.massivecore.xlib.mongodb.BasicDBObject) DBCursor(com.massivecraft.massivecore.xlib.mongodb.DBCursor)

Example 8 with BasicDBObject

use of com.massivecraft.massivecore.xlib.mongodb.BasicDBObject in project MassiveCore by MassiveCraft.

the class DriverMongo method loadAll.

@Override
public Map<String, Entry<JsonObject, Long>> loadAll(Coll<?> coll) {
    // Declare Ret
    Map<String, Entry<JsonObject, Long>> ret = null;
    // Fix Coll
    DBCollection dbcoll = fixColl(coll);
    // Find All
    DBCursor cursor = dbcoll.find();
    try {
        // Create Ret
        ret = new MassiveMap<>(cursor.count());
        // For Each Found
        while (cursor.hasNext()) {
            BasicDBObject raw = (BasicDBObject) cursor.next();
            // Get ID
            Object rawId = raw.removeField(ID_FIELD);
            if (rawId == null)
                continue;
            String id = rawId.toString();
            // Get Entry
            Entry<JsonObject, Long> entry = loadRaw(raw);
            // NOTE: The entry can be a failed one with null and 0.
            // NOTE: We add it anyways since it's an informative failure.
            // NOTE: This is supported by our defined specification.
            // Add
            ret.put(id, entry);
        }
    } finally {
        cursor.close();
    }
    // Return Ret
    return ret;
}
Also used : DBCollection(com.massivecraft.massivecore.xlib.mongodb.DBCollection) BasicDBObject(com.massivecraft.massivecore.xlib.mongodb.BasicDBObject) Entry(java.util.Map.Entry) SimpleEntry(java.util.AbstractMap.SimpleEntry) DBCursor(com.massivecraft.massivecore.xlib.mongodb.DBCursor) JsonObject(com.massivecraft.massivecore.xlib.gson.JsonObject) JsonObject(com.massivecraft.massivecore.xlib.gson.JsonObject) BasicDBObject(com.massivecraft.massivecore.xlib.mongodb.BasicDBObject)

Example 9 with BasicDBObject

use of com.massivecraft.massivecore.xlib.mongodb.BasicDBObject in project MassiveCore by MassiveCraft.

the class GsonMongoConverter method mongo2GsonObject.

public static JsonObject mongo2GsonObject(DBObject inObject) {
    if (!(inObject instanceof BasicDBObject))
        throw new IllegalArgumentException("Expected BasicDBObject as argument type!");
    BasicDBObject in = (BasicDBObject) inObject;
    JsonObject jsonObject = new JsonObject();
    for (Entry<String, Object> entry : in.entrySet()) {
        String key = mongo2GsonKey(entry.getKey());
        Object val = entry.getValue();
        if (val instanceof BasicDBList) {
            jsonObject.add(key, mongo2GsonArray((BasicDBList) val));
        } else if (val instanceof BasicDBObject) {
            jsonObject.add(key, mongo2GsonObject((BasicDBObject) val));
        } else {
            jsonObject.add(key, mongo2GsonPrimitive(val));
        }
    }
    return jsonObject;
}
Also used : BasicDBObject(com.massivecraft.massivecore.xlib.mongodb.BasicDBObject) BasicDBList(com.massivecraft.massivecore.xlib.mongodb.BasicDBList) JsonObject(com.massivecraft.massivecore.xlib.gson.JsonObject) JsonObject(com.massivecraft.massivecore.xlib.gson.JsonObject) DBObject(com.massivecraft.massivecore.xlib.mongodb.DBObject) BasicDBObject(com.massivecraft.massivecore.xlib.mongodb.BasicDBObject)

Example 10 with BasicDBObject

use of com.massivecraft.massivecore.xlib.mongodb.BasicDBObject in project MassiveCore by MassiveCraft.

the class GsonMongoConverter method mongo2GsonArray.

public static JsonArray mongo2GsonArray(DBObject inObject) {
    if (!(inObject instanceof BasicDBList))
        throw new IllegalArgumentException("Expected BasicDBList as argument type!");
    BasicDBList in = (BasicDBList) inObject;
    JsonArray jsonArray = new JsonArray();
    for (int i = 0; i < in.size(); i++) {
        Object object = in.get(i);
        if (object instanceof BasicDBList) {
            jsonArray.add(mongo2GsonArray((BasicDBList) object));
        } else if (object instanceof BasicDBObject) {
            jsonArray.add(mongo2GsonObject((BasicDBObject) object));
        } else {
            jsonArray.add(mongo2GsonPrimitive(object));
        }
    }
    return jsonArray;
}
Also used : JsonArray(com.massivecraft.massivecore.xlib.gson.JsonArray) BasicDBList(com.massivecraft.massivecore.xlib.mongodb.BasicDBList) BasicDBObject(com.massivecraft.massivecore.xlib.mongodb.BasicDBObject) JsonObject(com.massivecraft.massivecore.xlib.gson.JsonObject) DBObject(com.massivecraft.massivecore.xlib.mongodb.DBObject) BasicDBObject(com.massivecraft.massivecore.xlib.mongodb.BasicDBObject)

Aggregations

BasicDBObject (com.massivecraft.massivecore.xlib.mongodb.BasicDBObject)10 DBCollection (com.massivecraft.massivecore.xlib.mongodb.DBCollection)7 JsonObject (com.massivecraft.massivecore.xlib.gson.JsonObject)5 DBCursor (com.massivecraft.massivecore.xlib.mongodb.DBCursor)3 BasicDBList (com.massivecraft.massivecore.xlib.mongodb.BasicDBList)2 DBObject (com.massivecraft.massivecore.xlib.mongodb.DBObject)2 SimpleEntry (java.util.AbstractMap.SimpleEntry)2 JsonArray (com.massivecraft.massivecore.xlib.gson.JsonArray)1 Entry (java.util.Map.Entry)1