mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-18 21:40:32 +00:00
Merge pull request #1066 from ithkuil/devel
Transaction support for redis
This commit is contained in:
@@ -45,7 +45,7 @@ proc raiseInvalidReply(expected, got: char) =
|
||||
[$expected, $got])
|
||||
|
||||
proc raiseNoOK(status: string) =
|
||||
if status != "OK":
|
||||
if status != "QUEUED" and status != "OK":
|
||||
raise newException(EInvalidReply, "Expected \"OK\" got \"$1\"" % status)
|
||||
|
||||
proc parseStatus(r: TRedis): TRedisStatus =
|
||||
@@ -64,6 +64,10 @@ proc parseStatus(r: TRedis): TRedisStatus =
|
||||
proc parseInteger(r: TRedis): TRedisInteger =
|
||||
var line = ""
|
||||
r.socket.readLine(line)
|
||||
|
||||
if line == "+QUEUED": # inside of multi
|
||||
return -1
|
||||
|
||||
if line == "":
|
||||
raise newException(ERedis, "Server closed connection prematurely")
|
||||
|
||||
@@ -81,10 +85,7 @@ proc recv(sock: TSocket, size: int): TaintedString =
|
||||
if sock.recv(cstring(result), size) != size:
|
||||
raise newException(EInvalidReply, "recv failed")
|
||||
|
||||
proc parseBulk(r: TRedis, allowMBNil = False): TRedisString =
|
||||
var line = ""
|
||||
r.socket.readLine(line.TaintedString)
|
||||
|
||||
proc parseSingle(r: TRedis, line:string, allowMBNil = False): TRedisString =
|
||||
# Error.
|
||||
if line[0] == '-':
|
||||
raise newException(ERedis, strip(line))
|
||||
@@ -94,6 +95,9 @@ proc parseBulk(r: TRedis, allowMBNil = False): TRedisString =
|
||||
if line == "*-1":
|
||||
return RedisNil
|
||||
|
||||
if line == "+QUEUED" or line == "+OK" : # inside of a transaction (multi)
|
||||
return nil
|
||||
|
||||
if line[0] != '$':
|
||||
raiseInvalidReply('$', line[0])
|
||||
|
||||
@@ -104,18 +108,41 @@ proc parseBulk(r: TRedis, allowMBNil = False): TRedisString =
|
||||
var s = r.socket.recv(numBytes+2)
|
||||
result = strip(s.string)
|
||||
|
||||
proc parseMultiBulk(r: TRedis): TRedisList =
|
||||
var line = TaintedString""
|
||||
r.socket.readLine(line)
|
||||
|
||||
if line.string[0] != '*':
|
||||
raiseInvalidReply('*', line.string[0])
|
||||
|
||||
var numElems = parseInt(line.string.substr(1))
|
||||
proc parseMultiLines(r: TRedis, countLine:string): TRedisList =
|
||||
if countLine.string[0] != '*':
|
||||
raiseInvalidReply('*', countLine.string[0])
|
||||
|
||||
var numElems = parseInt(countLine.string.substr(1))
|
||||
if numElems == -1: return nil
|
||||
result = @[]
|
||||
for i in 1..numElems:
|
||||
result.add(r.parseBulk())
|
||||
var line = ""
|
||||
r.socket.readLine(line.TaintedString)
|
||||
if line[0] == '*': # after exec() may contain more multi-bulk replies
|
||||
var parsed = r.parseMultiLines(line)
|
||||
for item in parsed:
|
||||
result.add(item)
|
||||
else:
|
||||
result.add(r.parseSingle(line))
|
||||
|
||||
proc parseBulk(r: TRedis, allowMBNil = False): TRedisString =
|
||||
var line = ""
|
||||
r.socket.readLine(line.TaintedString)
|
||||
|
||||
if line == "+QUEUED" or line == "+OK": # inside of a transaction (multi)
|
||||
return nil
|
||||
|
||||
return r.parseSingle(line, allowMBNil)
|
||||
|
||||
proc parseMultiBulk(r: TRedis): TRedisList =
|
||||
var line = TaintedString""
|
||||
r.socket.readLine(line)
|
||||
|
||||
if line == "+QUEUED": # inside of a transaction (multi)
|
||||
return nil
|
||||
|
||||
return r.parseMultiLines(line)
|
||||
|
||||
|
||||
proc sendCommand(r: TRedis, cmd: string, args: varargs[string]) =
|
||||
var request = "*" & $(1 + args.len()) & "\c\L"
|
||||
@@ -722,6 +749,7 @@ proc discardMulti*(r: TRedis) =
|
||||
proc exec*(r: TRedis): TRedisList =
|
||||
## Execute all commands issued after MULTI
|
||||
r.sendCommand("EXEC")
|
||||
|
||||
return r.parseMultiBulk()
|
||||
|
||||
proc multi*(r: TRedis) =
|
||||
|
||||
Reference in New Issue
Block a user