UpsertListner-class: Class '"UpsertListener"'

UpsertListener-classR Documentation

Class "UpsertListener"

Description

This listener takes messages that match its incomming set and inject them into another Mongo database (presumably a queue for another service). If a matching message exists, it is replaced instead.

Details

The database is a mongo collection identified by dburi, dbname and colname (collection within the database). The mess field of the P4Message is checked against the applicable messages in messSet. If it is there, then the message is saved in the collection.

Before the message is saved, the collection is checked to see if another message exits which matches on the fields listed in qfields. If this is true, the message in the database is replaced. If not, the message is inserted.

Extends

This class implements the Listener interface.

All reference classes extend and inherit methods from "envRefClass".

Methods

isListener

signature(x = "UpsertListener"): returns true.

receiveMessage

signature(x = "UpsertListener", message): If the message is in the messSet, it saves or replaces the message inthe database. (See details)

listenerName

signature(x = "UpsertListener"): Returns the name assigned to the listener.

listenerDataTable

signature(listener = "UpsertListener", appid ): Builds a data datable from the messages.

Data Table

When the listenerDataTable method is called, a general find query (mdbFind on the backing collection. The app, uid, context, timestamp fields are selected, and the data (details) field is unpackaged and added as additional columns.

Fields

sender:

Object of class character which is used as the sender field for the message.

dbname:

Object of class character giving the name of the Mongo database

dburi:

Object of class character giving the url of the Mongo database.

colname:

Object of class character giving the column of the Mongo database.

qfields:

Object of class character giving the names of the fields which should be considered a key for the messages.

messSet:

A vector of class character giving the name of messages which are sent to the database. Only messages for which mess(mess) is an element of messSet will be inserted.

db:

Object of class MongoDB giving the database. Use messdb() to access this field to makes sure it has been set up.

Class-Based Methods

messdb():

Accessor for the database collection. Initializes the connection if it has not been set up.

receiveMessage(mess):

Does the work of inserting the message. See Details.

reset(app):

Empties the database collection of messages with this app id.

initialize(sender, dbname, dburi, colname, messSet, qfields, ...):

Sets the default values for the fields.

Author(s)

Russell Almond

References

This is an example of the observer design pattern. https://en.wikipedia.org/wiki/Observer_pattern.

See Also

Listener, P4Message, UpsertListener, UpdateListener, CaptureListener, InjectionListener, TableListener, mongo

Examples

## Not run: 
mess1 <- P4Message(app="default",uid="Phred",context="Down Hill",
                   sender="EABN",mess="Statistics",
                   details=list("Physics_EAP"=0.5237,"Physics_Mode"="High"))
ul <- UpsertListener(colname="Statistics",qfields=c("app","uid"),
         messSet=c("Statistics"))
receiveMessage(ul,mess1)

## End(Not run)

ralmond/Proc4 documentation built on June 13, 2025, 7:13 a.m.