Skip to content

Commit

Permalink
more compact syncfile
Browse files Browse the repository at this point in the history
  • Loading branch information
bdon committed Jan 19, 2024
1 parent da7a965 commit 4f75162
Showing 1 changed file with 3 additions and 8 deletions.
11 changes: 3 additions & 8 deletions pmtiles/makesync.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,38 +43,33 @@ func SerializeSyncBlocks(output io.Writer, blocks []SyncBlock) {
var n int

lastStartId := uint64(0)
lastOffset := uint64(0)
for _, block := range blocks {
n = binary.PutUvarint(tmp, uint64(block.Start-lastStartId))
output.Write(tmp[:n])
n = binary.PutUvarint(tmp, uint64(block.Offset-lastOffset))
output.Write(tmp[:n])
n = binary.PutUvarint(tmp, uint64(block.Length))
output.Write(tmp[:n])
binary.LittleEndian.PutUint64(tmp, block.Hash)
output.Write(tmp[0:8])

lastStartId = block.Start
lastOffset = block.Offset
}
}

func DeserializesyncBlocks(num_blocks int, reader *bufio.Reader) []SyncBlock {
blocks := make([]SyncBlock, 0)

lastStartId := uint64(0)
lastOffset := uint64(0)
offset := uint64(0)
buf := make([]byte, 8)

for i := 0; i < num_blocks; i++ {
start, _ := binary.ReadUvarint(reader)
offset, _ := binary.ReadUvarint(reader)
length, _ := binary.ReadUvarint(reader)
_, _ = io.ReadFull(reader, buf)
blocks = append(blocks, SyncBlock{Start: lastStartId + start, Offset: lastOffset + offset, Length: length, Hash: binary.LittleEndian.Uint64(buf)})
blocks = append(blocks, SyncBlock{Start: lastStartId + start, Offset: offset, Length: length, Hash: binary.LittleEndian.Uint64(buf)})

lastStartId = lastStartId + start
lastOffset = lastOffset + offset
offset = offset + length
}

return blocks
Expand Down

0 comments on commit 4f75162

Please sign in to comment.