Skip to content

Commit ee8bdba

Browse files
makutojunkblocker
authored andcommitted
Fix Windows not being able to delete temporary files #13
1 parent 351619a commit ee8bdba

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

index/merge.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,9 @@ func Merge(dst, src1, src2 string) {
232232
ix3.flush()
233233
ix3.finish().Close()
234234

235+
nameIndexFile.file.Close()
235236
os.Remove(nameIndexFile.name)
237+
w.postIndexFile.file.Close()
236238
os.Remove(w.postIndexFile.name)
237239
}
238240

index/write.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,11 +250,15 @@ func (ix *IndexWriter) Flush() {
250250
}
251251
ix.main.writeString(trailerMagic)
252252

253+
ix.nameData.file.Close()
253254
os.Remove(ix.nameData.name)
254255
for _, f := range ix.postFile {
256+
f.Close()
255257
os.Remove(f.Name())
256258
}
259+
ix.nameIndex.file.Close()
257260
os.Remove(ix.nameIndex.name)
261+
ix.postIndex.file.Close()
258262
os.Remove(ix.postIndex.name)
259263

260264
log.Printf("%d data bytes, %d index bytes", ix.totalBytes, ix.main.offset())
@@ -355,6 +359,9 @@ func (ix *IndexWriter) mergePost(out *bufWriter) {
355359
break
356360
}
357361
}
362+
for _, mappedData := range h.mappedData {
363+
unmmapFile(mappedData)
364+
}
358365
}
359366

360367
// A postChunk represents a chunk of post entries flushed to disk or
@@ -368,13 +375,17 @@ const postBuf = 4096
368375

369376
// A postHeap is a heap (priority queue) of postChunks.
370377
type postHeap struct {
371-
ch []*postChunk
378+
ch []*postChunk
379+
mappedData []*mmapData
372380
}
373381

374382
func (h *postHeap) addFile(f *os.File) {
375-
data := mmapFile(f).d
383+
mappedData := mmapFile(f)
384+
data := mappedData.d
376385
m := (*[npost]postEntry)(unsafe.Pointer(&data[0]))[:len(data)/8]
377386
h.addMem(m)
387+
// Make sure we close the mmap memory once we're done with it
388+
h.mappedData = append(h.mappedData, (&mappedData))
378389
}
379390

380391
func (h *postHeap) addMem(x []postEntry) {

0 commit comments

Comments
 (0)