Skip to content

Commit

Permalink
move md5 into writer thread
Browse files Browse the repository at this point in the history
  • Loading branch information
bdon committed Jan 6, 2024
1 parent 7515fee commit e9fd12b
Showing 1 changed file with 8 additions and 15 deletions.
23 changes: 8 additions & 15 deletions pmtiles/makesync.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,21 +111,6 @@ func Makesync(logger *log.Logger, cli_version string, file string, block_size_kb
defer output.Close()
output.Write([]byte(fmt.Sprintf("version=%s\n", cli_version)))

if checksum == "md5" {
localfile, err := os.Open(file)
if err != nil {
panic(err)
}
defer localfile.Close()
reader := bufio.NewReaderSize(localfile, 64*1024*1024)
md5hasher := md5.New()
if _, err := io.Copy(md5hasher, reader); err != nil {
panic(err)
}
md5checksum := md5hasher.Sum(nil)
fmt.Printf("Completed md5 in %v.\n", time.Since(start))
output.Write([]byte(fmt.Sprintf("md5=%x\n", md5checksum)))
}

output.Write([]byte("hash=fnv1a\n"))
output.Write([]byte(fmt.Sprintf("blocksize=%d\n", block_size_bytes)))
Expand Down Expand Up @@ -169,6 +154,8 @@ func Makesync(logger *log.Logger, cli_version string, file string, block_size_kb

synclines := make([][]uint64, 0)

md5hasher := md5.New()

go func() {
buffer := make(map[uint64]Result)
nextIndex := uint64(0)
Expand All @@ -180,6 +167,9 @@ func Makesync(logger *log.Logger, cli_version string, file string, block_size_kb
if next, ok := buffer[nextIndex]; ok {

synclines = append(synclines, []uint64{next.Block.Start, next.Block.Offset, next.Block.Length, next.Hash})
if _, err := io.Copy(md5hasher, bytes.NewReader(next.Data)); err != nil {
panic(err)
}

delete(buffer, nextIndex)
nextIndex++
Expand Down Expand Up @@ -233,6 +223,9 @@ func Makesync(logger *log.Logger, cli_version string, file string, block_size_kb

<-done

md5checksum := md5hasher.Sum(nil)
output.Write([]byte(fmt.Sprintf("md5=%x\n", md5checksum)))

for _, s := range synclines {
output.Write([]byte(fmt.Sprintf("%d\t%d\t%d\t%x\n", s[0], s[1], s[2], s[3])))
}
Expand Down

0 comments on commit e9fd12b

Please sign in to comment.