use of v7db.files.spi.ContentPointer in project v7files by thiloplanz.
the class ZipFileTest method testIndexZipFile.
public void testIndexZipFile() throws MongoException, IOException, ZipException, DecoderException {
ContentStorage storage = new MongoContentStorage(getMongo().getDB("test"));
ContentPointer zip = storage.storeContent(getClass().getResourceAsStream("mongodb.epub"));
ZipFile.index(storage, zip);
assertEquals("fc012bb0439382f709d3caebab958ff592811d17", DigestUtils.shaHex(storage.getContent(decodeHex("fc012bb0439382f709d3caebab958ff592811d17".toCharArray())).getInputStream()));
}
use of v7db.files.spi.ContentPointer in project v7files by thiloplanz.
the class MongoReferenceTracking method updateReferences.
public void updateReferences(Object ownerId, ContentPointer... contents) throws IOException {
List<byte[]> content = new ArrayList<byte[]>();
for (ContentPointer cp : contents) {
if (cp instanceof InlineContent)
continue;
if (cp instanceof StoredContent)
content.add(((StoredContent) cp).getBaseSHA());
else if (cp instanceof ContentSHA)
content.add(((ContentSHA) cp).getSHA());
else
throw new IllegalArgumentException(cp.getClass().getName());
}
WriteResult r = refCollection.update(new BasicDBObject("_id", ownerId), new BasicDBObject("$set", new BasicDBObject("refs", content)).append("$addToSet", new BasicDBObject("refHistory", new BasicDBObject("$each", content))), false, false, WriteConcern.SAFE);
if (r.getN() == 1)
return;
if (r.getN() != 0)
throw new IllegalStateException();
refCollection.insert(WriteConcern.SAFE, new BasicDBObject("_id", ownerId).append("refs", content).append("refHistory", content));
}
use of v7db.files.spi.ContentPointer in project v7files by thiloplanz.
the class V7File method getDigest.
public String getDigest() {
ContentPointer contentPointer = getContentPointer();
if (contentPointer instanceof ContentSHA) {
return ((ContentSHA) contentPointer).getDigest();
}
if (contentPointer instanceof InlineContent) {
try {
return DigestUtils.shaHex(((InlineContent) contentPointer).getInputStream());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if (contentPointer.getLength() == 0)
return ContentSHA.calculate(ArrayUtils.EMPTY_BYTE_ARRAY).getDigest();
// TODO:
System.err.println("NO DIGEST!");
return null;
}
use of v7db.files.spi.ContentPointer in project v7files by thiloplanz.
the class V7GridFS method updateContents.
private void updateContents(DBObject metaData, InputStream contents) throws IOException {
Object fileId = metaData.get("_id");
ContentPointer oldContents = getContentPointer(metaData);
String filename = (String) metaData.get("filename");
String contentType = (String) metaData.get("contentType");
BSONObject newContent = storage.insertContentsAndBackRefs(contents, fileId, filename, contentType);
// check if it has changed
ContentPointer newContents = getContentPointer(newContent);
if (newContents.contentEquals(oldContents))
return;
metaData.removeField("sha");
metaData.removeField("length");
metaData.removeField("in");
metaData.putAll(newContent);
updateMetaData(metaData);
}
use of v7db.files.spi.ContentPointer in project v7files by thiloplanz.
the class V7GridFS method updateContents.
private void updateContents(DBObject metaData, byte[] contents, int offset, int len) throws IOException {
Object fileId = metaData.get("_id");
ContentPointer oldContents = getContentPointer(metaData);
String filename = (String) metaData.get("filename");
String contentType = (String) metaData.get("contentType");
// for up to 55 bytes, storing the complete file inline
// takes less space than just storing the SHA-1 and length
// 20 (SHA-1) + 1 (sha - in) + 6 (length) + 4 (int32) + 2*12
// (ObjectId back-references)
BSONObject newContent = storage.inlineOrInsertContentsAndBackRefs(55, contents, offset, len, fileId, filename, contentType);
// check if it has changed
ContentPointer newContents = getContentPointer(newContent);
if (newContents.contentEquals(oldContents))
return;
metaData.removeField("sha");
metaData.removeField("length");
metaData.removeField("in");
metaData.putAll(newContent);
updateMetaData(metaData);
}
Aggregations