Module: Familia::Horreum::DatabaseCommands
- Included in:
- Familia::Horreum
- Defined in:
- lib/familia/horreum/database_commands.rb
Overview
DatabaseCommands - Instance-level methods for horreum models that call Database commands
NOTE: There is no hgetall for Horreum. This is because Horreum is a single hash in Database that we aren't meant to have be working on in memory for more than, making changes -> committing. To emphasize this, instead of "refreshing" the object with hgetall, just load the object again.
Instance Method Summary collapse
-
#current_expiration ⇒ Integer
Retrieves the remaining time to live (TTL) for the object's dbkey.
- #data_type ⇒ Object
- #decr(field) ⇒ Object (also: #decrement)
- #decrby(field, decrement) ⇒ Object (also: #decrementby)
-
#delete! ⇒ Boolean
(also: #clear)
Deletes the dbkey for this horreum :object.
- #echo(*args) ⇒ Object
-
#exists?(check_size: true) ⇒ Boolean
Checks if the calling object's key exists in the database.
-
#expire(default_expiration = nil) ⇒ Object
Sets a timeout on key.
-
#field_count ⇒ Integer
(also: #size, #length)
Returns the number of fields in the main object hash.
- #hget(field) ⇒ Object
-
#hgetall ⇒ Object
(also: #all)
For parity with DataType#hgetall.
- #hkeys ⇒ Object
- #hmset(hsh = {}) ⇒ Object
-
#hset(field, value) ⇒ Object
The number of fields that were added to the hash.
-
#hsetnx(field, value) ⇒ Integer
Sets field in the hash stored at key to value, only if field does not yet exist.
- #hstrlen(field) ⇒ Object (also: #hstrlength)
- #hvals ⇒ Object
- #incr(field) ⇒ Object (also: #increment)
- #incrby(field, increment) ⇒ Object (also: #incrementby)
- #incrbyfloat(field, increment) ⇒ Object (also: #incrementbyfloat)
- #key?(field) ⇒ Boolean (also: #has_key?)
- #move(logical_database) ⇒ Object
-
#remove_field(field) ⇒ Integer
(also: #remove)
Removes a field from the hash stored at the dbkey.
Instance Method Details
#current_expiration ⇒ Integer
Retrieves the remaining time to live (TTL) for the object's dbkey.
This method accesses the objects Database client to obtain the TTL of dbkey.
If debugging is enabled, it logs the TTL retrieval operation using Familia.trace.
71 72 73 74 |
# File 'lib/familia/horreum/database_commands.rb', line 71 def current_expiration Familia.trace :CURRENT_EXPIRATION, nil, uri if Familia.debug? dbclient.ttl dbkey end |
#data_type ⇒ Object
86 87 88 89 |
# File 'lib/familia/horreum/database_commands.rb', line 86 def data_type Familia.trace :DATATYPE, nil, uri if Familia.debug? dbclient.type dbkey(suffix) end |
#decr(field) ⇒ Object Also known as: decrement
158 159 160 |
# File 'lib/familia/horreum/database_commands.rb', line 158 def decr(field) dbclient.hdecr field end |
#decrby(field, decrement) ⇒ Object Also known as: decrementby
153 154 155 |
# File 'lib/familia/horreum/database_commands.rb', line 153 def decrby(field, decrement) dbclient.decrby dbkey(suffix), field, decrement end |
#delete! ⇒ Boolean Also known as: clear
Deletes the dbkey for this horreum :object.
It does not delete the related fields keys. See destroy!
178 179 180 181 182 183 184 |
# File 'lib/familia/horreum/database_commands.rb', line 178 def delete! Familia.trace :DELETE!, nil, uri if Familia.debug? # Delete the main object key ret = dbclient.del dbkey ret.positive? end |
#echo(*args) ⇒ Object
187 188 189 |
# File 'lib/familia/horreum/database_commands.rb', line 187 def echo(*args) dbclient.echo "[#{self.class}] #{args.join(' ')}" end |
#exists?(check_size: true) ⇒ Boolean
The default behavior maintains backward compatibility by treating empty hashes
as non-existent. Use check_size: false for pure key existence checking.
Checks if the calling object's key exists in the database.
39 40 41 42 43 44 |
# File 'lib/familia/horreum/database_commands.rb', line 39 def exists?(check_size: true) key_exists = self.class.exists?(identifier) return key_exists unless check_size key_exists && !size.zero? end |
#expire(default_expiration = nil) ⇒ Object
Sets a timeout on key. After the timeout has expired, the key will automatically be deleted. Returns 1 if the timeout was set, 0 if key does not exist or the timeout could not be set.
58 59 60 61 62 |
# File 'lib/familia/horreum/database_commands.rb', line 58 def expire(default_expiration = nil) default_expiration ||= self.class.default_expiration Familia.trace :EXPIRE, nil, default_expiration if Familia.debug? dbclient.expire dbkey, default_expiration.to_i end |
#field_count ⇒ Integer Also known as: size, length
Returns the number of fields in the main object hash
48 49 50 |
# File 'lib/familia/horreum/database_commands.rb', line 48 def field_count dbclient.hlen dbkey end |
#hget(field) ⇒ Object
98 99 100 101 |
# File 'lib/familia/horreum/database_commands.rb', line 98 def hget(field) Familia.trace :HGET, nil, field if Familia.debug? dbclient.hget dbkey(suffix), field end |
#hgetall ⇒ Object Also known as: all
For parity with DataType#hgetall
92 93 94 95 |
# File 'lib/familia/horreum/database_commands.rb', line 92 def hgetall Familia.trace :HGETALL, nil, uri if Familia.debug? dbclient.hgetall dbkey(suffix) end |
#hkeys ⇒ Object
129 130 131 132 |
# File 'lib/familia/horreum/database_commands.rb', line 129 def hkeys Familia.trace :HKEYS, nil, 'uri' if Familia.debug? dbclient.hkeys dbkey(suffix) end |
#hmset(hsh = {}) ⇒ Object
123 124 125 126 127 |
# File 'lib/familia/horreum/database_commands.rb', line 123 def hmset(hsh = {}) hsh ||= to_h Familia.trace :HMSET, nil, hsh if Familia.debug? dbclient.hmset dbkey(suffix), hsh end |
#hset(field, value) ⇒ Object
Returns The number of fields that were added to the hash. If the field already exists, this will return 0.
105 106 107 108 |
# File 'lib/familia/horreum/database_commands.rb', line 105 def hset(field, value) Familia.trace :HSET, nil, field if Familia.debug? dbclient.hset dbkey, field, value end |
#hsetnx(field, value) ⇒ Integer
Sets field in the hash stored at key to value, only if field does not yet exist. If key does not exist, a new key holding a hash is created. If field already exists, this operation has no effect.
118 119 120 121 |
# File 'lib/familia/horreum/database_commands.rb', line 118 def hsetnx(field, value) Familia.trace :HSETNX, nil, field if Familia.debug? dbclient.hsetnx dbkey, field, value end |
#hstrlen(field) ⇒ Object Also known as: hstrlength
163 164 165 |
# File 'lib/familia/horreum/database_commands.rb', line 163 def hstrlen(field) dbclient.hstrlen dbkey(suffix), field end |
#hvals ⇒ Object
134 135 136 |
# File 'lib/familia/horreum/database_commands.rb', line 134 def hvals dbclient.hvals dbkey(suffix) end |
#incr(field) ⇒ Object Also known as: increment
138 139 140 |
# File 'lib/familia/horreum/database_commands.rb', line 138 def incr(field) dbclient.hincrby dbkey(suffix), field, 1 end |
#incrby(field, increment) ⇒ Object Also known as: incrementby
143 144 145 |
# File 'lib/familia/horreum/database_commands.rb', line 143 def incrby(field, increment) dbclient.hincrby dbkey(suffix), field, increment end |
#incrbyfloat(field, increment) ⇒ Object Also known as: incrementbyfloat
148 149 150 |
# File 'lib/familia/horreum/database_commands.rb', line 148 def incrbyfloat(field, increment) dbclient.hincrbyfloat dbkey(suffix), field, increment end |
#key?(field) ⇒ Boolean Also known as: has_key?
168 169 170 |
# File 'lib/familia/horreum/database_commands.rb', line 168 def key?(field) dbclient.hexists dbkey(suffix), field end |
#move(logical_database) ⇒ Object
19 20 21 |
# File 'lib/familia/horreum/database_commands.rb', line 19 def move(logical_database) dbclient.move dbkey, logical_database end |