@@ -100,42 +100,54 @@ setMethod("as.jlist",c("P4Message","list"), function(obj,ml,serialize=TRUE) {
100
100
})
101
101
102
102
saveRec <- function (mess , col , serialize = TRUE ) {
103
- jso <- as.json(mess ,serialize )
104
- if (is.na(mess @ " _id" )) {
105
- # # Insert
106
- col $ insert(jso )
107
- it <- col $ iterate(jso ,' {"_id":true}' ,limit = 1 )
108
- mess @ " _id" <- it $ one()$ " _id"
109
- names(mess @ " _id" ) <- " oid" # # Aids in extraction
110
- } else {
111
- if (col $ count(paste(' {"_id":{"$oid":"' ,mess @ " _id" ,' "}}' ,sep = " " ))) {
112
- # # Replace
113
- col $ update(paste(' {"_id":{"$oid":"' ,mess @ " _id" ,' "}}' ,sep = " " ),
114
- paste(' {"$set":' ,jso ,' }' ,sep = " " ))
115
- } else {
116
- # # ID is out of date, insert and get new ID.
103
+ if (! is.null(col )) {
104
+ jso <- as.json(mess ,serialize )
105
+ if (is.na(mess @ " _id" )) {
106
+ # # Insert
117
107
col $ insert(jso )
118
108
it <- col $ iterate(jso ,' {"_id":true}' ,limit = 1 )
119
109
mess @ " _id" <- it $ one()$ " _id"
120
110
names(mess @ " _id" ) <- " oid" # # Aids in extraction
111
+ } else {
112
+ if (col $ count(paste(' {"_id":{"$oid":"' ,mess @ " _id" ,' "}}' ,sep = " " ))) {
113
+ # # Replace
114
+ col $ update(paste(' {"_id":{"$oid":"' ,mess @ " _id" ,' "}}' ,sep = " " ),
115
+ paste(' {"$set":' ,jso ,' }' ,sep = " " ))
116
+ } else {
117
+ # # ID is out of date, insert and get new ID.
118
+ col $ insert(jso )
119
+ it <- col $ iterate(jso ,' {"_id":true}' ,limit = 1 )
120
+ mess @ " _id" <- it $ one()$ " _id"
121
+ names(mess @ " _id" ) <- " oid" # # Aids in extraction
122
+ }
121
123
}
124
+ } else {
125
+ flog.trace(" DB is null, not saving message." )
122
126
}
123
127
mess
124
128
}
125
129
126
130
markAsProcessed <- function (mess ,col ) {
127
131
processed(mess ) <- TRUE
128
- col $ update(paste(' {"_id":{"$oid":"' ,mess @ " _id" ,' "}}' ,sep = " " ),
129
- ' {"$set": {"processed":true}}' )
132
+ if (! is.null(col )) {
133
+ col $ update(paste(' {"_id":{"$oid":"' ,mess @ " _id" ,' "}}' ,sep = " " ),
134
+ ' {"$set": {"processed":true}}' )
135
+ } else {
136
+ flog.trace(" DB is null, not saving message." )
137
+ }
130
138
mess
131
139
}
132
140
133
141
markAsError <- function (mess ,col , e ) {
134
142
processingError(mess ) <- e
135
- col $ update(paste(' {"_id":{"$oid":"' ,mess @ " _id" ,' "}}' ,sep = " " ),
136
- paste(' {"$set": {"pError":"' ,
137
- chartr(" \" " ," '" , # Problem with interior quotes.
138
- encodeString(toString(e ))),' "}}' ,sep = " " ))
143
+ if (! is.null(col )) {
144
+ col $ update(paste(' {"_id":{"$oid":"' ,mess @ " _id" ,' "}}' ,sep = " " ),
145
+ paste(' {"$set": {"pError":"' ,
146
+ chartr(" \" " ," '" , # Problem with interior quotes.
147
+ encodeString(toString(e ))),' "}}' ,sep = " " ))
148
+ } else {
149
+ flog.trace(" DB is null, not saving message." )
150
+ }
139
151
mess
140
152
}
141
153
@@ -170,14 +182,14 @@ cleanMessageJlist <- function (rec) {
170
182
171
183
parseMessage <- function (rec ) {
172
184
rec <- cleanMessageJlist(rec )
173
- new(" P4Message" ," _id" = ununboxer(rec $ " _id" ),
174
- app = as.vector (ununboxer(rec $ app )),
175
- uid = as.vector (ununboxer(rec $ uid )),
185
+ new(" P4Message" ," _id" = as.character( ununboxer(rec $ " _id" ) ),
186
+ app = as.character (ununboxer(rec $ app )),
187
+ uid = as.character (ununboxer(rec $ uid )),
176
188
context = as.vector(ununboxer(rec $ context )),
177
- sender = as.vector (ununboxer(rec $ sender )),
178
- mess = as.vector (ununboxer(rec $ mess )),
189
+ sender = as.character (ununboxer(rec $ sender )),
190
+ mess = as.character (ununboxer(rec $ mess )),
179
191
timestamp = as.POSIXlt(ununboxer(rec $ timestamp )),
180
- processed = ununboxer(rec $ processed ),
192
+ processed = as.logical( ununboxer(rec $ processed ) ),
181
193
pError = rec $ pError ,
182
194
data = parseData(rec $ data ))
183
195
}
@@ -378,3 +390,26 @@ all.equal.P4Message <- function (target, current, ...,checkTimestamp=FALSE,check
378
390
if (length(msg )== 0L ) TRUE
379
391
else msg
380
392
}
393
+
394
+ # ##
395
+ # # This is a construction I find myself using in a lot of places to
396
+ # # build up the "mongodb://" URI for the database.
397
+
398
+ makeDBuri <- function (username = " " ,password = " " , host = " localhost" ,
399
+ port = " " ,protocol = " mongodb" ) {
400
+ # # Setup DB URI
401
+ security <- " "
402
+ if (nchar(username ) > 0L ) {
403
+ if (nchar(password ) > 0L )
404
+ security <- paste(username ,password ,sep = " :" )
405
+ else
406
+ security <- username
407
+ }
408
+ if (nchar(port ) > 0L )
409
+ host <- paste(host ,port ,sep = " :" )
410
+ else
411
+ host <- host
412
+ if (nchar(security ) > 0L )
413
+ host <- paste(security ,host ,sep = " @" )
414
+ paste(paste(protocol ," :/" ,sep = " " ),host ,sep = " /" )
415
+ }
0 commit comments