ignite-rs 0.1.1

Apache Ignite thin client
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
use std::convert::TryFrom;

use crate::api::key_value::{
    CacheBoolResp, CacheDataObjectResp, CachePairsResp, CacheReq, CacheSizeResp,
};
use crate::cache::AtomicityMode::{Atomic, Transactional};
use crate::cache::CacheMode::{Local, Partitioned, Replicated};
use crate::cache::IndexType::{Fulltext, GeoSpatial, Sorted};
use crate::cache::PartitionLossPolicy::{
    Ignore, ReadOnlyAll, ReadOnlySafe, ReadWriteAll, ReadWriteSafe,
};
use crate::cache::RebalanceMode::Async;
use crate::cache::WriteSynchronizationMode::{FullAsync, FullSync, PrimarySync};
use crate::error::{IgniteError, IgniteResult};

use crate::api::OpCode;
use crate::connection::Connection;
use crate::{ReadableType, WritableType};
use std::marker::PhantomData;
use std::sync::Arc;

#[derive(Clone, Debug)]
pub enum AtomicityMode {
    Transactional = 0,
    Atomic = 1,
}

impl TryFrom<i32> for AtomicityMode {
    type Error = IgniteError;

    fn try_from(value: i32) -> Result<Self, Self::Error> {
        match value {
            0 => Ok(Transactional),
            1 => Ok(Atomic),
            _ => Err(IgniteError::from("Cannot read AtomicityMode")),
        }
    }
}

#[derive(Clone, Debug)]
pub enum CacheMode {
    Local = 0,
    Replicated = 1,
    Partitioned = 2,
}

impl TryFrom<i32> for CacheMode {
    type Error = IgniteError;

    fn try_from(value: i32) -> Result<Self, Self::Error> {
        match value {
            0 => Ok(Local),
            1 => Ok(Replicated),
            2 => Ok(Partitioned),
            _ => Err(IgniteError::from("Cannot read CacheMode")),
        }
    }
}

#[derive(Clone, Debug)]
pub enum PartitionLossPolicy {
    ReadOnlySafe = 0,
    ReadOnlyAll = 1,
    ReadWriteSafe = 2,
    ReadWriteAll = 3,
    Ignore = 4,
}

impl TryFrom<i32> for PartitionLossPolicy {
    type Error = IgniteError;

    fn try_from(value: i32) -> Result<Self, Self::Error> {
        match value {
            0 => Ok(ReadOnlySafe),
            1 => Ok(ReadOnlyAll),
            2 => Ok(ReadWriteSafe),
            3 => Ok(ReadWriteAll),
            4 => Ok(Ignore),
            _ => Err(IgniteError::from("Cannot read PartitionLossPolicy")),
        }
    }
}

#[derive(Clone, Debug)]
pub enum RebalanceMode {
    Sync = 0,
    Async = 1,
    None = 2,
}

impl TryFrom<i32> for RebalanceMode {
    type Error = IgniteError;

    fn try_from(value: i32) -> Result<Self, Self::Error> {
        match value {
            0 => Ok(RebalanceMode::Sync),
            1 => Ok(Async),
            2 => Ok(RebalanceMode::None),
            _ => Err(IgniteError::from("Cannot read RebalanceMode")),
        }
    }
}

#[derive(Clone, Debug)]
pub enum WriteSynchronizationMode {
    FullSync = 0,
    FullAsync = 1,
    PrimarySync = 2,
}

impl TryFrom<i32> for WriteSynchronizationMode {
    type Error = IgniteError;

    fn try_from(value: i32) -> Result<Self, Self::Error> {
        match value {
            0 => Ok(FullSync),
            1 => Ok(FullAsync),
            2 => Ok(PrimarySync),
            _ => Err(IgniteError::from("Cannot read WriteSynchronizationMode")),
        }
    }
}

#[derive(Clone, Debug)]
pub enum CachePeekMode {
    All = 0,
    Near = 1,
    Primary = 2,
    Backup = 3,
}

