-
-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Redis reversed history fix when meta does not exist (#328)
- Loading branch information
Showing
6 changed files
with
184 additions
and
144 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
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,32 @@ | ||
local list_key = KEYS[1] | ||
local meta_key = KEYS[2] | ||
local message_payload = ARGV[1] | ||
local ltrim_right_bound = ARGV[2] | ||
local list_ttl = ARGV[3] | ||
local channel = ARGV[4] | ||
local meta_expire = ARGV[5] | ||
local new_epoch_if_empty = ARGV[6] | ||
local publish_command = ARGV[7] | ||
|
||
local current_epoch = redis.call("hget", meta_key, "e") | ||
if current_epoch == false then | ||
current_epoch = new_epoch_if_empty | ||
redis.call("hset", meta_key, "e", current_epoch) | ||
end | ||
|
||
local top_offset = redis.call("hincrby", meta_key, "s", 1) | ||
|
||
if meta_expire ~= '0' then | ||
redis.call("expire", meta_key, meta_expire) | ||
end | ||
|
||
local payload = "__" .. "p1:" .. top_offset .. ":" .. current_epoch .. "__" .. message_payload | ||
redis.call("lpush", list_key, payload) | ||
redis.call("ltrim", list_key, 0, ltrim_right_bound) | ||
redis.call("expire", list_key, list_ttl) | ||
|
||
if channel ~= '' then | ||
redis.call(publish_command, channel, payload) | ||
end | ||
|
||
return {top_offset, current_epoch} |
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,31 @@ | ||
local stream_key = KEYS[1] | ||
local meta_key = KEYS[2] | ||
local message_payload = ARGV[1] | ||
local stream_size = ARGV[2] | ||
local stream_ttl = ARGV[3] | ||
local channel = ARGV[4] | ||
local meta_expire = ARGV[5] | ||
local new_epoch_if_empty = ARGV[6] | ||
local publish_command = ARGV[7] | ||
|
||
local current_epoch = redis.call("hget", meta_key, "e") | ||
if current_epoch == false then | ||
current_epoch = new_epoch_if_empty | ||
redis.call("hset", meta_key, "e", current_epoch) | ||
end | ||
|
||
local top_offset = redis.call("hincrby", meta_key, "s", 1) | ||
|
||
if meta_expire ~= '0' then | ||
redis.call("expire", meta_key, meta_expire) | ||
end | ||
|
||
redis.call("xadd", stream_key, "MAXLEN", stream_size, top_offset, "d", message_payload) | ||
redis.call("expire", stream_key, stream_ttl) | ||
|
||
if channel ~= '' then | ||
local payload = "__" .. "p1:" .. top_offset .. ":" .. current_epoch .. "__" .. message_payload | ||
redis.call(publish_command, channel, payload) | ||
end | ||
|
||
return {top_offset, current_epoch} |
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,26 @@ | ||
local list_key = KEYS[1] | ||
local meta_key = KEYS[2] | ||
local include_publications = ARGV[1] | ||
local list_right_bound = ARGV[2] | ||
local meta_expire = ARGV[3] | ||
local new_epoch_if_empty = ARGV[4] | ||
|
||
local stream_meta = redis.call("hmget", meta_key, "e", "s") | ||
local current_epoch, top_offset = stream_meta[1], stream_meta[2] | ||
|
||
if current_epoch == false then | ||
current_epoch = new_epoch_if_empty | ||
top_offset = 0 | ||
redis.call("hset", meta_key, "e", current_epoch, "s", "0") | ||
end | ||
|
||
if meta_expire ~= '0' then | ||
redis.call("expire", meta_key, meta_expire) | ||
end | ||
|
||
local pubs = nil | ||
if include_publications ~= "0" then | ||
pubs = redis.call("lrange", list_key, 0, list_right_bound) | ||
end | ||
|
||
return {top_offset, current_epoch, pubs} |
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,49 @@ | ||
local stream_key = KEYS[1] | ||
local meta_key = KEYS[2] | ||
local include_publications = ARGV[1] | ||
local since_offset = ARGV[2] | ||
local limit = ARGV[3] | ||
local reverse = ARGV[4] | ||
local meta_expire = ARGV[5] | ||
local new_epoch_if_empty = ARGV[6] | ||
|
||
local stream_meta = redis.call("hmget", meta_key, "e", "s") | ||
local current_epoch, top_offset = stream_meta[1], stream_meta[2] | ||
|
||
if current_epoch == false then | ||
current_epoch = new_epoch_if_empty | ||
top_offset = 0 | ||
redis.call("hset", meta_key, "e", current_epoch, "s", "0") | ||
end | ||
|
||
if meta_expire ~= '0' then | ||
redis.call("expire", meta_key, meta_expire) | ||
end | ||
|
||
local pubs = nil | ||
|
||
if include_publications ~= "0" then | ||
if limit ~= "0" then | ||
if reverse == "0" then | ||
pubs = redis.call("xrange", stream_key, since_offset, "+", "COUNT", limit) | ||
else | ||
local get_offset = top_offset | ||
if since_offset ~= "0" then | ||
get_offset = since_offset | ||
end | ||
pubs = redis.call("xrevrange", stream_key, get_offset, "-", "COUNT", limit) | ||
end | ||
else | ||
if reverse == "0" then | ||
pubs = redis.call("xrange", stream_key, since_offset, "+") | ||
else | ||
local get_offset = top_offset | ||
if since_offset ~= "0" then | ||
get_offset = since_offset | ||
end | ||
pubs = redis.call("xrevrange", stream_key, get_offset, "-") | ||
end | ||
end | ||
end | ||
|
||
return {top_offset, current_epoch, pubs} |