add support for a local in-memory triple registry

Project: http://git-wip-us.apache.org/repos/asf/marmotta/repo
Commit: http://git-wip-us.apache.org/repos/asf/marmotta/commit/471edcbc
Tree: http://git-wip-us.apache.org/repos/asf/marmotta/tree/471edcbc
Diff: http://git-wip-us.apache.org/repos/asf/marmotta/diff/471edcbc

Branch: refs/heads/master
Commit: 471edcbc6ce75fbe6edbeedf71aca0e3774aa67a
Parents: ff1c4a1
Author: Sebastian Schaffert <[email protected]>
Authored: Fri Mar 28 15:29:38 2014 +0100
Committer: Sebastian Schaffert <[email protected]>
Committed: Fri Mar 28 15:29:38 2014 +0100

----------------------------------------------------------------------
 .../marmotta/kiwi/config/RegistryStrategy.java  |   8 +-
 .../registry/LocalTripleRegistry.java           | 106 +++++++++++++++++++
 .../marmotta/kiwi/sail/KiWiValueFactory.java    |   4 +
 3 files changed, 117 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/marmotta/blob/471edcbc/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/config/RegistryStrategy.java
----------------------------------------------------------------------
diff --git 
a/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/config/RegistryStrategy.java
 
b/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/config/RegistryStrategy.java
index 841d1cf..30caa2f 100644
--- 
a/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/config/RegistryStrategy.java
+++ 
b/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/config/RegistryStrategy.java
@@ -34,6 +34,12 @@ public enum RegistryStrategy {
      * Use a synchronized replicated infinispan cache to synchronize between 
parallel instances. Faster but requires
      * more memory.
      */
-    CACHE
+    CACHE,
+
+    /**
+     * Use a local in-memory hash map to synchronize between parallel 
instances. Does not synchronize across machines
+     * in a cluster
+     */
+    LOCAL
 
 }

http://git-wip-us.apache.org/repos/asf/marmotta/blob/471edcbc/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/persistence/registry/LocalTripleRegistry.java
----------------------------------------------------------------------
diff --git 
a/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/persistence/registry/LocalTripleRegistry.java
 
b/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/persistence/registry/LocalTripleRegistry.java
new file mode 100644
index 0000000..762a179
--- /dev/null
+++ 
b/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/persistence/registry/LocalTripleRegistry.java
@@ -0,0 +1,106 @@
+/*
+ * 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.marmotta.kiwi.persistence.registry;
+
+import org.apache.marmotta.commons.sesame.tripletable.IntArray;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * Add file description here!
+ *
+ * @author Sebastian Schaffert ([email protected])
+ */
+public class LocalTripleRegistry implements KiWiTripleRegistry {
+
+    private Map<Long,Long> cache;
+
+
+    private Map<Long,List<Long>>  transactions;
+
+
+    public LocalTripleRegistry() {
+        cache        = new ConcurrentHashMap<>();
+        transactions = new ConcurrentHashMap<>();
+
+    }
+
+
+    /**
+     * Register a key/triple id pair in the triple registry for the given 
transaction ID.
+     *
+     * @param key           the key identifying the triple arguments (subject, 
object, predicate, context)
+     * @param transactionId the identifier of the transaction registering the 
triple id
+     * @param tripleId      the new triple identifier
+     */
+    @Override
+    public void registerKey(IntArray key, long transactionId, long tripleId) {
+        List<Long> transaction = transactions.get(transactionId);
+        if(transaction == null) {
+            transaction = new ArrayList<>();
+            transactions.put(transactionId, transaction);
+        }
+        cache.put(key.longHashCode(), tripleId);
+        transaction.add(key.longHashCode());
+    }
+
+    /**
+     * Check if another (or the same) transaction has already registered an ID 
for the triple with the
+     * given key. Returns -1 in case no other ID has been registered, or a 
value >0 otherwise.
+     *
+     * @param key the key identifying the triple arguments (subject, object, 
predicate, context)
+     * @return id of the triple or -1
+     */
+    @Override
+    public long lookupKey(IntArray key) {
+        Long value = cache.get(key.longHashCode());
+        if(value != null) {
+            return value;
+        } else {
+            return -1;
+        }
+    }
+
+    /**
+     * Free all registry entries claimed by the transaction with the given 
identifier. Should remove or
+     * expire all registry entries to avoid unnecessary storage consumption.
+     *
+     * @param transactionId the identifier of the transaction registering the 
triple id
+     */
+    @Override
+    public void releaseTransaction(long transactionId) {
+        if(transactions.containsKey(transactionId)) {
+            for(long key : transactions.remove(transactionId)) {
+                cache.remove(key);
+            }
+        }
+    }
+
+    /**
+     * Remove the key with the given key, e.g. when a statement is again 
deleted during a transaction.
+     *
+     * @param key the key identifying the triple arguments (subject, object, 
predicate, context)
+     */
+    @Override
+    public void deleteKey(IntArray key) {
+        cache.remove(key.longHashCode());
+    }
+}

http://git-wip-us.apache.org/repos/asf/marmotta/blob/471edcbc/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/sail/KiWiValueFactory.java
----------------------------------------------------------------------
diff --git 
a/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/sail/KiWiValueFactory.java
 
b/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/sail/KiWiValueFactory.java
index f248dfb..2502ea1 100644
--- 
a/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/sail/KiWiValueFactory.java
+++ 
b/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/sail/KiWiValueFactory.java
@@ -26,6 +26,7 @@ import org.apache.marmotta.kiwi.persistence.KiWiConnection;
 import org.apache.marmotta.kiwi.persistence.registry.CacheTripleRegistry;
 import org.apache.marmotta.kiwi.persistence.registry.DBTripleRegistry;
 import org.apache.marmotta.kiwi.persistence.registry.KiWiTripleRegistry;
+import org.apache.marmotta.kiwi.persistence.registry.LocalTripleRegistry;
 import org.openrdf.model.*;
 import org.openrdf.model.impl.ContextStatementImpl;
 import org.slf4j.Logger;
@@ -66,6 +67,9 @@ public class KiWiValueFactory implements ValueFactory {
             case CACHE:
                 registry        = new 
CacheTripleRegistry(store.getPersistence().getCacheManager());
                 break;
+            case LOCAL:
+                registry        = new LocalTripleRegistry();
+                break;
             default:
                 registry        = new 
CacheTripleRegistry(store.getPersistence().getCacheManager());
         }

Reply via email to