From 54449082b1ede8923343389fb0486504cb46bd20 Mon Sep 17 00:00:00 2001 From: Daniel Reus Date: Fri, 11 Dec 2020 21:51:34 +0100 Subject: [PATCH] Also support TTL nodes --- README.md | 6 +++++- lib/constants.js | 6 ++++++ lib/typedeclarations.d.ts | 6 ++++++ lib/zk_promise.js | 5 +++-- lib/zookeeper.js | 31 +++++++++++++++++++++++++++++-- src/node-zk.cpp | 8 +++++--- 6 files changed, 54 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 1ce4931e..3f2245a2 100644 --- a/README.md +++ b/README.md @@ -93,7 +93,7 @@ Have a look at the code in the [examples](./examples) folder: with __master__, _ * `init(options)` * `connect(options, connect_cb)` * `close()` -* `path = await create(path, data, flags)` +* `path = await create(path, data, flags, ttl)` * `mkdirp(path, callback(Error))` * `stat = await exists(path, watch)` * `data = await get(path, watch)` @@ -126,6 +126,7 @@ Have a look at the code in the [examples](./examples) folder: with __master__, _ * `connect(options, connect_cb)` * `close()` * `a_create(path, data, flags, path_cb)` +* `a_createTtl(path, data, flags, path_cb)` * `mkdirp(path, callback(Error))` * `a_exists(path, watch, stat_cb)` * `a_get(path, watch, data_cb)` @@ -170,8 +171,11 @@ Have a look at the code in the [examples](./examples) folder: with __master__, _ - `ZOO_PERSISTENT_SEQUENTIAL` - `ZOO_EPHEMERAL_SEQUENTIAL` - `ZOO_CONTAINER` + - `ZOO_PERSISTENT_WITH_TTL` + - `ZOO_PERSISTENT_SEQUENTIAL_WITH_TTL` * version : int32. Pass 'null' or don't pas any to skip version checking. * watch : boolean +* ttl: int32. Must be postive if any of the TTL modes is used; not positive or unspecified otherwise. * scheme : authorisation scheme (digest, auth) * auth : authorisation credentials (username:password) * acl : acls list (same as output parameter, look below) - read only diff --git a/lib/constants.js b/lib/constants.js index d8a4260c..9489e0bf 100644 --- a/lib/constants.js +++ b/lib/constants.js @@ -64,6 +64,12 @@ module.exports.ZOO_EPHEMERAL_SEQUENTIAL = NativeZk.ZOO_EPHEMERAL_SEQUENTIAL; /** @type {number} 4 */ module.exports.ZOO_CONTAINER = NativeZk.ZOO_CONTAINER; +/** @type {number} 5 */ +module.exports.ZOO_PERSISTENT_WITH_TTL = NativeZk.ZOO_PERSISTENT_WITH_TTL; + +/** @type {number} 6 */ +module.exports.ZOO_PERSISTENT_SEQUENTIAL_WITH_TTL = NativeZk.ZOO_PERSISTENT_SEQUENTIAL_WITH_TTL; + /** @type {number} 0 */ module.exports.ZOK = NativeZk.ZOK; diff --git a/lib/typedeclarations.d.ts b/lib/typedeclarations.d.ts index adcaa58e..6a62a5f2 100644 --- a/lib/typedeclarations.d.ts +++ b/lib/typedeclarations.d.ts @@ -90,6 +90,10 @@ declare module "zookeeperWithCallbacks" { static get ZOO_EPHEMERAL_SEQUENTIAL(): number; /** @deprecated @returns {number} 4 */ static get ZOO_CONTAINER(): number; + /** @deprecated @returns {number} 5 */ + static get ZOO_PERSISTENT_WITH_TTL(): number; + /** @deprecated @returns {number} 6 */ + static get ZOO_PERSISTENT_SEQUENTIAL_WITH_TTL(): number; /** @deprecated @returns {number} 0 */ static get ZOK(): number; /** @deprecated @returns {number} -1 */ @@ -371,6 +375,8 @@ declare module "zookeeperConstants" { export var ZOO_EPHEMERAL_SEQUENTIAL: number; export var ZOO_SEQUENCE: number; export var ZOO_CONTAINER: number; + export var ZOO_PERSISTENT_WITH_TTL: number; + export var ZOO_PERSISTENT_SEQUENTIAL_WITH_TTL: number; export var ZOK: number; export var ZSYSTEMERROR: number; export var ZRUNTIMEINCONSISTENCY: number; diff --git a/lib/zk_promise.js b/lib/zk_promise.js index a69fb234..9a435bc9 100644 --- a/lib/zk_promise.js +++ b/lib/zk_promise.js @@ -24,11 +24,12 @@ class ZooKeeperPromise extends ZooKeeper { * @param {string} path * @param {(string|Buffer)} data * @param {number} flags + * @param {number} ttl, if flags is ZOO_PERSISTENT_WITH_TTL or ZOO_PERSISTENT_SEQUENTIAL_WITH_TTL * @fulfill {string} * @returns {Promise.} */ - create(path, data, flags) { - return this.promisify(super.a_create, [path, data, flags]); + create(path, data, flags, ttl) { + return this.promisify(super.a_createTtl, [path, data, flags, ttl]); } /** diff --git a/lib/zookeeper.js b/lib/zookeeper.js index 712f859b..69086895 100644 --- a/lib/zookeeper.js +++ b/lib/zookeeper.js @@ -175,8 +175,24 @@ class ZooKeeper extends EventEmitter { * @returns {*} */ a_create(path, data, flags, pathCb) { - this.log(`Calling a_create with ${util.inspect([path, data, flags, pathCb])}`); - return this.native.a_create(path, data, flags, pathCb); + this.log(`Calling a_create with ${util.inspect([path, data, flags, -1, pathCb])}`); + return this.native.a_create(path, data, flags, -1, pathCb); + } + + /** + * @param {string} path + * @param {string|Buffer} data + * @param {number} flags - an int32 value + * @param {number} ttl + * @param {pathCb} pathCb + * @returns {*} + */ + a_createTtl(path, data, flags, ttl, pathCb) { + if (!ttl) { + ttl = -1; + } + this.log(`Calling a_create with ${util.inspect([path, data, flags, ttl, pathCb])}`); + return this.native.a_create(path, data, flags, ttl, pathCb); } /** @@ -525,6 +541,17 @@ class ZooKeeper extends EventEmitter { static get ZOO_CONTAINER() { return NativeZk.ZOO_CONTAINER; } + + /** @deprecated @returns {number} 5 */ + static get ZOO_PERSISTENT_WITH_TTL() { + return NativeZk.ZOO_PERSISTENT_WITH_TTL; + } + + /** @deprecated @returns {number} 6 */ + static get ZOO_PERSISTENT_SEQUENTIAL_WITH_TTL() { + return NativeZk.ZOO_PERSISTENT_SEQUENTIAL_WITH_TTL; + } + /** @deprecated @returns {number} 0 */ static get ZOK() { return NativeZk.ZOK; diff --git a/src/node-zk.cpp b/src/node-zk.cpp index 737143e1..f7589659 100644 --- a/src/node-zk.cpp +++ b/src/node-zk.cpp @@ -196,6 +196,8 @@ class ZooKeeper: public Nan::ObjectWrap { NODE_DEFINE_CONSTANT(constructor, ZOO_PERSISTENT_SEQUENTIAL); NODE_DEFINE_CONSTANT(constructor, ZOO_EPHEMERAL_SEQUENTIAL); NODE_DEFINE_CONSTANT(constructor, ZOO_CONTAINER); + NODE_DEFINE_CONSTANT(constructor, ZOO_PERSISTENT_WITH_TTL); + NODE_DEFINE_CONSTANT(constructor, ZOO_PERSISTENT_SEQUENTIAL_WITH_TTL); NODE_DEFINE_CONSTANT(constructor, ZOO_CREATED_EVENT); NODE_DEFINE_CONSTANT(constructor, ZOO_DELETED_EVENT); @@ -641,17 +643,17 @@ class ZooKeeper: public Nan::ObjectWrap { } static void ACreate(const Nan::FunctionCallbackInfo& info) { - A_METHOD_PROLOG(4); + A_METHOD_PROLOG(5); Nan::Utf8String _path (toString(info[0])); uint32_t flags = toUint(info[2]); if (Buffer::HasInstance(info[1])) { // buffer Local _data = toLocalObj(info[1]); - METHOD_EPILOG(zoo_acreate(zk->zhandle, *_path, BufferData(_data), BufferLength(_data), &ZOO_OPEN_ACL_UNSAFE, flags, string_completion, cb)); + METHOD_EPILOG(zoo_acreate_ttl(zk->zhandle, *_path, BufferData(_data), BufferLength(_data), &ZOO_OPEN_ACL_UNSAFE, flags, ttl, string_completion, cb)); } else { // other Nan::Utf8String _data (toString(info[1])); - METHOD_EPILOG(zoo_acreate(zk->zhandle, *_path, *_data, _data.length(), &ZOO_OPEN_ACL_UNSAFE, flags, string_completion, cb)); + METHOD_EPILOG(zoo_acreate_ttl(zk->zhandle, *_path, *_data, _data.length(), &ZOO_OPEN_ACL_UNSAFE, flags, ttl, string_completion, cb)); } }