Module: Familia::Horreum::ManagementMethods
- Includes:
- AuditMethods, RelatedFieldsManagement, RepairMethods
- Defined in:
- lib/familia/horreum/management.rb
Overview
ManagementMethods - Class-level methods for Horreum model management
This module is extended into classes that include Familia::Horreum, providing class methods for database operations and object management (e.g., Customer.create, Customer.find_by_id)
Key features:
- Includes RelatedFieldsManagement for DataType field handling
- Includes AuditMethods for proactive consistency detection
- Includes RepairMethods for repair and rebuild operations
- Provides utility methods for working with Database objects
Instance Method Summary collapse
- #all(suffix = nil) ⇒ Object
-
#any? ⇒ Boolean
Checks if any tracked instances exist (fast, from instances sorted set).
-
#build {|instance| ... } ⇒ Horreum
Builds, populates, and atomically persists a new instance in one step.
-
#cleanup_stale_instance_entry(objkey) ⇒ void
Removes a stale entry from the instances sorted set.
-
#config_name ⇒ String
Converts the class name into a string that can be used to look up configuration values.
-
#count ⇒ Integer
(also: #size, #length)
Returns the number of tracked instances (fast, from instances sorted set).
-
#create! {|hobj| ... } ⇒ Object
Creates and persists a new instance of the class.
- #dbkey(identifier, suffix = self.suffix) ⇒ Object
-
#destroy!(identifier, suffix = nil) ⇒ Boolean
Destroys an object in Database with the given identifier.
-
#exists?(identifier, suffix = nil) ⇒ Boolean
Checks if an object with the given identifier exists in the database.
-
#extract_identifier_from_key(key, key_suffix = suffix) ⇒ String?
Extracts the identifier from a full Redis key by stripping the known prefix and suffix.
-
#familia_name ⇒ Object
Familia::Horreum::DefinitionMethods#familia_name.
-
#find_by_dbkey(objkey, check_exists: true) ⇒ Object?
(also: #find_by_key)
Retrieves and instantiates an object from Database using the full object key.
-
#find_by_identifier(identifier, suffix: nil, check_exists: true) ⇒ Object?
(also: #find_by_id)
Retrieves and instantiates an object from Database using its identifier.
-
#find_keys(suffix = '*') ⇒ Array<String>
Finds all keys in Database matching the given suffix pattern.
-
#in_instances?(identifier) ⇒ Boolean
Checks whether the given identifier appears in the +instances+ sorted set.
-
#instantiate_from_hash(obj_hash) ⇒ Object
private
Instantiates an object from a hash of field values.
-
#key_prefix ⇒ String
Returns the key prefix for this class including the delimiter.
-
#keys_any?(filter = '*') ⇒ Boolean
Checks if any objects exist using blocking KEYS command (production-dangerous).
-
#keys_count(filter = '*') ⇒ Integer
Returns authoritative count using blocking KEYS command (production-dangerous).
-
#load_multi(identifiers, suffix = nil) ⇒ Array<Object>
(also: #load_batch)
Loads multiple objects by their identifiers using pipelined HGETALL commands.
-
#load_multi_by_keys(objkeys) ⇒ Array<Object>
Loads multiple objects by their full dbkeys using pipelined HGETALL commands.
- #multiget ⇒ Object
- #rawmultiget(*hids) ⇒ Object
-
#scan_any?(filter = '*') ⇒ Boolean
(also: #any!)
Checks if any objects exist using non-blocking SCAN command (production-safe).
-
#scan_count(filter = '*') ⇒ Integer
(also: #count!)
Returns authoritative count using non-blocking SCAN command (production-safe).
-
#scan_pattern(match_suffix = suffix) ⇒ String
Returns the SCAN pattern for finding all objects of this class.
-
#storage_inspect(identifier_or_key) ⇒ Hash{String => Hash}?
Decodes raw HGETALL output for debugging purposes.
Methods included from RepairMethods
#rebuild_instances, #repair_all!, #repair_indexes!, #repair_instances!, #repair_multi_indexes!, #repair_participations!, #repair_related_fields!, #scan_keys
Methods included from AuditMethods
#audit_cross_references, #audit_instances, #audit_multi_indexes, #audit_participations, #audit_related_fields, #audit_unique_indexes, #health_check
Methods included from RelatedFieldsManagement
#attach_class_related_field, #attach_instance_related_field
Instance Method Details
#all(suffix = nil) ⇒ Object
694 695 696 697 698 |
# File 'lib/familia/horreum/management.rb', line 694 def all(suffix = nil) suffix ||= self.suffix # objects that could not be parsed will be nil find_keys(suffix).filter_map { |k| find_by_key(k) } end |
#any? ⇒ Boolean
For authoritative check, use #scan_any? (production-safe) or #keys_any? (blocking)
Checks if any tracked instances exist (fast, from instances sorted set).
This method provides O(1) performance by querying the instances sorted set.
However, objects deleted outside Familia may leave stale entries.
791 792 793 |
# File 'lib/familia/horreum/management.rb', line 791 def any? count.positive? end |
#build {|instance| ... } ⇒ Horreum
Builds, populates, and atomically persists a new instance in one step.
This is a factory wrapper around Horreum#atomic_write for the common case of "create an object, set up its collections, and save -- all at once". The block receives the freshly-built (but not-yet-persisted) instance. Scalar assignments and collection mutations made inside the block are all committed in a single MULTI/EXEC when the block exits:
- Scalar setters (+u.name = ...+) stay in memory until the implicit save queues the HMSET inside the transaction.
- Collection mutations (+u.tags.add(...)+) auto-route into the open transaction because +DataType#dbclient+ honours +Fiber[:familia_transaction]+.
Nothing reaches the database until the block exits, so a factory no longer has to choose between sequencing +save+ before collection writes or reaching for +atomic_write+ directly.
Persistence semantics
+build+ has create-only semantics: it raises RecordExistsError if an object with the same identifier already exists. This follows the principle of least astonishment -- a factory helper should not silently overwrite existing records. Use Persistence#save or Persistence#save_with_collections when you explicitly want overwrite/upsert behaviour.
Concurrency: both paths use WATCH + MULTI/EXEC for race-safe duplicate detection. Without a block, +build+ delegates to Persistence#save_if_not_exists!. With a block, +atomic_write+ is called with +watch_keys:+ and +pre_check:+ so the existence check runs between WATCH and MULTI -- if the key is created by another client in that window, Redis aborts the transaction and the method retries with exponential backoff (up to 3 attempts). See issue #288.
Without a block
When called without a block there are no collection operations to fold in, so +build+ uses +save_if_not_exists!+ and returns the instance.
Positional and keyword arguments are forwarded to new. The block is NOT forwarded to the constructor -- it is invoked here, after building, with the new instance.
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/familia/horreum/management.rb', line 167 def build(*, **) # Forward only positional/keyword args to the constructor; the block is # a post-build callback, so it must not leak into new/initialize. instance = new(*, **) if block_given? instance.atomic_write( watch_keys: [instance.dbkey], pre_check: -> { raise Familia::RecordExistsError, instance.dbkey if instance.exists? } ) { yield instance } else # No block means no collection ops to fold in, so we can use the # WATCH-guarded save_if_not_exists! directly — race-safe. instance.save_if_not_exists! end instance end |
#cleanup_stale_instance_entry(objkey) ⇒ void
This method returns an undefined value.
Removes a stale entry from the instances sorted set.
Call this explicitly when you detect that an object no longer exists and want to prune its ghost entry from the instances timeline. Finder methods (find_by_dbkey, find_by_id, load) are read-only and will not call this automatically.
309 310 311 312 313 314 315 316 317 318 319 |
# File 'lib/familia/horreum/management.rb', line 309 def cleanup_stale_instance_entry(objkey) return unless respond_to?(:instances) # Key format is prefix:identifier:suffix. Use extract_identifier_from_key # to correctly handle compound identifiers containing the delimiter. identifier = extract_identifier_from_key(objkey) return if identifier.nil? || identifier.empty? instances.remove(identifier) Familia.debug "[cleanup_stale_instance_entry] Removed stale entry: #{identifier}" end |
#config_name ⇒ String
Converts the class name into a string that can be used to look up configuration values. This is particularly useful when mapping familia models with specific database numbers in the configuration.
Familia::Horreum::DefinitionMethods#config_name
207 208 209 210 211 |
# File 'lib/familia/horreum/management.rb', line 207 def config_name return nil if name.nil? name.demodularize.snake_case end |
#count ⇒ Integer Also known as: size, length
For authoritative count, use #scan_count (production-safe) or #keys_count (blocking)
Returns the number of tracked instances (fast, from instances sorted set).
This method provides O(1) performance by querying the instances sorted set,
which is automatically maintained when objects are created/destroyed through
Familia. However, objects deleted outside Familia (e.g., direct Redis commands)
may leave stale entries.
718 719 720 |
# File 'lib/familia/horreum/management.rb', line 718 def count instances.count end |
#create! {|hobj| ... } ⇒ Object
The behavior of this method depends on the implementation of #new,
exists?, and #save in the class and its superclasses.
Creates and persists a new instance of the class.
This method serves as a factory method for creating and persisting new instances of the class. It combines object instantiation, existence checking, and persistence in a single operation.
The method is flexible in accepting both positional and keyword arguments:
- Positional arguments (*args) are passed directly to the constructor.
- Keyword arguments (**kwargs) are passed as a hash to the constructor.
After instantiation, the method checks if an object with the same identifier already exists. If it does, a Familia::RecordExistsError exception is raised to prevent overwriting existing data.
Concurrency: the duplicate check is best-effort, not fully atomic. It routes through Persistence#save_if_not_exists!, which uses WATCH + MULTI/EXEC to abort if the key appears between the check and the write -- more cautious than a bare read-then-write, but a true guarantee needs a server-side check (e.g. Lua). See Persistence#save_if_not_exists!.
Finally, the method saves the new instance returns it.
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/familia/horreum/management.rb', line 85 def create!(...) hobj = new(...) # Best-effort duplicate guard: WATCH + MULTI/EXEC, not fully atomic. hobj.save_if_not_exists! # If a block is given, yield the created object # This allows for additional operations on successful creation yield hobj if block_given? hobj end |
#dbkey(identifier, suffix = self.suffix) ⇒ Object
685 686 687 688 689 690 691 692 |
# File 'lib/familia/horreum/management.rb', line 685 def dbkey(identifier, suffix = self.suffix) if identifier.to_s.empty? raise NoIdentifier, "#{self} requires non-empty identifier, got: #{identifier.inspect}" end identifier &&= identifier.to_s Familia.dbkey(prefix, identifier, suffix) end |
#destroy!(identifier, suffix = nil) ⇒ Boolean
Destroys an object in Database with the given identifier.
Deletes the main hash key, all related fields, and removes the identifier from the +instances+ sorted set. This is the class-level counterpart to the instance method of the same name.
This method is part of Familia's high-level object lifecycle management. While delete!
operates directly on dbkeys, destroy! operates at the object level and is used for
ORM-style operations. Use destroy! when removing complete objects from the system, and
delete! when working directly with dbkeys.
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 |
# File 'lib/familia/horreum/management.rb', line 571 def destroy!(identifier, suffix = nil) suffix ||= self.suffix raise Familia::NoIdentifier, "#{self} requires non-empty identifier" if identifier.to_s.empty? objkey = dbkey identifier, suffix # Execute all deletion operations within a transaction transaction do |conn| # Clean up related fields first to avoid orphaned keys if relations? Familia.trace :DESTROY_RELATIONS!, nil, "#{self} has relations: #{.keys}" if Familia.debug? # Create a temporary instance to access related fields. # Pass identifier in constructor so init() sees it and can set dependent fields. identifier_field_name = self.identifier_field temp_instance = identifier_field_name ? new(identifier_field_name => identifier.to_s) : new .each do |name, _definition| obj = temp_instance.send(name) Familia.trace :DESTROY_RELATION!, name, "Deleting related field #{name} (#{obj.dbkey})" if Familia.debug? conn.del(obj.dbkey) end end # Delete the main object key ret = conn.del(objkey) Familia.trace :DESTROY!, nil, "#{objkey} #{ret.inspect}" if Familia.debug? # Remove from instances collection to avoid ghost entries instances.remove(identifier) if respond_to?(:instances) end end |
#exists?(identifier, suffix = nil) ⇒ Boolean
Checks if an object with the given identifier exists in the database.
This method constructs the full dbkey using the provided identifier and suffix, then checks if the key exists in the database.
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 |
# File 'lib/familia/horreum/management.rb', line 537 def exists?(identifier, suffix = nil) raise NoIdentifier, 'Empty identifier' if identifier.to_s.empty? suffix ||= self.suffix objkey = dbkey identifier, suffix ret = dbclient.exists objkey Familia.trace :EXISTS, nil, "#{objkey} #{ret.inspect}" if Familia.debug? # Handle Redis::Future objects during transactions return ret if ret.is_a?(Redis::Future) ret.positive? # differs from Valkey API but I think it's okay bc `exists?` is a predicate method. end |
#extract_identifier_from_key(key, key_suffix = suffix) ⇒ String?
Extracts the identifier from a full Redis key by stripping the known prefix and suffix.
This is safe for compound identifiers that contain the delimiter character (e.g., "mymodel:foo:bar:object" where the identifier is "foo:bar"). A naive split on the delimiter would truncate these.
675 676 677 678 679 680 681 682 683 |
# File 'lib/familia/horreum/management.rb', line 675 def extract_identifier_from_key(key, key_suffix = suffix) d = Familia.delim pfx = "#{prefix}#{d}" sfx = "#{d}#{key_suffix}" return nil unless key.start_with?(pfx) && key.end_with?(sfx) key[pfx.length..-(sfx.length + 1)] end |
#familia_name ⇒ Object
Familia::Horreum::DefinitionMethods#familia_name
217 218 219 220 221 |
# File 'lib/familia/horreum/management.rb', line 217 def familia_name return nil if name.nil? name.demodularize end |
#find_by_dbkey(objkey, check_exists: true) ⇒ Object? Also known as: find_by_key
This method is read-only. Ghost entries (identifiers lingering in +instances+ after their hash key expires) are not cleaned up here. Use +cleanup_stale_instance_entry+ explicitly when cleanup is desired.
When check_exists: false, HGETALL on non-existent keys returns {} which we detect and return nil (not an empty object instance).
Retrieves and instantiates an object from Database using the full object key.
This method can operate in two modes:
Safe mode (check_exists: true, default):
- First checks if the key exists with EXISTS command
- Returns nil immediately if key doesn't exist
- If exists, retrieves data with HGETALL and instantiates object
- Best for: Single object lookups, defensive code
- Commands: 2 per object (EXISTS + HGETALL)
Optimized mode (check_exists: false):
- Directly calls HGETALL without EXISTS check
- Returns nil if HGETALL returns empty hash (key doesn't exist)
- Otherwise instantiates object with returned data
- Best for: Bulk operations, performance-critical paths, when keys likely exist
- Commands: 1 per object (HGETALL only)
- Reduction: 50% fewer Redis commands
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/familia/horreum/management.rb', line 264 def find_by_dbkey(objkey, check_exists: true) raise ArgumentError, 'Empty key' if objkey.to_s.empty? if check_exists # Safe mode: Check existence first (original behavior) # We use a lower-level method here b/c we're working with the # full key and not just the identifier. does_exist = Familia.positive?(dbclient.exists(objkey)) Familia.debug "[find_by_key] #{self} from key #{objkey} (exists: #{does_exist})" Familia.trace :FIND_BY_DBKEY_KEY, nil, objkey if Familia.debug? # This is the reason for calling exists first. We want to definitively # and without any ambiguity know if the object exists in the database. If it # doesn't, we return nil. If it does, we proceed to load the object. # Otherwise, hgetall will return an empty hash, which will be passed to # the constructor, which will then be annoying to debug. return nil unless does_exist else # Optimized mode: Skip existence check Familia.debug "[find_by_key] #{self} from key #{objkey} (check_exists: false)" Familia.trace :FIND_BY_DBKEY_KEY, nil, objkey if Familia.debug? end obj = dbclient.hgetall(objkey) # horreum objects are persisted as database hashes Familia.trace :FIND_BY_DBKEY_INSPECT, nil, "#{objkey}: #{obj.inspect}" if Familia.debug? # Always check for empty hash to handle race conditions where the key # expires between EXISTS check and HGETALL (when check_exists: true), # or simply doesn't exist (when check_exists: false). return nil if obj.empty? # Create instance and deserialize fields using shared helper method instantiate_from_hash(obj) end |
#find_by_identifier(identifier, suffix: nil, check_exists: true) ⇒ Object? Also known as: find_by_id
Retrieves and instantiates an object from Database using its identifier.
Returns a fully-hydrated instance if the key exists, or +nil+ if it does not. This method never returns a "shell" object with empty fields. If you need an in-memory placeholder without loading from the database, use +new(identifier)+ instead -- but be aware that such an object has no persisted field values and will overwrite existing data if saved.
This method constructs the full dbkey using the provided identifier
and suffix, then delegates to find_by_key for the actual retrieval and
instantiation.
It's a higher-level method that abstracts away the key construction, making it easier to retrieve objects when you only have their identifier.
364 365 366 367 368 369 370 371 372 373 |
# File 'lib/familia/horreum/management.rb', line 364 def find_by_identifier(identifier, suffix: nil, check_exists: true) suffix ||= self.suffix return nil if identifier.to_s.empty? objkey = dbkey(identifier, suffix) Familia.debug "[find_by_id] #{self} from key #{objkey})" Familia.trace :FIND_BY_ID, nil, objkey if Familia.debug? find_by_dbkey objkey, check_exists: check_exists end |
#find_keys(suffix = '*') ⇒ Array<String>
Finds all keys in Database matching the given suffix pattern.
This method searches for all dbkeys that match the given suffix pattern. It uses the class's dbkey method to construct the search pattern.
616 617 618 |
# File 'lib/familia/horreum/management.rb', line 616 def find_keys(suffix = '*') dbclient.keys(dbkey('*', suffix)) || [] end |
#in_instances?(identifier) ⇒ Boolean
Checks whether the given identifier appears in the +instances+ sorted set.
This is a fast O(log N) ZSCORE lookup. It does NOT verify that the underlying hash key still exists in the database -- use #exists? for that. A true result means the object was persisted through Familia at some point and has not been removed from instances since.
519 520 521 522 523 |
# File 'lib/familia/horreum/management.rb', line 519 def in_instances?(identifier) return false if identifier.to_s.empty? instances.member?(identifier) end |
#instantiate_from_hash(obj_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.
This method:
- Allocates a new instance without calling initialize
- Initializes related DataType fields
- Deserializes and assigns field values from the hash
Instantiates an object from a hash of field values.
This is an internal helper method used by find_by_dbkey, load_multi, and load_multi_by_keys to eliminate code duplication. Not intended for direct use.
859 860 861 862 863 864 865 866 867 868 869 870 |
# File 'lib/familia/horreum/management.rb', line 859 def instantiate_from_hash(obj_hash) instance = allocate instance.instance_variable_set(:@dirty_fields, Concurrent::Map.new) instance.instance_variable_set(:@warned_dirty_signatures, Concurrent::Map.new) instance.send(:initialize_relatives) instance.send(:initialize_with_keyword_args_deserialize_value, **obj_hash) # Object was just loaded from Redis, so it matches DB state exactly. # Clear dirty flags set during field assignment above, mirroring what # initialize (horreum.rb:246) and refresh! (persistence.rb:608) do. instance.send(:clear_dirty!) instance end |
#key_prefix ⇒ String
Returns the key prefix for this class including the delimiter.
Centralizes key prefix generation to prevent bugs from manual string interpolation across the codebase.
641 642 643 |
# File 'lib/familia/horreum/management.rb', line 641 def key_prefix "#{prefix}#{Familia.delim}" end |
#keys_any?(filter = '*') ⇒ Boolean
For production-safe authoritative check, use #scan_any?
Checks if any objects exist using blocking KEYS command (production-dangerous).
⚠️ WARNING: This method uses the KEYS command which blocks Redis during execution. It scans ALL keys in the database and should NEVER be used in production.
811 812 813 |
# File 'lib/familia/horreum/management.rb', line 811 def keys_any?(filter = '*') keys_count(filter).positive? end |
#keys_count(filter = '*') ⇒ Integer
For production-safe authoritative count, use #scan_count
Returns authoritative count using blocking KEYS command (production-dangerous).
⚠️ WARNING: This method uses the KEYS command which blocks Redis during execution. It scans ALL keys in the database and should NEVER be used in production.
740 741 742 |
# File 'lib/familia/horreum/management.rb', line 740 def keys_count(filter = '*') dbclient.keys(dbkey(filter)).compact.size end |
#load_multi(identifiers, suffix = nil) ⇒ Array<Object> Also known as: load_batch
Returns nil for non-existent keys (maintains same contract as find_by_id)
Objects are returned in the same order as input identifiers
Empty/nil identifiers are skipped and return nil in result array
Loads multiple objects by their identifiers using pipelined HGETALL commands.
This method provides significant performance improvements for bulk loading by:
- Batching all HGETALL commands into a single Redis pipeline
- Eliminating network round-trip overhead
- Skipping individual EXISTS checks (like check_exists: false)
Performance characteristics:
- Standard approach: N objects × 2 commands (EXISTS + HGETALL) = 2N round trips
- check_exists: false: N objects × 1 command (HGETALL) = N round trips
- load_multi: 1 pipeline with N commands = 1 round trip
- Improvement: Up to 2N× faster for bulk operations
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 |
# File 'lib/familia/horreum/management.rb', line 406 def load_multi(identifiers, suffix = nil) suffix ||= self.suffix return [] if identifiers.empty? # Build list of valid keys and track their original positions valid_keys = [] valid_positions = [] identifiers.each_with_index do |identifier, idx| next if identifier.to_s.empty? valid_keys << dbkey(identifier, suffix) valid_positions << idx end Familia.trace :LOAD_MULTI, nil, "Loading #{identifiers.size} objects" if Familia.debug? # Pipeline all HGETALL commands multi_result = pipelined do |pipeline| valid_keys.each do |objkey| pipeline.hgetall(objkey) end end # Extract results array from MultiResult results = multi_result.results # Map results back to original positions objects = Array.new(identifiers.size) valid_positions.each_with_index do |pos, result_idx| obj_hash = results[result_idx] # Skip empty hashes (non-existent keys) next if obj_hash.nil? || obj_hash.empty? # Instantiate object using shared helper method objects[pos] = instantiate_from_hash(obj_hash) end objects end |
#load_multi_by_keys(objkeys) ⇒ Array<Object>
Returns nil for empty/nil keys, maintaining position alignment with input array
Loads multiple objects by their full dbkeys using pipelined HGETALL commands.
This is a lower-level variant of load_multi that works directly with dbkeys instead of identifiers. Useful when you already have the full keys.
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 |
# File 'lib/familia/horreum/management.rb', line 465 def load_multi_by_keys(objkeys) return [] if objkeys.empty? Familia.trace :LOAD_MULTI_BY_KEYS, nil, "Loading #{objkeys.size} objects" if Familia.debug? # Track which positions have valid keys to maintain result array alignment valid_positions = [] objkeys.each_with_index do |objkey, idx| valid_positions << idx unless objkey.to_s.empty? end # Pipeline all HGETALL commands for valid keys multi_result = pipelined do |pipeline| objkeys.each do |objkey| next if objkey.to_s.empty? pipeline.hgetall(objkey) end end # Extract results array from MultiResult results = multi_result.results # Map results back to original positions objects = Array.new(objkeys.size) valid_positions.each_with_index do |pos, result_idx| obj_hash = results[result_idx] # Skip empty hashes (non-existent keys) next if obj_hash.nil? || obj_hash.empty? # Instantiate object using shared helper method objects[pos] = instantiate_from_hash(obj_hash) end objects end |
#multiget ⇒ Object
186 187 188 |
# File 'lib/familia/horreum/management.rb', line 186 def multiget(...) rawmultiget(...).filter_map { |json| Familia::JsonSerializer.parse(json) } end |
#rawmultiget(*hids) ⇒ Object
190 191 192 193 194 195 196 |
# File 'lib/familia/horreum/management.rb', line 190 def rawmultiget(*hids) hids.collect! { |hobjid| dbkey(hobjid) } return [] if hids.compact.empty? Familia.trace :MULTIGET, nil, "#{hids.size}: #{hids}" if Familia.debug? dbclient.mget(*hids) end |
#scan_any?(filter = '*') ⇒ Boolean Also known as: any!
For fast check (potentially stale), use #any?
Checks if any objects exist using non-blocking SCAN command (production-safe).
This method uses cursor-based SCAN iteration to check for matching keys without blocking Redis. Safe for production use and returns early on first match.
831 832 833 834 835 836 837 838 839 840 841 842 |
# File 'lib/familia/horreum/management.rb', line 831 def scan_any?(filter = '*') pattern = dbkey(filter) cursor = "0" loop do cursor, keys = dbclient.scan(cursor, match: pattern, count: 100) return true unless keys.empty? break if cursor == "0" end false end |
#scan_count(filter = '*') ⇒ Integer Also known as: count!
For fast count (potentially stale), use #count
Returns authoritative count using non-blocking SCAN command (production-safe).
This method uses cursor-based SCAN iteration to count matching keys without blocking Redis. Safe for production use as it processes keys in chunks.
760 761 762 763 764 765 766 767 768 769 770 771 772 |
# File 'lib/familia/horreum/management.rb', line 760 def scan_count(filter = '*') pattern = dbkey(filter) count = 0 cursor = "0" loop do cursor, keys = dbclient.scan(cursor, match: pattern, count: 1000) count += keys.size break if cursor == "0" end count end |
#scan_pattern(match_suffix = suffix) ⇒ String
Returns the SCAN pattern for finding all objects of this class.
Centralizes SCAN pattern generation to ensure consistency across rebuild strategies and other key enumeration operations.
656 657 658 |
# File 'lib/familia/horreum/management.rb', line 656 def scan_pattern(match_suffix = suffix) "#{prefix}#{Familia.delim}*#{Familia.delim}#{match_suffix}" end |
#storage_inspect(identifier_or_key) ⇒ Hash{String => Hash}?
Decodes raw HGETALL output for debugging purposes.
When inspecting data from redis-cli, field values appear as raw JSON-encoded strings (e.g. +"\"UK\""+ for the string +"UK"+). This method retrieves the hash, deserializes each value, and returns a diagnostic hash showing the raw stored form, decoded Ruby value, and Ruby type for every field.
Accepts either an identifier (resolved via +dbkey+) or a full database key (detected by the presence of the class delimiter).
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 |
# File 'lib/familia/horreum/management.rb', line 900 def storage_inspect(identifier_or_key) # Determine if this is a full key or an identifier objkey = if identifier_or_key.to_s.include?(Familia.delim) identifier_or_key.to_s else dbkey(identifier_or_key) end raw_hash = dbclient.hgetall(objkey) return nil if raw_hash.empty? # Use a temporary instance for deserialization (needs serialize_value/deserialize_value) temp = allocate temp.instance_variable_set(:@dirty_fields, Concurrent::Map.new) temp.instance_variable_set(:@warned_dirty_signatures, Concurrent::Map.new) temp.send(:initialize_relatives) raw_hash.each_with_object({}) do |(field, raw_val), result| decoded = temp.send(:deserialize_value, raw_val, field_name: field.to_sym) result[field] = { raw: raw_val, decoded: decoded, type: decoded.class.name } end end |