solve bitop problem while inexisting srckeys occours

This commit is contained in:
silentsai 2014-06-27 11:54:04 +08:00
parent 30b1a665e3
commit c2cfc60cf8
2 changed files with 41 additions and 26 deletions

View File

@ -455,28 +455,40 @@ func (db *DB) BOperation(op uint8, dstkey []byte, srckeys ...[]byte) (blen int32
return
}
blen = -1
t := db.binTx
t.Lock()
defer t.Unlock()
var seq, off uint32
var segments = make([][]byte, maxSegCount) // todo : init count also can be optimize while 'and' / 'or'
// init - meta info
var maxDstSeq, maxDstOff uint32
var nowSeq, nowOff int32
if nowSeq, nowOff, err = db.bGetMeta(srckeys[0]); err != nil { // todo : if key not exists ....
var keyNum int = len(srckeys)
var iniIdx int = 0
for ; iniIdx < keyNum; iniIdx++ {
if nowSeq, nowOff, err = db.bGetMeta(srckeys[iniIdx]); err != nil { // todo : if key not exists ....
return
} else if nowSeq < 0 {
return // incorrect ...
} else {
}
if nowSeq >= 0 {
maxDstSeq = uint32(nowSeq)
maxDstOff = uint32(nowOff)
break
}
}
if iniIdx+1 >= keyNum { // todo ... while operation is 'not'
// msg("lack of arguments")
return
}
// init - data
it := db.bIterator(srckeys[0])
var seq, off uint32
var segments = make([][]byte, maxSegCount) // todo : init count also can be optimize while 'and' / 'or'
it := db.bIterator(srckeys[iniIdx])
for ; it.Valid(); it.Next() {
if _, seq, err = db.bDecodeBinKey(it.Key()); err != nil {
// to do ...
@ -488,8 +500,7 @@ func (db *DB) BOperation(op uint8, dstkey []byte, srckeys ...[]byte) (blen int32
it.Close()
// operation with following keys
var keyNum int = len(srckeys)
for i := 1; i < keyNum; i++ {
for i := iniIdx + 1; i < keyNum; i++ {
if nowSeq, nowOff, err = db.bGetMeta(srckeys[i]); err != nil {
return
}
@ -546,12 +557,12 @@ func (db *DB) BOperation(op uint8, dstkey []byte, srckeys ...[]byte) (blen int32
db.rmExpire(t, bExpType, dstkey)
// set data and meta
if op == OPand || op == OPor {
// if op == OPand || op == OPor {
// for i := maxDstSeq; i >= 0; i-- {
//
// }
// }
db.bSetMeta(t, dstkey, maxDstSeq, maxDstOff)
}
var bk []byte
for seq, seg := range segments {
@ -565,6 +576,10 @@ func (db *DB) BOperation(op uint8, dstkey []byte, srckeys ...[]byte) (blen int32
}
err = t.Commit()
if err != nil {
blen = int32(maxDstSeq<<segBitWidth | maxDstOff)
}
return
}

View File

@ -11,10 +11,6 @@ func cmpBytes(a []byte, b []byte) bool {
for i, n := range a {
if n != b[i] {
println("diff !")
println(i)
println(n)
println(b[i])
return true
}
}
@ -82,7 +78,9 @@ func testSimple(t *testing.T) {
func testSimpleII(t *testing.T) {
db := getTestDB()
key := []byte("test_bin_2")
db.FlushAll()
key := []byte("test_bin")
pos := int32(1234567)
if ori, _ := db.BSetBit(key, pos, 1); ori != 0 {
@ -124,13 +122,15 @@ func testSimpleII(t *testing.T) {
func testOp(t *testing.T) {
db := getTestDB()
db.FlushAll()
dstKey := []byte("test_bin_op")
k0 := []byte("op_0")
k1 := []byte("op_10")
//k2 := []byte("op_11")
srcKeys := [][]byte{k0, k1}
k1 := []byte("op_01")
k2 := []byte("op_10")
k3 := []byte("op_11")
srcKeys := [][]byte{k2, k0, k1, k3}
/*
<k0>