Skip to content
This repository has been archived by the owner on Jan 3, 2019. It is now read-only.

Persist jcr/xml in hierarchy structure. #49

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
Expand All @@ -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");
}
}

Expand Down Expand Up @@ -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;
Expand All @@ -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 "
Expand All @@ -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
Expand All @@ -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 );
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -63,9 +63,9 @@ public JcrXmlPersistenceIndexer(final String pathName) {
* @return
**/
@Override
public Callable<File> updateSynch(final String id, final InputStream content) {
public Callable<File> 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 '/'!");
}
Expand All @@ -74,12 +74,19 @@ public Callable<File> 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();
}
Expand All @@ -94,10 +101,10 @@ private String getPath() {
* Remove the object from the file system.
**/
@Override
public Callable<File> removeSynch(final String id) {
public Callable<File> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down