Skip to content

Commit fe49625

Browse files
author
Ruben
committed
Allow an array of matches in the patern matching rules
This allows setting multiple matches (a combination of subject, predicate and object) for one definition. The same can be achieved by creating multiple definitions with the same callback/options and a different single match pattern. Using an object (instead of array) for a `match` is still allowed: this is backwards compatible. This also changes two aspects of how deltas are sent through: When a delta matches multiple patterns (matches), it will send the same change sets multiple times to the same url, for every match that is in the rules. When using an array for `match`es, the change set will only be sent once. When using in conjunction with `sendMatchesOnly`, all patterns matching those defined in `match`es will be sent in one change set together. When not using an array for `match`es, multiple change sets with "one type of match" each would be sent to the url.
1 parent d77e06d commit fe49625

2 files changed

Lines changed: 21 additions & 10 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export default [
5858

5959
The exported property contains an array of definitions, each linking a match to a callback.
6060

61-
- `match`: Pattern to match against. Any supplied key must match, anything unspecified is ignored.
61+
- `match`: Pattern to match against. Any supplied key must match, anything unspecified is ignored. Can pass multiple patterns as a list, which will be combined as an "or" (one valid pattern results in a match).
6262
- `match.subject`: Matches the subject. Both `type` and `value` may be specified.
6363
- `match.predicate`: Matches the predicade. Both `type` and `value` may be specified.
6464
- `match.object`: Matches the object. Both `type` and `value` may be specified.
@@ -72,7 +72,7 @@ The exported property contains an array of definitions, each linking a match to
7272
- `options.ignoreFromSelf`: Don't inform about changes that originated from the microservice to be informed (based on the hostname).
7373
- `options.retry`: (experimental) How many times the request is sent again on failure. Defaults to 0. Warning: in case of retries, deltas may be received out of order!
7474
- `options.retryTimeout`: (experimental) How much time is left in between retries (in ms). Currently defaults to 250ms.
75-
- `options.sendMatchesOnly`: Only send triples that match, removing the other triples from the changes.
75+
- `options.sendMatchesOnly`: Only send triples that match the pattern(s), removing the other triples from the changes.
7676

7777
### Modifying quads
7878
#### Normalize datetime

matching.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { DEBUG_TRIPLE_MATCHES_SPEC } from './env';
66
* @param {Array<Object>} changeSets - An array of change set objects,
77
* each containing `insert` and `delete` properties.
88
* @param {Object} entry - An object containing the matching criteria.
9-
* @param {Array} entry.match - The pattern used to filter the triples
9+
* @param {Array} entry.match - The pattern(s) used to filter the triples
1010
* in the `insert` and `delete` arrays.
1111
* @returns {Array<Object>} A new array of change set objects with
1212
* filtered `insert` and `delete` properties.
@@ -22,19 +22,30 @@ export function filterChangesetsOnPattern(changeSets, entry) {
2222
effectiveInsert: effectiveInsert.filter((triple) => tripleMatchesSpec(triple, entry.match)),
2323
effectiveDelete: effectiveDelete.filter((triple) => tripleMatchesSpec(triple, entry.match)),
2424
};
25-
filteredChangesets.push(clonedChangeSet);
26-
};
25+
// do not keep empty change sets
26+
if( clonedChangeSet.insert.length > 0
27+
|| clonedChangeSet.delete.length > 0
28+
|| clonedChangeSet.effectiveInsert.length > 0
29+
|| clonedChangeSet.effectiveDelete.length > 0 ) {
30+
filteredChangesets.push(clonedChangeSet);
31+
}
32+
}
2733
return filteredChangesets;
2834
}
2935

3036
export function tripleMatchesSpec( triple, matchSpec ) {
31-
// form of triple is {s, p, o}, same as matchSpec
37+
const matches = Array.isArray(matchSpec) ? matchSpec : [matchSpec];
3238
if(DEBUG_TRIPLE_MATCHES_SPEC)
3339
console.log(`Does ${JSON.stringify(triple)} match ${JSON.stringify(matchSpec)}?`);
3440

35-
for( let key in matchSpec ){
36-
// key is one of s, p, o
37-
const subMatchSpec = matchSpec[key];
41+
return matches.some((match) => tripleMatchesPattern(triple, match));
42+
}
43+
44+
function tripleMatchesPattern( triple, pattern ) {
45+
// form of triple is {s, p, o} or {subject, predicate, object}, same as pattern
46+
for( let key in pattern ){
47+
// key is one of subject, predicate, object
48+
const subMatchSpec = pattern[key];
3849
const subMatchValue = triple[key];
3950

4051
if( subMatchSpec && !subMatchValue )
@@ -45,7 +56,7 @@ export function tripleMatchesSpec( triple, matchSpec ) {
4556
if( subMatchSpec[subKey] !== subMatchValue[subKey] )
4657
return false;
4758
}
48-
return true; // no false matches found, let's send a response
59+
return true; // no false matches found
4960
}
5061

5162

0 commit comments

Comments
 (0)