From 662a4130b45987c18c6cad5ade1d2742cae12a6e Mon Sep 17 00:00:00 2001 From: siddontang Date: Wed, 5 Nov 2014 17:34:14 +0800 Subject: [PATCH] update file rpl store --- rpl/file_store.go | 15 ++++++++++++--- rpl/file_table.go | 12 ------------ rpl/file_table_test.go | 9 +++++++++ rpl/log.go | 39 ++++++++++++++++++++++++--------------- 4 files changed, 45 insertions(+), 30 deletions(-) delete mode 100644 rpl/file_table.go create mode 100644 rpl/file_table_test.go diff --git a/rpl/file_store.go b/rpl/file_store.go index 1c85846..27b9e30 100644 --- a/rpl/file_store.go +++ b/rpl/file_store.go @@ -18,20 +18,29 @@ const ( //why 4G, we can use uint32 as offset, reduce memory useage maxLogFileSize = uint32(4*1024*1024*1024 - 1) + + maxLogNumInFile = uint64(10000000) ) /* File Store: - 00000001.log - 00000002.log + 00000001.ldb + 00000002.ldb log: log1 data | log2 data | split data | log1 offset | log 2 offset | offset start pos | offset length | magic data log id can not be 0, we use here for split data if data has no magic data, it means that we don't close replication gracefully. so we must repair the log data + log data: id (bigendian uint64), create time (bigendian uint32), compression (byte), data len(bigendian uint32), data split data = log0 data - log0: id 0, create time 0, compression 0, data "" + log0: id 0, create time 0, compression 0, data len 0, data "" + + log offset: bigendian uint32 | bigendian uint32 + + offset start pos: bigendian uint64 + offset length: bigendian uint32 + //sha1 of github.com/siddontang/ledisdb 20 bytes magic data = "\x1c\x1d\xb8\x88\xff\x9e\x45\x55\x40\xf0\x4c\xda\xe0\xce\x47\xde\x65\x48\x71\x17" diff --git a/rpl/file_table.go b/rpl/file_table.go deleted file mode 100644 index 92f1309..0000000 --- a/rpl/file_table.go +++ /dev/null @@ -1,12 +0,0 @@ -package rpl - -import ( - "os" -) - -type table struct { - f *os.File - - first uint64 - last uint64 -} diff --git a/rpl/file_table_test.go b/rpl/file_table_test.go new file mode 100644 index 0000000..82efade --- /dev/null +++ b/rpl/file_table_test.go @@ -0,0 +1,9 @@ +package rpl + +import ( + "testing" +) + +func TestFileTable(t *testing.T) { + +} diff --git a/rpl/log.go b/rpl/log.go index 261e852..382dff5 100644 --- a/rpl/log.go +++ b/rpl/log.go @@ -69,24 +69,11 @@ func (l *Log) Encode(w io.Writer) error { } func (l *Log) Decode(r io.Reader) error { - buf := make([]byte, l.HeadSize()) - - if _, err := io.ReadFull(r, buf); err != nil { + length, err := l.DecodeHead(r) + if err != nil { return err } - pos := 0 - l.ID = binary.BigEndian.Uint64(buf[pos:]) - pos += 8 - - l.CreateTime = binary.BigEndian.Uint32(buf[pos:]) - pos += 4 - - l.Compression = uint8(buf[pos]) - pos++ - - length := binary.BigEndian.Uint32(buf[pos:]) - l.Data = l.Data[0:0] if cap(l.Data) >= int(length) { @@ -100,3 +87,25 @@ func (l *Log) Decode(r io.Reader) error { return nil } + +func (l *Log) DecodeHead(r io.Reader) (uint32, error) { + buf := make([]byte, l.HeadSize()) + + if _, err := io.ReadFull(r, buf); err != nil { + return 0, err + } + + pos := 0 + l.ID = binary.BigEndian.Uint64(buf[pos:]) + pos += 8 + + l.CreateTime = binary.BigEndian.Uint32(buf[pos:]) + pos += 4 + + l.Compression = uint8(buf[pos]) + pos++ + + length := binary.BigEndian.Uint32(buf[pos:]) + + return length, nil +}