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