MCPcopy Create free account
hub / github.com/IceFireDB/IceFireDB / cmdSETBIT

Function cmdSETBIT

strings.go:264–304  ·  view source on GitHub ↗

cmdSETBIT sets or clears the bit at offset in the string value stored at key. Syntax: SETBIT key offset value The bit is either set or cleared depending on value, which can be either 0 or 1. When key does not exist, a new string value is created. The string is grown to make sure it can hold a bit at

(m uhaha.Machine, args []string)

Source from the content-addressed store, hash-verified

262// The string is grown to make sure it can hold a bit at offset.
263// The offset argument is required to be greater than or equal to 0.
264// Returns the original bit value stored at offset.
265func cmdSETBIT(m uhaha.Machine, args []string) (interface{}, error) {
266 // Check the number of arguments. It must be exactly 4: SETBIT, key, offset, value.
267 if len(args) != 4 {
268 return nil, uhaha.ErrWrongNumArgs
269 }
270
271 // Extract the key from the arguments and convert it to a byte slice.
272 key := []byte(args[1])
273
274 // Extract the offset from the arguments and convert it to an integer.
275 offset, err := strconv.Atoi(args[2])
276 if err != nil {
277 return nil, err // Return the error if the offset is not a valid integer.
278 }
279
280 // Validate that the offset is not negative. SETBIT requires a non-negative offset.
281 if offset < 0 {
282 return nil, errors.New("offset must be a non-negative integer")
283 }
284
285 // Extract the value from the arguments and convert it to an integer.
286 value, err := strconv.Atoi(args[3])
287 if err != nil {
288 return nil, err // Return the error if the value is not a valid integer.
289 }
290
291 // Validate that the value is either 0 or 1. SETBIT only accepts 0 or 1 as values.
292 if value != 0 && value != 1 {
293 return nil, errors.New("value must be 0 or 1")
294 }
295
296 // Call the ldb.SetBit function to set the bit at the specified offset.
297 // This function returns the original bit value at the offset.
298 n, err := ldb.SetBit(key, offset, value)
299 if err != nil {
300 return nil, err // Return any error from the ldb.SetBit function.
301 }
302
303 // Return the original bit value as a redcon.SimpleInt.
304 return redcon.SimpleInt(n), nil
305}
306
307// Retrieves the bit value at a specified offset in the string stored at the given key.

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected