vineyard 0.18.1

Vineyard Rust SDK: core library
Documentation
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
// Copyright 2020-2023 Alibaba Group Holding Limited.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::backtrace::{Backtrace, BacktraceStatus};
use std::env::VarError as EnvVarError;
use std::io::Error as IOError;
use std::num::{ParseFloatError, ParseIntError, TryFromIntError};
use std::sync::PoisonError;

use num_derive::{FromPrimitive, ToPrimitive};
use serde_json::Error as JSONError;

use super::uuid::ObjectID;

#[derive(Debug, Clone, Default, PartialEq, Eq, FromPrimitive, ToPrimitive)]
pub enum StatusCode {
    #[default]
    OK = 0,
    Invalid = 1,
    KeyError = 2,
    TypeError = 3,
    IOError = 4,
    EndOfFile = 5,
    NotImplemented = 6,
    AssertionFailed = 7,
    UserInputError = 8,

    ObjectExists = 11,
    ObjectNotExists = 12,
    ObjectSealed = 13,
    ObjectNotSealed = 14,
    ObjectIsBlob = 15,
    ObjectTypeError = 16,
    ObjectSpilled = 17,
    ObjectNotSpilled = 18,

    MetaTreeInvalid = 21,
    MetaTreeTypeInvalid = 22,
    MetaTreeTypeNotExists = 23,
    MetaTreeNameInvalid = 24,
    MetaTreeNameNotExists = 25,
    MetaTreeLinkInvalid = 26,
    MetaTreeSubtreeNotExists = 27,

    VineyardServerNotReady = 31,
    ArrowError = 32,
    ConnectionFailed = 33,
    ConnectionError = 34,
    EtcdError = 35,
    AlreadyStopped = 36,
    RedisError = 37,

    NotEnoughMemory = 41,
    StreamDrained = 42,
    StreamFailed = 43,
    InvalidStreamState = 44,
    StreamOpened = 45,

    GlobalObjectInvalid = 51,

    UnknownError = 255,
}

pub struct VineyardError {
    pub code: StatusCode,
    pub message: String,
    pub backtrace: Backtrace,
}

impl std::error::Error for VineyardError {}

impl std::fmt::Debug for VineyardError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if self.backtrace.status() == BacktraceStatus::Captured {
            write!(
                f,
                "{:?}: {}\nBacktrace: {:?}",
                self.code, self.message, self.backtrace
            )
        } else {
            write!(f, "{:?}: {}", self.code, self.message)
        }
    }
}

impl std::fmt::Display for VineyardError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Vineyard error {:?}: {}", self.code, self.message)
    }
}

impl From<IOError> for VineyardError {
    fn from(error: IOError) -> Self {
        VineyardError::new(StatusCode::IOError, format!("internal io error: {}", error))
    }
}

impl From<EnvVarError> for VineyardError {
    fn from(error: EnvVarError) -> Self {
        VineyardError::new(StatusCode::IOError, format!("env var error: {}", error))
    }
}

impl From<ParseIntError> for VineyardError {
    fn from(error: ParseIntError) -> Self {
        VineyardError::new(StatusCode::IOError, format!("parse int error: {}", error))
    }
}

impl From<ParseFloatError> for VineyardError {
    fn from(error: ParseFloatError) -> Self {
        VineyardError::new(StatusCode::IOError, format!("parse float error: {}", error))
    }
}

impl From<TryFromIntError> for VineyardError {
    fn from(error: TryFromIntError) -> Self {
        VineyardError::new(
            StatusCode::IOError,
            format!("try from int error: {}", error),
        )
    }
}

impl<T> From<PoisonError<T>> for VineyardError {
    fn from(error: PoisonError<T>) -> Self {
        VineyardError::new(StatusCode::Invalid, format!("lock poison error: {}", error))
    }
}

impl From<JSONError> for VineyardError {
    fn from(error: JSONError) -> Self {
        VineyardError::new(StatusCode::IOError, format!("json error: {}", error))
    }
}

pub type Result<T> = std::result::Result<T, VineyardError>;

impl VineyardError {
    pub fn new<T: Into<String>>(code: StatusCode, message: T) -> Self {
        VineyardError {
            code,
            message: message.into(),
            backtrace: Backtrace::capture(),
        }
    }

    pub fn wrap<T: Into<String>>(self, message: T) -> Self {
        if self.ok() {
            return self;
        }
        VineyardError::new(self.code, format!("{}: {}", self.message, message.into()))
    }

    pub fn invalid<T: Into<String>>(message: T) -> Self {
        VineyardError::new(StatusCode::Invalid, message.into())
    }

    pub fn key_error<T: Into<String>>(message: T) -> Self {
        VineyardError::new(StatusCode::KeyError, message)
    }

    pub fn type_error<T: Into<String>>(message: T) -> Self {
        VineyardError::new(StatusCode::TypeError, message)
    }

    pub fn io_error<T: Into<String>>(message: T) -> Self {
        VineyardError::new(StatusCode::IOError, message)
    }

    pub fn end_of_file<T: Into<String>>(message: T) -> Self {
        VineyardError::new(StatusCode::EndOfFile, message)
    }

