@iobroker/db-objects-redis 4.0.0-alpha.9-20211115-5dac659e → 4.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -3,4 +3,4 @@ The Library contains the Database classes for Redis based objects database clien
3
3
 
4
4
  ## License
5
5
  Apache 2.0
6
- Copyright 2018-2020 bluefox <dogafox@gmail.com>
6
+ Copyright 2018-2022 bluefox <dogafox@gmail.com>
package/index.js CHANGED
@@ -3,6 +3,6 @@ module.exports = {
3
3
  Server: null,
4
4
  objectsUtils: require('./lib/objects/objectsUtils.js'),
5
5
  getDefaultPort: host => {
6
- return (host.includes(',')) ? 26379 : 6379;
6
+ return host.includes(',') ? 26379 : 6379;
7
7
  }
8
8
  };
@@ -1,5 +1,5 @@
1
- -- design: custom
2
- -- search: state
1
+ -- design: system
2
+ -- search: custom
3
3
  local rep = {}
4
4
  -- local keys=redis.call("keys", KEYS[1].."*")
5
5
  local cursor = KEYS[4];
@@ -8,6 +8,7 @@ cursor = result[1]
8
8
  local keys = result[2]
9
9
  local argStart = KEYS[1] .. KEYS[2]
10
10
  local argEnd = KEYS[1] .. KEYS[3]
