Delete data according to the key
(key []byte)
| 346 | |
| 347 | // Delete data according to the key |
| 348 | func (db *DB) Delete(key []byte) error { |
| 349 | zap.L().Info("delete", zap.ByteString("key", key)) |
| 350 | |
| 351 | // Determine the validity of the key |
| 352 | if len(key) == 0 { |
| 353 | return _const.ErrKeyIsEmpty |
| 354 | } |
| 355 | |
| 356 | // Check whether the key exists. If it does not exist, return it |
| 357 | if pst := db.index.Get(key); pst == nil { |
| 358 | return nil |
| 359 | } |
| 360 | |
| 361 | // Construct a logRecord to indicate that it was deleted |
| 362 | logRecord := &data2.LogRecord{ |
| 363 | Key: encodeLogRecordKeyWithSeq(key, nonTransactionSeqNo), |
| 364 | Type: data2.LogRecordDeleted, |
| 365 | } |
| 366 | |
| 367 | // Write to the data file |
| 368 | _, err := db.appendLogRecordWithLock(logRecord) |
| 369 | if err != nil { |
| 370 | return err |
| 371 | } |
| 372 | |
| 373 | // Removes key from memory index |
| 374 | ok := db.index.Delete(key) |
| 375 | if !ok { |
| 376 | return _const.ErrIndexUpdateFailed |
| 377 | } |
| 378 | return nil |
| 379 | } |
| 380 | |
| 381 | // Load the data file from disk |
| 382 | func (db *DB) loadDataFiles() error { |