Aerospike允许列出已加载的UDF模块。这是 aql -example(taken from help-portal):
SHOW MODULES
结果包含hash
字段:
aql> show modules
+---------------------------+-------+------------------------+
| module | type | hash |
+---------------------------+-------+------------------------+
| "example1.lua" | "lua" | "033671e05067888fce09" |
| "example2.lua" | "lua" | "07b42082cca8e73a96b2" |
+---------------------------+-------+------------------------+
2 rows in set (0.000 secs)
我的问题(1):我有example1.lua
文件(UDF模块的来源),如何计算(和检查)哈希?
我的假设:
md5sum
- 失败(显然,参见假设3)sha1sum
关于纯源文件 - 失败(参见假设3)实际上,我想检查已注册模块的版本,并在必要时升级该模块。如文档中所述:
文件的哈希值。大多数用户不会发现哈希值有用,但有些人可能会使用它来验证服务器上UDF的版本或实例。
那么,备用问题(2)是:如何检查已注册UDF模块的版本?
答案 0 :(得分:2)
以下3个字段的20字节sha1哈希表示为json文档。 json文档是字符串表示,没有任何换行符或EOF字符。
"context64" : base64 encoding of whole file
"type" : LUA
"name" : filename
示例:
{"content64": "ZnVuY3Rpb24gcHJpbnRfbWV0YShyZWMpCgoJaWYgbm90IGFlcm9zcGlrZTpleGlzdHMocmVjKSB0aGVuCgkJcmV0dXJuICJub3JlYyIKCWVuZAoKCWluZm8ocmVjb3JkLnR0bChyZWMpKQoJaW5mbyhyZWNvcmQubGFzdF91cGRhdGVfdGltZShyZWMpKQoJcmV0dXJuIHJlY29yZC5sYXN0X3VwZGF0ZV90aW1lKHJlYykKZW5kCg==", "type": "LUA", "name": "lut.lua"}
交叉检查:(忽略为了echo命令而添加的'\'转义字符)
$ echo -n {\"content64\": \"ZnVuY3Rpb24gcHJpbnRfbWV0YShyZWMpCgoJaWYgbm90IGFlcm9zcGlrZTpleGlzdHMocmVjKSB0aGVuCgkJcmV0dXJuICJub3JlYyIKCWVuZAoKCWluZm8ocmVjb3JkLnR0bChyZWMpKQoJaW5mbyhyZWNvcmQubGFzdF91cGRhdGVfdGltZShyZWMpKQoJcmV0dXJuIHJlY29yZC5sYXN0X3VwZGF0ZV90aW1lKHJlYykKZW5kCg==\", \"type\": \"LUA\", \"name\": \"lut.lua\"} | sha1sum
998354a59337b229e2dd777a3288e8e8f33568a5 -
$ asinfo -v "udf-list"
filename=lut.lua,hash=998354a59337b229e2dd777a3288e8e8f33568a5,type=LUA;
答案 1 :(得分:0)
除了@sunil回答
此例程使用AeroSpike的UDF计算LUA模块的哈希值,用 python 3
编写import base64
import hashlib
# You could pass one argument - full path, and then extract
# name. But here we have only this naive implementation
def udf_module_get_hash(MODULE_PATH, MODULE_NAME_WITHOUT_DIR_WITH_EXT):
with open(MODULE_PATH, 'rb') as f:
content = f.read()
b64 = base64.b64encode(content)
meta = '{{"content64": "{0}", "type": "LUA", "name": "{1}"}}'.format(
b64.decode('ascii'),
MODULE_NAME_WITHOUT_DIR_WITH_EXT
)
sha1 = hashlib.sha1()
sha1.update(meta.encode())
h = sha1.hexdigest()
# h is something like '052ac7359e46d1c6c97a5bf1a9854739cd9e481a'
return h