    pub fn not_implemented<T: Into<String>>(message: T) -> Self {
        VineyardError::new(StatusCode::NotImplemented, message)
    }

    pub fn assertion_failed<T: Into<String>>(message: T) -> Self {
        VineyardError::new(StatusCode::AssertionFailed, message)
    }

    pub fn user_input_error<T: Into<String>>(message: T) -> Self {
        VineyardError::new(StatusCode::UserInputError, message)
    }

    pub fn object_exists<T: Into<String>>(message: T) -> Self {
        VineyardError::new(StatusCode::ObjectExists, message)
    }

    pub fn object_not_exists<T: Into<String>>(message: T) -> Self {
        VineyardError::new(StatusCode::ObjectNotExists, message)
    }

    pub fn object_sealed<T: Into<String>>(message: T) -> Self {
        VineyardError::new(StatusCode::ObjectSealed, message)
    }

    pub fn object_not_sealed<T: Into<String>>(message: T) -> Self {
        VineyardError::new(StatusCode::ObjectNotSealed, message)
    }

    pub fn object_is_blob<T: Into<String>>(message: T) -> Self {
        VineyardError::new(StatusCode::ObjectIsBlob, message)
    }

    pub fn object_type_error<U: Into<String>, V: Into<String>>(expected: U, actual: V) -> Self {
        VineyardError::new(
            StatusCode::ObjectTypeError,
            format!(
                "expect typename '{}', but got '{}'",
                expected.into(),
                actual.into()
            ),
        )
    }

    pub fn object_spilled(object_id: ObjectID) -> Self {
        VineyardError::new(
            StatusCode::ObjectSpilled,
            format!("object '{}' has already been spilled", object_id),
        )
    }

    pub fn object_not_spilled(object_id: ObjectID) -> Self {
        VineyardError::new(
            StatusCode::ObjectNotSpilled,
            format!("object '{}' hasn't been spilled yet", object_id),
        )
    }

    pub fn meta_tree_invalid<T: Into<String>>(message: T) -> Self {
        VineyardError::new(StatusCode::MetaTreeInvalid, message)
    }

    pub fn meta_tree_type_invalid<T: Into<String>>(message: T) -> Self {
        VineyardError::new(StatusCode::MetaTreeTypeInvalid, message)
    }

    pub fn meta_tree_type_not_exists<T: Into<String>>(message: T) -> Self {
        VineyardError::new(StatusCode::MetaTreeTypeNotExists, message)
    }

    pub fn meta_tree_name_invalid<T: Into<String>>(message: T) -> Self {
        VineyardError::new(StatusCode::MetaTreeNameInvalid, message)
    }

    pub fn meta_tree_name_not_exists<T: Into<String>>(message: T) -> Self {
        VineyardError::new(StatusCode::MetaTreeNameNotExists, message)
    }

    pub fn meta_tree_link_invalid<T: Into<String>>(message: T) -> Self {
        VineyardError::new(StatusCode::MetaTreeLinkInvalid, message)
    }

    pub fn meta_tree_subtree_not_exists<T: Into<String>>(message: T) -> Self {
        VineyardError::new(StatusCode::MetaTreeSubtreeNotExists, message)
    }

    pub fn vineyard_server_not_ready<T: Into<String>>(message: T) -> Self {
        VineyardError::new(StatusCode::VineyardServerNotReady, message)
    }

    pub fn arrow_error<T: Into<String>>(message: T) -> Self {
        VineyardError::new(StatusCode::ArrowError, message)
    }

    pub fn connection_failed<T: Into<String>>(message: T) -> Self {
        VineyardError::new(StatusCode::ConnectionFailed, message)
    }

    pub fn etcd_error<T: Into<String>>(message: T) -> Self {
        VineyardError::new(StatusCode::EtcdError, message)
    }

    pub fn redis_error<T: Into<String>>(message: T) -> Self {
        VineyardError::new(StatusCode::RedisError, message)
    }

    pub fn already_stopped<T: Into<String>>(message: T) -> Self {
        VineyardError::new(StatusCode::AlreadyStopped, message)
    }

    pub fn not_enough_memory<T: Into<String>>(message: T) -> Self {
        VineyardError::new(StatusCode::NotEnoughMemory, message)
    }

    pub fn stream_drained<T: Into<String>>(message: T) -> Self {
        VineyardError::new(StatusCode::StreamDrained, message)
    }

    pub fn stream_failed<T: Into<String>>(message: T) -> Self {
        VineyardError::new(StatusCode::StreamFailed, message)
    }

    pub fn invalid_stream_state<T: Into<String>>(message: T) -> Self {
        VineyardError::new(StatusCode::InvalidStreamState, message)
    }

    pub fn stream_opened<T: Into<String>>(message: T) -> Self {
        VineyardError::new(StatusCode::StreamOpened, message)
    }

    pub fn global_object_invalid<T: Into<String>>(message: T) -> Self {
        VineyardError::new(StatusCode::GlobalObjectInvalid, message)
    }

