org.axonframework.mongo3.eventstore
Class MongoEventStore

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

public class MongoEventStore
extends Object
implements SnapshotEventStore, EventStoreManagement, UpcasterAware, PartialStreamSupport

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

This event store implementation needs a serializer as well as a MongoTemplate to interact with the mongo database.

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:
2.0 (in incubator since 0.7)
Author:
Jettro Coenradie

Constructor Summary
MongoEventStore(MongoTemplate mongo)
          Constructor that uses the default Serializer.
MongoEventStore(MongoTemplate mongoTemplate, Serializer eventSerializer, StorageStrategy storageStrategy)
          Initialize the mongo event store with given mongoTemplate, eventSerializer and storageStrategy.
MongoEventStore(MongoTemplate mongoTemplate, StorageStrategy storageStrategy)
          Constructor that accepts a MongoTemplate and a custom StorageStrategy.
MongoEventStore(Serializer eventSerializer, MongoTemplate mongo)
          Constructor that accepts a Serializer and the MongoTemplate.
 
Method Summary
 void appendEvents(String type, DomainEventStream events)
          Append the events in the given stream to the event store.
 void appendSnapshotEvent(String type, DomainEventMessage 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.
 MongoCriteriaBuilder newCriteriaBuilder()
          Returns a CriteriaBuilder that allows the construction of criteria for this EventStore implementation
 DomainEventStream readEvents(String type, Object identifier)
          Read the events of the aggregate identified by the given type and identifier that allow the current aggregate state to be rebuilt.
 DomainEventStream readEvents(String type, Object identifier, long firstSequenceNumber)
          Returns a Stream containing events for the aggregate identified by the given type and identifier, starting at the event with the given firstSequenceNumber (included).
 DomainEventStream readEvents(String type, Object identifier, long firstSequenceNumber, long lastSequenceNumber)
          Returns a Stream containing events for the aggregate identified by the given type and identifier, starting at the event with the given firstSequenceNumber (included) up to and including the event with given lastSequenceNumber.
 void setUpcasterChain(UpcasterChain upcasterChain)
          Sets the UpcasterChain which allow older revisions of serialized objects to be deserialized.
 void visitEvents(Criteria criteria, EventVisitor visitor)
          Loads all events available in the event store that match the given criteria and calls EventVisitor.doWithEvent(org.axonframework.domain.DomainEventMessage) for each event found.
 void visitEvents(EventVisitor visitor)
          Loads all events available in the event store and calls EventVisitor.doWithEvent(org.axonframework.domain.DomainEventMessage) 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(Serializer eventSerializer,
                       MongoTemplate mongo)
Constructor that accepts a Serializer and the MongoTemplate. A Document-Per-Event storage strategy is used, causing each event to be stored in a separate Mongo Document.

Note: the SerializedType of Message Meta Data is not stored. Upon retrieval, it is set to the default value (name = "org.axonframework.domain.MetaData", revision = null). See SerializedMetaData.isSerializedMetaData(org.axonframework.serializer.SerializedObject)

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

MongoEventStore

public MongoEventStore(MongoTemplate mongo)
Constructor that uses the default Serializer. A Document-Per-Event storage strategy is used, causing each event to be stored in a separate Mongo Document.

Parameters:
mongo - MongoTemplate instance to obtain the database and the collections.

MongoEventStore

public MongoEventStore(MongoTemplate mongoTemplate,
                       StorageStrategy storageStrategy)
Constructor that accepts a MongoTemplate and a custom StorageStrategy.

Parameters:
mongoTemplate - The template giving access to the required collections
storageStrategy - The strategy for storing and retrieving events from the collections

MongoEventStore

public MongoEventStore(MongoTemplate mongoTemplate,
                       Serializer eventSerializer,
                       StorageStrategy storageStrategy)
Initialize the mongo event store with given mongoTemplate, eventSerializer and storageStrategy.

Parameters:
mongoTemplate - The template giving access to the required collections
eventSerializer - The serializer to serialize events with
storageStrategy - The strategy for storing and retrieving events from 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,
                                    Object 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

readEvents

public DomainEventStream readEvents(String type,
                                    Object identifier,
                                    long firstSequenceNumber)
Description copied from interface: PartialStreamSupport
Returns a Stream containing events for the aggregate identified by the given type and identifier, starting at the event with the given firstSequenceNumber (included).

The returned stream will not contain any snapshot events.

Specified by:
readEvents in interface PartialStreamSupport
Parameters:
type - The type identifier of the aggregate
identifier - The identifier of the aggregate
firstSequenceNumber - The sequence number of the first event to find
Returns:
a Stream containing events for the given aggregate, starting at the given first sequence number

readEvents

public DomainEventStream readEvents(String type,
                                    Object identifier,
                                    long firstSequenceNumber,
                                    long lastSequenceNumber)
Description copied from interface: PartialStreamSupport
Returns a Stream containing events for the aggregate identified by the given type and identifier, starting at the event with the given firstSequenceNumber (included) up to and including the event with given lastSequenceNumber. If no event with given lastSequenceNumber exists, the returned stream will simply read until the end of the aggregate's events.

The returned stream will not contain any snapshot events.

Specified by:
readEvents in interface PartialStreamSupport
Parameters:
type - The type identifier of the aggregate
identifier - The identifier of the aggregate
firstSequenceNumber - The sequence number of the first event to find
lastSequenceNumber - The sequence number of the last event in the stream
Returns:
a Stream containing events for the given aggregate, starting at the given first sequence number

appendSnapshotEvent

public void appendSnapshotEvent(String type,
                                DomainEventMessage 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.

Implementations may choose to prune snapshots upon appending a new snapshot, in order to minimize storage space.

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.

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.DomainEventMessage) 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

visitEvents

public void visitEvents(Criteria criteria,
                        EventVisitor visitor)
Description copied from interface: EventStoreManagement
Loads all events available in the event store that match the given criteria and calls EventVisitor.doWithEvent(org.axonframework.domain.DomainEventMessage) 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:
criteria - The criteria describing the events to select
visitor - The visitor the receives each loaded event
See Also:
EventStoreManagement.newCriteriaBuilder()

newCriteriaBuilder

public MongoCriteriaBuilder newCriteriaBuilder()
Description copied from interface: EventStoreManagement
Returns a CriteriaBuilder that allows the construction of criteria for this EventStore implementation

Specified by:
newCriteriaBuilder in interface EventStoreManagement
Returns:
a builder to create Criteria for this Event Store.
See Also:
EventStoreManagement.visitEvents(Criteria, org.axonframework.eventstore.EventVisitor)

setUpcasterChain

public void setUpcasterChain(UpcasterChain upcasterChain)
Description copied from interface: UpcasterAware
Sets the UpcasterChain which allow older revisions of serialized objects to be deserialized.

Specified by:
setUpcasterChain in interface UpcasterAware
Parameters:
upcasterChain - the upcaster chain providing the upcasting capabilities


Copyright © 2010-2016. All Rights Reserved.