use of com.opentext.ia.sdk.support.io.EncodedHash in project infoarchive-sip-sdk by Enterprise-Content-Management.
the class WhenAssemblingSips method assertPackagingInformation.
private void assertPackagingInformation(ZipInputStream zip, Collection<Object> objects, EncodedHash pdiHash) throws IOException {
ZipEntry entry = zip.getNextEntry();
assertNotNull(entry, "Missing Packaging Information");
assertEquals("eas_sip.xml", entry.getName(), "Zip entry");
try (ByteArrayInputOutputStream packagingInformation = new ByteArrayInputOutputStream()) {
IOUtils.copy(zip, packagingInformation);
Element sipElement = assertValidXml(packagingInformation.getInputStream(), "PackagingInformation", "sip.xsd").getDocumentElement();
assertTrue(XmlUtil.namedElementsIn(sipElement, "pdi_hash").filter(e -> equals(pdiHash, e)).findAny().isPresent(), "Missing pdi_hash: " + pdiHash);
String aiuCount = XmlUtil.getFirstChildElement(sipElement, "aiu_count").getTextContent();
assertEquals(objects.size(), Integer.parseInt(aiuCount), "# AIUs");
}
zip.closeEntry();
}
use of com.opentext.ia.sdk.support.io.EncodedHash in project infoarchive-sip-sdk by Enterprise-Content-Management.
the class ContentAssemblerDefault method addContent.
protected synchronized ContentInfo addContent(String ri, DigitalObject digitalObject) throws IOException {
if (zip == null) {
throw new IllegalStateException("Missing zip; did youc call begin()?");
}
try (InputStream stream = digitalObject.get()) {
String referenceInformation = digitalObject.getReferenceInformation();
Collection<EncodedHash> hashes;
long numBytesHashed;
synchronized (zip) {
hashes = zip.addEntry(referenceInformation, stream, contentHashAssembler);
numBytesHashed = contentHashAssembler.numBytesHashed();
}
incMetric(SipMetrics.SIZE_DIGITAL_OBJECTS, numBytesHashed);
return new ContentInfo(ri, hashes);
}
}
use of com.opentext.ia.sdk.support.io.EncodedHash in project infoarchive-sip-sdk by Enterprise-Content-Management.
the class WhenAssemblingSips method shouldZipContentsAndReportMetrics.
@Test
void shouldZipContentsAndReportMetrics() throws IOException {
Assembler<HashedContents<Object>> pdiAssembler = mock(Assembler.class);
HashAssembler pdiHashAssembler = mock(HashAssembler.class);
EncodedHash hash = someHash();
when(pdiHashAssembler.get()).thenReturn(Collections.singletonList(hash));
long pdiSize = randomInt(7, 128);
when(pdiHashAssembler.numBytesHashed()).thenReturn(pdiSize);
Object object1 = "object1_" + randomString(8);
Object object2 = "object2_" + randomString(8);
Collection<Object> objects = Arrays.asList(object1, object2);
DigitalObjectsExtraction<Object> contentsExtraction = mock(DigitalObjectsExtraction.class);
String id1a = randomString(8);
String id1b = randomString(8);
String id2 = randomString(8);
List<? extends DigitalObject> digitalObjects1 = Arrays.asList(someContentDataObject(id1a), someContentDataObject(id1b));
List<? extends DigitalObject> digitalObjects2 = Collections.singletonList(someContentDataObject(id2));
when(contentsExtraction.apply(object1)).thenAnswer(invocation -> digitalObjects1.iterator());
when(contentsExtraction.apply(object2)).thenAnswer(invocation -> digitalObjects2.iterator());
HashAssembler contentHashAssembler = mock(HashAssembler.class);
Collection<EncodedHash> hashes1a = Collections.singletonList(someHash());
Collection<EncodedHash> hashes1b = Collections.singletonList(someHash());
Collection<EncodedHash> hashes2 = Collections.singletonList(someHash());
Iterator<Collection<EncodedHash>> hashes = Arrays.asList(hashes1a, hashes1b, hashes2).iterator();
when(contentHashAssembler.get()).thenAnswer(invocation -> hashes.next());
long digitalObjectSize = randomInt(5, 255);
when(contentHashAssembler.numBytesHashed()).thenReturn(digitalObjectSize);
Map<String, ContentInfo> hashesById1 = new HashMap<>();
hashesById1.put(id1a, new ContentInfo(id1a, hashes1a));
hashesById1.put(id1b, new ContentInfo(id1b, hashes1b));
Map<String, ContentInfo> hashesById2 = Collections.singletonMap(id2, new ContentInfo(id2, hashes2));
PackagingInformation packagingInformationPrototype = somePackagingInformation();
SipAssembler<Object> sipAssembler = SipAssembler.forPdiAndContentWithHashing(packagingInformationPrototype, pdiAssembler, pdiHashAssembler, contentsExtraction, contentHashAssembler);
DataBuffer buffer = new MemoryBuffer();
long time = System.currentTimeMillis();
sipAssembler.start(buffer);
for (Object object : objects) {
sipAssembler.add(object);
}
sipAssembler.end();
time = System.currentTimeMillis() - time;
verify(pdiAssembler).start(any(DataBuffer.class));
verify(pdiAssembler).add(eq(new HashedContents<>(object1, hashesById1)));
verify(pdiAssembler).add(eq(new HashedContents<>(object2, hashesById2)));
verify(pdiAssembler).end();
try (ZipInputStream zip = new ZipInputStream(buffer.openForReading())) {
assertContentDataObject(zip, id1a);
assertContentDataObject(zip, id1b);
assertContentDataObject(zip, id2);
assertPreservationDescriptionInformation(zip);
assertPackagingInformation(zip, objects, hash);
assertNull(zip.getNextEntry(), "Additional zip entries");
}
SipMetrics metrics = sipAssembler.getMetrics();
assertEquals(2, metrics.numAius(), SipMetrics.NUM_AIUS);
assertEquals(3, metrics.numDigitalObjects(), SipMetrics.NUM_DIGITAL_OBJECTS);
assertEquals(time, metrics.assemblyTime(), DELTA_MS, SipMetrics.ASSEMBLY_TIME);
assertEquals(3 * digitalObjectSize, metrics.digitalObjectsSize(), SipMetrics.SIZE_DIGITAL_OBJECTS);
assertEquals(pdiSize, metrics.pdiSize(), SipMetrics.SIZE_PDI);
// long packagingInformationSize =
// getPackagingInformationSize(packagingInformationPrototype, 2, Optional.of(hash));
// assertEquals(pdiSize + 3 * digitalObjectSize + packagingInformationSize, metrics.sipSize(),
// SipMetrics.SIZE_SIP);
assertEquals(buffer.length(), metrics.sipFileSize(), SipMetrics.SIZE_SIP_FILE);
}
use of com.opentext.ia.sdk.support.io.EncodedHash in project infoarchive-sip-sdk by Enterprise-Content-Management.
the class WhenWorkingWithPackingInformation method shouldAcceptSetValues.
@Test
void shouldAcceptSetValues() {
DataSubmissionSession dss = mock(DataSubmissionSession.class);
Date productionDate = mock(Date.class);
int sequenceNumber = randomInt(0, Integer.MAX_VALUE);
boolean isLast = true;
long aiuCount = randomInt(0, Integer.MAX_VALUE);
long pageCount = randomInt(0, Integer.MAX_VALUE);
Optional<EncodedHash> pdiHash = Optional.empty();
PackagingInformation pi = new PackagingInformation(dss, productionDate, sequenceNumber, isLast, aiuCount, pageCount, pdiHash);
long newAiuCount = randomInt(0, Integer.MAX_VALUE);
pi.setAiuCount(newAiuCount);
assertEquals(newAiuCount, pi.getAiuCount(), "AIU Count is changed.");
long newPageCount = randomInt(0, Integer.MAX_VALUE);
pi.setPageCount(newPageCount);
assertEquals(newPageCount, pi.getPageCount(), "Page Count is changed.");
}
use of com.opentext.ia.sdk.support.io.EncodedHash in project infoarchive-sip-sdk by Enterprise-Content-Management.
the class ContentAssemblerWithDedupOnHash method addContent.
@Override
protected ContentInfo addContent(String ri, DigitalObject digitalObject) throws IOException {
RepeatableInputStream memoryStream = memoryStreamOf(digitalObject);
// First compute hashes
Collection<EncodedHash> hashes = contentHashFor(memoryStream);
// Check if contentInfo exist of the collection of hashes.
// If yes, skip adding the content and return existing content info.
ContentInfo contentInfo = hashesToContentInfo.get(hashes);
if (contentInfo != null) {
return contentInfo;
}
try (InputStream stream = memoryStream.get()) {
addZipEntry(ri, stream, noHashAssembler);
incMetric(SipMetrics.SIZE_DIGITAL_OBJECTS, getContentHashAssembler().numBytesHashed());
contentInfo = new ContentInfo(ri, hashes);
hashesToContentInfo.put(hashes, contentInfo);
}
return contentInfo;
}
Aggregations