@@ -193,7 +193,8 @@ func Merge(dst, src1, src2 string) {
193193 var w postDataWriter
194194 r1 .init (ix1 , map1 )
195195 r2 .init (ix2 , map2 )
196- w .init (ix , true )
196+ postIndexFile := bufCreate ("" )
197+ w .init (ix , postIndexFile )
197198 old1 , old2 := uint32 (0 ), uint32 (0 )
198199 for {
199200 if ! (r1 .trigram > old1 || r2 .trigram > old2 ) {
@@ -237,7 +238,9 @@ func Merge(dst, src1, src2 string) {
237238 w .endTrigram ()
238239 }
239240 }
240- w .postIndexFile .Align (postBlockSize )
241+ if len (w .block ) > 0 {
242+ w .flush ()
243+ }
241244
242245 // Name index
243246 ix .Align (16 )
@@ -247,7 +250,7 @@ func Merge(dst, src1, src2 string) {
247250 // Posting list index
248251 ix .Align (16 )
249252 postIndex := ix .Offset ()
250- copyFile (ix , w . postIndexFile )
253+ copyFile (ix , postIndexFile )
251254
252255 // Trailer
253256 ix .Align (16 )
@@ -389,63 +392,72 @@ type postDataWriter struct {
389392 t uint32
390393 delta deltaWriter
391394 numTrigram int
395+ tmp [32 ]byte
396+ block []byte
392397}
393398
394- func (w * postDataWriter ) init (out * Buffer , doIndex bool ) {
395- w .out = out
396- w .base = out .Offset ()
399+ func (w * postDataWriter ) flush () {
400+ if w .postIndexFile != nil && len (w .block ) > 0 {
401+ w .postIndexFile .Write (w .block [:cap (w .block )])
402+ w .block = w .block [:0 ]
403+ }
404+ }
405+
406+ func (w * postDataWriter ) init (postData , postIndex * Buffer ) {
407+ w .out = postData
408+ w .base = w .out .Offset ()
397409 w .postIndexFile = nil
398- w .delta .init (out )
410+ w .delta .init (w . out )
399411 w .lastOffset = w .base
400- if doIndex {
401- w .postIndexFile = bufCreate ("" )
402- }
412+ w .postIndexFile = postIndex
413+ w .block = make ([]byte , 0 , postBlockSize )
403414}
404415
405416func (w * postDataWriter ) trigram (t uint32 ) {
417+ if t == 0 {
418+ panic ("invalid trigram" )
419+ }
406420 w .offset = w .out .Offset ()
407421 w .count = 0
408422 w .t = t
409423 w .lastID = - 1
410424 w .numTrigram ++
425+ w .out .WriteTrigram (w .t )
411426}
412427
413428func (w * postDataWriter ) fileid (id int ) {
414- if w .count == 0 {
415- w .out .WriteTrigram (w .t )
416- }
417429 w .delta .Write (id - w .lastID )
418430 w .lastID = id
419431 w .count ++
420432}
421433
422434func (w * postDataWriter ) endTrigram () {
423- if w .count == 0 {
424- return
425- }
426435 w .delta .Write (0 )
427436 w .delta .Flush ()
428437 if w .postIndexFile == nil {
429438 return
430439 }
431- if writeVersion == 2 {
432- var buf []byte
433- buf = append (buf , byte (w .t >> 16 ), byte (w .t >> 8 ), byte (w .t ))
434- buf = binary .AppendUvarint (buf , uint64 (w .count ))
435- cbuf := buf
436- buf = binary .AppendUvarint (buf , uint64 (w .offset - w .lastOffset ))
437- if w .postIndexFile .Offset ()/ postBlockSize != (w .postIndexFile .Offset ()+ len (buf )- 1 )/ postBlockSize {
438- for w .postIndexFile .Offset ()% postBlockSize != 0 {
439- w .postIndexFile .WriteByte (0 )
440- }
441- w .lastOffset = w .base
442- buf = binary .AppendUvarint (cbuf , uint64 (w .offset - w .lastOffset ))
443- }
444- w .postIndexFile .Write (buf [:])
445- w .lastOffset = w .offset
446- } else {
440+ if writeVersion == 1 {
447441 w .postIndexFile .WriteTrigram (w .t )
448442 w .postIndexFile .WriteUint (w .count )
449443 w .postIndexFile .WriteUint (w .offset - w .base )
444+ return
445+ }
446+
447+ buf := w .tmp [:]
448+ buf [0 ] = byte (w .t >> 16 )
449+ buf [1 ] = byte (w .t >> 8 )
450+ buf [2 ] = byte (w .t )
451+
452+ n := 3
453+ n += binary .PutUvarint (buf [n :], uint64 (w .count ))
454+ n1 := binary .PutUvarint (buf [n :], uint64 (w .offset - w .lastOffset ))
455+ if len (w .block )+ n + n1 > cap (w .block ) {
456+ w .postIndexFile .Write (w .block [:cap (w .block )])
457+ clear (w .block )
458+ w .block = w .block [:0 ]
459+ n1 = binary .PutUvarint (buf [n :], uint64 (w .offset - w .base ))
450460 }
461+ w .block = append (w .block , buf [:n + n1 ]... )
462+ w .lastOffset = w .offset
451463}
0 commit comments