mirror of
https://bitbucket.org/s_l_teichmann/mtsatellite
synced 2024-12-23 08:40:18 +01:00
Implementation of Redis command HDEL. Needs testing. For issue #18.
This commit is contained in:
parent
9f73435783
commit
60f43c9f52
@ -15,6 +15,8 @@ type (
|
||||
|
||||
// Session is a database session.
|
||||
Session interface {
|
||||
// Del deletes a block by a given key.
|
||||
Del(hash, key []byte) (bool, error)
|
||||
// Fetch fetches the block data for a given position.
|
||||
Fetch(hash, key []byte) ([]byte, error)
|
||||
// InTransaction returns true if a transaction is running.
|
||||
|
@ -21,6 +21,8 @@ var (
|
||||
redisQueued = []byte("+QUEUED\r\n")
|
||||
redisTrue = []byte(":1\r\n")
|
||||
redisFalse = []byte(":0\r\n")
|
||||
redisZero = []byte("0\r\n")
|
||||
redisOne = []byte("1\r\n")
|
||||
)
|
||||
|
||||
type Connection struct {
|
||||
@ -57,6 +59,20 @@ func logError(err error) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (c *Connection) Hdel(hash, key []byte) bool {
|
||||
|
||||
success, err := c.session.Del(hash, key)
|
||||
if err != nil {
|
||||
return c.writeError(err)
|
||||
}
|
||||
|
||||
if success {
|
||||
return c.writeMessage(redisOne)
|
||||
}
|
||||
|
||||
return c.writeMessage(redisZero)
|
||||
}
|
||||
|
||||
func (c *Connection) Hget(hash, key []byte) bool {
|
||||
|
||||
var err error
|
||||
@ -203,14 +219,17 @@ func (c *Connection) writeBoolArray(arr []bool) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (c *Connection) writeOk() bool {
|
||||
_, err := c.conn.Write(redisOk)
|
||||
func (c *Connection) writeMessage(msg []byte) bool {
|
||||
_, err := c.conn.Write(msg)
|
||||
return logError(err)
|
||||
}
|
||||
|
||||
func (c *Connection) writeOk() bool {
|
||||
return c.writeMessage(redisOk)
|
||||
}
|
||||
|
||||
func (c *Connection) writeQueued() bool {
|
||||
_, err := c.conn.Write(redisQueued)
|
||||
return logError(err)
|
||||
return c.writeMessage(redisQueued)
|
||||
}
|
||||
|
||||
func (c *Connection) writeBlock(data []byte) bool {
|
||||
|
@ -140,6 +140,30 @@ func (ldb *LevelDBBackend) Shutdown() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ldbs *LevelDBSession) Del(hash, key []byte) (success bool, err error) {
|
||||
if key, err = ldbs.backend.decoder(key); err != nil {
|
||||
return
|
||||
}
|
||||
ldbs.backend.doRead(func(db *leveldb.DB) {
|
||||
ro := leveldb.NewReadOptions()
|
||||
defer ro.Close()
|
||||
var data []byte
|
||||
data, err = ldbs.backend.db.Get(ro, key)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if data == nil {
|
||||
success = false
|
||||
return
|
||||
}
|
||||
success = true
|
||||
wo := leveldb.NewWriteOptions()
|
||||
defer wo.Close()
|
||||
err = ldbs.backend.db.Delete(wo, key)
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (ldbs *LevelDBSession) Fetch(hash, key []byte) (value []byte, err error) {
|
||||
if key, err = ldbs.backend.decoder(key); err != nil {
|
||||
return
|
||||
|
@ -15,6 +15,7 @@ import (
|
||||
)
|
||||
|
||||
type RedisCommands interface {
|
||||
Hdel(hash, key []byte) bool
|
||||
Hget(hash, key []byte) bool
|
||||
Hset(hash, key, block []byte) bool
|
||||
Multi() bool
|
||||
@ -161,6 +162,18 @@ func (rp *RedisParser) execute() bool {
|
||||
}
|
||||
cmd := strings.ToUpper(asString(rp.args[0]))
|
||||
switch cmd {
|
||||
case "HDEL":
|
||||
if l < 3 {
|
||||
log.Println("WARN: Missing argments for HGET.")
|
||||
return false
|
||||
}
|
||||
hash, ok1 := rp.args[1].([]byte)
|
||||
key, ok2 := rp.args[2].([]byte)
|
||||
if !ok1 || !ok2 {
|
||||
log.Println("WARN: HDEL data are not byte slices.")
|
||||
return false
|
||||
}
|
||||
return rp.commands.Hdel(hash, key)
|
||||
case "HGET":
|
||||
if l < 3 {
|
||||
log.Println("WARN: Missing argments for HGET.")
|
||||
|
@ -17,6 +17,7 @@ import (
|
||||
var globalLock sync.RWMutex
|
||||
|
||||
const (
|
||||
deleteSQL = "DELETE FROM blocks WHERE pos = ?"
|
||||
fetchSQL = "SELECT data FROM blocks WHERE pos = ?"
|
||||
existsSQL = "SELECT 1 FROM blocks WHERE pos = ?"
|
||||
updateSQL = "UPDATE blocks SET data = ? WHERE pos = ?"
|
||||
@ -34,6 +35,7 @@ type SQLiteBackend struct {
|
||||
interleaved bool
|
||||
coverage *common.Coverage3D
|
||||
existsStmt *sql.Stmt
|
||||
deleteStmt *sql.Stmt
|
||||
fetchStmt *sql.Stmt
|
||||
insertStmt *sql.Stmt
|
||||
updateStmt *sql.Stmt
|
||||
@ -80,6 +82,11 @@ func NewSQLiteBackend(
|
||||
return
|
||||
}
|
||||
|
||||
if res.deleteStmt, err = res.db.Prepare(deleteSQL); err != nil {
|
||||
res.closeAll()
|
||||
return
|
||||
}
|
||||
|
||||
if res.insertStmt, err = res.db.Prepare(insertSQL); err != nil {
|
||||
res.closeAll()
|
||||
return
|
||||
@ -164,6 +171,7 @@ func closeDB(db **sql.DB) error {
|
||||
}
|
||||
|
||||
func (sqlb *SQLiteBackend) closeAll() error {
|
||||
closeStmt(&sqlb.deleteStmt)
|
||||
closeStmt(&sqlb.fetchStmt)
|
||||
closeStmt(&sqlb.insertStmt)
|
||||
closeStmt(&sqlb.updateStmt)
|
||||
@ -188,6 +196,36 @@ func (ss *SQLiteSession) txStmt(stmt *sql.Stmt) *sql.Stmt {
|
||||
return stmt
|
||||
}
|
||||
|
||||
func (ss *SQLiteSession) Del(hash, key []byte) (success bool, err error) {
|
||||
var pos int64
|
||||
if pos, err = ss.backend.decoder(key); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
globalLock.Lock()
|
||||
defer globalLock.Unlock()
|
||||
|
||||
existsStmt := ss.txStmt(ss.backend.existsStmt)
|
||||
var x int
|
||||
err2 := existsStmt.QueryRow(pos).Scan(&x)
|
||||
|
||||
if err2 == sql.ErrNoRows {
|
||||
success = false
|
||||
return
|
||||
}
|
||||
if err2 != nil {
|
||||
err = err2
|
||||
return
|
||||
}
|
||||
|
||||
success = true
|
||||
|
||||
deleteStmt := ss.txStmt(ss.backend.deleteStmt)
|
||||
_, err = deleteStmt.Exec(pos)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (ss *SQLiteSession) Fetch(hash, key []byte) (data []byte, err error) {
|
||||
var pos int64
|
||||
if pos, err = ss.backend.decoder(key); err != nil {
|
||||
|
Loading…
Reference in New Issue
Block a user