11
+ local checkStr = string.format("%q:{", "custom")
11
12
  -- function(doc) {
12
13
  -- if (doc.type==="state" && (doc.common.custom || doc.common.history))
13
14
  -- emit(doc._id, doc.common.custom || doc.common.history)
@@ -15,9 +16,11 @@ local argEnd = KEYS[1] .. KEYS[3]
15
16
  for _, key in ipairs(keys) do
16
17
  if (key >= argStart and key < argEnd) then
17
18
  local obj = redis.call("get", key)
18
- local success, decoded = pcall(cjson.decode, obj)
19
- if (success and decoded.type == "state" and decoded.common ~= nil and decoded.common.custom ~= nil) then
20
- rep[#rep + 1] = obj
19
+ if (obj:find(checkStr) ~= nil) then
20
+ local success, decoded = pcall(cjson.decode, obj)
21
+ if (success and decoded.type == "state" and decoded.common ~= nil and decoded.common.custom ~= nil) then
22
+ rep[#rep + 1] = obj
23
+ end
21
24
  end
22
25
  end
23
26
  end
@@ -8,13 +8,17 @@ local keys = result[2]
8
8
  local argStart = KEYS[1] .. KEYS[2]
9
9
  local argEnd = KEYS[1] .. KEYS[3]
10
10
  local type = KEYS[4]
11
+ local checkStr = string.format("%q:%q", "type", type)
12
+ local skipCheck = not (type == "channel" or type == "device" or type == "folder")
11
13
  -- function(doc) { if (doc.type === "chart") emit(doc._id, doc); }
12
14
  for _, key in ipairs(keys) do
13
15
  if (key >= argStart and key < argEnd) then
14
16
  local obj = redis.call("get", key)
15
- local success, decoded = pcall(cjson.decode, obj)
16
- if (success and decoded.type == type) then
17
- rep[#rep + 1] = obj
17
+ if (skipCheck or obj:find(checkStr) ~= nil) then
18
+ local success, decoded = pcall(cjson.decode, obj)
19
+ if (success and decoded.type == type) then
20
+ rep[#rep + 1] = obj
21
+ end
18
22
  end
19
23
  end
20
24
  end
@@ -7,6 +7,7 @@ cursor = result[1]
7
7
  local keys = result[2]
8
8
  local argStart = KEYS[1] .. KEYS[2]
9
9
  local argEnd = KEYS[1] .. KEYS[3]
10
+ local checkStr = string.format("%q:%q", "TypeName", "PROGRAM")
10
11
  -- function(doc) {
11
12
  -- if (doc._id.match(/^hm-rega\\.[0-9]+\\.[0-9,A-Z,a-z]+/) && (doc.native.TypeName === "PROGRAM")) {
12
13
  -- emit(doc._id, doc);
@@ -15,9 +16,11 @@ local argEnd = KEYS[1] .. KEYS[3]
15
16
  for _, key in ipairs(keys) do
16
17
  if (key >= argStart and key < argEnd and key:sub(7, 13) == "hm-rega") then
17
18
  local obj = redis.call("get", key)
18
- local success, decoded = pcall(cjson.decode, obj)
19
- if (success and decoded.native ~= nil and decoded.native.TypeName == "PROGRAM") then
20
- rep[#rep + 1] = obj
19
+ if (obj:find(checkStr) ~= nil) then
20
+ local success, decoded = pcall(cjson.decode, obj)
21
+ if (success and decoded.native ~= nil and decoded.native.TypeName == "PROGRAM") then
22
+ rep[#rep + 1] = obj
23
+ end
21
24
  end
22
25
  end
23
26
  end
@@ -7,14 +7,17 @@ cursor = result[1]
7
7
  local keys = result[2]
8
8
  local argStart = KEYS[1] .. KEYS[2]
9
9
  local argEnd = KEYS[1] .. KEYS[3]
10
+ local checkStr = string.format("%q:%q", "type", "script")
10
11
  -- function(doc) {
11
12
  -- if (doc.type === "script" && doc.common.engineType.match(/^[jJ]ava[sS]cript|^[cC]offee[sS]cript|^[tT]ype[sS]cript|^Blockly/)) emit(doc.common.name, doc); }
12
13
  for _, key in ipairs(keys) do
13
14
  if (key >= argStart and key < argEnd) then
14
15
  local obj = redis.call("get", key)
15
- local success, decoded = pcall(cjson.decode, obj)
16
- if (success and decoded.type == "script") then
17
- rep[#rep + 1] = obj
16
+ if (obj:find(checkStr) ~= nil) then
17
+ local success, decoded = pcall(cjson.decode, obj)
18
+ if (success and decoded.type == "script") then
19
+ rep[#rep + 1] = obj
20
+ end
18
21
  end
19
22
  end
20
23
  end
@@ -8,6 +8,7 @@ cursor = result[1]
8
8
  local keys = result[2]
9
9
  local argStart = KEYS[1] .. KEYS[2]
10
10
  local argEnd = KEYS[1] .. KEYS[3]
11
+ local checkStr = string.format("%q:", "TypeName");
11
12
  -- function(doc) {
12
13
  -- if (doc._id.match(/^hm-rega\\.[0-9]+\\.[0-9,A-Z,a-z]+/) && (doc.native.TypeName === "ALARMDP" || doc.native.TypeName === "VARDP")) {
13
14
  -- emit(doc._id, doc);
@@ -16,9 +17,11 @@ local argEnd = KEYS[1] .. KEYS[3]
16
17
  for _, key in ipairs(keys) do
17
18
  if (key >= argStart and key < argEnd and key:sub(7, 13) == "hm-rega") then
18
19
  local obj = redis.call("get", key)
19
- local success, decoded = pcall(cjson.decode, obj)
20
- if (success and decoded.native ~= nil and (decoded.native.TypeName == "ALARMDP" or decoded.native.TypeName == "VARDP")) then
21
- rep[#rep + 1] = obj
20
+ if (obj:find(checkStr) ~= nil) then
21
+ local success, decoded = pcall(cjson.decode, obj)
22
+ if (success and decoded.native ~= nil and (decoded.native.TypeName == "ALARMDP" or decoded.native.TypeName == "VARDP")) then
23
+ rep[#rep + 1] = obj
24
+ end
22
25
  end
23
26
  end
24
27
  end
@@ -0,0 +1,20 @@
1
+ -- design: system
2
+ -- search: custom
3
+ local rep = {}
4
+ -- local keys=redis.call("keys", KEYS[1].."*")
5
+ local cursor = KEYS[4];
6
+ local result = redis.call("SSCAN", KEYS[5], cursor, "MATCH", KEYS[1] .. "*", "COUNT", 500)
7
+ cursor = result[1]
8
+ local keys = result[2]
9
+ local argStart = KEYS[1] .. KEYS[2]
10
+ local argEnd = KEYS[1] .. KEYS[3]
11
+ -- function(doc) {
12
+ -- if (doc.type==="state" && (doc.common.custom || doc.common.history))
13
+ -- emit(doc._id, doc.common.custom || doc.common.history)
14
+ -- }
15
+ for _, key in ipairs(keys) do
16
+ if (key >= argStart and key < argEnd) then
17
+ rep[#rep + 1] = redis.call("get", key)
18
+ end
19
+ end
20
+ return { rep, cursor }
@@ -0,0 +1,16 @@
1
+ -- func: function(doc) { if (doc.type === "%1") emit(doc._id, doc); }
2
+ local rep = {}
3
+ -- local keys=redis.call("keys", KEYS[1].."*")
4
+ local cursor = KEYS[5];
5
+ local result = redis.call("SSCAN", KEYS[6], cursor, "MATCH", KEYS[1] .. "*", "COUNT", 500)
6
+ cursor = result[1]
7
+ local keys = result[2]
8
+ local argStart = KEYS[1] .. KEYS[2]
9
+ local argEnd = KEYS[1] .. KEYS[3]
10
+ -- function(doc) { if (doc.type === "chart") emit(doc._id, doc); }
11
+ for _, key in ipairs(keys) do
12
+ if (key >= argStart and key < argEnd) then
13
+ rep[#rep + 1] = redis.call("get", key)
14
+ end
15
+ end
16
+ return { rep, cursor }
@@ -0,0 +1,27 @@
1
+ -- design: hm-rega
2
+ -- search: programs
3
+ local rep = {}
4
+ local cursor = KEYS[4];
5
+ local result = redis.call("SSCAN", KEYS[5], cursor, "MATCH", KEYS[1] .. "*", "COUNT", 500)
6
+ cursor = result[1]
7
+ local keys = result[2]
8
+ local argStart = KEYS[1] .. KEYS[2]
9
+ local argEnd = KEYS[1] .. KEYS[3]
10
+ local checkStr = string.format("%q:%q", "TypeName", "PROGRAM")
11
+ -- function(doc) {
12
+ -- if (doc._id.match(/^hm-rega\\.[0-9]+\\.[0-9,A-Z,a-z]+/) && (doc.native.TypeName === "PROGRAM")) {
13
+ -- emit(doc._id, doc);
14
+ -- }
15
+ -- }
16
+ for _, key in ipairs(keys) do
17
+ if (key >= argStart and key < argEnd) then
18
+ local obj = redis.call("get", key)
19
+ if (obj:find(checkStr) ~= nil) then
20
+ local success, decoded = pcall(cjson.decode, obj)
21
+ if (success and decoded.native ~= nil and decoded.native.TypeName == "PROGRAM") then
22
+ rep[#rep + 1] = obj
23
+ end
24
+ end
25
+ end
26
+ end
27
+ return { rep, cursor }
@@ -0,0 +1,12 @@
1
+ -- REDLOCK SCRIPT
2
+ -- call with key, value, ms (for set px)
3
+ local key = KEYS[1]
4
+
5
+ if redis.call("exists", key) == 1 then
6
+ -- lock already acquired by someone could be us too (we can only extend)
7
+ return 0
8
+ end
9
+
10
+ redis.call("set", key, KEYS[2], "PX", KEYS[3])
11
+ -- return 1 if lock acquired
12
+ return 1
@@ -0,0 +1,12 @@
1
+ -- REDLOCK SCRIPT
2
+ -- call with key, value, ms (for set px)
3
+ local key = KEYS[1]
4
+
5
+ if redis.call("get", key) ~= KEYS[2] then
6
+ -- we return 0 if lock is already taken by someone else
7
+ return 0
8
+ end
9
+
10
+ redis.call("set", key, KEYS[2], "PX", KEYS[3])
11
+ -- return 1 if we extended the lock successfully
12
+ return 1
@@ -0,0 +1,10 @@
1
+ -- REDLOCK SCRIPT
2
+ -- release a lock by deleting the key
3
+ -- call with key, value
4
+ local key = KEYS[1]
5
+
6
+ if redis.call("get", key) == KEYS[2] then
7
+ redis.pcall("del", key)
8
+ -- we need to publish a expire, because redis won't do it
9
+ redis.call("publish", "__keyevent@" .. KEYS[3] .. "__:expired", KEYS[4])
10
+ end
@@ -0,0 +1,17 @@
1
+ -- design: script
2
+ -- search: javascript
3
+ local rep = {}
4
+ local cursor = KEYS[4];
5
+ local result = redis.call("SSCAN", KEYS[5], cursor, "MATCH", KEYS[1] .. "*", "COUNT", 500)
6
+ cursor = result[1]
7
+ local keys = result[2]
8
+ local argStart = KEYS[1] .. KEYS[2]
9
+ local argEnd = KEYS[1] .. KEYS[3]
10
+ -- function(doc) {
11
+ -- if (doc.type === "script" && doc.common.engineType.match(/^[jJ]ava[sS]cript|^[cC]offee[sS]cript|^[tT]ype[sS]cript|^Blockly/)) emit(doc.common.name, doc); }
12
+ for _, key in ipairs(keys) do
13
+ if (key >= argStart and key < argEnd) then
14
+ rep[#rep + 1] = redis.call("get", key)
15
+ end
16
+ end
17
+ return { rep, cursor }
@@ -0,0 +1,28 @@
1
+ -- design: hm-rega
2
+ -- search: variables
3
+ local rep = {}
4
+ -- local keys=redis.call("keys", KEYS[1].."*")
5
+ local cursor = KEYS[4];
6
+ local result = redis.call("SSCAN", KEYS[5], cursor, "MATCH", KEYS[1] .. "*", "COUNT", 500)
7
+ cursor = result[1]
8
+ local keys = result[2]
9
+ local argStart = KEYS[1] .. KEYS[2]
10
+ local argEnd = KEYS[1] .. KEYS[3]
11
+ local checkStr = string.format("%q:", "TypeName");
12
+ -- function(doc) {
13
+ -- if (doc._id.match(/^hm-rega\\.[0-9]+\\.[0-9,A-Z,a-z]+/) && (doc.native.TypeName === "ALARMDP" || doc.native.TypeName === "VARDP")) {
14
+ -- emit(doc._id, doc);
15
+ -- }
16
+ -- }
17
+ for _, key in ipairs(keys) do
18
+ if (key >= argStart and key < argEnd) then
19
+ local obj = redis.call("get", key)
20
+ if (obj:find(checkStr) ~= nil) then
21
+ local success, decoded = pcall(cjson.decode, obj)
22
+ if (success and decoded.native ~= nil and (decoded.native.TypeName == "ALARMDP" or decoded.native.TypeName == "VARDP")) then
23
+ rep[#rep + 1] = obj
24
+ end
25
+ end
26
+ end
27
+ end
28
+ return { rep, cursor }
@@ -0,0 +1,27 @@
1
+ -- design: system
2
+ -- search: custom
3
+ local rep = {}
4
+ -- local keys=redis.call("keys", KEYS[1].."*")
5
+ local cursor = KEYS[4];
6
+ local result = redis.call("SCAN", cursor, "MATCH", KEYS[1] .. "*", "COUNT", 500)
7
+ cursor = result[1]
8
+ local keys = result[2]
9
+ local argStart = KEYS[1] .. KEYS[2]
10
+ local argEnd = KEYS[1] .. KEYS[3]
11
+ local checkStr = string.format("%q:{", "custom")
12
+ -- function(doc) {
13
+ -- if (doc.type==="state" && (doc.common.custom || doc.common.history))
14
+ -- emit(doc._id, doc.common.custom || doc.common.history)
15
+ -- }
16
+ for _, key in ipairs(keys) do
17
+ if (key >= argStart and key < argEnd) then
18
+ local obj = redis.call("get", key)
19
+ if (obj:find(checkStr) ~= nil) then
20
+ local success, decoded = pcall(cjson.decode, obj)
21
+ if (success and decoded.type == "state" and decoded.common ~= nil and decoded.common.custom ~= nil) then
22
+ rep[#rep + 1] = obj
23
+ end
24
+ end
25
+ end
26
+ end
27
+ return { rep, cursor }
@@ -0,0 +1,25 @@
1
+ -- func: function(doc) { if (doc.type === "%1") emit(doc._id, doc); }
2
+ local rep = {}
3
+ -- local keys=redis.call("keys", KEYS[1].."*")
4
+ local cursor = KEYS[5];
5
+ local result = redis.call("SCAN", cursor, "MATCH", KEYS[1] .. "*", "COUNT", 500)
6
+ cursor = result[1]
7
+ local keys = result[2]
8
+ local argStart = KEYS[1] .. KEYS[2]
9
+ local argEnd = KEYS[1] .. KEYS[3]
10
+ local type = KEYS[4]
11
+ local checkStr = string.format("%q:%q", "type", type)
12
+ local skipCheck = not (type == "channel" or type == "device" or type == "folder")
13
+ -- function(doc) { if (doc.type === "chart") emit(doc._id, doc); }
14
+ for _, key in ipairs(keys) do
15
+ if (key >= argStart and key < argEnd) then
16
+ local obj = redis.call("get", key)
17
+ if (skipCheck or obj:find(checkStr) ~= nil) then
18
+ local success, decoded = pcall(cjson.decode, obj)
19
+ if (success and decoded.type == type) then
20
+ rep[#rep + 1] = obj
21
+ end
22
+ end
23
+ end
24
+ end
25
+ return { rep, cursor }
@@ -0,0 +1,27 @@
1
+ -- design: hm-rega
2
+ -- search: programs
3
+ local rep = {}
4
+ local cursor = KEYS[4];
5
+ local result = redis.call("SCAN", cursor, "MATCH", KEYS[1] .. "*", "COUNT", 500)
6
+ cursor = result[1]
7
+ local keys = result[2]
8
+ local argStart = KEYS[1] .. KEYS[2]
9
+ local argEnd = KEYS[1] .. KEYS[3]
10
+ local checkStr = string.format("%q:%q", "TypeName", "PROGRAM")
11
+ -- function(doc) {
12
+ -- if (doc._id.match(/^hm-rega\\.[0-9]+\\.[0-9,A-Z,a-z]+/) && (doc.native.TypeName === "PROGRAM")) {
13
+ -- emit(doc._id, doc);
14
+ -- }
15
+ -- }
16
+ for _, key in ipairs(keys) do
17
+ if (key >= argStart and key < argEnd and key:sub(7, 13) == "hm-rega") then
18
+ local obj = redis.call("get", key)
19
+ if (obj:find(checkStr) ~= nil) then
20
+ local success, decoded = pcall(cjson.decode, obj)
21
+ if (success and decoded.native ~= nil and decoded.native.TypeName == "PROGRAM") then
22
+ rep[#rep + 1] = obj
23
+ end
24
+ end
25
+ end
26
+ end
27
+ return { rep, cursor }
@@ -0,0 +1,12 @@
1
+ -- REDLOCK SCRIPT
2
+ -- call with key, value, ms (for set px)
3
+ local key = KEYS[1]
4
+
5
+ if redis.call("exists", key) == 1 then
6
+ -- lock already acquired by someone could be us too (we can only extend)
7
+ return 0
8
+ end
9
+
10
+ redis.call("set", key, KEYS[2], "PX", KEYS[3])
11
+ -- return 1 if lock acquired
12
+ return 1
@@ -0,0 +1,12 @@
1
+ -- REDLOCK SCRIPT
2
+ -- call with key, value, ms (for set px)
3
+ local key = KEYS[1]
4
+
5
+ if redis.call("get", key) ~= KEYS[2] then
6
+ -- we return 0 if lock is already taken by someone else
7
+ return 0
8
+ end
9
+
10
+ redis.call("set", key, KEYS[2], "PX", KEYS[3])
11
+ -- return 1 if we extended the lock successfully
12
+ return 1
@@ -0,0 +1,10 @@
1
+ -- REDLOCK SCRIPT
2
+ -- release a lock by deleting the key
3
+ -- call with key, value
4
+ local key = KEYS[1]
5
+
6
+ if redis.call("get", key) == KEYS[2] then
7
+ redis.pcall("del", key)
8
+ -- we need to publish a expire, because redis won't do it
9
+ redis.call("publish", "__keyevent@" .. KEYS[3] .. "__:expired", KEYS[4])
10
+ end
@@ -0,0 +1,24 @@
1
+ -- design: script
2
+ -- search: javascript
3
+ local rep = {}
4
+ local cursor = KEYS[4];
5
+ local result = redis.call("SCAN", cursor, "MATCH", KEYS[1] .. "*", "COUNT", 500)
6
+ cursor = result[1]
7
+ local keys = result[2]
8
+ local argStart = KEYS[1] .. KEYS[2]
9
+ local argEnd = KEYS[1] .. KEYS[3]
10
+ local checkStr = string.format("%q:%q", "type", "script")
11
+ -- function(doc) {
12
+ -- if (doc.type === "script" && doc.common.engineType.match(/^[jJ]ava[sS]cript|^[cC]offee[sS]cript|^[tT]ype[sS]cript|^Blockly/)) emit(doc.common.name, doc); }
13
+ for _, key in ipairs(keys) do
14
+ if (key >= argStart and key < argEnd) then
15
+ local obj = redis.call("get", key)
16
+ if (obj:find(checkStr) ~= nil) then
17
+ local success, decoded = pcall(cjson.decode, obj)
18
+ if (success and decoded.type == "script") then
19
+ rep[#rep + 1] = obj
20
+ end
21
+ end
22
+ end
23
+ end
24
+ return { rep, cursor }
@@ -0,0 +1,28 @@
1
+ -- design: hm-rega
2
+ -- search: variables
3
+ local rep = {}
4
+ -- local keys=redis.call("keys", KEYS[1].."*")
5
+ local cursor = KEYS[4];
6
+ local result = redis.call("SCAN", cursor, "MATCH", KEYS[1] .. "*", "COUNT", 500)
7
+ cursor = result[1]
8
+ local keys = result[2]
9
+ local argStart = KEYS[1] .. KEYS[2]
10
+ local argEnd = KEYS[1] .. KEYS[3]
11
+ local checkStr = string.format("%q:", "TypeName");
12
+ -- function(doc) {
13
+ -- if (doc._id.match(/^hm-rega\\.[0-9]+\\.[0-9,A-Z,a-z]+/) && (doc.native.TypeName === "ALARMDP" || doc.native.TypeName === "VARDP")) {
14
+ -- emit(doc._id, doc);
15
+ -- }
16
+ -- }
17
+ for _, key in ipairs(keys) do
18
+ if (key >= argStart and key < argEnd and key:sub(7, 13) == "hm-rega") then
19
+ local obj = redis.call("get", key)
20
+ if (obj:find(checkStr) ~= nil) then
21
+ local success, decoded = pcall(cjson.decode, obj)
22
+ if (success and decoded.native ~= nil and (decoded.native.TypeName == "ALARMDP" or decoded.native.TypeName == "VARDP")) then
23
+ rep[#rep + 1] = obj
24
+ end
25
+ end
26
+ end
27
+ end
28
+ return { rep, cursor }