    pub fn unknown_error<T: Into<String>>(message: T) -> Self {
        VineyardError::new(StatusCode::UnknownError, message)
    }

    pub fn ok(&self) -> bool {
        return self.code == StatusCode::OK;
    }

    pub fn is_invalid(&self) -> bool {
        return self.code == StatusCode::Invalid;
    }

    pub fn is_key_error(&self) -> bool {
        return self.code == StatusCode::KeyError;
    }

    pub fn is_type_error(&self) -> bool {
        return self.code == StatusCode::TypeError;
    }

    pub fn is_io_error(&self) -> bool {
        return self.code == StatusCode::IOError;
    }

    pub fn is_end_of_file(&self) -> bool {
        return self.code == StatusCode::EndOfFile;
    }

    pub fn is_not_implemented(&self) -> bool {
        return self.code == StatusCode::NotImplemented;
    }

    pub fn is_assertion_failed(&self) -> bool {
        return self.code == StatusCode::AssertionFailed;
    }

    pub fn is_user_input_error(&self) -> bool {
        return self.code == StatusCode::UserInputError;
    }

    pub fn is_object_exists(&self) -> bool {
        return self.code == StatusCode::ObjectExists;
    }

    pub fn is_object_not_exists(&self) -> bool {
        return self.code == StatusCode::ObjectNotExists;
    }

    pub fn is_object_sealed(&self) -> bool {
        return self.code == StatusCode::ObjectSealed;
    }

    pub fn is_object_not_sealed(&self) -> bool {
        return self.code == StatusCode::ObjectNotSealed;
    }

    pub fn is_object_is_blob(&self) -> bool {
        return self.code == StatusCode::ObjectIsBlob;
    }

    pub fn is_object_type_error(&self) -> bool {
        return self.code == StatusCode::ObjectTypeError;
    }

    pub fn is_object_spilled(&self) -> bool {
        return self.code == StatusCode::ObjectSpilled;
    }

    pub fn is_object_not_spilled(&self) -> bool {
        return self.code == StatusCode::ObjectNotSpilled;
    }

    pub fn is_meta_tree_invalid(&self) -> bool {
        return self.code == StatusCode::MetaTreeInvalid
            || self.code == StatusCode::MetaTreeNameInvalid
            || self.code == StatusCode::MetaTreeTypeInvalid
            || self.code == StatusCode::MetaTreeLinkInvalid;
    }

    pub fn is_meta_tree_element_not_exists(&self) -> bool {
        return self.code == StatusCode::MetaTreeNameNotExists
            || self.code == StatusCode::MetaTreeTypeNotExists
            || self.code == StatusCode::MetaTreeSubtreeNotExists;
    }

    pub fn is_vineyard_server_not_ready(&self) -> bool {
        return self.code == StatusCode::VineyardServerNotReady;
    }

    pub fn is_arrow_error(&self) -> bool {
        return self.code == StatusCode::ArrowError;
    }

    pub fn is_connection_failed(&self) -> bool {
        return self.code == StatusCode::ConnectionFailed;
    }

    pub fn is_connection_error(&self) -> bool {
        return self.code == StatusCode::ConnectionError;
    }

    pub fn is_etcd_error(&self) -> bool {
        return self.code == StatusCode::EtcdError;
    }

    pub fn is_already_stopped(&self) -> bool {
        return self.code == StatusCode::AlreadyStopped;
    }

    pub fn is_not_enough_memory(&self) -> bool {
        return self.code == StatusCode::NotEnoughMemory;
    }

    pub fn is_stream_drained(&self) -> bool {
        return self.code == StatusCode::StreamDrained;
    }

    pub fn is_stream_failed(&self) -> bool {
        return self.code == StatusCode::StreamFailed;
    }

    pub fn is_invalid_stream_state(&self) -> bool {
        return self.code == StatusCode::InvalidStreamState;
    }

    pub fn is_stream_opened(&self) -> bool {
        return self.code == StatusCode::StreamOpened;
    }

    pub fn is_global_object_invalid(&self) -> bool {
        return self.code == StatusCode::GlobalObjectInvalid;
    }

    pub fn is_unknown_error(&self) -> bool {
        return self.code == StatusCode::UnknownError;
    }

    pub fn code(&self) -> &StatusCode {
        return &self.code;
    }

    pub fn message(&self) -> &String {
        return &self.message;
    }
}

pub fn vineyard_check_ok<T: std::fmt::Debug>(status: Result<T>) {
    if status.is_err() {
        panic!("Error occurs: {:?}.", status)
    }
}

pub fn vineyard_assert<T: Into<String>>(condition: bool, message: T) -> Result<()> {
    if !condition {
        return Err(VineyardError::assertion_failed(format!(
            "assertion failed: {}",
            message.into()
        )));
    }
    return Ok(());
}

pub fn vineyard_assert_typename<U: Into<String> + PartialEq<V>, V: Into<String>>(
    expect: U,
    actual: V,
) -> Result<()> {
    if expect != actual {
        return Err(VineyardError::object_type_error(
            expect.into(),
            actual.into(),
        ));
    }
    return Ok(());
}