getBitmapFromDB retrieves data from the database. When isKeyCanNotExist is true, it returns an empty slice if the key doesn't exist instead of an error.
(key string, isKeyCanNotExist bool)
| 341 | |
| 342 | // getBitmapFromDB retrieves data from the database. When isKeyCanNotExist is true, it returns an empty slice if the key doesn't exist instead of an error. |
| 343 | func (b *BitMapStructure) getBitmapFromDB(key string, isKeyCanNotExist bool) ([]byte, uint, error) { |
| 344 | if isKeyCanNotExist { |
| 345 | // Get data corresponding to the key from the database |
| 346 | dbData, err := b.db.Get([]byte(key)) |
| 347 | // Since the key might not exist, we need to handle ErrKeyNotFound separately as it is a valid case |
| 348 | if err != nil && err != _const.ErrKeyNotFound { |
| 349 | return nil, 0, err |
| 350 | } |
| 351 | // Deserialize the data into a bitmap |
| 352 | bitmapArr, length, err := b.decodeBitmap(dbData) |
| 353 | if err != nil { |
| 354 | if len(dbData) != 0 { |
| 355 | return nil, 0, err |
| 356 | } else { |
| 357 | bitmapArr = make([]byte, 0) |
| 358 | length = 0 |
| 359 | } |
| 360 | } |
| 361 | return bitmapArr, length, nil |
| 362 | } else { |
| 363 | // Get data corresponding to the key from the database |
| 364 | dbData, err := b.db.Get([]byte(key)) |
| 365 | if err != nil { |
| 366 | return nil, 0, err |
| 367 | } |
| 368 | // Deserialize the data into a bitmap |
| 369 | bitmapArr, length, err := b.decodeBitmap(dbData) |
| 370 | if err != nil { |
| 371 | return nil, 0, err |
| 372 | } |
| 373 | return bitmapArr, length, nil |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | // setBitmapToDB stores the data into the database. |
| 378 | func (b *BitMapStructure) setBitmapToDB(key string, bm []byte, length uint) error { |