|
| 1 | +package redis |
| 2 | + |
| 3 | +import "context" |
| 4 | + |
| 5 | +type BitMapCmdable interface { |
| 6 | + GetBit(ctx context.Context, key string, offset int64) *IntCmd |
| 7 | + SetBit(ctx context.Context, key string, offset int64, value int) *IntCmd |
| 8 | + BitCount(ctx context.Context, key string, bitCount *BitCount) *IntCmd |
| 9 | + BitOpAnd(ctx context.Context, destKey string, keys ...string) *IntCmd |
| 10 | + BitOpOr(ctx context.Context, destKey string, keys ...string) *IntCmd |
| 11 | + BitOpXor(ctx context.Context, destKey string, keys ...string) *IntCmd |
| 12 | + BitOpNot(ctx context.Context, destKey string, key string) *IntCmd |
| 13 | + BitPos(ctx context.Context, key string, bit int64, pos ...int64) *IntCmd |
| 14 | + BitPosSpan(ctx context.Context, key string, bit int8, start, end int64, span string) *IntCmd |
| 15 | + BitField(ctx context.Context, key string, values ...interface{}) *IntSliceCmd |
| 16 | +} |
| 17 | + |
| 18 | +func (c cmdable) GetBit(ctx context.Context, key string, offset int64) *IntCmd { |
| 19 | + cmd := NewIntCmd(ctx, "getbit", key, offset) |
| 20 | + _ = c(ctx, cmd) |
| 21 | + return cmd |
| 22 | +} |
| 23 | + |
| 24 | +func (c cmdable) SetBit(ctx context.Context, key string, offset int64, value int) *IntCmd { |
| 25 | + cmd := NewIntCmd( |
| 26 | + ctx, |
| 27 | + "setbit", |
| 28 | + key, |
| 29 | + offset, |
| 30 | + value, |
| 31 | + ) |
| 32 | + _ = c(ctx, cmd) |
| 33 | + return cmd |
| 34 | +} |
| 35 | + |
| 36 | +type BitCount struct { |
| 37 | + Start, End int64 |
| 38 | +} |
| 39 | + |
| 40 | +func (c cmdable) BitCount(ctx context.Context, key string, bitCount *BitCount) *IntCmd { |
| 41 | + args := []interface{}{"bitcount", key} |
| 42 | + if bitCount != nil { |
| 43 | + args = append( |
| 44 | + args, |
| 45 | + bitCount.Start, |
| 46 | + bitCount.End, |
| 47 | + ) |
| 48 | + } |
| 49 | + cmd := NewIntCmd(ctx, args...) |
| 50 | + _ = c(ctx, cmd) |
| 51 | + return cmd |
| 52 | +} |
| 53 | + |
| 54 | +func (c cmdable) bitOp(ctx context.Context, op, destKey string, keys ...string) *IntCmd { |
| 55 | + args := make([]interface{}, 3+len(keys)) |
| 56 | + args[0] = "bitop" |
| 57 | + args[1] = op |
| 58 | + args[2] = destKey |
| 59 | + for i, key := range keys { |
| 60 | + args[3+i] = key |
| 61 | + } |
| 62 | + cmd := NewIntCmd(ctx, args...) |
| 63 | + _ = c(ctx, cmd) |
| 64 | + return cmd |
| 65 | +} |
| 66 | + |
| 67 | +func (c cmdable) BitOpAnd(ctx context.Context, destKey string, keys ...string) *IntCmd { |
| 68 | + return c.bitOp(ctx, "and", destKey, keys...) |
| 69 | +} |
| 70 | + |
| 71 | +func (c cmdable) BitOpOr(ctx context.Context, destKey string, keys ...string) *IntCmd { |
| 72 | + return c.bitOp(ctx, "or", destKey, keys...) |
| 73 | +} |
| 74 | + |
| 75 | +func (c cmdable) BitOpXor(ctx context.Context, destKey string, keys ...string) *IntCmd { |
| 76 | + return c.bitOp(ctx, "xor", destKey, keys...) |
| 77 | +} |
| 78 | + |
| 79 | +func (c cmdable) BitOpNot(ctx context.Context, destKey string, key string) *IntCmd { |
| 80 | + return c.bitOp(ctx, "not", destKey, key) |
| 81 | +} |
| 82 | + |
| 83 | +// BitPos is an API before Redis version 7.0, cmd: bitpos key bit start end |
| 84 | +// if you need the `byte | bit` parameter, please use `BitPosSpan`. |
| 85 | +func (c cmdable) BitPos(ctx context.Context, key string, bit int64, pos ...int64) *IntCmd { |
| 86 | + args := make([]interface{}, 3+len(pos)) |
| 87 | + args[0] = "bitpos" |
| 88 | + args[1] = key |
| 89 | + args[2] = bit |
| 90 | + switch len(pos) { |
| 91 | + case 0: |
| 92 | + case 1: |
| 93 | + args[3] = pos[0] |
| 94 | + case 2: |
| 95 | + args[3] = pos[0] |
| 96 | + args[4] = pos[1] |
| 97 | + default: |
| 98 | + panic("too many arguments") |
| 99 | + } |
| 100 | + cmd := NewIntCmd(ctx, args...) |
| 101 | + _ = c(ctx, cmd) |
| 102 | + return cmd |
| 103 | +} |
| 104 | + |
| 105 | +// BitPosSpan supports the `byte | bit` parameters in redis version 7.0, |
| 106 | +// the bitpos command defaults to using byte type for the `start-end` range, |
| 107 | +// which means it counts in bytes from start to end. you can set the value |
| 108 | +// of "span" to determine the type of `start-end`. |
| 109 | +// span = "bit", cmd: bitpos key bit start end bit |
| 110 | +// span = "byte", cmd: bitpos key bit start end byte |
| 111 | +func (c cmdable) BitPosSpan(ctx context.Context, key string, bit int8, start, end int64, span string) *IntCmd { |
| 112 | + cmd := NewIntCmd(ctx, "bitpos", key, bit, start, end, span) |
| 113 | + _ = c(ctx, cmd) |
| 114 | + return cmd |
| 115 | +} |
| 116 | + |
| 117 | +// BitField accepts multiple values: |
| 118 | +// - BitField("set", "i1", "offset1", "value1","cmd2", "type2", "offset2", "value2") |
| 119 | +// - BitField([]string{"cmd1", "type1", "offset1", "value1","cmd2", "type2", "offset2", "value2"}) |
| 120 | +// - BitField([]interface{}{"cmd1", "type1", "offset1", "value1","cmd2", "type2", "offset2", "value2"}) |
| 121 | +func (c cmdable) BitField(ctx context.Context, key string, values ...interface{}) *IntSliceCmd { |
| 122 | + args := make([]interface{}, 2, 2+len(values)) |
| 123 | + args[0] = "bitfield" |
| 124 | + args[1] = key |
| 125 | + args = appendArgs(args, values) |
| 126 | + cmd := NewIntSliceCmd(ctx, args...) |
| 127 | + _ = c(ctx, cmd) |
| 128 | + return cmd |
| 129 | +} |
0 commit comments