Permalink
Browse files

adding search wrapper.

  • Loading branch information...
1 parent b20c988 commit f001fbf879b5c9ed5f15c344123405653ecb13ab billy committed Oct 23, 2009
Showing with 210 additions and 83 deletions.
  1. +1 −0 .project
  2. +27 −0 part2/ExpressionBuilderTest.cfc
  3. +133 −11 part2/MongoDB.cfc
  4. +3 −72 part2/MongoDBTest.cfc
  5. +46 −0 part2/SearchMongoTest.cfc
View
@@ -7,5 +7,6 @@
<buildSpec>
</buildSpec>
<natures>
+ <nature>org.cfeclipse.cfml.CFENature</nature>
</natures>
</projectDescription>
@@ -6,6 +6,33 @@
keys = createObject('java', 'com.mongodb.BasicDBObject').init(key_exp);
+function $testKeys(){
+ var key_exp = {AUTHOR=1};
+ var keys = createObject('java', 'com.mongodb.BasicDBObject').init(key_exp);
+ temp = builder.regex( 'TITLE', '.*No.600$');
+ q = createObject('java', 'com.mongodb.BasicDBObject').init(builder.get());
+ items = coll.find( q, keys );
+ ia = items.toArray();
+ debug(items.toArray());
+ debug(items.toArray().toString());
+
+}
+
+
+function $testInRange(){
+ fail('not implemented. but good idea');
+
+ criteria = builder.start().
+ inRange( 'TS', 0, 9999999999).
+ get();
+
+ q = createObject('java', 'com.mongodb.BasicDBObject').init(criteria);
+ debug(q);
+ items = coll.find(q,keys);
+ debug(items.count());
+ debug(items.toArray().toString());
+}
+
function $testFluentChain(){
name_list = ['bill_1','bill_202','bill_101','bill_999','bogus_bill','bill_506'] ;
View
@@ -1,24 +1,32 @@
<cfcomponent output="false">
<cfscript>
-//This is configurable.
-server_name = 'localhost';
-server_port = 27017;
-db_name = 'default_db';
-collection_name = 'default_collection';
+//This maybe should be a config object
+config = {
+ server_name = 'localhost',
+ server_port = 27017,
+ db_name = 'default_db',
+ collection_name = 'default_collection'
+ };
+
-//maybe this goes in super class?
-mongo = createObject('java', 'com.mongodb.Mongo').init( variables.server_name , variables.server_port );
-db = mongo.getDb(db_name);
-collection = db.getCollection(collection_name);
+//maybe this goes in super class? Or make factory for returning mongos
+/*--------------------------------------------------------------------
+ mongo1 = factory.createMongo(config);
+--------------------------------------------------------------------*/
+mongo = createObject('java', 'com.mongodb.Mongo').init( variables.config.server_name , variables.config.server_port );
+db = mongo.getDb(config.db_name);
+collection = db.getCollection(config.collection_name);
+expression_builder = createObject('component', 'ExpressionBuilder')
/*---------------------------------------------------------------------
DSL for MongoDB searches:
mongo.expressionBuilder().
- results = mongo.startsWith('name','foo'). //string
+ results = mongo.getCollection('blog').
+ startsWith('name','foo'). //string
endsWith('title','bar'). //string
exists('field','value'). //string
regex('field','value'). //string
@@ -33,7 +41,7 @@ collection = db.getCollection(collection_name);
size('field','value'). //numeric
search('title,author,date', limit, start);
- search(keys=[keys_to_return],limit=num,start=num);
+ search(keys=list_of_keys_to_return,sort={field=direction},limit=num,start=num);
@@ -132,9 +140,123 @@ function getCollection(collection_name){
return collection;
}
+function collection(collection_name){
+ collection = db.getCollection(collection_name);
+ return this;
+}
+
+
+function listToStruct(list){
+ var item = '';
+ var s = {};
+ var i = 1;
+ for(i; i lte listlen(list); i++) {
+ s.put(listgetat(list,i),1);
+ }
+ return s;
+}
+
+
+/*---------------------------------------------------------
+ Expression Builder Wrappers
+ ---------------------------------------------------------*/
+
+function startsWith(element, val){
+ expression_builder.startsWith(element, val);
+ return this;
+}
+
+function endsWith(element, val){
+ expression_builder.endsWith(element, val);
+ return this;
+}
+
+
+function exists(element, val){
+ var regex = '.*' & val & '.*';
+ expression_builder.exists( element, pattern.compile(regex) );
+ return this;
+}
+
+function regex(element, val){
+ var regex = val;
+ expression_builder.regex( element, pattern.compile(regex) );
+ return this;
+}
+
+function where( js_expression ){
+ expression_builder.where( '$where', js_expression );
+}
+
+function inArray(element, val){
+ expression_builder.inArray( element, val );
+ return this;
+}
+
+ //vals should be list or array
+function $in(element,vals){
+ expression_builder.$in(element,vals);
+ return this;
+}
+
+function $nin(element,vals){
+ expression_builder.$in(element,vals);
+ return this;
+}
+
+
+function $eq(element,val){
+ expression_builder.$eq(element,val);
+ return this;
+}
+
+
+function $ne(element,val){
+ expression_builder.$ne(element,val);
+ return this;
+}
+
+
+function $lt(element,val){
+ expression_builder.$lt(element,val);
+ return this;
+ }
+
+
+function $lte(element,val){
+ expression_builder.$lte(element,val);
+ return this;
+}
+
+
+function $gt(element,val){
+ expression_builder.$gt(element,val);
+ return this;
+}
+
+
+function $gte(element,val){
+ expression_builder.$gte(element,val);
+ return this;
+}
+
</cfscript>
+
+<cffunction name="search">
+ <cfargument name="keys_to_return" type="string" required="false" default="" hint="A list of keys to return" />
+ <cfscript>
+ var key_exp = listToStruct(keys_to_return);
+ var keys = createObject('java', 'com.mongodb.BasicDBObject').init(key_exp);
+ var search_results = [];
+ var criteria = expression_builder.get();
+ var q = createObject('java', 'com.mongodb.BasicDBObject').init(criteria);
+ search_results = collection.find(q,keys);
+ return search_results;
+ </cfscript>
+</cffunction>
+
</cfcomponent>
View
@@ -56,8 +56,8 @@
//exp['$where'] = 'this.TS > 1256133363469';
//exp = { $gt=1256133363469 };
//exp = {};
- exp['$gte']= javacast('long',1256148921000 );
- exp['$lte']= javacast('long',1256148921000 );
+ //exp['$gte']= javacast('long',1256148921000 );
+ exp['$gte']= javacast('long',1256148921000 );
debug( exp );
//q = createObject('java', 'com.mongodb.BasicDBObject').init("TS", javacast('long',1256148921000) );
@@ -71,76 +71,7 @@
}
-
- function $exploreStringSearchExpression2(){
- coll = mongo.getCollection('blog');
-
- key_exp = {TITLE=1,TS=1,AUTHOR=1};
- keys = createObject('java', 'com.mongodb.BasicDBObject').init(key_exp);
-
- debug( 'bill' > 'z' );
-
- exp = createObject('java', 'com.mongodb.BasicDBObjectBuilder').start();
- //exp.add( '$ne', javacast('long',1256148920969) ); //bill_792
- exp.add( '$gt', 'bill_792' );
-
- debug( exp.get() );
-
- q = createObject('java', 'com.mongodb.BasicDBObject').init("AUTHOR", exp.get() );
- items = coll.find( q, keys );
- debug(items.count());
- debug(items.toArray().toString());
-
- }
-
-
- function $buildSearchExpressionWithBuilder2(){
- coll = mongo.getCollection('blog');
-
- // BasicDBObjectBuilder.start().add( "name" , "eliot" ).add( "number" , 17 ).get()
-
- key_exp = {TITLE=1,TS=1};
- keys = createObject('java', 'com.mongodb.BasicDBObject').init(key_exp);
-
- exp = createObject('java', 'com.mongodb.BasicDBObjectBuilder').start();
- exp.add( '$gte', javacast('long',1256148921000 ) );
- exp.add( '$lte', javacast('long',1256148921000 ) );
-
- debug( exp.get() );
-
- q = createObject('java', 'com.mongodb.BasicDBObject').init("TS", exp.get() );
- items = coll.find( q, keys );
- debug(items.count());
- debug(items.toArray().toString());
-
- }
-
-
- function $findByExpression2(){
- coll = mongo.getCollection('blog');
-
- key_exp = {TITLE=1,TS=1};
- keys = createObject('java', 'com.mongodb.BasicDBObject').init(key_exp);
-
- // { a : { $gt: 3 } }
- //exp['$where'] = 'this.TS > 1256133363469';
- //exp = { $gt=1256133363469 };
- //exp = {};
- exp['$gte']= javacast('long',1256148921000 );
- exp['$lte']= javacast('long',1256148921000 );
- debug( exp );
-
- //q = createObject('java', 'com.mongodb.BasicDBObject').init("TS", javacast('long',1256148921000) );
- q = createObject('java', 'com.mongodb.BasicDBObject').init("TS", exp );
- items = coll.find( q, keys );
-
- debug(items.count());
- debug(items.toArray().toString());
- assert(items.count() > 0);
-
- }
-
- function $findByRegEx(){
+ function $findByRegEx(){
coll = mongo.getCollection('blog');
//raw = mongo.getMongo(); //raw jo
debug(coll.getName());
@@ -0,0 +1,46 @@
+<cfcomponent output="false" extends="mxunit.framework.TestCase">
+<cfscript>
+
+
+ function searchTitleStartsWith() {
+ results = mongo.collection('blog').startsWith('TITLE','Blog Title No.60').search('TITLE,AUTHOR,PUB_DATE');
+ debug( results.toArray().toString() );
+ assertEquals( 11,results.toArray().size() );
+ }
+
+ function searchTitleEndsWith() {
+ results = mongo.collection('blog').endsWith('TITLE','Blog Title No.60').search('TITLE,AUTHOR,PUB_DATE');
+ debug( results.toArray().toString() );
+ assertEquals( 11,results.toArray().size() );
+ }
+
+
+
+
+ function testEmptyListToStruct(){
+ list = '';
+ s = mongo.listToStruct(list);
+ debug(s);
+ assertEquals(0,s.size());
+ }
+
+
+ function testListToStruct(){
+ list = 'a,b,c,d,e,f,g';
+ s = mongo.listToStruct(list);
+ debug(s);
+ assertEquals(7,s.size());
+ }
+
+
+ function setUp(){
+ mongo = createObject('component','MongoDB');
+ }
+
+ function tearDown(){
+
+ }
+
+
+</cfscript>
+</cfcomponent>

0 comments on commit f001fbf

Please sign in to comment.