0 Replies Latest reply on Jun 1, 2012 1:18 PM by liberticide

    mongodb bundle persistance manager

    liberticide

      i'd like to implement persistance manger using mongo db. I got the site up running and able to see all the nodes under crxde without issue. but the content page does not load up properly and also won't be able to go to certain page. I saw  DefaultSecurityManager: Failed to create anonymous user. in crx log file. I think both problem i am experiencing are probably related to this error. I don't know how to attach jar file, so i am going to post the code in the post. I think this is nice to have inaddition to persistance managers out there already. Please help!!

       

      - steven

       

      package org.apache.jackrabbit.core.persistence.mongo;

       

       

      import com.mongodb.*;

      import org.apache.jackrabbit.core.id.NodeId;

      import org.apache.jackrabbit.core.id.PropertyId;

      import org.apache.jackrabbit.core.persistence.PMContext;

      import org.apache.jackrabbit.core.persistence.bundle.AbstractBundlePersistenceManager;

      import org.apache.jackrabbit.core.persistence.util.*;

      import org.apache.jackrabbit.core.state.ItemStateException;

      import org.apache.jackrabbit.core.state.NoSuchItemStateException;

      import org.apache.jackrabbit.core.state.NodeReferences;

      import org.bson.types.Binary;

      import org.slf4j.Logger;

      import org.slf4j.LoggerFactory;

       

       

      import javax.jcr.RepositoryException;

      import java.io.ByteArrayInputStream;

      import java.io.ByteArrayOutputStream;

      import java.io.IOException;

      import java.io.InputStream;

      import java.util.ArrayList;

      import java.util.List;

       

       

      /**

      * Created with IntelliJ IDEA.

      * User: linhui

      * Date: 6/1/12

      * Time: 2:12 PM

      * To change this template use File | Settings | File Templates.

      */

      public class MongoBundlePersistenceManager extends AbstractBundlePersistenceManager {

       

       

          protected DB itsDb;

          protected DBCollection itsNodeCollection;

          /**

           * flag for error handling

           */

          protected ErrorHandling errorHandling = new ErrorHandling();

          protected BundleBinding binding;

          /**

           * the minimum size of a property until it gets written to the blob store

           */

          private int minBlobSize = 0x1000;

       

       

          /** the default logger */

          private static Logger log = LoggerFactory.getLogger(MongoDbPersistanceManager.class);

          protected DBCollection itsRefCollection;

       

       

          @Override

          protected NodePropBundle loadBundle(NodeId id) throws ItemStateException {

       

       

              BasicDBObject query = new BasicDBObject();

              query.put("_id", id.toString());

              BasicDBObject theDBObject = (BasicDBObject) itsNodeCollection.findOne(query);

              if (theDBObject == null) {

                  return null;

              }

       

       

              Binary theD = (Binary) theDBObject.get("d");

              InputStream in = new ByteArrayInputStream(theD.getData());

              try {

                  return binding.readBundle(in, id);

              } catch (IOException e) {

                  String msg = "failed to read bundle: " + id + ": " + e;

                  log.error(msg,e);

                  throw new ItemStateException(msg, e);

              }

          }

       

       

          /** initial size of buffer used to serialize objects */

          protected static final int INITIAL_BUFFER_SIZE = 1024;

          @Override

          protected void storeBundle(NodePropBundle bundle) throws ItemStateException {

              try {

                  DBObject theQuery = new BasicDBObject();

                  theQuery.put("_id", bundle.getId().toString());

       

       

                  DBObject theDbObject = new BasicDBObject();

                  theQuery.put("_id", bundle.getId().toString());

                  ByteArrayOutputStream out = new ByteArrayOutputStream(INITIAL_BUFFER_SIZE);

                  binding.writeBundle(out, bundle);

                  theDbObject.put("d", out.toByteArray());

       

                  itsNodeCollection.update(theQuery, theDbObject, true, false);

              } catch (IOException e) {

                  String msg = "failed to write bundle: " + bundle.getId() + ": " + e;

                  log.error(msg, e);

                  throw new ItemStateException(msg, e);

              }

          }

       

       

          @Override

          protected void destroyBundle(NodePropBundle bundle) throws ItemStateException {

              //todo: delete entire subtree in single query

              DBObject theDBObject = new BasicDBObject();

              theDBObject.put("_id", bundle.getId().toString());

              itsNodeCollection.remove(theDBObject);

          }

       

       

          @Override

          protected void destroy(NodeReferences refs) throws ItemStateException {

              DBObject theDBObject = new BasicDBObject();

              theDBObject.put("_id", refs.getTargetId().toString());

              itsRefCollection.remove(theDBObject);

          }

       

       

          @Override

          protected void store(NodeReferences refs) throws ItemStateException {

       

       

              DBObject theQuery = new BasicDBObject();

              theQuery.put("_id", refs.getTargetId().toString());

       

       

              DBObject theRef = new BasicDBObject();

              theRef.put("_id", refs.getTargetId().toString());

       

       

              ByteArrayOutputStream out = new ByteArrayOutputStream(INITIAL_BUFFER_SIZE);

              // serialize references

              try {

                  Serializer.serialize(refs, out);

                  theRef.put("d", out.toByteArray());

                  itsRefCollection.update(theQuery, theRef, true, false);

              } catch (Exception e) {

                  String msg = "failed to write " + refs;

                  log.error(msg, e);

                  throw new ItemStateException(msg, e);

              }

       

       

       

       

          }

       

       

          @Override

          protected BLOBStore getBlobStore() {

              return null;  //To change body of implemented methods use File | Settings | File Templates.

          }

       

       

          public void init(PMContext inContext) throws Exception {

              super.init(inContext);

              Mongo m = new Mongo("localhost");

              itsDb = m.getDB("productbundledb");

              itsNodeCollection = itsDb.getCollection("node");

              itsRefCollection = itsDb.getCollection("ref");

              // load namespaces

              binding = new BundleBinding(errorHandling, null, getNsIndex(), getNameIndex(), context.getDataStore());

              binding.setMinBlobSize(minBlobSize);

          }

       

       

          public NodeReferences loadReferencesTo(NodeId id) throws NoSuchItemStateException, ItemStateException {

       

       

              BasicDBObject theQuery = new BasicDBObject();

              theQuery.put("_id", id.toString());

       

       

              BasicDBObject theRefDb = (BasicDBObject) itsRefCollection.findOne(theQuery);

              if(theRefDb == null) {

                  throw new NoSuchItemStateException(id.toString());

              }

       

       

              Binary theD = (Binary) theRefDb.get("d");

              InputStream in = new ByteArrayInputStream(theD.getData());

       

       

              NodeReferences theRef = new NodeReferences(id);

              try {

                  Serializer.deserialize(theRef, in);

                  return theRef;

              } catch (Exception e) {

                  if (e instanceof NoSuchItemStateException) {

                      throw (NoSuchItemStateException) e;

                  }

                  String msg = "failed to read references: " + id;

                  log.error(msg, e);

                  throw new ItemStateException(msg, e);

              }

          }

       

       

          public boolean existsReferencesTo(NodeId targetId) throws ItemStateException {

       

       

              BasicDBObject theDBObject = new BasicDBObject();

              theDBObject.put("_id", targetId.toString());

              BasicDBObject theRefDb = (BasicDBObject) itsRefCollection.findOne(theDBObject);

              return theRefDb != null;

          }

       

       

          public Iterable<NodeId> getAllNodeIds(NodeId after, int maxCount) throws ItemStateException, RepositoryException {

              DBObject theSort = new BasicDBObject();

              theSort.put("_id", "1");

       

       

              DBObject theQuery = new BasicDBObject();

              theQuery.put("_id", after.toString());

       

       

              theQuery =        QueryBuilder.start().greaterThan(theQuery).get();

              DBCursor theCursor = itsRefCollection.find(theQuery).sort(theSort).limit(maxCount);

       

       

              ArrayList<NodeId> result = new ArrayList<NodeId>();

       

       

              for (DBObject theDBObject : theCursor) {

                  result.add(NodeId.valueOf(theDBObject.get("_id").toString()));

              }

       

       

              return result;

          }

      }