The two sets were of course redundant. The two maps covered those two cases.

This commit is contained in:
Olof Larsson 2013-01-16 12:33:16 +01:00
parent 3191ff7916
commit e4942a2eb8
2 changed files with 26 additions and 37 deletions

View File

@ -70,27 +70,6 @@ public class Coll<E, L extends Comparable<? super L>> implements CollInterface<E
// STORAGE // STORAGE
// -------------------------------------------- // // -------------------------------------------- //
protected Set<L> ids;
@Override public Collection<L> getIds() { return Collections.unmodifiableCollection(this.ids); }
@Override public Collection<L> getIdsRemote() { return this.getDb().getDriver().getIds(this); }
@Override public boolean containsEntity(Object entity) { return this.entities.contains(entity); };
@Override
public boolean containsId(Object oid)
{
L id = this.fixId(oid);
if (id == null) return false;
return this.ids.contains(id);
}
// We use a ConcurrentSkipListSet here in order for the entities to keep their natural ordering
// You may want to have your entities implement the Comparable interface
protected Set<E> entities;
@Override public Collection<E> getAll() { return Collections.unmodifiableCollection(this.entities); }
@Override public Collection<E> getAll(Predictate<E> where) { return MStoreUtil.uglySQL(this.getAll(), where, null, null, null); }
@Override public Collection<E> getAll(Predictate<E> where, Comparator<E> orderby) { return MStoreUtil.uglySQL(this.getAll(), where, orderby, null, null); }
@Override public Collection<E> getAll(Predictate<E> where, Comparator<E> orderby, Integer limit) { return MStoreUtil.uglySQL(this.getAll(), where, orderby, limit, null); }
@Override public Collection<E> getAll(Predictate<E> where, Comparator<E> orderby, Integer limit, Integer offset) { return MStoreUtil.uglySQL(this.getAll(), where, orderby, limit, offset); }
protected Map<L, E> id2entity; protected Map<L, E> id2entity;
@Override public Map<L, E> getId2entity() { return Collections.unmodifiableMap(this.id2entity); } @Override public Map<L, E> getId2entity() { return Collections.unmodifiableMap(this.id2entity); }
@Override @Override
@ -113,10 +92,27 @@ 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<L> getIdsRemote() { return this.getDb().getDriver().getIds(this); }
@Override
public boolean containsId(Object oid)
{
L id = this.fixId(oid);
if (id == null) return false;
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, L> entity2id;
@Override public Map<E, L> getEntity2id() { return Collections.unmodifiableMap(this.entity2id); } @Override public Map<E, L> getEntity2id() { return Collections.unmodifiableMap(this.entity2id); }
@Override public L getId(Object entity) { return this.entity2id.get(entity); } @Override public L 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()); }
@Override public Collection<E> getAll(Predictate<E> where) { return MStoreUtil.uglySQL(this.getAll(), where, null, null, null); }
@Override public Collection<E> getAll(Predictate<E> where, Comparator<E> orderby) { return MStoreUtil.uglySQL(this.getAll(), where, orderby, null, null); }
@Override public Collection<E> getAll(Predictate<E> where, Comparator<E> orderby, Integer limit) { return MStoreUtil.uglySQL(this.getAll(), where, orderby, limit, null); }
@Override public Collection<E> getAll(Predictate<E> where, Comparator<E> orderby, Integer limit, Integer offset) { return MStoreUtil.uglySQL(this.getAll(), where, orderby, limit, offset); }
@Override @Override
public L fixId(Object oid) public L fixId(Object oid)
@ -227,7 +223,7 @@ public class Coll<E, L extends Comparable<? super L>> implements CollInterface<E
{ {
id = this.fixId(oid); id = this.fixId(oid);
if (id == null) return null; if (id == null) return null;
if (this.ids.contains(id)) return null; if (this.id2entity.containsKey(id)) return null;
} }
// Add entity reference info // Add entity reference info
@ -240,8 +236,6 @@ public class Coll<E, L extends Comparable<? super L>> implements CollInterface<E
// Attach // Attach
this.id2entity.put(id, entity); this.id2entity.put(id, entity);
this.entity2id.put(entity, id); this.entity2id.put(entity, id);
this.ids.add(id);
this.entities.add(entity);
// Make note of the change // Make note of the change
if (noteChange) if (noteChange)
@ -316,12 +310,10 @@ public class Coll<E, L extends Comparable<? super L>> implements CollInterface<E
this.clearIdentifiedChanges(id); this.clearIdentifiedChanges(id);
this.clearSynclog(id); this.clearSynclog(id);
this.ids.remove(id);
E entity = this.id2entity.remove(id); E entity = this.id2entity.remove(id);
if (entity == null) return null; if (entity == null) return null;
this.entity2id.remove(entity); this.entity2id.remove(entity);
this.entities.remove(entity);
// Remove entity reference info // Remove entity reference info
if (entity instanceof Entity) if (entity instanceof Entity)
@ -503,7 +495,7 @@ 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.ids); Set<L> allids = new HashSet<L>(this.id2entity.keySet());
allids.addAll(this.getDriver().getIds(this)); allids.addAll(this.getDriver().getIds(this));
for (L id : allids) for (L id : allids)
{ {
@ -520,7 +512,7 @@ public class Coll<E, L extends Comparable<? super L>> implements CollInterface<E
// 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<L> allids = new HashSet<L>();
allids.addAll(id2RemoteMtime.keySet()); allids.addAll(id2RemoteMtime.keySet());
allids.addAll(this.ids); allids.addAll(this.id2entity.keySet());
// Check for modifications // Check for modifications
for (L id : allids) for (L id : allids)
@ -591,8 +583,6 @@ public class Coll<E, L extends Comparable<? super L>> implements CollInterface<E
this.collDriverObject = db.getCollDriverObject(this); this.collDriverObject = db.getCollDriverObject(this);
// STORAGE // STORAGE
this.ids = new ConcurrentSkipListSet<L>(idComparator);
this.entities = new ConcurrentSkipListSet<E>(entityComparator);
this.id2entity = new ConcurrentSkipListMap<L, E>(idComparator); this.id2entity = new ConcurrentSkipListMap<L, E>(idComparator);
this.entity2id = new ConcurrentSkipListMap<E, L>(entityComparator); this.entity2id = new ConcurrentSkipListMap<E, L>(entityComparator);

View File

@ -34,23 +34,22 @@ public interface CollInterface<E, L extends Comparable<? super L>>
// -------------------------------------------- // // -------------------------------------------- //
// STORAGE // STORAGE
// -------------------------------------------- // // -------------------------------------------- //
public Map<L, E> getId2entity();
public E get(Object oid);
public E get(Object oid, boolean creative);
public Collection<L> getIds(); public Collection<L> getIds();
public Collection<L> getIdsRemote(); public Collection<L> getIdsRemote();
public boolean containsId(Object oid); public boolean containsId(Object oid);
public boolean containsEntity(Object entity);
public Map<E, L> getEntity2id();
public L getId(Object entity);
public boolean containsEntity(Object entity);
public Collection<E> getAll(); public Collection<E> getAll();
public Collection<E> getAll(Predictate<E> where); public Collection<E> getAll(Predictate<E> where);
public Collection<E> getAll(Predictate<E> where, Comparator<E> orderby); public Collection<E> getAll(Predictate<E> where, Comparator<E> orderby);
public Collection<E> getAll(Predictate<E> where, Comparator<E> orderby, Integer limit); public Collection<E> getAll(Predictate<E> where, Comparator<E> orderby, Integer limit);
public Collection<E> getAll(Predictate<E> where, Comparator<E> orderby, Integer limit, Integer offset); public Collection<E> getAll(Predictate<E> where, Comparator<E> orderby, Integer limit, Integer offset);
public Map<L, E> getId2entity();
public E get(Object oid);
public E get(Object oid, boolean creative);
public Map<E, L> getEntity2id();
public L getId(Object entity);
public L fixId(Object oid); public L fixId(Object oid);
// -------------------------------------------- // // -------------------------------------------- //