From 8a11180fd4cc659d28473f78d26dbb5eac937db1 Mon Sep 17 00:00:00 2001 From: roseduan Date: Thu, 21 Sep 2023 21:30:41 +0800 Subject: [PATCH 1/3] update example --- examples/main.go | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/examples/main.go b/examples/main.go index 9d04786..c8bfa92 100644 --- a/examples/main.go +++ b/examples/main.go @@ -3,23 +3,30 @@ package main import ( "fmt" "io" + "log" "github.com/rosedblabs/wal" ) func main() { - wal, _ := wal.Open(wal.DefaultOptions) + walFile, _ := wal.Open(wal.DefaultOptions) // write some data - chunkPosition, _ := wal.Write([]byte("some data 1")) + chunkPosition, _ := walFile.Write([]byte("some data 1")) // read by the position - val, _ := wal.Read(chunkPosition) + val, _ := walFile.Read(chunkPosition) fmt.Println(string(val)) - wal.Write([]byte("some data 2")) - wal.Write([]byte("some data 3")) + _, err := walFile.Write([]byte("some data 2")) + if err != nil { + log.Println(err) + } + _, err = walFile.Write([]byte("some data 3")) + if err != nil { + log.Println(err) + } // iterate all data in wal - reader := wal.NewReader() + reader := walFile.NewReader() for { val, pos, err := reader.Next() if err == io.EOF { From 581142afdb4c889a74251ea28ac2a67ca5a5aa6a Mon Sep 17 00:00:00 2001 From: akiozihao Date: Thu, 21 Sep 2023 21:34:40 +0800 Subject: [PATCH 2/3] check ErrPendingSizeTooLarge first (#32) --- wal.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/wal.go b/wal.go index 7dcb63b..8a34de8 100644 --- a/wal.go +++ b/wal.go @@ -354,16 +354,17 @@ func (wal *WAL) WriteAll() ([]*ChunkPosition, error) { wal.mu.Unlock() }() + // if the pending size is still larger than segment size, return error + if wal.pendingSize > wal.options.SegmentSize { + return nil, ErrPendingSizeTooLarge + } + // if the active segment file is full, sync it and create a new one. if wal.activeSegment.Size()+wal.pendingSize > wal.options.SegmentSize { if err := wal.rotateActiveSegment(); err != nil { return nil, err } } - // if the pending size is still larger than segment size, return error - if wal.pendingSize > wal.options.SegmentSize { - return nil, ErrPendingSizeTooLarge - } // write all data to the active segment file. positions, err := wal.activeSegment.writeAll(wal.pendingWrites) From 0753d5ac24c43727338a5d006b992e44cf4dc13b Mon Sep 17 00:00:00 2001 From: roseduan Date: Thu, 21 Sep 2023 21:39:31 +0800 Subject: [PATCH 3/3] avoid reset pool data --- segment.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/segment.go b/segment.go index a769a2a..152b514 100644 --- a/segment.go +++ b/segment.go @@ -194,7 +194,7 @@ func (seg *segment) writeToBuffer(data []byte, chunkBuffer *bytebufferpool.ByteB } } - // return the start position of the chunk, for reading. + // return the start position of the chunk, then the user can use it to read the data. position := &ChunkPosition{ SegmentId: seg.id, BlockNumber: seg.currentBlockNumber, @@ -242,7 +242,6 @@ func (seg *segment) writeToBuffer(data []byte, chunkBuffer *bytebufferpool.ByteB blockCount += 1 currBlockSize = (currBlockSize + chunkSize + chunkHeaderSize) % blockSize } - position.ChunkSize = blockCount*chunkHeaderSize + dataSize } @@ -281,7 +280,6 @@ func (seg *segment) writeAll(data [][]byte) (positions []*ChunkPosition, err err seg.currentBlockNumber = originBlockNumber seg.currentBlockSize = originBlockSize } - chunkBuffer.Reset() bytebufferpool.Put(chunkBuffer) }() @@ -319,7 +317,6 @@ func (seg *segment) Write(data []byte) (pos *ChunkPosition, err error) { seg.currentBlockNumber = originBlockNumber seg.currentBlockSize = originBlockSize } - chunkBuffer.Reset() bytebufferpool.Put(chunkBuffer) }()