@@ -40,64 +40,76 @@ func (c *Client) Error() error {
40
40
41
41
func (c * Client ) EntriesSumDB (tree tlog.Tree , start int64 ) iter.Seq2 [int64 , []byte ] {
42
42
return func (yield func (int64 , []byte ) bool ) {
43
+ if c .err != nil {
44
+ return
45
+ }
43
46
for {
44
- t := tlog .TileForIndex (tileHeight , tlog .StoredHashIndex (0 , start ))
45
- tileStart := t .N * tileWidth
46
- tileEnd := tileStart + tileWidth
47
- if tileEnd > tree .N {
47
+ base := start / tileWidth * tileWidth
48
+ tiles := make ([]tlog.Tile , 0 , 16 )
49
+ for i := 0 ; i < 50 ; i ++ {
50
+ tileStart := base + int64 (i )* tileWidth
51
+ tileEnd := tileStart + tileWidth
52
+ if tileEnd > tree .N {
53
+ break
54
+ }
55
+ tiles = append (tiles , tlog.Tile {H : tileHeight , L : - 1 ,
56
+ N : tileStart / tileWidth , W : tileWidth })
57
+ }
58
+ if len (tiles ) == 0 {
48
59
// TODO: document and support partial tile optimization.
49
60
return
50
61
}
51
-
52
- t .L = - 1
53
- t .W = tileWidth
54
- tt , err := c .tr .ReadTiles ([]tlog.Tile {t })
62
+ tdata , err := c .tr .ReadTiles (tiles )
55
63
if err != nil {
56
64
c .err = err
57
65
return
58
66
}
59
- data := tt [0 ]
60
67
61
68
// TODO: hash data tile directly against level 8 hash.
62
- indexes := make ([]int64 , tileWidth )
69
+ indexes := make ([]int64 , tileWidth * len ( tiles ) )
63
70
for i := range indexes {
64
- indexes [i ] = tlog .StoredHashIndex (0 , tileStart + int64 (i ))
71
+ indexes [i ] = tlog .StoredHashIndex (0 , base + int64 (i ))
65
72
}
66
73
hashes , err := tlog .TileHashReader (tree , c .tr ).ReadHashes (indexes )
67
74
if err != nil {
68
75
c .err = err
69
76
return
70
77
}
71
78
72
- for i := tileStart ; i < tileEnd ; i ++ {
73
- if len (data ) == 0 {
74
- c .err = fmt .Errorf ("unexpected end of tile data" )
75
- return
76
- }
77
-
78
- var entry []byte
79
- if idx := bytes .Index (data , []byte ("\n \n " )); idx >= 0 {
80
- // Add back one of the newlines.
81
- entry , data = data [:idx + 1 ], data [idx + 2 :]
82
- } else {
83
- entry , data = data , nil
84
- }
85
-
86
- if tlog .RecordHash (entry ) != hashes [i - tileStart ] {
87
- c .err = fmt .Errorf ("hash mismatch for entry %d" , i )
88
- return
89
- }
90
-
91
- if i < start {
92
- continue
93
- }
94
- if ! yield (i , entry ) {
95
- return
79
+ for ti , t := range tiles {
80
+ tileStart := t .N * tileWidth
81
+ tileEnd := tileStart + tileWidth
82
+ data := tdata [ti ]
83
+ for i := tileStart ; i < tileEnd ; i ++ {
84
+ if len (data ) == 0 {
85
+ c .err = fmt .Errorf ("unexpected end of tile data" )
86
+ return
87
+ }
88
+
89
+ var entry []byte
90
+ if idx := bytes .Index (data , []byte ("\n \n " )); idx >= 0 {
91
+ // Add back one of the newlines.
92
+ entry , data = data [:idx + 1 ], data [idx + 2 :]
93
+ } else {
94
+ entry , data = data , nil
95
+ }
96
+
97
+ if tlog .RecordHash (entry ) != hashes [i - base ] {
98
+ c .err = fmt .Errorf ("hash mismatch for entry %d" , i )
99
+ return
100
+ }
101
+
102
+ if i < start {
103
+ continue
104
+ }
105
+ if ! yield (i , entry ) {
106
+ return
107
+ }
96
108
}
109
+ start = tileEnd
97
110
}
98
111
99
- c .tr .SaveTiles ([]tlog.Tile {t }, tt )
100
- start = tileEnd
112
+ c .tr .SaveTiles (tiles , tdata )
101
113
}
102
114
}
103
115
}
@@ -285,7 +297,7 @@ func (c *PermanentCache) ReadTiles(tiles []tlog.Tile) (data [][]byte, err error)
285
297
} else if err != nil {
286
298
return nil , err
287
299
} else {
288
- c .log .Info ("loaded tile from cache" , "path" , path , "size" , len (d ))
300
+ c .log .Info ("loaded tile from cache" , "path" , t . Path () , "size" , len (d ))
289
301
data [i ] = d
290
302
}
291
303
}
@@ -311,6 +323,9 @@ func (c *PermanentCache) SaveTiles(tiles []tlog.Tile, data [][]byte) {
311
323
continue // skip partial tiles
312
324
}
313
325
path := filepath .Join (c .dir , t .Path ())
326
+ if _ , err := os .Stat (path ); err == nil {
327
+ continue
328
+ }
314
329
if err := os .MkdirAll (filepath .Dir (path ), 0700 ); err != nil {
315
330
c .log .Error ("failed to create directory" , "path" , path , "error" , err )
316
331
return
0 commit comments