Skip to content

Commit

Permalink
implement writer deduplication [protomaps#5]
Browse files Browse the repository at this point in the history
  • Loading branch information
bdon committed Dec 13, 2021
1 parent 49c357e commit c37d1f7
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions pmtiles/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pmtiles

import (
"encoding/binary"
"hash/fnv"
"os"
)

Expand All @@ -11,9 +12,10 @@ type Entry struct {
}

type Writer struct {
file *os.File
offset uint64
tiles []Entry
file *os.File
offset uint64
tiles []Entry
hashToOffset map[uint64]uint64
}

func NewWriter(path string) Writer {
Expand All @@ -26,7 +28,8 @@ func NewWriter(path string) Writer {
if err != nil {
panic("Write failed")
}
return Writer{file: f, offset: offset}
hashToOffset := make(map[uint64]uint64)
return Writer{file: f, offset: offset, hashToOffset: hashToOffset}
}

// return (uint32(binary.LittleEndian.Uint16(b[1:3])) << 8) + uint32(b[0])
Expand Down Expand Up @@ -54,9 +57,21 @@ func (writer *Writer) writeEntry(entry Entry) {
}

func (writer *Writer) WriteTile(zxy Zxy, data []byte) {
writer.file.Write(data)
writer.tiles = append(writer.tiles, Entry{zxy: zxy, rng: Range{Offset: writer.offset, Length: uint32(len(data))}})
writer.offset += uint64(len(data))
// TODO do gzip decompression here
hsh := fnv.New64a()
hsh.Write(data)
tileHash := hsh.Sum64()

existingOffset, ok := writer.hashToOffset[tileHash]

if ok {
writer.tiles = append(writer.tiles, Entry{zxy: zxy, rng: Range{Offset: existingOffset, Length: uint32(len(data))}})
} else {
writer.file.Write(data)
writer.tiles = append(writer.tiles, Entry{zxy: zxy, rng: Range{Offset: writer.offset, Length: uint32(len(data))}})
writer.hashToOffset[tileHash] = writer.offset
writer.offset += uint64(len(data))
}
}

func (writer *Writer) writeHeader(metadata []byte, numRootEntries int) {
Expand Down

0 comments on commit c37d1f7

Please sign in to comment.