Search in sources :

Example 1 with InlineContent

use of v7db.files.spi.InlineContent in project v7files by thiloplanz.

the class MongoContentStorage method getContent.

public Content getContent(ContentPointer pointer) throws IOException {
    if (pointer == null)
        return null;
    if (pointer instanceof InlineContent)
        return (Content) pointer;
    if (pointer instanceof ContentSHA) {
        ContentSHA p = (ContentSHA) pointer;
        byte[] sha = p.getSHA();
        Content base = getContent(sha);
        if (base == null)
            throw new IllegalArgumentException("base SHA not found: " + Hex.encodeHexString(sha));
        return base;
    }
    if (pointer instanceof StoredContent) {
        StoredContent p = (StoredContent) pointer;
        byte[] sha = p.getBaseSHA();
        Content base = getContent(sha);
        if (base == null)
            throw new IllegalArgumentException("base SHA not found: " + Hex.encodeHexString(sha));
        if (p.getLength() != base.getLength()) {
            return new OffsetAndLength(base, 0, p.getLength());
        }
        return base;
    }
    throw new IllegalArgumentException(pointer.getClass().toString());
}
Also used : OffsetAndLength(v7db.files.spi.OffsetAndLength) ContentSHA(v7db.files.spi.ContentSHA) InlineContent(v7db.files.spi.InlineContent) GzippedContent(v7db.files.spi.GzippedContent) Content(v7db.files.spi.Content) StoredContent(v7db.files.spi.StoredContent) InlineContent(v7db.files.spi.InlineContent) StoredContent(v7db.files.spi.StoredContent)

Example 2 with InlineContent

use of v7db.files.spi.InlineContent 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 3 with InlineContent

use of v7db.files.spi.InlineContent 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)

Aggregations

ContentSHA (v7db.files.spi.ContentSHA)3 InlineContent (v7db.files.spi.InlineContent)3 ContentPointer (v7db.files.spi.ContentPointer)2 StoredContent (v7db.files.spi.StoredContent)2 BasicDBObject (com.mongodb.BasicDBObject)1 WriteResult (com.mongodb.WriteResult)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Content (v7db.files.spi.Content)1 GzippedContent (v7db.files.spi.GzippedContent)1 OffsetAndLength (v7db.files.spi.OffsetAndLength)1