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

Method BitOp

structure/bitmap_1.go:168–251  ·  view source on GitHub ↗

BitOp count the number of bits set to 1 in the specified range of the bitmap If the key does not exist, it returns an error

(operation []byte, destkey string, keys ...string)

Source from the content-addressed store, hash-verified

166// BitOp count the number of bits set to 1 in the specified range of the bitmap
167// If the key does not exist, it returns an error
168func (b *BitMapStructure) BitOp(operation []byte, destkey string, keys ...string) error {
169 // Check the validity of the parameters
170 if string(operation) != "AND" && string(operation) != "OR" && string(operation) != "XOR" && string(operation) != "NOT" {
171 return ErrInvalidArgs
172 }
173
174 if string(operation) == "NOT" { // Handle single and double parameters separately
175 if len(keys) != 1 {
176 return ErrInvalidValue
177 }
178 bitmap, length, err := b.getBitmapFromDB(keys[0], false)
179 if err != nil {
180 return err
181 }
182 result := make([]byte, len2Size(length))
183
184 for i := uint(0); i < length; i++ {
185 b.setBit(result, i, !b.getBit(bitmap, i))
186 }
187 return b.setBitmapToDB(destkey, result, length)
188 } else {
189 if len(keys) != 2 {
190 return ErrInvalidValue
191 }
192 bitmap1, length1, err := b.getBitmapFromDB(keys[0], false)
193 if err != nil {
194 return err
195 }
196 bitmap2, length2, err := b.getBitmapFromDB(keys[1], false)
197 if err != nil {
198 return err
199 }
200
201 if string(operation) == "AND" {
202 length := min(length1, length2)
203 result := make([]byte, len2Size(length))
204
205 for i := uint(0); i < length; i++ {
206 b.setBit(result, i, b.getBit(bitmap1, i) && b.getBit(bitmap2, i))
207 }
208
209 return b.setBitmapToDB(destkey, result, length)
210 } else if string(operation) == "OR" {
211 length := max(length1, length2)
212 minLength := min(length1, length2)
213 result := make([]byte, len2Size(length))
214
215 for i := uint(0); i < minLength; i++ {
216 b.setBit(result, i, b.getBit(bitmap1, i) || b.getBit(bitmap2, i))
217 }
218 // Handle the remaining bits separately
219 if length1 > length2 {
220 for i := minLength; i < length; i++ {
221 b.setBit(result, i, b.getBit(bitmap1, i))
222 }
223 } else {
224 for i := minLength; i < length; i++ {
225 b.setBit(result, i, b.getBit(bitmap2, i))

Callers 1

Calls 7

getBitmapFromDBMethod · 0.95
setBitMethod · 0.95
getBitMethod · 0.95
setBitmapToDBMethod · 0.95
len2SizeFunction · 0.85
minFunction · 0.85
maxFunction · 0.85

Tested by 1