Has returns true if the record exists. It checks without retrieving the value from disk making lookups significantly faster while keeping memory usage down as well. It does require the `HintKeyAndRAMIdxMode` option to be enabled to function as described.
(bucket string, key []byte)
| 258 | // usage down as well. It does require the `HintKeyAndRAMIdxMode` option to be |
| 259 | // enabled to function as described. |
| 260 | func (tx *Tx) Has(bucket string, key []byte) (exists bool, err error) { |
| 261 | if err := tx.checkTxIsClosed(); err != nil { |
| 262 | return false, err |
| 263 | } |
| 264 | |
| 265 | status, b := tx.getBucketAndItsStatus(DataStructureBTree, bucket) |
| 266 | if isBucketNotFoundStatus(status) { |
| 267 | return false, ErrNotFoundBucket |
| 268 | } |
| 269 | bucketId := b.Id |
| 270 | |
| 271 | status, _ = tx.findEntryAndItsStatus(DataStructureBTree, bucket, string(key)) |
| 272 | switch status { |
| 273 | case EntryDeleted: |
| 274 | return false, ErrKeyNotFound |
| 275 | case EntryUpdated: |
| 276 | return true, nil |
| 277 | } |
| 278 | |
| 279 | idx, bucketExists := tx.db.Index.BTree.exist(bucketId) |
| 280 | if !bucketExists { |
| 281 | return false, ErrBucketNotExist |
| 282 | } |
| 283 | |
| 284 | if _, recordExists := idx.Find(key); recordExists { |
| 285 | return true, nil |
| 286 | } |
| 287 | |
| 288 | return false, nil |
| 289 | } |
| 290 | |
| 291 | // RangeScanEntries query a range at given bucket, start and end slice. It will |
| 292 | // return keys and/or values based on the includeKeys and includeValues flags. |