impl Into<u8> for CachePeekMode {
    fn into(self) -> u8 {
        self as u8
    }
}

#[derive(Clone, Debug)]
pub enum IndexType {
    Sorted = 0,
    Fulltext = 1,
    GeoSpatial = 2,
}

impl TryFrom<u8> for IndexType {
    type Error = IgniteError;

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        match value {
            0 => Ok(Sorted),
            1 => Ok(Fulltext),
            2 => Ok(GeoSpatial),
            _ => Err(IgniteError::from("Cannot read IndexType")),
        }
    }
}

#[derive(Clone, Debug)]
pub struct CacheConfiguration {
    pub atomicity_mode: AtomicityMode,
    pub num_backup: i32,
    pub cache_mode: CacheMode,
    pub copy_on_read: bool,
    pub data_region_name: Option<String>,
    pub eager_ttl: bool,
    pub statistics_enabled: bool,
    pub group_name: Option<String>,
    pub default_lock_timeout_ms: i64,
    pub max_concurrent_async_operations: i32,
    pub max_query_iterators: i32,
    pub name: String,
    pub onheap_cache_enabled: bool,
    pub partition_loss_policy: PartitionLossPolicy,
    pub query_detail_metrics_size: i32,
    pub query_parallelism: i32,
    pub read_from_backup: bool,
    pub rebalance_batch_size: i32,
    pub rebalance_batches_prefetch_count: i64,
    pub rebalance_delay_ms: i64,
    pub rebalance_mode: RebalanceMode,
    pub rebalance_order: i32,
    pub rebalance_throttle_ms: i64,
    pub rebalance_timeout_ms: i64,
    pub sql_escape_all: bool,
    pub sql_index_max_size: i32,
    pub sql_schema: Option<String>,
    pub write_synchronization_mode: WriteSynchronizationMode,
    pub cache_key_configurations: Option<Vec<CacheKeyConfiguration>>,
    pub query_entities: Option<Vec<QueryEntity>>,
}

impl CacheConfiguration {
    pub fn new(name: &str) -> CacheConfiguration {
        CacheConfiguration {
            name: name.to_owned(),
            ..Self::default()
        }
    }

    fn default() -> CacheConfiguration {
        CacheConfiguration {
            atomicity_mode: AtomicityMode::Atomic,
            num_backup: 0,
            cache_mode: CacheMode::Partitioned,
            copy_on_read: true,
            data_region_name: None,
            eager_ttl: true,
            statistics_enabled: true,
            group_name: None,
            default_lock_timeout_ms: 0,
            max_concurrent_async_operations: 500,
            max_query_iterators: 1024,
            name: String::new(),
            onheap_cache_enabled: false,
            partition_loss_policy: PartitionLossPolicy::Ignore,
            query_detail_metrics_size: 0,
            query_parallelism: 1,
            read_from_backup: true,
            rebalance_batch_size: 512 * 1024, //512K
            rebalance_batches_prefetch_count: 2,
            rebalance_delay_ms: 0,
            rebalance_mode: RebalanceMode::Async,
            rebalance_order: 0,
            rebalance_throttle_ms: 0,
            rebalance_timeout_ms: 10000, //1sec
            sql_escape_all: false,
            sql_index_max_size: -1,
            sql_schema: None,
            write_synchronization_mode: WriteSynchronizationMode::PrimarySync,
            cache_key_configurations: None,
            query_entities: None,
        }
    }
}

#[derive(Clone, Debug)]
pub struct CacheKeyConfiguration {
    pub type_name: String,
    pub affinity_key_field_name: String,
}

#[derive(Clone, Debug)]
pub struct QueryEntity {
    pub(crate) key_type: String,
    pub(crate) value_type: String,
    pub(crate) table: String,
    pub(crate) key_field: String,
    pub(crate) value_field: String,
    pub(crate) query_fields: Vec<QueryField>,
    pub(crate) field_aliases: Vec<(String, String)>,
    pub(crate) query_indexes: Vec<QueryIndex>,
    pub(crate) default_value: Option<String>, //TODO: find the issue where this field is listed
}

