Search in sources :

Example 6 with ContentPointer

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()));
}
Also used : MongoContentStorage(v7db.files.mongodb.MongoContentStorage) MongoContentStorage(v7db.files.mongodb.MongoContentStorage) ContentStorage(v7db.files.spi.ContentStorage) ContentPointer(v7db.files.spi.ContentPointer)

Example 7 with ContentPointer

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));
}
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 8 with ContentPointer

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

Example 9 with ContentPointer

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);
}
Also used : ContentPointer(v7db.files.spi.ContentPointer) BSONObject(org.bson.BSONObject) BasicDBObject(com.mongodb.BasicDBObject) BSONObject(org.bson.BSONObject) DBObject(com.mongodb.DBObject)

Example 10 with ContentPointer

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);
}
Also used : ContentPointer(v7db.files.spi.ContentPointer) BSONObject(org.bson.BSONObject) BasicDBObject(com.mongodb.BasicDBObject) BSONObject(org.bson.BSONObject) DBObject(com.mongodb.DBObject)

Aggregations

ContentPointer (v7db.files.spi.ContentPointer)12 BasicDBObject (com.mongodb.BasicDBObject)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 BSONObject (org.bson.BSONObject)4 ContentStorage (v7db.files.spi.ContentStorage)4 DBObject (com.mongodb.DBObject)3 Mongo (com.mongodb.Mongo)3 ContentSHA (v7db.files.spi.ContentSHA)3 BasicBSONObject (org.bson.BasicBSONObject)2 MongoContentStorage (v7db.files.mongodb.MongoContentStorage)2 InlineContent (v7db.files.spi.InlineContent)2 DBCollection (com.mongodb.DBCollection)1 DBRef (com.mongodb.DBRef)1 WriteResult (com.mongodb.WriteResult)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Random (java.util.Random)1 Content (v7db.files.spi.Content)1 ReferenceTracking (v7db.files.spi.ReferenceTracking)1 StoredContent (v7db.files.spi.StoredContent)1