Class: Trifle::Stats::Driver::Mongo

Inherits:
Object
  • Object
show all
Includes:
Mixins::Packer
Defined in:
lib/trifle/stats/driver/mongo.rb

Overview

rubocop:disable Metrics/ClassLength

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixins::Packer

included

Constructor Details

#initialize(client, collection_name: 'trifle_stats', joined_identifier: :full, expire_after: nil, system_tracking: true, bulk_write: true) ⇒ Mongo

rubocop:disable Layout/LineLength, Metrics/ParameterLists



12
13
14
15
16
17
18
19
20
# File 'lib/trifle/stats/driver/mongo.rb', line 12

def initialize(client, collection_name: 'trifle_stats', joined_identifier: :full, expire_after: nil, system_tracking: true, bulk_write: true) # rubocop:disable Layout/LineLength, Metrics/ParameterLists
  @client = client
  @collection_name = collection_name
  @joined_identifier = self.class.normalize_joined_identifier(joined_identifier)
  @expire_after = expire_after
  @system_tracking = system_tracking
  @bulk_write = bulk_write
  @separator = '::'
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



10
11
12
# File 'lib/trifle/stats/driver/mongo.rb', line 10

def client
  @client
end

#collection_nameObject

Returns the value of attribute collection_name.



10
11
12
# File 'lib/trifle/stats/driver/mongo.rb', line 10

def collection_name
  @collection_name
end

#separatorObject (readonly)

Returns the value of attribute separator.



46
47
48
# File 'lib/trifle/stats/driver/mongo.rb', line 46

def separator
  @separator
end

Class Method Details

.normalize_joined_identifier(value) ⇒ Object



196
197
198
199
200
201
202
203
# File 'lib/trifle/stats/driver/mongo.rb', line 196

def self.normalize_joined_identifier(value)
  case value
  when nil, :full, 'full', :partial, 'partial'
    value.nil? ? nil : value.to_sym
  else
    raise ArgumentError, 'joined_identifier must be nil, :full, "full", :partial, or "partial"'
  end
end

.setup!(client, collection_name: 'trifle_stats', joined_identifier: :full, expire_after: nil) ⇒ Object

rubocop:disable Metrics/MethodLength



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/trifle/stats/driver/mongo.rb', line 22

def self.setup!(client, collection_name: 'trifle_stats', joined_identifier: :full, expire_after: nil) # rubocop:disable Metrics/MethodLength
  collection = client[collection_name]
  collection.create
  identifier_mode = normalize_joined_identifier(joined_identifier)
  case identifier_mode
  when :full
    collection.indexes.create_one({ key: 1 }, unique: true)
  when :partial
    collection.indexes.create_one({ key: 1, at: -1 }, unique: true)
  else
    collection.indexes.create_one({ key: 1, granularity: 1, at: -1 }, unique: true)
  end
  collection.indexes.create_one({ expire_at: 1 }, expire_after_seconds: 0) if expire_after
end

Instance Method Details

#build_update(operation, data:, expire_at: nil) ⇒ Object



160
161
162
163
164
165
166
167
168
169
# File 'lib/trifle/stats/driver/mongo.rb', line 160

def build_update(operation, data:, expire_at: nil)
  # Merge if $set and $set
  update_data = operation == '$set' && expire_at ? data.merge(expire_at: expire_at) : data

  # Add if $inc and $set
  {
    operation => update_data,
    **(operation != '$set' && expire_at ? { '$set' => { expire_at: expire_at } } : {})
  }
end

#descriptionObject



37
38
39
40
41
42
43
44
# File 'lib/trifle/stats/driver/mongo.rb', line 37

def description
  mode = if @joined_identifier == :full
           'J'
         else
           @joined_identifier == :partial ? 'P' : 'S'
         end
  "#{self.class.name}(#{mode})"
end

#get(keys:) ⇒ Object

rubocop:disable Metrics/AbcSize



171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/trifle/stats/driver/mongo.rb', line 171

def get(keys:) # rubocop:disable Metrics/AbcSize
  combinations = keys.map { |key| identifier_for(key) }
  data = collection.find('$or' => combinations)
  map = data.inject({}) do |o, d|
    o.merge(
      Nocturnal::Key.new(
        key: d['key'], granularity: d['granularity'], at: d['at']
      ).identifier(separator, @joined_identifier) => d['data']
    )
  end

  combinations.map { |combination| map[combination] || {} }
end

#inc(keys:, values:, count: 1, tracking_key: nil) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/trifle/stats/driver/mongo.rb', line 58

