Since I only used String ids for now, let's assume that's always the case. Saves us a lot of generics-crap.
This commit is contained in:
parent
21dac166eb
commit
7a358a80d5
@ -263,7 +263,7 @@ public class InternalListener implements Listener
|
||||
public void syncAllForPlayer(Player player)
|
||||
{
|
||||
String playerName = player.getName();
|
||||
for (Coll<?, ?> coll : Coll.instances)
|
||||
for (Coll<?> coll : Coll.instances)
|
||||
{
|
||||
if (!(coll instanceof SenderColl)) continue;
|
||||
SenderColl<?> pcoll = (SenderColl<?>)coll;
|
||||
|
@ -95,7 +95,7 @@ public class MCore extends MPlugin
|
||||
{
|
||||
public void run()
|
||||
{
|
||||
for (Coll<?, ?> coll : Coll.instances)
|
||||
for (Coll<?> coll : Coll.instances)
|
||||
{
|
||||
coll.onTick();
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ import com.massivecraft.mcore.store.Entity;
|
||||
import com.massivecraft.mcore.util.MUtil;
|
||||
import com.massivecraft.mcore.util.PermUtil;
|
||||
|
||||
public class MCoreConf extends Entity<MCoreConf, String>
|
||||
public class MCoreConf extends Entity<MCoreConf>
|
||||
{
|
||||
// -------------------------------------------- //
|
||||
// META
|
||||
|
@ -4,7 +4,7 @@ import com.massivecraft.mcore.MCore;
|
||||
import com.massivecraft.mcore.store.Coll;
|
||||
import com.massivecraft.mcore.store.MStore;
|
||||
|
||||
public class MCoreConfColl extends Coll<MCoreConf, String>
|
||||
public class MCoreConfColl extends Coll<MCoreConf>
|
||||
{
|
||||
// -------------------------------------------- //
|
||||
// INSTANCE & CONSTRUCT
|
||||
@ -14,7 +14,7 @@ public class MCoreConfColl extends Coll<MCoreConf, String>
|
||||
public static MCoreConfColl get() { return i; }
|
||||
private MCoreConfColl()
|
||||
{
|
||||
super(MStore.getDb(ConfServer.dburi), MCore.get(), "ai", "mcore_conf", MCoreConf.class, String.class, true);
|
||||
super(MStore.getDb(ConfServer.dburi), MCore.get(), "ai", "mcore_conf", MCoreConf.class, true);
|
||||
}
|
||||
|
||||
// -------------------------------------------- //
|
||||
|
@ -53,7 +53,7 @@ public abstract class MPlugin extends JavaPlugin implements Listener
|
||||
public void onDisable()
|
||||
{
|
||||
// Collection shutdowns.
|
||||
for (Coll<?, ?> coll : Coll.instances)
|
||||
for (Coll<?> coll : Coll.instances)
|
||||
{
|
||||
if (coll.getPlugin() != this) continue;
|
||||
coll.examineThread().interrupt();
|
||||
|
@ -22,13 +22,13 @@ import com.massivecraft.mcore.store.idstrategy.IdStrategy;
|
||||
import com.massivecraft.mcore.store.storeadapter.StoreAdapter;
|
||||
import com.massivecraft.mcore.xlib.gson.Gson;
|
||||
|
||||
public class Coll<E, L extends Comparable<? super L>> implements CollInterface<E, L>
|
||||
public class Coll<E> implements CollInterface<E>
|
||||
{
|
||||
// -------------------------------------------- //
|
||||
// GLOBAL REGISTRY
|
||||
// -------------------------------------------- //
|
||||
|
||||
public static List<Coll<?, ?>> instances = new CopyOnWriteArrayList<Coll<?, ?>>();
|
||||
public static List<Coll<?>> instances = new CopyOnWriteArrayList<Coll<?>>();
|
||||
|
||||
// -------------------------------------------- //
|
||||
// WHAT DO WE HANDLE?
|
||||
@ -45,9 +45,6 @@ public class Coll<E, L extends Comparable<? super L>> implements CollInterface<E
|
||||
|
||||
protected final Class<E> entityClass;
|
||||
@Override public Class<E> getEntityClass() { return this.entityClass; }
|
||||
|
||||
protected final Class<L> idClass;
|
||||
@Override public Class<L> getIdClass() { return this.idClass; }
|
||||
|
||||
// -------------------------------------------- //
|
||||
// SUPPORTING SYSTEM
|
||||
@ -71,8 +68,8 @@ public class Coll<E, L extends Comparable<? super L>> implements CollInterface<E
|
||||
@Override public Db<?> getDb() { return this.db; }
|
||||
@Override public Driver<?> getDriver() { return this.db.getDriver(); }
|
||||
|
||||
protected IdStrategy<L, ?> idStrategy;
|
||||
@Override public IdStrategy<L, ?> getIdStrategy() { return this.idStrategy; }
|
||||
protected IdStrategy idStrategy;
|
||||
@Override public IdStrategy getIdStrategy() { return this.idStrategy; }
|
||||
|
||||
protected StoreAdapter storeAdapter;
|
||||
@Override public StoreAdapter getStoreAdapter() { return this.storeAdapter; }
|
||||
@ -84,8 +81,8 @@ public class Coll<E, L extends Comparable<? super L>> implements CollInterface<E
|
||||
// STORAGE
|
||||
// -------------------------------------------- //
|
||||
|
||||
protected Map<L, E> id2entity;
|
||||
@Override public Map<L, E> getId2entity() { return Collections.unmodifiableMap(this.id2entity); }
|
||||
protected Map<String, E> id2entity;
|
||||
@Override public Map<String, E> getId2entity() { return Collections.unmodifiableMap(this.id2entity); }
|
||||
@Override
|
||||
public E get(Object oid)
|
||||
{
|
||||
@ -98,7 +95,7 @@ public class Coll<E, L extends Comparable<? super L>> implements CollInterface<E
|
||||
}
|
||||
protected E get(Object oid, boolean creative, boolean noteChange)
|
||||
{
|
||||
L id = this.fixId(oid);
|
||||
String id = this.fixId(oid);
|
||||
if (id == null) return null;
|
||||
E ret = this.id2entity.get(id);
|
||||
if (ret != null) return ret;
|
||||
@ -106,20 +103,20 @@ public class Coll<E, L extends Comparable<? super L>> implements CollInterface<E
|
||||
return this.create(id, noteChange);
|
||||
}
|
||||
|
||||
@Override public Collection<L> getIds() { return Collections.unmodifiableCollection(this.id2entity.keySet()); }
|
||||
@Override public Collection<L> getIdsRemote() { return this.getDb().getDriver().getIds(this); }
|
||||
@Override public Collection<String> getIds() { return Collections.unmodifiableCollection(this.id2entity.keySet()); }
|
||||
@Override public Collection<String> getIdsRemote() { return this.getDb().getDriver().getIds(this); }
|
||||
@Override
|
||||
public boolean containsId(Object oid)
|
||||
{
|
||||
L id = this.fixId(oid);
|
||||
String id = this.fixId(oid);
|
||||
if (id == null) return false;
|
||||
return this.id2entity.containsKey(id);
|
||||
}
|
||||
|
||||
// Get the id for this entity.
|
||||
protected Map<E, L> entity2id;
|
||||
@Override public Map<E, L> getEntity2id() { return Collections.unmodifiableMap(this.entity2id); }
|
||||
@Override public L getId(Object entity) { return this.entity2id.get(entity); }
|
||||
protected Map<E, String> entity2id;
|
||||
@Override public Map<E, String> getEntity2id() { return Collections.unmodifiableMap(this.entity2id); }
|
||||
@Override public String getId(Object entity) { return this.entity2id.get(entity); }
|
||||
@Override public boolean containsEntity(Object entity) { return this.entity2id.containsKey(entity); };
|
||||
|
||||
@Override public Collection<E> getAll() { return Collections.unmodifiableCollection(this.entity2id.keySet()); }
|
||||
@ -129,10 +126,10 @@ public class Coll<E, L extends Comparable<? super L>> implements CollInterface<E
|
||||
@Override public Collection<E> getAll(Predictate<? super E> where, Comparator<? super E> orderby, Integer limit, Integer offset) { return MStoreUtil.uglySQL(this.getAll(), where, orderby, limit, offset); }
|
||||
|
||||
@Override
|
||||
public L fixId(Object oid)
|
||||
public String fixId(Object oid)
|
||||
{
|
||||
if (oid == null) return null;
|
||||
if (oid.getClass() == this.idClass) return this.idClass.cast(oid);
|
||||
if (oid instanceof String) return (String)oid;
|
||||
if (oid.getClass() == this.entityClass) return this.entity2id.get(oid);
|
||||
return null;
|
||||
}
|
||||
@ -224,23 +221,23 @@ public class Coll<E, L extends Comparable<? super L>> implements CollInterface<E
|
||||
// -------------------------------------------- //
|
||||
|
||||
@Override
|
||||
public L attach(E entity)
|
||||
public String attach(E entity)
|
||||
{
|
||||
return this.attach(entity, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized L attach(E entity, Object oid)
|
||||
public synchronized String attach(E entity, Object oid)
|
||||
{
|
||||
return this.attach(entity, oid, true);
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
protected synchronized L attach(E entity, Object oid, boolean noteChange)
|
||||
protected synchronized String attach(E entity, Object oid, boolean noteChange)
|
||||
{
|
||||
// Check entity
|
||||
if (entity == null) return null;
|
||||
L id = this.getId(entity);
|
||||
String id = this.getId(entity);
|
||||
if (id != null) return id;
|
||||
|
||||
// Check/Fix id
|
||||
@ -285,7 +282,7 @@ public class Coll<E, L extends Comparable<? super L>> implements CollInterface<E
|
||||
@Override
|
||||
public E detachId(Object oid)
|
||||
{
|
||||
L id = this.fixId(oid);
|
||||
String id = this.fixId(oid);
|
||||
if (id == null) return null;
|
||||
|
||||
// Remove @ local
|
||||
@ -302,13 +299,13 @@ public class Coll<E, L extends Comparable<? super L>> implements CollInterface<E
|
||||
// IDENTIFIED CHANGES
|
||||
// -------------------------------------------- //
|
||||
|
||||
protected Set<L> localAttachIds;
|
||||
protected Set<L> localDetachIds;
|
||||
protected Set<L> changedIds;
|
||||
protected Set<String> localAttachIds;
|
||||
protected Set<String> localDetachIds;
|
||||
protected Set<String> changedIds;
|
||||
|
||||
protected synchronized void clearIdentifiedChanges(Object oid)
|
||||
{
|
||||
L id = this.fixId(oid);
|
||||
String id = this.fixId(oid);
|
||||
this.localAttachIds.remove(id);
|
||||
this.localDetachIds.remove(id);
|
||||
this.changedIds.remove(id);
|
||||
@ -318,13 +315,13 @@ public class Coll<E, L extends Comparable<? super L>> implements CollInterface<E
|
||||
// SYNCLOG
|
||||
// -------------------------------------------- //
|
||||
|
||||
protected Map<L, Long> lastMtime;
|
||||
protected Map<L, Object> lastRaw;
|
||||
protected Set<L> lastDefault;
|
||||
protected Map<String, Long> lastMtime;
|
||||
protected Map<String, Object> lastRaw;
|
||||
protected Set<String> lastDefault;
|
||||
|
||||
protected synchronized void clearSynclog(Object oid)
|
||||
{
|
||||
L id = this.fixId(oid);
|
||||
String id = this.fixId(oid);
|
||||
this.lastMtime.remove(id);
|
||||
this.lastRaw.remove(id);
|
||||
this.lastDefault.remove(id);
|
||||
@ -338,7 +335,7 @@ public class Coll<E, L extends Comparable<? super L>> implements CollInterface<E
|
||||
@Override
|
||||
public synchronized E removeAtLocal(Object oid)
|
||||
{
|
||||
L id = this.fixId(oid);
|
||||
String id = this.fixId(oid);
|
||||
this.clearIdentifiedChanges(id);
|
||||
this.clearSynclog(id);
|
||||
|
||||
@ -360,7 +357,7 @@ public class Coll<E, L extends Comparable<? super L>> implements CollInterface<E
|
||||
@Override
|
||||
public synchronized void removeAtRemote(Object oid)
|
||||
{
|
||||
L id = this.fixId(oid);
|
||||
String id = this.fixId(oid);
|
||||
|
||||
this.clearIdentifiedChanges(id);
|
||||
this.clearSynclog(id);
|
||||
@ -371,7 +368,7 @@ public class Coll<E, L extends Comparable<? super L>> implements CollInterface<E
|
||||
@Override
|
||||
public synchronized void saveToRemote(Object oid)
|
||||
{
|
||||
L id = this.fixId(oid);
|
||||
String id = this.fixId(oid);
|
||||
|
||||
this.clearIdentifiedChanges(id);
|
||||
this.clearSynclog(id);
|
||||
@ -398,7 +395,7 @@ public class Coll<E, L extends Comparable<? super L>> implements CollInterface<E
|
||||
@Override
|
||||
public synchronized void loadFromRemote(Object oid)
|
||||
{
|
||||
L id = this.fixId(oid);
|
||||
String id = this.fixId(oid);
|
||||
|
||||
this.clearIdentifiedChanges(id);
|
||||
|
||||
@ -432,20 +429,20 @@ public class Coll<E, L extends Comparable<? super L>> implements CollInterface<E
|
||||
@Override
|
||||
public ModificationState examineId(Object oid)
|
||||
{
|
||||
L id = this.fixId(oid);
|
||||
String id = this.fixId(oid);
|
||||
return this.examineId(id, null, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ModificationState examineId(Object oid, Long remoteMtime)
|
||||
{
|
||||
L id = this.fixId(oid);
|
||||
String id = this.fixId(oid);
|
||||
return this.examineId(id, remoteMtime, true);
|
||||
}
|
||||
|
||||
protected ModificationState examineId(Object oid, Long remoteMtime, boolean remoteMtimeSupplied)
|
||||
{
|
||||
L id = this.fixId(oid);
|
||||
String id = this.fixId(oid);
|
||||
|
||||
if (this.localDetachIds.contains(id)) return ModificationState.LOCAL_DETACH;
|
||||
if (this.localAttachIds.contains(id)) return ModificationState.LOCAL_ATTACH;
|
||||
@ -486,7 +483,7 @@ public class Coll<E, L extends Comparable<? super L>> implements CollInterface<E
|
||||
|
||||
return ModificationState.NONE;
|
||||
}
|
||||
protected boolean examineHasLocalAlter(L id, E entity)
|
||||
protected boolean examineHasLocalAlter(String id, E entity)
|
||||
{
|
||||
Object lastRaw = this.lastRaw.get(id);
|
||||
Object currentRaw = this.storeAdapter.read(this, entity);
|
||||
@ -496,7 +493,7 @@ public class Coll<E, L extends Comparable<? super L>> implements CollInterface<E
|
||||
@Override
|
||||
public ModificationState syncId(Object oid)
|
||||
{
|
||||
L id = this.fixId(oid);
|
||||
String id = this.fixId(oid);
|
||||
|
||||
ModificationState mstate = this.examineId(id);
|
||||
|
||||
@ -529,7 +526,7 @@ public class Coll<E, L extends Comparable<? super L>> implements CollInterface<E
|
||||
@Override
|
||||
public void syncSuspects()
|
||||
{
|
||||
for (L id : this.changedIds)
|
||||
for (String id : this.changedIds)
|
||||
{
|
||||
this.syncId(id);
|
||||
}
|
||||
@ -539,9 +536,9 @@ public class Coll<E, L extends Comparable<? super L>> implements CollInterface<E
|
||||
public void syncAll()
|
||||
{
|
||||
// Find all ids
|
||||
Set<L> allids = new HashSet<L>(this.id2entity.keySet());
|
||||
Set<String> allids = new HashSet<String>(this.id2entity.keySet());
|
||||
allids.addAll(this.getDriver().getIds(this));
|
||||
for (L id : allids)
|
||||
for (String id : allids)
|
||||
{
|
||||
this.syncId(id);
|
||||
}
|
||||
@ -551,15 +548,15 @@ public class Coll<E, L extends Comparable<? super L>> implements CollInterface<E
|
||||
public void findSuspects()
|
||||
{
|
||||
// Get remote id and mtime snapshot
|
||||
Map<L, Long> id2RemoteMtime = this.getDb().getDriver().getId2mtime(this);
|
||||
Map<String, Long> id2RemoteMtime = this.getDb().getDriver().getId2mtime(this);
|
||||
|
||||
// Compile a list of all ids (both remote and local)
|
||||
Set<L> allids = new HashSet<L>();
|
||||
Set<String> allids = new HashSet<String>();
|
||||
allids.addAll(id2RemoteMtime.keySet());
|
||||
allids.addAll(this.id2entity.keySet());
|
||||
|
||||
// Check for modifications
|
||||
for (L id : allids)
|
||||
for (String id : allids)
|
||||
{
|
||||
Long remoteMtime = id2RemoteMtime.get(id);
|
||||
ModificationState state = this.examineId(id, remoteMtime);
|
||||
@ -584,14 +581,14 @@ public class Coll<E, L extends Comparable<? super L>> implements CollInterface<E
|
||||
this.syncSuspects();
|
||||
}
|
||||
|
||||
protected ExamineThread<E, L> examineThread;
|
||||
protected ExamineThread<E> examineThread;
|
||||
@Override public Thread examineThread() { return this.examineThread; }
|
||||
|
||||
// -------------------------------------------- //
|
||||
// CONSTRUCT
|
||||
// -------------------------------------------- //
|
||||
|
||||
public Coll(Db<?> db, Plugin plugin, String idStrategyName, String name, Class<E> entityClass, Class<L> idClass, boolean creative, Comparator<? super L> idComparator, Comparator<? super E> entityComparator)
|
||||
public Coll(Db<?> db, Plugin plugin, String idStrategyName, String name, Class<E> entityClass, boolean creative, Comparator<? super String> idComparator, Comparator<? super E> entityComparator)
|
||||
{
|
||||
// Setup the name and the parsed parts
|
||||
this.name = name;
|
||||
@ -608,7 +605,6 @@ public class Coll<E, L extends Comparable<? super L>> implements CollInterface<E
|
||||
|
||||
// WHAT DO WE HANDLE?
|
||||
this.entityClass = entityClass;
|
||||
this.idClass = idClass;
|
||||
this.creative = creative;
|
||||
|
||||
// SUPPORTING SYSTEM
|
||||
@ -620,41 +616,37 @@ public class Coll<E, L extends Comparable<? super L>> implements CollInterface<E
|
||||
{
|
||||
throw new IllegalArgumentException("UNKNOWN: The id stragegy \""+idStrategyName+"\" is unknown to the driver \""+db.getDriver().getName()+"\".");
|
||||
}
|
||||
else if (this.idStrategy.getLocalClass() != idClass)
|
||||
{
|
||||
throw new IllegalArgumentException("MISSMATCH: The id stragegy \""+idStrategyName+"\" for the driver \""+db.getDriver().getName()+"\" uses \""+this.idStrategy.getLocalClass().getSimpleName()+"\" but the collection "+this.name+"/"+this.getClass().getSimpleName()+" uses \""+idClass.getSimpleName()+"\".");
|
||||
}
|
||||
this.collDriverObject = db.getCollDriverObject(this);
|
||||
|
||||
// STORAGE
|
||||
this.id2entity = new ConcurrentSkipListMap<L, E>(idComparator);
|
||||
this.entity2id = new ConcurrentSkipListMap<E, L>(entityComparator);
|
||||
this.id2entity = new ConcurrentSkipListMap<String, E>(idComparator);
|
||||
this.entity2id = new ConcurrentSkipListMap<E, String>(entityComparator);
|
||||
|
||||
// IDENTIFIED CHANGES
|
||||
this.localAttachIds = new ConcurrentSkipListSet<L>(idComparator);
|
||||
this.localDetachIds = new ConcurrentSkipListSet<L>(idComparator);
|
||||
this.changedIds = new ConcurrentSkipListSet<L>(idComparator);
|
||||
this.localAttachIds = new ConcurrentSkipListSet<String>(idComparator);
|
||||
this.localDetachIds = new ConcurrentSkipListSet<String>(idComparator);
|
||||
this.changedIds = new ConcurrentSkipListSet<String>(idComparator);
|
||||
|
||||
// SYNCLOG
|
||||
this.lastMtime = new ConcurrentSkipListMap<L, Long>(idComparator);
|
||||
this.lastRaw = new ConcurrentSkipListMap<L, Object>(idComparator);
|
||||
this.lastDefault = new ConcurrentSkipListSet<L>(idComparator);
|
||||
this.lastMtime = new ConcurrentSkipListMap<String, Long>(idComparator);
|
||||
this.lastRaw = new ConcurrentSkipListMap<String, Object>(idComparator);
|
||||
this.lastDefault = new ConcurrentSkipListSet<String>(idComparator);
|
||||
|
||||
final Coll<E, L> me = this;
|
||||
final Coll<E> me = this;
|
||||
this.tickTask = new Runnable()
|
||||
{
|
||||
@Override public void run() { me.onTick(); }
|
||||
};
|
||||
}
|
||||
|
||||
public Coll(Db<?> db, Plugin plugin, String idStrategyName, String name, Class<E> entityClass, Class<L> idClass, boolean creative)
|
||||
public Coll(Db<?> db, Plugin plugin, String idStrategyName, String name, Class<E> entityClass, boolean creative)
|
||||
{
|
||||
this(db, plugin, idStrategyName, name, entityClass, idClass, creative, null, null);
|
||||
this(db, plugin, idStrategyName, name, entityClass, creative, null, null);
|
||||
}
|
||||
|
||||
public Coll(Plugin plugin, String idStrategyName, String name, Class<E> entityClass, Class<L> idClass, boolean creative)
|
||||
public Coll(Plugin plugin, String idStrategyName, String name, Class<E> entityClass, boolean creative)
|
||||
{
|
||||
this(MCore.getDb(), plugin, idStrategyName, name, entityClass, idClass, creative);
|
||||
this(MCore.getDb(), plugin, idStrategyName, name, entityClass, creative);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -662,7 +654,7 @@ public class Coll<E, L extends Comparable<? super L>> implements CollInterface<E
|
||||
{
|
||||
if (this.inited()) return;
|
||||
this.syncAll();
|
||||
this.examineThread = new ExamineThread<E, L>(this);
|
||||
this.examineThread = new ExamineThread<E>(this);
|
||||
this.examineThread.start();
|
||||
instances.add(this);
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ import com.massivecraft.mcore.Predictate;
|
||||
import com.massivecraft.mcore.store.idstrategy.IdStrategy;
|
||||
import com.massivecraft.mcore.store.storeadapter.StoreAdapter;
|
||||
|
||||
public interface CollInterface<E, L extends Comparable<? super L>>
|
||||
public interface CollInterface<E>
|
||||
{
|
||||
// -------------------------------------------- //
|
||||
// WHAT DO WE HANDLE?
|
||||
@ -19,7 +19,6 @@ public interface CollInterface<E, L extends Comparable<? super L>>
|
||||
public String getBasename();
|
||||
public String getUniverse();
|
||||
public Class<E> getEntityClass();
|
||||
public Class<L> getIdClass();
|
||||
|
||||
// -------------------------------------------- //
|
||||
// SUPPORTING SYSTEM
|
||||
@ -29,21 +28,21 @@ public interface CollInterface<E, L extends Comparable<? super L>>
|
||||
public Db<?> getDb();
|
||||
public Driver<?> getDriver();
|
||||
public StoreAdapter getStoreAdapter();
|
||||
public IdStrategy<L, ?> getIdStrategy();
|
||||
public IdStrategy getIdStrategy();
|
||||
public Object getCollDriverObject();
|
||||
|
||||
// -------------------------------------------- //
|
||||
// STORAGE
|
||||
// -------------------------------------------- //
|
||||
public Map<L, E> getId2entity();
|
||||
public Map<String, E> getId2entity();
|
||||
public E get(Object oid);
|
||||
public E get(Object oid, boolean creative);
|
||||
public Collection<L> getIds();
|
||||
public Collection<L> getIdsRemote();
|
||||
public Collection<String> getIds();
|
||||
public Collection<String> getIdsRemote();
|
||||
public boolean containsId(Object oid);
|
||||
|
||||
public Map<E, L> getEntity2id();
|
||||
public L getId(Object entity);
|
||||
public Map<E, String> getEntity2id();
|
||||
public String getId(Object entity);
|
||||
public boolean containsEntity(Object entity);
|
||||
public Collection<E> getAll();
|
||||
public Collection<E> getAll(Predictate<? super E> where);
|
||||
@ -51,7 +50,7 @@ public interface CollInterface<E, L extends Comparable<? super L>>
|
||||
public Collection<E> getAll(Predictate<? super E> where, Comparator<? super E> orderby, Integer limit);
|
||||
public Collection<E> getAll(Predictate<? super E> where, Comparator<? super E> orderby, Integer limit, Integer offset);
|
||||
|
||||
public L fixId(Object oid);
|
||||
public String fixId(Object oid);
|
||||
|
||||
// -------------------------------------------- //
|
||||
// BAHAVIOR
|
||||
@ -81,8 +80,8 @@ public interface CollInterface<E, L extends Comparable<? super L>>
|
||||
// -------------------------------------------- //
|
||||
// ATTACH AND DETACH
|
||||
// -------------------------------------------- //
|
||||
public L attach(E entity);
|
||||
public L attach(E entity, Object oid);
|
||||
public String attach(E entity);
|
||||
public String attach(E entity, Object oid);
|
||||
public E detachEntity(Object entity);
|
||||
public E detachId(Object oid);
|
||||
|
||||
|
@ -9,7 +9,7 @@ import com.massivecraft.mcore.usys.Aspect;
|
||||
import com.massivecraft.mcore.usys.Multiverse;
|
||||
import com.massivecraft.mcore.util.MUtil;
|
||||
|
||||
public abstract class Colls<C extends Coll<E, L>, E, L extends Comparable<? super L>>
|
||||
public abstract class Colls<C extends Coll<E>, E>
|
||||
{
|
||||
protected Map<String, C> name2coll = new HashMap<String, C>();
|
||||
|
||||
|
@ -12,5 +12,5 @@ public interface Db<R>
|
||||
|
||||
public Driver<R> getDriver();
|
||||
|
||||
public Object getCollDriverObject(Coll<?, ?> coll);
|
||||
}
|
||||
public Object getCollDriverObject(Coll<?> coll);
|
||||
}
|
@ -50,7 +50,7 @@ public class DbGson extends DbAbstract<JsonElement>
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getCollDriverObject(Coll<?, ?> coll)
|
||||
public Object getCollDriverObject(Coll<?> coll)
|
||||
{
|
||||
return new File(dir, coll.getName());
|
||||
}
|
||||
|
@ -49,7 +49,7 @@ public class DbMongo extends DbAbstract<BasicDBObject>
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getCollDriverObject(Coll<?, ?> coll)
|
||||
public Object getCollDriverObject(Coll<?> coll)
|
||||
{
|
||||
return db.getCollection(coll.getName());
|
||||
}
|
||||
|
@ -21,8 +21,8 @@ public interface Driver<R>
|
||||
public boolean equal(Object rawOne, Object rawTwo);
|
||||
|
||||
// This is some sort of database specific id strategy with built in adapter
|
||||
public boolean registerIdStrategy(IdStrategy<?, ?> idStrategy);
|
||||
public <L extends Comparable<? super L>> IdStrategy<L, ?> getIdStrategy(String idStrategyName);
|
||||
public boolean registerIdStrategy(IdStrategy idStrategy);
|
||||
public IdStrategy getIdStrategy(String idStrategyName);
|
||||
|
||||
// Get the default store adapter for the driver.
|
||||
public StoreAdapter getStoreAdapter();
|
||||
@ -34,25 +34,25 @@ public interface Driver<R>
|
||||
public Set<String> getCollnames(Db<?> db);
|
||||
|
||||
// Is id X in the collection?
|
||||
public <L extends Comparable<? super L>> boolean containsId(Coll<?, L> coll, L id);
|
||||
public boolean containsId(Coll<?> coll, String id);
|
||||
|
||||
// When was X last altered?
|
||||
public <L extends Comparable<? super L>> Long getMtime(Coll<?, L> coll, L id);
|
||||
public Long getMtime(Coll<?> coll, String id);
|
||||
|
||||
// What ids are in the collection?
|
||||
public <L extends Comparable<? super L>> Collection<L> getIds(Coll<?, L> coll);
|
||||
public Collection<String> getIds(Coll<?> coll);
|
||||
|
||||
// Return a map of all ids with their corresponding mtimes
|
||||
public <L extends Comparable<? super L>> Map<L, Long> getId2mtime(Coll<?, L> coll);
|
||||
public Map<String, Long> getId2mtime(Coll<?> coll);
|
||||
|
||||
// Load the raw data for X. The second part of the entry is the remote mtime at the load.
|
||||
public <L extends Comparable<? super L>> Entry<R, Long> load(Coll<?, L> coll, L id);
|
||||
public Entry<R, Long> load(Coll<?> coll, String id);
|
||||
|
||||
// Save raw data as X
|
||||
// Return value is the new mtime (we caused the change).
|
||||
// If the mtime is null something failed.
|
||||
public <L extends Comparable<? super L>> Long save(Coll<?, L> coll, L id, final Object rawData);
|
||||
public Long save(Coll<?> coll, String id, final Object rawData);
|
||||
|
||||
// Delete X
|
||||
public <L extends Comparable<? super L>> void delete(Coll<?, L> coll, L id);
|
||||
public void delete(Coll<?> coll, String id);
|
||||
}
|
||||
|
@ -15,20 +15,18 @@ public abstract class DriverAbstract<R> implements Driver<R>
|
||||
protected String name;
|
||||
@Override public String getName() { return this.name; }
|
||||
|
||||
protected Map<String, IdStrategy<?, ?>> idStrategies = new HashMap<String, IdStrategy<?, ?>>();
|
||||
protected Map<String, IdStrategy> idStrategies = new HashMap<String, IdStrategy>();
|
||||
@Override
|
||||
public boolean registerIdStrategy(IdStrategy<?, ?> idStrategy)
|
||||
public boolean registerIdStrategy(IdStrategy idStrategy)
|
||||
{
|
||||
if (idStrategies.containsKey(idStrategy.getName())) return false;
|
||||
idStrategies.put(idStrategy.getName(), idStrategy);
|
||||
return true;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public <L extends Comparable<? super L>> IdStrategy<L, ?> getIdStrategy(String idStrategyName)
|
||||
public IdStrategy getIdStrategy(String idStrategyName)
|
||||
{
|
||||
IdStrategy<?, ?> idStrategy = idStrategies.get(idStrategyName);
|
||||
return (IdStrategy<L, ?>) idStrategy;
|
||||
return idStrategies.get(idStrategyName);
|
||||
}
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ import java.util.Set;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import com.massivecraft.mcore.store.idstrategy.IdStrategyAiGson;
|
||||
import com.massivecraft.mcore.store.idstrategy.IdStrategyOidGson;
|
||||
import com.massivecraft.mcore.store.idstrategy.IdStrategyOid;
|
||||
import com.massivecraft.mcore.store.idstrategy.IdStrategyUuid;
|
||||
import com.massivecraft.mcore.store.storeadapter.StoreAdapter;
|
||||
import com.massivecraft.mcore.store.storeadapter.StoreAdapterGson;
|
||||
@ -72,13 +72,13 @@ public class DriverGson extends DriverAbstract<JsonElement>
|
||||
}
|
||||
|
||||
@Override
|
||||
public <L extends Comparable<? super L>> boolean containsId(Coll<?, L> coll, L id)
|
||||
public boolean containsId(Coll<?> coll, String id)
|
||||
{
|
||||
return fileFromId(coll, id).isFile();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <L extends Comparable<? super L>> Long getMtime(Coll<?, L> coll, L id)
|
||||
public Long getMtime(Coll<?> coll, String id)
|
||||
{
|
||||
File file = fileFromId(coll, id);
|
||||
if ( ! file.isFile()) return null;
|
||||
@ -86,45 +86,39 @@ public class DriverGson extends DriverAbstract<JsonElement>
|
||||
}
|
||||
|
||||
@Override
|
||||
public <L extends Comparable<? super L>> Collection<L> getIds(Coll<?, L> coll)
|
||||
public Collection<String> getIds(Coll<?> coll)
|
||||
{
|
||||
List<L> ret = new ArrayList<L>();
|
||||
List<String> ret = new ArrayList<String>();
|
||||
|
||||
// Scan the collection folder for .json files
|
||||
File collDir = getCollDir(coll);
|
||||
if ( ! collDir.isDirectory()) return ret;
|
||||
for(File file : collDir.listFiles(JsonFileFilter.get()))
|
||||
{
|
||||
// Then convert them to what they should be
|
||||
String remoteId = idFromFile(file);
|
||||
L localId = coll.getIdStrategy().remoteToLocal(remoteId);
|
||||
ret.add(localId);
|
||||
ret.add(idFromFile(file));
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <L extends Comparable<? super L>> Map<L, Long> getId2mtime(Coll<?, L> coll)
|
||||
public Map<String, Long> getId2mtime(Coll<?> coll)
|
||||
{
|
||||
Map<L, Long> ret = new HashMap<L, Long>();
|
||||
Map<String, Long> ret = new HashMap<String, Long>();
|
||||
|
||||
// Scan the collection folder for .json files
|
||||
File collDir = getCollDir(coll);
|
||||
if ( ! collDir.isDirectory()) return ret;
|
||||
for(File file : collDir.listFiles(JsonFileFilter.get()))
|
||||
{
|
||||
// Then convert them to what they should be
|
||||
String remoteId = idFromFile(file);
|
||||
L localId = coll.getIdStrategy().remoteToLocal(remoteId);
|
||||
ret.put(localId, file.lastModified());
|
||||
ret.put(idFromFile(file), file.lastModified());
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <L extends Comparable<? super L>> Entry<JsonElement, Long> load(Coll<?, L> coll, L id)
|
||||
public Entry<JsonElement, Long> load(Coll<?> coll, String id)
|
||||
{
|
||||
File file = fileFromId(coll, id);
|
||||
Long mtime = file.lastModified();
|
||||
@ -137,7 +131,7 @@ public class DriverGson extends DriverAbstract<JsonElement>
|
||||
}
|
||||
|
||||
@Override
|
||||
public <L extends Comparable<? super L>> Long save(Coll<?, L> coll, L id, Object rawData)
|
||||
public Long save(Coll<?> coll, String id, Object rawData)
|
||||
{
|
||||
File file = fileFromId(coll, id);
|
||||
String content = coll.getGson().toJson((JsonElement)rawData);
|
||||
@ -146,7 +140,7 @@ public class DriverGson extends DriverAbstract<JsonElement>
|
||||
}
|
||||
|
||||
@Override
|
||||
public <L extends Comparable<? super L>> void delete(Coll<?, L> coll, L id)
|
||||
public void delete(Coll<?> coll, String id)
|
||||
{
|
||||
File file = fileFromId(coll, id);
|
||||
file.delete();
|
||||
@ -156,7 +150,7 @@ public class DriverGson extends DriverAbstract<JsonElement>
|
||||
// UTIL
|
||||
// -------------------------------------------- //
|
||||
|
||||
protected static File getCollDir(Coll<?, ?> coll)
|
||||
protected static File getCollDir(Coll<?> coll)
|
||||
{
|
||||
return (File) coll.getCollDriverObject();
|
||||
}
|
||||
@ -168,11 +162,10 @@ public class DriverGson extends DriverAbstract<JsonElement>
|
||||
return name.substring(0, name.length()-5);
|
||||
}
|
||||
|
||||
protected static <L extends Comparable<? super L>> File fileFromId(Coll<?, L> coll, L id)
|
||||
protected static File fileFromId(Coll<?> coll, String id)
|
||||
{
|
||||
File collDir = getCollDir(coll);
|
||||
String idString = (String)coll.getIdStrategy().localToRemote(id);
|
||||
File idFile = new File(collDir, idString+DOTJSON);
|
||||
File idFile = new File(collDir, id+DOTJSON);
|
||||
return idFile;
|
||||
}
|
||||
|
||||
@ -201,7 +194,7 @@ public class DriverGson extends DriverAbstract<JsonElement>
|
||||
{
|
||||
instance = new DriverGson();
|
||||
instance.registerIdStrategy(IdStrategyAiGson.get());
|
||||
instance.registerIdStrategy(IdStrategyOidGson.get());
|
||||
instance.registerIdStrategy(IdStrategyOid.get());
|
||||
instance.registerIdStrategy(IdStrategyUuid.get());
|
||||
}
|
||||
|
||||
|
@ -10,7 +10,7 @@ import java.util.AbstractMap.SimpleEntry;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import com.massivecraft.mcore.store.idstrategy.IdStrategyAiMongo;
|
||||
import com.massivecraft.mcore.store.idstrategy.IdStrategyOidMongo;
|
||||
import com.massivecraft.mcore.store.idstrategy.IdStrategyOid;
|
||||
import com.massivecraft.mcore.store.idstrategy.IdStrategyUuid;
|
||||
import com.massivecraft.mcore.store.storeadapter.StoreAdapter;
|
||||
import com.massivecraft.mcore.store.storeadapter.StoreAdapterMongo;
|
||||
@ -72,39 +72,38 @@ public class DriverMongo extends DriverAbstract<BasicDBObject>
|
||||
}
|
||||
|
||||
@Override
|
||||
public <L extends Comparable<? super L>> boolean containsId(Coll<?, L> coll, L id)
|
||||
public boolean containsId(Coll<?> coll, String id)
|
||||
{
|
||||
DBCollection dbcoll = fixColl(coll);
|
||||
DBCursor cursor = dbcoll.find(new BasicDBObject(ID_FIELD, coll.getIdStrategy().localToRemote(id)));
|
||||
DBCursor cursor = dbcoll.find(new BasicDBObject(ID_FIELD, id));
|
||||
return cursor.count() != 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <L extends Comparable<? super L>> Long getMtime(Coll<?, L> coll, L id)
|
||||
public Long getMtime(Coll<?> coll, String id)
|
||||
{
|
||||
DBCollection dbcoll = fixColl(coll);
|
||||
BasicDBObject found = (BasicDBObject)dbcoll.findOne(new BasicDBObject(ID_FIELD, coll.getIdStrategy().localToRemote(id)), dboKeysMtime);
|
||||
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);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <L extends Comparable<? super L>> Collection<L> getIds(Coll<?, L> coll)
|
||||
public Collection<String> getIds(Coll<?> coll)
|
||||
{
|
||||
List<L> ret = null;
|
||||
List<String> ret = null;
|
||||
|
||||
DBCollection dbcoll = fixColl(coll);
|
||||
|
||||
DBCursor cursor = dbcoll.find(dboEmpty, dboKeysId);
|
||||
try
|
||||
{
|
||||
ret = new ArrayList<L>(cursor.count());
|
||||
ret = new ArrayList<String>(cursor.count());
|
||||
while(cursor.hasNext())
|
||||
{
|
||||
Object remoteId = cursor.next().get(ID_FIELD);
|
||||
L localId = coll.getIdStrategy().remoteToLocal(remoteId);
|
||||
ret.add(localId);
|
||||
ret.add(remoteId.toString());
|
||||
}
|
||||
}
|
||||
finally
|
||||
@ -116,24 +115,23 @@ public class DriverMongo extends DriverAbstract<BasicDBObject>
|
||||
}
|
||||
|
||||
@Override
|
||||
public <L extends Comparable<? super L>> Map<L, Long> getId2mtime(Coll<?, L> coll)
|
||||
public Map<String, Long> getId2mtime(Coll<?> coll)
|
||||
{
|
||||
Map<L, Long> ret = null;
|
||||
Map<String, Long> ret = null;
|
||||
|
||||
DBCollection dbcoll = fixColl(coll);
|
||||
|
||||
DBCursor cursor = dbcoll.find(dboEmpty, dboKeysIdandMtime);
|
||||
try
|
||||
{
|
||||
ret = new HashMap<L, Long>(cursor.count());
|
||||
ret = new HashMap<String, Long>(cursor.count());
|
||||
while(cursor.hasNext())
|
||||
{
|
||||
BasicDBObject raw = (BasicDBObject)cursor.next();
|
||||
Object remoteId = raw.get(ID_FIELD);
|
||||
L localId = coll.getIdStrategy().remoteToLocal(remoteId);
|
||||
if ( ! raw.containsField(MTIME_FIELD)) continue; // This should not happen! But better to ignore than crash?
|
||||
Long mtime = raw.getLong(MTIME_FIELD);
|
||||
ret.put(localId, mtime);
|
||||
ret.put(remoteId.toString(), mtime);
|
||||
}
|
||||
}
|
||||
finally
|
||||
@ -145,17 +143,17 @@ public class DriverMongo extends DriverAbstract<BasicDBObject>
|
||||
}
|
||||
|
||||
@Override
|
||||
public <L extends Comparable<? super L>> Entry<BasicDBObject, Long> load(Coll<?, L> coll, L id)
|
||||
public Entry<BasicDBObject, Long> load(Coll<?> coll, String id)
|
||||
{
|
||||
DBCollection dbcoll = fixColl(coll);
|
||||
BasicDBObject raw = (BasicDBObject)dbcoll.findOne(new BasicDBObject(ID_FIELD, coll.getIdStrategy().localToRemote(id)));
|
||||
BasicDBObject raw = (BasicDBObject)dbcoll.findOne(new BasicDBObject(ID_FIELD, id));
|
||||
if (raw == null) return null;
|
||||
Long mtime = (Long) raw.removeField(MTIME_FIELD);
|
||||
return new SimpleEntry<BasicDBObject, Long>(raw, mtime);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <L extends Comparable<? super L>> Long save(Coll<?, L> coll, L id, Object rawData)
|
||||
public Long save(Coll<?> coll, String id, Object rawData)
|
||||
{
|
||||
DBCollection dbcoll = fixColl(coll);
|
||||
|
||||
@ -165,23 +163,22 @@ public class DriverMongo extends DriverAbstract<BasicDBObject>
|
||||
Long mtime = System.currentTimeMillis();
|
||||
data.put(MTIME_FIELD, mtime);
|
||||
|
||||
Object remoteId = coll.getIdStrategy().localToRemote(id);
|
||||
dbcoll.update(new BasicDBObject(ID_FIELD, remoteId), data, true, false);
|
||||
dbcoll.update(new BasicDBObject(ID_FIELD, id), data, true, false);
|
||||
|
||||
return mtime;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <L extends Comparable<? super L>> void delete(Coll<?, L> coll, L id)
|
||||
public void delete(Coll<?> coll, String id)
|
||||
{
|
||||
fixColl(coll).remove(new BasicDBObject(ID_FIELD, coll.getIdStrategy().localToRemote(id)));
|
||||
fixColl(coll).remove(new BasicDBObject(ID_FIELD, id));
|
||||
}
|
||||
|
||||
//----------------------------------------------//
|
||||
// UTIL
|
||||
//----------------------------------------------//
|
||||
|
||||
protected static DBCollection fixColl(Coll<?, ?> coll)
|
||||
protected static DBCollection fixColl(Coll<?> coll)
|
||||
{
|
||||
return (DBCollection) coll.getCollDriverObject();
|
||||
}
|
||||
@ -234,7 +231,7 @@ public class DriverMongo extends DriverAbstract<BasicDBObject>
|
||||
{
|
||||
instance = new DriverMongo();
|
||||
instance.registerIdStrategy(IdStrategyAiMongo.get());
|
||||
instance.registerIdStrategy(IdStrategyOidMongo.get());
|
||||
instance.registerIdStrategy(IdStrategyOid.get());
|
||||
instance.registerIdStrategy(IdStrategyUuid.get());
|
||||
}
|
||||
}
|
||||
|
@ -12,33 +12,33 @@ import com.massivecraft.mcore.xlib.gson.Gson;
|
||||
|
||||
// Self referencing generic.
|
||||
// http://www.angelikalanger.com/GenericsFAQ/FAQSections/ProgrammingIdioms.html#FAQ206
|
||||
public abstract class Entity<E extends Entity<E, L>, L extends Comparable<? super L>> implements Comparable<E>
|
||||
public abstract class Entity<E extends Entity<E>> implements Comparable<E>
|
||||
{
|
||||
protected transient Coll<E, L> coll;
|
||||
protected void setColl(Coll<E, L> val) { this.coll = val; }
|
||||
public Coll<E, L> getColl() { return this.coll; }
|
||||
protected transient Coll<E> coll;
|
||||
protected void setColl(Coll<E> val) { this.coll = val; }
|
||||
public Coll<E> getColl() { return this.coll; }
|
||||
|
||||
protected transient L id;
|
||||
protected void setid(L id) { this.id = id; }
|
||||
public L getId() { return this.id; }
|
||||
protected transient String id;
|
||||
protected void setid(String id) { this.id = id; }
|
||||
public String getId() { return this.id; }
|
||||
|
||||
public String getUniverse()
|
||||
{
|
||||
Coll<E, L> coll = this.getColl();
|
||||
Coll<E> coll = this.getColl();
|
||||
if (coll == null) return null;
|
||||
|
||||
return coll.getUniverse();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public L attach(Coll<E, L> coll)
|
||||
public String attach(Coll<E> coll)
|
||||
{
|
||||
return coll.attach((E) this);
|
||||
}
|
||||
|
||||
public E detach()
|
||||
{
|
||||
Coll<E, L> coll = this.getColl();
|
||||
Coll<E> coll = this.getColl();
|
||||
if (coll == null) return null;
|
||||
|
||||
return coll.detachEntity(this);
|
||||
@ -56,7 +56,7 @@ public abstract class Entity<E extends Entity<E, L>, L extends Comparable<? supe
|
||||
|
||||
public void changed()
|
||||
{
|
||||
L id = this.getId();
|
||||
String id = this.getId();
|
||||
if (id == null) return;
|
||||
|
||||
this.getColl().changedIds.add(id);
|
||||
@ -64,14 +64,14 @@ public abstract class Entity<E extends Entity<E, L>, L extends Comparable<? supe
|
||||
|
||||
public ModificationState sync()
|
||||
{
|
||||
L id = this.getId();
|
||||
String id = this.getId();
|
||||
if (id == null) return ModificationState.UNKNOWN;
|
||||
return this.getColl().syncId(id);
|
||||
}
|
||||
|
||||
public void saveToRemote()
|
||||
{
|
||||
L id = this.getId();
|
||||
String id = this.getId();
|
||||
if (id == null) return;
|
||||
|
||||
this.getColl().saveToRemote(id);
|
||||
@ -79,7 +79,7 @@ public abstract class Entity<E extends Entity<E, L>, L extends Comparable<? supe
|
||||
|
||||
public void loadFromRemote()
|
||||
{
|
||||
L id = this.getId();
|
||||
String id = this.getId();
|
||||
if (id == null) return;
|
||||
|
||||
this.getColl().loadFromRemote(id);
|
||||
@ -89,7 +89,7 @@ public abstract class Entity<E extends Entity<E, L>, L extends Comparable<? supe
|
||||
public String toString()
|
||||
{
|
||||
Gson gson = MCore.gson;
|
||||
Coll<E, L> coll = this.getColl();
|
||||
Coll<E> coll = this.getColl();
|
||||
if (coll != null) gson = coll.getGson();
|
||||
|
||||
return this.getClass().getSimpleName()+gson.toJson(this, this.getClass());
|
||||
@ -114,8 +114,8 @@ public abstract class Entity<E extends Entity<E, L>, L extends Comparable<? supe
|
||||
|
||||
if (this.equals(that)) return 0;
|
||||
|
||||
L thisId = this.getId();
|
||||
L thatId = that.getId();
|
||||
String thisId = this.getId();
|
||||
String thatId = that.getId();
|
||||
|
||||
if (thisId == null) return -1;
|
||||
if (thatId == null) return +1;
|
||||
|
@ -1,10 +1,10 @@
|
||||
package com.massivecraft.mcore.store;
|
||||
|
||||
public class ExamineThread<E, L extends Comparable<? super L>> extends Thread
|
||||
public class ExamineThread<E> extends Thread
|
||||
{
|
||||
protected Coll<E, L> coll;
|
||||
protected Coll<E> coll;
|
||||
|
||||
public ExamineThread(Coll<E, L> coll)
|
||||
public ExamineThread(Coll<E> coll)
|
||||
{
|
||||
this.coll = coll;
|
||||
this.setName("ExamineThread for "+coll.getName());
|
||||
|
@ -12,7 +12,7 @@ import com.massivecraft.mcore.Predictate;
|
||||
import com.massivecraft.mcore.mixin.Mixin;
|
||||
import com.massivecraft.mcore.util.MUtil;
|
||||
|
||||
public class SenderColl<E extends SenderEntity<E>> extends Coll<E, String> implements SenderIdSource
|
||||
public class SenderColl<E extends SenderEntity<E>> extends Coll<E> implements SenderIdSource
|
||||
{
|
||||
// -------------------------------------------- //
|
||||
// CONSTANTS
|
||||
@ -41,7 +41,7 @@ public class SenderColl<E extends SenderEntity<E>> extends Coll<E, String> imple
|
||||
|
||||
public SenderColl(Db<?> db, Plugin plugin, String name, Class<E> entityClass, boolean creative, boolean lowercasing, Comparator<? super String> idComparator, Comparator<? super E> entityComparator)
|
||||
{
|
||||
super(db, plugin, "ai", name, entityClass, String.class, creative, idComparator, entityComparator);
|
||||
super(db, plugin, "ai", name, entityClass, creative, idComparator, entityComparator);
|
||||
this.lowercasing = lowercasing;
|
||||
}
|
||||
|
||||
@ -128,7 +128,7 @@ public class SenderColl<E extends SenderEntity<E>> extends Coll<E, String> imple
|
||||
|
||||
public static void setSenderRefferences(String senderId, CommandSender sender)
|
||||
{
|
||||
for (Coll<?, ?> coll : Coll.instances)
|
||||
for (Coll<?> coll : Coll.instances)
|
||||
{
|
||||
if (!(coll instanceof SenderColl)) continue;
|
||||
SenderColl<?> senderColl = (SenderColl<?>)coll;
|
||||
|
@ -12,7 +12,7 @@ import org.bukkit.entity.Player;
|
||||
import com.massivecraft.mcore.mixin.Mixin;
|
||||
import com.massivecraft.mcore.util.SenderUtil;
|
||||
|
||||
public abstract class SenderEntity<E extends SenderEntity<E>> extends Entity<E, String>
|
||||
public abstract class SenderEntity<E extends SenderEntity<E>> extends Entity<E>
|
||||
{
|
||||
// -------------------------------------------- //
|
||||
// FIELDS
|
||||
|
@ -11,19 +11,11 @@ import com.massivecraft.mcore.store.CollInterface;
|
||||
* Thus you will find multiple implementations with the name "ai" (auto increment).
|
||||
* There must be one implementation per driver.
|
||||
*/
|
||||
public interface IdStrategy<L extends Comparable<? super L>, R>
|
||||
public interface IdStrategy
|
||||
{
|
||||
// The name of the strategy (such as "auto_increment")
|
||||
public String getName();
|
||||
|
||||
// The id classes
|
||||
public Class<L> getLocalClass();
|
||||
public Class<R> getRemoteClass();
|
||||
|
||||
// Convert
|
||||
public R localToRemote(Object local);
|
||||
public L remoteToLocal(Object remote);
|
||||
|
||||
// Generate
|
||||
public L generate(CollInterface<?, L> coll);
|
||||
public String generate(CollInterface<?> coll);
|
||||
}
|
||||
|
@ -4,30 +4,21 @@ import java.util.Collection;
|
||||
|
||||
import com.massivecraft.mcore.store.CollInterface;
|
||||
|
||||
public abstract class IdStrategyAbstract<L extends Comparable<? super L>, R> implements IdStrategy<L, R>
|
||||
public abstract class IdStrategyAbstract implements IdStrategy
|
||||
{
|
||||
public IdStrategyAbstract(String name, Class<L> localClass, Class<R> remoteClass)
|
||||
public IdStrategyAbstract(String name)
|
||||
{
|
||||
this.name = name;
|
||||
this.localClass = localClass;
|
||||
this.remoteClass = remoteClass;
|
||||
}
|
||||
|
||||
protected String name;
|
||||
@Override public String getName() { return this.name; }
|
||||
|
||||
protected Class<L> localClass;
|
||||
@Override public Class<L> getLocalClass() { return this.localClass; }
|
||||
|
||||
protected Class<R> remoteClass;
|
||||
@Override public Class<R> getRemoteClass() { return this.remoteClass; }
|
||||
|
||||
|
||||
@Override
|
||||
public L generate(CollInterface<?, L> coll)
|
||||
public String generate(CollInterface<?> coll)
|
||||
{
|
||||
Collection<L> alreadyInUse = coll.getIds();
|
||||
L ret = null;
|
||||
Collection<String> alreadyInUse = coll.getIds();
|
||||
String ret = null;
|
||||
do
|
||||
{
|
||||
ret = this.generateAttempt(coll);
|
||||
@ -37,5 +28,5 @@ public abstract class IdStrategyAbstract<L extends Comparable<? super L>, R> imp
|
||||
return ret;
|
||||
}
|
||||
|
||||
public abstract L generateAttempt(CollInterface<?, L> coll);
|
||||
public abstract String generateAttempt(CollInterface<?> coll);
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ package com.massivecraft.mcore.store.idstrategy;
|
||||
|
||||
import com.massivecraft.mcore.store.CollInterface;
|
||||
|
||||
public abstract class IdStrategyAiAbstract extends IdStrategyAbstract<String, String>
|
||||
public abstract class IdStrategyAiAbstract extends IdStrategyAbstract
|
||||
{
|
||||
//----------------------------------------------//
|
||||
// CONSTRUCT
|
||||
@ -10,18 +10,15 @@ public abstract class IdStrategyAiAbstract extends IdStrategyAbstract<String, St
|
||||
|
||||
public IdStrategyAiAbstract()
|
||||
{
|
||||
super("ai", String.class, String.class);
|
||||
super("ai");
|
||||
}
|
||||
|
||||
// -------------------------------------------- //
|
||||
// OVERRIDE: IdStrategyAbstract
|
||||
// -------------------------------------------- //
|
||||
|
||||
@Override public String localToRemote(Object local) { return (String)local; }
|
||||
@Override public String remoteToLocal(Object remote) { return (String)remote; }
|
||||
|
||||
@Override
|
||||
public String generateAttempt(CollInterface<?, String> coll)
|
||||
public String generateAttempt(CollInterface<?> coll)
|
||||
{
|
||||
Integer ret = this.getNextAndUpdate(coll);
|
||||
if (ret == null) return null;
|
||||
@ -33,8 +30,8 @@ public abstract class IdStrategyAiAbstract extends IdStrategyAbstract<String, St
|
||||
// ABSTRACT
|
||||
// -------------------------------------------- //
|
||||
|
||||
public abstract Integer getNextAndUpdate(CollInterface<?, String> coll);
|
||||
public abstract Integer getNext(CollInterface<?, String> coll);
|
||||
public abstract boolean setNext(CollInterface<?, String> coll, int next);
|
||||
public abstract Integer getNextAndUpdate(CollInterface<?> coll);
|
||||
public abstract Integer getNext(CollInterface<?> coll);
|
||||
public abstract boolean setNext(CollInterface<?> coll, int next);
|
||||
|
||||
}
|
||||
|
@ -22,7 +22,7 @@ public class IdStrategyAiGson extends IdStrategyAiAbstract
|
||||
// -------------------------------------------- //
|
||||
|
||||
@Override
|
||||
public Integer getNextAndUpdate(CollInterface<?, String> coll)
|
||||
public Integer getNextAndUpdate(CollInterface<?> coll)
|
||||
{
|
||||
Integer next = this.getNext(coll);
|
||||
if (next == null) return null;
|
||||
@ -34,7 +34,7 @@ public class IdStrategyAiGson extends IdStrategyAiAbstract
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getNext(CollInterface<?, String> coll)
|
||||
public Integer getNext(CollInterface<?> coll)
|
||||
{
|
||||
File file = this.getAiFile(coll);
|
||||
if (this.ensureFileExists(file) == false) return null;
|
||||
@ -46,7 +46,7 @@ public class IdStrategyAiGson extends IdStrategyAiAbstract
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setNext(CollInterface<?, String> coll, int next)
|
||||
public boolean setNext(CollInterface<?> coll, int next)
|
||||
{
|
||||
File file = this.getAiFile(coll);
|
||||
if (this.ensureFileExists(file) == false) return false;
|
||||
@ -58,7 +58,7 @@ public class IdStrategyAiGson extends IdStrategyAiAbstract
|
||||
// UTIL
|
||||
// -------------------------------------------- //
|
||||
|
||||
private File getAiFile(CollInterface<?, String> coll)
|
||||
private File getAiFile(CollInterface<?> coll)
|
||||
{
|
||||
DbGson cdb = (DbGson)coll.getDb();
|
||||
return new File(cdb.dir, coll.getName() + "_ai.txt");
|
||||
|
@ -30,7 +30,7 @@ public class IdStrategyAiMongo extends IdStrategyAiAbstract
|
||||
// http://dev.bubblemix.net/blog/2011/04/auto-increment-for-mongodb-with-the-java-driver/
|
||||
|
||||
@Override
|
||||
public Integer getNextAndUpdate(CollInterface<?, String> coll)
|
||||
public Integer getNextAndUpdate(CollInterface<?> coll)
|
||||
{
|
||||
DBCollection dbcoll = this.getSeqColl(coll);
|
||||
BasicDBObject res = (BasicDBObject) dbcoll.findAndModify(createQueryObject(coll), new BasicDBObject(), new BasicDBObject(), false, createUpdateObject(), true, true);
|
||||
@ -38,7 +38,7 @@ public class IdStrategyAiMongo extends IdStrategyAiAbstract
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getNext(CollInterface<?, String> coll)
|
||||
public Integer getNext(CollInterface<?> coll)
|
||||
{
|
||||
DBCollection dbcoll = this.getSeqColl(coll);
|
||||
BasicDBObject res = (BasicDBObject) dbcoll.findOne(createQueryObject(coll));
|
||||
@ -46,7 +46,7 @@ public class IdStrategyAiMongo extends IdStrategyAiAbstract
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setNext(CollInterface<?, String> coll, int next)
|
||||
public boolean setNext(CollInterface<?> coll, int next)
|
||||
{
|
||||
throw new RuntimeException("Not implemented yet");
|
||||
|
||||
@ -60,12 +60,12 @@ public class IdStrategyAiMongo extends IdStrategyAiAbstract
|
||||
// UTIL
|
||||
// -------------------------------------------- //
|
||||
|
||||
public DBCollection getSeqColl(CollInterface<?, String> coll)
|
||||
public DBCollection getSeqColl(CollInterface<?> coll)
|
||||
{
|
||||
return ((DbMongo)coll.getDb()).db.getCollection(SEC_COLL);
|
||||
}
|
||||
|
||||
public static DBObject createQueryObject(CollInterface<?, String> coll)
|
||||
public static DBObject createQueryObject(CollInterface<?> coll)
|
||||
{
|
||||
// this object represents your "query", its analogous to a WHERE clause in SQL
|
||||
DBObject query = new BasicDBObject();
|
||||
|
@ -0,0 +1,26 @@
|
||||
package com.massivecraft.mcore.store.idstrategy;
|
||||
|
||||
import com.massivecraft.mcore.store.CollInterface;
|
||||
import com.massivecraft.mcore.xlib.bson.types.ObjectId;
|
||||
|
||||
public class IdStrategyOid extends IdStrategyAbstract
|
||||
{
|
||||
// -------------------------------------------- //
|
||||
// INSTANCE & CONSTRUCT
|
||||
// -------------------------------------------- //
|
||||
|
||||
private static IdStrategyOid i = new IdStrategyOid();
|
||||
public static IdStrategyOid get() { return i; }
|
||||
private IdStrategyOid() { super("oid"); }
|
||||
|
||||
// -------------------------------------------- //
|
||||
// OVERRIDE
|
||||
// -------------------------------------------- //
|
||||
|
||||
@Override
|
||||
public String generateAttempt(CollInterface<?> coll)
|
||||
{
|
||||
return ObjectId.get().toString();
|
||||
}
|
||||
|
||||
}
|
@ -1,29 +0,0 @@
|
||||
package com.massivecraft.mcore.store.idstrategy;
|
||||
|
||||
import com.massivecraft.mcore.store.CollInterface;
|
||||
import com.massivecraft.mcore.xlib.bson.types.ObjectId;
|
||||
|
||||
public class IdStrategyOidGson extends IdStrategyAbstract<ObjectId, String>
|
||||
{
|
||||
// -------------------------------------------- //
|
||||
// INSTANCE & CONSTRUCT
|
||||
// -------------------------------------------- //
|
||||
|
||||
private static IdStrategyOidGson i = new IdStrategyOidGson();
|
||||
public static IdStrategyOidGson get() { return i; }
|
||||
private IdStrategyOidGson() { super("oid", ObjectId.class, String.class); }
|
||||
|
||||
// -------------------------------------------- //
|
||||
// OVERRIDE
|
||||
// -------------------------------------------- //
|
||||
|
||||
@Override public String localToRemote(Object local) { return ((ObjectId)local).toStringBabble(); }
|
||||
@Override public ObjectId remoteToLocal(Object remote) { return ObjectId.massageToObjectId((String)remote); }
|
||||
|
||||
@Override
|
||||
public ObjectId generateAttempt(CollInterface<?, ObjectId> coll)
|
||||
{
|
||||
return ObjectId.get();
|
||||
}
|
||||
|
||||
}
|
@ -1,29 +0,0 @@
|
||||
package com.massivecraft.mcore.store.idstrategy;
|
||||
|
||||
import com.massivecraft.mcore.store.CollInterface;
|
||||
import com.massivecraft.mcore.xlib.bson.types.ObjectId;
|
||||
|
||||
public class IdStrategyOidMongo extends IdStrategyAbstract<ObjectId, ObjectId>
|
||||
{
|
||||
// -------------------------------------------- //
|
||||
// INSTANCE & CONSTRUCT
|
||||
// -------------------------------------------- //
|
||||
|
||||
private static IdStrategyOidMongo i = new IdStrategyOidMongo();
|
||||
public static IdStrategyOidMongo get() { return i; }
|
||||
private IdStrategyOidMongo() { super("oid", ObjectId.class, ObjectId.class); }
|
||||
|
||||
// -------------------------------------------- //
|
||||
// OVERRIDE
|
||||
// -------------------------------------------- //
|
||||
|
||||
@Override public ObjectId localToRemote(Object local) { return (ObjectId)local; }
|
||||
@Override public ObjectId remoteToLocal(Object remote) { return (ObjectId)remote; }
|
||||
|
||||
@Override
|
||||
public ObjectId generateAttempt(CollInterface<?, ObjectId> coll)
|
||||
{
|
||||
return ObjectId.get();
|
||||
}
|
||||
|
||||
}
|
@ -2,10 +2,9 @@ package com.massivecraft.mcore.store.idstrategy;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import com.massivecraft.mcore.adapter.UUIDAdapter;
|
||||
import com.massivecraft.mcore.store.CollInterface;
|
||||
|
||||
public class IdStrategyUuid extends IdStrategyAbstract<UUID, String>
|
||||
public class IdStrategyUuid extends IdStrategyAbstract
|
||||
{
|
||||
// -------------------------------------------- //
|
||||
// INSTANCE & CONSTRUCT
|
||||
@ -13,19 +12,16 @@ public class IdStrategyUuid extends IdStrategyAbstract<UUID, String>
|
||||
|
||||
private static IdStrategyUuid i = new IdStrategyUuid();
|
||||
public static IdStrategyUuid get() { return i; }
|
||||
private IdStrategyUuid() { super("uuid", UUID.class, String.class); }
|
||||
private IdStrategyUuid() { super("uuid"); }
|
||||
|
||||
// -------------------------------------------- //
|
||||
// OVERRIDE
|
||||
// -------------------------------------------- //
|
||||
|
||||
@Override public String localToRemote(Object local) { return UUIDAdapter.convertUUIDToString((UUID)local); }
|
||||
@Override public UUID remoteToLocal(Object remote) { return UUIDAdapter.convertStringToUUID((String)remote); }
|
||||
|
||||
@Override
|
||||
public UUID generateAttempt(CollInterface<?, UUID> coll)
|
||||
public String generateAttempt(CollInterface<?> coll)
|
||||
{
|
||||
return UUID.randomUUID();
|
||||
return UUID.randomUUID().toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -8,6 +8,6 @@ public interface StoreAdapter
|
||||
// This method returns the name of that driver.
|
||||
public String forDriverName();
|
||||
|
||||
public Object read(Coll<?, ?> coll, Object entity);
|
||||
public void write(Coll<?, ?> coll, Object raw, Object entity); // (This is an opaque/complete write)
|
||||
public Object read(Coll<?> coll, Object entity);
|
||||
public void write(Coll<?> coll, Object raw, Object entity); // (This is an opaque/complete write)
|
||||
}
|
||||
|
@ -11,13 +11,13 @@ public class StoreAdapterGson extends StoreAdapterAbstract
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object read(Coll<?, ?> coll, Object entity)
|
||||
public Object read(Coll<?> coll, Object entity)
|
||||
{
|
||||
return coll.getGson().toJsonTree(entity, coll.getEntityClass());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(Coll<?, ?> coll, Object raw, Object entity)
|
||||
public void write(Coll<?> coll, Object raw, Object entity)
|
||||
{
|
||||
if (raw == null) throw new NullPointerException("raw");
|
||||
if (entity == null) throw new NullPointerException("entity");
|
||||
|
@ -12,13 +12,13 @@ public class StoreAdapterMongo extends StoreAdapterAbstract
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object read(Coll<?, ?> coll, Object entity)
|
||||
public Object read(Coll<?> coll, Object entity)
|
||||
{
|
||||
return MongoGsonConverter.gson2MongoObject((JsonElement)StoreAdapterGson.get().read(coll, entity));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(Coll<?, ?> coll, Object raw, Object entity)
|
||||
public void write(Coll<?> coll, Object raw, Object entity)
|
||||
{
|
||||
StoreAdapterGson.get().write(coll, MongoGsonConverter.mongo2GsonObject((DBObject) raw), entity);
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ import com.massivecraft.mcore.MCore;
|
||||
import com.massivecraft.mcore.store.Entity;
|
||||
import com.massivecraft.mcore.xlib.gson.annotations.SerializedName;
|
||||
|
||||
public class Aspect extends Entity<Aspect, String>
|
||||
public class Aspect extends Entity<Aspect>
|
||||
{
|
||||
// -------------------------------------------- //
|
||||
// META
|
||||
|
@ -6,7 +6,7 @@ import java.util.List;
|
||||
import com.massivecraft.mcore.MCore;
|
||||
import com.massivecraft.mcore.store.Coll;
|
||||
|
||||
public class AspectColl extends Coll<Aspect, String>
|
||||
public class AspectColl extends Coll<Aspect>
|
||||
{
|
||||
// -------------------------------------------- //
|
||||
// INSTANCE & CONSTRUCT
|
||||
@ -16,7 +16,7 @@ public class AspectColl extends Coll<Aspect, String>
|
||||
public static AspectColl get() { return i; }
|
||||
private AspectColl()
|
||||
{
|
||||
super(MCore.get(), "ai", "mcore_aspect", Aspect.class, String.class, false);
|
||||
super(MCore.get(), "ai", "mcore_aspect", Aspect.class, false);
|
||||
}
|
||||
|
||||
// -------------------------------------------- //
|
||||
|
@ -13,7 +13,7 @@ import com.massivecraft.mcore.cmd.arg.ARUniverse;
|
||||
import com.massivecraft.mcore.store.Entity;
|
||||
import com.massivecraft.mcore.util.MUtil;
|
||||
|
||||
public class Multiverse extends Entity<Multiverse, String>
|
||||
public class Multiverse extends Entity<Multiverse>
|
||||
{
|
||||
// -------------------------------------------- //
|
||||
// META
|
||||
|
@ -3,7 +3,7 @@ package com.massivecraft.mcore.usys;
|
||||
import com.massivecraft.mcore.MCore;
|
||||
import com.massivecraft.mcore.store.Coll;
|
||||
|
||||
public class MultiverseColl extends Coll<Multiverse, String>
|
||||
public class MultiverseColl extends Coll<Multiverse>
|
||||
{
|
||||
// -------------------------------------------- //
|
||||
// INSTANCE & CONSTRUCT
|
||||
@ -13,7 +13,7 @@ public class MultiverseColl extends Coll<Multiverse, String>
|
||||
public static MultiverseColl get() { return i; }
|
||||
private MultiverseColl()
|
||||
{
|
||||
super(MCore.get(), "ai", "mcore_multiverse", Multiverse.class, String.class, false);
|
||||
super(MCore.get(), "ai", "mcore_multiverse", Multiverse.class, false);
|
||||
}
|
||||
|
||||
// -------------------------------------------- //
|
||||
|
Loading…
Reference in New Issue
Block a user