Package org.apache.marmotta.ldcache.api

Examples of org.apache.marmotta.ldcache.api.LDCachingConnection


        if (accept(subj)) {
            log.debug("Refreshing resource: {}", subj.stringValue());
            ldcache.refreshResource((URI) subj, false);

            try {
                final LDCachingConnection cachingConnection = ldcache.getCacheConnection(subj.stringValue());
                // join the results of the cache connection and the wrapped connection in a single result
                return new UnionIteration<Statement, SailException>(
                        new ExceptionConvertingIteration<Statement, SailException>(cachingConnection.getStatements(subj,pred,obj,includeInferred, contexts)) {
                            @Override
                            protected SailException convert(Exception e) {
                                return new SailException("error while accessing cache connection",e);
                            }

                            @Override
                            protected void handleClose() throws SailException {
                                super.handleClose();

                                try {
                                    cachingConnection.close();
                                } catch (RepositoryException e) {
                                    throw new SailException("error while closing cache connection",e);
                                }
                            }
                        },
View Full Code Here


    @Override
    public void expire(URI resource) {
        Date now = new Date();

        try {
            LDCachingConnection con = backend.getCacheConnection(resource.stringValue());
            try {
                con.begin();

                CacheEntry entry = con.getCacheEntry(resource);
                if(entry.getExpiryDate().getTime() > now.getTime()) {
                    entry.setExpiryDate(now);

                    con.removeCacheEntry(entry.getResource());
                    con.addCacheEntry(entry.getResource(),entry);
                }

                con.commit();
            } catch(RepositoryException ex) {
                con.rollback();
            } finally {
                con.close();
            }
        } catch(RepositoryException ex) {
            ex.printStackTrace(); // TODO: handle error
        }
View Full Code Here

     */
    @Override
    public void refreshResource(URI resource, boolean forceRefresh) {
        resourceLocks.lock(resource.stringValue());
        try {
            LDCachingConnection cacheConnection = backend.getCacheConnection(resource.stringValue());
            CacheEntry entry = null;
            try {
                cacheConnection.begin();

                // 2. check whether the resource has a cache entry; if no, goto 4
                entry = cacheConnection.getCacheEntry(resource);

                // commit/close the connection, the retrieveResource method takes too long to hold the DB connection open
                cacheConnection.commit();

                // 3. check whether the expiry time of the cache entry has passed; if no, returns immediately
                if(!forceRefresh && entry != null && entry.getExpiryDate().after(new Date())) {
                    log.debug("not refreshing resource {}, as the cached entry is not yet expired",resource);
                    return;
                }
            } catch(RepositoryException ex) {
                cacheConnection.rollback();
            } finally {
                cacheConnection.close();
            }

            // 4.
            log.debug("refreshing resource {}",resource);
            this.lock.readLock().lock();
            try {
                ClientResponse response = ldclient.retrieveResource(resource.stringValue());

                if(response != null) {
                    log.info("refreshed resource {}",resource);

                    // obtain a new cache connection, since we closed the original connection above
                    LDCachingConnection cacheConnection1 = backend.getCacheConnection(resource.stringValue());
                    cacheConnection1.begin();
                    try {
                        URI subject = cacheConnection1.getValueFactory().createURI(resource.stringValue());

                        RepositoryConnection respConnection = response.getTriples().getConnection();

                        cacheConnection1.remove(subject, null, null);

                        int count = 0;
                        RepositoryResult<Statement> triples = respConnection.getStatements(null,null,null,true);
                        while(triples.hasNext()) {
                            Statement triple = triples.next();
                            try {
                                cacheConnection1.add(triple);
                            } catch (RuntimeException ex) {
                                log.warn("not adding triple {}: an exception occurred ({})",triple,ex.getMessage());
                            }
                            count++;
                        }
                        triples.close();
                        respConnection.close();

                        CacheEntry newEntry = new CacheEntry();
                        newEntry.setResource(subject);
                        newEntry.setExpiryDate(response.getExpires());
                        newEntry.setLastRetrieved(new Date());
                        if(entry != null) {
                            newEntry.setUpdateCount(entry.getUpdateCount()+1);
                        } else {
                            newEntry.setUpdateCount(1);
                        }
                        newEntry.setTripleCount(count);

                        cacheConnection1.removeCacheEntry(resource);
                        cacheConnection1.addCacheEntry(resource, newEntry);
                        cacheConnection1.commit();
                    } catch (RepositoryException e) {
                        log.error("repository error while refreshing the remote resource {} from the Linked Data Cloud", resource, e);
                        cacheConnection1.rollback();
                    } finally {
                        cacheConnection1.close();
                    }
                }

            } catch (DataRetrievalException e) {
                // on exception, save an expiry information and retry in one day
                CacheEntry newEntry = new CacheEntry();
                newEntry.setResource(cacheConnection.getValueFactory().createURI(resource.stringValue()));
                newEntry.setExpiryDate(new Date(System.currentTimeMillis() + config.getDefaultExpiry()*1000));
                newEntry.setLastRetrieved(new Date());
                if(entry != null) {
                    newEntry.setUpdateCount(entry.getUpdateCount()+1);
                } else {
                    newEntry.setUpdateCount(1);
                }
                newEntry.setTripleCount(0);

                LDCachingConnection cacheConnection2 = backend.getCacheConnection(resource.stringValue());
                cacheConnection2.begin();
                try {
                    cacheConnection2.removeCacheEntry(resource);
                    cacheConnection2.addCacheEntry(resource, newEntry);

                    cacheConnection2.commit();
                    log.error("refreshing the remote resource {} from the Linked Data Cloud failed ({})",resource,e.getMessage());
                    //log.info("exception was:",e);
                    return;
                } catch (RepositoryException ex) {
                    log.error("repository error while refreshing the remote resource {} from the Linked Data Cloud", resource, ex);
                    cacheConnection2.rollback();
                } finally {
                    cacheConnection2.close();
                }
            } finally {
                this.lock.readLock().unlock();
            }
        } catch (RepositoryException e) {
View Full Code Here

                    if(next.getExpiryDate().getTime() > now.getTime()) {
                        next.setExpiryDate(now);

                        try {
                            LDCachingConnection con = backend.getCacheConnection(next.getResource().stringValue());
                            try {
                                con.begin();

                                con.removeCacheEntry(next.getResource());
                                con.addCacheEntry(next.getResource(), next);

                                con.commit();
                            } catch(RepositoryException ex) {
                                con.rollback();
                            } finally {
                                con.close();
                            }
                        } catch(RepositoryException ex) {
                        }
                    }
                }
View Full Code Here

     *
     * @throws Exception
     */
    @Test
    public void testStoreEntries() throws Exception {
        LDCachingConnection con = backend.getCacheConnection("http://localhost/resource/1");
        try {
            con.begin();

            URI subject1     = con.getValueFactory().createURI("http://localhost/resource/"+ RandomStringUtils.randomAlphanumeric(8));
            URI subject2     = con.getValueFactory().createURI("http://localhost/resource/"+ RandomStringUtils.randomAlphanumeric(8));
            Literal object1  = con.getValueFactory().createLiteral(RandomStringUtils.random(64));
            Literal object2  = con.getValueFactory().createLiteral(RandomStringUtils.random(64));
            URI predicate    = con.getValueFactory().createURI("http://localhost/resource/"+ RandomStringUtils.randomAlphanumeric(8));

            Statement stmt1 = con.getValueFactory().createStatement(subject1,predicate,object1);
            Statement stmt2 = con.getValueFactory().createStatement(subject2,predicate,object2);

            con.add(stmt1);
            con.add(stmt2);

            con.commit();

            con.begin();
            CacheEntry entry1 = new CacheEntry();
            entry1.setExpiryDate(new Date(System.currentTimeMillis()+1000*60));
            entry1.setLastRetrieved(new Date());
            entry1.setUpdateCount(1);
            entry1.setResource(subject1);
            entry1.setTripleCount(1);
            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());

            con.begin();
            CacheEntry entry2 = new CacheEntry();
            entry2.setExpiryDate(new Date(System.currentTimeMillis() - 1000 * 60));
            entry2.setLastRetrieved(new Date());
            entry2.setUpdateCount(1);
            entry2.setResource(subject2);
            entry2.setTripleCount(1);
            con.addCacheEntry(subject2,entry2);

            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());

            con.begin();
            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) {
            con.rollback();
        } finally {
            con.close();
        }
    }
View Full Code Here

     *
     * @throws Exception
     */
    @Test
    public void testStoreEntries() throws Exception {
        LDCachingConnection con = backend.getCacheConnection("http://localhost/resource/1");
        try {
            con.begin();

            URI subject1     = con.getValueFactory().createURI("http://localhost/resource/"+ RandomStringUtils.randomAlphanumeric(8));
            URI subject2     = con.getValueFactory().createURI("http://localhost/resource/"+ RandomStringUtils.randomAlphanumeric(8));
            Literal object1  = con.getValueFactory().createLiteral(RandomStringUtils.random(64));
            Literal object2  = con.getValueFactory().createLiteral(RandomStringUtils.random(64));
            URI predicate    = con.getValueFactory().createURI("http://localhost/resource/"+ RandomStringUtils.randomAlphanumeric(8));

            Statement stmt1 = con.getValueFactory().createStatement(subject1,predicate,object1);
            Statement stmt2 = con.getValueFactory().createStatement(subject2,predicate,object2);

            con.add(stmt1);
            con.add(stmt2);

            con.commit();

            con.begin();
            CacheEntry entry1 = new CacheEntry();
            entry1.setExpiryDate(new Date(System.currentTimeMillis()+1000*60));
            entry1.setLastRetrieved(new Date());
            entry1.setUpdateCount(1);
            entry1.setResource(subject1);
            entry1.setTripleCount(1);
            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());

            con.begin();
            CacheEntry entry2 = new CacheEntry();
            entry2.setExpiryDate(new Date(System.currentTimeMillis() - 1000 * 60));
            entry2.setLastRetrieved(new Date());
            entry2.setUpdateCount(1);
            entry2.setResource(subject2);
            entry2.setTripleCount(1);
            con.addCacheEntry(subject2,entry2);

            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());

            con.begin();
            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) {
            con.rollback();
        } finally {
            con.close();
        }
    }
View Full Code Here

     *
     * @throws Exception
     */
    @Test
    public void testStoreEntries() throws Exception {
        LDCachingConnection con = backend.getCacheConnection("http://localhost/resource/1");
        try {
            con.begin();

            URI subject1     = con.getValueFactory().createURI("http://localhost/resource/"+ RandomStringUtils.randomAlphanumeric(8));
            URI subject2     = con.getValueFactory().createURI("http://localhost/resource/"+ RandomStringUtils.randomAlphanumeric(8));
            Literal object1  = con.getValueFactory().createLiteral(RandomStringUtils.random(64));
            Literal object2  = con.getValueFactory().createLiteral(RandomStringUtils.random(64));
            URI predicate    = con.getValueFactory().createURI("http://localhost/resource/"+ RandomStringUtils.randomAlphanumeric(8));

            Statement stmt1 = con.getValueFactory().createStatement(subject1,predicate,object1);
            Statement stmt2 = con.getValueFactory().createStatement(subject2,predicate,object2);

            con.add(stmt1);
            con.add(stmt2);

            con.commit();

            con.begin();
            CacheEntry entry1 = new CacheEntry();
            entry1.setExpiryDate(new Date(System.currentTimeMillis()+1000*60));
            entry1.setLastRetrieved(new Date());
            entry1.setUpdateCount(1);
            entry1.setResource(subject1);
            con.addCacheEntry(subject1, entry1);
            con.commit();

            Assert.assertEquals(1,asList(backend.listCacheEntries()).size());
            Assert.assertEquals(0,asList(backend.listExpiredEntries()).size());

            con.begin();
            CacheEntry entry2 = new CacheEntry();
            entry2.setExpiryDate(new Date(System.currentTimeMillis() - 1000 * 60));
            entry2.setLastRetrieved(new Date());
            entry2.setUpdateCount(1);
            entry2.setResource(subject2);
            con.addCacheEntry(subject2,entry2);

            con.commit();

            Assert.assertEquals(2,asList(backend.listCacheEntries()).size());
            Assert.assertEquals(1,asList(backend.listExpiredEntries()).size());

            con.begin();
            con.removeCacheEntry(subject1);
            con.commit();

            Assert.assertEquals(1,asList(backend.listCacheEntries()).size());
            Assert.assertEquals(1,asList(backend.listExpiredEntries()).size());
        } catch(RepositoryException ex) {
            con.rollback();
        } finally {
            con.close();
        }
    }
View Full Code Here

    @Override
    public void expire(URI resource) {
        Date now = new Date();

        try {
            LDCachingConnection con = backend.getCacheConnection(resource.stringValue());
            try {
                con.begin();

                CacheEntry entry = con.getCacheEntry(resource);
                if(entry.getExpiryDate().getTime() > now.getTime()) {
                    entry.setExpiryDate(now);

                    con.removeCacheEntry(entry.getResource());
                    con.addCacheEntry(entry.getResource(),entry);
                }

                con.commit();
            } catch(RepositoryException ex) {
                con.rollback();
            } finally {
                con.close();
            }
        } catch(RepositoryException ex) {
            ex.printStackTrace(); // TODO: handle error
        }
View Full Code Here

     */
    @Override
    public void refreshResource(URI resource, boolean forceRefresh) {
        final ReentrantLock lock = lockResource(resource);
        try {
            LDCachingConnection cacheConnection = backend.getCacheConnection(resource.stringValue());
            CacheEntry entry = null;
            try {
                cacheConnection.begin();

                // 2. check whether the resource has a cache entry; if no, goto 4
                entry = cacheConnection.getCacheEntry(resource);

                // commit/close the connection, the retrieveResource method takes too long to hold the DB connection open
                cacheConnection.commit();

                // 3. check whether the expiry time of the cache entry has passed; if no, returns immediately
                if(!forceRefresh && entry != null && entry.getExpiryDate().after(new Date())) {
                    log.debug("not refreshing resource {}, as the cached entry is not yet expired",resource);
                    return;
                }
            } catch(RepositoryException ex) {
                cacheConnection.rollback();
            } finally {
                cacheConnection.close();
            }

            // 4.
            log.debug("refreshing resource {}",resource);
            this.lock.readLock().lock();
            try {
                ClientResponse response = ldclient.retrieveResource(resource.stringValue());

                if(response != null) {
                    log.info("refreshed resource {}",resource);

                    // obtain a new cache connection, since we closed the original connection above
                    LDCachingConnection cacheConnection1 = backend.getCacheConnection(resource.stringValue());
                    cacheConnection1.begin();
                    try {
                        URI subject = cacheConnection1.getValueFactory().createURI(resource.stringValue());

                        RepositoryConnection respConnection = response.getTriples().getConnection();

                        cacheConnection1.remove(subject, null, null);


                        RepositoryResult<Statement> triples = respConnection.getStatements(null,null,null,true);
                        while(triples.hasNext()) {
                            Statement triple = triples.next();
                            try {
                                cacheConnection1.add(triple);
                            } catch (RuntimeException ex) {
                                log.warn("not adding triple {}: an exception occurred ({})",triple,ex.getMessage());
                            }
                        }
                        triples.close();
                        respConnection.close();

                        CacheEntry newEntry = new CacheEntry();
                        newEntry.setResource(subject);
                        newEntry.setExpiryDate(response.getExpires());
                        newEntry.setLastRetrieved(new Date());
                        if(entry != null) {
                            newEntry.setUpdateCount(entry.getUpdateCount()+1);
                        } else {
                            newEntry.setUpdateCount(1);
                        }

                        cacheConnection1.removeCacheEntry(resource);
                        cacheConnection1.addCacheEntry(resource, newEntry);
                        cacheConnection1.commit();
                    } catch (RepositoryException e) {
                        log.error("repository error while refreshing the remote resource {} from the Linked Data Cloud", resource, e);
                        cacheConnection1.rollback();
                    } finally {
                        cacheConnection1.close();
                    }
                }

            } catch (DataRetrievalException e) {
                // on exception, save an expiry information and retry in one day
                CacheEntry newEntry = new CacheEntry();
                newEntry.setResource(cacheConnection.getValueFactory().createURI(resource.stringValue()));
                newEntry.setExpiryDate(new Date(System.currentTimeMillis() + config.getDefaultExpiry()*1000));
                newEntry.setLastRetrieved(new Date());
                if(entry != null) {
                    newEntry.setUpdateCount(entry.getUpdateCount()+1);
                } else {
                    newEntry.setUpdateCount(1);
                }

                LDCachingConnection cacheConnection2 = backend.getCacheConnection(resource.stringValue());
                cacheConnection2.begin();
                try {
                    cacheConnection2.removeCacheEntry(resource);
                    cacheConnection2.addCacheEntry(resource, newEntry);

                    cacheConnection2.commit();
                    log.error("refreshing the remote resource {} from the Linked Data Cloud failed ({})",resource,e.getMessage());
                    //log.info("exception was:",e);
                    return;
                } catch (RepositoryException ex) {
                    log.error("repository error while refreshing the remote resource {} from the Linked Data Cloud", resource, ex);
                    cacheConnection2.rollback();
                } finally {
                    cacheConnection2.close();
                }
            } finally {
                this.lock.readLock().unlock();
            }
        } catch (RepositoryException e) {
View Full Code Here

                    if(next.getExpiryDate().getTime() > now.getTime()) {
                        next.setExpiryDate(now);

                        try {
                            LDCachingConnection con = backend.getCacheConnection(next.getResource().stringValue());
                            try {
                                con.begin();

                                con.removeCacheEntry(next.getResource());
                                con.addCacheEntry(next.getResource(), next);

                                con.commit();
                            } catch(RepositoryException ex) {
                                con.rollback();
                            } finally {
                                con.close();
                            }
                        } catch(RepositoryException ex) {
                        }
                    }
                }
View Full Code Here

TOP

Related Classes of org.apache.marmotta.ldcache.api.LDCachingConnection

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.