GridFS files and file reference attributes


MemoryX use MongoDB implementation of GridFS to store files in a database.


Following properties are supported:

  • <ArmarX|MemoryX>.CachePath: Files will be cached in $CachePath/files directory

The default cache directory is


File reference attributes

Amongst other data types, EntityAttribute can have a file as its value. One common usage is an inventor model file required to visualize an object.

MemoryX handles this case as follows: the actual file resides in GridFS, whereas the attribute itself holds only a reference to it (s. memoryx::MongoDBRef class for details).

memoryx::GridFileManager class provides a convenient way to "dereference" such attributes and get the file content. Moreover, it performs local caching of retrieved files "under the hood".

Reading a file stored in object attribute

Init short-term memory

// get a proxy
memoryPrx = getProxy<WorkingMemoryInterfacePrx>("WorkingMemory");
// get a memory segment containing object instances
WorkingMemorySegmentBasePrx instancesSegment = memoryPrx->getObjectInstancesSegment();
// fetch an entity
const EntityBasePtr entity = instancesSegment->getEntityById(1);

Init GridFileManager

// CommonStorage proxy must be provided as a parameter
GridFileManagerPtr fileManager ( new GridFileManager(memoryPrx->getCommonStorage()) );

Option 1. Get an opened file stream

std::ifstream fs;
if (fileManager->getFileStream(entity->getAttribute("IvFile"), fs))
// read filestream as usual

Option 2. Get a local file name

std::string localFileName;
if (fileManager->ensureFileInCache(entity->getAttribute("IvFile"), localFileName))
// open an ifstream for localFileName, pass it to your library function etc

Both methods will retrieve the whole file and cache it locally, making available for further processing with usual filesystem tools.

Saving a file into object attribute

// create object instance
ObjectInstancePtr inst = new ObjectInstance();
// create attribute
EntityAttributePtr fileAttr = new EntityAttribute("IvFile");
// Store file in GridFS and put a reference to it into attribute
"mydb", // Mongo database to store file into
"/home/alex/data/table.iv", // File name on the locale filesystem
fileAttr, // Attribute to store reference into
"table.iv"); // File name in Mongo GridFS (optional)
// add file attribute to instance
// save instance into working memory
IceInternal::Handle< ObjectInstance > ObjectInstancePtr
Definition: ObjectInstance.h:42
std::shared_ptr< GridFileManager > GridFileManagerPtr
Definition: AbstractEntityWrapper.h:32
IceInternal::Handle< EntityAttribute > EntityAttributePtr
Typedef of EntityAttributePtr as IceInternal::Handle<EntityAttribute> for convenience.
Definition: EntityAttribute.h:39