Skip to content

Commit

Permalink
refactor LowLeveLService into a proper proxy; unit test fcrepo-kernel…
Browse files Browse the repository at this point in the history
…/Datastream
  • Loading branch information
barmintor committed Mar 27, 2013
1 parent c8cefbe commit 6c6b076
Show file tree
Hide file tree
Showing 10 changed files with 477 additions and 108 deletions.
Expand Up @@ -55,6 +55,7 @@
import org.fcrepo.jaxb.responses.management.DatastreamHistory;
import org.fcrepo.jaxb.responses.management.DatastreamProfile;
import org.fcrepo.services.DatastreamService;
import org.fcrepo.services.LowLevelStorageService;
import org.fcrepo.utils.DatastreamIterator;
import org.fcrepo.utils.FixityResult;
import org.slf4j.Logger;
Expand All @@ -68,6 +69,9 @@ public class FedoraDatastreams extends AbstractResource {

@Inject
DatastreamService datastreamService;

@Inject
LowLevelStorageService llStoreService;

/**
* Returns a list of datastreams for the object
Expand Down Expand Up @@ -425,7 +429,7 @@ public DatastreamFixity getDatastreamFixity(@PathParam("pid")
dsf.dsId = dsid;
dsf.timestamp = new Date();

Collection<FixityResult> blobs = ds.runFixityAndFixProblems();
Collection<FixityResult> blobs = llStoreService.runFixityAndFixProblems(ds);
dsf.statuses = new ArrayList<FixityResult>(blobs);
return dsf;
}
Expand Down
Expand Up @@ -38,6 +38,7 @@
import org.fcrepo.jaxb.responses.management.DatastreamHistory;
import org.fcrepo.jaxb.responses.management.DatastreamProfile;
import org.fcrepo.services.DatastreamService;
import org.fcrepo.services.LowLevelStorageService;
import org.fcrepo.utils.DatastreamIterator;
import org.junit.After;
import org.junit.Before;
Expand All @@ -50,15 +51,19 @@ public class FedoraDatastreamsTest {

DatastreamService mockDatastreams;

LowLevelStorageService mockLow;

Repository mockRepo;

Session mockSession;

@Before
public void setUp() throws LoginException, RepositoryException {
mockDatastreams = mock(DatastreamService.class);
mockLow = mock(LowLevelStorageService.class);
testObj = new FedoraDatastreams();
testObj.datastreamService = mockDatastreams;
testObj.llStoreService = mockLow;
mockRepo = mock(Repository.class);
mockSession = mock(Session.class);
when(mockRepo.login()).thenReturn(mockSession);
Expand Down Expand Up @@ -217,7 +222,7 @@ public void testGetDatastreamFixity() throws RepositoryException, IOException {
DatastreamFixity actual = testObj.getDatastreamFixity(pid, dsId);
assertNotNull(actual);
verify(mockDatastreams).getDatastream(pid, dsId);
verify(mockDs).runFixityAndFixProblems();
verify(mockLow).runFixityAndFixProblems(mockDs);
verify(mockSession, never()).save();
}

Expand Down
5 changes: 5 additions & 0 deletions fcrepo-http-api/src/test/java/org/fcrepo/api/TestHelpers.java
Expand Up @@ -8,11 +8,15 @@
import java.util.List;
import java.util.Map;

import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.UnsupportedRepositoryOperationException;
import javax.jcr.Value;
import javax.jcr.ValueFactory;
import javax.jcr.Workspace;
import javax.jcr.query.Query;
Expand Down Expand Up @@ -98,4 +102,5 @@ public static Session getQuerySessionMock() {
}
return mock;
}

}
88 changes: 6 additions & 82 deletions fcrepo-kernel/src/main/java/org/fcrepo/Datastream.java
Expand Up @@ -7,7 +7,6 @@
import static com.google.common.collect.Sets.difference;
import static com.yammer.metrics.MetricRegistry.name;
import static java.security.MessageDigest.getInstance;
import static org.fcrepo.services.LowLevelStorageService.getFixity;
import static org.fcrepo.services.PathService.getDatastreamJcrNodePath;
import static org.fcrepo.services.ServiceHelpers.getNodePropertySize;
import static org.fcrepo.services.RepositoryService.metrics;
Expand Down Expand Up @@ -41,6 +40,7 @@
import javax.jcr.version.VersionException;

import org.fcrepo.exception.InvalidChecksumException;
import org.fcrepo.services.LowLevelStorageService;
import org.fcrepo.utils.ContentDigest;
import org.fcrepo.utils.FedoraJcrTypes;
import org.fcrepo.utils.FixityResult;
Expand All @@ -62,28 +62,17 @@
*/
public class Datastream extends JcrTools implements FedoraJcrTypes {

private static JcrTools jcrTools = new JcrTools(false);

private final static Logger logger = getLogger(Datastream.class);

final static Histogram contentSizeHistogram = metrics.histogram(name(
Datastream.class, "content-size"));

final static Counter fixityCheckCounter = metrics.counter(name(
Datastream.class, "fixity-check-counter"));

final static Timer timer = metrics.timer(name(Datastream.class,
"fixity-check-time"));

final static Counter fixityRepairedCounter = metrics.counter(name(
Datastream.class, "fixity-repaired-counter"));

final static Counter fixityErrorCounter = metrics.counter(name(
Datastream.class, "fixity-error-counter"));

Node node;

LowLevelStorageService llStore;

public Datastream(Node n) {
super(false); // turn off debug logging
this.node = n;
}

Expand All @@ -94,7 +83,8 @@ public Datastream(final Session session, String pid, String dsId)

public Datastream(final Session session, final String dsPath)
throws RepositoryException {
this.node = jcrTools.findOrCreateNode(session, dsPath, NT_FILE);
super(false);
this.node = findOrCreateNode(session, dsPath, NT_FILE);
if (this.node.isNew()) {
this.node.addMixin(FEDORA_DATASTREAM);
this.node.addMixin(FEDORA_OWNED);
Expand Down Expand Up @@ -224,72 +214,6 @@ public String getContentDigestType() throws RepositoryException {
.getString();
}

public Collection<FixityResult> runFixityAndFixProblems()
throws RepositoryException {
Set<FixityResult> fixityResults;
Set<FixityResult> goodEntries;
final URI digestUri = getContentDigest();
final long size = getContentSize();
MessageDigest digest;

fixityCheckCounter.inc();

try {
digest = getInstance(getContentDigestType());
} catch (NoSuchAlgorithmException e) {
throw new RepositoryException(e.getMessage(), e);
}

final Timer.Context context = timer.time();

try {
fixityResults = copyOf(getFixity(node, digest, digestUri, size));

goodEntries =
copyOf(filter(fixityResults, new Predicate<FixityResult>() {

@Override
public boolean apply(FixityResult result) {
return result.computedChecksum.equals(digestUri) &&
result.computedSize == size;
};
}));
} finally {
context.stop();
}

if (goodEntries.size() == 0) {
logger.error("ALL COPIES OF " + getObject().getName() + "/" +
getDsId() + " HAVE FAILED FIXITY CHECKS.");
return fixityResults;
}

final LowLevelCacheEntry anyGoodCacheEntry =
goodEntries.iterator().next().getEntry();

final Set<FixityResult> badEntries =
difference(fixityResults, goodEntries);

for (final FixityResult result : badEntries) {
try {
result.getEntry()
.storeValue(anyGoodCacheEntry.getInputStream());
final FixityResult newResult =
result.getEntry().checkFixity(digestUri, size, digest);
if (newResult.status.contains(SUCCESS)) {
result.status.add(REPAIRED);
fixityRepairedCounter.inc();
} else {
fixityErrorCounter.inc();
}
} catch (IOException e) {
e.printStackTrace();
}
}

return fixityResults;
}

/**
* @return The ID of this datastream, unique within an object. Normally just the name of the backing JCR node.
* @throws RepositoryException
Expand Down

0 comments on commit 6c6b076

Please sign in to comment.