Search in sources :

Example 1 with StoredContent

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

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

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

the class MongoReferenceTrackingTest method testInsert.

public void testInsert() throws MongoException, IOException {
    Mongo mongo = getMongo();
    ReferenceTracking refs = new MongoReferenceTracking(mongo.getDB("test").getCollection("v7files.refs"));
    Object owner = new DBRef(null, "test", "test");
    refs.updateReferences(owner, new StoredContent(new byte[20], 1000));
    assertMockMongoFieldContains(new byte[20], "test.v7files.refs", owner, "refs");
    assertMockMongoFieldContains(new byte[20], "test.v7files.refs", owner, "refHistory");
    mongo.close();
}
Also used : ReferenceTracking(v7db.files.spi.ReferenceTracking) Mongo(com.mongodb.Mongo) DBRef(com.mongodb.DBRef) BasicBSONObject(org.bson.BasicBSONObject) StoredContent(v7db.files.spi.StoredContent)

Example 4 with StoredContent

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

the class MongoReferenceTrackingTest method testUpdate.

public void testUpdate() throws MongoException, IOException {
    byte[] oldRef = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
    prepareMockData("test.v7files.refs", new BasicBSONObject("_id", "x").append("refs", new Object[] { oldRef }).append("refHistory", new Object[] { oldRef }));
    Mongo mongo = getMongo();
    ReferenceTracking refs = new MongoReferenceTracking(mongo.getDB("test").getCollection("v7files.refs"));
    refs.updateReferences("x", new StoredContent(new byte[20], 1000));
    assertMockMongoFieldContains(new byte[20], "test.v7files.refs", "x", "refs");
    assertMockMongoFieldContains(new byte[20], "test.v7files.refs", "x", "refHistory");
    assertMockMongoFieldDoesNotContain(oldRef, "test.v7files.refs", "x", "refs");
    assertMockMongoFieldContains(oldRef, "test.v7files.refs", "x", "refHistory");
    mongo.close();
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) ReferenceTracking(v7db.files.spi.ReferenceTracking) Mongo(com.mongodb.Mongo) BasicBSONObject(org.bson.BasicBSONObject) StoredContent(v7db.files.spi.StoredContent)

Example 5 with StoredContent

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

the class MongoContentStorage method getContent.

/**
 * Supported formats: 1) Serialized ContentPointers, e.g.
 *
 * <pre>
 * { in: [bytes] }
 * </pre>
 *
 * and
 *
 * <pre>
 * { sha: <sha>, length: 123 }
 * </pre>
 *
 * 2) Internal StorageScheme representations (must have {store: something}")
 */
public Content getContent(Map<String, Object> data) throws IOException {
    if (data == null)
        return null;
    String store = MapUtils.getString(data, "store");
    if (store == null || "raw".equals(store)) {
        if (data.containsKey("in"))
            return InlineContent.deserialize(data);
        if (data.containsKey("sha")) {
            return new StoredContent((byte[]) data.get("sha"), MapUtils.getRequiredLong(data, "length")).loadOrLazyLoad(this, 8 * 1024);
        }
        throw new UnsupportedOperationException(data.toString());
    }
    StorageScheme s = storageSchemes.get(store);
    if (s == null)
        throw new UnsupportedOperationException(store);
    return s.getContent(this, data);
}
Also used : StorageScheme(v7db.files.spi.StorageScheme) StoredContent(v7db.files.spi.StoredContent)

Aggregations

StoredContent (v7db.files.spi.StoredContent)6 BasicDBObject (com.mongodb.BasicDBObject)2 Mongo (com.mongodb.Mongo)2 BasicBSONObject (org.bson.BasicBSONObject)2 ContentSHA (v7db.files.spi.ContentSHA)2 InlineContent (v7db.files.spi.InlineContent)2 ReferenceTracking (v7db.files.spi.ReferenceTracking)2 StorageScheme (v7db.files.spi.StorageScheme)2 DBObject (com.mongodb.DBObject)1 DBRef (com.mongodb.DBRef)1 WriteResult (com.mongodb.WriteResult)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 BSONObject (org.bson.BSONObject)1 Content (v7db.files.spi.Content)1 ContentPointer (v7db.files.spi.ContentPointer)1 GzippedContent (v7db.files.spi.GzippedContent)1 OffsetAndLength (v7db.files.spi.OffsetAndLength)1