diff --git a/cmd/ledis-dump/main.go b/cmd/ledis-dump/main.go new file mode 100644 index 0000000..bf02ad7 --- /dev/null +++ b/cmd/ledis-dump/main.go @@ -0,0 +1,62 @@ +package main + +import ( + "bufio" + "flag" + "fmt" + "github.com/siddontang/ledisdb/server" + "net" + "os" +) + +var host = flag.String("host", "127.0.0.1", "ledis server host") +var port = flag.Int("port", 6380, "ledis server port") +var sock = flag.String("sock", "", "ledis unix socket domain") +var dumpFile = flag.String("o", "./ledis.dump", "dump file to save") + +var fullSyncCmd = []byte("*1\r\n$8\r\nfullsync\r\n") //fullsync + +func main() { + flag.Parse() + + var c net.Conn + var err error + var f *os.File + + if f, err = os.OpenFile(*dumpFile, os.O_CREATE|os.O_WRONLY, os.ModePerm); err != nil { + println(err.Error()) + return + } + + defer f.Close() + + if len(*sock) != 0 { + c, err = net.Dial("unix", *sock) + } else { + addr := fmt.Sprintf("%s:%d", *host, *port) + c, err = net.Dial("tcp", addr) + } + + if err != nil { + println(err.Error()) + return + } + + defer c.Close() + + println("dump begin") + + if _, err = c.Write(fullSyncCmd); err != nil { + println(err.Error()) + return + } + + rb := bufio.NewReaderSize(c, 16*1024) + + if err = server.ReadBulkTo(rb, f); err != nil { + println(err.Error()) + return + } + + println("dump end") +} diff --git a/server/client.go b/server/client.go index bb4fe0e..11c9f97 100644 --- a/server/client.go +++ b/server/client.go @@ -81,7 +81,7 @@ func (c *client) run() { } func (c *client) readLine() ([]byte, error) { - return readLine(c.rb) + return ReadLine(c.rb) } //A client sends to the Redis server a RESP Array consisting of just Bulk Strings. diff --git a/server/replication.go b/server/replication.go index 6c30968..ee74466 100644 --- a/server/replication.go +++ b/server/replication.go @@ -254,7 +254,7 @@ func (m *master) fullSync() error { defer os.Remove(dumpPath) - err = readBulkTo(m.rb, f) + err = ReadBulkTo(m.rb, f) f.Close() if err != nil { log.Error("read dump data error %s", err.Error()) @@ -291,7 +291,7 @@ func (m *master) sync() error { m.syncBuf.Reset() - err := readBulkTo(m.rb, &m.syncBuf) + err := ReadBulkTo(m.rb, &m.syncBuf) if err != nil { return err } diff --git a/server/util.go b/server/util.go index 9afee4e..49c20ba 100644 --- a/server/util.go +++ b/server/util.go @@ -14,7 +14,7 @@ var ( errLineFormat = errors.New("bad response line format") ) -func readLine(rb *bufio.Reader) ([]byte, error) { +func ReadLine(rb *bufio.Reader) ([]byte, error) { p, err := rb.ReadSlice('\n') if err != nil { @@ -27,8 +27,8 @@ func readLine(rb *bufio.Reader) ([]byte, error) { return p[:i], nil } -func readBulkTo(rb *bufio.Reader, w io.Writer) error { - l, err := readLine(rb) +func ReadBulkTo(rb *bufio.Reader, w io.Writer) error { + l, err := ReadLine(rb) if len(l) == 0 { return errBulkFormat } else if l[0] == '$' { @@ -43,7 +43,7 @@ func readBulkTo(rb *bufio.Reader, w io.Writer) error { return err } - if l, err = readLine(rb); err != nil { + if l, err = ReadLine(rb); err != nil { return err } else if len(l) != 0 { return errBulkFormat