From dc8a5763836a78c2116b5070bf69ae0252637c73 Mon Sep 17 00:00:00 2001 From: Bertrand Paquet Date: Sun, 17 Sep 2017 21:58:29 -0700 Subject: [PATCH] Do not use LUA for array push --- commands.go | 26 +++++++++++++++++--------- redis.lua | 32 -------------------------------- writers.go | 12 ++++++++++++ 3 files changed, 29 insertions(+), 41 deletions(-) diff --git a/commands.go b/commands.go index e50ac63..864e1c7 100644 --- a/commands.go +++ b/commands.go @@ -155,20 +155,24 @@ func cmdHDEL(wf io.Writer, ctx *context, args [][]byte) error { return writeLine(wf, ":"+strconv.Itoa(rec.(int))) } -func arrayPush(wf io.Writer, ctx *context, args [][]byte, f string, ttl int) error { +func listOpReturnSize(wf io.Writer, ctx *context, args [][]byte, ttl int, op *as.Operation) error { key, err := buildKey(ctx, args[0]) if err != nil { return err } - rec, err := ctx.client.Execute(ctx.writePolicy, key, MODULE_NAME, f, as.NewValue(binName), as.NewValue(encode(ctx, args[1])), as.NewValue(ttl)) + rec, err := ctx.client.Operate(fillWritePolicyEx(ttl, false), key, op, as.ListSizeOp(binName)) if err != nil { return err } - return writeLine(wf, ":"+strconv.Itoa(rec.(int))) + return writeBinIntListSize(wf, rec, binName) +} + +func arrayRPush(wf io.Writer, ctx *context, args [][]byte, ttl int) error { + return listOpReturnSize(wf, ctx, args, ttl, as.ListAppendOp(binName, encode(ctx, args[1]))) } func cmdRPUSH(wf io.Writer, ctx *context, args [][]byte) error { - return arrayPush(wf, ctx, args, "RPUSH", -1) + return arrayRPush(wf, ctx, args, -1) } func cmdRPUSHEX(wf io.Writer, ctx *context, args [][]byte) error { @@ -177,11 +181,15 @@ func cmdRPUSHEX(wf io.Writer, ctx *context, args [][]byte) error { return err } - return arrayPush(wf, ctx, args, "RPUSH", ttl) + return arrayRPush(wf, ctx, args, ttl) +} + +func arrayLPush(wf io.Writer, ctx *context, args [][]byte, ttl int) error { + return listOpReturnSize(wf, ctx, args, ttl, as.ListInsertOp(binName, 0, encode(ctx, args[1]))) } func cmdLPUSH(wf io.Writer, ctx *context, args [][]byte) error { - return arrayPush(wf, ctx, args, "LPUSH", -1) + return arrayLPush(wf, ctx, args, -1) } func cmdLPUSHEX(wf io.Writer, ctx *context, args [][]byte) error { @@ -190,7 +198,7 @@ func cmdLPUSHEX(wf io.Writer, ctx *context, args [][]byte) error { return err } - return arrayPush(wf, ctx, args, "LPUSH", ttl) + return arrayLPush(wf, ctx, args, ttl) } func arrayPop(wf io.Writer, ctx *context, args [][]byte, f string) error { @@ -243,11 +251,11 @@ func cmdLLEN(wf io.Writer, ctx *context, args [][]byte) error { if err != nil { return err } - rec, err := ctx.client.Get(ctx.readPolicy, key, binName+"_size") + rec, err := ctx.client.Operate(ctx.writePolicy, key, as.ListSizeOp(binName)) if err != nil { return err } - return writeBinInt(wf, rec, binName+"_size") + return writeBinInt(wf, rec, binName) } func cmdLRANGE(wf io.Writer, ctx *context, args [][]byte) error { diff --git a/redis.lua b/redis.lua index c6481f9..268c942 100644 --- a/redis.lua +++ b/redis.lua @@ -42,22 +42,6 @@ function LPOP(rec, bin, count, ttl) return nil end -function LPUSH(rec, bin, value, ttl) - local l = rec[bin] - if (l == nil) then - l = list() - end - list.prepend(l, value) - rec[bin] = l - local length = #l - rec[bin .. '_size']= length - if (ttl ~= -1) then - record.set_ttl(rec, ttl) - end - UPDATE(rec) - return length -end - local function ARRAY_RANGE (rec, bin, start, stop) if (EXISTS(rec, bin)) then local l = rec[bin] @@ -144,22 +128,6 @@ function RPOP (rec, bin, count, ttl) return nil end -function RPUSH (rec, bin, value, ttl) - local l = rec[bin] - if (l == nil) then - l = list() - end - list.append(l, value) - rec[bin] = l - local length = #l - rec[bin .. '_size']= length - if (ttl ~= -1) then - record.set_ttl(rec, ttl) - end - UPDATE(rec) - return length -end - function HSET (rec, bin, value) local created = 1 if (EXISTS(rec, bin)) then diff --git a/writers.go b/writers.go index 87b8ce5..68e400d 100644 --- a/writers.go +++ b/writers.go @@ -139,6 +139,18 @@ func writeBinInt(wf io.Writer, rec *as.Record, binName string) error { return writeLine(wf, ":"+strconv.Itoa(x.(int))) } +func writeBinIntListSize(wf io.Writer, rec *as.Record, binName string) error { + nilValue := ":0" + if rec == nil { + return writeLine(wf, nilValue) + } + x := rec.Bins[binName] + if x == nil { + return writeLine(wf, nilValue) + } + return writeLine(wf, ":"+strconv.Itoa(x.([]interface{})[0].(int))) +} + func writeArrayBin(wf io.Writer, res []*as.Record, binName string, keyBinName string) error { l := len(res) if keyBinName != "" {