Copydb command access check, remote mtime defaults to 0, mote mtime field to the top.

This commit is contained in:
Olof Larsson 2014-03-28 15:13:23 +01:00
parent 16fc6b94e5
commit fa69405239
3 changed files with 35 additions and 10 deletions

View File

@ -78,6 +78,14 @@ public class CmdMCoreMStoreCopydb extends MCommand
Collection<String> ids = fromDriver.getIds(fromColl);
msg("<i>Now copying collection <h>%d/%d %s <i>with <h>%d <i>documents.", countCollCurrent, countCollTotal, collname, ids.size());
// Do a load check to verify we have access to this folder.
if (ids.size() > 0 && fromDriver.load(fromColl, ids.iterator().next()) == null)
{
msg("<b>Skipping <h>%s <b>since could not load data.", collname);
continue;
}
for (String id : ids)
{
Entry<JsonElement, Long> data = fromDriver.load(fromColl, id);

View File

@ -70,10 +70,14 @@ public class DriverMongo extends DriverAbstract
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 null;
if ( ! found.containsField(MTIME_FIELD)) return null; // This should not happen! But better to ignore than crash?
return found.getLong(MTIME_FIELD);
// In case there is no _mtime set we assume 0. Probably a manual database addition by the server owner.
long mtime = found.getLong(MTIME_FIELD, 0);
return mtime;
}
@Override
@ -116,8 +120,10 @@ public class DriverMongo extends DriverAbstract
{
BasicDBObject raw = (BasicDBObject)cursor.next();
Object remoteId = raw.get(ID_FIELD);
if ( ! raw.containsField(MTIME_FIELD)) continue; // This should not happen! But better to ignore than crash?
Long mtime = raw.getLong(MTIME_FIELD);
// In case there is no _mtime set we assume 0. Probably a manual database addition by the server owner.
long mtime = raw.getLong(MTIME_FIELD, 0);
ret.put(remoteId.toString(), mtime);
}
}
@ -136,8 +142,12 @@ public class DriverMongo extends DriverAbstract
BasicDBObject raw = (BasicDBObject)dbcoll.findOne(new BasicDBObject(ID_FIELD, id));
if (raw == null) return null;
Long mtime = ((Number)raw.removeField(MTIME_FIELD)).longValue();
raw.removeField(ID_FIELD);
Long mtime = 0L;
Object mtimeObject = raw.removeField(MTIME_FIELD);
if (mtimeObject != null)
{
mtime = ((Number)mtimeObject).longValue();
}
JsonElement element = GsonMongoConverter.mongo2GsonObject(raw);
@ -149,10 +159,13 @@ public class DriverMongo extends DriverAbstract
{
DBCollection dbcoll = fixColl(coll);
BasicDBObject dbo = GsonMongoConverter.gson2MongoObject(data);
BasicDBObject dbo = new BasicDBObject();
Long mtime = System.currentTimeMillis();
dbo.put(MTIME_FIELD, mtime);
dbo.put(ID_FIELD, id);
dbo.put(MTIME_FIELD, mtime);
GsonMongoConverter.gson2MongoObject(data, dbo);
dbcoll.save(dbo);

View File

@ -38,10 +38,9 @@ public final class GsonMongoConverter
return key;
}
public static BasicDBObject gson2MongoObject(JsonElement inElement)
public static BasicDBObject gson2MongoObject(JsonElement inElement, BasicDBObject out)
{
JsonObject in = inElement.getAsJsonObject();
BasicDBObject out = new BasicDBObject();
for (Entry<String, JsonElement> entry : in.entrySet())
{
String key = gson2MongoKey(entry.getKey());
@ -62,6 +61,11 @@ public final class GsonMongoConverter
return out;
}
public static BasicDBObject gson2MongoObject(JsonElement inElement)
{
return gson2MongoObject(inElement, new BasicDBObject());
}
public static BasicDBList gson2MongoArray(JsonElement inElement)
{
JsonArray in = inElement.getAsJsonArray();