-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
42ff789
commit 11486c3
Showing
6 changed files
with
252 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
package proxy | ||
|
||
import ( | ||
"bytes" | ||
"strings" | ||
|
||
"github.com/deepfabric/elasticell/pkg/pb/raftcmdpb" | ||
"github.com/fagongzi/goetty/protocol/redis" | ||
"github.com/fagongzi/log" | ||
"github.com/fagongzi/util/format" | ||
"github.com/fagongzi/util/hack" | ||
"github.com/pilosa/pilosa/roaring" | ||
) | ||
|
||
const ( | ||
optionWithBM = "WITHBM" | ||
) | ||
|
||
// bmand [withbm|start count] bm1 bm2 [bm3 bm4] | ||
func (p *RedisProxy) doBMAnd(rs *redisSession, cmd redis.Command) (bool, error) { | ||
return p.doBMAggregation(rs, cmd, p.doBMAndMerge) | ||
} | ||
|
||
// bmor [withbm|start count] bm1 bm2 [bm3 bm4] | ||
func (p *RedisProxy) doBMOr(rs *redisSession, cmd redis.Command) (bool, error) { | ||
return p.doBMAggregation(rs, cmd, p.doBMOrMerge) | ||
} | ||
|
||
// bmandnot [withbm|start count] bm1 bm2 [bm3 bm4] | ||
func (p *RedisProxy) doBMAndNot(rs *redisSession, cmd redis.Command) (bool, error) { | ||
return p.doBMAggregation(rs, cmd, p.doBMAndNotMerge) | ||
} | ||
|
||
func (p *RedisProxy) doBMAggregation(rs *redisSession, cmd redis.Command, mergeFn func([][]byte, ...*raftcmdpb.Response) *raftcmdpb.Response) (bool, error) { | ||
offset := 1 | ||
n := len(cmd.Args()) | ||
if n < 3 { | ||
return false, nil | ||
} | ||
|
||
if strings.ToUpper(hack.SliceToString(cmd.Args()[0])) != optionWithBM { | ||
if n < 4 { | ||
return false, nil | ||
} | ||
|
||
offset = 2 | ||
} | ||
|
||
id := newID() | ||
rs.addAggregation(id, newAggregationReq(n-offset, mergeFn, cmd.Args()[0:offset])) | ||
|
||
for idx, key := range cmd.Args()[offset:] { | ||
cmd := redis.Command([][]byte{cmdGet, key}) | ||
p.addToForward(newReqUUID(bytes.Join([][]byte{id, format.UInt64ToString(uint64(idx))}, sep), cmd, rs)) | ||
} | ||
|
||
return true, nil | ||
} | ||
|
||
func (p *RedisProxy) doBMAndMerge(args [][]byte, rsps ...*raftcmdpb.Response) *raftcmdpb.Response { | ||
bm := roaring.NewBTreeBitmap() | ||
tmp := roaring.NewBTreeBitmap() | ||
var target *roaring.Bitmap | ||
for idx, rsp := range rsps { | ||
if len(rsp.BulkResult) > 0 { | ||
tmp.Containers.Reset() | ||
|
||
if idx == 0 { | ||
target = bm | ||
} else { | ||
target = tmp | ||
} | ||
|
||
_, _, err := target.ImportRoaringBits(rsp.BulkResult, false, false, 0) | ||
if err != nil { | ||
return &raftcmdpb.Response{ | ||
ErrorResult: hack.StringToSlice(err.Error()), | ||
} | ||
} | ||
|
||
if idx > 0 { | ||
bm = bm.Intersect(tmp) | ||
} | ||
} | ||
} | ||
|
||
return p.buildResult(bm, args) | ||
} | ||
|
||
func (p *RedisProxy) doBMOrMerge(args [][]byte, rsps ...*raftcmdpb.Response) *raftcmdpb.Response { | ||
bm := roaring.NewBTreeBitmap() | ||
tmp := roaring.NewBTreeBitmap() | ||
var target *roaring.Bitmap | ||
for idx, rsp := range rsps { | ||
if len(rsp.BulkResult) > 0 { | ||
tmp.Containers.Reset() | ||
|
||
if idx == 0 { | ||
target = bm | ||
} else { | ||
target = tmp | ||
} | ||
|
||
_, _, err := target.ImportRoaringBits(rsp.BulkResult, false, false, 0) | ||
if err != nil { | ||
return &raftcmdpb.Response{ | ||
ErrorResult: hack.StringToSlice(err.Error()), | ||
} | ||
} | ||
|
||
if idx > 0 { | ||
bm = bm.Union(tmp) | ||
} | ||
} | ||
} | ||
|
||
return p.buildResult(bm, args) | ||
} | ||
|
||
func (p *RedisProxy) doBMAndNotMerge(args [][]byte, rsps ...*raftcmdpb.Response) *raftcmdpb.Response { | ||
bm := roaring.NewBTreeBitmap() | ||
tmp := roaring.NewBTreeBitmap() | ||
var target *roaring.Bitmap | ||
for idx, rsp := range rsps { | ||
if len(rsp.BulkResult) > 0 { | ||
tmp.Containers.Reset() | ||
|
||
if idx == 0 { | ||
target = bm | ||
} else { | ||
target = tmp | ||
} | ||
|
||
_, _, err := target.ImportRoaringBits(rsp.BulkResult, false, false, 0) | ||
if err != nil { | ||
return &raftcmdpb.Response{ | ||
ErrorResult: hack.StringToSlice(err.Error()), | ||
} | ||
} | ||
|
||
if idx > 0 { | ||
bm = bm.Xor(tmp) | ||
} | ||
} | ||
} | ||
|
||
return p.buildResult(bm, args) | ||
} | ||
|
||
func (p *RedisProxy) buildResult(bm *roaring.Bitmap, args [][]byte) *raftcmdpb.Response { | ||
log.Debugf("bm and args: %+v", args) | ||
if len(args) < 2 { | ||
buf := bytes.NewBuffer(nil) | ||
bm.WriteTo(buf) | ||
return &raftcmdpb.Response{ | ||
BulkResult: buf.Bytes(), | ||
} | ||
} | ||
|
||
start, err := format.ParseStrUInt64(hack.SliceToString(args[0])) | ||
if err != nil { | ||
return &raftcmdpb.Response{ | ||
ErrorResult: hack.StringToSlice(err.Error()), | ||
} | ||
} | ||
|
||
limit, err := format.ParseStrUInt64(hack.SliceToString(args[1])) | ||
if err != nil { | ||
return &raftcmdpb.Response{ | ||
ErrorResult: hack.StringToSlice(err.Error()), | ||
} | ||
} | ||
|
||
rsp := &raftcmdpb.Response{} | ||
var values [][]byte | ||
count := uint64(0) | ||
itr := bm.Iterator() | ||
itr.Seek(start) | ||
for { | ||
value, eof := itr.Next() | ||
if eof { | ||
break | ||
} | ||
|
||
values = append(values, format.UInt64ToString(value)) | ||
count++ | ||
|
||
if count >= limit { | ||
break | ||
} | ||
} | ||
rsp.SliceArrayResult = values | ||
rsp.HasEmptySliceArrayResult = len(values) == 0 | ||
return rsp | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package proxy | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
|
||
"github.com/deepfabric/elasticell/pkg/pb/raftcmdpb" | ||
"github.com/fagongzi/goetty/protocol/redis" | ||
) | ||
|
||
func (p *RedisProxy) doMGet(rs *redisSession, cmd redis.Command) (bool, error) { | ||
n := len(cmd.Args()) | ||
|
||
if n < 2 { | ||
return false, nil | ||
} | ||
|
||
id := newID() | ||
rs.addAggregation(id, newAggregationReq(n, p.doMGetMerge, nil)) | ||
|
||
for idx, key := range cmd.Args() { | ||
cmd := redis.Command([][]byte{cmdGet, key}) | ||
p.addToForward(newReqUUID(bytes.Join([][]byte{id, []byte(fmt.Sprintf("%d", idx))}, sep), cmd, rs)) | ||
} | ||
|
||
return true, nil | ||
} | ||
|
||
func (p *RedisProxy) doMGetMerge(args [][]byte, rsps ...*raftcmdpb.Response) *raftcmdpb.Response { | ||
n := len(rsps) | ||
value := make([][]byte, n, n) | ||
for idx, rsp := range rsps { | ||
value[idx] = rsp.BulkResult | ||
} | ||
|
||
return &raftcmdpb.Response{ | ||
SliceArrayResult: value, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -85,6 +85,9 @@ | |
"bmcontains", | ||
"bmdel", | ||
"bmcount", | ||
"bmrange" | ||
"bmrange", | ||
"bmand", | ||
"bmor", | ||
"bmandnot" | ||
] | ||
} |