Permalink
Browse files

/

  • Loading branch information...
1 parent d674910 commit b80f6054cc24c7ae3c3df67feb66c88944bf5d51 billy committed Oct 22, 2009
Showing with 134 additions and 2 deletions.
  1. +43 −1 part2/ExpressionBuilder.cfc
  2. +80 −0 part2/ExpressionBuilderTest.cfc
  3. +11 −1 part2/MongoDBTest.cfc
View
44 part2/ExpressionBuilder.cfc
@@ -72,15 +72,44 @@ function get(){
}
-//may need at least some exception handling
+//May need at least some exception handling
+
+function $tags(val){
+ builder.add( 'TAGS', val );
+}
+
+ //vals should be list or array
+function $in(element,vals){
+ if(isArray(vals)) return addArrayCriteria(element,vals,'$in');
+ return addArrayCriteria(element, listToArray(vals),'$in');
+}
+
+function $nin(element,vals){
+ if(isArray(vals)) return addArrayCriteria(element,vals,'$nin');
+ return addArrayCriteria(element, listToArray(vals),'$nin');
+}
+
+
+function $eq(element,val){
+ builder.add( element, val );
+}
+
+
+function $ne(element,val){
+ return addNumericCriteria(element,val,'$ne');
+}
+
+
function $lt(element,val){
return addNumericCriteria(element,val,'$lt');
}
+
function $lte(element,val){
return addNumericCriteria(element,val,'$lte');
}
+
function $gt(element,val){
return addNumericCriteria(element,val,'$gt');
}
@@ -112,4 +141,17 @@ But, this also proved to be a very good refactor.
</cfscript>
</cffunction>
+<cffunction name="addArrayCriteria" hint="refactored $expressions for numerics">
+ <cfargument name="element" type="string" hint="The array element in the document we're searching"/>
+ <cfargument name="val" type="array" hint="The value(s) of an element in the array" />
+ <cfargument name="type" type="string" hint="$in,$nin,etc." />
+ <cfscript>
+ var exp = {};
+ exp[type] = val;
+ builder.add( element, exp );
+ return this;
+ </cfscript>
+</cffunction>
+
+
</cfcomponent>
View
80 part2/ExpressionBuilderTest.cfc
@@ -5,6 +5,86 @@
key_exp = {AUTHOR=1,TITLE=1,TS=1};
keys = createObject('java', 'com.mongodb.BasicDBObject').init(key_exp);
+//array tests: search tags:
+function testNIN(){
+ builder.start();
+ temp = builder.$nin( 'AUTHOR', ['bill_1','bill_202','bill_101','bill_999','bogus_bill'] );
+ q = createObject('java', 'com.mongodb.BasicDBObject').init(builder.get());
+ debug(q);
+ items = coll.find(q,keys);
+ ia = items.toArray();
+ debug(items.count());
+ //debug(items.toArray().toString());
+ assertEquals( 996 , items.count() );
+}
+
+
+function testNINWithStringList(){
+ builder.start();
+ temp = builder.$nin( 'AUTHOR', 'bill_1,bill_202,bill_101,bill_999,bogus_bill' );
+ q = createObject('java', 'com.mongodb.BasicDBObject').init(builder.get());
+ debug(q);
+ items = coll.find(q,keys);
+ ia = items.toArray();
+ debug(items.count());
+ //debug(items.toArray().toString());
+ assertEquals( 996 , items.count() );
+
+}
+
+
+function testInWithStringList(){
+ builder.start();
+ temp = builder.$in( 'AUTHOR', 'bill_1,bill_202,bill_101,bill_999,bogus_bill' );
+ q = createObject('java', 'com.mongodb.BasicDBObject').init(builder.get());
+ debug(q);
+ items = coll.find(q,keys);
+ ia = items.toArray();
+ debug(items.count());
+ //debug(items.toArray().toString());
+ assertEquals( 4 , items.count() );
+
+}
+
+function testIn(){
+ builder.start();
+ temp = builder.$in( 'AUTHOR', ['bill_1','bill_202','bill_101','bill_999','bogus_bill'] );
+ q = createObject('java', 'com.mongodb.BasicDBObject').init(builder.get());
+ debug(q);
+ items = coll.find(q,keys);
+ ia = items.toArray();
+ debug(items.count());
+ //debug(items.toArray().toString());
+ assertEquals( 4 , items.count() );
+}
+
+
+function testEQ(){
+ builder.start();
+ temp = builder.$eq( 'TS', 1256174339614); //not good test - volitile
+ q = createObject('java', 'com.mongodb.BasicDBObject').init(builder.get());
+ debug(q);
+ items = coll.find(q,keys);
+ ia = items.toArray();
+ debug(items.count());
+ //debug(items.toArray().toString());
+ assertEquals( 1 , items.count() );
+}
+
+
+
+function testNEQ(){
+ builder.start();
+ temp = builder.$ne( 'TS', -9);
+ q = createObject('java', 'com.mongodb.BasicDBObject').init(builder.get());
+ debug(q);
+ items = coll.find(q,keys);
+ ia = items.toArray();
+ debug(items.count());
+ //debug(items.toArray().toString());
+ assertEquals( 1000 , items.count() );
+}
+
function testLTE(){
builder.start();
View
12 part2/MongoDBTest.cfc
@@ -160,7 +160,7 @@
function genDataTest(){
- //genBlogData();
+ // genBlogData() ;
}
@@ -180,10 +180,20 @@
var i = 0;
var entry = {foo='bar'};
var start = getTickCount();
+ var tags = ['Food','Java','Comics','Games','Python','NoSQL','Ruby','ColdFusion','TDD','JavaScript','JSON','MongoDB'];
+ var max = arrayLen(tags);
+ var shuffled = createObject('java','java.util.ArrayList').init(tags);
+ var r1 = randrange(1,max);
+ var r2 = randrange(r1,max)
+ var newTags = [];
+ //Note case sensitivity!!
mongo.getCollection('blog');
for(i; i < 1000;i++){
+ createObject('java','java.util.Collections').shuffle(tags);
+ newTags = shuffled.subList(r1,r2);
entry.title = 'Blog Title No.' & i;
entry.author = 'bill_' & i;
+ entry.tags = newTags;
entry.ts = javacast('long',getTickCount());
entry.body = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. In eu justo lectus. Morbi aliquet consectetur consequat. Mauris mauris nulla, condimentum in aliquet nec, suscipit in lacus. Donec vel ante ut metus imperdiet interdum. Curabitur non sapien at felis egestas bibendum in eu urna. In rutrum ligula erat. Integer tristique viverra consequat. Curabitur tristique velit vel nunc aliquet congue eleifend lacus cursus. Aenean a lorem a arcu tincidunt tempus. Nulla faucibus diam in sem consequat tincidunt. Aenean quis nunc vitae leo luctus porta. Etiam justo enim, imperdiet vel commodo sed, placerat nec dolor. Nunc placerat sapien id ligula varius eget tempus est eleifend.

0 comments on commit b80f605

Please sign in to comment.