From ee6f41f36f3a53d85dcd142bf97707f4d5335b92 Mon Sep 17 00:00:00 2001 From: lsitu Date: Tue, 12 Aug 2014 10:59:11 -0700 Subject: [PATCH] Persist jcr/xml in hierarchy structure, corrected codes as suggested. --- .../java/org/fcrepo/indexer/IndexerGroup.java | 41 ++++++++++++------- .../persistence/JcrXmlPersistenceIndexer.java | 25 +++++++---- .../JcrXmlPersistenceIndexerTest.java | 21 ++++++++++ 3 files changed, 64 insertions(+), 23 deletions(-) diff --git a/fcrepo-jms-indexer-core/src/main/java/org/fcrepo/indexer/IndexerGroup.java b/fcrepo-jms-indexer-core/src/main/java/org/fcrepo/indexer/IndexerGroup.java index 7313615..e370d76 100644 --- a/fcrepo-jms-indexer-core/src/main/java/org/fcrepo/indexer/IndexerGroup.java +++ b/fcrepo-jms-indexer-core/src/main/java/org/fcrepo/indexer/IndexerGroup.java @@ -32,6 +32,7 @@ import org.apache.http.impl.client.DefaultRedirectStrategy; import org.apache.http.impl.client.StandardHttpRequestRetryHandler; import org.apache.http.impl.conn.PoolingClientConnectionManager; +import org.fcrepo.indexer.Indexer.IndexerType; import org.fcrepo.kernel.utils.EventType; import org.slf4j.Logger; @@ -249,7 +250,8 @@ public void onMessage(final Message message) { baseURL = baseURL.substring(0, baseURL.length() - 1); } - index( baseURL + id, eventType ); + // jcr/xml persist need the base url to storage file in hierarchy structure + index( baseURL, id, eventType ); } catch (final JMSException e) { LOGGER.error("Error processing JMS event!", e); } @@ -258,7 +260,8 @@ public void onMessage(final Message message) { /** * Index a resource. **/ - private void index( final String uri, final String eventType ) { + private void index( final String baseURL, final String id, final String eventType ) { + final String uri = baseURL + id; final Boolean removal = REMOVAL_EVENT_TYPE.equals(eventType); final HttpClient httpClient = httpClient(uri); LOGGER.debug("It is {} that this is a removal operation.", removal); @@ -284,9 +287,9 @@ private void index( final String uri, final String eventType ) { // if this is a datastream, also index the parent object if (rdf.contains(createResource(uri), type, DATASTREAM_TYPE) && uri.indexOf("/fedora:system/") == -1 ) { - final String parent = uri.substring(0, uri.lastIndexOf("/")); - LOGGER.info("Datastream found, also indexing parent {}", parent); - index( parent, "NODE_UPDATED"); + final String parentID = uri.substring(0, uri.lastIndexOf("/")); + LOGGER.info("Datastream found, also indexing parent {}", parentID); + index( baseURL, parentID, "NODE_UPDATED"); } } @@ -319,7 +322,7 @@ private void index( final String uri, final String eventType ) { case JCRXML_PERSISTENCE: LOGGER.debug( "Retrieving jcr/xml for: {} and persist it to {}...", - uri, indexer); + id, indexer); content = jcrfr.get(); hasContent = true; break; @@ -334,13 +337,21 @@ private void index( final String uri, final String eventType ) { LOGGER.debug( "Executing removal of: {} to indexer: {}...", uri, indexer); - indexer.remove(uri); + if (indexer.getIndexerType().equals(IndexerType.JCRXML_PERSISTENCE)) { + indexer.remove(id); + } else { + indexer.remove(uri); + } } else { if (hasContent) { LOGGER.debug( "Executing update of: {} to indexer: {}...", uri, indexer); - indexer.update(uri, content); + if (indexer.getIndexerType().equals(IndexerType.JCRXML_PERSISTENCE)) { + indexer.update(id, content); + } else { + indexer.update(uri, content); + } } else if (indexable) { LOGGER.error( "Received update for: {} but was unable to retrieve " @@ -365,19 +376,21 @@ public void reindex( final String baseURI ) { /** * Reindex a resource (and optionally all of its children). - * @param uri The resource URI to reindex. + * @param baseURI the repository's base uri * @param recursive If true, also recursively reindex all children. **/ - public void reindex( final String uri, final boolean recursive ) { + public void reindex( final String baseURI, final boolean recursive ) { reindexed = new HashSet<>(); - reindexURI( uri, recursive ); + final String id = ""; + reindexURI( baseURI, id, recursive ); } - private void reindexURI( final String uri, final boolean recursive ) { + private void reindexURI( final String baseURI, final String id, final boolean recursive ) { + final String uri = baseURI + id; LOGGER.debug("Reindexing {}, recursive: {}", uri, recursive); if ( !reindexed.contains(uri) ) { // index() will check for indexable mixin - index( uri, REINDEX_EVENT_TYPE ); + index( baseURI, id, REINDEX_EVENT_TYPE ); } // prevent infinite recursion @@ -392,7 +405,7 @@ private void reindexURI( final String uri, final boolean recursive ) { while ( children.hasNext() ) { final String child = children.nextNode().asResource().getURI(); if ( !reindexed.contains(child) ) { - reindexURI( child, true ); + reindexURI( child, "", true ); } } } diff --git a/fcrepo-jms-indexer-core/src/main/java/org/fcrepo/indexer/persistence/JcrXmlPersistenceIndexer.java b/fcrepo-jms-indexer-core/src/main/java/org/fcrepo/indexer/persistence/JcrXmlPersistenceIndexer.java index 7659f5b..c6f07d4 100644 --- a/fcrepo-jms-indexer-core/src/main/java/org/fcrepo/indexer/persistence/JcrXmlPersistenceIndexer.java +++ b/fcrepo-jms-indexer-core/src/main/java/org/fcrepo/indexer/persistence/JcrXmlPersistenceIndexer.java @@ -22,9 +22,9 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; -import java.net.URLEncoder; import java.nio.file.CopyOption; import java.nio.file.Files; +import java.nio.file.LinkOption; import java.nio.file.Path; import java.nio.file.Paths; import java.util.concurrent.Callable; @@ -63,9 +63,9 @@ public JcrXmlPersistenceIndexer(final String pathName) { * @return **/ @Override - public Callable updateSynch(final String id, final InputStream content) { + public Callable updateSynch(final String idPath, final InputStream content) { - if (id.endsWith("/")) { + if (idPath.endsWith("/")) { throw new IllegalArgumentException( "Identifiers for use with this indexer may not end in '/'!"); } @@ -74,12 +74,19 @@ public Callable updateSynch(final String id, final InputStream content) { @Override public File call() throws IOException { + final Path dir = Paths.get(getPath(), idPath); + if (Files.notExists(dir, LinkOption.NOFOLLOW_LINKS)) { + Files.createDirectories(Paths.get(getPath(), idPath)); + } + // file name with object identifier - final String fileName = URLEncoder.encode(id, "UTF-8") + "-jcr.xml"; + final String fileName = dir.getFileName() + "-jcr.xml"; - LOGGER.debug("Updating {} to file: {}", id, getPath() + File.pathSeparatorChar + fileName); // write content to disk - final Path p = Paths.get(getPath(), fileName); + final Path p = Paths.get(dir.toString(), fileName); + + LOGGER.debug("Updating {} to file: {}", idPath, p.toAbsolutePath().toString()); + Files.copy(content, p, new CopyOption[]{}); return p.toFile(); } @@ -94,10 +101,10 @@ private String getPath() { * Remove the object from the file system. **/ @Override - public Callable removeSynch(final String id) { + public Callable removeSynch(final String idPath) { // empty update - LOGGER.debug("Received remove for identifier: {}", id); - return updateSynch(id, new ByteArrayInputStream("".getBytes())); + LOGGER.debug("Received remove for identifier: {}", idPath); + return updateSynch(idPath, new ByteArrayInputStream("".getBytes())); } @Override diff --git a/fcrepo-jms-indexer-core/src/test/java/org/fcrepo/indexer/persistence/JcrXmlPersistenceIndexerTest.java b/fcrepo-jms-indexer-core/src/test/java/org/fcrepo/indexer/persistence/JcrXmlPersistenceIndexerTest.java index 851c5d4..40a1801 100644 --- a/fcrepo-jms-indexer-core/src/test/java/org/fcrepo/indexer/persistence/JcrXmlPersistenceIndexerTest.java +++ b/fcrepo-jms-indexer-core/src/test/java/org/fcrepo/indexer/persistence/JcrXmlPersistenceIndexerTest.java @@ -79,6 +79,27 @@ public void updateTest() throws IOException, InterruptedException, ExecutionExce assertTrue("Content doesn't contain our property!", content.equals(testContent)); } + @Test + public void updateWithHierarchyPathTest() + throws IOException, InterruptedException, ExecutionException { + final String path1 = "updateHier" + randomUUID(); + final String path2 = "" + randomUUID(); + final String testId = path1 + "/" + path2; + final InputStream input = new ByteArrayInputStream (testContent.getBytes()); + + final File f = indexer.update(testId, input).get(); + + // file should exist + LOGGER.debug("Got filename: {}", f.getName()); + assertTrue("Filename doesn't match", f.getName().startsWith(path2)); + assertTrue("File Path2 doesn't match", f.getParentFile().getName().equals(path2)); + assertTrue("File Path1 doesn't match", + f.getParentFile().getParentFile().getName().equals(path1)); + + // content should be 'test content' + final String content = new String(readAllBytes(f.toPath())); + assertTrue("Content doesn't contain our property!", content.equals(testContent)); + } @Test public void removeTest() throws IOException, InterruptedException, ExecutionException { final String testId = "removeTest" + randomUUID();