-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathscripts.go
143 lines (125 loc) · 5.69 KB
/
scripts.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright 2015 Alex Browne. All rights reserved.
// Use of this source code is governed by the MIT
// license, which can be found in the LICENSE file.
// File scripts.go contains code related to parsing
// lua scripts in the scripts file.
// This file has been automatically generated by go generate,
// which calls scripts/main.go. Do not edit it directly!
package zoom
import (
"github.com/garyburd/redigo/redis"
)
var (
deleteModelsBySetIdsScript = redis.NewScript(0, `-- Copyright 2015 Alex Browne. All rights reserved.
-- Use of this source code is governed by the MIT
-- license, which can be found in the LICENSE file.
-- delete_models_by_set_ids is a lua script that takes the following arguments:
-- 1) The key of a set of model ids
-- 2) The name of a registered model
-- The script then deletes all the models corresponding to the ids in the given
-- set. It returns the number of models that were deleted. It does not delete the
-- given set.
-- IMPORTANT: If you edit this file, you must run go generate . to rewrite ../scripts.go
-- Assign keys to variables for easy access
local setKey = ARGV[1]
local collectionName = ARGV[2]
-- Get all the ids from the set name
local ids = redis.call('SMEMBERS', setKey)
local count = 0
if #ids > 0 then
-- Iterate over the ids
for i, id in ipairs(ids) do
-- Delete the main hash for each model
local key = collectionName .. ':' .. id
count = count + redis.call('DEL', key)
-- Remove the model id from the set of all ids
-- NOTE: this is not necessarily the same as the
-- setName we were given
local setKey = collectionName .. ':all'
redis.call('SREM', setKey, id)
end
end
return count
`)
deleteStringIndexScript = redis.NewScript(0, `-- Copyright 2015 Alex Browne. All rights reserved.
-- Use of this source code is governed by the MIT
-- license, which can be found in the LICENSE file.
-- delete_string_index is a lua script that takes the following arguments:
-- 1) The name of a registered model
-- 2) The id of the model to be deleted from the index
-- 3) The name of the indexed string field
-- The script then checks if there is a value for the given field name stored in the
-- model hash, and if there is, removes the model from the index on the given field.
-- NOTE: This script *must* be called before the main hash for the model is updated/deleted.
-- IMPORTANT: If you edit this file, you must run go generate . to rewrite ../scripts.go
-- Assign keys to variables for easy access
local collectionName = ARGV[1]
local modelID = ARGV[2]
local fieldName = ARGV[3]
-- Get the old value from the existing model hash (if any)
local modelKey = collectionName .. ":" .. modelID
local oldValue = redis.call("HGET", modelKey, fieldName)
local indexKey = collectionName .. ":" .. fieldName
if oldValue ~= false then
-- Remove the model from the field index
local oldMember = oldValue .. "\0" .. modelID
redis.call("ZREM", indexKey, oldMember)
end
`)
extractIdsFromFieldIndexScript = redis.NewScript(0, `-- Copyright 2015 Alex Browne. All rights reserved.
-- Use of this source code is governed by the MIT
-- license, which can be found in the LICENSE file.
-- exctract_ids_from_field_index is a lua script that takes the following arguments:
-- 1) setKey: The key of a sorted set for a field index (either numeric or bool)
-- 2) destKey: The key of a sorted set where the resulting ids will be stored
-- 3) min: The min argument for the ZRANGEBYSCORE command
-- 4) max: The max argument for the ZRANGEBYSCORE command
-- The script then calls ZRANGEBYSCORE on setKey with the given min and max arguments,
-- and then stores the resulting set in destKey. It does not preserve the existing
-- scores, and instead just replaces scores with sequential numbers to keep the members
-- in the same order.
-- IMPORTANT: If you edit this file, you must run go generate . to rewrite ../scripts.go
-- Assign keys to variables for easy access
local setKey = ARGV[1]
local destKey = ARGV[2]
local min = ARGV[3]
local max = ARGV[4]
-- Get all the members (value+id pairs) from the sorted set
local members = redis.call('ZRANGEBYSCORE', setKey, min, max)
-- Iterate over the members and add each to the destKey
for i, member in ipairs(members) do
redis.call('ZADD', destKey, i, member)
end
`)
extractIdsFromStringIndexScript = redis.NewScript(0, `-- Copyright 2015 Alex Browne. All rights reserved.
-- Use of this source code is governed by the MIT
-- license, which can be found in the LICENSE file.
-- exctract_ids_from_string_index is a lua script that takes the following arguments:
-- 1) setKey: The key of a sorted set for a string index, where each member is of the
-- form: value + NULL + id, where NULL is the ASCII NULL character which has a codepoint
-- value of 0.
-- 2) destKey: The key of a sorted set where the resulting ids will be stored
-- 3) min: The min argument for the ZRANGEBYLEX command
-- 4) max: The max argument for the ZRANGEBYLEX command
-- The script then extracts the ids from setKey using the given min and max arguments,
-- and then stores them destKey with the appropriate scores in ascending order.
-- IMPORTANT: If you edit this file, you must run go generate . to rewrite ../scripts.go
-- Assign keys to variables for easy access
local setKey = ARGV[1]
local destKey = ARGV[2]
local min = ARGV[3]
local max = ARGV[4]
-- Get all the members (value+id pairs) from the sorted set
local members = redis.call('ZRANGEBYLEX', setKey, min, max)
if #members > 0 then
-- Iterate over the members and extract the ids
for i, member in ipairs(members) do
-- The id is everything after the last space
-- Find the index of the last space
local idStart = string.find(member, '%z[^%z]*$')
local id = string.sub(member, idStart+1)
redis.call('ZADD', destKey, i, id)
end
end
`)
)