Skip to content

Commit 06ea3fd

Browse files
authored
Merge pull request #872 from spiccinini/add-shared-state-multiwriter
Add shared state multiwriter
2 parents 94b78b1 + 60b2048 commit 06ea3fd

5 files changed

Lines changed: 378 additions & 126 deletions

File tree

packages/lime-system/files/usr/lib/lua/lime/utils.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,4 +543,21 @@ function utils.is_valid_mac(string)
543543
end
544544
end
545545

546+
function utils.deepcompare(t1,t2)
547+
if t1 == t2 then return true end
548+
local ty1 = type(t1)
549+
local ty2 = type(t2)
550+
if ty1 ~= ty2 then return false end
551+
if ty1 ~= 'table' and ty2 ~= 'table' then return t1 == t2 end
552+
for k1, v1 in pairs(t1) do
553+
local v2 = t2[k1]
554+
if v2 == nil or not utils.deepcompare(v1, v2) then return false end
555+
end
556+
for k2, v2 in pairs(t2) do
557+
local v1 = t1[k2]
558+
if v1 == nil or not utils.deepcompare(v1, v2) then return false end
559+
end
560+
return true
561+
end
562+
546563
return utils
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/lua
2+
--! Minimalistic CRDT-like shared state structure suitable for mesh networks
3+
--! which handles conflicts in the same entry using last modified timestamp.
4+
--!
5+
--! Copyright (C) 2019 Gioacchino Mazzurco <gio@altermundi.net>
6+
--!
7+
--! This program is free software: you can redistribute it and/or modify
8+
--! it under the terms of the GNU Affero General Public License version 3 as
9+
--! published by the Free Software Foundation.
10+
--!
11+
--! This program is distributed in the hope that it will be useful,
12+
--! but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
--! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
--! GNU Affero General Public License for more details.
15+
--!
16+
--! You should have received a copy of the GNU Affero General Public License
17+
--! along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
19+
if type(arg[2]) ~= "string" or arg[2]:len() < 1 then
20+
print (arg[0], "needs CRDT name to be specified as second argument")
21+
os.exit(-22)
22+
end
23+
24+
local JSON = require("luci.jsonc")
25+
local shared_state = require("shared-state")
26+
require("nixio.util")
27+
28+
nixio.openlog("shared-state")
29+
local logmask = "info"
30+
if os.getenv("DEBUG") then
31+
logmask = "debug"
32+
end
33+
nixio.setlogmask(logmask)
34+
35+
local sharedState = shared_state.SharedStateMultiWriter:new(arg[2], nixio.syslog)
36+
37+
if arg[1] == "insert" then
38+
local inputTable = JSON.parse(io.stdin:read("*all")) or {}
39+
sharedState:insert(inputTable)
40+
elseif arg[1] == "get" then
41+
local resultTable = sharedState:get()
42+
print(JSON.stringify(resultTable))
43+
elseif arg[1] == "sync" then
44+
local urls = {}
45+
if arg[3] ~= nil then for i=3,#arg do table.insert(urls, arg[i]) end end
46+
sharedState:sync(urls)
47+
elseif arg[1] == "reqsync" then
48+
local inputJson = JSON.parse(io.stdin:read("*all"))
49+
sharedState:merge(inputJson)
50+
print(sharedState:toJsonString())
51+
else
52+
print(arg[0], "needs an operation name to be specified as first argument")
53+
nixio.closelog()
54+
os.exit(-22)
55+
end
56+
57+
nixio.closelog()

0 commit comments

Comments
 (0)