You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@marmotta.apache.org by ss...@apache.org on 2013/05/15 17:14:26 UTC

git commit: created a MarmottaNotCachedFilter in the same style of the MarmottaLocalFilter to indicate if a resource is not cached by LDCache

Updated Branches:
  refs/heads/develop 289547934 -> 53f8778f4


created a MarmottaNotCachedFilter in the same style of the MarmottaLocalFilter to indicate if a resource is not cached by LDCache


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

Branch: refs/heads/develop
Commit: 53f8778f4efead0408e0adcc437731b2a73945da
Parents: 2895479
Author: Sebastian Schaffert <ss...@apache.org>
Authored: Wed May 15 17:14:17 2013 +0200
Committer: Sebastian Schaffert <ss...@apache.org>
Committed: Wed May 15 17:14:17 2013 +0200

----------------------------------------------------------------------
 .../sparql/persistence/KiWiSparqlConnection.java   |    1 +
 .../marmotta/ldcache/api/LDCachingBackend.java     |    8 ++
 .../ldcache/backend/file/LDCachingFileBackend.java |   14 +++
 .../backend/file/test/LDCacheBackendTest.java      |    6 +
 .../ldcache/backend/kiwi/LDCachingKiWiBackend.java |   21 ++++
 .../backend/kiwi/test/LDCacheBackendTest.java      |    6 +
 .../apache/marmotta/ldcache/services/LDCache.java  |   12 +++
 .../model/filter/MarmottaNotCachedFilter.java      |   75 +++++++++++++++
 8 files changed, 143 insertions(+), 0 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/53f8778f/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/persistence/KiWiSparqlConnection.java
----------------------------------------------------------------------
diff --git a/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/persistence/KiWiSparqlConnection.java b/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/persistence/KiWiSparqlConnection.java
index a192c2c..90618f2 100644
--- a/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/persistence/KiWiSparqlConnection.java
+++ b/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/persistence/KiWiSparqlConnection.java
@@ -311,6 +311,7 @@ public class KiWiSparqlConnection {
             }
         });
 
+        // materialize result to avoid having more than one result set open at the same time
         return new CloseableIteratorIteration<BindingSet, SQLException>(Iterations.asList(it).iterator());
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/53f8778f/libraries/ldcache/ldcache-api/src/main/java/org/apache/marmotta/ldcache/api/LDCachingBackend.java
----------------------------------------------------------------------
diff --git a/libraries/ldcache/ldcache-api/src/main/java/org/apache/marmotta/ldcache/api/LDCachingBackend.java b/libraries/ldcache/ldcache-api/src/main/java/org/apache/marmotta/ldcache/api/LDCachingBackend.java
index 3e9bc50..a121e20 100644
--- a/libraries/ldcache/ldcache-api/src/main/java/org/apache/marmotta/ldcache/api/LDCachingBackend.java
+++ b/libraries/ldcache/ldcache-api/src/main/java/org/apache/marmotta/ldcache/api/LDCachingBackend.java
@@ -60,6 +60,14 @@ public interface LDCachingBackend {
 
 
     /**
+     * Return true in case the resource is a cached resource.
+     *
+     * @param resource the URI of the resource to check
+     * @return true in case the resource is a cached resource
+     */
+    public boolean isCached(String resource)  throws RepositoryException;
+
+    /**
      * Carry out any initialization tasks that might be necessary
      */
     public void initialize();

http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/53f8778f/libraries/ldcache/ldcache-backend-file/src/main/java/org/apache/marmotta/ldcache/backend/file/LDCachingFileBackend.java
----------------------------------------------------------------------
diff --git a/libraries/ldcache/ldcache-backend-file/src/main/java/org/apache/marmotta/ldcache/backend/file/LDCachingFileBackend.java b/libraries/ldcache/ldcache-backend-file/src/main/java/org/apache/marmotta/ldcache/backend/file/LDCachingFileBackend.java
index 937ace5..396b0cd 100644
--- a/libraries/ldcache/ldcache-backend-file/src/main/java/org/apache/marmotta/ldcache/backend/file/LDCachingFileBackend.java
+++ b/libraries/ldcache/ldcache-backend-file/src/main/java/org/apache/marmotta/ldcache/backend/file/LDCachingFileBackend.java
@@ -38,6 +38,7 @@ import org.openrdf.model.ValueFactory;
 import org.openrdf.repository.Repository;
 import org.openrdf.repository.RepositoryException;
 import org.openrdf.repository.sail.SailRepository;
+import org.openrdf.sail.SailException;
 import org.openrdf.sail.nativerdf.NativeStore;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -109,6 +110,19 @@ public class LDCachingFileBackend implements LDCachingBackend {
         };
     }
 
+    /**
+     * Return true in case the resource is a cached resource.
+     *
+     * @param resource the URI of the resource to check
+     * @return true in case the resource is a cached resource
+     */
+    @Override
+    public boolean isCached(String resource) throws RepositoryException {
+        File file = FileBackendUtils.getMetaFile(resource, storageDir);
+        return file.exists();
+    }
+
+
     /* (non-Javadoc)
      * @see org.apache.marmotta.ldcache.api.LDCachingBackend#initialize()
      */

