Search in sources :

Example 1 with BasicDBObject

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

the class DriverMongo method loadRaw.

public Entry<JsonObject, Long> loadRaw(BasicDBObject raw) {
    if (raw == null)
        return new SimpleEntry<>(null, 0L);
    // Throw away the id field
    raw.removeField(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 = 1337L;
    Object mtimeObject = raw.removeField(MTIME_FIELD);
    if (mtimeObject != null) {
        mtime = ((Number) mtimeObject).longValue();
    }
    // Convert MongoDB --> GSON
    JsonObject element = GsonMongoConverter.mongo2GsonObject(raw);
    return new SimpleEntry<>(element, mtime);
}
Also used : SimpleEntry(java.util.AbstractMap.SimpleEntry) JsonObject(com.massivecraft.massivecore.xlib.gson.JsonObject) JsonObject(com.massivecraft.massivecore.xlib.gson.JsonObject) BasicDBObject(com.massivecraft.massivecore.xlib.mongodb.BasicDBObject)

Example 2 with BasicDBObject

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

the class DriverMongo method getMtime.

@Override
public long getMtime(Coll<?> coll, String id) {
    DBCollection dbcoll = fixColl(coll);
    BasicDBObject found = (BasicDBObject) dbcoll.findOne(new BasicDBObject(ID_FIELD, id), dboKeysMtime);
    if (found == null)
        return 0;
    // 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 = found.getLong(MTIME_FIELD, 1337L);
    return mtime;
}
Also used : DBCollection(com.massivecraft.massivecore.xlib.mongodb.DBCollection) BasicDBObject(com.massivecraft.massivecore.xlib.mongodb.BasicDBObject)

Example 3 with BasicDBObject

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

the class DriverMongo method save.

@Override
public long save(Coll<?> coll, String id, JsonObject data) {
    DBCollection dbcoll = fixColl(coll);
    BasicDBObject dbo = new BasicDBObject();
    long mtime = System.currentTimeMillis();
    dbo.put(ID_FIELD, id);
    dbo.put(MTIME_FIELD, mtime);
    GsonMongoConverter.gson2MongoObject(data, dbo);
    dbcoll.save(dbo, MassiveCoreMConf.get().getMongoDbWriteConcernSave());
    return mtime;
}
Also used : DBCollection(com.massivecraft.massivecore.xlib.mongodb.DBCollection) BasicDBObject(com.massivecraft.massivecore.xlib.mongodb.BasicDBObject)

Example 4 with BasicDBObject

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

the class DriverMongo method delete.

@Override
public void delete(Coll<?> coll, String id) {
    DBCollection dbcoll = fixColl(coll);
    dbcoll.remove(new BasicDBObject(ID_FIELD, id), MassiveCoreMConf.get().getMongoDbWriteConcernDelete());
}
Also used : DBCollection(com.massivecraft.massivecore.xlib.mongodb.DBCollection) BasicDBObject(com.massivecraft.massivecore.xlib.mongodb.BasicDBObject)

Example 5 with BasicDBObject

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

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