use of v7db.files.spi.ContentSHA 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.ContentSHA 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.ContentSHA in project v7files by thiloplanz.
the class CatCommandTest method testSha.
public void testSha() throws IOException {
MongoContentStorage storage = new MongoContentStorage(getMongo().getDB("test"));
ContentSHA sha = storage.storeContent(new ByteArrayInputStream("test".getBytes()));
CatCommand.main(new String[] { "cat", "-sha", sha.getDigest() });
System.out.flush();
assertEquals("test", out.toString());
}
use of v7db.files.spi.ContentSHA in project v7files by thiloplanz.
the class CopyCommandTest method testSHA.
public void testSHA() throws IOException {
MongoContentStorage storage = new MongoContentStorage(getMongo().getDB("test"));
ContentSHA sha = storage.storeContent(new ByteArrayInputStream("test".getBytes()));
CopyCommand.main(new String[] { "copy", "-sha", sha.getDigest(), "x", "copy.txt" });
V7GridFS fs = new V7GridFS(getMongo().getDB("test"));
V7File file = fs.getFile("x", "copy.txt");
assertEquals("test", IOUtils.toString(file.getInputStream()));
}
use of v7db.files.spi.ContentSHA in project v7files by thiloplanz.
the class MongoContentStorage method storeContent.
public ContentSHA storeContent(InputStream data) throws IOException {
try {
MessageDigest completeSHA = MessageDigest.getInstance("SHA");
long completeLength = 0;
byte[] chunk = new byte[chunkSize];
int read;
List<ContentSHA> chunks = new ArrayList<ContentSHA>();
while (0 < (read = readFully(data, chunk))) {
completeSHA.update(chunk, 0, read);
completeLength += read;
chunks.add(storeContentChunk(chunk, 0, read));
}
if (chunks.isEmpty())
return storeContentChunk(ArrayUtils.EMPTY_BYTE_ARRAY, 0, 0);
if (chunks.size() == 1)
return chunks.get(0);
List<Map<String, Object>> bases = new ArrayList<Map<String, Object>>(chunks.size());
for (ContentSHA c : chunks) {
bases.add(c.serialize());
}
ContentSHA result = ContentSHA.forDigestAndLength(completeSHA.digest(), completeLength);
long existing = contentCollection.count(new BasicDBObject(_ID, result.getSHA()));
if (existing == 0) {
contentCollection.insert(new BasicDBObject(_ID, result.getSHA()).append("store", "cat").append("base", bases), WriteConcern.SAFE);
}
return result;
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
} finally {
IOUtils.closeQuietly(data);
}
}
Aggregations