-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
65 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.tomfran.lsm.tree; | ||
|
||
import com.tomfran.lsm.memtable.Memtable; | ||
import com.tomfran.lsm.sstable.SSTable; | ||
import com.tomfran.lsm.types.Item; | ||
|
||
import java.util.LinkedList; | ||
|
||
public class LSMTree { | ||
|
||
static final int MEMTABLE_MAX_SIZE = 1 << 16; | ||
|
||
Memtable mutableMemtable; | ||
LinkedList<Memtable> immutableMemtables; | ||
LinkedList<SSTable> tables; | ||
|
||
public LSMTree() { | ||
mutableMemtable = new Memtable(MEMTABLE_MAX_SIZE); | ||
immutableMemtables = new LinkedList<>(); | ||
tables = new LinkedList<>(); | ||
} | ||
|
||
public void add(Item item) { | ||
mutableMemtable.add(item); | ||
checkMemtableSize(); | ||
} | ||
|
||
public void delete(byte[] key) { | ||
mutableMemtable.remove(key); | ||
checkMemtableSize(); | ||
} | ||
|
||
private void checkMemtableSize() { | ||
if (mutableMemtable.size() >= MEMTABLE_MAX_SIZE) { | ||
immutableMemtables.add(mutableMemtable); | ||
mutableMemtable = new Memtable(MEMTABLE_MAX_SIZE); | ||
} | ||
} | ||
|
||
public Item get(byte[] key) { | ||
Item result; | ||
|
||
if ((result = mutableMemtable.get(key)) != null) | ||
return result; | ||
|
||
for (Memtable memtable : immutableMemtables) | ||
if ((result = memtable.get(key)) != null) | ||
return result; | ||
|
||
for (SSTable table : tables) | ||
if ((result = table.get(key)) != null) | ||
return result; | ||
|
||
return null; | ||
} | ||
} |