def inc(keys:, values:, count: 1, tracking_key: nil) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
  data = self.class.pack(hash: { data: values })

  if @bulk_write
    operations = keys.each_with_object([]) do |key, ops|
      filter = identifier_for(key)
      expire_at = @expire_after ? key.at + @expire_after : nil

      ops << upsert_operation('$inc', filter: filter, data: data, expire_at: expire_at)
      next unless @system_tracking

      ops << upsert_operation(
        '$inc',
        filter: system_identifier_for(key: key),
        data: system_data_for(key: key, count: count, tracking_key: tracking_key),
        expire_at: expire_at
      )
    end

    collection.bulk_write(operations)
  else
    keys.each do |key|
      filter = identifier_for(key)
      expire_at = @expire_after ? key.at + @expire_after : nil
      update = build_update('$inc', data: data, expire_at: expire_at)

      collection.update_many(filter, update, upsert: true)
      next unless @system_tracking

      system_update = build_update(
        '$inc',
        data: system_data_for(key: key, count: count, tracking_key: tracking_key),
        expire_at: expire_at
      )
      collection.update_many(system_identifier_for(key: key), system_update, upsert: true)
    end
  end
end

#ping(key:, values:) ⇒ Object

rubocop:disable Metrics/MethodLength



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/trifle/stats/driver/mongo.rb', line 136

def ping(key:, values:) # rubocop:disable Metrics/MethodLength
  return [] if @joined_identifier

  data = self.class.pack(hash: { data: values, at: key.at })
  identifier = identifier_for(key)
  expire_at = @expire_after ? key.at + @expire_after : nil

  if @bulk_write
    operations = [
      upsert_operation('$set', filter: identifier.slice(:key), data: data, expire_at: expire_at)
    ]

    collection.bulk_write(operations)
  else
    update = build_update('$set', data: data, expire_at: expire_at)
    collection.update_many(identifier.slice(:key), update, upsert: true)
  end
end

#scan(key:) ⇒ Object



185
186
187
188
189
190
191
192
193
194
# File 'lib/trifle/stats/driver/mongo.rb', line 185

def scan(key:)
  return [] if @joined_identifier

  data = collection.find(
    **identifier_for(key)
  ).sort(at: -1).first # rubocop:disable Style/RedundantSort
  return [] if data.nil?

  [data['at'], data['data']]
end

#set(keys:, values:, count: 1, tracking_key: nil) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/trifle/stats/driver/mongo.rb', line 97

def set(keys:, values:, count: 1, tracking_key: nil) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
  data = self.class.pack(hash: { data: values })

  if @bulk_write
    operations = keys.each_with_object([]) do |key, ops|
      filter = identifier_for(key)
      expire_at = @expire_after ? key.at + @expire_after : nil

      ops << upsert_operation('$set', filter: filter, data: data, expire_at: expire_at)
      next unless @system_tracking

      ops << upsert_operation(
        '$inc',
        filter: system_identifier_for(key: key),
        data: system_data_for(key: key, count: count, tracking_key: tracking_key),
        expire_at: expire_at
      )
    end

    collection.bulk_write(operations)
  else
    keys.each do |key|
      filter = identifier_for(key)
      expire_at = @expire_after ? key.at + @expire_after : nil
      update = build_update('$set', data: data, expire_at: expire_at)

      collection.update_many(filter, update, upsert: true)
      next unless @system_tracking

      system_update = build_update(
        '$inc',
        data: system_data_for(key: key, count: count, tracking_key: tracking_key),
        expire_at: expire_at
      )
      collection.update_many(system_identifier_for(key: key), system_update, upsert: true)
    end
  end
end

#system_data_for(key:, count: 1, tracking_key: nil) ⇒ Object



53
54
55
56
# File 'lib/trifle/stats/driver/mongo.rb', line 53

def system_data_for(key:, count: 1, tracking_key: nil)
  tracking_key ||= key.key
  self.class.pack(hash: { data: { count: count, keys: { tracking_key => count } } })
end

#system_identifier_for(key:) ⇒ Object



48
49
50
51
# File 'lib/trifle/stats/driver/mongo.rb', line 48

def system_identifier_for(key:)
  key = Nocturnal::Key.new(key: '__system__key__', granularity: key.granularity, at: key.at)
  identifier_for(key)
end

#upsert_operation(operation, filter:, data:, expire_at: nil) ⇒ Object



155
156
157
158
# File 'lib/trifle/stats/driver/mongo.rb', line 155

def upsert_operation(operation, filter:, data:, expire_at: nil)
  update = build_update(operation, data: data, expire_at: expire_at)
  { update_many: { filter: filter, update: update, upsert: true } }
end