forked from guycipher/btree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbtree.go
1735 lines (1502 loc) · 35.9 KB
/
btree.go
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
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Package btree
// An embedded concurrent, disk based, BTree implementation
// Copyright (C) 2024 Alex Gaetano Padula
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along with
// this program. If not, see <http://www.gnu.org/licenses/>.
package btree
import (
"bytes"
"encoding/gob"
"errors"
"fmt"
"io"
"os"
"sync"
)
const PAGE_SIZE = 1024 // Page size for each node within the tree file
// BTree is the main BTree struct
type BTree struct {
File *os.File // The open btree file
T int // The order of the tree
TreeLock *sync.RWMutex // Lock for the tree file
PageLocks map[int64]*sync.RWMutex // Locks for each btree node, consumes some memory but allows for concurrent reads
PageLocksMu *sync.RWMutex // Lock for the page locks
}
// Key is the key struct for the BTree
type Key struct {
K interface{}
V []interface{} // key can have multiple values
Overflowed bool // If the key has overflowed, it will be stored in the overflow page
OverflowPage int64 // the page where we can find the overflowed values. An overflow node has 1 key, no children and a list of values tied to the key. The overflow node [0] key can also be overflowed
}
// Node is the node struct for the BTree
type Node struct {
Page int64 // The page number of the node
Keys []*Key // The keys in node
Children []int64 // The children of the node
Leaf bool // If the node is a leaf node
Overflow bool // If the node is an overflow node
Reuse bool // If node will be reused
}
type Iterator struct {
node *Node
keyIndex int
valIndex int
btree *BTree
}
// Open opens a new or existing BTree
func Open(name string, perm int, t int) (*BTree, error) {
if t < 2 {
return nil, errors.New("t must be greater than 1")
}
treeFile, err := os.OpenFile(name, os.O_CREATE|os.O_RDWR, os.FileMode(perm))
if err != nil {
return nil, err
}
pgLocks := make(map[int64]*sync.RWMutex)
// Read the tree file and create locks for each page
stat, err := treeFile.Stat()
if err != nil {
return nil, err
}
for i := int64(0); i < stat.Size()/PAGE_SIZE; i++ {
pgLocks[i] = &sync.RWMutex{}
}
return &BTree{
File: treeFile,
PageLocks: pgLocks,
T: t,
TreeLock: &sync.RWMutex{},
PageLocksMu: &sync.RWMutex{},
}, nil
}
func (b *BTree) NewIteratorFromKey(key interface{}) (*Iterator, error) {
root, err := b.getRoot()
if err != nil {
return nil, err
}
node, keyIndex, err := b.findNodeForKey(root, key)
if err != nil {
return nil, err
}
return &Iterator{node: node, keyIndex: keyIndex, valIndex: 0, btree: b}, nil
}
// Close closes the BTree
func (b *BTree) Close() error {
return b.File.Close()
}
// newBTreeNode
func (b *BTree) newBTreeNode(leaf bool) (*Node, error) {
newNode := &Node{
Leaf: leaf,
Keys: make([]*Key, 0),
}
var err error
newNode.Page, err = b.newPageNumber()
if err != nil {
return nil, err
}
// We write the node to file
_, err = b.writePage(newNode)
if err != nil {
return nil, err
}
// we return the new node
return newNode, nil
}
// encodeNode encodes a node into a byte slice
// The byte slice is padded with zeros to PAGE_SIZE
func encodeNode(n *Node) ([]byte, error) {
buff := bytes.NewBuffer([]byte{}) // Create a new buffer to store the encoded node
enc := gob.NewEncoder(buff) // Create a new encoder passing the buffer
err := enc.Encode(n) // Encode the node
if err != nil {
return nil, err
}
// Check if the node is too large to encode
if len(buff.Bytes()) > PAGE_SIZE {
// **** This can occur if you set a T too large with a small PAGE_SIZE
return nil, errors.New("node too large to encode")
}
// Fill the rest of the page with zeros
for i := len(buff.Bytes()); i < PAGE_SIZE; i++ {
buff.WriteByte(0)
}
return buff.Bytes(), nil
}
// decodeNode decodes a byte slice into a node
func decodeNode(data []byte) (*Node, error) {
n := &Node{} // Create a new node which will be decoded into
dec := gob.NewDecoder(bytes.NewBuffer(data)) // Create a new decoder passing the data
err := dec.Decode(n) // Decode the data into the node
if err != nil {
return nil, err
}
return n, nil
}
// getRoot returns the root of the BTree
func (b *BTree) getRoot() (*Node, error) {
root, err := b.getPage(0)
if err != nil {
if err.Error() == "failed to read page 0: EOF" {
// create root
// initial root if a leaf node and starts at page 0
root = &Node{
Leaf: true,
Page: 0,
Children: make([]int64, 0),
Keys: make([]*Key, 0),
}
// write the root to the file
_, err = b.writePage(root)
if err != nil {
return nil, err
}
} else {
return nil, err
}
}
return root, nil
}
// splitRoot splits the root node
func (b *BTree) splitRoot() error {
oldRoot, err := b.getRoot()
if err != nil {
return err
}
// Create new node (this will be the new "old root")
newOldRoot, err := b.newBTreeNode(oldRoot.Leaf)
if err != nil {
return err
}
// lock the new root
newRootLock := b.getPageLock(newOldRoot.Page)
newRootLock.Lock()
defer newRootLock.Unlock()
// Copy keys and children from old root to new old root
newOldRoot.Keys = oldRoot.Keys
newOldRoot.Children = oldRoot.Children
// Create new root and make new old root a child of new root
newRoot := &Node{
Page: 0, // New root takes the old root's page number
Children: []int64{newOldRoot.Page},
}
// Split new old root and move median key up to new root
err = b.splitChild(newRoot, 0, newOldRoot)
if err != nil {
return err
}
// Write new root and new old root to file
_, err = b.writePage(newRoot)
if err != nil {
return err
}
_, err = b.writePage(newOldRoot)
if err != nil {
return err
}
return nil
}
// splitChild splits a child node of x at index i
func (b *BTree) splitChild(x *Node, i int, y *Node) error {
z, err := b.newBTreeNode(y.Leaf)
if err != nil {
return err
}
zLock := b.getPageLock(z.Page)
zLock.Lock()
defer zLock.Unlock()
z.Keys = append(z.Keys, y.Keys[b.T:]...)
y.Keys = y.Keys[:b.T]
if !y.Leaf {
z.Children = append(z.Children, y.Children[b.T:]...)
y.Children = y.Children[:b.T]
}
x.Keys = append(x.Keys, nil)
x.Children = append(x.Children, 0)
for j := len(x.Keys) - 1; j > i; j-- {
x.Keys[j] = x.Keys[j-1]
}
x.Keys[i] = y.Keys[b.T-1]
// remove the key from y
y.Keys = y.Keys[:b.T-1]
for j := len(x.Children) - 1; j > i+1; j-- {
x.Children[j] = x.Children[j-1]
}
x.Children[i+1] = z.Page
_, err = b.writePage(y)
if err != nil {
return err
}
_, err = b.writePage(z)
if err != nil {
return err
}
_, err = b.writePage(x)
if err != nil {
return err
}
return nil
}
// Put inserts a key into the BTree
// A key can have multiple values
// Put inserts a key value pair into the BTree
func (b *BTree) Put(key interface{}, value interface{}) error {
root, err := b.getRoot()
if err != nil {
return err
}
// lock root
rootLock := b.getPageLock(root.Page)
rootLock.Lock()
// we will unlock the root after we are done
defer rootLock.Unlock()
if len(root.Keys) == (2*b.T)-1 {
err = b.splitRoot()
if err != nil {
return err
}
root, err = b.getPage(0)
if err != nil {
return err
}
}
err = b.insertNonFull(root, key, value)
if err != nil {
return err
}
return nil
}
// insertNonFull inserts a key into a non-full node
func (b *BTree) insertNonFull(x *Node, key interface{}, value interface{}) error {
i := len(x.Keys) - 1
if x.Leaf {
for i >= 0 && lessThan(key, x.Keys[i].K) {
i--
}
// If key exists, append the value
if i >= 0 && equal(key, x.Keys[i].K) {
x.Keys[i].V = append(x.Keys[i].V, value)
// check if the key has overflowed
if b.nodeKeyOverflowed(x) {
// remove last value from the key
x.Keys[i].V = x.Keys[i].V[:len(x.Keys[i].V)-1]
err := b.handleKeyOverflow(x, i, key, value)
if err != nil {
return err
}
return nil
}
} else {
// If key doesn't exist, insert new key and value
x.Keys = append(x.Keys, nil)
j := len(x.Keys) - 1
for j > i+1 {
x.Keys[j] = x.Keys[j-1]
j--
}
x.Keys[j] = &Key{K: key, V: []interface{}{value}}
}
_, err := b.writePage(x)
if err != nil {
return err
}
} else {
for i >= 0 && lessThan(key, x.Keys[i].K) {
i--
}
i++
child, err := b.getPage(x.Children[i])
if err != nil {
return err
}
if len(child.Keys) == (2*b.T)-1 {
err = b.splitChild(x, i, child)
if err != nil {
return err
}
if greaterThan(key, x.Keys[i].K) {
i++
}
}
child, err = b.getPage(x.Children[i])
if err != nil {
return err
}
// lock the child
childLock := b.getPageLock(child.Page)
childLock.Lock()
defer childLock.Unlock()
err = b.insertNonFull(child, key, value)
if err != nil {
return err
}
}
return nil
}
// Get returns the values associated with a key
func (b *BTree) Get(k interface{}) ([]interface{}, error) {
root, err := b.getRoot()
if err != nil {
return nil, err
}
return b.searchKey(k, root)
}
// searchKey searches for a key in the BTree
func (b *BTree) searchKey(k interface{}, x *Node) ([]interface{}, error) {
if x != nil {
nodeLock := b.getPageLock(x.Page)
nodeLock.RLock()
defer nodeLock.RUnlock()
i := 0
for i < len(x.Keys) && lessThan(x.Keys[i].K, k) {
i++
}
if i < len(x.Keys) && equal(k, x.Keys[i].K) {
var values []interface{}
values = append(values, x.Keys[i].V...)
// Check if the key has overflowed
if x.Keys[i].Overflowed {
overflowPage, err := b.getPage(x.Keys[i].OverflowPage)
if err != nil {
return nil, err
}
// Append the values from the overflow page
values = append(values, overflowPage.Keys[0].V...)
// Check if the overflow page has overflowed and repeat the process
for overflowPage.Keys[0].Overflowed {
overflowPage, err = b.getPage(overflowPage.Keys[0].OverflowPage)
if err != nil {
return nil, err
}
values = append(values, overflowPage.Keys[0].V...)
}
}
return values, nil
} else if x.Leaf {
return nil, nil
} else {
child, err := b.getPage(x.Children[i])
if err != nil {
return nil, err
}
return b.searchKey(k, child)
}
} else {
root, err := b.getRoot()
if err != nil {
return nil, err
}
return b.searchKey(k, root)
}
}
// handleKeyOverflow handles the overflow of a key
func (b *BTree) handleKeyOverflow(x *Node, i int, key interface{}, value interface{}) error {
if x.Keys[i].Overflowed {
// Get the last overflow page
overflowPage, err := b.getPage(x.Keys[i].OverflowPage)
if err != nil {
return err
}
for overflowPage.Keys[0].Overflowed {
overflowPage, err = b.getPage(overflowPage.Keys[0].OverflowPage)
if err != nil {
return err
}
}
// Append the new value to the overflow page
overflowPage.Keys[0].V = append(overflowPage.Keys[0].V, value)
// Check if the overflow page has overflowed
if b.nodeKeyOverflowed(overflowPage) {
// Remove the last value from the overflow page
overflowPage.Keys[0].V = overflowPage.Keys[0].V[:len(overflowPage.Keys[0].V)-1]
// Create a new overflow page
newOverflowPage, err := b.newBTreeNode(true)
if err != nil {
return err
}
// Add the value to the new overflow page
if len(overflowPage.Keys[0].V) == 0 {
overflowPage.Keys[0].V = append(overflowPage.Keys[0].V, value)
} else {
newOverflowPage.Keys = append(newOverflowPage.Keys, &Key{K: key, V: []interface{}{value}})
}
// Link the old overflow page to the new one
overflowPage.Keys[0].Overflowed = true
overflowPage.Keys[0].OverflowPage = newOverflowPage.Page
// Write the new overflow page to the file
_, err = b.writePage(newOverflowPage)
if err != nil {
return err
}
}
// Write the old overflow page to the file
_, err = b.writePage(overflowPage)
if err != nil {
return err
}
} else {
existingOverflow, err := b.getAvailableOverflowNode()
if err != nil {
return err
}
if existingOverflow == nil {
existingOverflow, err = b.newBTreeNode(true)
if err != nil {
return err
}
}
if len(existingOverflow.Keys) == 0 {
existingOverflow.Keys = append(existingOverflow.Keys, &Key{K: key, V: []interface{}{value}})
} else {
existingOverflow.Keys[0].V = append(existingOverflow.Keys[0].V, value)
}
x.Keys[i].Overflowed = true
x.Keys[i].OverflowPage = existingOverflow.Page
_, err = b.writePage(existingOverflow)
if err != nil {
return err
}
_, err = b.writePage(x)
if err != nil {
return err
}
}
return nil
}
// getPageLock returns the lock for a page
// If the lock does not exist, it creates a new lock
func (b *BTree) getPageLock(pageno int64) *sync.RWMutex {
// Lock the mutex that protects the PageLocks map
b.PageLocksMu.Lock()
defer b.PageLocksMu.Unlock()
// Used for page level locking
// This is decent for concurrent reads and writes
if lock, ok := b.PageLocks[pageno]; ok {
return lock
} else {
// Create a new lock
b.PageLocks[pageno] = &sync.RWMutex{}
return b.PageLocks[pageno]
}
}
// Get page from file.
// Decodes and returns node
func (b *BTree) getPage(pageno int64) (*Node, error) {
// Read the page from the file
page := make([]byte, PAGE_SIZE)
if pageno == 0 { // if pageno is 0, read from the start of the file
_, err := b.File.ReadAt(page, io.SeekStart)
if err != nil {
return nil, fmt.Errorf("failed to read page %d: %w", pageno, err)
}
} else {
_, err := b.File.ReadAt(page, pageno*PAGE_SIZE)
if err != nil {
return nil, fmt.Errorf("failed to read page %d: %w", pageno, err)
}
}
// Unmarshal the page into a node
node, err := decodeNode(page)
if err != nil {
return nil, fmt.Errorf("failed to decode page %d: %w", pageno, err)
}
return node, nil
}
// newPageNumber returns the next page number
// to be used before writing a new page
func (b *BTree) newPageNumber() (int64, error) {
fileInfo, err := b.File.Stat()
if err != nil {
return 0, err
}
return fileInfo.Size() / PAGE_SIZE, nil
}
// writePage encodes a node and writes it to the tree file at node page
func (b *BTree) writePage(n *Node) (int64, error) {
buff, err := encodeNode(n)
if err != nil {
return 0, err
}
if n.Page == 0 {
_, err = b.File.WriteAt(buff, io.SeekStart)
if err != nil {
return 0, err
}
} else {
_, err = b.File.WriteAt(buff, n.Page*PAGE_SIZE)
if err != nil {
return 0, err
}
}
return n.Page, nil
}
// lessThan compares two values and returns true if a is less than b
func lessThan(a, b interface{}) bool {
// check if a and b are the same type
// if not, return false
aT := fmt.Sprintf("%T", a)
bT := fmt.Sprintf("%T", b)
if aT != bT {
return false
}
switch a := a.(type) {
case int:
if b, ok := b.(int); ok {
return a < b
}
case int8:
if b, ok := b.(int8); ok {
return a < b
}
case int16:
if b, ok := b.(int16); ok {
return a < b
}
case int32:
if b, ok := b.(int32); ok {
return a < b
}
case int64:
if b, ok := b.(int64); ok {
return a < b
}
case uint:
if b, ok := b.(uint); ok {
return a < b
}
case uint8:
if b, ok := b.(uint8); ok {
return a < b
}
case uint16:
if b, ok := b.(uint16); ok {
return a < b
}
case uint32:
if b, ok := b.(uint32); ok {
return a < b
}
case uint64:
if b, ok := b.(uint64); ok {
return a < b
}
case float32:
if b, ok := b.(float32); ok {
return a < b
}
case float64:
if b, ok := b.(float64); ok {
return a < b
}
case string:
if b, ok := b.(string); ok {
return a < b
}
case []byte:
if b, ok := b.([]byte); ok {
return bytes.Compare(a, b) < 0
}
}
return false
}
// greaterThan compares two values and returns true if a is greater than b
func greaterThan(a, b interface{}) bool {
// check if a and b are the same type
// if not, return false
aT := fmt.Sprintf("%T", a)
bT := fmt.Sprintf("%T", b)
if aT != bT {
return false
}
switch a := a.(type) {
case int:
if b, ok := b.(int); ok {
return a > b
}
case int8:
if b, ok := b.(int8); ok {
return a > b
}
case int16:
if b, ok := b.(int16); ok {
return a > b
}
case int32:
if b, ok := b.(int32); ok {
return a > b
}
case int64:
if b, ok := b.(int64); ok {
return a > b
}
case uint:
if b, ok := b.(uint); ok {
return a > b
}
case uint8:
if b, ok := b.(uint8); ok {
return a > b
}
case uint16:
if b, ok := b.(uint16); ok {
return a > b
}
case uint32:
if b, ok := b.(uint32); ok {
return a > b
}
case uint64:
if b, ok := b.(uint64); ok {
return a > b
}
case float32:
if b, ok := b.(float32); ok {
return a > b
}
case float64:
if b, ok := b.(float64); ok {
return a > b
}
case string:
if b, ok := b.(string); ok {
return a > b
}
case []byte:
if b, ok := b.([]byte); ok {
return bytes.Compare(a, b) > 0
}
}
return false
}
// equal compares two values and returns true if a is equal than b
func equal(a, b interface{}) bool {
// check if a and b are the same type
// if not, return false
aT := fmt.Sprintf("%T", a)
bT := fmt.Sprintf("%T", b)
if aT != bT {
return false
}
switch a := a.(type) {
case int:
if b, ok := b.(int); ok {
return a == b
}
case int8:
if b, ok := b.(int8); ok {
return a == b
}
case int16:
if b, ok := b.(int16); ok {
return a == b
}
case int32:
if b, ok := b.(int32); ok {
return a == b
}
case int64:
if b, ok := b.(int64); ok {
return a == b
}
case uint:
if b, ok := b.(uint); ok {
return a == b
}
case uint8:
if b, ok := b.(uint8); ok {
return a == b
}
case uint16:
if b, ok := b.(uint16); ok {
return a == b
}
case uint32:
if b, ok := b.(uint32); ok {
return a == b
}
case uint64:
if b, ok := b.(uint64); ok {
return a == b
}
case float32:
if b, ok := b.(float32); ok {
return a == b
}
case float64:
if b, ok := b.(float64); ok {
return a == b
}
case string:
if b, ok := b.(string); ok {
return a == b
}
case []byte:
if b, ok := b.([]byte); ok {
return bytes.Equal(a, b)
}
}
return false
}
// greaterThanEq compares two values and returns true if a greater than or equal to b
func greaterThanEq(a, b interface{}) bool {
// check if a and b are the same type
// if not, return false
aT := fmt.Sprintf("%T", a)
bT := fmt.Sprintf("%T", b)
if aT != bT {
return false
}
switch a := a.(type) {
case int:
if b, ok := b.(int); ok {
return a >= b
}
case int8:
if b, ok := b.(int8); ok {
return a >= b
}
case int16:
if b, ok := b.(int16); ok {
return a >= b
}
case int32:
if b, ok := b.(int32); ok {
return a >= b
}
case int64:
if b, ok := b.(int64); ok {
return a >= b
}
case uint:
if b, ok := b.(uint); ok {
return a >= b
}
case uint8:
if b, ok := b.(uint8); ok {
return a >= b
}
case uint16:
if b, ok := b.(uint16); ok {
return a >= b
}
case uint32:
if b, ok := b.(uint32); ok {
return a >= b
}
case uint64:
if b, ok := b.(uint64); ok {
return a >= b
}
case float32:
if b, ok := b.(float32); ok {
return a >= b
}
case float64:
if b, ok := b.(float64); ok {
return a >= b
}
case string:
if b, ok := b.(string); ok {
return a >= b
}
}
return false
}
// lessThanEq compares two values and returns true if a is less than or equal to b
func lessThanEq(a, b interface{}) bool {
// check if a and b are the same type
// if not, return false
aT := fmt.Sprintf("%T", a)
bT := fmt.Sprintf("%T", b)
if aT != bT {
return false
}
switch a := a.(type) {
case int:
if b, ok := b.(int); ok {
return a <= b
}
case int8:
if b, ok := b.(int8); ok {
return a <= b
}
case int16:
if b, ok := b.(int16); ok {
return a <= b
}
case int32:
if b, ok := b.(int32); ok {
return a <= b
}
case int64:
if b, ok := b.(int64); ok {
return a <= b
}
case uint:
if b, ok := b.(uint); ok {
return a <= b
}
case uint8:
if b, ok := b.(uint8); ok {
return a <= b
}
case uint16:
if b, ok := b.(uint16); ok {
return a <= b
}
case uint32:
if b, ok := b.(uint32); ok {
return a <= b