-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor storage code and update data structure methods and fix sync …
…issue
- Loading branch information
Showing
16 changed files
with
238 additions
and
159 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,46 @@ | ||
# Nodis | ||
|
||
[English](https://github.com/diiyw/nodis/blob/main/README.md) | 简体中文 | ||
|
||
Nodis 是一个简单可嵌入到应用中内存数据库,实现Redis的数据结构。 | ||
|
||
## 支持的类型 | ||
|
||
- String | ||
- List | ||
- Hash | ||
- Set | ||
- Sorted Set | ||
|
||
## 特点 | ||
|
||
- 快速可嵌入的 | ||
- 低内存使用,只有热数据才在内存中 | ||
- 快照和WAL存储的支持 | ||
|
||
## Get Started | ||
|
||
```bash | ||
go get github.com/diiyw/nodis | ||
``` | ||
|
||
```go | ||
package main | ||
|
||
import "github.com/diiyw/nodis" | ||
|
||
func main() { | ||
// Create a new Nodis instance | ||
opt := nodis.DefaultOptions | ||
n := nodis.Open(opt) | ||
|
||
// Set a key-value pair | ||
n.Set("key", []byte("value"), 0) | ||
n.LPush("list", []byte("value1")) | ||
} | ||
|
||
``` | ||
|
||
## Note | ||
|
||
Nodis 实现了Redis的数据结构. 但是并不是完整的Redis Server服务,它只是可以方便的切入到各自的应用使用 |
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
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
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
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,84 @@ | ||
package nodis | ||
|
||
import ( | ||
"errors" | ||
"hash/crc32" | ||
|
||
"github.com/diiyw/nodis/ds" | ||
"github.com/diiyw/nodis/ds/hash" | ||
"github.com/diiyw/nodis/ds/list" | ||
"github.com/diiyw/nodis/ds/set" | ||
"github.com/diiyw/nodis/ds/str" | ||
"github.com/diiyw/nodis/ds/zset" | ||
"github.com/kelindar/binary" | ||
) | ||
|
||
var ( | ||
ErrCorruptedData = errors.New("corrupted data") | ||
) | ||
|
||
type Entry struct { | ||
Key string | ||
Value ds.DataStruct | ||
ExpiredAt int64 | ||
} | ||
|
||
type entryBlock struct { | ||
Crc32 uint32 | ||
Type ds.DataType | ||
Body []byte | ||
} | ||
|
||
// newEntry creates a new entry | ||
func newEntry(key string, value ds.DataStruct, expiredAt int64) *Entry { | ||
return &Entry{ | ||
Key: key, | ||
Value: value, | ||
ExpiredAt: expiredAt, | ||
} | ||
} | ||
|
||
// Marshal marshals the entry | ||
func (e *Entry) Marshal() ([]byte, error) { | ||
var err error | ||
data, err := binary.Marshal(e) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var buf = make([]byte, len(data)+1, len(data)+1) | ||
buf[0] = byte(e.Value.GetType()) | ||
copy(buf[1:], data) | ||
var block = entryBlock{ | ||
Crc32: crc32.ChecksumIEEE(buf), | ||
Type: e.Value.GetType(), | ||
Body: data, | ||
} | ||
return binary.Marshal(block) | ||
} | ||
|
||
// Unmarshal unmarshals the entry | ||
func (e *Entry) Unmarshal(data []byte) error { | ||
var block entryBlock | ||
if err := binary.Unmarshal(data, &block); err != nil { | ||
return err | ||
} | ||
var buf = make([]byte, len(block.Body)+1, len(block.Body)+1) | ||
buf[0] = byte(block.Type) | ||
copy(buf[1:], block.Body) | ||
if block.Crc32 != crc32.ChecksumIEEE(buf) { | ||
return ErrCorruptedData | ||
} | ||
switch block.Type { | ||
case ds.String: | ||
e.Value = str.NewString() | ||
case ds.ZSet: | ||
e.Value = zset.NewSortedSet() | ||
case ds.List: | ||
e.Value = list.NewDoublyLinkedList() | ||
case ds.Hash: | ||
e.Value = hash.NewHashMap() | ||
case ds.Set: | ||
e.Value = set.NewSet() | ||
} | ||
return binary.Unmarshal(block.Body, e) | ||
} |
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
Oops, something went wrong.