Sha256: 66b8d14cb09d84ee091b6c9bce51e83345c10ec24e0b5c541b286ebf0d0866b0
Contents?: true
Size: 1.73 KB
Versions: 1
Compression:
Stored size: 1.73 KB
Contents
# frozen_string_literal: true require_relative '../mixins/packer' module Trifle module Stats module Driver class Mongo include Mixins::Packer attr_accessor :client, :collection_name, :separator def initialize(client, collection_name: 'trifle_stats') @client = client @collection_name = collection_name @separator = '::' end def self.setup!(client, collection_name: 'trifle_stats') client[collection_name].create client[collection_name].indexes.create_one({ key: 1 }, unique: true) end def inc(keys:, **values) data = self.class.pack(hash: { data: values }) collection.bulk_write( keys.map do |key| upsert_operation('$inc', pkey: key.join(separator), data: data) end ) end def set(keys:, **values) data = self.class.pack(hash: { data: values }) collection.bulk_write( keys.map do |key| upsert_operation('$set', pkey: key.join(separator), data: data) end ) end def upsert_operation(operation, pkey:, data:) { update_many: { filter: { key: pkey }, update: { operation => data }, upsert: true } } end def get(keys:) pkeys = keys.map { |key| key.join(separator) } data = collection.find(key: { '$in' => pkeys }) map = data.inject({}) { |o, d| o.merge(d['key'] => d['data']) } pkeys.map { |pkey| map[pkey] || {} } end private def collection client[collection_name] end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
trifle-stats-1.3.1 | lib/trifle/stats/driver/mongo.rb |