Class: Orchestrate::Collection::KeyValueList

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/orchestrate/collection.rb

Overview

An enumerator with boundaries for performing a KeyValue List query against a collection.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection, range = {}) ⇒ Object

Instantiate a new KeyValueList to enumerate over a collection

Parameters:

  • collection (Collection)

    the collection to enumerate over.

  • range (Hash) (defaults to: {})

    parameters for setting the boundaries of the enumeration.

Options Hash (range):

  • :begin (#to_s)

    The beginning of the range.

  • :begin_inclusive (true, false)

    Whether the begin key is inclusive or not.

  • :end (#to_s)

    The end of the range.

  • :end_inclusive (true, false)

    Whether the end key is inclusive or not.



257
258
259
260
261
# File 'lib/orchestrate/collection.rb', line 257

def initialize(collection, range={})
  @collection = collection
  @range = range
  range[:limit] ||= 100
end

Instance Attribute Details

#collectionCollection (readonly)

Returns The collection which this KeyValueList enumerates over.

Returns:

  • (Collection)

    The collection which this KeyValueList enumerates over.



244
245
246
# File 'lib/orchestrate/collection.rb', line 244

def collection
  @collection
end

#rangeHash (readonly)

Returns parameters for setting the boundaries of the enumeration.

Returns:

  • (Hash)

    parameters for setting the boundaries of the enumeration.



247
248
249
# File 'lib/orchestrate/collection.rb', line 247

def range
  @range
end

Instance Method Details

#after(start_key) ⇒ KeyValueList

Sets the exclusive start key for enumeration over the KeyValue items in the collection. Overwrites any value given to #start.

Parameters:

  • start_key (#to_s)

    The exclusive start of the key range.

Returns:



275
276
277
# File 'lib/orchestrate/collection.rb', line 275

def after(start_key)
  self.class.new(collection, range.merge({begin: start_key, begin_inclusive: false}))
end

#before(end_key) ⇒ KeyValueList

Sets the exclusive end key for enumeration over the KeyValue items in the collection. Overwrites any value given to #end.

Parameters:

  • end_key (#to_s)

    The exclusive end of the key range.

Returns:



283
284
285
# File 'lib/orchestrate/collection.rb', line 283

def before(end_key)
  self.class.new(collection, range.merge({end: end_key, end_inclusive: false}))
end

#eachObject #each {|key_value| ... } ⇒ Object

Iterates over each KeyValue item in the collection. Used as the basis for Enumerable methods. Items are provided in lexicographically sorted order by key name.

Examples:

keys = collection.after(:foo).take(20).map(&:key)
# returns the first 20 keys in the collection that occur after "foo"

Overloads:

  • #eachObject

    Returns Enumerator.

    Returns:

    • Enumerator

  • #each {|key_value| ... } ⇒ Object

    Yield Parameters:

Raises:



306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/orchestrate/collection.rb', line 306

def each
  params = {}
  if range[:begin]
    begin_key = range[:begin_inclusive] ? :start : :after
    params[begin_key] = range[:begin]
  end
  if range[:end]
    end_key = range[:end_inclusive] ? :end : :before
    params[end_key] = range[:end]
  end
  params[:limit] = range[:limit]
  @response ||= collection.perform(:list, params)
  return enum_for(:each) unless block_given?
  raise ResultsNotReady.new if collection.app.inside_parallel?
  loop do
    @response.results.each do |doc|
      yield KeyValue.from_listing(collection, doc, @response)
    end
    break unless @response.next_link
    @response = @response.next_results
  end
  @response = nil
end

#end(end_key) ⇒ KeyValueList

Sets the inclusive end key for enumeration over the KeyValue items in the collection. Overwrites any value given to #before.

Parameters:

  • end_key (#to_s)

    The inclusive end of the key range.

Returns:



291
292
293
# File 'lib/orchestrate/collection.rb', line 291

def end(end_key)
  self.class.new(collection, range.merge({end: end_key, end_inclusive: true}))
end

#lazyObject

Creates a Lazy Enumerator for the KeyValue list. If called inside the app's #in_parallel block, will prefetch results.



332
333
334
335
# File 'lib/orchestrate/collection.rb', line 332

def lazy
  return each.lazy if collection.app.inside_parallel?
  super
end

#start(start_key) ⇒ KeyValueList

Sets the inclusive start key for enumeration over the KeyValue items in the collection. Overwrites any value given to #after.

Parameters:

  • start_key (#to_s)

    The inclusive start of the key range.

Returns:



267
268
269
# File 'lib/orchestrate/collection.rb', line 267

def start(start_key)
  self.class.new(collection, range.merge({begin: start_key, begin_inclusive: true}))
end

#take(count) ⇒ Array

Returns the first n items. Equivalent to Enumerable#take. Sets the limit parameter on the query to Orchestrate, so we don't ask for more than is needed.

Parameters:

  • count (Integer)

    The number of items to limit to.

Returns:

  • (Array)


341
342
343
344
345
# File 'lib/orchestrate/collection.rb', line 341

def take(count)
  count = 1 if count < 1
  range[:limit] = count > 100 ? 100 : count
  super(count)
end