Entity store
Karl Wettin <[email protected]>
| Newsgroups | gmane.comp.java.prevayler |
|---|---|
| Message-ID | <[email protected]> |
I've tried to factor out this entity object management I wrote for a
project of mine that use Prevayler.
http://github.com/karlwettin/kodapan-entity-store
This is basically a thick layer around the two follwoing classes:
/** All identifiable objects stored in a primary index must implement
this class */
interface EntityObject {
String getId();
}
class EntityStore {
/** One primary index per class and superclass assignable from
EntityObject.
Makes it possible to iterate all instances of any entity class.
All entity objects are thus available as key EntityObject.class
*/
Map<Class<? extends EntityObject>, Map<String, EntityObject>>
primaryIndices;
Map<String, Map<Object, Set<EntityObject>>> secondaryIndices;
}
Caveat emptor:
* I ended up rewriting this API from scratch. Even though there are
test cases for most of the stuff I have not yet been running this in a
live environment so I'm sure there are bugs lurking in the darkness.
* Forced string identity. (This might change to a byte[].)
* Identities must be unique within the application, eg a UUID. (Comes
with DeterministicUIDHandler)
* Identities must be set before putting an entity to the store.
* Secondary indices will not be updated unless you re-put the entity
in the store or rebuild the secondary index.
There is virtually no documentation but hopefully the API is simple
enough:
class EntityStore {
EntityObject put(EntityObject entity);
EntityObject get(String id);
boolean remove(String id);
PrimaryIndex<EntityType> getPrimaryIndex(Class<EntityType>
entityType);
SecondaryIndex getSecondaryIndex(String name);
boolean registerSecondaryIndex(String name, SecondaryIndex
secondaryIndex);
}
class PrimaryIndex<EntityType extends EntityObject> {
EntityType put(EntityType entity);
EntityType get(String id);
boolean remove(String id);
Map<String, EntityType> getEntitiesById();
}
abstract class SecondaryIndex<EntityType extends EntityObject> {
/** There are simple reflection based implementations */
abstract Object keyFactory(EntityType entity);
void put(EntityType entity);
boolean remove(EntityType entity);
EntityType get(Object... parameters);
Set<EntityType> list(Object... parameters);
}
http://github.com/karlwettin/kodapan-entity-store/blob/master/src/test/java/se/kodapan/index/TestEntityStore.java
The test domain model can be viewed as UML class diagram here:
http://github.com/karlwettin/kodapan-entity-store/raw/master/src/test/java/se/kodapan/index/domain/doc-files/classdiagram.pdf
Beans can optionally be augmented with UML class diagram syntax
annotations @BinaryAssociationEnd and @BinarAssociationClassEnd on
fields, getters or setters to have the entity store automatically
decouple instance from other ends and remove composite parts when the
instance is removed. Compare with relational annotations for
casacading, many to one and what not, but using OO syntax. Decoupling
qualified binary associations (Maps) has not yet been implemented.
I'm new to git, just noticed I've managed to push some old CVS and SVN
directories to the repo so there might be other problems related to
that too, sorry.
I hope you find this a useful package but any comments or reports are
most welcome!
This depends on my common package <http://github.com/karlwettin/kodapan-common
> for annotations, reflection and collections. mvn install. I think
some of you might question the SerializableBean class and want to
remove it. It's work in progress for a more refactor friendly java
serialization.
karl
------------------------------------------------------------------------------
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
_______________________________________________
To unsubscribe go to the end of this page: http://lists.sourceforge.net/lists/listinfo/prevayler-discussion
_______________________________________________
"Databases in Memoriam" -- http://www.prevayler.org