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

View File

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