http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/53f8778f/libraries/ldcache/ldcache-backend-file/src/test/java/org/apache/marmotta/ldcache/backend/file/test/LDCacheBackendTest.java
----------------------------------------------------------------------
diff --git a/libraries/ldcache/ldcache-backend-file/src/test/java/org/apache/marmotta/ldcache/backend/file/test/LDCacheBackendTest.java b/libraries/ldcache/ldcache-backend-file/src/test/java/org/apache/marmotta/ldcache/backend/file/test/LDCacheBackendTest.java
index 2061c9f..9afdb4c 100644
--- a/libraries/ldcache/ldcache-backend-file/src/test/java/org/apache/marmotta/ldcache/backend/file/test/LDCacheBackendTest.java
+++ b/libraries/ldcache/ldcache-backend-file/src/test/java/org/apache/marmotta/ldcache/backend/file/test/LDCacheBackendTest.java
@@ -163,6 +163,8 @@ public class LDCacheBackendTest {
             con.addCacheEntry(subject1, entry1);
             con.commit();
 
+            Assert.assertTrue(backend.isCached(subject1.stringValue()));
+            Assert.assertFalse(backend.isCached(subject2.stringValue()));
             Assert.assertEquals(1,asList(backend.listCacheEntries()).size());
             Assert.assertEquals(0,asList(backend.listExpiredEntries()).size());
 
@@ -176,6 +178,8 @@ public class LDCacheBackendTest {
 
             con.commit();
 
+            Assert.assertTrue(backend.isCached(subject1.stringValue()));
+            Assert.assertTrue(backend.isCached(subject2.stringValue()));
             Assert.assertEquals(2,asList(backend.listCacheEntries()).size());
             Assert.assertEquals(1,asList(backend.listExpiredEntries()).size());
 
@@ -183,6 +187,8 @@ public class LDCacheBackendTest {
             con.removeCacheEntry(subject1);
             con.commit();
 
+            Assert.assertFalse(backend.isCached(subject1.stringValue()));
+            Assert.assertTrue(backend.isCached(subject2.stringValue()));
             Assert.assertEquals(1,asList(backend.listCacheEntries()).size());
             Assert.assertEquals(1,asList(backend.listExpiredEntries()).size());
         } catch(RepositoryException ex) {

http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/53f8778f/libraries/ldcache/ldcache-backend-kiwi/src/main/java/org/apache/marmotta/ldcache/backend/kiwi/LDCachingKiWiBackend.java
----------------------------------------------------------------------
diff --git a/libraries/ldcache/ldcache-backend-kiwi/src/main/java/org/apache/marmotta/ldcache/backend/kiwi/LDCachingKiWiBackend.java b/libraries/ldcache/ldcache-backend-kiwi/src/main/java/org/apache/marmotta/ldcache/backend/kiwi/LDCachingKiWiBackend.java
index 3e90e1e..63ba51b 100644
--- a/libraries/ldcache/ldcache-backend-kiwi/src/main/java/org/apache/marmotta/ldcache/backend/kiwi/LDCachingKiWiBackend.java
+++ b/libraries/ldcache/ldcache-backend-kiwi/src/main/java/org/apache/marmotta/ldcache/backend/kiwi/LDCachingKiWiBackend.java
@@ -177,6 +177,27 @@ public class LDCachingKiWiBackend implements LDCachingBackend {
     }
 
 
+    /**
+     * Return true in case the resource is a cached resource.
+     *
+     * @param resource the URI of the resource to check
+     * @return true in case the resource is a cached resource
+     */
+    @Override
+    public boolean isCached(String resource) throws RepositoryException {
+        try {
+            final LDCachingKiWiSailConnection sailConnection = sail.getConnection();
+            sailConnection.begin();
+            boolean result = sailConnection.getCacheEntry(sailConnection.getValueFactory().createURI(resource)) != null;
+            sailConnection.commit();
+            sailConnection.close();
+
+            return result;
+        } catch (SailException e) {
+            throw new RepositoryException(e);
+        }
+    }
+
     public LDCachingKiWiPersistence getPersistence() {
         return persistence;
     }

http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/53f8778f/libraries/ldcache/ldcache-backend-kiwi/src/test/java/org/apache/marmotta/ldcache/backend/kiwi/test/LDCacheBackendTest.java
----------------------------------------------------------------------
diff --git a/libraries/ldcache/ldcache-backend-kiwi/src/test/java/org/apache/marmotta/ldcache/backend/kiwi/test/LDCacheBackendTest.java b/libraries/ldcache/ldcache-backend-kiwi/src/test/java/org/apache/marmotta/ldcache/backend/kiwi/test/LDCacheBackendTest.java
index bfd12c9..f0c6af5 100644
--- a/libraries/ldcache/ldcache-backend-kiwi/src/test/java/org/apache/marmotta/ldcache/backend/kiwi/test/LDCacheBackendTest.java
+++ b/libraries/ldcache/ldcache-backend-kiwi/src/test/java/org/apache/marmotta/ldcache/backend/kiwi/test/LDCacheBackendTest.java
@@ -252,6 +252,8 @@ public class LDCacheBackendTest {
             con.addCacheEntry(subject1, entry1);
             con.commit();
 
+            Assert.assertTrue(backend.isCached(subject1.stringValue()));
+            Assert.assertFalse(backend.isCached(subject2.stringValue()));
             Assert.assertEquals(1,asList(backend.listCacheEntries()).size());
             Assert.assertEquals(0,asList(backend.listExpiredEntries()).size());
 
@@ -265,6 +267,8 @@ public class LDCacheBackendTest {
 
             con.commit();
 
+            Assert.assertTrue(backend.isCached(subject1.stringValue()));
+            Assert.assertTrue(backend.isCached(subject2.stringValue()));
             Assert.assertEquals(2,asList(backend.listCacheEntries()).size());
             Assert.assertEquals(1,asList(backend.listExpiredEntries()).size());
 
@@ -272,6 +276,8 @@ public class LDCacheBackendTest {
             con.removeCacheEntry(subject1);
             con.commit();
 
+            Assert.assertFalse(backend.isCached(subject1.stringValue()));
+            Assert.assertTrue(backend.isCached(subject2.stringValue()));
             Assert.assertEquals(1,asList(backend.listCacheEntries()).size());
             Assert.assertEquals(1,asList(backend.listExpiredEntries()).size());
         } catch(RepositoryException ex) {

http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/53f8778f/libraries/ldcache/ldcache-core/src/main/java/org/apache/marmotta/ldcache/services/LDCache.java
----------------------------------------------------------------------
diff --git a/libraries/ldcache/ldcache-core/src/main/java/org/apache/marmotta/ldcache/services/LDCache.java b/libraries/ldcache/ldcache-core/src/main/java/org/apache/marmotta/ldcache/services/LDCache.java
index 89ee13b..c137f2a 100644
--- a/libraries/ldcache/ldcache-core/src/main/java/org/apache/marmotta/ldcache/services/LDCache.java
+++ b/libraries/ldcache/ldcache-core/src/main/java/org/apache/marmotta/ldcache/services/LDCache.java
@@ -127,6 +127,18 @@ public class LDCache implements LDCachingService {
         return backend.listExpiredEntries();
     }
 
+
+    /**
+     * Return true if the resource is a cached resource.
+     *
+     * @param resourceUri
+     * @return
+     * @throws RepositoryException
+     */
+    public boolean isCached(String resourceUri) throws RepositoryException {
+        return backend.isCached(resourceUri);
+    }
+
     /**
      * Manually expire the caching information for the given resource. The resource will be
      * re-retrieved upon the next access.

http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/53f8778f/platform/marmotta-ldcache/src/main/java/org/apache/marmotta/platform/ldcache/model/filter/MarmottaNotCachedFilter.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-ldcache/src/main/java/org/apache/marmotta/platform/ldcache/model/filter/MarmottaNotCachedFilter.java b/platform/marmotta-ldcache/src/main/java/org/apache/marmotta/platform/ldcache/model/filter/MarmottaNotCachedFilter.java
new file mode 100644
index 0000000..d39fa4c
--- /dev/null
+++ b/platform/marmotta-ldcache/src/main/java/org/apache/marmotta/platform/ldcache/model/filter/MarmottaNotCachedFilter.java
@@ -0,0 +1,75 @@
+/**
+ * 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.platform.ldcache.model.filter;
+
+import org.apache.marmotta.commons.sesame.filter.resource.ResourceFilter;
+import org.apache.marmotta.platform.core.util.CDIContext;
+import org.apache.marmotta.platform.ldcache.services.ldcache.LDCacheSailProvider;
+import org.openrdf.model.BNode;
+import org.openrdf.model.Resource;
+import org.openrdf.model.URI;
+import org.openrdf.repository.RepositoryException;
+
+/**
+ * Accept only resources that are considered "not cached", i.e. do not have an entry in the caching table.
+ * <p/>
+ * Author: Sebastian Schaffert
+ */
+public class MarmottaNotCachedFilter implements ResourceFilter {
+
+    private LDCacheSailProvider cacheSailProvider;
+
+    public MarmottaNotCachedFilter() {
+        cacheSailProvider = CDIContext.getInstance(LDCacheSailProvider.class);
+    }
+
+
+    private static MarmottaNotCachedFilter instance = null;
+
+    public static MarmottaNotCachedFilter getInstance() {
+        if(instance == null) {
+            instance = new MarmottaNotCachedFilter();
+        }
+        return instance;
+    }
+
+
+    /**
+     * Return false in case the filter does not accept the resource passed as argument, true otherwise.
+     *
+     *
+     * @param resource
+     * @return
+     */
+    @Override
+    public boolean accept(Resource resource) {
+        if(resource instanceof BNode) {
+            return true;
+        }
+
+        URI uri = (URI)resource;
+
+        try {
+            return !cacheSailProvider.getLDCache().isCached(uri.stringValue());
+        } catch (RepositoryException e) {
+            return false;
+        }
+
+    }
+
+}