Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// Copyright 2019 Microsoft Corporation. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef RANDOM_ACCESS_STREAM_H
#define RANDOM_ACCESS_STREAM_H
 
#include <wrl.h>
#include <wrl/client.h>
 
#include <windows.storage.streams.h>
#include <robuffer.h>
 
#include <istream>
 
namespace WinMLTest {
 
struct BufferBackedRandomAccessStreamReadAsync
  : public Microsoft::WRL::RuntimeClass<
      Microsoft::WRL::RuntimeClassFlags<Microsoft::WRL::WinRtClassicComMix | Microsoft::WRL::InhibitRoOriginateError>,
      __FIAsyncOperationWithProgress_2_Windows__CStorage__CStreams__CIBuffer_UINT32,
      ABI::Windows::Foundation::IAsyncInfo> {
  InspectableClass(L"WinMLTest.BufferBackedRandomAccessStreamReadAsync", BaseTrust)
 
    Microsoft::WRL::ComPtr<ABI::Windows::Storage::Streams::IBuffer> buffer_;
 
  Microsoft::WRL::ComPtr<ABI::Windows::Foundation::IAsyncOperationWithProgressCompletedHandler<
    ABI::Windows::Storage::Streams::IBuffer*,
    UINT32>>
    completed_handler_;
  Microsoft::WRL::ComPtr<
    ABI::Windows::Foundation::IAsyncOperationProgressHandler<ABI::Windows::Storage::Streams::IBuffer*, UINT32>>
    progress_handler_;
 
  AsyncStatus status_ = AsyncStatus::Started;
 
 public:
  virtual /* [propget] */ HRESULT STDMETHODCALLTYPE get_Id(
    /* [retval][out] */ __RPC__out unsigned __int32* id
  ) override {
    *id = 0;  // Do we need to implement this?
    return S_OK;
  }
 
  virtual /* [propget] */ HRESULT STDMETHODCALLTYPE get_Status(
    /* [retval][out] */ __RPC__out AsyncStatus* status
  ) override {
    *status = status_;
    return S_OK;
  }
 
  virtual /* [propget] */ HRESULT STDMETHODCALLTYPE get_ErrorCode(
    /* [retval][out] */ __RPC__out HRESULT* /*errorCode*/
  ) override {
    return E_NOTIMPL;
  }
 
  virtual HRESULT STDMETHODCALLTYPE Cancel(void) override { return E_NOTIMPL; }
 
  virtual HRESULT STDMETHODCALLTYPE Close(void) override { return E_NOTIMPL; }
 
  HRESULT SetBuffer(ABI::Windows::Storage::Streams::IBuffer* buffer) {
    buffer_ = buffer;
    status_ = AsyncStatus::Completed;
    if (buffer_ != nullptr) {
      if (completed_handler_ != nullptr) {
        completed_handler_->Invoke(this, ABI::Windows::Foundation::AsyncStatus::Completed);
      }
    }
    return S_OK;
  }
 
  virtual HRESULT STDMETHODCALLTYPE put_Progress(
    ABI::Windows::Foundation::IAsyncOperationProgressHandler<ABI::Windows::Storage::Streams::IBuffer*, UINT32>* handler
  ) override {
    progress_handler_ = handler;
    return S_OK;
  }
 
  virtual HRESULT STDMETHODCALLTYPE get_Progress(
    ABI::Windows::Foundation::IAsyncOperationProgressHandler<ABI::Windows::Storage::Streams::IBuffer*, UINT32>** handler
  ) override {
    progress_handler_.CopyTo(handler);
    return S_OK;
  }
 
  virtual HRESULT STDMETHODCALLTYPE put_Completed(
    ABI::Windows::Foundation::
      IAsyncOperationWithProgressCompletedHandler<ABI::Windows::Storage::Streams::IBuffer*, UINT32>* handler
  ) override {
    completed_handler_ = handler;
    return S_OK;
  }
 
  virtual HRESULT STDMETHODCALLTYPE get_Completed(
    ABI::Windows::Foundation::
      IAsyncOperationWithProgressCompletedHandler<ABI::Windows::Storage::Streams::IBuffer*, UINT32>** handler
  ) override {
    completed_handler_.CopyTo(handler);
    return S_OK;
  }
 
  virtual HRESULT STDMETHODCALLTYPE GetResults(ABI::Windows::Storage::Streams::IBuffer** results) override {
    if (buffer_ == nullptr) {
      return E_FAIL;
    }
 
    buffer_.CopyTo(results);
    return S_OK;
  }
};
 
struct RandomAccessStream
  : public Microsoft::WRL::RuntimeClass<
      Microsoft::WRL::RuntimeClassFlags<Microsoft::WRL::WinRtClassicComMix | Microsoft::WRL::InhibitRoOriginateError>,
      ABI::Windows::Storage::Streams::IRandomAccessStreamWithContentType,
      ABI::Windows::Storage::Streams::IContentTypeProvider,
      ABI::Windows::Storage::Streams::IRandomAccessStream,
      ABI::Windows::Storage::Streams::IInputStream,
      ABI::Windows::Storage::Streams::IOutputStream,
      ABI::Windows::Foundation::IClosable> {
 InspectableClass(L"WinMLTest.RandomAccessStream", BaseTrust)
 
   private : Microsoft::WRL::ComPtr<ABI::Windows::Storage::Streams::IBuffer> buffer_ = nullptr;
  UINT64 position_ = 0;
 
 public:
  HRESULT RuntimeClassInitialize(ABI::Windows::Storage::Streams::IBuffer* buffer) {
    buffer_ = buffer;
    position_ = 0;
    return S_OK;
  }
 
  HRESULT RuntimeClassInitialize(ABI::Windows::Storage::Streams::IBuffer* buffer, UINT64 position) {
    buffer_ = buffer;
    position_ = position;
    return S_OK;
  }
 
  // Content Provider
 
  /* [propget] */ virtual HRESULT STDMETHODCALLTYPE get_ContentType(
    /* [retval, out] */ __RPC__deref_out_opt HSTRING* value
  ) override {
    return WindowsCreateString(nullptr, 0, value);
  }
 
  // IRandomAccessStream
 
  /* [propget] */ virtual HRESULT STDMETHODCALLTYPE get_Size(
    /* [retval, out] */ __RPC__out UINT64* value
  ) override {
    *value = 0;
    uint32_t length;
    buffer_->get_Length(&length);
    *value = static_cast<uint64_t>(length);
    return S_OK;
  }
 
  /* [propput] */ virtual HRESULT STDMETHODCALLTYPE put_Size(
    /* [in] */ UINT64 /*value*/
  ) override {
    return E_NOTIMPL;
  }
 
  virtual HRESULT STDMETHODCALLTYPE GetInputStreamAt(
    /* [in] */ UINT64 position,
    /* [retval, out] */ __RPC__deref_out_opt ABI::Windows::Storage::Streams::IInputStream** stream
  ) override {
    return Microsoft::WRL::MakeAndInitialize<RandomAccessStream>(stream, buffer_.Get(), position);
  }
 
  virtual HRESULT STDMETHODCALLTYPE GetOutputStreamAt(
    /* [in] */ UINT64 /*position*/,
    /* [retval, out] */ __RPC__deref_out_opt ABI::Windows::Storage::Streams::IOutputStream** /*stream*/
  ) override {
    return E_NOTIMPL;
  }
 
  /* [propget] */ virtual HRESULT STDMETHODCALLTYPE get_Position(
    /* [retval, out] */ __RPC__out UINT64* value
  ) override {
    *value = position_;
    return S_OK;
  }
 
  virtual HRESULT STDMETHODCALLTYPE Seek(
    /* [in] */ UINT64 position
  ) override {
    position_ = position;
    return S_OK;
  }
 
  virtual HRESULT STDMETHODCALLTYPE CloneStream(
    /* [retval, out] */ __RPC__deref_out_opt ABI::Windows::Storage::Streams::IRandomAccessStream** stream
  ) override {
    return Microsoft::WRL::MakeAndInitialize<RandomAccessStream>(stream, buffer_.Get(), 0);
  }
 
  /* [propget] */ virtual HRESULT STDMETHODCALLTYPE get_CanRead(
    /* [retval, out] */ __RPC__out::boolean* value
  ) override {
    UINT32 length;
    buffer_->get_Length(&length);
    *value = buffer_ != nullptr && position_ < static_cast<UINT64>(length);
    return S_OK;
  }
 
  /* [propget] */ virtual HRESULT STDMETHODCALLTYPE get_CanWrite(
    /* [retval, out] */ __RPC__out::boolean* value
  ) override {
    *value = false;
    return S_OK;
  }
 
  // IInputStream
  virtual HRESULT STDMETHODCALLTYPE ReadAsync(
    /* [in] */ __RPC__in_opt ABI::Windows::Storage::Streams::IBuffer* buffer,
    /* [in] */ UINT32 count,
    /* [in] */ ABI::Windows::Storage::Streams::InputStreamOptions /*options*/,
    /* [retval, out] */
    __RPC__deref_out_opt __FIAsyncOperationWithProgress_2_Windows__CStorage__CStreams__CIBuffer_UINT32** operation
  ) override {
    auto read_async = Microsoft::WRL::Make<BufferBackedRandomAccessStreamReadAsync>();
    read_async.CopyTo(operation);
 
    // perform the "async work" which is actually synchronous atm
    Microsoft::WRL::ComPtr<ABI::Windows::Storage::Streams::IBuffer> spBuffer = buffer;
    Microsoft::WRL::ComPtr<Windows::Storage::Streams::IBufferByteAccess> out_buffer_byte_access;
    spBuffer.As<Windows::Storage::Streams::IBufferByteAccess>(&out_buffer_byte_access);
    byte* out_bytes = nullptr;
    out_buffer_byte_access->Buffer(&out_bytes);
 
    Microsoft::WRL::ComPtr<Windows::Storage::Streams::IBufferByteAccess> in_buffer_byte_access;
    buffer_.As<Windows::Storage::Streams::IBufferByteAccess>(&in_buffer_byte_access);
    byte* in_bytes = nullptr;
    in_buffer_byte_access->Buffer(&in_bytes);
 
    memcpy(out_bytes, in_bytes + static_cast<uint32_t>(position_), count);
 
    read_async->SetBuffer(buffer);
 
    return S_OK;
  }
 
  // IOutputStream
  virtual HRESULT STDMETHODCALLTYPE WriteAsync(
    /* [in] */ __RPC__in_opt ABI::Windows::Storage::Streams::IBuffer* /*buffer*/,
    /* [retval, out] */ __RPC__deref_out_opt __FIAsyncOperationWithProgress_2_UINT32_UINT32** /*operation*/
  ) override {
    return E_NOTIMPL;
  }
 
  virtual HRESULT STDMETHODCALLTYPE FlushAsync(
    /* [retval, out] */ __RPC__deref_out_opt __FIAsyncOperation_1_boolean** /*operation*/
  ) override {
    return E_NOTIMPL;
  }
 
  // IClosable
  virtual HRESULT STDMETHODCALLTYPE Close(void) override {
    buffer_ = nullptr;
    return S_OK;
  }
};
 
struct BufferBackedRandomAccessStreamReferenceOpenReadAsync
  : public Microsoft::WRL::RuntimeClass<
      Microsoft::WRL::RuntimeClassFlags<Microsoft::WRL::WinRtClassicComMix | Microsoft::WRL::InhibitRoOriginateError>,
      __FIAsyncOperation_1_Windows__CStorage__CStreams__CIRandomAccessStreamWithContentType,
      ABI::Windows::Foundation::IAsyncInfo> {
 InspectableClass(L"WinMLTest.BufferBackedRandomAccessStreamReferenceOpenReadAsync", BaseTrust) public
   : Microsoft::WRL::ComPtr<ABI::Windows::Storage::Streams::IRandomAccessStreamWithContentType> ras_;
  Microsoft::WRL::ComPtr<ABI::Windows::Foundation::IAsyncOperationCompletedHandler<
    ABI::Windows::Storage::Streams::IRandomAccessStreamWithContentType*>>
    completed_handler_;
  AsyncStatus status_ = AsyncStatus::Started;
 
  HRESULT SetRandomAccessStream(ABI::Windows::Storage::Streams::IRandomAccessStreamWithContentType* ras) {
    ras_ = ras;
    status_ = AsyncStatus::Completed;
    if (ras_ != nullptr) {
      if (completed_handler_ != nullptr) {
        completed_handler_->Invoke(this, status_);
      }
    }
    return S_OK;
  }
 
  virtual /* [propget] */ HRESULT STDMETHODCALLTYPE get_Id(
    /* [retval][out] */ __RPC__out unsigned __int32* id
  ) override {
    *id = 0;  // Do we need to implement this?
    return S_OK;
  }
 
  virtual /* [propget] */ HRESULT STDMETHODCALLTYPE get_Status(
    /* [retval][out] */ __RPC__out AsyncStatus* status
  ) override {
    *status = status_;
    return S_OK;
  }
 
  virtual /* [propget] */ HRESULT STDMETHODCALLTYPE get_ErrorCode(
    /* [retval][out] */ __RPC__out HRESULT* /*errorCode*/
  ) override {
    return E_NOTIMPL;
  }
 
  virtual HRESULT STDMETHODCALLTYPE Cancel(void) override { return E_NOTIMPL; }
 
  virtual HRESULT STDMETHODCALLTYPE Close(void) override { return E_NOTIMPL; }
 
  virtual HRESULT STDMETHODCALLTYPE put_Completed(
    ABI::Windows::Foundation::IAsyncOperationCompletedHandler<
      ABI::Windows::Storage::Streams::IRandomAccessStreamWithContentType*>* handler
  ) override {
    completed_handler_ = handler;
    return S_OK;
  }
 
  virtual HRESULT STDMETHODCALLTYPE get_Completed(
    ABI::Windows::Foundation::IAsyncOperationCompletedHandler<
      ABI::Windows::Storage::Streams::IRandomAccessStreamWithContentType*>** handler
  ) override {
    completed_handler_.CopyTo(handler);
    return S_OK;
  }
 
  virtual HRESULT STDMETHODCALLTYPE
  GetResults(ABI::Windows::Storage::Streams::IRandomAccessStreamWithContentType** results) override {
    if (ras_ == nullptr) {
      return E_FAIL;
    }
    ras_.CopyTo(results);
    return S_OK;
  }
};
 
struct BufferBackedRandomAccessStreamReference
  : public Microsoft::WRL::RuntimeClass<
      Microsoft::WRL::RuntimeClassFlags<Microsoft::WRL::WinRtClassicComMix | Microsoft::WRL::InhibitRoOriginateError>,
      ABI::Windows::Storage::Streams::IRandomAccessStreamReference> {
  InspectableClass(L"WinMLTest.BufferBackedRandomAccessStreamReference", BaseTrust)
 
    Microsoft::WRL::ComPtr<ABI::Windows::Storage::Streams::IBuffer> buffer_ = nullptr;
 
 public:
  HRESULT RuntimeClassInitialize(ABI::Windows::Storage::Streams::IBuffer* buffer) {
    buffer_ = buffer;
    return S_OK;
  }
 
  virtual HRESULT STDMETHODCALLTYPE OpenReadAsync(
    /* [retval, out] */
    __RPC__deref_out_opt __FIAsyncOperation_1_Windows__CStorage__CStreams__CIRandomAccessStreamWithContentType**
      operation
  ) override {
    auto open_read_async = Microsoft::WRL::Make<BufferBackedRandomAccessStreamReferenceOpenReadAsync>();
    open_read_async.CopyTo(operation);
 
    Microsoft::WRL::ComPtr<RandomAccessStream> ras;
    Microsoft::WRL::MakeAndInitialize<RandomAccessStream>(&ras, buffer_.Get());
 
    Microsoft::WRL::ComPtr<ABI::Windows::Storage::Streams::IRandomAccessStreamWithContentType> ras_interface = nullptr;
    ras.As<ABI::Windows::Storage::Streams::IRandomAccessStreamWithContentType>(&ras_interface);
 
    open_read_async.Get()->SetRandomAccessStream(ras_interface.Get());
    return S_OK;
  }
};
 
}  // namespace WinMLTest
 
#endif  // RANDOM_ACCESS_STREAM_H