IOManagerDms.cpp 9.64 KB
Newer Older
Jørgen Lind's avatar
Jørgen Lind committed
1
2
3
4
5
6
7
8
9
10
#include "IOManagerDms.h"

#include <future>
#include <condition_variable>

#include <cstdint>

#ifdef _MSC_VER
#pragma warning( disable : 4275 )
#endif
11

Jørgen Lind's avatar
Jørgen Lind committed
12
13
14
#include "IOManagerCurl.h"
#include "IORefreshToken.h"

Jørgen Lind's avatar
Jørgen Lind committed
15
16
17
namespace OpenVDS
{
  GetHeadRequestDms::GetHeadRequestDms(seismicdrive::SDGenericDataset &dataset, const std::string& id, const std::shared_ptr<TransferDownloadHandler>& handler)
18
    : RequestImpl(id)
Jørgen Lind's avatar
Jørgen Lind committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
    , m_dataset(dataset)
    , m_handler(handler)
  {
  }

  GetHeadRequestDms::~GetHeadRequestDms()
  {
  }


  ReadObjectInfoRequestDms::ReadObjectInfoRequestDms(seismicdrive::SDGenericDataset &dataset, const std::string& id, const std::shared_ptr<TransferDownloadHandler>& handler)
    : GetHeadRequestDms(dataset, id, handler)
  {
  }

  DownloadRequestDms::DownloadRequestDms(seismicdrive::SDGenericDataset &dataset, const std::string& id, const std::shared_ptr<TransferDownloadHandler>& handler)
    : GetHeadRequestDms(dataset, id, handler)
  {
  }

39
  template<typename T>
Jørgen Lind's avatar
Jørgen Lind committed
40
  static void run_request(const std::string& requestName, std::weak_ptr <T> weak_request, const IORange &range, std::vector<uint8_t>* data)
Jørgen Lind's avatar
Jørgen Lind committed
41
  {
Jørgen Lind's avatar
Jørgen Lind committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  }

  void ReadObjectInfoRequestDms::run(const std::string& requestName, std::weak_ptr<ReadObjectInfoRequestDms> weak_request, ThreadPool &threadPool)
  {
    m_job = threadPool.Enqueue([requestName, weak_request]() {
      auto request = weak_request.lock();
      if (!request)
        return;

      RequestStateHandler requestHandler(*request);

      if (requestHandler.isCancelledRequested())
      {
        return;
      }

Jørgen Lind's avatar
Jørgen Lind committed
58
      uint64_t size = 0;
Jørgen Lind's avatar
Jørgen Lind committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
      std::string created_date;
      try
      {
        size = request->m_dataset.getBlockSize(requestName);
        created_date = request->m_dataset.getCreatedDate();
      }
      catch (const seismicdrive::SDException& ex)
      {
        request->m_error.code = -1;
        request->m_error.string = ex.what();
      }
      catch (...)
      {
        request->m_error.code = -1;
        request->m_error.string = "Unknown exception in DMS upload";
      }

      if (request->m_error.code == 0)
      {
        request->m_handler->HandleObjectSize(size);
        request->m_handler->HandleObjectLastWriteTime(created_date);
      }
      request->m_handler->Completed(*request, request->m_error);
    });
  }
  void DownloadRequestDms::run(const std::string& requestName, const IORange& range, std::weak_ptr<DownloadRequestDms> weak_request, ThreadPool &threadPool)
  {
    m_job = threadPool.Enqueue([requestName, weak_request, range]() {
      auto request = weak_request.lock();
      if (!request)
        return;

      RequestStateHandler requestHandler(*request);

      if (requestHandler.isCancelledRequested())
      {
        return;
      }

      std::vector<uint8_t> data;
      try
100
101
      {
        if (range.end)
Jørgen Lind's avatar
Jørgen Lind committed
102
        {
Jørgen Lind's avatar
Jørgen Lind committed
103
104
          data.resize(range.end - range.start);
          request->m_dataset.readBlock(requestName, (char*)data.data(), range.start, data.size());
Jørgen Lind's avatar
Jørgen Lind committed
105
        }
106
107
        else
        {
Jørgen Lind's avatar
Jørgen Lind committed
108
109
110
111
112
113
114
115
116
117
118
119
          //char* read_data = nullptr;
          //std::size_t read_size = 0;
          //request->m_dataset.readBlock(requestName, &read_data, read_size);
          //data.resize(read_size);
          //memcpy(data.data(), read_data, data.size());
          //delete[] read_data;
          size_t read_size = size_t(request->m_dataset.getBlockSize(requestName));
          if (read_size)
          {
            data.resize(read_size);
            request->m_dataset.readBlock(requestName, (char*)data.data(), read_size);
          }
120
        }
Jørgen Lind's avatar
Jørgen Lind committed
121
      }
Jørgen Lind's avatar
Jørgen Lind committed
122
123
124
125
126
127
128
129
130
131
      catch (const seismicdrive::SDException& ex)
      {
        request->m_error.code = -1;
        request->m_error.string = ex.what();
      }
      catch (...)
      {
        request->m_error.code = -1;
        request->m_error.string = "Unknown exception in DMS upload";
      }
132

Jørgen Lind's avatar
Jørgen Lind committed
133
134
135
136
137
138
      if (request->m_error.code == 0)
      {
        request->m_handler->HandleObjectSize(data.size());
        request->m_handler->HandleData(std::move(data));
      }
      request->m_handler->Completed(*request, request->m_error);
Jørgen Lind's avatar
Jørgen Lind committed
139
140
141
142
    });
  }

