MCPcopy Create free account
hub / github.com/distributedio/titan / SetBit

Function SetBit

command/strings.go:478–513  ·  view source on GitHub ↗

SetBit sets or clears the bit at offset in the string value stored at key.

(ctx *Context, txn *db.Transaction)

Source from the content-addressed store, hash-verified

476
477// SetBit sets or clears the bit at offset in the string value stored at key.
478func SetBit(ctx *Context, txn *db.Transaction) (OnCommit, error) {
479 key := []byte(ctx.Args[0])
480 offset, err := strconv.Atoi(ctx.Args[1])
481 if err != nil {
482 return nil, ErrBitOffset
483 }
484 if offset < 0 {
485 return nil, ErrBitOffset
486 }
487
488 on, err := strconv.Atoi(ctx.Args[2])
489 if err != nil {
490 return nil, ErrBitInteger
491 }
492
493 // Bits can only be set or cleared...
494 if (on & ^1) != 0 {
495 return nil, ErrBitInteger
496 }
497
498 str, err := txn.String(key)
499 if err != nil {
500 if err == db.ErrTypeMismatch {
501 return nil, ErrTypeMismatch
502 }
503 return nil, errors.New("ERR " + err.Error())
504 }
505 val, err := str.SetBit(offset, on)
506 if err != nil {
507 return nil, errors.New("ERR " + err.Error())
508 }
509 if val != 0 {
510 return Integer(ctx.Out, 1), nil
511 }
512 return Integer(ctx.Out, 0), nil
513}
514
515// GetBit gets the bit at offset in the string value stored at key.
516func GetBit(ctx *Context, txn *db.Transaction) (OnCommit, error) {

Callers

nothing calls this directly

Calls 4

IntegerFunction · 0.85
SetBitMethod · 0.80
ErrorMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected