Search in sources :

Example 6 with ContentSHA

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));
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) WriteResult(com.mongodb.WriteResult) ContentSHA(v7db.files.spi.ContentSHA) ContentPointer(v7db.files.spi.ContentPointer) ArrayList(java.util.ArrayList) InlineContent(v7db.files.spi.InlineContent) StoredContent(v7db.files.spi.StoredContent)

Example 7 with ContentSHA

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;
}
Also used : ContentSHA(v7db.files.spi.ContentSHA) ContentPointer(v7db.files.spi.ContentPointer) InlineContent(v7db.files.spi.InlineContent) IOException(java.io.IOException)

Example 8 with ContentSHA

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());
}
Also used : MongoContentStorage(v7db.files.mongodb.MongoContentStorage) ContentSHA(v7db.files.spi.ContentSHA) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 9 with ContentSHA

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()));
}
Also used : MongoContentStorage(v7db.files.mongodb.MongoContentStorage) V7File(v7db.files.mongodb.V7File) ContentSHA(v7db.files.spi.ContentSHA) ByteArrayInputStream(java.io.ByteArrayInputStream) V7GridFS(v7db.files.mongodb.V7GridFS)

Example 10 with ContentSHA

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);
    }
}
Also used : ContentSHA(v7db.files.spi.ContentSHA) ArrayList(java.util.ArrayList) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) BSONObject(org.bson.BSONObject) MessageDigest(java.security.MessageDigest) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ContentSHA (v7db.files.spi.ContentSHA)12 MongoContentStorage (v7db.files.mongodb.MongoContentStorage)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 BasicDBObject (com.mongodb.BasicDBObject)3 IOException (java.io.IOException)3 ContentPointer (v7db.files.spi.ContentPointer)3 InlineContent (v7db.files.spi.InlineContent)3 GetMethodWebRequest (com.meterware.httpunit.GetMethodWebRequest)2 HttpNotFoundException (com.meterware.httpunit.HttpNotFoundException)2 PostMethodWebRequest (com.meterware.httpunit.PostMethodWebRequest)2 PutMethodWebRequest (com.meterware.httpunit.PutMethodWebRequest)2 WebRequest (com.meterware.httpunit.WebRequest)2 WebResponse (com.meterware.httpunit.WebResponse)2 ServletUnitClient (com.meterware.servletunit.ServletUnitClient)2 ArrayList (java.util.ArrayList)2 BSONObject (org.bson.BSONObject)2 BasicBSONObject (org.bson.BasicBSONObject)2 V7File (v7db.files.mongodb.V7File)2 V7GridFS (v7db.files.mongodb.V7GridFS)2 StoredContent (v7db.files.spi.StoredContent)2