Skip to content

Commit 16c92df

Browse files
committed
Added to InsertDocumentAsJson and Up(date/sert)Document(s)AsJson custom
output functions.
1 parent 276ae82 commit 16c92df

File tree

8 files changed

+168
-39
lines changed

8 files changed

+168
-39
lines changed

com.ibm.streamsx.mongodb/com.ibm.streamsx.mongodb/Common/BSONCommon.pm

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,30 +31,45 @@ sub buildBSONObject(@) {
3131

3232

3333
sub buildBSONObjectWithKey(@) {
34-
my ($exprLocation, $attrName, $cppExpr, $splType) = @_;
34+
my ($exprLocation, $attrName, $cppExpr, $splType, $isJson) = @_;
35+
36+
my $buildBSONObjectFunc = $isJson ? \&buildBSONObjectFromJson : \&buildBSONObject;
37+
my $bsonObj = 'b0.obj()';
3538
my $seq = 0;
36-
39+
40+
# if $attrName is not empty then create a root element
3741
if ($attrName) {
3842
$seq++;
3943

4044
print qq(
4145
BSONObjBuilder b0;
4246
);
4347

44-
}
45-
46-
my ($appendFunction, $value) = BSONCommon::buildBSONObject($exprLocation, $cppExpr, $splType, $seq);
47-
48-
if ($attrName) {
48+
#my ($appendFunction, $value) = BSONCommon::buildBSONObject($exprLocation, $cppExpr, $splType, $seq);
49+
my ($appendFunction, $value) = $buildBSONObjectFunc->($exprLocation, $cppExpr, $splType, $seq);
4950

5051
print qq(
5152
b0.$appendFunction($attrName, $value);
5253
);
5354

5455
}
56+
else {
57+
my ($appendFunction, $value) = $buildBSONObjectFunc->($exprLocation, $cppExpr, $splType, $seq);
58+
$bsonObj = $value;
59+
}
60+
61+
print qq(
62+
const BSONObj & bsonObj = $bsonObj;
63+
);
64+
5565
}
5666

5767

68+
sub buildBSONObjectFromJson(@) {
69+
my ($exprLocation, $cppExpr) = @_;
70+
return ('append', "fromjson($cppExpr)");
71+
}
72+
5873
sub buildBSONObjectFromListOrSet(@) {
5974
my ($exprLocation, $cppExpr, $splType, $seq) = @_;
6075
my $valueType = SPL::CodeGen::Type::getElementType($splType);

com.ibm.streamsx.mongodb/com.ibm.streamsx.mongodb/Insert/Insert.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ One output port is defined for use with custom output functions and optionally t
4343
<description>writes 'data' to 'db.coolection' with a 'key' as a root element.</description>
4444
<prototype>&lt;any T> rstring InsertDocument(rstring db, rstring collection, rstring key, T data)</prototype>
4545
</function>
46+
<function pseudoFunction="true">
47+
<description>writes rstring JSON 'data' to 'db.coolection'.</description>
48+
<prototype>rstring InsertDocumentAsJson(rstring db, rstring collection, rstring data)</prototype>
49+
</function>
50+
<function pseudoFunction="true">
51+
<description>writes rstring JSON 'data' to 'db.coolection' with a 'key' as a root element.</description>
52+
<prototype>rstring InsertDocumentAsJson(rstring db, rstring collection, rstring key, rstring data)</prototype>
53+
</function>
4654
</customOutputFunction>
4755
</customOutputFunctions>
4856
<libraryDependencies>

com.ibm.streamsx.mongodb/com.ibm.streamsx.mongodb/Insert/Insert_cpp.cgt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ void MY_OPERATOR::process(Tuple const & tuple, uint32_t port) {
4545
my $name = $attribute->getName();
4646
if ($attribute->hasAssignmentWithOutputFunction()) {
4747
my $operation = $attribute->getAssignmentOutputFunctionName();
48+
my $isJson = ($operation =~ /AsJson$/) ? 1 : 0;
49+
4850
if ($operation eq 'AsIs') {
4951
my $init = $attribute->getAssignmentOutputFunctionParameterValueAt(0)->getCppExpression();
5052
%>
@@ -61,7 +63,7 @@ void MY_OPERATOR::process(Tuple const & tuple, uint32_t port) {
6163
}
6264
else {
6365
$expr = $attribute->getAssignmentOutputFunctionParameterValueAt(2);
64-
if (BSONCommon::keyLess($expr->getSPLType())) {
66+
if (!$isJson && BSONCommon::keyLess($expr->getSPLType())) {
6567
SPL::CodeGen::errorln("The type '%s' of the expression '%s' requires additional key parameter.", $expr->getSPLType(), $expr->getSPLExpression(), $expr->getSourceLocation());
6668
}
6769
}
@@ -86,7 +88,7 @@ void MY_OPERATOR::process(Tuple const & tuple, uint32_t port) {
8688
{
8789

8890
<%# [----- perl code -----]
89-
BSONCommon::buildBSONObjectWithKey($exprLocation, $key, $cppExpr, $splType);
91+
BSONCommon::buildBSONObjectWithKey($exprLocation, $key, $cppExpr, $splType, $isJson);
9092

9193
my $db = $attribute->getAssignmentOutputFunctionParameterValueAt(0)->getCppExpression();
9294
my $collection = $attribute->getAssignmentOutputFunctionParameterValueAt(1)->getCppExpression();
@@ -110,7 +112,7 @@ void MY_OPERATOR::process(Tuple const & tuple, uint32_t port) {
110112
connPtr->setDbProfilingLevel(<%=$db%>, ProfileAll);
111113
<%}%>
112114

113-
connPtr->insert(buildDbCollection(<%=$db%>, <%=$collection%>), b0.obj());
115+
connPtr->insert(buildDbCollection(<%=$db%>, <%=$collection%>), bsonObj);
114116
const string & errorMsg = connPtr->getLastError();
115117

116118
if (errorMsg == "") {

com.ibm.streamsx.mongodb/com.ibm.streamsx.mongodb/Insert/Insert_cpp.pm

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ sub main::generate($$) {
104104
my $name = $attribute->getName();
105105
if ($attribute->hasAssignmentWithOutputFunction()) {
106106
my $operation = $attribute->getAssignmentOutputFunctionName();
107+
my $isJson = $operation =~ /AsJson$/ ? 1 : 0;
107108
if ($operation eq 'AsIs') {
108109
my $init = $attribute->getAssignmentOutputFunctionParameterValueAt(0)->getCppExpression();
109110

@@ -126,7 +127,7 @@ sub main::generate($$) {
126127
}
127128
else {
128129
$expr = $attribute->getAssignmentOutputFunctionParameterValueAt(2);
129-
if (BSONCommon::keyLess($expr->getSPLType())) {
130+
if (!$isJson && BSONCommon::keyLess($expr->getSPLType())) {
130131
SPL::CodeGen::errorln("The type '%s' of the expression '%s' requires additional key parameter.", $expr->getSPLType(), $expr->getSPLExpression(), $expr->getSourceLocation());
131132
}
132133
}
@@ -156,7 +157,7 @@ sub main::generate($$) {
156157
print ' {', "\n";
157158
print ' ', "\n";
158159
# [----- perl code -----]
159-
BSONCommon::buildBSONObjectWithKey($exprLocation, $key, $cppExpr, $splType);
160+
BSONCommon::buildBSONObjectWithKey($exprLocation, $key, $cppExpr, $splType, $isJson);
160161

161162
my $db = $attribute->getAssignmentOutputFunctionParameterValueAt(0)->getCppExpression();
162163
my $collection = $attribute->getAssignmentOutputFunctionParameterValueAt(1)->getCppExpression();
@@ -211,7 +212,7 @@ sub main::generate($$) {
211212
print $db;
212213
print ', ';
213214
print $collection;
214-
print '), b0.obj());', "\n";
215+
print '), bsonObj);', "\n";
215216
print ' const string & errorMsg = connPtr->getLastError();', "\n";
216217
print ' ', "\n";
217218
print ' if (errorMsg == "") {', "\n";

com.ibm.streamsx.mongodb/com.ibm.streamsx.mongodb/Update/Update.xml

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,33 +43,65 @@ One output port is defined for use with custom output functions and optionally t
4343
<prototype>&lt;any T> rstring UpdateDocument(rstring db, rstring collection, T data)</prototype>
4444
</function>
4545
<function pseudoFunction="true">
46-
<description>updates one instance of 'data' to 'db.coolection' with a 'key' as a root element.</description>
46+
<description>updates one instance of 'data' in 'db.coolection' with a 'key' as a root element.</description>
4747
<prototype>&lt;any T> rstring UpdateDocument(rstring db, rstring collection, rstring key, T data)</prototype>
4848
</function>
4949
<function pseudoFunction="true">
5050
<description>updates all 'data' in 'db.coolection'. T can be of type tuple or map only.</description>
5151
<prototype>&lt;any T> rstring UpdateDocuments(rstring db, rstring collection, T data)</prototype>
5252
</function>
5353
<function pseudoFunction="true">
54-
<description>updates all 'data' to 'db.coolection' with a 'key' as a root element.</description>
54+
<description>updates all 'data' in 'db.coolection' with a 'key' as a root element.</description>
5555
<prototype>&lt;any T> rstring UpdateDocuments(rstring db, rstring collection, rstring key, T data)</prototype>
5656
</function>
57+
<function pseudoFunction="true">
58+
<description>updates one instance of rstring 'json' in 'db.coolection'.</description>
59+
<prototype>rstring UpdateDocumentAsJson(rstring db, rstring collection, rstring json)</prototype>
60+
</function>
61+
<function pseudoFunction="true">
62+
<description>updates one instance of rstring 'json' in 'db.coolection' with a 'key' as a root element.</description>
63+
<prototype>rstring UpdateDocumentAsJson(rstring db, rstring collection, rstring key, rstring json)</prototype>
64+
</function>
65+
<function pseudoFunction="true">
66+
<description>updates all rstring 'json' in 'db.coolection'.</description>
67+
<prototype>rstring UpdateDocumentsAsJson(rstring db, rstring collection, rstring json)</prototype>
68+
</function>
69+
<function pseudoFunction="true">
70+
<description>updates all rstring 'json' in 'db.coolection' with a 'key' as a root element.</description>
71+
<prototype>rstring UpdateDocumentsAsJson(rstring db, rstring collection, rstring key, rstring json)</prototype>
72+
</function>
5773
<function pseudoFunction="true">
5874
<description>upserts one instance of 'data' in 'db.coolection'. T can be of type tuple or map only.</description>
5975
<prototype>&lt;any T> rstring UpsertDocument(rstring db, rstring collection, T data)</prototype>
6076
</function>
6177
<function pseudoFunction="true">
62-
<description>upserts one instance of 'data' to 'db.coolection' with a 'key' as a root element.</description>
78+
<description>upserts one instance of 'data' in 'db.coolection' with a 'key' as a root element.</description>
6379
<prototype>&lt;any T> rstring UpsertDocument(rstring db, rstring collection, rstring key, T data)</prototype>
6480
</function>
6581
<function pseudoFunction="true">
6682
<description>upserts all 'data' in 'db.coolection'. T can be of type tuple or map only.</description>
6783
<prototype>&lt;any T> rstring UpsertDocuments(rstring db, rstring collection, T data)</prototype>
6884
</function>
6985
<function pseudoFunction="true">
70-
<description>upserts all 'data' to 'db.coolection' with a 'key' as a root element.</description>
86+
<description>upserts all 'data' in 'db.coolection' with a 'key' as a root element.</description>
7187
<prototype>&lt;any T> rstring UpsertDocuments(rstring db, rstring collection, rstring key, T data)</prototype>
7288
</function>
89+
<function pseudoFunction="true">
90+
<description>upserts one instance of rstring 'json' in 'db.coolection'.</description>
91+
<prototype>rstring UpsertDocumentAsJson(rstring db, rstring collection, rstring json)</prototype>
92+
</function>
93+
<function pseudoFunction="true">
94+
<description>upserts one instance of 'data' in 'db.coolection' with a 'key' as a root element.</description>
95+
<prototype>rstring UpsertDocumentAsJson(rstring db, rstring collection, rstring key, rstring json)</prototype>
96+
</function>
97+
<function pseudoFunction="true">
98+
<description>upserts all rstring 'json' in 'db.coolection'.</description>
99+
<prototype>rstring UpsertDocumentsAsJson(rstring db, rstring collection, rstring json)</prototype>
100+
</function>
101+
<function pseudoFunction="true">
102+
<description>upserts all rstring 'json' in 'db.coolection' with a 'key' as a root element.</description>
103+
<prototype>rstring UpsertDocumentsAsJson(rstring db, rstring collection, rstring key, rstring json)</prototype>
104+
</function>
73105
</customOutputFunction>
74106
</customOutputFunctions>
75107
<libraryDependencies>

com.ibm.streamsx.mongodb/com.ibm.streamsx.mongodb/Update/Update_cpp.cgt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ void MY_OPERATOR::process(Tuple const & tuple, uint32_t port) {
5151
my $name = $attribute->getName();
5252
if ($attribute->hasAssignmentWithOutputFunction()) {
5353
my $operation = $attribute->getAssignmentOutputFunctionName();
54+
my $upsert = ($operation =~ /^Upsert/) ? 'true' : 'false';
55+
my $multi = ($operation =~ /Documents/) ? 'true' : 'false';
56+
my $isJson = ($operation =~ /AsJson$/) ? 1 : 0;
57+
5458
if ($operation eq 'AsIs') {
5559
my $init = $attribute->getAssignmentOutputFunctionParameterValueAt(0)->getCppExpression();
5660
%>
@@ -67,7 +71,7 @@ void MY_OPERATOR::process(Tuple const & tuple, uint32_t port) {
6771
}
6872
else {
6973
$expr = $attribute->getAssignmentOutputFunctionParameterValueAt(2);
70-
if (BSONCommon::keyLess($expr->getSPLType())) {
74+
if (!$isJson && BSONCommon::keyLess($expr->getSPLType())) {
7175
SPL::CodeGen::errorln("The type '%s' of the expression '%s' requires additional key parameter.", $expr->getSPLType(), $expr->getSPLExpression(), $expr->getSourceLocation());
7276
}
7377
}
@@ -92,7 +96,7 @@ void MY_OPERATOR::process(Tuple const & tuple, uint32_t port) {
9296
{
9397

9498
<%# [----- perl code -----]
95-
BSONCommon::buildBSONObjectWithKey($exprLocation, $key, $cppExpr, $splType);
99+
BSONCommon::buildBSONObjectWithKey($exprLocation, $key, $cppExpr, $splType, $isJson);
96100

97101
my $db = $attribute->getAssignmentOutputFunctionParameterValueAt(0)->getCppExpression();
98102
my $collection = $attribute->getAssignmentOutputFunctionParameterValueAt(1)->getCppExpression();
@@ -123,12 +127,9 @@ void MY_OPERATOR::process(Tuple const & tuple, uint32_t port) {
123127
connPtr->setDbProfilingLevel(<%=$db%>, ProfileAll);
124128
<%}%>
125129

126-
// connPtr-><%=$BSONCommon::methods{$operation}%>(buildDbCollection(<%=$db%>, <%=$collection%>), b0.obj());
127130
<%
128-
my $upsert = ($operation =~ /^Upsert/) ? 'true' : 'false';
129-
my $multi = ($operation =~ /Documents$/) ? 'true' : 'false';
130131
%>
131-
connPtr->update(buildDbCollection(<%=$db%>, <%=$collection%>), findQueryBO, b0.obj(), <%=$upsert%>, <%=$multi%>);
132+
connPtr->update(buildDbCollection(<%=$db%>, <%=$collection%>), findQueryBO, bsonObj, <%=$upsert%>, <%=$multi%>);
132133
const string & errorMsg = connPtr->getLastError();
133134

134135
if (errorMsg == "") {

com.ibm.streamsx.mongodb/com.ibm.streamsx.mongodb/Update/Update_cpp.pm

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,10 @@ sub main::generate($$) {
172172
my $name = $attribute->getName();
173173
if ($attribute->hasAssignmentWithOutputFunction()) {
174174
my $operation = $attribute->getAssignmentOutputFunctionName();
175+
my $upsert = ($operation =~ /^Upsert/) ? 'true' : 'false';
176+
my $multi = ($operation =~ /Documents/) ? 'true' : 'false';
177+
my $isJson = ($operation =~ /AsJson$/) ? 1 : 0;
178+
175179
if ($operation eq 'AsIs') {
176180
my $init = $attribute->getAssignmentOutputFunctionParameterValueAt(0)->getCppExpression();
177181

@@ -194,7 +198,7 @@ sub main::generate($$) {
194198
}
195199
else {
196200
$expr = $attribute->getAssignmentOutputFunctionParameterValueAt(2);
197-
if (BSONCommon::keyLess($expr->getSPLType())) {
201+
if (!$isJson && BSONCommon::keyLess($expr->getSPLType())) {
198202
SPL::CodeGen::errorln("The type '%s' of the expression '%s' requires additional key parameter.", $expr->getSPLType(), $expr->getSPLExpression(), $expr->getSourceLocation());
199203
}
200204
}
@@ -224,7 +228,7 @@ sub main::generate($$) {
224228
print ' {', "\n";
225229
print ' ', "\n";
226230
# [----- perl code -----]
227-
BSONCommon::buildBSONObjectWithKey($exprLocation, $key, $cppExpr, $splType);
231+
BSONCommon::buildBSONObjectWithKey($exprLocation, $key, $cppExpr, $splType, $isJson);
228232

229233
my $db = $attribute->getAssignmentOutputFunctionParameterValueAt(0)->getCppExpression();
230234
my $collection = $attribute->getAssignmentOutputFunctionParameterValueAt(1)->getCppExpression();
@@ -288,23 +292,14 @@ sub main::generate($$) {
288292
}
289293
print "\n";
290294
print ' ', "\n";
291-
print '// connPtr->';
292-
print $BSONCommon::methods{$operation};
293-
print '(buildDbCollection(';
294-
print $db;
295-
print ', ';
296-
print $collection;
297-
print '), b0.obj());', "\n";
298295
print ' ';
299-
my $upsert = ($operation =~ /^Upsert/) ? 'true' : 'false';
300-
my $multi = ($operation =~ /Documents$/) ? 'true' : 'false';
301296

302297
print "\n";
303298
print ' connPtr->update(buildDbCollection(';
304299
print $db;
305300
print ', ';
306301
print $collection;
307-
print '), findQueryBO, b0.obj(), ';
302+
print '), findQueryBO, bsonObj, ';
308303
print $upsert;
309304
print ', ';
310305
print $multi;

0 commit comments

Comments
 (0)