  UploadRequestDms::UploadRequestDms(seismicdrive::SDGenericDataset &dataset, const std::string& id, std::function<void(const Request& request, const Error& error)> completedCallback)
143
    : RequestImpl(id)
Jørgen Lind's avatar
Jørgen Lind committed
144
145
146
147
148
149
150
151
152
153
154
155
    , m_dataset(dataset)
    , m_completedCallback(completedCallback)
  {
  }

  void UploadRequestDms::run(const std::string& requestName, const std::string& contentDispositionFilename, const std::string& contentType, const std::vector<std::pair<std::string, std::string>>& metadataHeader, std::shared_ptr<std::vector<uint8_t>> data, std::weak_ptr<UploadRequestDms> request, ThreadPool &threadPool)
  {
    m_job = threadPool.Enqueue([requestName, contentDispositionFilename, contentType, metadataHeader, data, request]() {
      auto request_ptr = request.lock();
      if (!request_ptr)
        return;

156
157
158
      RequestStateHandler requestHandler(*request_ptr);
      if (requestHandler.isCancelledRequested())
      {
Jørgen Lind's avatar
Jørgen Lind committed
159
        return;
160
      }
Jørgen Lind's avatar
Jørgen Lind committed
161
162
163

      try
      {
164
        request_ptr->m_dataset.writeBlock(requestName, (const char*)data->data(), data->size(), false);
Jørgen Lind's avatar
Jørgen Lind committed
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
      }
      catch (const seismicdrive::SDException& ex)
      {
        request_ptr->m_error.code = -1;
        request_ptr->m_error.string = ex.what();
      }
      catch (...)
      {
        request_ptr->m_error.code = -1;
        request_ptr->m_error.string = "Unknown exception in DMS upload";
      }

      if (request_ptr->m_completedCallback)
        request_ptr->m_completedCallback(*request_ptr, request_ptr->m_error);
    });
  }

Jørgen Lind's avatar
Jørgen Lind committed
182
183
184
185
186
187
  std::string IOManagerDms::AuthProviderCallback(const void* data)
  {
    IOManagerDms* iomanager = static_cast<IOManagerDms*>(const_cast<void*>(data));
    return iomanager->m_tokenRefresher->newToken();
  }

188
  IOManagerDms::IOManagerDms(const DMSOpenOptions& openOptions, IOManager::AccessPattern accessPattern, Error& error)
Jørgen Lind's avatar
Jørgen Lind committed
189
190
    : IOManager(openOptions.connectionType)
    , m_opened(false)
191
    , m_useFileNameForSingleFileDatasets(openOptions.useFileNameForSingleFileDatasets)
192
    , m_threadPool(16)
Jørgen Lind's avatar
Jørgen Lind committed
193
  {
194
    if (openOptions.datasetPath.size() && m_useFileNameForSingleFileDatasets)
195
196
197
198
199
200
201
202
203
204
205
    {
      auto it = openOptions.datasetPath.rfind('/');
      if (it == openOptions.datasetPath.size() - 1)
      {
        it = openOptions.datasetPath.rfind('/', 1);
      }
      if (it != std::string::npos)
      {
        m_filename = openOptions.datasetPath.substr(it+1);
      }
    }
206
207
208
209
    else
    {
      m_filename = "0";
    }
Jørgen Lind's avatar
Jørgen Lind committed
210
211


Jørgen Lind's avatar
Jørgen Lind committed
212
213
    try {
      m_sdManager.reset(new seismicdrive::SDManager(openOptions.sdAuthorityUrl, openOptions.sdApiKey, openOptions.logLevel));
Paal Kvamme's avatar
Paal Kvamme committed
214
215
216
217
218
      if (openOptions.authProviderCallback)
      {
        m_sdManager->setAuthProviderCallback(openOptions.authProviderCallback, openOptions.authProviderCallbackData);
      }
      else if (openOptions.authTokenUrl.size() && openOptions.clientId.size() && openOptions.refreshToken.size())
Jørgen Lind's avatar
Jørgen Lind committed
219
220
221
222
      {
        m_curlHandler.reset(new CurlHandler(error));
        if (error.code != 0)
          return;
223
        m_tokenRefresher.reset(new TokenRefresher(openOptions.authTokenUrl, openOptions.clientId, openOptions.clientSecret, openOptions.scopes, openOptions.refreshToken, *m_curlHandler, std::function<void(std::string&& new_token)>()));
Jørgen Lind's avatar
Jørgen Lind committed
224
225
226
227
228
229
230
        m_sdManager->setAuthProviderCallback(&AuthProviderCallback, this);
      }
      else
      {
        m_sdManager->setAuthProviderFromString(openOptions.sdToken);
      }

Jørgen Lind's avatar
Jørgen Lind committed
231
      m_dataset.reset(new seismicdrive::SDGenericDataset(m_sdManager.get(), openOptions.datasetPath, openOptions.logLevel != 0));
Jørgen Lind's avatar
Jørgen Lind committed
232
233

      seismicdrive::SDDatasetDisposition disposition = seismicdrive::SDDatasetDisposition::READ_ONLY;
234
      switch (accessPattern)
Jørgen Lind's avatar
Jørgen Lind committed
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
      {
      case IOManager::ReadOnly:
        disposition = seismicdrive::SDDatasetDisposition::READ_ONLY;
        break;
      case IOManager::ReadWrite:
        disposition = seismicdrive::SDDatasetDisposition::OVERWRITE;
        break;
      }
      m_dataset->open(disposition);
      m_opened = true;
    }
    catch (seismicdrive::SDException &exception)
    {
      error.string = exception.what();
      error.code = -1;
    }
Jørgen Lind's avatar
Jørgen Lind committed
251
252
253
254
255
    catch (std::exception &exception)
    {
      error.string = exception.what();
      error.code = -1;
    }
Jørgen Lind's avatar
Jørgen Lind committed
256
257
    catch (...)
    {
Jørgen Lind's avatar
Jørgen Lind committed
258
      error.string = "Unknown DMS exception.";
Jørgen Lind's avatar
Jørgen Lind committed
259
260
261
262
263
264
265
266
267
268
269
270
271
272
      error.code = -1;
    }
  }

