Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
akiozihao committed Sep 21, 2023
2 parents 79296a8 + 0753d5a commit cea51ec
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
19 changes: 13 additions & 6 deletions examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 1 addition & 4 deletions segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
}()

Expand Down Expand Up @@ -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)
}()

Expand Down

0 comments on commit cea51ec

Please sign in to comment.