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