Skip to content

Commit

Permalink
Do not use LUA for array push
Browse files Browse the repository at this point in the history
  • Loading branch information
bpaquet committed Sep 18, 2017
1 parent 35fe3d4 commit dc8a576
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 41 deletions.
26 changes: 17 additions & 9 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
32 changes: 0 additions & 32 deletions redis.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions writers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand Down

0 comments on commit dc8a576

Please sign in to comment.