Class: Activr::Storage::MongoDriver

Inherits:
Object
  • Object
show all
Defined in:
lib/activr/storage/mongo_driver.rb

Overview

Generic Mongodb driver

This is main interface with the underlying MongobDB driver, which can be either the official ‘mongo` driver or `moped`, the `mongoid` driver.

Instance Method Summary collapse

Constructor Details

#initializeMongoDriver

Returns a new instance of MongoDriver.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/activr/storage/mongo_driver.rb', line 20

def initialize
  # check settings
  raise "Missing setting :uri in config: #{self.config.inspect}" if self.config[:uri].blank?

  @collections = { }

  @kind = if defined?(::Moped)
    if defined?(::Moped::BSON)
      # moped < 2.0.0
      :moped_1
    else
      # moped driver
      :moped
    end
  elsif defined?(::Mongo::MongoClient)
    # mongo ruby driver < 2.0.0
    :mongo
  elsif defined?(::Mongo::Client)
    raise "Sorry, mongo gem >= 2.0 is not supported yet"
  else
    raise "Can't find any suitable mongodb driver: please install 'mongo' or 'moped' gem"
  end

  # Activr.logger.info("Using mongodb driver: #{@kind}")

  if @kind == :mongo
    uri = URI.parse(self.config[:uri])

    @db_name = uri.path[1..-1]
    raise "Missing database name in setting uri: #{config[:uri]}" if @db_name.blank?
  end
end

Instance Method Details

#activities_selector(options) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Compute selector for querying ‘activities` collection

Parameters:

  • options (Hash)

    Options when querying ‘activities` collection

Returns:

  • (Hash)

    The computed selector



383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# File 'lib/activr/storage/mongo_driver.rb', line 383

def activities_selector(options)
  result = { }

  # compute selector
  if options[:before]
    result['at'] ||= { }
    result['at']["$lt"] = options[:before]
  end

  if options[:after]
    result['at'] ||= { }
    result['at']["$gt"] = options[:after]
  end

  (options[:entities] || { }).each do |name, value|
    result[name.to_s] = value
  end

  if !options[:only].blank?
    result['kind'] ||= { }
    result['kind']['$in'] = options[:only].map(&:kind)
  end

  if !options[:except].blank?
    result['kind'] ||= { }
    result['kind']['$nin'] = options[:except].map(&:kind)
  end

  result
end

#activity_collectionMongo::Collection, Moped::Collection

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get handler for ‘activities` collection

Returns:

  • (Mongo::Collection, Moped::Collection)

    Collection handler



275
276
277
278
279
280
281
282
283
284
285
# File 'lib/activr/storage/mongo_driver.rb', line 275

def activity_collection
  @activity_collection ||= begin
    col_name = self.config[:activities_col]
    if col_name.nil?
      col_name = "activities"
      col_name = "#{self.config[:col_prefix]}_#{col_name}" unless self.config[:col_prefix].blank?
    end

    self.collection(col_name)
  end
end

#add_activity_index(index, options = { }) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Add index for activities

Parameters:

  • index (String, Array<String>)

    Field or array of fields

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

    Options hash

Options Hash (options):

  • :background (Boolean)

    Background indexing ? (default: ‘true`)

  • :sparse (Boolean)

    Is it a sparse index ? (default: ‘false`)

Returns:

  • (String)

    Index created



450
451
452
453
454
455
# File 'lib/activr/storage/mongo_driver.rb', line 450

def add_activity_index(index, options = { })
  index = index.is_a?(Array) ? index : [ index ]
  index_spec = index.map{ |field| [ field, 1 ] }

  self.add_index(self.activity_collection, index_spec, options)
end

#add_index(col, index_spec, options = { }) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Add index to given collection

