-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: abstract encoder for all data structures * feat: support rdb persistance * feat: support save command --------- Co-authored-by: guangzhixu <[email protected]>
- Loading branch information
1 parent
4971cae
commit d392d29
Showing
41 changed files
with
1,377 additions
and
1,187 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,6 @@ coverage.* | |
*.aof | ||
.idea/ | ||
redis | ||
rotom | ||
rotom | ||
bench/ | ||
*.rdb |
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 |
---|---|---|
@@ -1,74 +1,73 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"os" | ||
|
||
"github.com/tidwall/mmap" | ||
) | ||
|
||
const ( | ||
KB = 1024 | ||
MB = 1024 * KB | ||
GB = 1024 * MB | ||
) | ||
|
||
// Aof manages an append-only file system for storing data. | ||
type Aof struct { | ||
filePath string | ||
file *os.File | ||
buf *bytes.Buffer | ||
} | ||
|
||
func NewAof(path string) (*Aof, error) { | ||
fd, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0666) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &Aof{ | ||
file: fd, | ||
filePath: path, | ||
buf: bytes.NewBuffer(make([]byte, 0, KB)), | ||
}, nil | ||
} | ||
|
||
func (aof *Aof) Close() error { | ||
return aof.file.Close() | ||
} | ||
|
||
func (aof *Aof) Write(buf []byte) (int, error) { | ||
return aof.buf.Write(buf) | ||
} | ||
|
||
func (aof *Aof) Flush() error { | ||
aof.buf.WriteTo(aof.file) | ||
return aof.file.Sync() | ||
} | ||
|
||
func (aof *Aof) Read(fn func(args []RESP)) error { | ||
// Read file data by mmap. | ||
data, err := mmap.Open(aof.filePath, false) | ||
if len(data) == 0 { | ||
return nil | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Iterate over the records in the file, applying the function to each. | ||
reader := NewReader(data) | ||
argsBuf := make([]RESP, 8) | ||
for { | ||
args, _, err := reader.ReadNextCommand(argsBuf) | ||
if err != nil { | ||
if err == io.EOF { | ||
break | ||
} | ||
return err | ||
} | ||
fn(args) | ||
} | ||
|
||
return nil | ||
} | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"github.com/xgzlucario/rotom/internal/resp" | ||
"io" | ||
"os" | ||
|
||
"github.com/tidwall/mmap" | ||
) | ||
|
||
const ( | ||
KB = 1024 | ||
MB = 1024 * KB | ||
GB = 1024 * MB | ||
) | ||
|
||
// Aof manages an append-only file system for storing data. | ||
type Aof struct { | ||
file *os.File | ||
buf *bytes.Buffer | ||
} | ||
|
||
func NewAof(path string) (*Aof, error) { | ||
fd, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0666) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &Aof{ | ||
file: fd, | ||
buf: bytes.NewBuffer(make([]byte, 0, KB)), | ||
}, nil | ||
} | ||
|
||
func (a *Aof) Close() error { | ||
return a.file.Close() | ||
} | ||
|
||
func (a *Aof) Write(buf []byte) (int, error) { | ||
return a.buf.Write(buf) | ||
} | ||
|
||
func (a *Aof) Flush() error { | ||
_, _ = a.buf.WriteTo(a.file) | ||
return a.file.Sync() | ||
} | ||
|
||
func (a *Aof) Read(fn func(args []resp.RESP)) error { | ||
// Read file data by mmap. | ||
data, err := mmap.MapFile(a.file, false) | ||
if len(data) == 0 { | ||
return nil | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Iterate over the records in the file, applying the function to each. | ||
reader := resp.NewReader(data) | ||
argsBuf := make([]resp.RESP, 8) | ||
for { | ||
args, _, err := reader.ReadNextCommand(argsBuf) | ||
if err != nil { | ||
if err == io.EOF { | ||
break | ||
} | ||
return err | ||
} | ||
fn(args) | ||
} | ||
|
||
return nil | ||
} |
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.