Search in sources :

Example 1 with DBCursor

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

the class DriverMongo method getIds.

@Override
public Collection<String> getIds(Coll<?> coll) {
    List<String> ret = null;
    DBCollection dbcoll = fixColl(coll);
    DBCursor cursor = dbcoll.find(dboEmpty, dboKeysId);
    try {
        ret = new ArrayList<>(cursor.count());
        while (cursor.hasNext()) {
            Object remoteId = cursor.next().get(ID_FIELD);
            ret.add(remoteId.toString());
        }
    } finally {
        cursor.close();
    }
    return ret;
}
Also used : DBCollection(com.massivecraft.massivecore.xlib.mongodb.DBCollection) DBCursor(com.massivecraft.massivecore.xlib.mongodb.DBCursor) JsonObject(com.massivecraft.massivecore.xlib.gson.JsonObject) BasicDBObject(com.massivecraft.massivecore.xlib.mongodb.BasicDBObject)

Example 2 with DBCursor

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

the class DriverMongo method getId2mtime.

@Override
public Map<String, Long> getId2mtime(Coll<?> coll) {
    Map<String, Long> ret = null;
    DBCollection dbcoll = fixColl(coll);
    DBCursor cursor = dbcoll.find(dboEmpty, dboKeysIdandMtime);
    try {
        ret = new HashMap<>(cursor.count());
        while (cursor.hasNext()) {
            BasicDBObject raw = (BasicDBObject) cursor.next();
            Object remoteId = raw.get(ID_FIELD);
            // In case there is no _mtime set we assume 1337.
            // NOTE: We can not use 0 since that one is reserved for errors.
            // Probably a manual database addition by the server owner.
            long mtime = raw.getLong(MTIME_FIELD, 1337L);
            ret.put(remoteId.toString(), mtime);
        }
    } finally {
        cursor.close();
    }
    return ret;
}
Also used : DBCollection(com.massivecraft.massivecore.xlib.mongodb.DBCollection) BasicDBObject(com.massivecraft.massivecore.xlib.mongodb.BasicDBObject) DBCursor(com.massivecraft.massivecore.xlib.mongodb.DBCursor) JsonObject(com.massivecraft.massivecore.xlib.gson.JsonObject) BasicDBObject(com.massivecraft.massivecore.xlib.mongodb.BasicDBObject)

Example 3 with DBCursor

use of com.massivecraft.massivecore.xlib.mongodb.DBCursor 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 4 with DBCursor

use of com.massivecraft.massivecore.xlib.mongodb.DBCursor 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)

Aggregations

BasicDBObject (com.massivecraft.massivecore.xlib.mongodb.BasicDBObject)4 DBCollection (com.massivecraft.massivecore.xlib.mongodb.DBCollection)4 DBCursor (com.massivecraft.massivecore.xlib.mongodb.DBCursor)4 JsonObject (com.massivecraft.massivecore.xlib.gson.JsonObject)3 SimpleEntry (java.util.AbstractMap.SimpleEntry)1 Entry (java.util.Map.Entry)1