Skip to content

Commit

Permalink
Also support TTL nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
dreusel committed Dec 11, 2020
1 parent 1e931e1 commit 5444908
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 8 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)`
Expand Down Expand Up @@ -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)`
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
6 changes: 6 additions & 0 deletions lib/typedeclarations.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions lib/zk_promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.<string>}
*/
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]);
}

/**
Expand Down
31 changes: 29 additions & 2 deletions lib/zookeeper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 5 additions & 3 deletions src/node-zk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -641,17 +643,17 @@ class ZooKeeper: public Nan::ObjectWrap {
}

static void ACreate(const Nan::FunctionCallbackInfo<Value>& 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<Object> _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));
}
}

Expand Down

0 comments on commit 5444908

Please sign in to comment.