Introduction
MemoryX use MongoDB implementation of GridFS to store files in a database.
Configuration
Following properties are supported:
- <ArmarX|MemoryX>.CachePath: Files will be cached in $CachePath/files directory
The default cache directory is
~/.armarx/mongo/.cache/files
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
memoryPrx = getProxy<WorkingMemoryInterfacePrx>("WorkingMemory");
WorkingMemorySegmentBasePrx instancesSegment = memoryPrx->getObjectInstancesSegment();
const EntityBasePtr entity = instancesSegment->getEntityById(1);
Init GridFileManager
GridFileManagerPtr fileManager ( new GridFileManager(memoryPrx->getCommonStorage()) );
Option 1. Get an opened file stream
std::ifstream fs;
if (fileManager->getFileStream(entity->getAttribute("IvFile"), fs))
{
}
Option 2. Get a local file name
std::string localFileName;
if (fileManager->ensureFileInCache(entity->getAttribute("IvFile"), localFileName))
{
}
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
ObjectInstancePtr inst = new ObjectInstance();
EntityAttributePtr fileAttr = new EntityAttribute("IvFile");
fileManager->storeFileToAttr(
"mydb",
"/home/alex/data/table.iv",
fileAttr,
"table.iv");
inst->putAttribute(fileAttr);
instancesSegment->putEntity(inst);