MCPcopy Create free account
hub / github.com/ByteStorage/FlyDB / BitOp

Method BitOp

structure/bitmap.go:266–319  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

264// BitOp function performs the specified bit operation on the provided keys and stores
265// the result in the destination key
266func (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
322func (b *BitmapStructure) getBits(key []byte) ([]byte, error) {

Callers 2

Calls 5

stringToBytesWithKeyFunction · 0.85
bitsetToByteArrayFunction · 0.85
GetMethod · 0.65
PutMethod · 0.65
UnmarshalBinaryMethod · 0.45

Tested by 2