Module: OpenC3::DbShardedModel::ClassMethods

Defined in:
lib/openc3/models/db_sharded_model.rb

Instance Method Summary collapse

Instance Method Details

#_active_db_shards(scope:) ⇒ Object

Collect all active db_shards (always fresh lookup, no cache).



48
49
50
# File 'lib/openc3/models/db_sharded_model.rb', line 48

def _active_db_shards(scope:)
  _collect_db_shards(scope: scope)
end

#_db_shard_for_name(name, scope:, use_cache: false) ⇒ Object

Lookup of db_shard for a given name. Hard-cached only when use_cache: true (intended for the set/create path where the db_shard won’t change within the process lifetime).



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/openc3/models/db_sharded_model.rb', line 28

def _db_shard_for_name(name, scope:, use_cache: false)
  cache = nil

  if use_cache
    cache = (@db_shard_cache ||= {})
    cache_key = "#{scope}__#{name}"
    cached = cache[cache_key]
    return cached unless cached.nil?
  end

  db_shard = _lookup_db_shard(name, scope: scope)

  if use_cache
    cache[cache_key] = db_shard
  end

  db_shard
end

#_db_sharded_all(key, scope:) ⇒ Object

DB_Shard-aware all: iterates all active db_shards and collects all values.



69
70
71
72
73
74
75
76
77
78
# File 'lib/openc3/models/db_sharded_model.rb', line 69

def _db_sharded_all(key, scope:)
  result = {}
  _active_db_shards(scope: scope).each do |db_shard|
    hash = store.instance(db_shard: db_shard).hgetall(key)
    hash.each do |k, value|
      result[k] = JSON.parse(value, allow_nan: true, create_additions: true)
    end
  end
  result
end

#_db_sharded_get(key, name:, scope:) ⇒ Object

DB_Shard-aware get: looks up the db_shard for name, reads from the correct store instance.



53
54
55
56
57
# File 'lib/openc3/models/db_sharded_model.rb', line 53

def _db_sharded_get(key, name:, scope:)
  db_shard = _db_shard_for_name(name, scope: scope)
  json = store.instance(db_shard: db_shard).hget(key, name)
  json ? JSON.parse(json, allow_nan: true, create_additions: true) : nil
end

#_db_sharded_names(key, scope:) ⇒ Object

DB_Shard-aware names: iterates all active db_shards and collects keys.



60
61
62
63
64
65
66
# File 'lib/openc3/models/db_sharded_model.rb', line 60

def _db_sharded_names(key, scope:)
  result = []
  _active_db_shards(scope: scope).each do |db_shard|
    result.concat(store.instance(db_shard: db_shard).hkeys(key))
  end
  result.uniq.sort
end