#[derive(Clone, Debug)]
pub struct QueryField {
    pub(crate) name: String,
    pub(crate) type_name: String,
    pub(crate) key_field: bool,
    pub(crate) not_null_constraint: bool,
}

#[derive(Clone, Debug)]
pub struct QueryIndex {
    pub(crate) index_name: String,
    pub(crate) index_type: IndexType,
    pub(crate) inline_size: i32,
    pub(crate) fields: Vec<(String, bool)>,
}

/// Ignite key-value cache. This cache is strongly typed and reading/writing some other
/// types leads to errors.
/// All caches created from the single IgniteClient shares the common TCP connection
pub struct Cache<K: WritableType + ReadableType, V: WritableType + ReadableType> {
    id: i32,
    pub _name: String,
    conn: Arc<Connection>,
    k_phantom: PhantomData<K>,
    v_phantom: PhantomData<V>,
}

impl<K: WritableType + ReadableType, V: WritableType + ReadableType> Cache<K, V> {
    pub(crate) fn new(id: i32, name: String, conn: Arc<Connection>) -> Cache<K, V> {
        Cache {
            id,
            _name: name,
            conn,
            k_phantom: PhantomData,
            v_phantom: PhantomData,
        }
    }

    pub fn get(&self, key: &K) -> IgniteResult<Option<V>> {
        self.conn
            .send_and_read(OpCode::CacheGet, CacheReq::Get::<K, V>(self.id, key))
            .map(|resp: CacheDataObjectResp<V>| resp.val)
    }

    pub fn get_all(&self, keys: &[K]) -> IgniteResult<Vec<(Option<K>, Option<V>)>> {
        self.conn
            .send_and_read(OpCode::CacheGetAll, CacheReq::GetAll::<K, V>(self.id, keys))
            .map(|resp: CachePairsResp<K, V>| resp.val)
    }

    pub fn put(&self, key: &K, value: &V) -> IgniteResult<()> {
        self.conn
            .send(OpCode::CachePut, CacheReq::Put::<K, V>(self.id, key, value))
    }

    pub fn put_all(&self, pairs: &[(K, V)]) -> IgniteResult<()> {
        self.conn.send(
            OpCode::CachePutAll,
            CacheReq::PutAll::<K, V>(self.id, pairs),
        )
    }

    pub fn contains_key(&self, key: &K) -> IgniteResult<bool> {
        self.conn
            .send_and_read(
                OpCode::CacheContainsKey,
                CacheReq::ContainsKey::<K, V>(self.id, key),
            )
            .map(|resp: CacheBoolResp| resp.flag)
    }

    pub fn contains_keys(&self, keys: &[K]) -> IgniteResult<bool> {
        self.conn
            .send_and_read(
                OpCode::CacheContainsKeys,
                CacheReq::ContainsKeys::<K, V>(self.id, keys),
            )
            .map(|resp: CacheBoolResp| resp.flag)
    }

    pub fn get_and_put(&self, key: &K, value: &V) -> IgniteResult<Option<V>> {
        self.conn
            .send_and_read(
                OpCode::CacheGetAndPut,
                CacheReq::GetAndPut::<K, V>(self.id, key, value),
            )
            .map(|resp: CacheDataObjectResp<V>| resp.val)
    }

    pub fn get_and_replace(&self, key: &K, value: &V) -> IgniteResult<Option<V>> {
        self.conn
            .send_and_read(
                OpCode::CacheGetAndReplace,
                CacheReq::GetAndReplace::<K, V>(self.id, key, value),
            )
            .map(|resp: CacheDataObjectResp<V>| resp.val)
    }

    pub fn get_and_remove(&self, key: &K) -> IgniteResult<Option<V>> {
        self.conn
            .send_and_read(
                OpCode::CacheGetAndRemove,
                CacheReq::GetAndRemove::<K, V>(self.id, key),
            )
            .map(|resp: CacheDataObjectResp<V>| resp.val)
    }

