MCPcopy Index your code
hub / github.com/ByteStorage/FlyDB / HSetNX

Method HSetNX

structure/hash.go:1047–1095  ·  view source on GitHub ↗

HSetNX sets a field in the hash only if the field does not already exist. It takes a string key 'k', a field 'f', and a value 'v' to set if the field doesn't exist. It returns a boolean indicating whether the field was set and any possible error. Parameters: k: The key of the hash table. f: The

(key string, field, value interface{})

Source from the content-addressed store, hash-verified

1045// bool: True if the field was set, false otherwise.
1046// error: An error if occurred during the operation, or nil on success.
1047func (hs *HashStructure) HSetNX(key string, field, value interface{}) (bool, error) {
1048 // Convert the parameters to bytes
1049 k := stringToBytesWithKey(key)
1050
1051 // Convert the parameters to bytes
1052 f, err, _ := interfaceToBytes(field)
1053 if err != nil {
1054 return false, err
1055 }
1056
1057 // Convert the parameters to bytes
1058 v, err, _ := interfaceToBytes(value)
1059 if err != nil {
1060 return false, err
1061 }
1062
1063 // Check the parameters
1064 if len(k) == 0 || len(f) == 0 || len(v) == 0 {
1065 return false, _const.ErrKeyIsEmpty
1066 }
1067
1068 // Find the hash metadata by the given key
1069 hashMeta, err := hs.findHashMeta(key, Hash)
1070 if err != nil {
1071 return false, err
1072 }
1073
1074 // Create a new HashField
1075 hf := &HashField{
1076 field: f,
1077 key: k,
1078 version: hashMeta.version,
1079 }
1080
1081 // Encode the HashField
1082 hfBuf := hf.encodeHashField()
1083
1084 // Get the field from the database
1085 _, err = hs.db.Get(hfBuf)
1086 if err != nil && err == _const.ErrKeyNotFound {
1087 _, err := hs.HSet(key, field, value)
1088 if err != nil {
1089 return false, err
1090 }
1091 return true, nil
1092 } else {
1093 return false, nil
1094 }
1095}
1096
1097// HTypes returns the type of a field in the hash.
1098// It takes a string key 'k' and a field 'f'.

Callers

nothing calls this directly

Calls 6

findHashMetaMethod · 0.95
encodeHashFieldMethod · 0.95
HSetMethod · 0.95
stringToBytesWithKeyFunction · 0.85
interfaceToBytesFunction · 0.85
GetMethod · 0.65

Tested by

no test coverage detected