Skip to content

Commit

Permalink
0.1.0版
Browse files Browse the repository at this point in the history
  • Loading branch information
JameyWoo committed Jan 21, 2021
1 parent d015386 commit 35657d9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
22 changes: 11 additions & 11 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
)

type DB struct {
eng *Engine
wal *os.File
memory *Engine
wal *os.File

dir string
walPath string
Expand Down Expand Up @@ -49,15 +49,15 @@ func Open(dirPath string) (*DB, error) {
wal = wa
}

return &DB{eng: NewEngine(), wal: wal, dir: dirPath, walPath: walPath}, nil
return &DB{memory: NewEngine(), wal: wal, dir: dirPath, walPath: walPath}, nil
}

func (db *DB) Close() {
db.wal.Close()
}

func (db *DB) Get(key string) (string, error) {
value, err := db.eng.Get(key)
value, err := db.memory.Get(key)
// 如果没有得到value
if err == nil {
return value, err
Expand All @@ -69,29 +69,29 @@ func (db *DB) Get(key string) (string, error) {
func (db *DB) Put(kv KeyValue) error {
// TODO: 改进写日志的方式
db.writeLogPut(kv)
db.eng.Put(kv)
db.memory.Put(kv)
// 这个阈值用常量 maxMemSize表示, maxMemSize定义在engine中, 后续改为可配置的量
if db.eng.memSize >= maxMemSize {
if db.memory.memSize >= maxMemSize {
// 刷到磁盘
db.flush()
// TODO: 之后实现异步的flush操作
db.eng.memStore = make(map[string]string)
db.memory.memStore = make(map[string]string)
// ! 这个也要重置
db.eng.memSize = 0
db.memory.memSize = 0
}
return nil
}

// 删除的元素的value用特殊的字符串来代替
func (db *DB) Delete(key string) error {
db.writeLogDelete(key)
return db.eng.Delete(key)
return db.memory.Delete(key)
}

// 扫描一个区间的key, 得到key value的结果slice
// 如果value为deleted, 那么不添加
func (db *DB) Scan(startKey, endKey string) ([]KeyValue, error) {
return db.eng.Scan(startKey, endKey)
return db.memory.Scan(startKey, endKey)
}

// Put的wal, 后续需要将WAL单独写成一个分离的结构
Expand Down Expand Up @@ -130,7 +130,7 @@ func (db *DB) flush() error {
fileId := len(files)

fileStr := ""
for key, val := range db.eng.memStore {
for key, val := range db.memory.memStore {
// 编码, [keyLen, key, valueLen, value]
keyLen, valueLen := make([]byte, 4), make([]byte, 4)
binary.LittleEndian.PutUint32(keyLen, uint32(len(key)))
Expand Down
19 changes: 13 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,20 @@ A persistent, LSM tree structured key value database engine implemented by go la
- [x] 接口: Get, Put, Delete, Scan
- [x] 布隆过滤器算法版 (暂不集成)
- [x] map版 Memstore
- [ ] 基本SSTable结构 (追加, 无序, 递增文件序号)
- [ ] LSM-Tree结构
- [ ] 实现key-value接口, 包括时间戳
- [ ] 实现WAL存储 (暂不实现故障恢复功能)
- [x] 基本SSTable结构 (追加, 无序, 递增文件序号)
- [x] LSM-Tree结构
- [x] 实现WAL存储 (暂不实现故障恢复功能)


## 其他待实现的feature
## 全部待实现的feature

- [x] 接口: Get, Put, Delete, Scan
- [x] 布隆过滤器算法版 (暂不集成)
- [x] map版 Memstore
- [x] 基本SSTable结构 (追加, 无序, 递增文件序号)
- [x] 基本LSM-Tree结构
- [x] 实现WAL存储 (暂不实现故障恢复功能)
- [ ] 实现完整的key-value结构, 包括时间戳
- [ ] 读取一个配置文件
- [ ] SkipList版 Memstore
- [ ] 支持并发的 SkipList
Expand All @@ -40,4 +46,5 @@ A persistent, LSM tree structured key value database engine implemented by go la
- [ ] 考虑键值分离的结构, 类似boltDB
- [ ] 使用标准使用接口改造项目调用方式
- [ ] 实现可变长度整形 varint 从而更好地压缩 (binary中有实现)
- [ ] 实现字符串压缩算法
- [ ] 实现字符串压缩算法
- [ ] 打开数据库时增加选项设置

0 comments on commit 35657d9

Please sign in to comment.