org.axonframework.eventstore.mongo
Class MongoEventStore

java.lang.Object
  extended by org.axonframework.eventstore.mongo.MongoEventStore
All Implemented Interfaces:
EventStore, EventStoreManagement, SnapshotEventStore

public class MongoEventStore
extends Object
implements SnapshotEventStore, EventStoreManagement

Implementation of the EventStore based on a MongoDB instance or replica set. Sharding and pairing are not explicitly supported.

Warning: This implementation is still in progress and may be subject to alterations. The implementation works, but has not been optimized to fully leverage MongoDB's features, yet.

Since:
0.7
Author:
Jettro Coenradie

Constructor Summary
MongoEventStore(EventSerializer eventSerializer, com.mongodb.Mongo mongo)
          Constructor that accepts an EventSerializer, the MongoTemplate and a string containing the testContext.
MongoEventStore(com.mongodb.Mongo mongo)
          Constructor that uses the default EventSerializer.
 
Method Summary
 void appendEvents(String type, DomainEventStream events)
          Append the events in the given stream to the event store.
 void appendSnapshotEvent(String type, DomainEvent snapshotEvent)
          Append the given snapshotEvent to the snapshot event log for the given type type.
 void ensureIndexes()
          Make sure an index is created on the collection that stores domain events
 DomainEventStream readEvents(String type, AggregateIdentifier identifier)
          Read the events of the aggregate identified by the given type and identifier that allow the current aggregate state to be rebuilt.
 void setDatabaseName(String databaseName)
          Sets the name of the database in which the event store should create the event collections.
 void setDomainEventsCollectionName(String domainEventsCollectionName)
          Sets the name of the collection where this event store should store domain events.
 void setSnapshotEventsCollectionName(String snapshotEventsCollectionName)
          Sets the name of the collection where this event store should store snapshot events.
 void visitEvents(EventVisitor visitor)
          Loads all events available in the event store and calls EventVisitor.doWithEvent(org.axonframework.domain.DomainEvent) for each event found.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MongoEventStore

public MongoEventStore(EventSerializer eventSerializer,
                       com.mongodb.Mongo mongo)
Constructor that accepts an EventSerializer, the MongoTemplate and a string containing the testContext. The TestContext can be Null. Provide true in case of the test context.

Parameters:
eventSerializer - Your own EventSerializer
mongo - Mongo instance to obtain the database and the collections.

MongoEventStore

public MongoEventStore(com.mongodb.Mongo mongo)
Constructor that uses the default EventSerializer.

Parameters:
mongo - Mongo instance to obtain the database and the collections.
Method Detail

ensureIndexes

@PostConstruct
public void ensureIndexes()
Make sure an index is created on the collection that stores domain events


appendEvents

public void appendEvents(String type,
                         DomainEventStream events)
Description copied from interface: EventStore
Append the events in the given stream to the event store.

Specified by:
appendEvents in interface EventStore
Parameters:
type - The type descriptor of the object to store
events - The event stream containing the events to store

readEvents

public DomainEventStream readEvents(String type,
                                    AggregateIdentifier identifier)
Description copied from interface: EventStore
Read the events of the aggregate identified by the given type and identifier that allow the current aggregate state to be rebuilt. Implementations may omit or replace events (e.g. by using snapshot events) from the stream for performance purposes.

Specified by:
readEvents in interface EventStore
Parameters:
type - The type descriptor of the object to retrieve
identifier - The unique aggregate identifier of the events to load
Returns:
an event stream containing the events of the aggregate

appendSnapshotEvent

public void appendSnapshotEvent(String type,
                                DomainEvent snapshotEvent)
Description copied from interface: SnapshotEventStore
Append the given snapshotEvent to the snapshot event log for the given type type. The sequence number of the snapshotEvent must be equal to the sequence number of the last regular domain event that is included in the snapshot.

Note that the aggregate identifier and sequence number must be set on the DomainEvent. See DomainEvent.DomainEvent(long, org.axonframework.domain.AggregateIdentifier).

Specified by:
appendSnapshotEvent in interface SnapshotEventStore
Parameters:
type - The type of aggregate the event belongs to
snapshotEvent - The event summarizing one or more domain events for a specific aggregate.
See Also:
org.axonframework.domain.DomainEvent#DomainEvent(long, org.axonframework.domain.AggregateIdentifier))

visitEvents

public void visitEvents(EventVisitor visitor)
Description copied from interface: EventStoreManagement
Loads all events available in the event store and calls EventVisitor.doWithEvent(org.axonframework.domain.DomainEvent) for each event found. Events of a single aggregate are guaranteed to be ordered by their sequence number.

Implementations are encouraged, though not required, to supply events in the absolute chronological order.

Processing stops when the visitor throws an exception.

Specified by:
visitEvents in interface EventStoreManagement
Parameters:
visitor - The visitor the receives each loaded event

setDatabaseName

public void setDatabaseName(String databaseName)
Sets the name of the database in which the event store should create the event collections. Defaults to "axonframework". If no database with the given name exists, it is created.

Parameters:
databaseName - the name of the database where events should be stored

setDomainEventsCollectionName

public void setDomainEventsCollectionName(String domainEventsCollectionName)
Sets the name of the collection where this event store should store domain events. Defaults to "domainevents".

Note that you should not given this collection the same name as the snapshot events collection.

Parameters:
domainEventsCollectionName - The name of the collection that stores domain events.

setSnapshotEventsCollectionName

public void setSnapshotEventsCollectionName(String snapshotEventsCollectionName)
Sets the name of the collection where this event store should store snapshot events. Defaults to "snapshotevents".

Note that you should not given this collection the same name as the domain events collection.

Parameters:
snapshotEventsCollectionName - The name of the collection that stores snapshot events.


Copyright © 2011. All Rights Reserved.