MCPcopy Create free account
hub / github.com/ByteStorage/FlyDB / HMGet

Method HMGet

structure/hash.go:231–293  ·  view source on GitHub ↗

HMGet gets the string value of multiple hash fields. It takes a string key 'k' and a variadic number of fields 'f'. It returns an array of interface{} containing the values corresponding to the provided fields and any possible error. Parameters: k: The key of the hash table. f: Variable number o

(key string, field ...interface{})

Source from the content-addressed store, hash-verified

229// []interface{}: An array of interface{} containing values corresponding to the fields.
230// error: An error if occurred during the operation, or nil on success.
231func (hs *HashStructure) HMGet(key string, field ...interface{}) ([]interface{}, error) {
232 // check the key ttl
233 ttl, _ := hs.TTL(key)
234 if ttl == -1 {
235 return nil, _const.ErrKeyIsExpired
236 }
237
238 // Convert the parameters to bytes
239 k := stringToBytesWithKey(key)
240 var interfaces []interface{}
241
242 for _, fi := range field {
243 // Convert the parameters to bytes
244 f, err, _ := interfaceToBytes(fi)
245 if err != nil {
246 return nil, err
247 }
248
249 // Check the parameters
250 if len(k) == 0 || len(f) == 0 {
251 return nil, _const.ErrKeyIsEmpty
252 }
253
254 // Find the hash metadata by the given key
255 hashMeta, err := hs.findHashMeta(key, Hash)
256 if err != nil {
257 return nil, err
258 }
259
260 // If the counter is 0, return nil
261 if hashMeta.counter == 0 {
262 return nil, nil
263 }
264
265 // Create a new HashField
266 hf := &HashField{
267 field: f,
268 key: k,
269 version: hashMeta.version,
270 }
271
272 // Encode the HashField
273 hfBuf := hf.encodeHashField()
274
275 // Get the field from the database
276 value, err := hs.db.Get(hfBuf)
277 if err != nil {
278 return nil, err
279 }
280
281 // Get the value type from the hashValueTypes
282 valueType := hs.hashValueType
283
284 // Values of different types need to be converted to corresponding types
285 valueToInterface, err := byteToInterface(value, valueType)
286 if err != nil {
287 return nil, err
288 }

Callers 1

TestHashStructure_HMGetFunction · 0.80

Calls 7

TTLMethod · 0.95
findHashMetaMethod · 0.95
encodeHashFieldMethod · 0.95
stringToBytesWithKeyFunction · 0.85
interfaceToBytesFunction · 0.85
byteToInterfaceFunction · 0.85
GetMethod · 0.65

Tested by 1

TestHashStructure_HMGetFunction · 0.64