Skip to content
This repository was archived by the owner on Mar 21, 2025. It is now read-only.

Commit 6aa0741

Browse files
committed
skiplist minor correction on node Size() method; We must take into accout time, and handle 32 bit or 64 bit. New EscalateFlush and EscalateCompaction methods
1 parent b2a6783 commit 6aa0741

File tree

4 files changed

+35
-5
lines changed

4 files changed

+35
-5
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,13 @@ func (txn *Transaction) Commit(k4 *K4) error
272272
// Recover from WAL file
273273
// WAL file must be placed in the data directory
274274
func (k4 *K4) RecoverFromWAL() error
275+
276+
// EscalateFlush escalates the memtable flush to the background queue
277+
func (k4 *K4) EscalateFlush() error
278+
279+
// EscalateCompaction escalates a compaction operation
280+
func (k4 *K4) EscalateCompaction() error
281+
275282
```
276283
277284
### What can you build with K4?

skiplist/skiplist.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"bytes"
3535
"math/rand"
3636
"time"
37+
"unsafe"
3738
)
3839

3940
const TOMBSTONE_VALUE = "$tombstone" // This is specific to k4
@@ -87,7 +88,12 @@ func NewNode(level int, key, value []byte, ttl *time.Duration) *Node {
8788

8889
// Size returns the size of the node in bytes
8990
func (n *Node) Size() int {
90-
return len(n.key) + len(n.value) + len(n.forward)*8 // assuming 64-bit pointers
91+
size := len(n.key) + len(n.value) + len(n.forward)*int(unsafe.Sizeof(uintptr(0)))
92+
if n.ttl != nil {
93+
size += int(unsafe.Sizeof(*n.ttl))
94+
}
95+
96+
return size
9197
}
9298

9399
// IsExpired checks if the node is expired

v2/k4.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,7 +1166,7 @@ func (txn *Transaction) Commit(k4 *K4) error {
11661166
}
11671167

11681168
// Check if memtable needs to be flushed
1169-
if k4.memtable.Size() > k4.memtableFlushThreshold {
1169+
if k4.memtable.Size() >= k4.memtableFlushThreshold {
11701170
k4.appendMemtableToFlushQueue() // Append memtable to flush queue
11711171
}
11721172

@@ -1369,7 +1369,7 @@ func (k4 *K4) Put(key, value []byte, ttl *time.Duration) error {
13691369
k4.memtable.Insert(key, value, ttl) // insert the key value pair into the memtable
13701370

13711371
// Check if memtable needs to be flushed
1372-
if k4.memtable.Size() > k4.memtableFlushThreshold {
1372+
if k4.memtable.Size() >= k4.memtableFlushThreshold {
13731373
k4.appendMemtableToFlushQueue()
13741374
}
13751375

@@ -2065,3 +2065,20 @@ func (it *Iterator) Reset() {
20652065
it.prevStarted = false // We reset the prevStarted to false
20662066

20672067
}
2068+
2069+
// EscalateFlush is a public method to force flush memtable to queue
2070+
func (k4 *K4) EscalateFlush() error {
2071+
// Lock the memtable
2072+
k4.memtableLock.Lock()
2073+
defer k4.memtableLock.Unlock()
2074+
2075+
// Append memtable to flush queue
2076+
k4.appendMemtableToFlushQueue()
2077+
2078+
return nil
2079+
}
2080+
2081+
// EscalateCompaction is a public method to force compaction
2082+
func (k4 *K4) EscalateCompaction() error {
2083+
return k4.compact()
2084+
}

v2/k4_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,7 +1303,7 @@ func TestCompressCompaction(t *testing.T) {
13031303
dir := setup(t)
13041304
defer teardown(dir)
13051305

1306-
k4, err := Open(dir, 12196/4, 2, true, true)
1306+
k4, err := Open(dir, 12196/4, 3000, true, true)
13071307
if err != nil {
13081308
t.Fatalf("Failed to open K4: %v", err)
13091309
}
@@ -1320,7 +1320,7 @@ func TestCompressCompaction(t *testing.T) {
13201320

13211321
}
13221322

1323-
time.Sleep(8 * time.Second)
1323+
k4.compact() // now we will compact and close
13241324

13251325
k4.Close()
13261326

0 commit comments

Comments
 (0)