Parameters:

  • col (Mongo::Collection, Moped::Collection)

    Collection handler

  • index_spec (Array)

    Array of [ String, Integer ] tuplets with String being a field to index and Integer the order (‘-1` of DESC and `1` for ASC)

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

    Options hash

Options Hash (options):

  • :background (Boolean)

    Background indexing ? (default: ‘true`)

  • :sparse (Boolean)

    Is it a sparse index ? (default: ‘false`)

Returns:

  • (String)

    Index created



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/activr/storage/mongo_driver.rb', line 211

def add_index(col, index_spec, options = { })
  options = {
    :background => true,
    :sparse     => false,
  }.merge(options)

  case @kind
  when :moped_1, :moped
    index_spec = index_spec.inject(ActiveSupport::OrderedHash.new) do |memo, field_spec|
      memo[field_spec[0]] = field_spec[1]
      memo
    end

    col.indexes.create(index_spec, options)

    index_spec

  when :mongo
    col.create_index(index_spec, options)
  end
end

#add_timeline_index(timeline_kind, index, options = { }) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Add index for timeline entries

Parameters:

  • timeline_kind (String)

    Timeline kind

  • index (String, Array<String>)

    Field or array of fields

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

    Options hash

Options Hash (options):

  • :background (Boolean)

    Background indexing ? (default: ‘true`)

  • :sparse (Boolean)

    Is it a sparse index ? (default: ‘false`)

Returns:

  • (String)

    Index created



571
572
573
574
575
576
# File 'lib/activr/storage/mongo_driver.rb', line 571

def add_timeline_index(timeline_kind, index, options = { })
  index = index.is_a?(Array) ? index : [ index ]
  index_spec = index.map{ |field| [ field, 1 ] }

  self.add_index(self.timeline_collection(timeline_kind), index_spec, options)
end

#collection(col_name) ⇒ Mongo::Collection, Moped::Collection

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Mongodb collection

Parameters:

  • col_name (String)

    Collection name

Returns:

  • (Mongo::Collection, Moped::Collection)

    Collection handler



84
85
86
87
88
89
90
91
# File 'lib/activr/storage/mongo_driver.rb', line 84

def collection(col_name)
  case @kind
  when :moped_1, :moped
    self.conn[col_name]
  when :mongo
    self.conn.db(@db_name).collection(col_name)
  end
end

#confighash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

MongoDB config

Returns:

  • (hash)

    Config



58
59
60
# File 'lib/activr/storage/mongo_driver.rb', line 58

def config
  Activr.config.mongodb
end

#connMongo::MongoClient, ...

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Mongodb connection/session

Returns:

  • (Mongo::MongoClient, Mongo::MongoReplicaSetClient, Moped::Session)

    Connection handler



67
68
69
70
71
72
73
74
75
76
# File 'lib/activr/storage/mongo_driver.rb', line 67

def conn
  @conn ||= begin
    case @kind
    when :moped_1, :moped
      ::Moped::Session.connect(self.config[:uri])
    when :mongo
      ::Mongo::MongoClient.from_uri(self.config[:uri])
    end
  end
end

#count(col, selector) ⇒ Integer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Count documents in given collection

Parameters:

  • col (Mongo::Collection, Moped::Collection)

    Collection handler

  • selector (Hash)

    Selector hash

Returns:

  • (Integer)

    Number of documents in collections that satisfy given selector



179
180
181
182
183
184
# File 'lib/activr/storage/mongo_driver.rb', line 179

def count(col, selector)
  case @kind
  when :moped_1, :moped, :mongo
    col.find(selector).count()
  end
end

#count_activities(options = { }) ⇒ Integer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

If you use one of options selectors then you have to setup corresponding indexes in database.

Count number of activities

Parameters:

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

    Options hash

Options Hash (options):

  • :before (Time)

    Find activities generated before that datetime (excluding)

  • :after (Time)

    Find activities generated after that datetime (excluding)

  • :entities (Hash{Sym=>String})

    Filter by entities values (empty means ‘all values’)

  • :only (Array<Class>)

    Find only these activities

  • :except (Array<Class>)

    Skip these activities

Returns:

  • (Integer)

    Number of activities



427
428
429
430
431
# File 'lib/activr/storage/mongo_driver.rb', line 427

def count_activities(options = { })
  selector = options[:mongo_selector] || self.activities_selector(options)

  self.count(self.activity_collection, selector)
end

#count_timeline_entries(timeline_kind, recipient_id, options = { }) ⇒ Integer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Count number of timeline entry documents

Parameters:

  • timeline_kind (String)

    Timeline kind

  • recipient_id (String, BSON::ObjectId, Moped::BSON::ObjectId)

    Recipient id

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

    Options hash

Returns:

  • (Integer)

    Number of documents in given timeline



538
539
540
541
542
# File 'lib/activr/storage/mongo_driver.rb', line 538

def count_timeline_entries(timeline_kind, recipient_id, options = { })
  selector = options[:mongo_selector] || self.timeline_selector(timeline_kind, recipient_id, options)

  self.count(self.timeline_collection(timeline_kind), selector)
end

#create_indexes {|String| ... } ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Ensure all necessary indexes

Yields:

  • (String)

    Created index name



586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
# File 'lib/activr/storage/mongo_driver.rb', line 586

def create_indexes
  # Create indexes on 'activities' collection for models that includes Activr::Entity::ModelMixin
  #
  # eg: activities
  #   [['actor', Mongo::ASCENDING], ['at', Mongo::ASCENDING]]
  #   [['album', Mongo::ASCENDING], ['at', Mongo::ASCENDING]]
  #   [['picture', Mongo::ASCENDING], ['at', Mongo::ASCENDING]]
  Activr.registry.models.each do |model_class|
    if !model_class.activr_entity_settings[:feed_index]
      # @todo Output a warning to remove the index if it exists
    else
      fields = [ model_class.activr_entity_feed_actual_name.to_s, 'at' ]

      index_name = self.add_activity_index(fields)
      yield("activity / #{index_name}") if block_given?
    end
  end

  # Create indexes on '*_timelines' collections for defined timeline classes
  #
  # eg: user_news_feed_timelines
  #   [['rcpt', Mongo::ASCENDING], ['activity.at', Mongo::ASCENDING]]
  Activr.registry.timelines.each do |timeline_kind, timeline_class|
    fields = [ 'rcpt', 'activity.at' ]

    index_name = self.add_timeline_index(timeline_kind, fields)
    yield("#{timeline_kind} timeline / #{index_name}") if block_given?
  end

  # Create sparse indexes to remove activities and timeline entries when entity is deleted
  #
  # eg: activities
  #   [['actor', Mongo::ASCENDING]], :sparse => true
  #
  # eg: user_news_feed_timelines
  #   [['activity.actor', Mongo::ASCENDING]], :sparse => true
  #   [['activity.album', Mongo::ASCENDING]], :sparse => true
  #   [['activity.picture', Mongo::ASCENDING]], :sparse => true
  Activr.registry.models.each do |model_class|
    if model_class.activr_entity_settings[:deletable]
      # create sparse index on `activities`
      Activr.registry.activity_entities_for_model(model_class).each do |entity_name|
        # if entity activity feed is enabled and this is the entity name used to fetch that feed then we can use the existing index...
        if !model_class.activr_entity_settings[:feed_index] || (entity_name != model_class.activr_entity_feed_actual_name)
          # ... else we create an index
          index_name = self.add_activity_index(entity_name.to_s, :sparse => true)
          yield("activity / #{index_name}") if block_given?
        end
      end

      # create sparse index on timeline classes where that entity can be present
      Activr.registry.timeline_entities_for_model(model_class).each do |timeline_class, entities|
        entities.each do |entity_name|
          index_name = self.add_timeline_index(timeline_class.kind, "activity.#{entity_name}", :sparse => true)
          yield("#{timeline_class.kind} timeline / #{index_name}") if block_given?
        end
      end
    end
  end
end

#delete(col, selector) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Delete documents in given collection

Parameters:

  • col (Mongo::Collection, Moped::Collection)

    Collection handler

  • selector (Hash)

    Selector hash



192
193
194
195
196
197
198
199
# File 'lib/activr/storage/mongo_driver.rb', line 192

def delete(col, selector)
  case @kind
  when :moped_1, :moped
    col.find(selector).remove_all
  when :mongo
    col.remove(selector)
  end
end

#delete_activities(options = { }) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



436
437
438
439
440
# File 'lib/activr/storage/mongo_driver.rb', line 436

def delete_activities(options = { })
  selector = options[:mongo_selector] || self.activities_selector(options)

  self.delete(self.activity_collection, selector)
end

#delete_timeline_entries(timeline_kind, recipient_id, options = { }) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Delete timeline entry documents

WARNING: If recipient_id is ‘nil` then documents are deleted for ALL recipients

Parameters:

  • timeline_kind (String)

    Timeline kind

  • recipient_id (String, BSON::ObjectId, Moped::BSON::ObjectId, nil)

    Recipient id

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

    Options hash



553
554
555
556
557
558
559
560
# File 'lib/activr/storage/mongo_driver.rb', line 553

def delete_timeline_entries(timeline_kind, recipient_id, options = { })
  selector = options[:mongo_selector] || self.timeline_selector(timeline_kind, recipient_id, options)

  # "end of the world" check
  raise "Deleting everything is not the solution" if selector.blank?

  self.delete(self.timeline_collection(timeline_kind), selector)
end

#drop_indexes(col) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Drop all indexes for given collection

Parameters:

  • col (Mongo::Collection, Moped::Collection)

    Collection handler



260
261
262
263
264
265
266
267
268
# File 'lib/activr/storage/mongo_driver.rb', line 260

def drop_indexes(col)
  case @kind
  when :moped_1, :moped
    col.indexes.drop

  when :mongo
    col.drop_indexes
  end
end

#find(col, selector, limit, skip, sort_field = nil) ⇒ Enumerable

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Find documents in given collection

Parameters:

  • col (Mongo::Collection, Moped::Collection)

    Collection handler

  • selector (Hash)

    Selector hash

  • limit (Integer)

    Maximum number of documents to find

  • skip (Integer)

    Number of documents to skip

  • sort_field (Symbol, String) (defaults to: nil)

    The field to use to sort documents in descending order

Returns:

  • (Enumerable)

    An enumerable on found documents



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/activr/storage/mongo_driver.rb', line 151

def find(col, selector, limit, skip, sort_field = nil)
  case @kind
  when :moped_1, :moped
    result = col.find(selector).skip(skip).limit(limit)
    result.sort(sort_field => -1) if sort_field
    result
  when :mongo
    # compute options hash
    options = {
      :limit => limit,
      :skip  => skip,
    }

    options[:sort] = [ sort_field, ::Mongo::DESCENDING ] if sort_field

    options[:batch_size] = 100 if (limit > 100)

    col.find(selector, options)
  end
end

#find_activities(limit, options = { }) ⇒ Array<Activity>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

If you use others selectors then ‘limit’ argument and ‘skip’ option then you have to setup corresponding indexes in database.

Find latest activities

Parameters:

  • limit (Integer)

    Max number of activities to find

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

    Options hash

Options Hash (options):

  • :skip (Integer)

    Number of activities to skip (default: 0)

  • :before (Time)

    Find activities generated before that datetime (excluding)

  • :after (Time)

    Find activities generated after that datetime (excluding)

  • :entities (Hash{Sym=>String})

    Filter by entities values (empty means ‘all values’)

  • :only (Array<Class>)

    Find only these activities

  • :except (Array<Class>)

    Skip these activities

Returns:

  • (Array<Activity>)

    An array of activities



417
418
419
420
421
422
# File 'lib/activr/storage/mongo_driver.rb', line 417

def find_activities(limit, options = { })
  selector   = options[:mongo_selector]   || self.activities_selector(options)
  sort_field = options[:mongo_sort_field] || 'at'

  self.find(self.activity_collection, selector, limit, options[:skip], sort_field)
end

#find_activity(activity_id) ⇒ Hash, ...

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Find an activity document

Parameters:

  • activity_id (BSON::ObjectId, Moped::BSON::ObjectId)

    The activity id

Returns:

  • (Hash, OrderedHash, Nil)

    Activity document



373
374
375
# File 'lib/activr/storage/mongo_driver.rb', line 373

def find_activity(activity_id)
  self.find_one(self.activity_collection, { '_id' => activity_id })
end

#find_one(col, selector) ⇒ Hash, ...

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Find a document by id

Parameters:

  • col (Mongo::Collection, Moped::Collection)

    Collection handler

  • selector (Hash)

    Selector hash

Returns:

  • (Hash, OrderedHash, Nil)

    Document



132
133
134
135
136
137
138
139
# File 'lib/activr/storage/mongo_driver.rb', line 132

def find_one(col, selector)
  case @kind
  when :moped_1, :moped
    col.find(selector).one
  when :mongo
    col.find_one(selector)
  end
end

#find_timeline_entries(timeline_kind, recipient_id, limit, options = { }) ⇒ Array<Hash>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Find several timeline entry documents

Parameters:

  • timeline_kind (String)

    Timeline kind

  • recipient_id (String, BSON::ObjectId, Moped::BSON::ObjectId)

    Recipient id

  • limit (Integer)

    Max number of entries to find

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

    Options hash

Returns:

  • (Array<Hash>)

    An array of timeline entry documents



523
524
525
526
527
528
# File 'lib/activr/storage/mongo_driver.rb', line 523

def find_timeline_entries(timeline_kind, recipient_id, limit, options = { })
  selector   = options[:mongo_selector]   || self.timeline_selector(timeline_kind, recipient_id, options)
  sort_field = options[:mongo_sort_field] || 'activity.at'

  self.find(self.timeline_collection(timeline_kind), selector, limit, options[:skip], sort_field)
end

#find_timeline_entry(timeline_kind, tl_entry_id) ⇒ Hash, ...

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Find a timeline entry document

Parameters:

  • timeline_kind (String)

    Timeline kind

  • tl_entry_id (BSON::ObjectId, Moped::BSON::ObjectId)

    Timeline entry document id

Returns:

  • (Hash, OrderedHash, Nil)

    Timeline entry document



479
480
481
# File 'lib/activr/storage/mongo_driver.rb', line 479

def find_timeline_entry(timeline_kind, tl_entry_id)
  self.find_one(self.timeline_collection(timeline_kind), { '_id' => tl_entry_id })
end

#indexes(col) ⇒ Array<String>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get all indexes for given collection

Parameters:

  • col (Mongo::Collection, Moped::Collection)

    Collection handler

Returns:

  • (Array<String>)

    Indexes names



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/activr/storage/mongo_driver.rb', line 239

def indexes(col)
  result = [ ]

  case @kind
  when :moped_1, :moped
    col.indexes.each do |index_spec|
      result << index_spec["name"]
    end

  when :mongo
    result = col.index_information.keys
  end

  result
end

#insert(col, doc) ⇒ BSON::ObjectId, Moped::BSON::ObjectId

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Insert a document into given collection

Parameters:

  • col (Mongo::Collection, Moped::Collection)

    Collection handler

  • doc (Hash)

    Document hash to insert

Returns:

  • (BSON::ObjectId, Moped::BSON::ObjectId)

    Inserted document id



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/activr/storage/mongo_driver.rb', line 100

def insert(col, doc)
  case @kind
  when :moped_1, :moped
    doc_id = doc[:_id] || doc['_id']
    if doc_id.nil?
      doc_id = case @kind
      when :moped_1
        # Moped < 2.0.0 uses a custom BSON implementation
        ::Moped::BSON::ObjectId.new
      when :moped
        # Moped >= 2.0.0 uses bson gem
        ::BSON::ObjectId.new
      end

      doc['_id'] = doc_id
    end

    col.insert(doc)

    doc_id
  when :mongo
    col.insert(doc)
  end
end

#insert_activity(activity_hash) ⇒ BSON::ObjectId, Moped::BSON::ObjectId

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Insert an activity document

Parameters:

  • activity_hash (Hash)

    Activity document to insert

Returns:

  • (BSON::ObjectId, Moped::BSON::ObjectId)

    Inserted activity id



363
364
365
# File 'lib/activr/storage/mongo_driver.rb', line 363

def insert_activity(activity_hash)
  self.insert(self.activity_collection, activity_hash)
end

#insert_timeline_entry(timeline_kind, timeline_entry_hash) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Insert a timeline entry document

Parameters:

  • timeline_kind (String)

    Timeline kind

  • timeline_entry_hash (Hash)

    Timeline entry document to insert



468
469
470
# File 'lib/activr/storage/mongo_driver.rb', line 468

def insert_timeline_entry(timeline_kind, timeline_entry_hash)
  self.insert(self.timeline_collection(timeline_kind), timeline_entry_hash)
end

#serialized_id?(doc_id) ⇒ true, false

Is it a serialized document id (ie. with format { ‘$oid’ => … })

Returns:

  • (true, false)


324
325
326
# File 'lib/activr/storage/mongo_driver.rb', line 324

def serialized_id?(doc_id)
  doc_id.is_a?(Hash) && !doc_id['$oid'].blank?
end

#timeline_collection(kind) ⇒ Mongo::Collection, Moped::Collection

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get handler for a ‘<kind>_timelines` collection

Parameters:

  • kind (String)

    Timeline kind

Returns:

  • (Mongo::Collection, Moped::Collection)

    Collection handler



293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/activr/storage/mongo_driver.rb', line 293

def timeline_collection(kind)
  @timeline_collection ||= { }
  @timeline_collection[kind] ||= begin
    col_name = self.config[:timelines_col]
    if col_name.nil?
      col_name = "#{kind}_timelines"
      col_name = "#{self.config[:col_prefix]}_#{col_name}" unless self.config[:col_prefix].blank?
    end

    self.collection(col_name)
  end
end

#timeline_selector(timeline_kind, recipient_id, options = { }) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Compute selector for querying a ‘*_timelines` collection

Parameters:

  • timeline_kind (String)

    Timeline kind

  • recipient_id (String, BSON::ObjectId, Moped::BSON::ObjectId)

    Recipient id

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

    Options hash

Returns:

  • (Hash)

    The computed selector



491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
# File 'lib/activr/storage/mongo_driver.rb', line 491

def timeline_selector(timeline_kind, recipient_id, options = { })
  result = { }

  # compute selector
  result['rcpt'] = recipient_id unless recipient_id.nil?

  if options[:before]
    result['activity.at'] = { "$lt" => options[:before] }
  end

  (options[:entities] || { }).each do |name, value|
    result["activity.#{name}"] = value
  end

  if !options[:only].blank?
    result['$or'] = options[:only].map do |route|
      { 'routing' => route.routing_kind, 'activity.kind' => route.activity_class.kind }
    end
  end

  result
end

#unserialize_id(doc_id) ⇒ BSON::ObjectId, Moped::BSON::ObjectId

Unserialize a document id

Parameters:

  • doc_id (String, Hash)

    Document id

Returns:

  • (BSON::ObjectId, Moped::BSON::ObjectId)

    Unserialized document id



332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/activr/storage/mongo_driver.rb', line 332

def unserialize_id(doc_id)
  # get string representation
  doc_id = self.serialized_id?(doc_id) ? doc_id['$oid'] : doc_id

  if @kind == :moped_1
    # Moped < 2.0.0 uses a custom BSON implementation
    if doc_id.is_a?(::Moped::BSON::ObjectId)
      doc_id
    else
      ::Moped::BSON::ObjectId(doc_id)
    end
  else
    if doc_id.is_a?(::BSON::ObjectId)
      doc_id
    else
      ::BSON::ObjectId.from_string(doc_id)
    end
  end
end

#valid_id?(doc_id) ⇒ true, false

Is it a valid document id

Parameters:

  • doc_id (Object)

    Document id to check

Returns:

  • (true, false)


312
313
314
315
316
317
318
319
# File 'lib/activr/storage/mongo_driver.rb', line 312

def valid_id?(doc_id)
  case @kind
  when :moped_1
    doc_id.is_a?(String) || doc_id.is_a?(::Moped::BSON::ObjectId)
  when :mongo, :moped
    doc_id.is_a?(String) || doc_id.is_a?(::BSON::ObjectId)
  end
end