SetBit Set the bit at the specified offset in the bitmap to the specified value If the key does not exist, it will be created If the bitmap length is not sufficient, it will be extended
(key string, offset uint, value bool)
| 30 | // If the key does not exist, it will be created |
| 31 | // If the bitmap length is not sufficient, it will be extended |
| 32 | func (b *BitMapStructure) SetBit(key string, offset uint, value bool) error { |
| 33 | // Get bitmap |
| 34 | bitmap, length, err := b.getBitmapFromDB(key, true) |
| 35 | |
| 36 | if err != nil { |
| 37 | return err |
| 38 | } |
| 39 | |
| 40 | // If the bitmap length is not sufficient and value is 1, extend it |
| 41 | if offset >= uint(length) { |
| 42 | if value { |
| 43 | length = offset + 1 |
| 44 | newSize := len2Size(length) |
| 45 | newBitmap := make([]byte, newSize) |
| 46 | copy(newBitmap, bitmap) |
| 47 | bitmap = newBitmap |
| 48 | } else { // If the value is 0, no operation is needed |
| 49 | return nil |
| 50 | } |
| 51 | } |
| 52 | b.setBit(bitmap, offset, value) |
| 53 | |
| 54 | return b.setBitmapToDB(key, bitmap, length) |
| 55 | } |
| 56 | |
| 57 | // SetBits Set the bit at the specified offset in the bitmap to the specified value |
| 58 | // If the key does not exist, it will be created |