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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
use crate::*;
pub type IxList = Vec<(Rc<SortedFile>, Rc<Vec<usize>>)>;
#[derive(PartialEq, PartialOrd, Clone, Copy)]
pub enum SaveOp {
Save,
RollBack,
}
pub struct Table {
pub file: Rc<SortedFile>,
pub info: Rc<ColInfo>,
pub ixlist: RefCell<IxList>,
pub id: i64,
pub id_gen: Cell<i64>,
pub id_gen_dirty: Cell<bool>,
}
impl Table {
pub fn new(id: i64, root_page: u64, id_gen: i64, info: Rc<ColInfo>) -> Rc<Table> {
let rec_size = info.total;
let key_size = 8;
let file = Rc::new(SortedFile::new(rec_size, key_size, root_page));
let ixlist = RefCell::new(Vec::new());
Rc::new(Table {
id,
file,
info,
ixlist,
id_gen: Cell::new(id_gen),
id_gen_dirty: Cell::new(false),
})
}
pub fn save(&self, db: &DB, op: SaveOp) {
self.file.save(db, op);
for (f, _) in &*self.ixlist.borrow() {
f.save(db, op);
}
}
pub fn free_pages(&self, db: &DB) {
let row = self.row();
self.file.free_pages(db, &row);
for (f, cols) in &*self.ixlist.borrow() {
let ixr = IndexRow::new(self, cols.clone(), &row);
f.free_pages(db, &ixr);
}
}
pub fn insert(&self, db: &DB, row: &mut Row) {
row.encode(db);
self.file.insert(db, row);
for (f, cols) in &*self.ixlist.borrow() {
let ixr = IndexRow::new(self, cols.clone(), row);
f.insert(db, &ixr);
}
}
pub fn remove(&self, db: &DB, row: &Row) {
self.file.remove(db, row);
for (f, cols) in &*self.ixlist.borrow() {
let ixr = IndexRow::new(self, cols.clone(), row);
f.remove(db, &ixr);
}
row.delcodes(db);
}
pub fn index_from(
self: &Rc<Table>,
b: &Block,
we: &mut Expr,
) -> (Option<CExpPtr<bool>>, Option<CTableExpression>) {
let mut kc = SmallSet::default();
get_known_cols(we, &mut kc);
let list = &*self.ixlist.borrow();
let mut best_match = 0;
let mut best_index = 0;
for (index, (_f, clist)) in list.iter().enumerate() {
let m = covered(clist, &kc);
if m > best_match {
best_match = m;
best_index = index;
}
}
if best_match > 0 {
let clist = &list[best_index].1;
let mut cols = SmallSet::default();
for col in clist.iter().take(best_match) {
cols.insert(*col);
}
let mut kmap = BTreeMap::new();
let cwe = get_keys(b, we, &mut cols, &mut kmap);
let keys = clist
.iter()
.take(best_match)
.map(|col| kmap.remove(col).unwrap())
.collect();
return (
cwe,
Some(CTableExpression::IxGet(self.clone(), keys, best_index)),
);
}
if let ExprIs::Binary(op, e1, e2) = &mut we.exp {
if *op == Token::Equal && e2.is_constant {
if let ExprIs::ColName(_) = &e1.exp {
if e1.col == usize::MAX
{
return (
None,
Some(CTableExpression::IdGet(self.clone(), c_int(b, e2))),
);
}
}
}
}
(Some(c_bool(b, we)), None)
}
pub fn id_get(&self, db: &DB, id: u64) -> Option<(PagePtr, usize)> {
self.file.get(db, &Id { id })
}
pub fn ix_get(&self, db: &DB, key: Vec<Value>, index: usize) -> Option<(PagePtr, usize)> {
let list = &*self.ixlist.borrow();
let (sf, cols) = &list[index];
let key = IndexKey::new(self, cols.clone(), key, Ordering::Equal);
if let Some((pp, off)) = sf.get(db, &key) {
let p = pp.borrow();
let id = util::getu64(&p.data, off);
let row = Id { id };
return self.file.get(db, &row);
}
None
}
pub fn scan(&self, db: &DB) -> Asc {
self.file.asc(db, Box::new(Zero {}))
}
pub fn scan_id(self: &Rc<Table>, db: &DB, id: i64) -> IdScan {
IdScan {
table: self.clone(),
db: db.clone(),
id,
done: false,
}
}
pub fn scan_key(self: &Rc<Table>, db: &DB, key: Value, index: usize) -> IndexScan {
let keys = vec![key];
self.scan_keys(db, keys, index)
}
pub fn scan_keys(self: &Rc<Table>, db: &DB, keys: Vec<Value>, index: usize) -> IndexScan {
let ixlist = &*self.ixlist.borrow();
let (sf, cols) = &ixlist[index];
let ikey = IndexKey::new(self, cols.clone(), keys.clone(), Ordering::Less);
let ixa = sf.asc(db, Box::new(ikey));
IndexScan {
ixa,
keys,
cols: cols.clone(),
table: self.clone(),
db: db.clone(),
}
}
pub fn add_index(&self, root: u64, cols: Vec<usize>) {
let key_size = self.info.index_key_size(&cols) + 8;
let sf = Rc::new(SortedFile::new(key_size, key_size, root));
let list = &mut self.ixlist.borrow_mut();
list.push((sf, Rc::new(cols)));
}
pub fn delete_index(&self, db: &DB, ix: usize) {
let ixlist = &*self.ixlist.borrow();
let (f, cols) = &ixlist[ix];
let row = self.row();
let ixr = IndexRow::new(self, cols.clone(), &row);
f.free_pages(db, &ixr);
}
pub fn init_index(&self, db: &DB) {
let mut row = self.row();
let ixlist = self.ixlist.borrow();
let (f, cols) = ixlist.last().unwrap();
for (pp, off) in self.scan(db) {
let p = pp.borrow();
let data = &p.data[off..];
row.load(db, data);
let ixr = IndexRow::new(self, cols.clone(), &row);
f.insert(db, &ixr);
}
}
pub fn access<'d, 't>(&'t self, p: &'d Page, off: usize) -> Access<'d, 't> {
Access::<'d, 't> {
data: &p.data[off..],
info: &self.info,
}
}
pub fn write_access<'d, 't>(&'t self, p: &'d mut Page, off: usize) -> WriteAccess<'d, 't> {
let data = Data::make_mut(&mut p.data);
WriteAccess::<'d, 't> {
data: &mut data[off..],
info: &self.info,
}
}
pub fn row(&self) -> Row {
Row::new(self.info.clone())
}
pub fn alloc_id(&self) -> i64 {
let result = self.id_gen.get();
self.id_gen.set(result + 1);
self.id_gen_dirty.set(true);
result
}
pub fn id_allocated(&self, id: i64) {
if id >= self.id_gen.get() {
self.id_gen.set(id + 1);
self.id_gen_dirty.set(true);
}
}
#[cfg(feature = "pack")]
pub fn repack(&self, db: &DB, k: usize) -> i64 {
let row = self.row();
if k == 0 {
self.file.repack(db, &row)
} else {
let list = &*self.ixlist.borrow();
if k <= list.len() {
let (f, cols) = &list[k - 1];
let ixr = IndexRow::new(self, cols.clone(), &row);
f.repack(db, &ixr)
} else {
-1
}
}
}
#[cfg(feature = "verify")]
pub fn get_used(&self, db: &DB, to: &mut HashSet<u64>) {
self.file.get_used(db, to);
for (f, _cols) in &*self.ixlist.borrow() {
f.get_used(db, to);
}
}
}
struct Zero {}
impl Record for Zero {
fn compare(&self, _db: &DB, _data: &[u8]) -> Ordering {
Ordering::Less
}
}
pub struct Access<'d, 'i> {
data: &'d [u8],
info: &'i ColInfo,
}
impl<'d, 'i> Access<'d, 'i> {
pub fn int(&self, colnum: usize) -> i64 {
util::iget(self.data, self.info.off[colnum], self.info.siz(colnum)) as i64
}
pub fn str(&self, db: &DB, colnum: usize) -> String {
let off = self.info.off[colnum];
let size = self.info.siz(colnum);
let bytes = get_bytes(db, &self.data[off..], size).0;
String::from_utf8(bytes).unwrap()
}
pub fn id(&self) -> u64 {
util::getu64(self.data, 0)
}
}
pub struct WriteAccess<'d, 'i> {
data: &'d mut [u8],
info: &'i ColInfo,
}
impl<'d, 'i> WriteAccess<'d, 'i> {
pub fn set_int(&mut self, colnum: usize, val: i64) {
util::iset(self.data, self.info.off[colnum], val, self.info.siz(colnum));
}
pub fn int(&self, colnum: usize) -> i64 {
util::get(self.data, self.info.off[colnum], self.info.siz(colnum)) as i64
}
pub fn id(&self) -> u64 {
util::getu64(self.data, 0)
}
}
#[non_exhaustive]
pub struct ColInfo {
pub name: ObjRef,
pub colmap: BTreeMap<String, usize>,
pub colnames: Vec<String>,
pub typ: Vec<DataType>,
pub off: Vec<usize>,
pub total: usize,
}
impl ColInfo {
pub fn empty(name: ObjRef) -> Self {
ColInfo {
name,
colmap: BTreeMap::new(),
typ: Vec::new(),
colnames: Vec::new(),
off: Vec::new(),
total: 8,
}
}
pub fn new(name: ObjRef, ct: &[(&str, DataType)]) -> Self {
let mut result = Self::empty(name);
for (n, t) in ct {
result.add((*n).to_string(), *t);
}
result
}
pub fn add(&mut self, name: String, typ: DataType) -> bool {
if self.colmap.contains_key(&name) {
return true;
}
let cn = self.typ.len();
self.typ.push(typ);
let size = data_size(typ);
self.off.push(self.total);
self.total += size;
self.colnames.push(name.clone());
self.colmap.insert(name, cn);
false
}
pub(crate) fn add_altered(&mut self, ci: &ColInfo, cnum: usize, actions: &[AlterCol]) -> bool {
let cname = &ci.colnames[cnum];
let mut typ = ci.typ[cnum];
for act in actions {
match act {
AlterCol::Drop(name) => {
if name == cname {
return false;
}
}
AlterCol::Modify(name, dt) => {
if name == cname {
if data_kind(typ) != data_kind(*dt) {
panic!("Cannot change column data kind");
}
typ = *dt;
}
}
_ => {}
}
}
self.add(cname.clone(), typ);
true
}
pub fn get(&self, name: &str) -> Option<&usize> {
if name == "Id" {
Some(&usize::MAX)
} else {
self.colmap.get(name)
}
}
fn siz(&self, col: usize) -> usize {
data_size(self.typ[col])
}
fn index_key_size(&self, cols: &[usize]) -> usize {
cols.iter().map(|cnum| self.siz(*cnum)).sum()
}
}
#[non_exhaustive]
pub struct IndexInfo {
pub tname: ObjRef,
pub iname: String,
pub cols: Vec<usize>,
}
#[derive(Clone)]
#[non_exhaustive]
pub struct Row {
pub id: i64,
pub values: Vec<Value>,
pub info: Rc<ColInfo>,
pub codes: Vec<Code>,
}
impl Row {
pub fn new(info: Rc<ColInfo>) -> Self {
let mut result = Row {
id: 0,
values: Vec::new(),
info,
codes: Vec::new(),
};
for t in &result.info.typ {
result.values.push(Value::default(*t));
}
result
}
pub fn encode(&mut self, db: &DB) {
self.codes.clear();
for (i, val) in self.values.iter().enumerate() {
let size = data_size(self.info.typ[i]);
let u = db.encode(val, size);
self.codes.push(u);
}
}
pub fn delcodes(&self, db: &DB) {
for u in &self.codes {
if u.id != u64::MAX {
db.delcode(*u);
}
}
}
pub fn load(&mut self, db: &DB, data: &[u8]) {
self.values.clear();
self.codes.clear();
self.id = util::getu64(data, 0) as i64;
let mut off = 8;
for typ in &self.info.typ {
let (val, code) = Value::load(db, *typ, data, off);
self.values.push(val);
self.codes.push(code);
off += data_size(*typ);
}
}
}
impl Record for Row {
fn save(&self, data: &mut [u8]) {
util::setu64(data, self.id as u64);
let t = &self.info;
let mut off = 8;
for (i, typ) in t.typ.iter().enumerate() {
self.values[i].save(*typ, data, off, self.codes[i]);
off += data_size(*typ);
}
}
fn compare(&self, _db: &DB, data: &[u8]) -> Ordering {
let id = util::getu64(data, 0) as i64;
self.id.cmp(&id)
}
}
pub struct IndexRow {
tinfo: Rc<ColInfo>,
cols: Rc<Vec<usize>>,
keys: Vec<Value>,
codes: Vec<Code>,
rowid: i64,
}
impl IndexRow {
fn new(table: &Table, cols: Rc<Vec<usize>>, row: &Row) -> Self {
let mut keys = Vec::new();
let mut codes = Vec::new();
if !row.codes.is_empty() {
for c in &*cols {
keys.push(row.values[*c].clone());
codes.push(row.codes[*c]);
}
}
Self {
tinfo: table.info.clone(),
cols,
rowid: row.id,
keys,
codes,
}
}
fn load(&mut self, db: &DB, data: &[u8]) {
self.rowid = util::getu64(data, 0) as i64;
let mut off = 8;
for col in &*self.cols {
let typ = self.tinfo.typ[*col];
let val = Value::load(db, typ, data, off).0;
let size = data_size(typ);
let code = db.encode(&val, size);
self.keys.push(val);
self.codes.push(code);
off += size;
}
}
}
impl Record for IndexRow {
fn compare(&self, db: &DB, data: &[u8]) -> Ordering {
let mut ix = 0;
let mut off = 8;
loop {
let typ = self.tinfo.typ[self.cols[ix]];
let val = Value::load(db, typ, data, off).0;
let cf = val.cmp(&self.keys[ix]);
if cf != Ordering::Equal {
return cf;
}
ix += 1;
off += data_size(typ);
if ix == self.cols.len() {
let rowid = util::getu64(data, 0) as i64;
return self.rowid.cmp(&rowid);
}
}
}
fn save(&self, data: &mut [u8]) {
util::setu64(data, self.rowid as u64);
let mut off = 8;
for (ix, k) in self.keys.iter().enumerate() {
let typ = self.tinfo.typ[self.cols[ix]];
k.save(typ, data, off, self.codes[ix]);
off += data_size(typ);
}
}
fn key(&self, db: &DB, data: &[u8]) -> Box<dyn Record> {
let mut result = Box::new(IndexRow {
cols: self.cols.clone(),
tinfo: self.tinfo.clone(),
rowid: 0,
keys: Vec::new(),
codes: Vec::new(),
});
result.load(db, data);
result
}
fn drop_key(&self, db: &DB, data: &[u8]) {
let mut off = 8;
for col in &*self.cols {
let typ = self.tinfo.typ[*col];
let code = Value::load(db, typ, data, off).1;
if code.id != u64::MAX {
db.delcode(code);
}
off += data_size(typ);
}
}
}
struct IndexKey {
tinfo: Rc<ColInfo>,
cols: Rc<Vec<usize>>,
key: Vec<Value>,
def: Ordering,
}
impl IndexKey {
fn new(table: &Table, cols: Rc<Vec<usize>>, key: Vec<Value>, def: Ordering) -> Self {
Self {
tinfo: table.info.clone(),
key,
cols,
def,
}
}
}
impl Record for IndexKey {
fn compare(&self, db: &DB, data: &[u8]) -> Ordering {
let mut ix = 0;
let mut off = 8;
loop {
if ix == self.key.len() {
return self.def;
}
let typ = self.tinfo.typ[self.cols[ix]];
let val = Value::load(db, typ, data, off).0;
let cf = val.cmp(&self.key[ix]);
if cf != Ordering::Equal {
return cf;
}
ix += 1;
off += data_size(typ);
}
}
}
pub struct IndexScan {
ixa: Asc,
table: Rc<Table>,
db: DB,
cols: Rc<Vec<usize>>,
keys: Vec<Value>,
}
impl IndexScan {
fn keys_equal(&self, data: &[u8]) -> bool {
let mut off = 8;
for (ix, k) in self.keys.iter().enumerate() {
let typ = self.table.info.typ[self.cols[ix]];
let val = Value::load(&self.db, typ, data, off).0;
let cf = val.cmp(k);
if cf != Ordering::Equal {
return false;
}
off += data_size(typ);
}
true
}
}
impl Iterator for IndexScan {
type Item = (PagePtr, usize);
fn next(&mut self) -> Option<<Self as Iterator>::Item> {
if let Some((pp, off)) = self.ixa.next() {
let p = pp.borrow();
let data = &p.data[off..];
if !self.keys_equal(data) {
return None;
}
let id = util::getu64(data, 0);
return self.table.id_get(&self.db, id);
}
None
}
}
pub struct IdScan {
id: i64,
table: Rc<Table>,
db: DB,
done: bool,
}
impl Iterator for IdScan {
type Item = (PagePtr, usize);
fn next(&mut self) -> Option<<Self as Iterator>::Item> {
if self.done {
return None;
}
self.done = true;
self.table.id_get(&self.db, self.id as u64)
}
}
fn get_known_cols(we: &Expr, kc: &mut SmallSet) {
match &we.exp {
ExprIs::Binary(Token::Equal, e1, e2) => {
if e2.is_constant {
if let ExprIs::ColName(_) = &e1.exp {
kc.insert(e1.col);
}
} else if e1.is_constant {
if let ExprIs::ColName(_) = &e2.exp {
kc.insert(e2.col);
}
}
}
ExprIs::Binary(Token::And, e1, e2) => {
get_known_cols(e1, kc);
get_known_cols(e2, kc);
}
_ => {}
}
}
fn covered(clist: &[usize], kc: &SmallSet) -> usize {
let mut result = 0;
for &c in clist {
if !kc.contains(c) {
break;
}
result += 1;
}
result
}
fn get_keys(
b: &Block,
we: &mut Expr,
cols: &mut SmallSet,
keys: &mut BTreeMap<usize, CExpPtr<Value>>,
) -> Option<CExpPtr<bool>> {
match &mut we.exp {
ExprIs::Binary(Token::Equal, e1, e2) => {
if e2.is_constant {
if let ExprIs::ColName(_) = &e1.exp {
if cols.remove(e1.col) {
keys.insert(e1.col, c_value(b, e2));
return None;
}
}
} else if e1.is_constant {
if let ExprIs::ColName(_) = &e2.exp {
if cols.remove(e2.col) {
keys.insert(e2.col, c_value(b, e1));
return None;
}
}
}
}
ExprIs::Binary(Token::And, e1, e2) => {
let x1 = get_keys(b, e1, cols, keys);
let x2 = get_keys(b, e2, cols, keys);
return if let Some(c1) = x1 {
if let Some(c2) = x2 {
Some(Box::new(cexp::And { c1, c2 }))
} else {
Some(c1)
}
} else {
x2
};
}
_ => {}
}
return Some(c_bool(b, we));
}
pub fn row_compare(a: &[Value], b: &[Value], desc: &[bool]) -> Ordering {
let mut ix = 0;
loop {
let cmp = a[ix].cmp(&b[ix]);
if cmp != Ordering::Equal {
if !desc[ix] {
return cmp;
};
return if cmp == Ordering::Less {
Ordering::Greater
} else {
Ordering::Less
};
}
ix += 1;
if ix == desc.len() {
return Ordering::Equal;
}
}
}