One single examineId method

This commit is contained in:
BuildTools 2015-11-27 18:30:53 +01:00 committed by Olof Larsson
parent fac00c00f4
commit 3ab7abc9ca
5 changed files with 37 additions and 146 deletions

View File

@ -578,11 +578,11 @@ public class Coll<E extends Entity<E>> extends CollAbstract<E>
// -------------------------------------------- // // -------------------------------------------- //
@Override @Override
public Modification examineIdFixed(String id, Long remoteMtime) public Modification examineIdFixed(String id, Long remoteMtime, boolean local, boolean remote)
{ {
if (id == null) throw new NullPointerException("id"); if (id == null) throw new NullPointerException("id");
// Meta might be non-existing. But then we create it here. if (!local && !remote) throw new IllegalArgumentException("Must be either remote or local.");
// If it is examined then it will be attached anyways.
Modification current = this.identifiedModifications.get(id); Modification current = this.identifiedModifications.get(id);
// DEBUG // DEBUG
// if (Bukkit.isPrimaryThread()) // if (Bukkit.isPrimaryThread())
@ -592,47 +592,54 @@ public class Coll<E extends Entity<E>> extends CollAbstract<E>
if (current != null && current.hasTopPriority()) return current; if (current != null && current.hasTopPriority()) return current;
E localEntity = this.id2entity.get(id); E localEntity = this.id2entity.get(id);
if (remoteMtime == null) if (remoteMtime == null && remote)
{ {
// TODO: This is slow // TODO: This is slow
remoteMtime = this.getDb().getMtime(this, id); remoteMtime = this.getDb().getMtime(this, id);
} }
boolean existsLocal = (localEntity != null); boolean existsLocal = (localEntity != null);
boolean existsRemote = (remoteMtime != 0); boolean existsRemote = remote ? (remoteMtime != 0) : true;
// So we don't have this anywhere? // So we don't have this anywhere?
if ( ! existsLocal && ! existsRemote) return Modification.UNKNOWN; if ( ! existsLocal && ! existsRemote) return Modification.UNKNOWN;
// If we have it both locally and remotely. // If we have it both locally and remotely...
if (existsLocal && existsRemote) if (existsLocal && existsRemote)
{ {
long lastMtime = localEntity.getLastMtime(); long lastMtime = localEntity.getLastMtime();
// If mtime is not equal remote takes priority, and we assume it is altered. // ...and we are looking for remote changes...
if ( ! remoteMtime.equals(lastMtime)) return Modification.REMOTE_ALTER; if (remote)
// Else we check for a local alter.
if (this.examineHasLocalAlterFixed(id, localEntity)) return Modification.LOCAL_ALTER;
}
// If we just have it locally...
else if (existsLocal)
{
// ...and it was default and thus not saved to the db...
if (localEntity.getLastDefault())
{ {
// ...and also actually altered. // ...and it was modified remotely.
// Then it is a local alter. if ( ! remoteMtime.equals(lastMtime)) return Modification.REMOTE_ALTER;
}
// ...and we are looking for local changes
if (local)
{
// ...and it was modified locally.
if (this.examineHasLocalAlterFixed(id, localEntity)) return Modification.LOCAL_ALTER; if (this.examineHasLocalAlterFixed(id, localEntity)) return Modification.LOCAL_ALTER;
} }
// ...otherwise it was detached remotely. }
else // Otherwise, if we only have it locally...
else if (existsLocal)
{
// ...and we look for local changes, and it was default...
if (local && localEntity.getLastDefault())
{ {
// ...then perhaps it was locally modified
if (this.examineHasLocalAlterFixed(id, localEntity)) return Modification.LOCAL_ALTER;
}
// ...if we look for remote changes, and it wasn't default
else if (remote && !localEntity.getLastDefault())
{
//...then it was remotely detached.
return Modification.REMOTE_DETACH; return Modification.REMOTE_DETACH;
} }
} }
// If we just have it remotely. It was attached there. // If we just have it remotely and look for remote changes. It was attached there.
else if (existsRemote) else if (remote && existsRemote)
{ {
return Modification.REMOTE_ATTACH; return Modification.REMOTE_ATTACH;
} }
@ -641,67 +648,6 @@ public class Coll<E extends Entity<E>> extends CollAbstract<E>
return Modification.NONE; return Modification.NONE;
} }
@Override
public Modification examineIdLocalFixed(final String id)
{
if (id == null) throw new NullPointerException("id");
// A local entity should have a meta.
Modification current = this.identifiedModifications.get(id);
if (current != null && current.hasTopPriority()) return current;
E localEntity = this.id2entity.get(id);
// If not existing, then wtf.
if (localEntity == null) return Modification.UNKNOWN;
// Altered locally.
if (this.examineHasLocalAlterFixed(id, localEntity)) return Modification.LOCAL_ALTER;
// Not altered locally.
return Modification.NONE;
}
@Override
public Modification examineIdRemoteFixed(final String id, Long remoteMtime)
{
if (id == null) throw new NullPointerException("id");
// We will always know beforehand, when local attach and detach is done.
// Because they are performed by calling a method on this coll.
// Meta might be non-existing. But then we create it here.
// If it is examined then it will be attached anyways.
Modification current = this.identifiedModifications.get(id);
if (current != null && current.hasTopPriority()) return current;
if (remoteMtime == null)
{
// TODO: This is slow
remoteMtime = this.getDb().getMtime(this, id);
}
E localEntity = this.id2entity.get(id);
boolean existsLocal = (localEntity != null);
boolean existsRemote = (remoteMtime != 0);
// So we don't have this anywhere?
if ( ! existsLocal && ! existsRemote) return Modification.UNKNOWN;
// If time is different
// then it is remotely altered
if (existsLocal && existsRemote && ! remoteMtime.equals(localEntity.getLastMtime())) return Modification.REMOTE_ALTER;
// If it doesn't exist remotely, and it wasn't because it was default. It was detached remotely.
if (!existsRemote && existsLocal && ! localEntity.getLastDefault()) return Modification.REMOTE_DETACH;
// If it doesn't exist locally, it was attached remotely.
if (!existsLocal) return Modification.REMOTE_ATTACH;
// No modification spotted.
return Modification.NONE;
}
protected boolean examineHasLocalAlterFixed(String id, E entity) protected boolean examineHasLocalAlterFixed(String id, E entity)
{ {
JsonObject lastRaw = entity.getLastRaw(); JsonObject lastRaw = entity.getLastRaw();
@ -732,7 +678,7 @@ public class Coll<E extends Entity<E>> extends CollAbstract<E>
Long remoteMtime = null; Long remoteMtime = null;
if (remoteEntry != null) remoteMtime = remoteEntry.getValue(); if (remoteEntry != null) remoteMtime = remoteEntry.getValue();
Modification actualModification = this.examineIdFixed(id, remoteMtime); Modification actualModification = this.examineIdFixed(id, remoteMtime, true, true);
if (MassiveCoreMConf.get().warnOnLocalAlter && modification == Modification.UNKNOWN_LOG && actualModification.isModified()) if (MassiveCoreMConf.get().warnOnLocalAlter && modification == Modification.UNKNOWN_LOG && actualModification.isModified())
{ {
E entity = this.id2entity.get(id); E entity = this.id2entity.get(id);
@ -836,7 +782,6 @@ public class Coll<E extends Entity<E>> extends CollAbstract<E>
@Override @Override
public void identifyModifications(Modification veto) public void identifyModifications(Modification veto)
{ {
if (MStore.DEBUG_ENABLED) System.out.println(this.getDebugName() + " polling for all changes");
// Get remote id2mtime snapshot // Get remote id2mtime snapshot
Map<String, Long> id2RemoteMtime = this.getDb().getId2mtime(this); Map<String, Long> id2RemoteMtime = this.getDb().getId2mtime(this);
@ -863,14 +808,13 @@ public class Coll<E extends Entity<E>> extends CollAbstract<E>
{ {
if (id == null) throw new NullPointerException("id"); if (id == null) throw new NullPointerException("id");
Modification modification = this.examineIdFixed(id, remoteMtime); Modification modification = this.examineIdFixed(id, remoteMtime, true, true);
this.storeModificationIdentification(id, modification, veto); this.storeModificationIdentification(id, modification, veto);
} }
@Override @Override
public void identifyLocalModifications(Modification veto) public void identifyLocalModifications(Modification veto)
{ {
if (MStore.DEBUG_ENABLED) System.out.println(this.getDebugName() + " polling for local changes");
for (String id : id2entity.keySet()) for (String id : id2entity.keySet())
{ {
this.identifyLocalModificationFixed(id, veto); this.identifyLocalModificationFixed(id, veto);
@ -882,14 +826,13 @@ public class Coll<E extends Entity<E>> extends CollAbstract<E>
{ {
if (id == null) throw new NullPointerException("id"); if (id == null) throw new NullPointerException("id");
Modification modification = this.examineIdLocalFixed(id); Modification modification = this.examineIdFixed(id, null, true, false);
this.storeModificationIdentification(id, modification, veto); this.storeModificationIdentification(id, modification, veto);
} }
@Override @Override
public void identifyRemoteModifications(Modification veto) public void identifyRemoteModifications(Modification veto)
{ {
if (MStore.DEBUG_ENABLED) System.out.println(this.getDebugName() + " polling for remote changes");
// Get remote id2mtime snapshot // Get remote id2mtime snapshot
Map<String, Long> id2RemoteMtime = this.getDb().getId2mtime(this); Map<String, Long> id2RemoteMtime = this.getDb().getId2mtime(this);
@ -917,7 +860,7 @@ public class Coll<E extends Entity<E>> extends CollAbstract<E>
{ {
if (id == null) throw new NullPointerException("id"); if (id == null) throw new NullPointerException("id");
Modification modification = this.examineIdRemoteFixed(id, remoteMtime); Modification modification = this.examineIdFixed(id, remoteMtime, false, true);
this.storeModificationIdentification(id, modification, veto); this.storeModificationIdentification(id, modification, veto);
} }

View File

@ -193,54 +193,10 @@ public abstract class CollAbstract<E extends Entity<E>> implements CollInterface
// Examine // Examine
@Override @Override
public Modification examineId(Object oid) public Modification examineId(Object oid, Long remoteMtime, boolean local, boolean remote)
{ {
if (oid == null) throw new NullPointerException("oid"); if (oid == null) throw new NullPointerException("oid");
return this.examineIdFixed(this.fixIdOrThrow(oid)); return this.examineIdFixed(this.fixIdOrThrow(oid), remoteMtime, local, remote);
}
@Override
public Modification examineId(Object oid, Long remoteMtime)
{
if (oid == null) throw new NullPointerException("oid");
return this.examineIdFixed(this.fixIdOrThrow(oid), remoteMtime);
}
@Override
public Modification examineIdFixed(String id)
{
if (id == null) throw new NullPointerException("id");
return this.examineIdFixed(id, null);
}
// Examine local
@Override
public Modification examineIdLocal(Object oid)
{
if (oid == null) throw new NullPointerException("oid");
return this.examineIdLocalFixed(this.fixIdOrThrow(oid));
}
// Examine remote
@Override
public Modification examineIdRemote(Object oid)
{
if (oid == null) throw new NullPointerException("oid");
return this.examineIdRemoteFixed(this.fixIdOrThrow(oid));
}
@Override
public Modification examineIdRemote(Object oid, Long remoteMtime)
{
if (oid == null) throw new NullPointerException("oid");
return this.examineIdRemoteFixed(this.fixIdOrThrow(oid), remoteMtime);
}
@Override
public Modification examineIdRemoteFixed(String id)
{
if (id == null) throw new NullPointerException("id");
return this.examineIdRemoteFixed(id, null);
} }
// Sync // Sync

View File

@ -178,18 +178,10 @@ public interface CollInterface<E extends Entity<E>> extends Named
// -------------------------------------------- // // -------------------------------------------- //
// oid // oid
public Modification examineId(Object oid); public Modification examineId(Object oid, Long remoteMtime, boolean local, boolean remote);
public Modification examineId(Object oid, Long remoteMtime);
public Modification examineIdLocal(Object oid);
public Modification examineIdRemote(Object oid);
public Modification examineIdRemote(Object oid, Long remoteMtime);
// Fixed id // Fixed id
public Modification examineIdFixed(String id); public Modification examineIdFixed(String id, Long remoteMtime, boolean local, boolean remote);
public Modification examineIdFixed(String id, Long remoteMtime);
public Modification examineIdLocalFixed(String id);
public Modification examineIdRemoteFixed(String id);
public Modification examineIdRemoteFixed(String id, Long remoteMtime);
// Sync // Sync
public Modification syncId(Object oid); public Modification syncId(Object oid);

View File

@ -17,7 +17,7 @@ public class MStore
// CONSTANTS // CONSTANTS
// -------------------------------------------- // // -------------------------------------------- //
public static boolean DEBUG_ENABLED = false; public static final boolean DEBUG_ENABLED = false;
public static final int DEFAULT_LOCAL_POLL_INFREQUENCY = 10; public static final int DEFAULT_LOCAL_POLL_INFREQUENCY = 10;
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -107,7 +107,7 @@ public class PusherCollFlatfile extends Thread implements PusherColl
// Most registered modifications here will actually be something done locally. // Most registered modifications here will actually be something done locally.
// So most of the time we should just ignore this. // So most of the time we should just ignore this.
Modification mod = this.coll.examineIdFixed(id, mtime); Modification mod = this.coll.examineIdFixed(id, mtime, true, true);
//System.out.println("old: " + coll.getMetaCreative(id).getMtime() + "new: " + mtime); //System.out.println("old: " + coll.getMetaCreative(id).getMtime() + "new: " + mtime);
//System.out.println(String.format("Coll %s found %s on %s", this.coll.getBasename(), mod, id)); //System.out.println(String.format("Coll %s found %s on %s", this.coll.getBasename(), mod, id));