Skip to content

Commit

Permalink
Separate qlobber-native
Browse files Browse the repository at this point in the history
  • Loading branch information
davedoesdev committed Feb 11, 2020
1 parent 9aa36b3 commit 18fa1be
Show file tree
Hide file tree
Showing 38 changed files with 207 additions and 141 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
node_modules
/.nyc_output
/build
14 changes: 0 additions & 14 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,6 @@ module.exports = function (grunt)
},

exec: {
build: {
cmd: function () {
return `node-gyp build ${grunt.option('debug') ? '--debug' : ''}`;
}
},

rebuild: {
cmd: function () {
return `node-gyp rebuild ${grunt.option('debug') ? '--debug' : ''}`;
}
},

cover: {
cmd: "./node_modules/.bin/nyc -x Gruntfile.js -x 'test/**' node --expose-gc ./node_modules/.bin/grunt test"
},
Expand Down Expand Up @@ -73,8 +61,6 @@ module.exports = function (grunt)
grunt.loadNpmTasks('grunt-exec');

grunt.registerTask('lint', 'jshint');
grunt.registerTask('build', 'exec:build');
grunt.registerTask('rebuild', 'exec:rebuild');
grunt.registerTask('test', 'mochaTest');
grunt.registerTask('docs', 'apidox');
grunt.registerTask('coverage', ['exec:cover',
Expand Down
67 changes: 49 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

Node.js globbing for amqp-like topics.

__Note:__ Version 4.2.0 adds async and worker thread support when used on Node 12+.
__Note:__ Version 5.0.0 adds async and worker thread support when used on Node 12+.

Example:

```javascript
var assert = require('assert');
var Qlobber = require('qlobber').Qlobber;
var matcher = new Qlobber();
matcher.add('foo.*', 'it matched!');
Expand Down Expand Up @@ -63,9 +64,9 @@ assert.deepEqual(['quick.orange.rabbit',
Same as the first example but using `await`:

```javascript
var assert = require('assert');
var Qlobber = require('qlobber').Qlobber.nativeString;
var matcher = new Qlobber();
const assert = require('assert');
const { Qlobber } = require('qlobber').set_native(require('qlobber-native'));
const matcher = new Qlobber.nativeString();

(async () => {
await matcher.addP('foo.*', 'it matched!');
Expand All @@ -79,13 +80,13 @@ var matcher = new Qlobber();
Same again but the matching is done on a separate thread:

```
const Qlobber = require('qlobber').Qlobber.nativeString;
const { Qlobber } = require('qlobber').set_native(require('qlobber-native'));
const {
Worker, isMainThread, parentPort, workerData
} = require('worker_threads');
if (isMainThread) {
const matcher = new Qlobber();
const matcher = new Qlobber.nativeString();
matcher.add('foo.*', 'it matched!');
const worker = new Worker(__filename, {
workerData: matcher.state_address
Expand All @@ -95,7 +96,7 @@ if (isMainThread) {
assert.deepEqual(msg, [['it matched!'], true]);
});
} else {
const matcher = new Qlobber(workerData);
const matcher = new Qlobber.nativeString(workerData);
parentPort.postMessage([
matcher.match('foo.bar'),
matcher.test('foo.bar', 'it matched!')
Expand Down Expand Up @@ -147,11 +148,27 @@ The Javascript Qlobbers don't support asynchronous calls and worker threads
because Javascript values can't be shared between threads.

In order to support asynchronous calls and worker threads, a native C++
implementation is included. If you have Gnu C++ version 9+ and Boost 1.70+
then the native version will be compiled when you install the module.
implementation is available in the
[qlobber-native](https://www.npmjs.com/package/qlobber-native) module.

Add qlobber-native as a dependency to your project and then add it to qlobber
like this:

```javascript
require('qlobber').set_native(require('qlobber-native'));
```

If compilation succeeds then the following classes will be available alongside
the Javascript classes:
Note that [`set_native`](#set_nativeqlobber_native) returns qlobber's exports so you can do something like
this:

```javascript
const { Qlobber } = require('qlobber').set_native(require('qlobber-native'));
```

Note that qlobber-native requires Gnu C++ version 9+ and Boost 1.70+.

Once's you've added it to qlobber, the following classes will be available
alongside the Javascript classes:

- `Qlobber.nativeString`
- `Qlobber.nativeNumber`
Expand All @@ -161,13 +178,6 @@ the Javascript classes:

They can only hold values of a single type (currently strings or numbers).

You can also build the native implemention using one of these commands:

```shell
grunt build [--debug]
grunt rebuild [--debug]
```

### Asynchronous calls

The native classes support the same API as the Javascript classes but have the
Expand Down Expand Up @@ -209,6 +219,7 @@ _Source: [lib/qlobber.js](lib/qlobber.js)_
- <a name="toc_qlobbertrueprototypematchtopic"></a>[QlobberTrue.prototype.match](#qlobbertrueprototypematchtopic)
- <a name="toc_visitorstreamqlobber"></a>[VisitorStream](#visitorstreamqlobber)
- <a name="toc_restorerstreamqlobber"></a>[RestorerStream](#restorerstreamqlobber)
- <a name="toc_set_nativeqlobber_native"></a>[set_native](#set_nativeqlobber_native)

## Qlobber([options])

Expand Down Expand Up @@ -480,4 +491,24 @@ Inherits from [`Writable`](https://nodejs.org/dist/latest-v8.x/docs/api/stream.h

<sub>Go: [TOC](#tableofcontents)</sub>

## set_native(qlobber_native)

> Add [qlobber-native](https://www.npmjs.com/package/qlobber-native) to qlobber.
**Parameters:**

- `{Object} qlobber_native` The qlobber-native module, obtained using `require('qlobber-native')`.

**Return:**

`{Object}` The qlobber exports with the following native classes added:

- `Qlobber.nativeString`
- `Qlobber.nativeNumber`
- `QlobberDedup.nativeString`
- `QlobberDedup.nativeNumber`
- `QlobberTrue.native`

<sub>Go: [TOC](#tableofcontents)</sub>

_&mdash;generated by [apidox](https://github.com/codeactual/apidox)&mdash;_
10 changes: 4 additions & 6 deletions aedes/qlobber-sub.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,11 @@ QlobberSub.prototype.clear = function ()
return Qlobber.prototype.clear.call(this);
};

try
{
const binding = require('bindings')('qlobber.node');
QlobberSub.native = qlobber.wrap_native(binding.QlobberSub, QlobberSub);
}
catch (ex)
QlobberSub.set_native = function (qlobber_native)
{
const wrap_native = require('../lib/wrap_native.js');
QlobberSub.native = wrap_native(qlobber_native.QlobberSub, QlobberSub);
return module.exports;
}

module.exports = QlobberSub;
6 changes: 3 additions & 3 deletions examples/example3.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var assert = require('assert');
var Qlobber = require('..').Qlobber.nativeString;
var matcher = new Qlobber();
const assert = require('assert');
const { Qlobber } = require('..').set_native(require('../native'));
const matcher = new Qlobber.nativeString();

(async () => {
await matcher.addP('foo.*', 'it matched!');
Expand Down
6 changes: 3 additions & 3 deletions examples/example4.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const Qlobber = require('..').Qlobber.nativeString;
const { Qlobber } = require('..').set_native(require('../native'));
const {
Worker, isMainThread, parentPort, workerData
} = require('worker_threads');

if (isMainThread) {
const matcher = new Qlobber();
const matcher = new Qlobber.nativeString();
matcher.add('foo.*', 'it matched!');
const worker = new Worker(__filename, {
workerData: matcher.state_address
Expand All @@ -14,7 +14,7 @@ if (isMainThread) {
assert.deepEqual(msg, [['it matched!'], true]);
});
} else {
const matcher = new Qlobber(workerData);
const matcher = new Qlobber.nativeString(workerData);
parentPort.postMessage([
matcher.match('foo.bar'),
matcher.test('foo.bar', 'it matched!')
Expand Down
78 changes: 47 additions & 31 deletions lib/qlobber.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
Node.js globbing for amqp-like topics.
__Note:__ Version 4.2.0 adds async and worker thread support when used on Node 12+.
__Note:__ Version 5.0.0 adds async and worker thread support when used on Node 12+.
Example:
```javascript
var assert = require('assert');
var Qlobber = require('qlobber').Qlobber;
var matcher = new Qlobber();
matcher.add('foo.*', 'it matched!');
Expand Down Expand Up @@ -64,9 +65,9 @@ assert.deepEqual(['quick.orange.rabbit',
Same as the first example but using `await`:
```javascript
var assert = require('assert');
var Qlobber = require('qlobber').Qlobber.nativeString;
var matcher = new Qlobber();
const assert = require('assert');
const { Qlobber } = require('qlobber').set_native(require('qlobber-native'));
const matcher = new Qlobber.nativeString();
(async () => {
await matcher.addP('foo.*', 'it matched!');
Expand All @@ -80,13 +81,13 @@ var matcher = new Qlobber();
Same again but the matching is done on a separate thread:
```
const Qlobber = require('qlobber').Qlobber.nativeString;
const { Qlobber } = require('qlobber').set_native(require('qlobber-native'));
const {
Worker, isMainThread, parentPort, workerData
} = require('worker_threads');
if (isMainThread) {
const matcher = new Qlobber();
const matcher = new Qlobber.nativeString();
matcher.add('foo.*', 'it matched!');
const worker = new Worker(__filename, {
workerData: matcher.state_address
Expand All @@ -96,7 +97,7 @@ if (isMainThread) {
assert.deepEqual(msg, [['it matched!'], true]);
});
} else {
const matcher = new Qlobber(workerData);
const matcher = new Qlobber.nativeString(workerData);
parentPort.postMessage([
matcher.match('foo.bar'),
matcher.test('foo.bar', 'it matched!')
Expand Down Expand Up @@ -148,11 +149,27 @@ The Javascript Qlobbers don't support asynchronous calls and worker threads
because Javascript values can't be shared between threads.
In order to support asynchronous calls and worker threads, a native C++
implementation is included. If you have Gnu C++ version 9+ and Boost 1.70+
then the native version will be compiled when you install the module.
implementation is available in the
[qlobber-native](https://www.npmjs.com/package/qlobber-native) module.
Add qlobber-native as a dependency to your project and then add it to qlobber
like this:
```javascript
require('qlobber').set_native(require('qlobber-native'));
```
If compilation succeeds then the following classes will be available alongside
the Javascript classes:
Note that [`set_native`](#set_nativeqlobber_native) returns qlobber's exports so you can do something like
this:
```javascript
const { Qlobber } = require('qlobber').set_native(require('qlobber-native'));
```
Note that qlobber-native requires Gnu C++ version 9+ and Boost 1.70+.
Once's you've added it to qlobber, the following classes will be available
alongside the Javascript classes:
- `Qlobber.nativeString`
- `Qlobber.nativeNumber`
Expand All @@ -162,13 +179,6 @@ the Javascript classes:
They can only hold values of a single type (currently strings or numbers).
You can also build the native implemention using one of these commands:
```shell
grunt build [--debug]
grunt rebuild [--debug]
```
### Asynchronous calls
The native classes support the same API as the Javascript classes but have the
Expand Down Expand Up @@ -1047,25 +1057,31 @@ RestorerStream.prototype._write = function (value, _, cb)
cb();
};

var wrap_native;
/**
Add [qlobber-native](https://www.npmjs.com/package/qlobber-native) to qlobber.
try
{
wrap_native = require('./wrap_native.js');
const binding = require('bindings')('qlobber.node');
Qlobber.nativeString = wrap_native(binding.QlobberString, Qlobber);
Qlobber.nativeNumber = wrap_native(binding.QlobberNumber, Qlobber);
QlobberDedup.nativeString = wrap_native(binding.QlobberDedupString, QlobberDedup);
QlobberDedup.nativeNumber = wrap_native(binding.QlobberDedupNumber, QlobberDedup);
QlobberTrue.native = wrap_native(binding.QlobberTrue, QlobberTrue);
}
catch (ex)
@param {Object} qlobber_native The qlobber-native module, obtained using `require('qlobber-native')`.
@return {Object} The qlobber exports with the following native classes added:
- `Qlobber.nativeString`
- `Qlobber.nativeNumber`
- `QlobberDedup.nativeString`
- `QlobberDedup.nativeNumber`
- `QlobberTrue.native`
*/
function set_native(qlobber_native)
{
const wrap_native = require('./wrap_native.js');
Qlobber.nativeString = wrap_native(qlobber_native.QlobberString, Qlobber);
Qlobber.nativeNumber = wrap_native(qlobber_native.QlobberNumber, Qlobber);
QlobberDedup.nativeString = wrap_native(qlobber_native.QlobberDedupString, QlobberDedup);
QlobberDedup.nativeNumber = wrap_native(qlobber_native.QlobberDedupNumber, QlobberDedup);
QlobberTrue.native = wrap_native(qlobber_native.QlobberTrue, QlobberTrue);
return exports;
}

exports.Qlobber = Qlobber;
exports.QlobberDedup = QlobberDedup;
exports.QlobberTrue = QlobberTrue;
exports.VisitorStream = VisitorStream;
exports.RestorerStream = RestorerStream;
exports.wrap_native = wrap_native;
exports.set_native = set_native;
1 change: 1 addition & 0 deletions native/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build
8 changes: 8 additions & 0 deletions native/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Adds async and worker thread support to [qlobber](https://github.com/davedoesdev/qlobber).

Documentation and tests are part of qlobber. This module isn't meant to be
used standalone. Add it to qlobber like this:

```javascript
require('qlobber').set_native(require('qlobber-native'));
```
2 changes: 1 addition & 1 deletion binding.gyp → native/binding.gyp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"targets": [
{
"target_name": "qlobber",
"target_name": "qlobber_native",
"sources": [ "src/qlobber.cc", "src/rwlock.cc" ],
"include_dirs": ["<!@(node -p \"require('node-addon-api').include\")"],
"dependencies": ["<!(node -p \"require('node-addon-api').gyp\")"],
Expand Down
1 change: 1 addition & 0 deletions native/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('bindings')('qlobber_native.node');
Loading

0 comments on commit 18fa1be

Please sign in to comment.