    pub fn put_if_absent(&self, key: &K, value: &V) -> IgniteResult<bool> {
        self.conn
            .send_and_read(
                OpCode::CachePutIfAbsent,
                CacheReq::PutIfAbsent::<K, V>(self.id, key, value),
            )
            .map(|resp: CacheBoolResp| resp.flag)
    }

    pub fn get_and_put_if_absent(&self, key: &K, value: &V) -> IgniteResult<Option<V>> {
        self.conn
            .send_and_read(
                OpCode::CacheGetAndPutIfAbsent,
                CacheReq::GetAndPutIfAbsent::<K, V>(self.id, key, value),
            )
            .map(|resp: CacheDataObjectResp<V>| resp.val)
    }

    pub fn replace(&self, key: &K, value: &V) -> IgniteResult<bool> {
        self.conn
            .send_and_read(
                OpCode::CacheReplace,
                CacheReq::Replace::<K, V>(self.id, key, value),
            )
            .map(|resp: CacheBoolResp| resp.flag)
    }

    pub fn replace_if_equals(&self, key: &K, old: &V, new: &V) -> IgniteResult<bool> {
        self.conn
            .send_and_read(
                OpCode::CacheReplaceIfEquals,
                CacheReq::ReplaceIfEquals::<K, V>(self.id, key, old, new),
            )
            .map(|resp: CacheBoolResp| resp.flag)
    }

    pub fn clear(&self) -> IgniteResult<()> {
        self.conn
            .send(OpCode::CacheClear, CacheReq::Clear::<K, V>(self.id))
    }

    pub fn clear_key(&self, key: &K) -> IgniteResult<()> {
        self.conn.send(
            OpCode::CacheClearKey,
            CacheReq::ClearKey::<K, V>(self.id, key),
        )
    }

    pub fn clear_keys(&self, keys: &[K]) -> IgniteResult<()> {
        self.conn.send(
            OpCode::CacheClearKeys,
            CacheReq::ClearKeys::<K, V>(self.id, keys),
        )
    }

    pub fn remove_key(&self, key: &K) -> IgniteResult<bool> {
        self.conn
            .send_and_read(
                OpCode::CacheRemoveKey,
                CacheReq::RemoveKey::<K, V>(self.id, key),
            )
            .map(|resp: CacheBoolResp| resp.flag)
    }

    pub fn remove_if_equals(&self, key: &K, value: &V) -> IgniteResult<bool> {
        self.conn
            .send_and_read(
                OpCode::CacheRemoveIfEquals,
                CacheReq::RemoveIfEquals::<K, V>(self.id, key, value),
            )
            .map(|resp: CacheBoolResp| resp.flag)
    }

    pub fn get_size(&self) -> IgniteResult<i64> {
        let modes = Vec::new();
        self.conn
            .send_and_read(
                OpCode::CacheGetSize,
                CacheReq::GetSize::<K, V>(self.id, modes),
            )
            .map(|resp: CacheSizeResp| resp.size)
    }

    pub fn get_size_peek_mode(&self, mode: CachePeekMode) -> IgniteResult<i64> {
        let modes = vec![mode];
        self.conn
            .send_and_read(
                OpCode::CacheGetSize,
                CacheReq::GetSize::<K, V>(self.id, modes),
            )
            .map(|resp: CacheSizeResp| resp.size)
    }

    pub fn get_size_peek_modes(&self, modes: Vec<CachePeekMode>) -> IgniteResult<i64> {
        self.conn
            .send_and_read(
                OpCode::CacheGetSize,
                CacheReq::GetSize::<K, V>(self.id, modes),
            )
            .map(|resp: CacheSizeResp| resp.size)
    }

    pub fn remove_keys(&self, keys: &[K]) -> IgniteResult<()> {
        self.conn.send(
            OpCode::CacheRemoveKeys,
            CacheReq::RemoveKeys::<K, V>(self.id, keys),
        )
    }

    pub fn remove_all(&self) -> IgniteResult<()> {
        self.conn
            .send(OpCode::CacheRemoveAll, CacheReq::RemoveAll::<K, V>(self.id))
    }
}