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 
32 using namespace memoryx;
33 
34 GridFileWrapper::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 
45 std::string
46 GridFileWrapper::getId(const Ice::Current&)
47 {
48  return gridFile->getFileField("_id").OID().toString();
49 }
50 
51 std::string
52 GridFileWrapper::getFilename(const Ice::Current&)
53 {
54  return gridFile->getFilename();
55 }
56 
57 std::string
58 GridFileWrapper::getMD5(const Ice::Current&)
59 {
60  return gridFile->getMD5();
61 }
62 
64 GridFileWrapper::getFileSize(const Ice::Current&)
65 {
66  return gridFile->getContentLength();
67 }
68 
70 GridFileWrapper::getUploadDate(const Ice::Current&)
71 {
72  return gridFile->getUploadDate();
73 }
74 
76 GridFileWrapper::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 
118 {
119  return iceId;
120 }
GridFileWrapper.h
memoryx
VirtualRobot headers.
Definition: CommonPlacesTester.cpp:48
memoryx::GridFileWrapper::getIceId
Ice::Identity getIceId() const
Definition: GridFileWrapper.cpp:117
memoryx::GridFileWrapper::getFileSize
::Ice::Long getFileSize(const ::Ice::Current &=Ice::emptyCurrent) override
Definition: GridFileWrapper.cpp:64
mongo
Definition: CommonStorage.h:35
memoryx::GridFileWrapper::getUploadDate
::Ice::Long getUploadDate(const ::Ice::Current &=Ice::emptyCurrent) override
Definition: GridFileWrapper.cpp:70
armarx::GetHandledExceptionString
std::string GetHandledExceptionString()
Definition: Exception.cpp:165
ARMARX_TRACE
#define ARMARX_TRACE
Definition: trace.h:77
GfxTL::Identity
void Identity(MatrixXX< N, N, T > *a)
Definition: MatrixXX.h:570
memoryx::GridFileWrapper::getId
::std::string getId(const ::Ice::Current &=Ice::emptyCurrent) override
Definition: GridFileWrapper.cpp:46
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::VariantType::Long
const VariantTypeId Long
Definition: Variant.h:918
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:196
memoryx::GridFileWrapper::getNextChunk
::Ice::Int getNextChunk(::memoryx::Blob &buffer, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: GridFileWrapper.cpp:76
memoryx::GridFileWrapper::~GridFileWrapper
~GridFileWrapper() override
VAROUT
#define VAROUT(x)
Definition: StringHelpers.h:198
memoryx::GridFileWrapper::getMD5
::std::string getMD5(const ::Ice::Current &=Ice::emptyCurrent) override
Definition: GridFileWrapper.cpp:58
memoryx::GridFileWrapper::GridFileWrapper
GridFileWrapper(const mongo::GridFile &gridFile, const std::shared_ptr< std::mutex > &accessGridFSFilesMutex)
Definition: GridFileWrapper.cpp:34
armarx::VariantType::Int
const VariantTypeId Int
Definition: Variant.h:917
memoryx::GridFileWrapper::getFilename
::std::string getFilename(const ::Ice::Current &=Ice::emptyCurrent) override
Definition: GridFileWrapper.cpp:52
Logging.h
Exception.h