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
crate::ix!();

#[inline] pub fn get_string_from_blob<'a>(blob: *mut Blob) -> &'a String {
    
    todo!();
    /*
        if (blob->template IsType<string>()) {
        return blob->template Get<string>();
      } else if (blob->template IsType<Tensor>()) {
        return *blob->template Get<Tensor>().template data<string>();
      } else {
        CAFFE_THROW("Unsupported Blob type");
      }
    */
}

///----------------------------------
pub struct BlobsQueueDBCursor {
    queue:             Arc<BlobsQueue>,
    key_blob_index:    i32,
    value_blob_index:  i32,
    timeout_secs:      f32,
    inited:            bool,
    key:               String,
    value:             String,
    valid:             bool,
}

impl BlobsQueueDBCursor {

    pub fn new(
        queue:            Arc<BlobsQueue>,
        key_blob_index:   i32,
        value_blob_index: i32,
        timeout_secs:     f32) -> Self {
    
        todo!();
        /*
            : queue_(queue),
            key_blob_index_(key_blob_index),
            value_blob_index_(value_blob_index),
            timeout_secs_(timeout_secs),
            inited_(false),
            valid_(false) 

        LOG(INFO) << "BlobsQueueDBCursor constructed";
        CAFFE_ENFORCE(queue_ != nullptr, "queue is null");
        CAFFE_ENFORCE(value_blob_index_ >= 0, "value_blob_index < 0");
        */
    }
}

impl Cursor for BlobsQueueDBCursor {
    
    #[inline] fn seek(&mut self, unused: &String)  {
        
        todo!();
        /*
            CAFFE_THROW("Seek is not supported.");
        */
    }
    
    #[inline] fn supports_seek(&mut self) -> bool {
        
        todo!();
        /*
            return false;
        */
    }
    
    #[inline] fn seek_to_first(&mut self)  {
        
        todo!();
        /*
            // not applicable
        */
    }
    
    #[inline] fn next(&mut self)  {
        
        todo!();
        /*
            unique_ptr<Blob> blob = make_unique<Blob>();
        vector<Blob*> blob_vector{blob.get()};
        auto success = queue_->blockingRead(blob_vector, timeout_secs_);
        if (!success) {
          LOG(ERROR) << "Timed out reading from BlobsQueue or it is closed";
          valid_ = false;
          return;
        }

        if (key_blob_index_ >= 0) {
          key_ = GetStringFromBlob(blob_vector[key_blob_index_]);
        }
        value_ = GetStringFromBlob(blob_vector[value_blob_index_]);
        valid_ = true;
        */
    }
    
    #[inline] fn key(&mut self) -> String {
        
        todo!();
        /*
            if (!inited_) {
          Next();
          inited_ = true;
        }
        return key_;
        */
    }
    
    #[inline] fn value(&mut self) -> String {
        
        todo!();
        /*
            if (!inited_) {
          Next();
          inited_ = true;
        }
        return value_;
        */
    }
    
    #[inline] fn valid(&mut self) -> bool {
        
        todo!();
        /*
            return valid_;
        */
    }
}

///---------------------------
pub struct BlobsQueueDB {
    queue:             Arc<BlobsQueue>,
    key_blob_index:    i32,
    value_blob_index:  i32,
    timeout_secs:      f32,
}

impl BlobsQueueDB {

    pub fn new(
        source:           &String,
        mode:             DatabaseMode,
        queue:            Arc<BlobsQueue>,
        key_blob_index:   Option<i32>,
        value_blob_index: Option<i32>,
        timeout_secs:     Option<f32>) -> Self 
    {
        let key_blob_index:   i32 = key_blob_index.unwrap_or(-1);
        let value_blob_index: i32 = value_blob_index.unwrap_or(0);
        let timeout_secs:     f32 = timeout_secs.unwrap_or(0.0);

        todo!();
        /*
            : DB(source, mode),
            queue_(queue),
            key_blob_index_(key_blob_index),
            value_blob_index_(value_blob_index),
            timeout_secs_(timeout_secs) 

        LOG(INFO) << "BlobsQueueDB constructed";
        */
    }
}

impl DB for BlobsQueueDB {
    
    #[inline] fn close(&mut self)  {
        
        todo!();
        /*
        
        */
    }
    
    #[inline] fn new_cursor(&mut self) -> Box<dyn Cursor> {
        
        todo!();
        /*
            return make_unique<BlobsQueueDBCursor>(
            queue_, key_blob_index_, value_blob_index_, timeout_secs_);
        */
    }
    
    #[inline] fn new_transaction(&mut self) -> Box<dyn Transaction> {
        
        todo!();
        /*
            CAFFE_THROW("Not implemented.");
        */
    }
}

impl Drop for BlobsQueueDB {
    fn drop(&mut self) {
        todo!();
        /* 
        Close();
       */
    }
}

/**
  | Create a DBReader from a BlobsQueue
  |
  */
pub struct CreateBlobsQueueDBOp {
    storage: OperatorStorage,
    context: CPUContext,
}

num_inputs!{CreateBlobsQueueDB, 1}

num_outputs!{CreateBlobsQueueDB, 1}

inputs!{CreateBlobsQueueDB, 
    0 => ("queue", "The shared pointer to a queue containing Blobs.")
}

outputs!{CreateBlobsQueueDB, 
    0 => ("reader", "The DBReader for the given BlobsQueue")
}

args!{CreateBlobsQueueDB, 
    0 => ("key_blob_index",    "(default: -1 (no key)) index of blob for DB key in the BlobsQueue."),
    1 => ("value_blob_index",  "(default: 0) index of blob for DB value in the BlobsQueue."),
    2 => ("timeout_secs",      "(default: 0.0 (no timeout)) Timeout in seconds for reading from the BlobsQueue.")
}

should_not_do_gradient!{CreateBlobsQueueDB}

impl CreateBlobsQueueDBOp {

    pub fn new(operator_def: &OperatorDef, ws: *mut Workspace) -> Self {
    
        todo!();
        /*
            : Operator<CPUContext>(operator_def, ws)
        */
    }
    
    #[inline] pub fn run_on_device(&mut self) -> bool {
        
        todo!();
        /*
            std::unique_ptr<db::DB> db = std::make_unique<BlobsQueueDB>(
            "",
            db::READ,
            OperatorStorage::Input<std::shared_ptr<BlobsQueue>>(0),
            OperatorStorage::template GetSingleArgument<int>("key_blob_index", -1),
            OperatorStorage::template GetSingleArgument<int>("value_blob_index", 0),
            OperatorStorage::template GetSingleArgument<float>("timeout_secs", 0.0));
        OperatorStorage::Output<db::DBReader>(0)->Open(std::move(db), 1, 0);
        return true;
        */
    }
}

register_cpu_operator!{CreateBlobsQueueDB, CreateBlobsQueueDBOp<CPUContext>}

#[cfg(caffe2_use_mkldnn)]
register_ideep_operator!{
    CreateBlobsQueueDB,
    IDEEPFallbackOp<CreateBlobsQueueDBOp<CPUContext>, SkipIndices<0>>
}