  IOManagerDms::~IOManagerDms()
  {
    if (m_dataset && m_opened)
    {
      m_dataset->close();
    }
  }

  std::shared_ptr<Request> IOManagerDms::ReadObjectInfo(const std::string& objectName, std::shared_ptr<TransferDownloadHandler> handler)
  {
273
274
275
    std::string toRead = objectName.empty() ? m_filename : objectName;
    auto req = std::make_shared<ReadObjectInfoRequestDms>(*m_dataset, toRead, handler);
    req->run(toRead, req, m_threadPool);
Jørgen Lind's avatar
Jørgen Lind committed
276
277
278
    return req;
  }

279
  std::shared_ptr<Request> IOManagerDms::ReadObject(const std::string& objectName, std::shared_ptr<TransferDownloadHandler> handler, const IORange& range)
Jørgen Lind's avatar
Jørgen Lind committed
280
  {
281
282
283
    std::string toRead = objectName.empty() ? m_filename : objectName;
    auto req = std::make_shared<DownloadRequestDms>(*m_dataset, toRead, handler);
    req->run(toRead, range, req, m_threadPool);
Jørgen Lind's avatar
Jørgen Lind committed
284
285
286
287
288
289
290
291
292
293
294
    return req;
  }

  std::shared_ptr<Request> IOManagerDms::WriteObject(const std::string& requestName, const std::string& contentDispostionFilename, const std::string& contentType, const std::vector<std::pair<std::string, std::string>>& metadataHeader, std::shared_ptr<std::vector<uint8_t>> data, std::function<void(const Request& request, const Error& error)> completedCallback)
  {
    auto req = std::make_shared<UploadRequestDms>(*m_dataset, requestName, completedCallback);
    req->run(requestName, contentDispostionFilename, contentType, metadataHeader, data, req, m_threadPool);
    return req;
  }

}