mirror of
https://github.com/HDT3213/godis.git
synced 2025-10-16 05:40:49 +08:00
Merge branch 'feat/raft'
# Conflicts: # cluster/cluster.go # cluster/router.go # config/config.go # database/database.go # database/server.go
This commit is contained in:
@@ -30,6 +30,10 @@ type DB struct {
|
||||
|
||||
// addaof is used to add command to aof
|
||||
addAof func(CmdLine)
|
||||
|
||||
// callbacks
|
||||
insertCallback database.KeyEventCallback
|
||||
deleteCallback database.KeyEventCallback
|
||||
}
|
||||
|
||||
// ExecFunc is interface for command executor
|
||||
@@ -159,7 +163,13 @@ func (db *DB) GetEntity(key string) (*database.DataEntity, bool) {
|
||||
|
||||
// PutEntity a DataEntity into DB
|
||||
func (db *DB) PutEntity(key string, entity *database.DataEntity) int {
|
||||
return db.data.PutWithLock(key, entity)
|
||||
ret := db.data.PutWithLock(key, entity)
|
||||
// db.insertCallback may be set as nil, during `if` and actually callback
|
||||
// so introduce a local variable `cb`
|
||||
if cb := db.insertCallback; ret > 0 && cb != nil {
|
||||
cb(db.index, key, entity)
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
// PutIfExists edit an existing DataEntity
|
||||
@@ -169,15 +179,28 @@ func (db *DB) PutIfExists(key string, entity *database.DataEntity) int {
|
||||
|
||||
// PutIfAbsent insert an DataEntity only if the key not exists
|
||||
func (db *DB) PutIfAbsent(key string, entity *database.DataEntity) int {
|
||||
return db.data.PutIfAbsentWithLock(key, entity)
|
||||
ret := db.data.PutIfAbsentWithLock(key, entity)
|
||||
// db.insertCallback may be set as nil, during `if` and actually callback
|
||||
// so introduce a local variable `cb`
|
||||
if cb := db.insertCallback; ret > 0 && cb != nil {
|
||||
cb(db.index, key, entity)
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
// Remove the given key from db
|
||||
func (db *DB) Remove(key string) {
|
||||
db.data.RemoveWithLock(key)
|
||||
raw, deleted := db.data.RemoveWithLock(key)
|
||||
db.ttlMap.Remove(key)
|
||||
taskKey := genExpireTask(key)
|
||||
timewheel.Cancel(taskKey)
|
||||
if cb := db.deleteCallback; cb != nil {
|
||||
var entity *database.DataEntity
|
||||
if deleted > 0 {
|
||||
entity = raw.(*database.DataEntity)
|
||||
}
|
||||
cb(db.index, key, entity)
|
||||
}
|
||||
}
|
||||
|
||||
// Removes the given keys from db
|
||||
|
Reference in New Issue
Block a user