Más contenido relacionado
La actualidad más candente (20)
Similar a EWD 3 Training Course Part 26: Event-driven Indexing (20)
EWD 3 Training Course Part 26: Event-driven Indexing
- 1. Copyright © 2016 M/Gateway Developments Ltd
EWD 3 Training Course
Part 26
JavaScript Abstraction of Global Storage:
Event-Driven Indexing
Rob Tweed
Director, M/Gateway Developments Ltd
Twitter: @rtweed
- 2. Copyright © 2016 M/Gateway Developments Ltd
Document Store Indexing
• When a document node is created,
changed or deleted, we may want to
maintain a corresponding set of index
document nodes
• How can this be done in a modular and
automatically-invoked way?
- 3. Copyright © 2016 M/Gateway Developments Ltd
Document Store Indexing
• When a document node is created,
changed or deleted, we may want to
maintain a corresponding set of index
document nodes
• How can this be done in a modular and
automatically-invoked way?
– Events
- 4. Copyright © 2016 M/Gateway Developments Ltd
Document Store Events
• Emitted automatically by:
– value property (when used to set/change a
value)
– setDocument() method (for each node
created)
– increment() method
– delete() method
• Allows you to write handlers to maintain
indices
- 5. Copyright © 2016 M/Gateway Developments Ltd
Document Store Events
• beforeSet
• afterSet
• beforeDelete
• afterDelete
– You'll probably use the after* events most
frequently
- 6. Copyright © 2016 M/Gateway Developments Ltd
Document Store Event Handling
• this.documentStore.on(eventName, function(obj) {..});
• eg:
this.documentStore.on('afterSet', function(nodeObj) {
// do something with the changed node
});
- 7. Copyright © 2016 M/Gateway Developments Ltd
Document Store Event Handling
• this.documentStore.on(eventName, function(obj) {..});
• eg:
this.documentStore.on('afterSet', function(nodeObj) {
// do something with the changed node
});
Tells you what node was changed,
what value it had previously (if any)
and what new value it has now
- 8. Copyright © 2016 M/Gateway Developments Ltd
Document Store Event Handling
Example of afterSet node object
{
"documentName":"CacheTempEWDSession",
"path":["session","72","foo"],
"before":{
"value":"bar",
"exists":true
},
"value":"bar"
}
- 9. Copyright © 2016 M/Gateway Developments Ltd
Document Store Event Handling
{
"documentName":"CacheTempEWDSession",
"path":["session","72","foo"],
"before":{
"value":"bar",
"exists":true
},
"value":"bar"
}
Tells you the DocumentNode
that may need indexing / re-indexing
- 10. Copyright © 2016 M/Gateway Developments Ltd
Document Store Event Handling
{
"documentName":"CacheTempEWDSession",
"path":["session","72","foo"],
"before":{
"value":"bar",
"exists":true
},
"value":"bar"
}
Tells you the previous contents,
if any, of the DocumentNode, prior
to the afterSet event
- 11. Copyright © 2016 M/Gateway Developments Ltd
Document Store Event Handling
{
"documentName":"CacheTempEWDSession",
"path":["session","72","foo"],
"before":{
"value":"bar",
"exists":true
},
"value":"bar"
}
Tells you the new value of the
DocumentNode, as a result of
the afterSet event
- 12. Copyright © 2016 M/Gateway Developments Ltd
Where to handle Document Store Events?
• Your QEWD application-specific back-end
handler module
– Use its optional init() function
• If defined in your module, this function is invoked
when the module is initially loaded by a worker
process
• You can define the handlers you need within this
function
• Filter the document names you're interested in
indexing within your application module
- 13. Copyright © 2016 M/Gateway Developments Ltd
Handling Document Store
Events
• eg C:qewdnode_modulesdemo1.js
module.exports = {
init: function() {
this.documentStore.on('afterSet', function(docNode) {
console.log('*** afterSet event triggered by ' + JSON.stringify(docNode));
});
},
handlers: {
testButton: function(messageObj, session, send, finished) {
session.data.$('foo').value = 'bar';
send({
type: 'intermediate',
foo: 'bar',
date: new Date().toString()
});
finished({
ok: 'testButton message was processed successfully!'
});
}
};
- 14. Copyright © 2016 M/Gateway Developments Ltd
Handling Document Store
Events
• eg C:qewdnode_modulesdemo1.js
module.exports = {
init: function() {
this.documentStore.on('afterSet', function(docNode) {
if (docNode.documentName === 'myDoc') {
// delete previous index document node
// create new index node
// remember that this will, itself, invoke more events for index document nodes!
}
});
},
handlers: {
// etc…
}
};