SetBit function sets a bit at the specified offset in the bitmap for the provided key
(k string, off uint)
| 49 | |
| 50 | // SetBit function sets a bit at the specified offset in the bitmap for the provided key |
| 51 | func (b *BitmapStructure) SetBit(k string, off uint) error { |
| 52 | // Convert the key to bytes |
| 53 | key := stringToBytesWithKey(k) |
| 54 | // Create a new bitset |
| 55 | bit := bitset.New(1) |
| 56 | // Get the current bits for the key |
| 57 | value, err := b.getBits(key) |
| 58 | // If the key is not found, initialize it with the new bitset |
| 59 | if err == _const.ErrKeyNotFound { |
| 60 | buf, _ := bitsetToByteArray(bit) |
| 61 | err = b.db.Put(key, buf) |
| 62 | if err != nil { |
| 63 | return err |
| 64 | } |
| 65 | value, err = b.getBits(key) |
| 66 | if err != nil { |
| 67 | return err |
| 68 | } |
| 69 | } |
| 70 | if err != nil { |
| 71 | return err |
| 72 | } |
| 73 | // Unmarshal the current bits into the bitset |
| 74 | err = bit.UnmarshalBinary(value) |
| 75 | if err != nil { |
| 76 | return err |
| 77 | } |
| 78 | // Set the bit at the specified offset |
| 79 | bit.Set(off) |
| 80 | // Convert the bitset to a byte array |
| 81 | buf, err := bitsetToByteArray(bit) |
| 82 | if err != nil { |
| 83 | return err |
| 84 | } |
| 85 | // Store the updated bitset in the database |
| 86 | return b.db.Put(key, buf) |
| 87 | } |
| 88 | |
| 89 | // SetBits function sets multiple bits at the specified offsets in the bitmap for the provided key |
| 90 | func (b *BitmapStructure) SetBits(k string, off ...uint) error { |