-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Fongo DataStoreProvider
PostPosted: Wed Jan 21, 2015 7:31 am 
Newbie

Joined: Wed Jan 21, 2015 7:24 am
Posts: 6
Hi,
Hibernate OGM is an awesome tool which have an integration with MongoDB, but there is one missing engine and it is Fongo. Fongo is a project which uses Mongo client to create a Java embedded in-memory MongoDB. In fact no server endpoint is provided it is pure Java project (we can think like a Map). So if you want to use Hibernate OGM and write tests using Fongo you need to create your own DataStore provider.

Currently I have done this by:

Code:
public class FongoDBDatastoreProvider extends MongoDBDatastoreProvider {

    private static final long serialVersionUID = -4709436439644997081L;

    private static final Log log = LoggerFactory.getLogger();

    private MongoClient mongo;
    private DB mongoDb;
    private MongoDBConfiguration config;
    private ServiceRegistryImplementor serviceRegistry;

    @Override
    public void start() {
        try {
            if (mongo == null) {
                mongo = new Fongo("fongo").getMongo();
            }
            mongoDb = extractDatabase(mongo, config);
        } catch (Exception e) {
            // Wrap Exception in a ServiceException to make the stack trace more
            // friendly
            // Otherwise a generic unable to request service is thrown
            throw log.unableToStartDatastoreProvider(e);
        }
    }
   
    @Override
    public void stop() {
    }

    @Override
    public void injectServices(ServiceRegistryImplementor serviceRegistry) {
        this.serviceRegistry = serviceRegistry;
    }

    @Override
    public void configure(Map configurationValues) {
        OptionsService optionsService = serviceRegistry
                .getService(OptionsService.class);
        ClassLoaderService classLoaderService = serviceRegistry
                .getService(ClassLoaderService.class);
        ConfigurationPropertyReader propertyReader = new ConfigurationPropertyReader(
                configurationValues, classLoaderService);

        try {
            this.config = new MongoDBConfiguration(propertyReader,
                    optionsService.context().getGlobalOptions());
        } catch (Exception e) {
            // Wrap Exception in a ServiceException to make the stack trace more
            // friendly
            // Otherwise a generic unable to request service is thrown
            throw log.unableToConfigureDatastoreProvider(e);
        }
    }

    public DB getDatabase() {
        return mongoDb;
    }

    private DB extractDatabase(MongoClient mongo, MongoDBConfiguration config) {
        String databaseName = config.getDatabaseName();
        log.connectingToMongoDatabase(databaseName);

        if (!mongo.getDatabaseNames().contains(databaseName)) {
            if (config.isCreateDatabase()) {
                log.creatingDatabase(databaseName);
            } else {
                throw log.databaseDoesNotExistException(config
                        .getDatabaseName());
            }
        }
        return mongo.getDB(databaseName);
    }

}


I can extend from MongoDBDataStoreProvider because Fongo in fact follows MongoDB driver but overrides it to store all data locally instead of creating a remote connection.

And then in persistence.xml

Code:
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
   version="2.0">

   <!-- Use this for Neo4j -->
   <!--
   <persistence-unit name="hikePu" transaction-type="RESOURCE_LOCAL">
      <provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>

      <properties>
         <property name="hibernate.ogm.datastore.provider" value="neo4j_embedded" />
         <property name="hibernate.ogm.datastore.database" value="HikeDB" />
         <property name="hibernate.ogm.neo4j.database_path" value="target/test_data_dir" />
      </properties>
   </persistence-unit>
   -->

   <!-- Use this for MongoDB -->
   <persistence-unit name="hikePu" transaction-type="RESOURCE_LOCAL">
      <provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>

      <properties>
         <property name="hibernate.ogm.datastore.provider" value="org.hibernate.ogm.demos.ogm101.part1.provider.FongoDBDatastoreProvider" />
         <property name="hibernate.ogm.datastore.database" value="HikeDB" />
         <property name="hibernate.ogm.datastore.create_database" value="true" />
      </properties>
   </persistence-unit>
</persistence>


And with these changes I have been able to run Hibernate-OGM Hiker example test using Fongo instead of a remote(although it is a localhost) MongoDB.

My next step is to provide an integration to NoSQLUnit, so you can write applications and test them in NoSQLUnit.


Top
 Profile  
 
 Post subject: Re: Fongo DataStoreProvider
PostPosted: Wed Jan 21, 2015 9:58 am 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
Hi,

Didn't know about Fongo, thanks for giving that a try and reporting the outcome! That looks very interesting, would you be interested in contributing this feature to Hibernate OGM? That'd be awesome. I've created https://hibernate.atlassian.net/browse/OGM-725 to track this.

--Gunnar

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


Top
 Profile  
 
 Post subject: Re: Fongo DataStoreProvider
PostPosted: Wed Jan 21, 2015 10:04 am 
Newbie

Joined: Wed Jan 21, 2015 7:24 am
Posts: 6
Sure, in fact I have already signed the jboss confidentiality document (because of Arquillian) so no problem to add support for Fongo in Hibernate OGM.

I am going to create an account to JIRA and assign it to me. I will try to do it this weekend :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.