Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[soc-dev] GSoC 2012 Project - XText based query engine for MongoEMF

Hi All,

I want to finish a XText and EMF relative GSoC project this summar, it is a simple introduction to my GSoC project here:

Ed Merks and Bryan Hunt have created a project called MongoEMF that allows you to persist EMF objects to MongoDB. MongoEMF has the ability to query objects based on attribute and reference values, but is lacking some functionality and robustness. This GSoC project will re-write MongoEMF's query engine using Xtext and EMF. 

Queries in MongoDB are expressed as JSON (BSON). Usually we think of query object as the equivalent of a SQL "WHERE" clause: 
db.users.find( { x : 3, y : "abc" } ).sort({x:1}); // select * from users where x=3 and y='abc' order by x asc;

So we would like to use SQL to be MongoEMF's query string (Use XText technology and special grammar to generate SQL model, some kind of similar with EMF Query 2 project), then user can build SQL to query data from MongoDB, get target EMF object list easily. I have finished a demo for this search engine, host the demo project code in Github MongoEMF-query-engine[1]. If you are interested in this project, you can get the detailed project proposal here [2]. 

The following code describe how this search engine works:

1. Suppose we have a MongoDB table with following records:
> db.users.find();
{ "_id" : ObjectId("4f65967bd7fdfafd4d45cece"), "name" : "Test User1", "age" : 1
 }
{ "_id" : ObjectId("4f65967bd7fdfafd4d45cecf"), "name" : "Test User2", "age" : 2
 }
{ "_id" : ObjectId("4f65967bd7fdfafd4d45ced0"), "name" : "Test User3", "age" : 3
 }
{ "_id" : ObjectId("4f65967bd7fdfafd4d45ced1"), "name" : "Test User4", "age" : 4
 }
{ "_id" : ObjectId("4f65967bd7fdfafd4d45ced2"), "name" : "Test User5", "age" : 5
 }
{ "_id" : ObjectId("4f65967bd7fdfafd4d45ced3"), "name" : "Test User6", "age" : 6
 }
{ "_id" : ObjectId("4f65967bd7fdfafd4d45ced4"), "name" : "Test User7", "age" : 7
 }
{ "_id" : ObjectId("4f65967bd7fdfafd4d45ced5"), "name" : "Test User8", "age" : 8
 }
{ "_id" : ObjectId("4f65967bd7fdfafd4d45ced6"), "name" : "Test User9", "age" : 9
 }
{ "_id" : ObjectId("4f65967bd7fdfafd4d45ced7"), "name" : "Test User10", "age" :
10 }

2. Query MongoDB by following code:
               //Transfer query language => EMF query model
new org.eclipse.emf.mwe.utils.StandaloneSetup().setPlatformUri("../");
Injector injector = new MongoSQLStandaloneSetup().createInjectorAndDoEMFRegistration();
XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
Resource resource = resourceSet.createResource(URI.createURI("dummy:/example.mongosql"));
InputStream in = new ByteArrayInputStream("SELECT * FROM mongo://localhost:27017/tigergui/users WHERE name='Test User7' AND age>2".getBytes());
try {
resource.load(in, resourceSet.getLoadOptions());
} catch (IOException e) {
e.printStackTrace();
}
ModelImpl model = (ModelImpl) resource.getContents().get(0);
//Transfer EMF query model => DBObject
        Mongo m = null;
try {
m = new Mongo( model.getDb().getUrl(), (int) model.getDb().getPort());
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
e.printStackTrace();
}
DB db = m.getDB(model.getDb().getDbName());
        DBCollection coll = db.getCollection(model.getDb().getName());
        BasicDBObject query = (BasicDBObject) tansferModule(model);
        
        //Query MongoDB by DBObject
        DBCursor cur = coll.find(query);
        while(cur.hasNext()) {
            System.out.println(cur.next());
        }

3. Query result:

{ "_id" : { "$oid" : "4f65967bd7fdfafd4d45ced4"} , "name" : "Test User7" , "age" : 7} 

And if we change the SQL to "SELECT * FROM mongo://localhost:27017/tigergui/users WHERE age>2", the search result will be:

{ "_id" : { "$oid" : "4f65967bd7fdfafd4d45ced0"} , "name" : "Test User3" , "age" : 3}
{ "_id" : { "$oid" : "4f65967bd7fdfafd4d45ced1"} , "name" : "Test User4" , "age" : 4}
{ "_id" : { "$oid" : "4f65967bd7fdfafd4d45ced2"} , "name" : "Test User5" , "age" : 5}
{ "_id" : { "$oid" : "4f65967bd7fdfafd4d45ced3"} , "name" : "Test User6" , "age" : 6}
{ "_id" : { "$oid" : "4f65967bd7fdfafd4d45ced4"} , "name" : "Test User7" , "age" : 7}
{ "_id" : { "$oid" : "4f65967bd7fdfafd4d45ced5"} , "name" : "Test User8" , "age" : 8}
{ "_id" : { "$oid" : "4f65967bd7fdfafd4d45ced6"} , "name" : "Test User9" , "age" : 9}
{ "_id" : { "$oid" : "4f65967bd7fdfafd4d45ced7"} , "name" : "Test User10" , "age" : 10}

Any advises, let me know, Thank you :-) 

[1] https://github.com/tigergui1990/MongoEMF-query-engine
[2] https://github.com/tigergui1990/MongoEMF-query-engine/wiki/XText-based-query-engine-for-MongoEMF-project-proposal

--
Best Regards From Tiger Gui
--------------------------------------
Open source is some kind of life attitude

Back to the top