BitOp function performs the specified bit operation on the provided keys and stores the result in the destination key
(op BitOperation, destKey string, keys ...string)
| 264 | // BitOp function performs the specified bit operation on the provided keys and stores |
| 265 | // the result in the destination key |
| 266 | func (b *BitmapStructure) BitOp(op BitOperation, destKey string, keys ...string) error { |
| 267 | // Check if any keys were provided |
| 268 | if len(keys) == 0 { |
| 269 | return errors.New("no keys specified") |
| 270 | } |
| 271 | // Get the bits for the first key |
| 272 | value, err := b.db.Get(stringToBytesWithKey(keys[0])) |
| 273 | if err != nil { |
| 274 | return err |
| 275 | } |
| 276 | // Create a new bitset and unmarshal the retrieved bits into it |
| 277 | baseBit := bitset.New(1) |
| 278 | err = baseBit.UnmarshalBinary(value) |
| 279 | if err != nil { |
| 280 | return err |
| 281 | } |
| 282 | // Iterate over the remaining keys |
| 283 | for i := 1; i < len(keys); i++ { |
| 284 | // Get the bits for the current key |
| 285 | value, err := b.db.Get(stringToBytesWithKey(keys[i])) |
| 286 | if err != nil { |
| 287 | return err |
| 288 | } |
| 289 | // Create a new bitset and unmarshal the retrieved bits into it |
| 290 | bit := bitset.New(1) |
| 291 | err = bit.UnmarshalBinary(value) |
| 292 | if err != nil { |
| 293 | return err |
| 294 | } |
| 295 | // Perform the specified bit operation |
| 296 | switch op { |
| 297 | case BitOrOperation: |
| 298 | baseBit = baseBit.Union(bit) |
| 299 | case BitAndOperation: |
| 300 | baseBit = baseBit.Intersection(bit) |
| 301 | case BitXorOperation: |
| 302 | baseBit = baseBit.SymmetricDifference(bit) |
| 303 | case BitNotOperation: |
| 304 | baseBit = baseBit.Difference(bit) |
| 305 | } |
| 306 | } |
| 307 | // Convert the resulting bitset to a byte array |
| 308 | buf, err := bitsetToByteArray(baseBit) |
| 309 | if err != nil { |
| 310 | return err |
| 311 | } |
| 312 | // Store the result in the destination key |
| 313 | err = b.db.Put(stringToBytesWithKey(destKey), buf) |
| 314 | if err != nil { |
| 315 | return err |
| 316 | } |
| 317 | // Return nil if no errors occurred |
| 318 | return nil |
| 319 | } |
| 320 | |
| 321 | // getBits function retrieves the bits for the provided key |
| 322 | func (b *BitmapStructure) getBits(key []byte) ([]byte, error) { |