You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by me...@apache.org on 2013/02/13 14:46:27 UTC
svn commit: r1445595 - in /jackrabbit/oak/trunk/oak-mongomk/src:
main/java/org/apache/jackrabbit/mongomk/prototype/
test/java/org/apache/jackrabbit/mongomk/prototype/
Author: meteatamel
Date: Wed Feb 13 13:46:27 2013
New Revision: 1445595
URL: http://svn.apache.org/r1445595
Log:
OAK-619 - Lock-free MongoMK implementation
Added initial mongo based store along with the test case
Added:
jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/prototype/MongoDocumentStore.java (with props)
jackrabbit/oak/trunk/oak-mongomk/src/test/java/org/apache/jackrabbit/mongomk/prototype/
jackrabbit/oak/trunk/oak-mongomk/src/test/java/org/apache/jackrabbit/mongomk/prototype/MongoDocumentStoreTest.java (with props)
Added: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/prototype/MongoDocumentStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/prototype/MongoDocumentStore.java?rev=1445595&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/prototype/MongoDocumentStore.java (added)
+++ jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/prototype/MongoDocumentStore.java Wed Feb 13 13:46:27 2013
@@ -0,0 +1,133 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.mongomk.prototype;
+
+import java.util.Map;
+import java.util.Map.Entry;
+
+import org.apache.jackrabbit.mongomk.prototype.UpdateOp.Operation;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.mongodb.BasicDBObject;
+import com.mongodb.DB;
+import com.mongodb.DBCollection;
+import com.mongodb.DBObject;
+import com.mongodb.QueryBuilder;
+import com.mongodb.WriteConcern;
+import com.mongodb.WriteResult;
+
+public class MongoDocumentStore implements DocumentStore {
+
+ private static final String KEY_PATH = "_path";
+ private static final Logger LOG = LoggerFactory.getLogger(MongoDocumentStore.class);
+
+ private final DBCollection nodesCollection;
+
+ public MongoDocumentStore(DB db) {
+ nodesCollection = db.getCollection(Collection.NODES.toString());
+ }
+
+ @Override
+ public Map<String, Object> find(Collection collection, String path) {
+ DBObject n = getNode(collection, path);
+ if (n == null) {
+ return null;
+ }
+ return convertFromDBObject(n);
+ }
+
+ @Override
+ public void remove(Collection collection, String path) {
+ DBCollection dbCollection = getDBCollection(collection);
+ WriteResult writeResult = dbCollection.remove(getByPathQuery(path), WriteConcern.SAFE);
+ if (writeResult.getError() != null) {
+ LOG.error("Remove failed: {}", writeResult.getError());
+ }
+ }
+
+ @Override
+ public Map<String, Object> createOrUpdate(Collection collection, UpdateOp updateOp) {
+ DBCollection dbCollection = getDBCollection(collection);
+
+ BasicDBObject setUpdates = new BasicDBObject();
+ BasicDBObject incUpdates = new BasicDBObject();
+
+ for (Entry<String, Operation> entry : updateOp.changes.entrySet()) {
+ String k = entry.getKey();
+ Operation op = entry.getValue();
+ switch (op.type) {
+ case SET: {
+ setUpdates.append(k, op.value);
+ break;
+ }
+ case INCREMENT: {
+ incUpdates.append(k, (Long)op.value);
+ break;
+ }
+ case ADD_MAP_ENTRY: {
+ setUpdates.append(k + "." + op.subKey.toString(), op.value.toString());
+ break;
+ }
+ case REMOVE_MAP_ENTRY: {
+ // TODO
+ break;
+ }
+ }
+ }
+
+ DBObject query = getByPathQuery(updateOp.key);
+ BasicDBObject update = new BasicDBObject();
+ if (!setUpdates.isEmpty()) {
+ update.append("$set", setUpdates);
+ }
+ if (!incUpdates.isEmpty()) {
+ update.append("$inc", incUpdates);
+ }
+
+ DBObject oldNode = dbCollection.findAndModify(query, null /*fields*/, null /*sort*/, false /*remove*/, update, false /*returnNew*/, true /*upsert*/);
+ return convertFromDBObject(oldNode);
+ }
+
+ private Map<String, Object> convertFromDBObject(DBObject n) {
+ Map<String, Object> copy = Utils.newMap();
+ synchronized (n) {
+ for (String key : n.keySet()) {
+ copy.put(key, n.get(key));
+ }
+ return copy;
+ }
+ }
+
+ private DBCollection getDBCollection(Collection collection) {
+ switch (collection) {
+ case NODES:
+ return nodesCollection;
+ default:
+ throw new IllegalArgumentException(collection.name());
+ }
+ }
+
+ private DBObject getNode(Collection collection, String path) {
+ DBCollection dbCollection = getDBCollection(collection);
+ return dbCollection.findOne(getByPathQuery(path));
+ }
+
+ private DBObject getByPathQuery(String path) {
+ return QueryBuilder.start(KEY_PATH).is(path).get();
+ }
+}
\ No newline at end of file
Propchange: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/prototype/MongoDocumentStore.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: jackrabbit/oak/trunk/oak-mongomk/src/test/java/org/apache/jackrabbit/mongomk/prototype/MongoDocumentStoreTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mongomk/src/test/java/org/apache/jackrabbit/mongomk/prototype/MongoDocumentStoreTest.java?rev=1445595&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-mongomk/src/test/java/org/apache/jackrabbit/mongomk/prototype/MongoDocumentStoreTest.java (added)
+++ jackrabbit/oak/trunk/oak-mongomk/src/test/java/org/apache/jackrabbit/mongomk/prototype/MongoDocumentStoreTest.java Wed Feb 13 13:46:27 2013
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.mongomk.prototype;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Map;
+
+import org.apache.jackrabbit.mongomk.AbstractMongoConnectionTest;
+import org.apache.jackrabbit.mongomk.prototype.DocumentStore.Collection;
+import org.junit.Test;
+
+public class MongoDocumentStoreTest extends AbstractMongoConnectionTest {
+
+ @Test
+ public void addGetAndRemove() throws Exception {
+ DocumentStore docStore = new MongoDocumentStore(mongoConnection.getDB());
+
+ UpdateOp updateOp = new UpdateOp("/");
+ updateOp.addMapEntry("property1", "key1", "value1");
+ updateOp.increment("property2", 1);
+ updateOp.set("property3", "value3");
+ docStore.createOrUpdate(Collection.NODES, updateOp);
+ Map<String, Object> obj = docStore.find(Collection.NODES, "/");
+
+ Map property1 = (Map)obj.get("property1");
+ String value1 = (String)property1.get("key1");
+ assertEquals("value1", value1);
+
+ Long value2 = (Long)obj.get("property2");
+ assertEquals(Long.valueOf(1), value2);
+
+ String value3 = (String)obj.get("property3");
+ assertEquals("value3", value3);
+
+ docStore.remove(Collection.NODES, "/");
+ obj = docStore.find(Collection.NODES, "/");
+ assertTrue(obj == null);
+ }
+}
\ No newline at end of file
Propchange: jackrabbit/oak/trunk/oak-mongomk/src/test/java/org/apache/jackrabbit/mongomk/prototype/MongoDocumentStoreTest.java
------------------------------------------------------------------------------
svn:eol-style = native