fakemongo / fongo

faked out in-memory mongo for java
Apache License 2.0
523 stars 155 forks source link

GridFS not supported #55

Open damienmg opened 10 years ago

damienmg commented 10 years ago

Apparently, mongodb does translation of GridFSDBFile on the fly when reading the database which fongo does not.

Try retrieving a GridFS files and you'll get a RuntimeException "somehow didn't get a GridFSDBFile"

ml054 commented 9 years ago

Is it still an issue? I'm using

com.github.fakemongo fongo 1.6.3 test

And it appears to save/load files using GridFsTemplate.

twillouer commented 9 years ago

Hi,

I didn't test, but I'm glad if all is fine now :)

v-lukes commented 8 years ago

UPDATE: This appears to have been fixed in 1.6.3, which would agree with the above comment by ml054. I just re-tested with 1.6.4 and the failing images now succeed.

I have had intermittent failures when saving and retrieving binary (.png) files. It's a bit strange (without knowing implementation details) that it's always the same files that fail consistently... I've tried it with both 1.5.8 and 1.6.2.

I'm attaching an example - one is the direct output of an ImageIO operation, and the other is the same binary content after being saved & retrieved from Fongo via the method call:

gridFsOperations.store(inputStream, assetId);

564df7f9e4b01ea9dd9fb3ed-gridfs 564df7f9e4b01ea9dd9fb3ed-imageio

dvoloshyn commented 7 years ago

If you use GridFs through com.mongodb.client.gridfs package classes, you run into the problem that fongo believes "fs.chunks" and "fs.files" are invalid collection names. I dirty fixed that in my project using the following code. Now it seems to work alright)))

package com.github.fakemongo

import com.mongodb.MongoNamespace import com.mongodb.WriteConcern import com.mongodb.binding.WriteBinding import com.mongodb.bulk.BulkWriteResult import com.mongodb.bulk.InsertRequest import com.mongodb.connection.ServerVersion import com.mongodb.operation.WriteOperation import org.bson.codecs.configuration.CodecRegistry

class FongoEx(name: String, serverVersion: ServerVersion, codecRegistry: CodecRegistry) : Fongo(name, serverVersion, codecRegistry) { override fun execute(operation: WriteOperation): T = operation.execute(WriteBindingEx(this))

internal class WriteBindingEx(private val fongo: FongoEx) : WriteBinding {
    override fun getWriteConnectionSource(): ConnectionSource = ConnectionSource(fongo)

    override fun retain(): WriteBinding = this

    override fun getCount(): Int = 0

    override fun release() {}
}

internal class ConnectionSource(private val fongo: FongoEx) : FongoConnectionSource(fongo) {
    override fun getConnection(): Connection = Connection(fongo)
}

internal class Connection(fongo: FongoEx) : FongoConnection(fongo) {
    override fun insertCommand(namespace: MongoNamespace, ordered: Boolean, writeConcern: WriteConcern?,
                               bypassDocumentValidation: Boolean?, inserts: MutableList<InsertRequest>?): BulkWriteResult {
        val effectiveBypassDocumentValidation = if (namespace.collectionName.contains('.')) true else bypassDocumentValidation
        return super.insertCommand(namespace, ordered, writeConcern, effectiveBypassDocumentValidation, inserts)
    }
}

}