rust_chain/
db.rs

1use core::convert::{
2    From,
3    Into,
4};
5
6use crate::structs::{ Uint128, Uint256, Float128 };
7use crate::serializer::Packer;
8
9use crate::string::{
10    String,
11};
12
13use crate::vmapi::db::*;
14
15use crate::name::{
16    Name,
17};
18
19use crate::serializer::{
20    Encoder,
21};
22
23use crate::asset::Asset;
24
25use crate::{
26    check,
27};
28
29use crate::print::{
30    Printable
31};
32
33use crate::{
34    vec::Vec,
35};
36
37
38///
39#[derive(Clone, Debug, Default)]
40pub struct TableError {
41    ///
42    pub message: String,
43}
44
45impl PrimaryValueInterface for Name {
46    fn get_primary(&self) -> u64 {
47        return self.value();
48    }
49}
50
51impl PrimaryValueInterface for u64 {
52    fn get_primary(&self) -> u64 {
53        return *self;
54    }
55}
56
57impl PrimaryValueInterface for Asset {
58    fn get_primary(&self) -> u64 {
59        return self.symbol().code().value();
60    }
61}
62
63///
64pub trait PrimaryValueInterface {
65    ///
66    fn get_primary(&self) -> u64;
67}
68
69pub trait SecondaryValueInterface: core::any::Any {
70    ///
71    fn get_secondary_value(&self, i: usize) -> SecondaryValue;
72    ///
73    fn set_secondary_value(&mut self, i: usize, value: SecondaryValue);
74}
75
76pub trait AsAny {
77    fn as_any(&self) -> &dyn core::any::Any;
78    fn as_any_mut(&mut self) -> &mut dyn core::any::Any;
79}
80
81///
82pub trait MultiIndexValue: PrimaryValueInterface + SecondaryValueInterface + Packer + AsAny {
83
84}
85
86pub struct Primary<'a, T>
87where T: Packer + PrimaryValueInterface + Default
88{
89    pub(crate) value: T,
90    _marker: core::marker::PhantomData<&'a ()>,
91}
92
93impl <'a, T> Primary<'a, T>
94where T: Packer + PrimaryValueInterface + Default
95{
96    pub fn new(value: T) -> Self {
97        Self { value, _marker: core::marker::PhantomData::<>{} }
98    }
99
100    pub fn get_primary(&self) -> u64 {
101        self.value.get_primary()
102    }
103
104    pub fn value(self) -> T {
105        self.value
106    }
107}
108
109pub struct Secondary<T>
110where T: Packer + Default
111{
112    pub(crate) value: T,
113}
114
115impl <T> Secondary<T>
116where T: Packer + Default
117{
118    pub fn new(value: T) -> Self {
119        Self { value }
120    }
121
122    pub fn value(self) -> T {
123        self.value
124    }
125}
126
127///
128pub struct Iterator<'a, T> 
129where T: Packer + PrimaryValueInterface + Default
130{
131    ///
132    pub(crate) i: i32,
133    pub(crate) primary: Option<u64>,
134    db: &'a TableI64<T>,
135}
136
137impl<'a, T> Iterator<'a, T> 
138where T: Packer + PrimaryValueInterface + Default
139{
140    ///
141    pub fn new(i: i32, primary: Option<u64>, db: &'a TableI64<T>) -> Self {
142        Self { i, primary, db }
143    }
144
145    /// get primary key of iterator
146    /// access `TableI64` to extract primary key from value if primary key is not cached
147    pub fn get_primary(&self) -> Option<u64> {
148        if !self.is_ok() {
149            return None;
150        }
151
152        if self.primary.is_some() {
153            return self.primary;
154        }
155
156        return Some(self.db.get(self).unwrap().get_primary());
157    }
158
159    pub fn set_primary(&mut self, primary: u64) {
160        if !self.is_ok() {
161            return;
162        }
163        self.primary = Some(primary);
164    }
165
166    ///
167    pub fn get_value(&self) -> Option<T> {
168        return self.db.get(self);
169    }
170
171    ///
172    pub fn get_i(&self) -> i32 {
173        return self.i;
174    }
175
176    /// return `true` if iterator is valid, else `false`
177    pub fn is_ok(&self) -> bool {
178        self.i >= 0
179    }
180
181    /// return `true` if it's an end iterator, else `false`
182    /// use this method to check the return value of `MultiIndex.end` or `TableI64.end`
183    pub fn is_end(&self) -> bool {
184        return self.i < -1;
185    }
186
187    ///help function for asserting on valid iterator
188    pub fn expect(self, msg: &str) -> Self {
189        check(self.is_ok(), msg);            
190        return self;
191    }
192
193    ///help function for asserting on invalid iterator
194    pub fn expect_not_ok(self, msg: &str) -> Self {
195        check(!self.is_ok(), msg);            
196        return self;
197    }
198}
199
200///
201#[derive(Clone, Copy, Debug, PartialEq)]
202pub struct SecondaryIterator {
203    ///
204    pub i: i32,
205    ///
206    pub db_index: usize,
207    ///
208    pub primary: u64
209}
210
211impl SecondaryIterator {
212    ///
213    pub fn is_ok(&self) -> bool {
214        self.i >= 0
215    }
216    ///
217    pub fn is_end(&self) -> bool {
218        return self.i < -1;
219    }
220}
221
222impl Default for SecondaryIterator {
223    fn default() -> Self {
224        SecondaryIterator{ i: -1, primary: 0, db_index: usize::MAX }
225    }
226}
227
228///
229#[derive(Clone, Copy, Debug, PartialEq, Eq)]
230pub enum SecondaryType {
231    ///
232    Idx64,
233    ///
234    Idx128,
235    ///
236    Idx256,
237    ///
238    IdxF64,
239    ///
240    IdxF128,
241}
242
243///
244#[derive(Clone, Copy, PartialEq)]
245pub enum SecondaryValue {
246    ///
247    None,
248    ///
249    Idx64(u64),
250    ///
251    Idx128(u128),
252    ///
253    Idx256(Uint256),
254    ///
255    IdxF64(f64),
256    ///
257    IdxF128(Float128),
258}
259
260impl From<u64> for SecondaryValue {
261    fn from(value: u64) -> Self {
262        SecondaryValue::Idx64(value)
263    }
264}
265
266impl From<u128> for SecondaryValue {
267    fn from(value: u128) -> Self {
268        SecondaryValue::Idx128(value)
269    }
270}
271
272impl From<Uint256> for SecondaryValue {
273    fn from(value: Uint256) -> Self {
274        SecondaryValue::Idx256(value)
275    }
276}
277
278impl From<f64> for SecondaryValue {
279    fn from(value: f64) -> Self {
280        SecondaryValue::IdxF64(value)
281    }
282}
283
284impl From<Float128> for SecondaryValue {
285    fn from(value: Float128) -> Self {
286        SecondaryValue::IdxF128(value)
287    }
288}
289
290impl From<SecondaryValue> for u64 {
291    fn from(value: SecondaryValue) -> Self {
292        if let SecondaryValue::Idx64(x) = value {
293            x
294        } else {
295            check(false, "From<SecondaryValue> for u64: Invalid SecondaryValue");
296            Default::default()
297        }
298    }
299}
300
301impl From<SecondaryValue> for u128 {
302    fn from(value: SecondaryValue) -> Self {
303        if let SecondaryValue::Idx128(x) = value {
304            x
305        } else {
306            check(false, "From<SecondaryValue> for u128: Invalid SecondaryValue");
307            Default::default()
308        }
309    }
310}
311
312impl From<SecondaryValue> for Uint256 {
313    fn from(value: SecondaryValue) -> Self {
314        if let SecondaryValue::Idx256(x) = value {
315            x
316        } else {
317            check(false, "From<SecondaryValue> for Uint256: Invalid SecondaryValue");
318            Default::default()
319        }
320    }
321}
322
323impl From<SecondaryValue> for f64 {
324    fn from(value: SecondaryValue) -> Self {
325        if let SecondaryValue::IdxF64(x) = value {
326            x
327        } else {
328            check(false, "From<SecondaryValue> for f64: Invalid SecondaryValue");
329            Default::default()
330        }
331    }
332}
333
334impl From<SecondaryValue> for Float128 {
335    fn from(value: SecondaryValue) -> Self {
336        if let SecondaryValue::IdxF128(x) = value {
337            x
338        } else {
339            check(false, "From<SecondaryValue> for Float128: Invalid SecondaryValue");
340            Default::default()
341        }
342    }
343}
344
345///
346pub struct TableI64<T> 
347where
348    T: Packer + PrimaryValueInterface + Default,
349{
350    ///
351    pub code: u64,
352    ///
353    pub scope: u64,
354    ///
355    pub table: u64,
356    _marker: core::marker::PhantomData<T>,
357}
358
359impl<T> TableI64<T>
360where
361    T: Packer + PrimaryValueInterface + Default,
362{
363    /// Creates a new TableI64 instance
364    pub fn new(code: Name, scope: Name, table: Name) -> Self {
365        TableI64 {
366            code: code.value(),
367            scope: scope.value(),
368            table: table.value(),
369            _marker: core::marker::PhantomData::<T>{}
370        }
371    }
372
373    ///
374    pub fn store(&self, value: &T, payer: Name) -> Iterator<T> {
375        let key = value.get_primary();
376        let data = Encoder::pack(value);
377        let it = db_store_i64(self.scope, self.table, payer.value(), key, data.as_ptr(), data.len() as u32);
378        Iterator::<T> { i: it, primary: Some(key), db: self }
379    }
380
381    ///
382    pub fn find(&self, key: u64) -> Iterator<T> {
383        let it = db_find_i64(self.code, self.scope, self.table, key);
384        if it != -1 {
385            Iterator::<T> { i: it, primary: Some(key), db: self }
386        } else {
387            Iterator::<T> { i: it, primary: None, db: self }
388        }
389    }
390
391    ///
392    pub fn update(&self, iterator: &Iterator<T>, value: &T, payer: Name) {
393        check(iterator.is_ok(), "TableI64::update:invalid iterator");
394        check(iterator.get_primary().unwrap() == value.get_primary(), "TableI64::update: can not change primary value during update!");
395        let data = Encoder::pack(value);
396        db_update_i64(iterator.i, payer.value(), data.as_ptr(), data.len() as u32);
397    }
398
399    /// remove value from database by iterator
400    pub fn remove(&self, iterator: &Iterator<T>) {
401        db_remove_i64(iterator.i);
402    }
403
404    /// get value by iterator. use [Iterator](crate::db::Iterator)::get_value for a more convenient way.
405    pub fn get(&self, iterator: &Iterator<T>) -> Option<T> {
406        if !iterator.is_ok() {
407            return None;
408        }
409
410        let data = db_get_i64(iterator.i);
411        let mut ret = T::default();
412        ret.unpack(&data); 
413        Some(ret)
414    }
415
416    /// get next iterator
417    pub fn next(&self, iterator: &Iterator<T>) -> Iterator<T> {
418        let mut primary = 0;
419        let it = db_next_i64(iterator.i, &mut primary);
420        if it != -1 {
421            Iterator::<T> { i: it, primary: Some(primary), db: self }
422        } else {
423            Iterator::<T> { i: it, primary: None, db: self }
424        }
425    }
426
427    /// get previous iterator
428    pub fn previous(&self, iterator: &Iterator<T>) -> Iterator<T> {
429        let mut primary = 0;
430        let it = db_previous_i64(iterator.i, &mut primary);
431        if it != -1 {
432            Iterator::<T> { i: it, primary: Some(primary), db: self }
433        } else {
434            Iterator::<T> { i: it, primary: None, db: self }
435        }
436    }
437
438    /// return a iterator with a key >= `key`
439    pub fn lower_bound(&self, key: u64) -> Iterator<T> {
440        let it = db_lowerbound_i64(self.code, self.scope, self.table, key);
441        Iterator::<T> { i: it, primary: None, db: self }
442    }
443
444    /// return a iterator with a key > `key`
445    pub fn upper_bound(&self, key: u64) -> Iterator<T> {
446        let it = db_upperbound_i64(self.code, self.scope, self.table, key);
447        Iterator::<T> { i: it, primary: None, db: self }
448    }
449
450    /// Return an end iterator, Iterator.is_end() return true if it's a valid end iterator.
451    /// This method is often used with `TableI64.previous` to get the last iterator.
452
453    /// ```rust
454    /// let mut it = db.end();
455    /// if it.is_end() {
456    ///     it = db.previous();
457    ///     //...
458    /// }
459    /// ```
460    pub fn end(&self) -> Iterator<T> {
461        let it = db_end_i64(self.code, self.scope, self.table);
462        Iterator::<T> { i: it, primary: None, db: self }
463    }
464}
465
466///
467pub struct Idx64Table {
468    ///
469    pub db_index: usize,
470    ///
471    pub code: u64,
472    ///
473    pub scope: u64,
474    ///
475    pub table: u64,
476}
477
478///
479pub struct Idx128Table {
480    ///
481    pub db_index: usize,
482    ///
483    pub code: u64,
484    ///
485    pub scope: u64,
486    ///
487    pub table: u64,
488}
489
490///
491pub struct Idx256Table {
492    ///
493    pub db_index: usize,
494    ///
495    pub code: u64,
496    ///
497    pub scope: u64,
498    ///
499    pub table: u64,
500}
501
502///
503pub struct IdxF64Table {
504    ///
505    pub db_index: usize,
506    ///
507    pub code: u64,
508    ///
509    pub scope: u64,
510    ///
511    pub table: u64,
512}
513
514///
515pub struct IdxF128Table {
516    ///
517    pub db_index: usize,
518    ///
519    pub code: u64,
520    ///
521    pub scope: u64,
522    ///
523    pub table: u64,
524}
525
526///
527pub trait IdxTable {
528    // fn new(code: u64, scope: u64, table: u64) -> Self;
529    ///
530    fn get_db_index(&self) -> usize;
531    ///
532    fn store(&self, key: u64, secondary: SecondaryValue, payer: Name) -> SecondaryIterator;
533    ///
534    fn update(&self, iterator: &SecondaryIterator, secondary: SecondaryValue, payer: Name);
535    ///
536    fn remove(&self, iterator: &SecondaryIterator);
537    ///
538    fn next(&self, iterator: &SecondaryIterator) -> SecondaryIterator;
539    ///
540    fn previous(&self, iterator: &SecondaryIterator) -> SecondaryIterator;
541    ///
542    fn find_primary(&self, primary: u64) -> (SecondaryIterator, SecondaryValue);
543    ///
544    fn find(&self, secondary: SecondaryValue) -> SecondaryIterator;
545    ///
546    fn lower_bound(&self, secondary: SecondaryValue) -> (SecondaryIterator, SecondaryValue);
547    ///
548    fn upper_bound(&self, secondary: SecondaryValue) -> (SecondaryIterator, SecondaryValue);
549    ///
550    fn end(&self) -> SecondaryIterator;
551}
552
553///
554pub struct IdxTableProxy<'a, T: From<SecondaryValue> + Into<SecondaryValue> + Printable + Default, const IDX_TYPE: usize> {
555    ///
556    pub db: &'a dyn IdxTable,
557    _secondary_type: SecondaryType,
558    _marker: core::marker::PhantomData<T>,
559}
560
561fn index_to_secondary_type(i: usize) -> SecondaryType {
562    return match i {
563        0 => SecondaryType::Idx64,
564        1 => SecondaryType::Idx128,
565        2 => SecondaryType::Idx256,
566        3 => SecondaryType::IdxF64,
567        4 => SecondaryType::IdxF128,
568        _ => SecondaryType::Idx64,
569    }
570}
571
572impl<'a, T: From<SecondaryValue> + Into<SecondaryValue> + Printable + Default, const IDX_TYPE: usize> IdxTableProxy<'a, T, IDX_TYPE>
573{
574    ///
575    pub fn new(db: &'a dyn IdxTable) -> Self {
576        Self {
577            db,
578            _secondary_type: index_to_secondary_type(IDX_TYPE),
579            _marker: core::marker::PhantomData::<T>{},
580        }
581    }
582    ///
583    pub fn get_db_index(&self) -> usize {
584        return self.db.get_db_index();
585    }
586
587    ///
588    pub fn store(&self, key: u64, value: T, payer: Name) -> SecondaryIterator {
589        let _value: SecondaryValue = value.into();
590        return self.db.store(key, _value, payer);
591    }
592
593    ///
594    pub fn update(&self, iterator: &SecondaryIterator, value: T, payer: Name) {
595        self.db.update(iterator, value.into(), payer);
596    }
597
598    ///
599    pub fn remove(&self, iterator: &SecondaryIterator) {
600        self.db.remove(iterator);
601    }
602
603    ///
604    pub fn next(&self, iterator: &SecondaryIterator) -> SecondaryIterator {
605        return self.db.next(iterator);
606    }
607
608    ///
609    pub fn previous(&self, iterator: &SecondaryIterator) -> SecondaryIterator {
610        return self.db.previous(iterator);
611    }
612
613    ///
614    pub fn find_primary(&self, primary: u64) -> (SecondaryIterator, T) {
615        let (it, value) = self.db.find_primary(primary);
616        return (it, value.into());
617    }
618
619    ///
620    pub fn find(&self, secondary: T) -> SecondaryIterator {
621        return self.db.find(secondary.into());
622    }
623
624    ///
625    pub fn lower_bound(&self, secondary: T) -> (SecondaryIterator, T) {
626        let (it, value) = self.db.lower_bound(secondary.into());
627        return (it, value.into());
628    }
629
630    ///
631    pub fn upper_bound(&self, secondary: T) -> (SecondaryIterator, T) {
632        let _secondary = secondary.into();
633        let (it, value) = self.db.upper_bound(_secondary);
634        let _value = value.into();
635        return (it, _value);
636    }
637
638    ///
639    pub fn end(&self) -> SecondaryIterator {
640        return self.db.end();
641    }
642
643}
644
645impl Idx64Table {
646    ///
647    pub fn new(db_index: usize, code: Name, scope: Name, table: Name) -> Self {
648        Idx64Table { db_index: db_index, code: code.value(), scope: scope.value(), table: table.value() }
649    }
650}
651
652impl IdxTable for Idx64Table {
653    fn get_db_index(&self) -> usize {
654        return self.db_index;
655    }
656
657    fn store(&self, key: u64, secondary: SecondaryValue, payer: Name) -> SecondaryIterator {
658        if let SecondaryValue::Idx64(value) = secondary {
659            let ret = db_idx64_store(self.scope, self.table, payer.value(), key, &value);
660            return SecondaryIterator{ i: ret, primary: key, db_index: self.db_index };    
661        }
662        check(false, "Idx64Table::store: bad secondary type");
663        return Default::default()
664    }
665
666    fn update(&self, iterator: &SecondaryIterator, secondary: SecondaryValue, payer: Name) {
667        if let SecondaryValue::Idx64(value) = secondary {
668            db_idx64_update(iterator.i, payer.value(), &value);
669            return;
670        } else {
671            check(false, "Idx64Table::update: bad secondary type");
672            return;
673        }
674    }
675
676    fn remove(&self, iterator: &SecondaryIterator) {
677        db_idx64_remove(iterator.i);
678    }
679
680    fn next(&self, iterator: &SecondaryIterator) -> SecondaryIterator {
681        let mut primary = 0;
682        let ret = db_idx64_next(iterator.i, &mut primary);
683        return SecondaryIterator{ i: ret, primary: primary, db_index: self.db_index };
684    }
685
686    fn previous(&self, iterator: &SecondaryIterator) -> SecondaryIterator {
687        let mut primary = 0;
688        let ret = db_idx64_previous(iterator.i, &mut primary);
689        return SecondaryIterator{ i: ret, primary: primary, db_index: self.db_index };
690    }
691
692    fn find_primary(&self, primary: u64) -> (SecondaryIterator, SecondaryValue) {
693        let mut secondary: u64 = 0;
694        let ret = db_idx64_find_primary(self.code, self.scope, self.table, &mut secondary, primary);
695        return (SecondaryIterator{ i: ret, primary: primary, db_index: self.db_index }, SecondaryValue::Idx64(secondary));
696    }
697
698    fn find(&self, secondary: SecondaryValue) -> SecondaryIterator {
699        if let SecondaryValue::Idx64(value) = secondary {
700            let mut primary = 0;
701            let ret = db_idx64_find_secondary(self.code, self.scope, self.table, &value, &mut primary);
702            return SecondaryIterator{ i: ret, primary: primary, db_index: self.db_index };
703        }
704        check(false, "Idx64Table::find_secondary: bad secondary type");
705        return Default::default();
706    }
707
708    fn lower_bound(&self, secondary: SecondaryValue) -> (SecondaryIterator, SecondaryValue) {
709        if let SecondaryValue::Idx64(mut value) = secondary {
710            let mut primary = 0;
711            let ret = db_idx64_lowerbound(self.code, self.scope, self.table, &mut value, &mut primary);
712            
713            return (SecondaryIterator{ i: ret, primary: primary, db_index: self.db_index }, SecondaryValue::Idx64(value));    
714        }
715        check(false, "Idx64Table::lower_bound: bad secondary type");
716        return (Default::default(), SecondaryValue::Idx64(0));
717    }
718
719    fn upper_bound(&self, secondary: SecondaryValue) -> (SecondaryIterator, SecondaryValue) {
720        if let SecondaryValue::Idx64(mut value) = secondary {
721            let mut primary = 0;
722            let ret = db_idx64_upperbound(self.code, self.scope, self.table, &mut value, &mut primary);            
723            return (SecondaryIterator{ i: ret, primary: primary, db_index: self.db_index }, SecondaryValue::Idx64(value));
724        }
725        check(false, "Idx64Table::upper_bounddd: bad secondary type");
726        return (Default::default(), SecondaryValue::Idx128(0));
727    }
728
729    fn end(&self) -> SecondaryIterator {
730        let ret = db_idx64_end(self.code, self.scope, self.table);
731        return SecondaryIterator{ i: ret, primary: 0, db_index: self.db_index };
732    }
733}
734
735impl Idx128Table {
736    ///
737    pub fn new(db_index: usize, code: Name, scope: Name, table: Name) -> Self {
738        Idx128Table { db_index: db_index, code: code.value(), scope: scope.value(), table: table.value() }
739    }
740}
741
742impl IdxTable for Idx128Table {
743    fn get_db_index(&self) -> usize {
744        return self.db_index;
745    }
746
747    fn store(&self, key: u64, secondary: SecondaryValue, payer: Name) -> SecondaryIterator {
748        if let SecondaryValue::Idx128(value) = secondary {
749            let _secondary = Uint128{lo: (value & u64::MAX as u128) as u64, hi: (value >> 64) as u64};
750            let ret = db_idx128_store(self.scope, self.table, payer.value(), key, &_secondary);
751            return SecondaryIterator{ i: ret, primary: key, db_index: self.db_index };
752        }
753        check(false, "Idx128Table::store: bad secondary type");
754        return Default::default();
755    }
756
757    fn update(&self, iterator: &SecondaryIterator, secondary: SecondaryValue, payer: Name) {
758        if let SecondaryValue::Idx128(value) = secondary {
759            let _secondary = Uint128{lo: (value & u64::MAX as u128) as u64, hi: (value >> 64) as u64};
760            db_idx128_update(iterator.i, payer.value(), &_secondary);
761        } else {
762            check(false, "Idx128Table::update: bad secondary type");
763        }
764    }
765
766    fn remove(&self, iterator: &SecondaryIterator) {
767        db_idx128_remove(iterator.i);
768    }
769
770    fn next(&self, iterator: &SecondaryIterator) -> SecondaryIterator {
771        let mut primary = 0;
772        let ret = db_idx128_next(iterator.i, &mut primary);
773        return SecondaryIterator{ i: ret, primary: primary, db_index: self.db_index };
774    }
775
776    fn previous(&self, iterator: &SecondaryIterator) -> SecondaryIterator {
777        let mut primary = 0;
778        let ret = db_idx128_previous(iterator.i, &mut primary);
779        return SecondaryIterator{ i: ret, primary: primary, db_index: self.db_index };
780    }
781
782    fn find_primary(&self, primary: u64) -> (SecondaryIterator, SecondaryValue) {
783        //initialize Uint128
784        let mut secondary = Uint128{lo:0, hi: 0};
785        let ret = db_idx128_find_primary(self.code, self.scope, self.table, &mut secondary, primary);
786        let _secondary = ((secondary.hi as u128) << 64) + secondary.lo as u128;
787        return (SecondaryIterator{ i: ret, primary: primary, db_index: self.db_index }, SecondaryValue::Idx128(_secondary));
788    }
789
790    fn find(&self, secondary: SecondaryValue) -> SecondaryIterator {
791        if let SecondaryValue::Idx128(value) = secondary {
792            let mut primary = 0;
793            let mut _secondary = Uint128{lo: (value & u64::MAX as u128) as u64, hi: (value >> 64) as u64};
794            let ret = db_idx128_find_secondary(self.code, self.scope, self.table, &mut _secondary, &mut primary);
795            return SecondaryIterator{ i: ret, primary: primary, db_index: self.db_index };
796        }
797        check(false, "Idx128Table::find_secondary: bad secondary type");
798        return Default::default();
799    }
800
801    fn lower_bound(&self, secondary: SecondaryValue) -> (SecondaryIterator, SecondaryValue) {
802        if let SecondaryValue::Idx128(mut value) = secondary {
803            let mut primary = 0;
804            // let _secondary: SecondaryValue = secondary;
805            let mut _secondary = Uint128{lo: (value & u64::MAX as u128) as u64, hi: (value >> 64) as u64};
806            let ret = db_idx128_lowerbound(self.code, self.scope, self.table, &mut _secondary, &mut primary);
807            value = ((_secondary.hi as u128) << 64) + _secondary.lo as u128;
808            return (SecondaryIterator{ i: ret, primary: primary, db_index: self.db_index }, SecondaryValue::Idx128(value));
809        }
810        check(false, "Idx128Table::lower_bound: bad secondary type");
811        return (Default::default(), SecondaryValue::Idx128(0));
812    }
813
814    fn upper_bound(&self, secondary: SecondaryValue) -> (SecondaryIterator, SecondaryValue) {
815        match secondary {
816            SecondaryValue::Idx128(mut value) => {
817                let mut primary = 0;
818                // let _secondary = secondary;
819                let mut _secondary = Uint128{lo: (value & u64::MAX as u128) as u64, hi: (value >> 64) as u64};
820                let ret = db_idx128_upperbound(self.code, self.scope, self.table, &mut _secondary, &mut primary);
821                value = ((_secondary.hi as u128) << 64) + _secondary.lo as u128;
822                return (SecondaryIterator{ i: ret, primary: primary, db_index: self.db_index }, SecondaryValue::Idx128(value));
823            },
824            _ => {
825                check(false, "Idx128Table::upper_bound: bad secondary type");
826                return (Default::default(), SecondaryValue::Idx128(0));
827            }
828        }
829    }
830
831    fn end(&self) -> SecondaryIterator {
832        let ret = db_idx128_end(self.code, self.scope, self.table);
833        return SecondaryIterator{ i: ret, primary: 0, db_index: self.db_index };
834    }
835}
836
837impl Idx256Table {
838    ///
839    pub fn new(db_index: usize, code: Name, scope: Name, table: Name) -> Self {
840        Idx256Table { db_index: db_index, code: code.value(), scope: scope.value(), table: table.value() }
841    }
842}
843
844impl IdxTable for Idx256Table {
845    fn get_db_index(&self) -> usize {
846        return self.db_index;
847    }
848
849    fn store(&self, key: u64, secondary: SecondaryValue, payer: Name) -> SecondaryIterator {
850        if let SecondaryValue::Idx256(value) = secondary {
851            let ret = db_idx256_store(self.scope, self.table, payer.value(), key, value.data.as_ptr() as *mut Uint128, 2);
852            return SecondaryIterator{ i: ret, primary: key, db_index: self.db_index };
853        }
854        check(false, "Idx256Table::store: bad secondary type");
855        return Default::default();
856    }
857
858    fn update(&self, iterator: &SecondaryIterator, secondary: SecondaryValue, payer: Name) {
859        if let SecondaryValue::Idx256(value) = secondary {
860            db_idx256_update(iterator.i, payer.value(), value.data.as_ptr() as *mut Uint128, 2);
861        } else {
862            check(false, "Idx256Table::update: bad secondary type");
863        }
864    }
865
866    fn remove(&self, iterator: &SecondaryIterator) {
867        db_idx256_remove(iterator.i);
868    }
869
870    fn next(&self, iterator: &SecondaryIterator) -> SecondaryIterator {
871        let mut primary = 0;
872        let ret = db_idx256_next(iterator.i, &mut primary);
873        return SecondaryIterator{ i: ret, primary: primary, db_index: self.db_index };
874    }
875
876    fn previous(&self, iterator: &SecondaryIterator) -> SecondaryIterator {
877        let mut primary = 0;
878        let ret = db_idx256_previous(iterator.i, &mut primary);
879        return SecondaryIterator{ i: ret, primary: primary, db_index: self.db_index };
880    }
881
882    fn find_primary(&self, primary: u64) -> (SecondaryIterator, SecondaryValue) {
883        //initialize Uint128
884        let mut secondary = Uint256{data: [0; 2]};
885        let ret = db_idx256_find_primary(self.code, self.scope, self.table, secondary.data.as_mut_ptr() as *mut Uint128, 2, primary);
886        return (SecondaryIterator{ i: ret, primary: primary, db_index: self.db_index }, SecondaryValue::Idx256(secondary));
887    }
888
889    fn find(&self, secondary: SecondaryValue) -> SecondaryIterator {
890        if let SecondaryValue::Idx256(mut value) = secondary {
891            let mut primary = 0;
892            let ret = db_idx256_find_secondary(self.code, self.scope, self.table, value.data.as_mut_ptr() as *mut Uint128, 2, &mut primary);
893            return SecondaryIterator{ i: ret, primary: primary, db_index: self.db_index };
894        }
895        check(false, "Idx256Table::find_secondary: bad secondary type");
896        return Default::default();
897    }
898
899    fn lower_bound(&self, secondary: SecondaryValue) -> (SecondaryIterator, SecondaryValue) {
900        if let SecondaryValue::Idx256(mut value) = secondary {
901            let mut primary = 0;
902            let ret = db_idx256_lowerbound(self.code, self.scope, self.table, value.data.as_mut_ptr() as *mut u8 as *mut Uint128, 2, &mut primary);
903            return (SecondaryIterator{ i: ret, primary: primary, db_index: self.db_index }, SecondaryValue::Idx256(value));
904        }
905        check(false, "Idx256Table::lower_bound: bad secondary type");
906        return (Default::default(), SecondaryValue::Idx128(0));
907    }
908
909    fn upper_bound(&self, secondary: SecondaryValue) -> (SecondaryIterator, SecondaryValue) {
910        match secondary {
911            SecondaryValue::Idx256(mut value) => {
912                let mut primary = 0;
913                let ret = db_idx256_upperbound(self.code, self.scope, self.table, value.data.as_mut_ptr() as *mut u8 as *mut Uint128, 2, &mut primary);
914                return (SecondaryIterator{ i: ret, primary: primary, db_index: self.db_index }, SecondaryValue::Idx256(value));
915            },
916            _ => {
917                check(false, "Idx256Table::upper_boundd: bad secondary type");
918                return (Default::default(), SecondaryValue::Idx128(0));
919            }
920        }
921    }
922
923    fn end(&self) -> SecondaryIterator {
924        let ret = db_idx256_end(self.code, self.scope, self.table);
925        return SecondaryIterator{ i: ret, primary: 0, db_index: self.db_index };
926    }
927}
928
929
930impl IdxF64Table {
931    ///
932    pub fn new(db_index: usize, code: Name, scope: Name, table: Name) -> Self {
933        IdxF64Table { db_index: db_index, code: code.value(), scope: scope.value(), table: table.value() }
934    }
935}
936
937impl IdxTable for IdxF64Table {
938    fn get_db_index(&self) -> usize {
939        return self.db_index;
940    }
941
942    fn store(&self, key: u64, secondary: SecondaryValue, payer: Name) -> SecondaryIterator {
943        if let SecondaryValue::IdxF64(value) = secondary {
944            let ret = db_idx_double_store(self.scope, self.table, payer.value(), key, &value);
945            return SecondaryIterator{ i: ret, primary: key, db_index: self.db_index };
946        }
947        check(false, "IdxF64Table::store: bad secondary type");
948        return Default::default();
949    }
950
951    fn update(&self, iterator: &SecondaryIterator, secondary: SecondaryValue, payer: Name) {
952        if let SecondaryValue::IdxF64(value) = secondary {
953            db_idx_double_update(iterator.i, payer.value(), &value);
954        } else {
955            check(false, "IdxF64Table::update: bad secondary type")
956        }
957    }
958
959    fn remove(&self, iterator: &SecondaryIterator) {
960        db_idx_double_remove(iterator.i);
961    }
962
963    fn next(&self, iterator: &SecondaryIterator) -> SecondaryIterator {
964        let mut primary = 0;
965        let ret = db_idx_double_next(iterator.i, &mut primary);
966        return SecondaryIterator{ i: ret, primary: primary, db_index: self.db_index };
967    }
968
969    fn previous(&self, iterator: &SecondaryIterator) -> SecondaryIterator {
970        let mut primary = 0;
971        let ret = db_idx_double_previous(iterator.i, &mut primary);
972        return SecondaryIterator{ i: ret, primary: primary, db_index: self.db_index };
973    }
974
975    fn find_primary(&self, primary: u64) -> (SecondaryIterator, SecondaryValue) {
976        //initialize Uint128
977        let mut secondary = 0 as f64;
978        let ret = db_idx_double_find_primary(self.code, self.scope, self.table, &mut secondary, primary);
979        return (SecondaryIterator{ i: ret, primary: primary, db_index: self.db_index }, SecondaryValue::IdxF64(secondary));
980    }
981
982    fn find(&self, secondary: SecondaryValue) -> SecondaryIterator {
983        if let SecondaryValue::IdxF64(mut value) = secondary {
984            let mut primary = 0;
985            let ret = db_idx_double_find_secondary(self.code, self.scope, self.table, &mut value, &mut primary);
986            return SecondaryIterator{ i: ret, primary: primary, db_index: self.db_index };
987        }
988        check(false, "IdxF64Table::find_secondary: bad secondary type");
989        return Default::default();
990    }
991
992    fn lower_bound(&self, secondary: SecondaryValue) -> (SecondaryIterator, SecondaryValue) {
993        if let SecondaryValue::IdxF64(mut value) = secondary {
994            let mut primary = 0;
995            let ret = db_idx_double_lowerbound(self.code, self.scope, self.table, &mut value, &mut primary);
996            return (SecondaryIterator{ i: ret, primary: primary, db_index: self.db_index }, SecondaryValue::IdxF64(value));
997        }
998        check(false, "IdxF64Table::lower_bound: bad secondary type");
999        return (Default::default(), SecondaryValue::IdxF64(0.0));
1000    }
1001
1002    fn upper_bound(&self, secondary: SecondaryValue) -> (SecondaryIterator, SecondaryValue) {
1003        match secondary {
1004            SecondaryValue::IdxF64(mut value) => {
1005                let mut primary = 0;
1006                let ret = db_idx_double_upperbound(self.code, self.scope, self.table, &mut value, &mut primary);
1007                return (SecondaryIterator{ i: ret, primary: primary, db_index: self.db_index }, SecondaryValue::IdxF64(value));    
1008            },
1009            _ => {
1010                check(false, "IdxF64Table::upper_boundddd: bad secondary type");
1011                return (Default::default(), SecondaryValue::IdxF64(0.0));
1012            }
1013        }
1014    }
1015
1016    fn end(&self) -> SecondaryIterator {
1017        let ret = db_idx_double_end(self.code, self.scope, self.table);
1018        return SecondaryIterator{ i: ret, primary: 0, db_index: self.db_index };
1019    }
1020}
1021
1022
1023impl IdxF128Table {
1024    ///
1025    pub fn new(db_index: usize, code: Name, scope: Name, table: Name) -> Self {
1026        IdxF128Table { db_index: db_index, code: code.value(), scope: scope.value(), table: table.value() }
1027    }
1028}
1029
1030impl IdxTable for IdxF128Table {
1031    fn get_db_index(&self) -> usize {
1032        return self.db_index;
1033    }
1034
1035    fn store(&self, key: u64, secondary: SecondaryValue, payer: Name) -> SecondaryIterator {
1036        if let SecondaryValue::IdxF128(value) = secondary {
1037            let ret = db_idx_long_double_store(self.scope, self.table, payer.value(), key, &value);
1038            return SecondaryIterator{ i: ret, primary: key, db_index: self.db_index };
1039        }
1040        check(false, "IdxF128Table::store: bad secondary type");
1041        return Default::default();
1042    }
1043
1044    fn update(&self, iterator: &SecondaryIterator, secondary: SecondaryValue, payer: Name) {
1045        if let SecondaryValue::IdxF128(value) = secondary {
1046            db_idx_long_double_update(iterator.i, payer.value(), &value);
1047        } else {
1048            check(false, "IdxF128Table::update: bad secondary type")
1049        }
1050    }
1051
1052    fn remove(&self, iterator: &SecondaryIterator) {
1053        db_idx_long_double_remove(iterator.i);
1054    }
1055
1056    fn next(&self, iterator: &SecondaryIterator) -> SecondaryIterator {
1057        let mut primary = 0;
1058        let ret = db_idx_long_double_next(iterator.i, &mut primary);
1059        return SecondaryIterator{ i: ret, primary: primary, db_index: self.db_index };
1060    }
1061
1062    fn previous(&self, iterator: &SecondaryIterator) -> SecondaryIterator {
1063        let mut primary = 0;
1064        let ret = db_idx_long_double_previous(iterator.i, &mut primary);
1065        return SecondaryIterator{ i: ret, primary: primary, db_index: self.db_index };
1066    }
1067
1068    fn find_primary(&self, primary: u64) -> (SecondaryIterator, SecondaryValue) {
1069        //initialize Uint128
1070        let mut secondary: Float128 = Default::default();
1071        let ret = db_idx_long_double_find_primary(self.code, self.scope, self.table, &mut secondary, primary);
1072        return (SecondaryIterator{ i: ret, primary: primary, db_index: self.db_index }, SecondaryValue::IdxF128(secondary));
1073    }
1074
1075    fn find(&self, secondary: SecondaryValue) -> SecondaryIterator {
1076        if let SecondaryValue::IdxF128(mut value) = secondary {
1077            let mut primary = 0;
1078            let ret = db_idx_long_double_find_secondary(self.code, self.scope, self.table, &mut value, &mut primary);
1079            return SecondaryIterator{ i: ret, primary: primary, db_index: self.db_index };
1080        }
1081        check(false, "IdxF128Table::find_secondary: bad secondary type");
1082        return Default::default();
1083    }
1084
1085    fn lower_bound(&self, secondary: SecondaryValue) -> (SecondaryIterator, SecondaryValue) {
1086        if let SecondaryValue::IdxF128(mut value) = secondary {
1087            let mut primary = 0;
1088            let ret = db_idx_long_double_lowerbound(self.code, self.scope, self.table, &mut value, &mut primary);
1089            return (SecondaryIterator{ i: ret, primary: primary, db_index: self.db_index }, SecondaryValue::IdxF128(value));
1090        }
1091        check(false, "IdxF128Table::lower_bound: bad secondary type");
1092        return (Default::default(), SecondaryValue::IdxF128(Float128::default()));
1093    }
1094
1095    fn upper_bound(&self, secondary: SecondaryValue) -> (SecondaryIterator, SecondaryValue) {
1096        if let SecondaryValue::IdxF128(mut value) = secondary {
1097            let mut primary = 0;
1098            let ret = db_idx_long_double_upperbound(self.code, self.scope, self.table, &mut value, &mut primary);
1099            return (SecondaryIterator{ i: ret, primary: primary, db_index: self.db_index }, SecondaryValue::IdxF128(value));    
1100        }
1101        check(false, "IdxF128Table::upper_bound: bad secondary type");
1102        return (Default::default(), SecondaryValue::None);   
1103    }
1104
1105    fn end(&self) -> SecondaryIterator {
1106        let ret = db_idx_long_double_end(self.code, self.scope, self.table);
1107        return SecondaryIterator{ i: ret, primary: 0, db_index: self.db_index };
1108    }
1109}