CollAbstract & pass id directly.
This commit is contained in:
parent
7751340c72
commit
2a824cd3ba
@ -57,8 +57,8 @@ public class CmdMassiveCoreStoreStats extends MassiveCommand
|
|||||||
{
|
{
|
||||||
String name = entry.getKey();
|
String name = entry.getKey();
|
||||||
Coll<?> coll = entry.getValue();
|
Coll<?> coll = entry.getValue();
|
||||||
long in = coll.getSyncCount(Coll.TOTAL, true);
|
long in = coll.getSyncCountFixed(Coll.TOTAL, true);
|
||||||
long out = coll.getSyncCount(Coll.TOTAL, false);
|
long out = coll.getSyncCountFixed(Coll.TOTAL, false);
|
||||||
|
|
||||||
msg("<v>%s <a>| <v>%d <a>| <v>%d", name, in, out);
|
msg("<v>%s <a>| <v>%d <a>| <v>%d", name, in, out);
|
||||||
}
|
}
|
||||||
|
@ -25,7 +25,7 @@ import com.massivecraft.massivecore.xlib.gson.Gson;
|
|||||||
import com.massivecraft.massivecore.xlib.gson.JsonElement;
|
import com.massivecraft.massivecore.xlib.gson.JsonElement;
|
||||||
import com.massivecraft.massivecore.xlib.gson.JsonObject;
|
import com.massivecraft.massivecore.xlib.gson.JsonObject;
|
||||||
|
|
||||||
public class Coll<E> implements CollInterface<E>
|
public class Coll<E> extends CollAbstract<E>
|
||||||
{
|
{
|
||||||
// -------------------------------------------- //
|
// -------------------------------------------- //
|
||||||
// GLOBAL REGISTRY
|
// GLOBAL REGISTRY
|
||||||
@ -104,19 +104,13 @@ public class Coll<E> implements CollInterface<E>
|
|||||||
protected Map<E, String> entity2id;
|
protected Map<E, String> entity2id;
|
||||||
|
|
||||||
@Override public Map<String, E> getId2entity() { return Collections.unmodifiableMap(this.id2entity); }
|
@Override public Map<String, E> getId2entity() { return Collections.unmodifiableMap(this.id2entity); }
|
||||||
@Override
|
|
||||||
public E get(Object oid)
|
|
||||||
{
|
|
||||||
return this.get(oid, this.isCreative());
|
|
||||||
}
|
|
||||||
@Override
|
@Override
|
||||||
public E get(Object oid, boolean creative)
|
public E getFixed(String id, boolean creative)
|
||||||
{
|
{
|
||||||
return this.get(oid, creative, true);
|
return this.getFixed(id, creative, true);
|
||||||
}
|
}
|
||||||
protected E get(Object oid, boolean creative, boolean noteModification)
|
protected E getFixed(String id, boolean creative, boolean noteModification)
|
||||||
{
|
{
|
||||||
String id = this.fixId(oid);
|
|
||||||
if (id == null) return null;
|
if (id == null) return null;
|
||||||
E ret = this.id2entity.get(id);
|
E ret = this.id2entity.get(id);
|
||||||
if (ret != null) return ret;
|
if (ret != null) return ret;
|
||||||
@ -127,9 +121,8 @@ public class Coll<E> implements CollInterface<E>
|
|||||||
@Override public Collection<String> getIds() { return Collections.unmodifiableCollection(this.id2entity.keySet()); }
|
@Override public Collection<String> getIds() { return Collections.unmodifiableCollection(this.id2entity.keySet()); }
|
||||||
@Override public Collection<String> getIdsRemote() { return this.getDb().getIds(this); }
|
@Override public Collection<String> getIdsRemote() { return this.getDb().getIds(this); }
|
||||||
@Override
|
@Override
|
||||||
public boolean containsId(Object oid)
|
public boolean containsIdFixed(String id)
|
||||||
{
|
{
|
||||||
String id = this.fixId(oid);
|
|
||||||
if (id == null) return false;
|
if (id == null) return false;
|
||||||
return this.id2entity.containsKey(id);
|
return this.id2entity.containsKey(id);
|
||||||
}
|
}
|
||||||
@ -153,14 +146,8 @@ public class Coll<E> implements CollInterface<E>
|
|||||||
if (oid == null) return null;
|
if (oid == null) return null;
|
||||||
|
|
||||||
String ret = null;
|
String ret = null;
|
||||||
if (oid instanceof String)
|
if (oid instanceof String) ret = (String) oid;
|
||||||
{
|
else if (oid.getClass() == this.getEntityClass()) ret = this.entity2id.get(oid);
|
||||||
ret = (String)oid;
|
|
||||||
}
|
|
||||||
else if (oid.getClass() == this.entityClass)
|
|
||||||
{
|
|
||||||
ret = this.entity2id.get(oid);
|
|
||||||
}
|
|
||||||
if (ret == null) return null;
|
if (ret == null) return null;
|
||||||
|
|
||||||
return this.isLowercasing() ? ret.toLowerCase() : ret;
|
return this.isLowercasing() ? ret.toLowerCase() : ret;
|
||||||
@ -233,12 +220,14 @@ public class Coll<E> implements CollInterface<E>
|
|||||||
|
|
||||||
// This simply creates and returns a new instance
|
// This simply creates and returns a new instance
|
||||||
// It does not detach/attach or anything. Just creates a new instance.
|
// It does not detach/attach or anything. Just creates a new instance.
|
||||||
|
// TODO: Would it ever make sense for this to fail?
|
||||||
|
// Should we just throw an exception immediately if it fails?
|
||||||
@Override
|
@Override
|
||||||
public E createNewInstance()
|
public E createNewInstance()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return this.entityClass.newInstance();
|
return this.getEntityClass().newInstance();
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
@ -246,13 +235,6 @@ public class Coll<E> implements CollInterface<E>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create new instance with automatic id
|
|
||||||
@Override
|
|
||||||
public E create()
|
|
||||||
{
|
|
||||||
return this.create(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create new instance with the requested id
|
// Create new instance with the requested id
|
||||||
@Override
|
@Override
|
||||||
public synchronized E create(Object oid)
|
public synchronized E create(Object oid)
|
||||||
@ -272,13 +254,7 @@ public class Coll<E> implements CollInterface<E>
|
|||||||
// -------------------------------------------- //
|
// -------------------------------------------- //
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String attach(E entity)
|
public String attach(E entity, Object oid)
|
||||||
{
|
|
||||||
return this.attach(entity, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public synchronized String attach(E entity, Object oid)
|
|
||||||
{
|
{
|
||||||
return this.attach(entity, oid, true);
|
return this.attach(entity, oid, true);
|
||||||
}
|
}
|
||||||
@ -288,9 +264,10 @@ public class Coll<E> implements CollInterface<E>
|
|||||||
{
|
{
|
||||||
// Check entity
|
// Check entity
|
||||||
if (entity == null) return null;
|
if (entity == null) return null;
|
||||||
String id = this.getId(entity);
|
String previousEntityId = this.getId(entity);
|
||||||
if (id != null) return id;
|
if (previousEntityId != null) return previousEntityId;
|
||||||
|
|
||||||
|
String id;
|
||||||
// Check/Fix id
|
// Check/Fix id
|
||||||
if (oid == null)
|
if (oid == null)
|
||||||
{
|
{
|
||||||
@ -344,24 +321,23 @@ public class Coll<E> implements CollInterface<E>
|
|||||||
return e;
|
return e;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.detach(e, id);
|
this.detachFixed(e, id);
|
||||||
return e;
|
return e;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public E detachId(Object oid)
|
public E detachIdFixed(String id)
|
||||||
{
|
{
|
||||||
if (oid == null) throw new NullPointerException("oid");
|
if (id == null) throw new NullPointerException("id");
|
||||||
|
|
||||||
String id = this.fixId(oid);
|
|
||||||
E e = this.get(id, false);
|
E e = this.get(id, false);
|
||||||
if (e == null) return null;
|
if (e == null) return null;
|
||||||
|
|
||||||
this.detach(e, id);
|
this.detachFixed(e, id);
|
||||||
return e;
|
return e;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void detach(E entity, String id)
|
private void detachFixed(E entity, String id)
|
||||||
{
|
{
|
||||||
if (entity == null) throw new NullPointerException("entity");
|
if (entity == null) throw new NullPointerException("entity");
|
||||||
if (id == null) throw new NullPointerException("id");
|
if (id == null) throw new NullPointerException("id");
|
||||||
@ -370,7 +346,7 @@ public class Coll<E> implements CollInterface<E>
|
|||||||
this.preDetach(entity, id);
|
this.preDetach(entity, id);
|
||||||
|
|
||||||
// Remove @ local
|
// Remove @ local
|
||||||
this.removeAtLocal(id);
|
this.removeAtLocalFixed(id);
|
||||||
|
|
||||||
// Identify Modification
|
// Identify Modification
|
||||||
this.identifiedModifications.put(id, Modification.LOCAL_DETACH);
|
this.identifiedModifications.put(id, Modification.LOCAL_DETACH);
|
||||||
@ -421,10 +397,9 @@ public class Coll<E> implements CollInterface<E>
|
|||||||
|
|
||||||
protected Map<String, Modification> identifiedModifications;
|
protected Map<String, Modification> identifiedModifications;
|
||||||
|
|
||||||
protected synchronized void removeIdentifiedModification(Object oid)
|
protected synchronized void removeIdentifiedModificationFixed(String id)
|
||||||
{
|
{
|
||||||
if (oid == null) throw new NullPointerException("oid");
|
if (id == null) throw new NullPointerException("id");
|
||||||
String id = this.fixId(oid);
|
|
||||||
this.identifiedModifications.remove(id);
|
this.identifiedModifications.remove(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -437,11 +412,10 @@ public class Coll<E> implements CollInterface<E>
|
|||||||
protected Map<String, JsonElement> lastRaw;
|
protected Map<String, JsonElement> lastRaw;
|
||||||
protected Set<String> lastDefault;
|
protected Set<String> lastDefault;
|
||||||
|
|
||||||
protected synchronized void clearSynclog(Object oid)
|
protected synchronized void clearSynclogFixed(String id)
|
||||||
{
|
{
|
||||||
if (oid == null) throw new NullPointerException("oid");
|
if (id == null) throw new NullPointerException("id");
|
||||||
|
|
||||||
String id = this.fixId(oid);
|
|
||||||
this.lastMtime.remove(id);
|
this.lastMtime.remove(id);
|
||||||
this.lastRaw.remove(id);
|
this.lastRaw.remove(id);
|
||||||
this.lastDefault.remove(id);
|
this.lastDefault.remove(id);
|
||||||
@ -451,21 +425,24 @@ public class Coll<E> implements CollInterface<E>
|
|||||||
private Map<String, Long> id2out = new TreeMap<String, Long>();
|
private Map<String, Long> id2out = new TreeMap<String, Long>();
|
||||||
private Map<String, Long> id2in = new TreeMap<String, Long>();
|
private Map<String, Long> id2in = new TreeMap<String, Long>();
|
||||||
|
|
||||||
|
@Override
|
||||||
public Map<String, Long> getSyncMap(boolean in)
|
public Map<String, Long> getSyncMap(boolean in)
|
||||||
{
|
{
|
||||||
return in ? this.id2in : this.id2out;
|
return in ? this.id2in : this.id2out;
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getSyncCount(String id, boolean in)
|
@Override
|
||||||
|
public long getSyncCountFixed(String id, boolean in)
|
||||||
{
|
{
|
||||||
Long count = this.getSyncMap(in).get(id);
|
Long count = this.getSyncMap(in).get(id);
|
||||||
if (count == null) return 0;
|
if (count == null) return 0;
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addSyncCount(String id, boolean in)
|
@Override
|
||||||
|
public void addSyncCountFixed(String id, boolean in)
|
||||||
{
|
{
|
||||||
long count = this.getSyncCount(id, in);
|
long count = this.getSyncCountFixed(id, in);
|
||||||
count++;
|
count++;
|
||||||
this.getSyncMap(in).put(id, count);
|
this.getSyncMap(in).put(id, count);
|
||||||
}
|
}
|
||||||
@ -476,14 +453,12 @@ public class Coll<E> implements CollInterface<E>
|
|||||||
|
|
||||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||||
@Override
|
@Override
|
||||||
public synchronized E removeAtLocal(Object oid)
|
public synchronized E removeAtLocalFixed(String id)
|
||||||
{
|
{
|
||||||
// Fix Id
|
if (id == null) throw new NullPointerException("id");
|
||||||
if (oid == null) throw new NullPointerException("oid");
|
|
||||||
String id = this.fixId(oid);
|
|
||||||
|
|
||||||
this.removeIdentifiedModification(id);
|
this.removeIdentifiedModificationFixed(id);
|
||||||
this.clearSynclog(id);
|
this.clearSynclogFixed(id);
|
||||||
|
|
||||||
E entity = this.id2entity.remove(id);
|
E entity = this.id2entity.remove(id);
|
||||||
if (entity == null) return null;
|
if (entity == null) return null;
|
||||||
@ -501,27 +476,23 @@ public class Coll<E> implements CollInterface<E>
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void removeAtRemote(Object oid)
|
public synchronized void removeAtRemoteFixed(String id)
|
||||||
{
|
{
|
||||||
// Fix Id
|
if (id == null) throw new NullPointerException("id");
|
||||||
if (oid == null) throw new NullPointerException("oid");
|
|
||||||
String id = this.fixId(oid);
|
|
||||||
|
|
||||||
this.removeIdentifiedModification(id);
|
this.removeIdentifiedModificationFixed(id);
|
||||||
this.clearSynclog(id);
|
this.clearSynclogFixed(id);
|
||||||
|
|
||||||
this.getDb().delete(this, id);
|
this.getDb().delete(this, id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void saveToRemote(Object oid)
|
public synchronized void saveToRemoteFixed(String id)
|
||||||
{
|
{
|
||||||
// Fix Id
|
if (id == null) throw new NullPointerException("id");
|
||||||
if (oid == null) throw new NullPointerException("oid");
|
|
||||||
String id = this.fixId(oid);
|
|
||||||
|
|
||||||
this.removeIdentifiedModification(id);
|
this.removeIdentifiedModificationFixed(id);
|
||||||
this.clearSynclog(id);
|
this.clearSynclogFixed(id);
|
||||||
|
|
||||||
E entity = this.id2entity.get(id);
|
E entity = this.id2entity.get(id);
|
||||||
if (entity == null) return;
|
if (entity == null) return;
|
||||||
@ -544,13 +515,11 @@ public class Coll<E> implements CollInterface<E>
|
|||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@Override
|
@Override
|
||||||
public synchronized void loadFromRemote(Object oid, Entry<JsonElement, Long> remoteEntry)
|
public synchronized void loadFromRemoteFixed(String id, Entry<JsonElement, Long> remoteEntry)
|
||||||
{
|
{
|
||||||
// Fix Id
|
if (id == null) throw new NullPointerException("id");
|
||||||
if (oid == null) throw new NullPointerException("oid");
|
|
||||||
String id = this.fixId(oid);
|
|
||||||
|
|
||||||
this.removeIdentifiedModification(id);
|
this.removeIdentifiedModificationFixed(id);
|
||||||
|
|
||||||
if (remoteEntry == null)
|
if (remoteEntry == null)
|
||||||
{
|
{
|
||||||
@ -636,22 +605,9 @@ public class Coll<E> implements CollInterface<E>
|
|||||||
// -------------------------------------------- //
|
// -------------------------------------------- //
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Modification examineId(Object oid)
|
public Modification examineIdFixed(String id, Long remoteMtime)
|
||||||
{
|
{
|
||||||
// Fix Id
|
if (id == null) throw new NullPointerException("id");
|
||||||
if (oid == null) throw new NullPointerException("oid");
|
|
||||||
//String id = this.fixId(oid); // Was done twice
|
|
||||||
|
|
||||||
return this.examineId(oid, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Modification examineId(Object oid, Long remoteMtime)
|
|
||||||
{
|
|
||||||
// Fix Id
|
|
||||||
if (oid == null) throw new NullPointerException("oid");
|
|
||||||
String id = this.fixId(oid);
|
|
||||||
|
|
||||||
// Local Attach and Detach has the top priority.
|
// Local Attach and Detach has the top priority.
|
||||||
// Otherwise newly attached entities would be removed thinking it was a remote detach.
|
// Otherwise newly attached entities would be removed thinking it was a remote detach.
|
||||||
// Otherwise newly detached entities would be loaded thinking it was a remote attach.
|
// Otherwise newly detached entities would be loaded thinking it was a remote attach.
|
||||||
@ -680,13 +636,13 @@ public class Coll<E> implements CollInterface<E>
|
|||||||
Long lastMtime = this.lastMtime.get(id);
|
Long lastMtime = this.lastMtime.get(id);
|
||||||
if (remoteMtime.equals(lastMtime) == false) return Modification.REMOTE_ALTER;
|
if (remoteMtime.equals(lastMtime) == false) return Modification.REMOTE_ALTER;
|
||||||
|
|
||||||
if (this.examineHasLocalAlter(id, localEntity)) return Modification.LOCAL_ALTER;
|
if (this.examineHasLocalAlterFixed(id, localEntity)) return Modification.LOCAL_ALTER;
|
||||||
}
|
}
|
||||||
else if (existsLocal)
|
else if (existsLocal)
|
||||||
{
|
{
|
||||||
if (this.lastDefault.contains(id))
|
if (this.lastDefault.contains(id))
|
||||||
{
|
{
|
||||||
if (this.examineHasLocalAlter(id, localEntity)) return Modification.LOCAL_ALTER;
|
if (this.examineHasLocalAlterFixed(id, localEntity)) return Modification.LOCAL_ALTER;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -701,7 +657,7 @@ public class Coll<E> implements CollInterface<E>
|
|||||||
return Modification.NONE;
|
return Modification.NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected boolean examineHasLocalAlter(String id, E entity)
|
protected boolean examineHasLocalAlterFixed(String id, E entity)
|
||||||
{
|
{
|
||||||
JsonElement lastRaw = this.lastRaw.get(id);
|
JsonElement lastRaw = this.lastRaw.get(id);
|
||||||
JsonElement currentRaw = null;
|
JsonElement currentRaw = null;
|
||||||
@ -721,32 +677,17 @@ public class Coll<E> implements CollInterface<E>
|
|||||||
|
|
||||||
return !MStore.equal(lastRaw, currentRaw);
|
return !MStore.equal(lastRaw, currentRaw);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Modification syncId(Object oid)
|
public Modification syncIdFixed(String id, Modification modification, Entry<JsonElement, Long> remoteEntry)
|
||||||
{
|
{
|
||||||
return this.syncId(oid, null);
|
if (id == null) throw new NullPointerException("id");
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Modification syncId(Object oid, Modification modification)
|
|
||||||
{
|
|
||||||
return this.syncId(oid, modification, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Modification syncId(Object oid, Modification modification, Entry<JsonElement, Long> remoteEntry)
|
|
||||||
{
|
|
||||||
// Fix Id
|
|
||||||
if (oid == null) throw new NullPointerException("oid");
|
|
||||||
String id = this.fixId(oid);
|
|
||||||
|
|
||||||
if (modification == null || modification == Modification.UNKNOWN)
|
if (modification == null || modification == Modification.UNKNOWN)
|
||||||
{
|
{
|
||||||
Long remoteMtime = null;
|
Long remoteMtime = null;
|
||||||
if (remoteEntry != null) remoteMtime = remoteEntry.getValue();
|
if (remoteEntry != null) remoteMtime = remoteEntry.getValue();
|
||||||
|
|
||||||
modification = this.examineId(id, remoteMtime);
|
modification = this.examineIdFixed(id, remoteMtime);
|
||||||
}
|
}
|
||||||
|
|
||||||
// DEBUG
|
// DEBUG
|
||||||
@ -756,40 +697,40 @@ public class Coll<E> implements CollInterface<E>
|
|||||||
{
|
{
|
||||||
case LOCAL_ALTER:
|
case LOCAL_ALTER:
|
||||||
case LOCAL_ATTACH:
|
case LOCAL_ATTACH:
|
||||||
this.saveToRemote(id);
|
this.saveToRemoteFixed(id);
|
||||||
if (this.inited())
|
if (this.inited())
|
||||||
{
|
{
|
||||||
this.addSyncCount(TOTAL, false);
|
this.addSyncCountFixed(TOTAL, false);
|
||||||
this.addSyncCount(id, false);
|
this.addSyncCountFixed(id, false);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case LOCAL_DETACH:
|
case LOCAL_DETACH:
|
||||||
this.removeAtRemote(id);
|
this.removeAtRemoteFixed(id);
|
||||||
if (this.inited())
|
if (this.inited())
|
||||||
{
|
{
|
||||||
this.addSyncCount(TOTAL, false);
|
this.addSyncCountFixed(TOTAL, false);
|
||||||
this.addSyncCount(id, false);
|
this.addSyncCountFixed(id, false);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case REMOTE_ALTER:
|
case REMOTE_ALTER:
|
||||||
case REMOTE_ATTACH:
|
case REMOTE_ATTACH:
|
||||||
this.loadFromRemote(id, remoteEntry);
|
this.loadFromRemoteFixed(id, remoteEntry);
|
||||||
if (this.inited())
|
if (this.inited())
|
||||||
{
|
{
|
||||||
this.addSyncCount(TOTAL, true);
|
this.addSyncCountFixed(TOTAL, true);
|
||||||
this.addSyncCount(id, true);
|
this.addSyncCountFixed(id, true);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case REMOTE_DETACH:
|
case REMOTE_DETACH:
|
||||||
this.removeAtLocal(id);
|
this.removeAtLocalFixed(id);
|
||||||
if (this.inited())
|
if (this.inited())
|
||||||
{
|
{
|
||||||
this.addSyncCount(TOTAL, true);
|
this.addSyncCountFixed(TOTAL, true);
|
||||||
this.addSyncCount(id, true);
|
this.addSyncCountFixed(id, true);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
this.removeIdentifiedModification(id);
|
this.removeIdentifiedModificationFixed(id);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -813,7 +754,7 @@ public class Coll<E> implements CollInterface<E>
|
|||||||
Long remoteMtime = id2RemoteMtime.get(id);
|
Long remoteMtime = id2RemoteMtime.get(id);
|
||||||
if (remoteMtime == null) remoteMtime = 0L;
|
if (remoteMtime == null) remoteMtime = 0L;
|
||||||
|
|
||||||
Modification modification = this.examineId(id, remoteMtime);
|
Modification modification = this.examineIdFixed(id, remoteMtime);
|
||||||
if (modification.isModified())
|
if (modification.isModified())
|
||||||
{
|
{
|
||||||
this.identifiedModifications.put(id, modification);
|
this.identifiedModifications.put(id, modification);
|
||||||
@ -832,7 +773,7 @@ public class Coll<E> implements CollInterface<E>
|
|||||||
{
|
{
|
||||||
modification = null;
|
modification = null;
|
||||||
}
|
}
|
||||||
this.syncId(id, modification);
|
this.syncIdFixed(id, modification);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -910,10 +851,9 @@ public class Coll<E> implements CollInterface<E>
|
|||||||
this.lastRaw = new ConcurrentHashMap<String, JsonElement>();
|
this.lastRaw = new ConcurrentHashMap<String, JsonElement>();
|
||||||
this.lastDefault = Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>());
|
this.lastDefault = Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>());
|
||||||
|
|
||||||
final Coll<E> me = this;
|
|
||||||
this.tickTask = new Runnable()
|
this.tickTask = new Runnable()
|
||||||
{
|
{
|
||||||
@Override public void run() { me.onTick(); }
|
@Override public void run() { Coll.this.onTick(); }
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -930,7 +870,7 @@ public class Coll<E> implements CollInterface<E>
|
|||||||
@Override
|
@Override
|
||||||
public void init()
|
public void init()
|
||||||
{
|
{
|
||||||
if (this.inited()) return;
|
if (this.inited()) return; // TODO: Would throwing an exception make more sense?
|
||||||
|
|
||||||
this.initLoadAllFromRemote();
|
this.initLoadAllFromRemote();
|
||||||
// this.syncAll();
|
// this.syncAll();
|
||||||
@ -941,7 +881,7 @@ public class Coll<E> implements CollInterface<E>
|
|||||||
@Override
|
@Override
|
||||||
public void deinit()
|
public void deinit()
|
||||||
{
|
{
|
||||||
if ( ! this.inited()) return;
|
if ( ! this.inited()) return; // TODO: Would throwing an exception make more sense?
|
||||||
|
|
||||||
// TODO: Save outwards only? We may want to avoid loads at this stage...
|
// TODO: Save outwards only? We may want to avoid loads at this stage...
|
||||||
this.syncAll();
|
this.syncAll();
|
||||||
@ -954,4 +894,5 @@ public class Coll<E> implements CollInterface<E>
|
|||||||
{
|
{
|
||||||
return name2instance.containsKey(this.getName());
|
return name2instance.containsKey(this.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
198
src/com/massivecraft/massivecore/store/CollAbstract.java
Normal file
198
src/com/massivecraft/massivecore/store/CollAbstract.java
Normal file
@ -0,0 +1,198 @@
|
|||||||
|
package com.massivecraft.massivecore.store;
|
||||||
|
|
||||||
|
import java.util.Map.Entry;
|
||||||
|
|
||||||
|
import com.massivecraft.massivecore.xlib.gson.JsonElement;
|
||||||
|
|
||||||
|
|
||||||
|
public abstract class CollAbstract<E> implements CollInterface<E>
|
||||||
|
{
|
||||||
|
// -------------------------------------------- //
|
||||||
|
// WHAT DO WE HANDLE?
|
||||||
|
// -------------------------------------------- //
|
||||||
|
// This is all placed in the implementation.
|
||||||
|
|
||||||
|
// -------------------------------------------- //
|
||||||
|
// SUPPORTING SYSTEM
|
||||||
|
// -------------------------------------------- //
|
||||||
|
// This is all placed in the implementation.
|
||||||
|
|
||||||
|
// -------------------------------------------- //
|
||||||
|
// STORAGE
|
||||||
|
// -------------------------------------------- //
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public E get(Object oid)
|
||||||
|
{
|
||||||
|
return this.getFixed(this.fixId(oid));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public E get(Object oid, boolean creative)
|
||||||
|
{
|
||||||
|
return this.getFixed(this.fixId(oid), creative);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public E getFixed(String id)
|
||||||
|
{
|
||||||
|
return this.getFixed(id, this.isCreative());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean containsId(Object oid)
|
||||||
|
{
|
||||||
|
return this.containsIdFixed(this.fixId(oid));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String fixIdOrThrow(Object oid) throws IllegalArgumentException
|
||||||
|
{
|
||||||
|
String ret = this.fixId(oid);
|
||||||
|
if (ret == null) throw new IllegalArgumentException(String.valueOf(oid) + " is not a valid id.");
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------- //
|
||||||
|
// BEHAVIOR
|
||||||
|
// -------------------------------------------- //
|
||||||
|
// This is all placed in the implementation.
|
||||||
|
|
||||||
|
// -------------------------------------------- //
|
||||||
|
// COPY AND CREATE
|
||||||
|
// -------------------------------------------- //
|
||||||
|
|
||||||
|
// Create new instance with automatic id
|
||||||
|
@Override
|
||||||
|
public E create()
|
||||||
|
{
|
||||||
|
return this.create(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------- //
|
||||||
|
// ATTACH AND DETACH
|
||||||
|
// -------------------------------------------- //
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String attach(E entity)
|
||||||
|
{
|
||||||
|
return this.attach(entity, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public E detachId(Object oid)
|
||||||
|
{
|
||||||
|
if (oid == null) throw new NullPointerException("oid");
|
||||||
|
return this.detachIdFixed(this.fixIdOrThrow(oid));
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------- //
|
||||||
|
// SYNC LOG
|
||||||
|
// -------------------------------------------- //
|
||||||
|
// This is all in the implementation.
|
||||||
|
|
||||||
|
// -------------------------------------------- //
|
||||||
|
// SYNC LOWLEVEL IO ACTIONS
|
||||||
|
// -------------------------------------------- //
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public E removeAtLocal(Object oid)
|
||||||
|
{
|
||||||
|
if (oid == null) throw new NullPointerException("oid");
|
||||||
|
return this.removeAtLocalFixed(this.fixIdOrThrow(oid));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void removeAtRemote(Object oid)
|
||||||
|
{
|
||||||
|
if (oid == null) throw new NullPointerException("oid");
|
||||||
|
this.removeAtRemoteFixed(this.fixIdOrThrow(oid));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void saveToRemote(Object oid)
|
||||||
|
{
|
||||||
|
if (oid == null) throw new NullPointerException("oid");
|
||||||
|
this.saveToRemoteFixed(this.fixIdOrThrow(oid));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void loadFromRemote(Object oid, Entry<JsonElement, Long> remoteEntry)
|
||||||
|
{
|
||||||
|
if (oid == null) throw new NullPointerException("oid");
|
||||||
|
this.loadFromRemoteFixed(this.fixIdOrThrow(oid), remoteEntry);
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------- //
|
||||||
|
// SYNC EXAMINE AND DO
|
||||||
|
// -------------------------------------------- //
|
||||||
|
|
||||||
|
// Examine
|
||||||
|
@Override
|
||||||
|
public Modification examineId(Object oid)
|
||||||
|
{
|
||||||
|
if (oid == null) throw new NullPointerException("oid");
|
||||||
|
return this.examineIdFixed(this.fixIdOrThrow(oid));
|
||||||
|
}
|
||||||
|
|
||||||
|
@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)
|
||||||
|
{
|
||||||
|
// Null check done later.
|
||||||
|
return this.examineIdFixed(id, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sync
|
||||||
|
@Override
|
||||||
|
public Modification syncId(Object oid)
|
||||||
|
{
|
||||||
|
if (oid == null) throw new NullPointerException("oid");
|
||||||
|
return this.syncIdFixed(this.fixIdOrThrow(oid));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Modification syncId(Object oid, Modification modificationState)
|
||||||
|
{
|
||||||
|
if (oid == null) throw new NullPointerException("oid");
|
||||||
|
return this.syncIdFixed(this.fixIdOrThrow(oid), modificationState);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Modification syncId(Object oid, Modification modificationState, Entry<JsonElement, Long> remoteEntry)
|
||||||
|
{
|
||||||
|
if (oid == null) throw new NullPointerException("oid");
|
||||||
|
return this.syncIdFixed(this.fixIdOrThrow(oid), modificationState, remoteEntry);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Modification syncIdFixed(String id)
|
||||||
|
{
|
||||||
|
// Null check done later.
|
||||||
|
return this.syncIdFixed(id, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Modification syncIdFixed(String id, Modification modification)
|
||||||
|
{
|
||||||
|
// Null check done later.
|
||||||
|
return this.syncIdFixed(id, modification, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------- //
|
||||||
|
// SYNC RUNNABLES / SCHEDULING
|
||||||
|
// -------------------------------------------- //
|
||||||
|
// This is all in the implementation
|
||||||
|
|
||||||
|
// -------------------------------------------- //
|
||||||
|
// CONSTRUCT
|
||||||
|
// -------------------------------------------- //
|
||||||
|
// This is all in the implementation;
|
||||||
|
|
||||||
|
}
|
@ -35,9 +35,12 @@ public interface CollInterface<E>
|
|||||||
public Map<String, E> getId2entity();
|
public Map<String, E> getId2entity();
|
||||||
public E get(Object oid);
|
public E get(Object oid);
|
||||||
public E get(Object oid, boolean creative);
|
public E get(Object oid, boolean creative);
|
||||||
|
public E getFixed(String id);
|
||||||
|
public E getFixed(String id, boolean creative);
|
||||||
public Collection<String> getIds();
|
public Collection<String> getIds();
|
||||||
public Collection<String> getIdsRemote();
|
public Collection<String> getIdsRemote();
|
||||||
public boolean containsId(Object oid);
|
public boolean containsId(Object oid);
|
||||||
|
public boolean containsIdFixed(String id);
|
||||||
|
|
||||||
public Map<E, String> getEntity2id();
|
public Map<E, String> getEntity2id();
|
||||||
public String getId(Object entity);
|
public String getId(Object entity);
|
||||||
@ -49,6 +52,7 @@ public interface CollInterface<E>
|
|||||||
public List<E> getAll(Predictate<? super E> where, Comparator<? super E> orderby, Integer limit, Integer offset);
|
public List<E> getAll(Predictate<? super E> where, Comparator<? super E> orderby, Integer limit, Integer offset);
|
||||||
|
|
||||||
public String fixId(Object oid);
|
public String fixId(Object oid);
|
||||||
|
public String fixIdOrThrow(Object oid) throws IllegalArgumentException;
|
||||||
|
|
||||||
// -------------------------------------------- //
|
// -------------------------------------------- //
|
||||||
// BEHAVIOR
|
// BEHAVIOR
|
||||||
@ -88,6 +92,7 @@ public interface CollInterface<E>
|
|||||||
|
|
||||||
public E detachEntity(Object entity);
|
public E detachEntity(Object entity);
|
||||||
public E detachId(Object oid);
|
public E detachId(Object oid);
|
||||||
|
public E detachIdFixed(String id);
|
||||||
|
|
||||||
public void preAttach(E entity, String id);
|
public void preAttach(E entity, String id);
|
||||||
public void postAttach(E entity, String id);
|
public void postAttach(E entity, String id);
|
||||||
@ -118,28 +123,47 @@ public interface CollInterface<E>
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
public Map<String, Long> getSyncMap(boolean in);
|
public Map<String, Long> getSyncMap(boolean in);
|
||||||
public long getSyncCount(String name, boolean in);
|
public long getSyncCountFixed(String name, boolean in);
|
||||||
public void addSyncCount(String name, boolean in);
|
public void addSyncCountFixed(String name, boolean in);
|
||||||
|
|
||||||
// -------------------------------------------- //
|
// -------------------------------------------- //
|
||||||
// SYNC LOWLEVEL IO ACTIONS
|
// SYNC LOWLEVEL IO ACTIONS
|
||||||
// -------------------------------------------- //
|
// -------------------------------------------- //
|
||||||
|
|
||||||
|
// oid
|
||||||
public E removeAtLocal(Object oid);
|
public E removeAtLocal(Object oid);
|
||||||
public void removeAtRemote(Object oid);
|
public void removeAtRemote(Object oid);
|
||||||
public void saveToRemote(Object oid);
|
public void saveToRemote(Object oid);
|
||||||
public void loadFromRemote(Object oid, Entry<JsonElement, Long> remoteEntry);
|
public void loadFromRemote(Object oid, Entry<JsonElement, Long> remoteEntry);
|
||||||
|
|
||||||
|
// Fixed id
|
||||||
|
public E removeAtLocalFixed(String id);
|
||||||
|
public void removeAtRemoteFixed(String id);
|
||||||
|
public void saveToRemoteFixed(String id);
|
||||||
|
public void loadFromRemoteFixed(String id, Entry<JsonElement, Long> remoteEntry);
|
||||||
|
|
||||||
// -------------------------------------------- //
|
// -------------------------------------------- //
|
||||||
// SYNC EXAMINE AND DO
|
// SYNC EXAMINE AND DO
|
||||||
// -------------------------------------------- //
|
// -------------------------------------------- //
|
||||||
|
|
||||||
|
// oid
|
||||||
public Modification examineId(Object oid);
|
public Modification examineId(Object oid);
|
||||||
public Modification examineId(Object oid, Long remoteMtime);
|
public Modification examineId(Object oid, Long remoteMtime);
|
||||||
|
|
||||||
|
// Fixed id
|
||||||
|
public Modification examineIdFixed(String id);
|
||||||
|
public Modification examineIdFixed(String id, Long remoteMtime);
|
||||||
|
|
||||||
|
// oid
|
||||||
public Modification syncId(Object oid);
|
public Modification syncId(Object oid);
|
||||||
public Modification syncId(Object oid, Modification modificationState);
|
public Modification syncId(Object oid, Modification modificationState);
|
||||||
public Modification syncId(Object oid, Modification modificationState, Entry<JsonElement, Long> remoteEntry);
|
public Modification syncId(Object oid, Modification modificationState, Entry<JsonElement, Long> remoteEntry);
|
||||||
|
|
||||||
|
// fixed id
|
||||||
|
public Modification syncIdFixed(String id);
|
||||||
|
public Modification syncIdFixed(String id, Modification modificationState);
|
||||||
|
public Modification syncIdFixed(String id, Modification modificationState, Entry<JsonElement, Long> remoteEntry);
|
||||||
|
|
||||||
public void syncIdentified(boolean safe);
|
public void syncIdentified(boolean safe);
|
||||||
public void syncAll();
|
public void syncAll();
|
||||||
public void identifyModifications();
|
public void identifyModifications();
|
||||||
@ -161,5 +185,4 @@ public interface CollInterface<E>
|
|||||||
public void deinit();
|
public void deinit();
|
||||||
public boolean inited();
|
public boolean inited();
|
||||||
|
|
||||||
|
}
|
||||||
}
|
|
||||||
|
@ -148,27 +148,32 @@ public class SenderColl<E extends SenderEntity<E>> extends Coll<E> implements Se
|
|||||||
// GET ALL ONLINE / OFFLINE
|
// GET ALL ONLINE / OFFLINE
|
||||||
// -------------------------------------------- //
|
// -------------------------------------------- //
|
||||||
|
|
||||||
// TODO: Stop mkaing a new predicate everytime.
|
public static final Predictate<SenderEntity<?>> PREDICTATE_ONLINE = new Predictate<SenderEntity<?>>()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public boolean apply(SenderEntity<?> entity)
|
||||||
|
{
|
||||||
|
return entity.isOnline();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
public static final Predictate<SenderEntity<?>> PREDICTATE_OFFLINE = new Predictate<SenderEntity<?>>()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public boolean apply(SenderEntity<?> entity)
|
||||||
|
{
|
||||||
|
return entity.isOffline();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
public Collection<E> getAllOnline()
|
public Collection<E> getAllOnline()
|
||||||
{
|
{
|
||||||
return this.getAll(new Predictate<E>()
|
return this.getAll(PREDICTATE_ONLINE);
|
||||||
{
|
|
||||||
public boolean apply(E entity)
|
|
||||||
{
|
|
||||||
return entity.isOnline();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Collection<E> getAllOffline()
|
public Collection<E> getAllOffline()
|
||||||
{
|
{
|
||||||
return this.getAll(new Predictate<E>()
|
return this.getAll(PREDICTATE_OFFLINE);
|
||||||
{
|
|
||||||
public boolean apply(E entity)
|
|
||||||
{
|
|
||||||
return entity.isOffline();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// -------------------------------------------- //
|
// -------------------------------------------- //
|
||||||
|
Loading…
Reference in New Issue
Block a user