GridFileWrapper.cpp
Go to the documentation of this file.
1/*
2* This file is part of ArmarX.
3*
4* ArmarX is free software; you can redistribute it and/or modify
5* it under the terms of the GNU General Public License version 2 as
6* published by the Free Software Foundation.
7*
8* ArmarX is distributed in the hope that it will be useful, but
9* WITHOUT ANY WARRANTY; without even the implied warranty of
10* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11* GNU General Public License for more details.
12*
13* You should have received a copy of the GNU General Public License
14* along with this program. If not, see <http://www.gnu.org/licenses/>.
15*
16* @package MemoryX::CommonStorage
17* @author Alexey Kozlov ( kozlov at kit dot edu)
18* @date 2012
19* @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20* GNU General Public License
21*/
22
23#include "GridFileWrapper.h"
24
25#include <IceUtil/UUID.h>
26
29
30#include <mongo/client/dbclient.h>
31
32using namespace memoryx;
33
34GridFileWrapper::GridFileWrapper(const mongo::GridFile& gridFile,
35 const std::shared_ptr<std::mutex>& accessGridFSFilesMutex) :
36 gridFile(new mongo::GridFile(gridFile)),
37 nextChunkNo(0),
38 accessGridFSFilesMutex(accessGridFSFilesMutex)
39{
40 iceId.name = IceUtil::generateUUID();
41}
42
44
45std::string
46GridFileWrapper::getId(const Ice::Current&)
47{
48 return gridFile->getFileField("_id").OID().toString();
49}
50
51std::string
52GridFileWrapper::getFilename(const Ice::Current&)
53{
54 return gridFile->getFilename();
55}
56
57std::string
58GridFileWrapper::getMD5(const Ice::Current&)
59{
60 return gridFile->getMD5();
61}
62
63Ice::Long
64GridFileWrapper::getFileSize(const Ice::Current&)
65{
66 return gridFile->getContentLength();
67}
68
69Ice::Long
71{
72 return gridFile->getUploadDate();
73}
74
75Ice::Int
76GridFileWrapper::getNextChunk(memoryx::Blob& buffer, const Ice::Current&)
77{
79 // this mutex was needed to avoid crashes when multiple clients access GridFS
80 std::unique_lock l(*accessGridFSFilesMutex);
81 try
82 {
83
84 if (nextChunkNo < gridFile->getNumChunks())
85 {
86 mongo::GridFSChunk chunk = gridFile->getChunk(nextChunkNo++);
87 int datalen = 0;
88 const char* data = chunk.data(datalen);
89 buffer.resize(datalen);
90 memcpy(buffer.data(), data, datalen);
91 return datalen;
92 }
93 else
94 {
95 return 0;
96 }
97 }
98 catch (...)
99 {
100 ARMARX_ERROR << VAROUT(nextChunkNo) << "\n"
101 << VAROUT(gridFile) << "\n"
102 << VAROUT(gridFile->getFilename()) << "\n"
103 << VAROUT(gridFile->exists()) << "\n"
104 << VAROUT(gridFile->getChunkSize()) << "\n"
105 << VAROUT(gridFile->getNumChunks()) << "\n"
106 << VAROUT(gridFile->getContentLength()) << "\n"
107 << VAROUT(gridFile->getContentType()) << "\n"
108 << VAROUT(gridFile->getMD5()) << "\n"
109 << VAROUT(gridFile->getMetadata().jsonString()) << "\n"
111
112 throw;
113 }
114}
115
116Ice::Identity
118{
119 return iceId;
120}
uint8_t data[1]
#define VAROUT(x)
::Ice::Long getFileSize(const ::Ice::Current &=Ice::emptyCurrent) override
::std::string getMD5(const ::Ice::Current &=Ice::emptyCurrent) override
Ice::Identity getIceId() const
::Ice::Long getUploadDate(const ::Ice::Current &=Ice::emptyCurrent) override
::std::string getId(const ::Ice::Current &=Ice::emptyCurrent) override
::std::string getFilename(const ::Ice::Current &=Ice::emptyCurrent) override
::Ice::Int getNextChunk(::memoryx::Blob &buffer, const ::Ice::Current &=Ice::emptyCurrent) override
GridFileWrapper(const mongo::GridFile &gridFile, const std::shared_ptr< std::mutex > &accessGridFSFilesMutex)
#define ARMARX_ERROR
The logging level for unexpected behaviour, that must be fixed.
Definition Logging.h:196
std::string GetHandledExceptionString()
VirtualRobot headers.
#define ARMARX_TRACE
Definition trace.h:77