-
Notifications
You must be signed in to change notification settings - Fork 1
/
lsmt.go
1289 lines (1023 loc) · 29.9 KB
/
lsmt.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 lsmt provides a single-level embedded log-structured merge-tree (LSM-tree)
// Copyright (C) Alex Gaetano Padula
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package lsmt
import (
"bytes"
"encoding/gob"
"errors"
"fmt"
"github.com/guycipher/lsmt/avl"
"io"
"os"
"strings"
"sync"
"sync/atomic"
)
const SSTABLE_EXTENSION = ".sst"
const TOMBSTONE_VALUE = "$tombstone"
const WAL_EXTENSION = ".wal"
// LSMT is the main struct for the log-structured merge-tree.
type LSMT struct {
memtable *avl.AVLTree // The memtable is an in-memory AVL tree.
memtableSize atomic.Int64 // The size of the memtable.
memtableLock *sync.RWMutex // Lock for the memtable.
sstables []*SSTable // The list of current SSTables.
sstablesLock *sync.RWMutex // Lock for the list of SSTables.
directory string // The directory where the SSTables are stored.
memtableFlushSize int // The size at which the memtable should be flushed to disk.
compactionInterval int // The interval at which the LSM-tree should be compacted. (in number of SSTables)
minimumSSTables int // The minimum number of SSTables to keep. On compaction, we will always keep this number of SSTables instead of one large SSTable.
activeTransactions []*Transaction // List of active transactions
wal *Wal // write-ahead log
isFlushing atomic.Int32 // Whether the LSM-tree is flushing
isCompacting atomic.Int32 // Whether the LSM-tree is compacting
cond *sync.Cond // Condition variable for signaling when the LSM-tree is flushing or compacting
}
// Wal is a struct representing a write-ahead log.
type Wal struct {
pager *Pager // The pager for the write-ahead log.
lock *sync.RWMutex // Lock for the write-ahead log.
}
// SSTable is a struct representing a sorted string table.
type SSTable struct {
pager *Pager // The pager for the SSTable.
minKey []byte // The minimum key in the SSTable.
maxKey []byte // The maximum key in the SSTable.
lock *sync.RWMutex // Lock for the SSTable.
}
// OperationType is an enum representing the type of operation.
type OperationType int
const (
OpPut OperationType = iota
OpDelete
)
// Operation is a struct representing an operation in a transaction.
type Operation struct {
Type OperationType
Key []byte // The key of the operation.
Value []byte // Only used for OpPut
}
// Transaction is a struct representing a transaction.
type Transaction struct {
Operations []Operation // List of operations in the transaction.
Aborted bool // Whether the transaction has been aborted.
}
// SSTableIterator is an iterator for SSTable.
type SSTableIterator struct {
pager *Pager
currentPage int64
maxPages int64
}
// New creates a new LSM-tree or opens an existing one.
func New(directory string, directoryPerm os.FileMode, memtableFlushSize, compactionInterval int, minimumSSTables int) (*LSMT, error) {
if directory == "" {
return nil, errors.New("directory cannot be empty")
}
// Check if the directory exists
if _, err := os.Stat(directory); os.IsNotExist(err) {
// Create the directory if it doesn't exist
err = os.Mkdir(directory, directoryPerm)
if err != nil {
return nil, err
}
// Create the write-ahead log
walPager, err := OpenPager(directory+string(os.PathSeparator)+WAL_EXTENSION, os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
return nil, err
}
return &LSMT{
memtable: avl.NewAVLTree(),
memtableLock: &sync.RWMutex{},
sstables: make([]*SSTable, 0),
sstablesLock: &sync.RWMutex{},
directory: directory,
memtableFlushSize: memtableFlushSize,
compactionInterval: compactionInterval,
minimumSSTables: minimumSSTables,
wal: &Wal{lock: &sync.RWMutex{}, pager: walPager},
cond: sync.NewCond(&sync.Mutex{}),
}, nil
} else {
// Open the write-ahead log
walPager, err := OpenPager(directory+string(os.PathSeparator)+WAL_EXTENSION, os.O_RDWR, 0644)
if err != nil {
return nil, err
}
files, err := os.ReadDir(directory)
if err != nil {
return nil, err
}
sstables := make([]*SSTable, 0)
for _, file := range files {
if file.IsDir() {
continue
}
if !strings.HasSuffix(file.Name(), SSTABLE_EXTENSION) {
continue
}
// Open the SSTable file
sstablePager, err := OpenPager(fmt.Sprintf("%s%s%s", directory, string(os.PathSeparator), file.Name()), os.O_RDWR, 0644)
if err != nil {
return nil, err
}
// Create a new SSTable
sstable := &SSTable{
minKey: nil,
maxKey: nil,
lock: &sync.RWMutex{},
pager: sstablePager,
}
// Add the SSTable to the list of SSTables
sstables = append(sstables, sstable)
}
return &LSMT{
memtable: avl.NewAVLTree(),
memtableLock: &sync.RWMutex{},
sstables: sstables,
sstablesLock: &sync.RWMutex{},
directory: directory,
memtableFlushSize: memtableFlushSize,
compactionInterval: compactionInterval,
minimumSSTables: minimumSSTables,
wal: &Wal{lock: &sync.RWMutex{}, pager: walPager},
cond: sync.NewCond(&sync.Mutex{}),
}, nil
}
}
// encodeOperation encodes an operation.
func encodeOperation(op Operation) ([]byte, error) {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err := enc.Encode(op)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// decodeOperation decodes an operation.
func decodeOperation(data []byte) (Operation, error) {
var op Operation
dec := gob.NewDecoder(bytes.NewReader(data))
err := dec.Decode(&op)
if err != nil {
return op, err
}
return op, nil
}
// WriteOperation writes an operation to the write-ahead log.
func (wal *Wal) WriteOperation(op Operation) error {
wal.lock.Lock()
defer wal.lock.Unlock()
encoded, err := encodeOperation(op)
if err != nil {
return err
}
_, err = wal.pager.Write(encoded)
if err != nil {
return err
}
return nil
}
// Recover reads the write-ahead log and recovers the operations.
func (wal *Wal) Recover() ([]Operation, error) {
wal.lock.Lock()
defer wal.lock.Unlock()
var operations []Operation
pageCount := wal.pager.Count()
for i := int64(0); i < pageCount; i++ {
data, err := wal.pager.GetPage(i)
if err != nil {
return nil, err
}
op, err := decodeOperation(data)
if err != nil {
return nil, err
}
operations = append(operations, op)
}
return operations, nil
}
// RunRecoveredOperations runs the recovered operations from the write-ahead log.
func (l *LSMT) RunRecoveredOperations(operations []Operation) error {
for _, op := range operations {
switch op.Type {
case OpPut:
err := l.Put(op.Key, op.Value)
if err != nil {
return err
}
case OpDelete:
err := l.Delete(op.Key)
if err != nil {
return err
}
}
}
return nil
}
// Ok returns whether the iterator is valid.
func (it *SSTableIterator) Ok() bool {
return it.currentPage < it.maxPages
}
// Next returns the next key-value pair from the SSTable.
func (it *SSTableIterator) Next() (*KeyValue, error) {
// Read the next key-value pair from the SSTable.
data, err := it.pager.GetPage(it.currentPage)
if err != nil {
return nil, err
}
kv, err := decodeKv(data)
if err != nil {
return nil, err
}
it.currentPage++
return kv, nil
}
// Put inserts a key-value pair into the LSM-tree.
func (l *LSMT) Put(key, value []byte) error {
// We will first put the key-value pair in the memtable.
// If the memtable size exceeds the flush size, we will flush the memtable to disk.
// Check if we are flushing or compacting
l.cond.L.Lock()
for l.isFlushing.Load() == 1 || l.isCompacting.Load() == 1 {
l.cond.Wait()
}
l.cond.L.Unlock()
// Lock memtable for writing.
l.memtableLock.Lock()
defer l.memtableLock.Unlock()
// Append the operation to the write-ahead log.
err := l.wal.WriteOperation(Operation{
Type: OpPut,
Key: key,
Value: value,
})
if err != nil {
return err
}
// Check if value is tombstone
if bytes.Compare(value, []byte(TOMBSTONE_VALUE)) == 0 {
return errors.New("value cannot be a tombstone")
}
// Put the key-value pair in the memtable.
l.memtable.Insert(key, value)
// If the memtable size exceeds the flush size, flush the memtable to disk.
if l.memtableSize.Load() > int64(l.memtableFlushSize) {
if err := l.flushMemtable(); err != nil {
return err
}
} else {
l.memtableSize.Store(l.memtableSize.Load() + 1)
}
return nil
}
// getSSTableIterator returns an iterator for the SSTable.
func getSSTableIterator(pager *Pager) (*SSTableIterator, error) {
return &SSTableIterator{
pager: pager,
maxPages: pager.PagesCount(),
}, nil
}
// flushMemtable flushes the memtable to disk, creating a new SSTable.
func (l *LSMT) flushMemtable() error {
// We will create a new SSTable from the memtable and add it to the list of SSTables.
// We will then clear the memtable.
l.isFlushing.Store(1)
// Create a new SSTable from the memtable.
sstable, err := l.newSSTable(l.directory, l.memtable)
if err != nil {
l.isFlushing.Store(0)
return err
}
// Lock sstables
l.sstablesLock.Lock()
defer l.sstablesLock.Unlock()
// Add the SSTable to the list of SSTables.
l.sstables = append(l.sstables, sstable)
// Clear the memtable.
l.memtable = avl.NewAVLTree()
l.memtableSize.Swap(0)
// Check the amount of sstables and if we need to compact
if len(l.sstables) > l.compactionInterval {
if err := l.Compact(); err != nil {
l.isFlushing.Store(0)
return err
}
}
l.isFlushing.Store(0)
// Signal the condition variable
l.cond.Broadcast()
return nil
}
// KeyValue is a struct representing a key-value pair.
type KeyValue struct {
Key []byte
Value []byte
}
// newSSTable creates a new SSTable file from the memtable.
func (l *LSMT) newSSTable(directory string, memtable *avl.AVLTree) (*SSTable, error) {
// Create a sorted map from the memtable which will be used to create the SSTable.
sstableSlice := make([]*KeyValue, 0)
memtable.InOrderTraversal(func(node *avl.Node) {
sstableSlice = append(sstableSlice, &KeyValue{Key: node.Key, Value: node.Value})
})
if len(sstableSlice) == 0 {
return nil, nil
}
// Based on amount of sstables we name the file
fileName := fmt.Sprintf("%s%s%d%s", directory, string(os.PathSeparator), len(l.sstables), SSTABLE_EXTENSION)
// Create a new SSTable file.
ssltablePager, err := OpenPager(fileName, os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
return nil, err
}
for _, kv := range sstableSlice {
encoded, err := encodeKv(kv)
if err != nil {
return nil, err
}
_, err = ssltablePager.Write(encoded)
if err != nil {
return nil, err
}
}
return &SSTable{
minKey: sstableSlice[0].Key,
maxKey: sstableSlice[len(sstableSlice)-1].Key,
lock: &sync.RWMutex{},
pager: ssltablePager,
}, nil
}
// encodeKv
func encodeKv(kv *KeyValue) ([]byte, error) {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err := enc.Encode(kv)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// decodeKv
func decodeKv(data []byte) (*KeyValue, error) {
var kv KeyValue
dec := gob.NewDecoder(bytes.NewReader(data))
err := dec.Decode(&kv)
if err != nil {
return nil, err
}
return &kv, nil
}
// Get retrieves the value for a given key from the LSM-tree.
func (l *LSMT) Get(key []byte) ([]byte, error) {
// We will first check the memtable for the key.
// If the key is not found in the memtable, we will search the SSTables.
// Check if we are flushing or compacting
l.cond.L.Lock()
for l.isFlushing.Load() == 1 || l.isCompacting.Load() == 1 {
l.cond.Wait()
}
l.cond.L.Unlock()
// Lock memtable for reading.
l.memtableLock.RLock()
// Check the memtable for the key.
if node := l.memtable.Search(key); node != nil {
l.memtableLock.RUnlock()
if bytes.Compare(node.Value, []byte(TOMBSTONE_VALUE)) == 0 {
return nil, errors.New("key not found")
}
return node.Value, nil
}
l.memtableLock.RUnlock()
// Search the SSTables for the key.
for i := len(l.sstables) - 1; i >= 0; i-- {
sstable := l.sstables[i]
sstable.lock.RLock()
// If the key is not within the range of this SSTable, skip it.
if bytes.Compare(key, sstable.minKey) < 0 || bytes.Compare(key, sstable.maxKey) > 0 {
sstable.lock.RUnlock()
continue
}
// Get an iterator for the SSTable file.
it, err := getSSTableIterator(sstable.pager)
if err != nil {
sstable.lock.RUnlock()
return nil, err
}
// Iterate over the SSTable.
for it.Ok() {
kv, err := it.Next()
if err == io.EOF {
break
} else if err != nil {
sstable.lock.RUnlock()
return nil, err
}
if bytes.Compare(kv.Key, key) == 0 {
sstable.lock.RUnlock()
return kv.Value, nil
}
}
sstable.lock.RUnlock()
}
return nil, errors.New("key not found")
}
// Delete removes a key from the LSM-tree.
func (l *LSMT) Delete(key []byte) error {
// Check if we are flushing or compacting
l.cond.L.Lock()
for l.isFlushing.Load() == 1 || l.isCompacting.Load() == 1 {
l.cond.Wait()
}
l.cond.L.Unlock()
// Append the operation to the write-ahead log.
err := l.wal.WriteOperation(Operation{
Type: OpPut,
Key: key,
})
if err != nil {
return err
}
// We will write a tombstone value to the memtable for the key.
// Lock memtable for writing.
l.memtableLock.Lock()
defer l.memtableLock.Unlock()
// Write a tombstone value to the memtable for the key.
l.memtable.Insert(key, []byte(TOMBSTONE_VALUE))
return nil
}
// Compact compacts the LSM-tree by merging all SSTables into a single SSTable.
func (l *LSMT) Compact() error {
l.isCompacting.Store(1)
// Create a new empty memtable.
newMemtable := avl.NewAVLTree()
// Iterate over all existing SSTables.
for _, sstable := range l.sstables {
// Read all key-value pairs from the SSTable.
// Get an iterator for the SSTable file.
it, err := getSSTableIterator(sstable.pager)
if err != nil {
return err
}
// Iterate over the SSTable.
for it.Ok() {
kv, err := it.Next()
if err == io.EOF {
break
} else if err != nil {
return err
}
if bytes.Compare(kv.Value, []byte(TOMBSTONE_VALUE)) == 0 { // If the value is a tombstone, skip this key-value pair
continue
}
// If the value is not a tombstone, add it to the new memtable.
newMemtable.Insert(kv.Key, kv.Value)
}
sstable.pager.Close() // Close the SSTable pager.
}
// We remove all the sstables in the directory lmst directory..
files, err := os.ReadDir(l.directory)
if err != nil {
return err
}
for _, file := range files {
if file.IsDir() {
continue
}
if !strings.HasSuffix(file.Name(), SSTABLE_EXTENSION) {
continue
}
err = os.Remove(fmt.Sprintf("%s%s%s", l.directory, string(os.PathSeparator), file.Name()))
if err != nil {
return err
}
err = os.Remove(fmt.Sprintf("%s%s%s", l.directory, string(os.PathSeparator), file.Name()+".del"))
if err != nil {
return err
}
}
// Clear the sstables
l.sstables = make([]*SSTable, 0)
// Flush the new memtable to disk, creating a new SSTable.
newSSTable, err := l.newSSTable(l.directory, newMemtable)
if err != nil {
return err
}
// Replace the list of old SSTables with the new SSTable.
l.sstables = []*SSTable{newSSTable}
// We will now split the sstable into smaller sstables
l.sstables, err = l.SplitSSTable(newSSTable, l.minimumSSTables)
if err != nil {
return err
}
l.isFlushing.Store(0)
// Signal the condition variable
l.cond.Broadcast()
return nil
}
// Close closes the LSM-tree gracefully closing all opened SSTable files.
func (l *LSMT) Close() error {
// Check size of memtable
if l.memtableSize.Load() > 0 {
// Flush the memtable to disk.
if err := l.flushMemtable(); err != nil {
return err
}
}
// Close the write-ahead log.
if err := l.wal.pager.Close(); err != nil {
return err
}
if len(l.sstables) > 0 {
// Close all SSTable pagers.
for _, sstable := range l.sstables {
if sstable.pager != nil {
if err := sstable.pager.Close(); err != nil {
return err
}
}
}
}
return nil
}
// SplitSSTable splits a compacted SSTable into n smaller SSTables.
func (l *LSMT) SplitSSTable(sstable *SSTable, n int) ([]*SSTable, error) {
memTables := make([]*avl.AVLTree, n)
for i := 0; i < n; i++ {
memTables[i] = avl.NewAVLTree()
}
memtSeq := 0
if sstable == nil {
return nil, nil
}
// Get an iterator for the SSTable file.
it, err := getSSTableIterator(sstable.pager)
if err != nil {
return nil, err
}
// Iterate over the SSTable.
for it.Ok() {
kv, err := it.Next()
if err == io.EOF {
break
} else if err != nil {
return nil, err
}
// If the value is a tombstone, skip this key-value pair.
if bytes.Compare(kv.Value, []byte(TOMBSTONE_VALUE)) == 0 {
continue
}
// If the value is not a tombstone, add it to the memtable.
if memtSeq < len(memTables) {
memTables[memtSeq].Insert(kv.Key, kv.Value)
// If we have reached the size of the memtable, flush it to disk.
if memTables[memtSeq].GetSize() >= l.memtableFlushSize {
memtSeq++
}
}
}
// Close the SSTable pager.
sstable.pager.Close()
// delete the sstable file
err = os.Remove(sstable.pager.file.Name())
if err != nil {
return nil, err
}
sstables := make([]*SSTable, n)
for i := 0; i < n; i++ {
sst, err := l.newSSTable(l.directory, memTables[i])
if err != nil {
return nil, err
}
if sst == nil {
continue
}
sstables[i] = sst
}
return sstables, nil
}
// Range retrieves all key-value pairs within a given range from the LSM-tree.
func (l *LSMT) Range(start, end []byte) ([][]byte, [][]byte, error) {
// We will first check the memtable for the range.
// If the range is not found in the memtable, we will search the SSTables.
// Check if we are flushing or compacting
l.cond.L.Lock()
for l.isFlushing.Load() == 1 || l.isCompacting.Load() == 1 {
l.cond.Wait()
}
l.cond.L.Unlock()
// Lock memtable for reading.
l.memtableLock.RLock()
// Check the memtable for the range.
var keys [][]byte
var values [][]byte
l.memtable.InOrderTraversal(func(node *avl.Node) {
if bytes.Compare(node.Key, start) >= 0 && bytes.Compare(node.Key, end) <= 0 {
keys = append(keys, node.Key)
values = append(values, node.Value)
}
})
l.memtableLock.RUnlock()
// Search the SSTables for the range.
for i := len(l.sstables) - 1; i >= 0; i-- {
sstable := l.sstables[i]
sstable.lock.RLock()
// If the range is not within the range of this SSTable, skip it.
if bytes.Compare(start, sstable.minKey) < 0 || bytes.Compare(end, sstable.maxKey) > 0 {
continue
}
// Get an iterator for the SSTable file.
it, err := getSSTableIterator(sstable.pager)
if err != nil {
sstable.lock.RUnlock()
return nil, nil, err
}
// Iterate over the SSTable.
for it.Ok() {
kv, err := it.Next()
if err == io.EOF {
break
} else if err != nil {
sstable.lock.RUnlock()
return nil, nil, err
}
if bytes.Compare(kv.Key, start) >= 0 && bytes.Compare(kv.Key, end) <= 0 {
keys = append(keys, kv.Key)
values = append(values, kv.Value)
}
}
sstable.lock.RUnlock()
}
return keys, values, nil
}
// NRange retrieves all key-value pairs not within a given range from the LSM-tree.
func (l *LSMT) NRange(start, end []byte) ([][]byte, [][]byte, error) {
// We will first check the memtable for the range.
// If the range is not found in the memtable, we will search the SSTables.
// Check if we are flushing or compacting
l.cond.L.Lock()
for l.isFlushing.Load() == 1 || l.isCompacting.Load() == 1 {
l.cond.Wait()
}
l.cond.L.Unlock()
// Lock memtable for reading.
l.memtableLock.RLock()
// Check the memtable for the range.
var keys [][]byte
var values [][]byte
l.memtable.InOrderTraversal(func(node *avl.Node) {
if bytes.Compare(node.Key, start) < 0 || bytes.Compare(node.Key, end) > 0 {
keys = append(keys, node.Key)
values = append(values, node.Value)
}
})
l.memtableLock.RUnlock()
// Search the SSTables for the range.
for i := len(l.sstables) - 1; i >= 0; i-- {
sstable := l.sstables[i]
sstable.lock.RLock()
// If the range is not within the range of this SSTable, skip it.
if bytes.Compare(start, sstable.minKey) < 0 || bytes.Compare(end, sstable.maxKey) > 0 {
continue
}
// Get an iterator for the SSTable file.
it, err := getSSTableIterator(sstable.pager)
if err != nil {
sstable.lock.RUnlock()
return nil, nil, err
}
// Iterate over the SSTable.
for it.Ok() {
kv, err := it.Next()
if err == io.EOF {
break
} else if err != nil {
sstable.lock.RUnlock()
return nil, nil, err
}
if bytes.Compare(kv.Key, start) < 0 || bytes.Compare(kv.Key, end) > 0 {
keys = append(keys, kv.Key)
values = append(values, kv.Value)
}
}
sstable.lock.RUnlock()
}
return keys, values, nil
}
// GreaterThan retrieves all key-value pairs greater than the key from the LSM-tree.
func (l *LSMT) GreaterThan(key []byte) ([][]byte, [][]byte, error) {
// We will first check the memtable for the range.
// If the range is not found in the memtable, we will search the SSTables.
// Check if we are flushing or compacting
l.cond.L.Lock()
for l.isFlushing.Load() == 1 || l.isCompacting.Load() == 1 {
l.cond.Wait()
}
l.cond.L.Unlock()
// Lock memtable for reading.
l.memtableLock.RLock()
// Check the memtable for the range.
var keys [][]byte
var values [][]byte
l.memtable.InOrderTraversal(func(node *avl.Node) {
if bytes.Compare(node.Key, key) > 0 {
keys = append(keys, node.Key)
values = append(values, node.Value)
}
})
l.memtableLock.RUnlock()
// Search the SSTables for the range.
for i := len(l.sstables) - 1; i >= 0; i-- {
sstable := l.sstables[i]
sstable.lock.RLock()
// Get an iterator for the SSTable file.
it, err := getSSTableIterator(sstable.pager)
if err != nil {
sstable.lock.RUnlock()
return nil, nil, err
}
// Iterate over the SSTable.
for it.Ok() {
kv, err := it.Next()
if err == io.EOF {
break
} else if err != nil {
sstable.lock.RUnlock()
return nil, nil, err
}
if bytes.Compare(kv.Key, key) > 0 {
keys = append(keys, kv.Key)
values = append(values, kv.Value)
}
}
sstable.lock.RUnlock()
}
return keys, values, nil
}
// GreaterThanEqual retrieves all key-value pairs greater than or equal to the key from the LSM-tree.
func (l *LSMT) GreaterThanEqual(key []byte) ([][]byte, [][]byte, error) {
// We will first check the memtable for the range.
// If the range is not found in the memtable, we will search the SSTables.
// Check if we are flushing or compacting
l.cond.L.Lock()
for l.isFlushing.Load() == 1 || l.isCompacting.Load() == 1 {
l.cond.Wait()
}
l.cond.L.Unlock()
// Lock memtable for reading.
l.memtableLock.RLock()
// Check the memtable for the range.
var keys [][]byte
var values [][]byte
l.memtable.InOrderTraversal(func(node *avl.Node) {
if bytes.Compare(node.Key, key) >= 0 {
keys = append(keys, node.Key)
values = append(values, node.Value)
}
})
l.memtableLock.RUnlock()
// Search the SSTables for the range.
for i := len(l.sstables) - 1; i >= 0; i-- {