11module RubyEventStore
2+
3+ # Used for building and executing the query specification.
24 class Specification
5+ # @private
6+ # @api private
37 NO_LIMIT = Object . new . freeze
8+ # @private
9+ # @api private
410 NO_BATCH = Object . new . freeze
511 DEFAULT_BATCH_SIZE = 100
612
@@ -35,18 +41,34 @@ def batched?
3541 end
3642 private_constant :Result
3743
44+ # @api private
45+ # @private
3846 attr_reader :result
3947
48+ # @api private
49+ # @private
4050 def initialize ( repository , mapper , result = Result . new ( :forward , :head , NO_LIMIT , Stream . new ( GLOBAL_STREAM ) , NO_BATCH ) )
4151 @mapper = mapper
4252 @repository = repository
4353 @result = result
4454 end
4555
56+ # Limits the query to certain stream.
57+ # {http://railseventstore.org/docs/read/ Find out more}.
58+ #
59+ # @param stream_name [String] name of the stream to get events from
60+ # @return [Specification]
4661 def stream ( stream_name )
4762 Specification . new ( repository , mapper , result . dup . tap { |r | r . stream = Stream . new ( stream_name ) } )
4863 end
4964
65+ # Limits the query to events before or after another event.
66+ # {http://railseventstore.org/docs/read/ Find out more}.
67+ #
68+ # @param start [:head, String] id of event to start reading from.
69+ # :head can mean the beginning or end of the stream, depending on the
70+ # #direction
71+ # @return [Specification]
5072 def from ( start )
5173 case start
5274 when Symbol
@@ -58,19 +80,38 @@ def from(start)
5880 Specification . new ( repository , mapper , result . dup . tap { |r | r . start = start } )
5981 end
6082
83+ # Sets the order of reading events to ascending (forward from the start).
84+ # {http://railseventstore.org/docs/read/ Find out more}.
85+ #
86+ # @return [Specification]
6187 def forward
6288 Specification . new ( repository , mapper , result . dup . tap { |r | r . direction = :forward } )
6389 end
6490
91+ # Sets the order of reading events to descending (backward from the start).
92+ # {http://railseventstore.org/docs/read/ Find out more}.
93+ #
94+ # @return [Specification]
6595 def backward
6696 Specification . new ( repository , mapper , result . dup . tap { |r | r . direction = :backward } )
6797 end
6898
99+ # Limits the query to specified number of events.
100+ # {http://railseventstore.org/docs/read/ Find out more}.
101+ #
102+ # @param count [Integer] maximal number of events to retrieve
103+ # @return [Specification]
69104 def limit ( count )
70105 raise InvalidPageSize unless count && count > 0
71106 Specification . new ( repository , mapper , result . dup . tap { |r | r . count = count } )
72107 end
73108
109+ # Executes the query based on the specification built up to this point.
110+ # Yields each batch of records that was retrieved from the store.
111+ # {http://railseventstore.org/docs/read/ Find out more}.
112+ #
113+ # @yield [Array<Event, Proto>] batch of events
114+ # @return [Enumerator, nil] Enumerator is returned when block not given
74115 def each_batch
75116 return to_enum ( :each_batch ) unless block_given?
76117
@@ -80,6 +121,12 @@ def each_batch
80121 end
81122 end
82123
124+ # Executes the query based on the specification built up to this point.
125+ # Yields events read from the store if block given. Otherwise, returns enumerable collection.
126+ # {http://railseventstore.org/docs/read/ Find out more}.
127+ #
128+ # @yield [Event, Proto] event
129+ # @return [Enumerator, nil] Enumerator is returned when block not given
83130 def each
84131 return to_enum unless block_given?
85132
@@ -88,6 +135,19 @@ def each
88135 end
89136 end
90137
138+ # Specifies that events should be obtained in batches.
139+ # {http://railseventstore.org/docs/read/ Find out more}.
140+ #
141+ # Looping through a collection of events from the store
142+ # can be inefficient since it will try to instantiate all
143+ # the events at once.
144+ #
145+ # In that case, batch processing methods allow you to work
146+ # with the records in batches, thereby greatly reducing
147+ # memory consumption.
148+ #
149+ # @param batch_size [Integer] number of events to read in a single batch
150+ # @return [Specification]
91151 def in_batches ( batch_size = DEFAULT_BATCH_SIZE )
92152 Specification . new ( repository , mapper , result . dup . tap { |r | r . batch_size = batch_size } )
93153 end
0 commit comments