forked from miku/microblob
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.go
65 lines (54 loc) · 1.37 KB
/
file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package microblob
import (
"fmt"
"io"
"os"
"sync"
)
// mu protects updates.
var mu sync.Mutex
// Append add a file to an existing blob file and adds their keys to the store.
func Append(blobfn, fn string, backend Backend, kf KeyFunc) error {
return AppendBatchSize(blobfn, fn, backend, kf, 100000, false)
}
// AppendBatchSize uses a given batch size.
func AppendBatchSize(blobfn, fn string, backend Backend, kf KeyFunc, size int, ignoreMissingKeys bool) (err error) {
mu.Lock()
defer mu.Unlock()
file, err := os.OpenFile(blobfn, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
return err
}
defer file.Close()
var offset int64
if fn != "" {
offset, err = file.Seek(0, io.SeekEnd)
if err != nil {
return err
}
f, err := os.Open(fn)
if err != nil {
return err
}
defer f.Close()
if _, err := io.Copy(file, f); err != nil {
return err
}
if _, err := file.Seek(offset, io.SeekStart); err != nil {
return err
}
}
processor := NewLineProcessor(file, backend.WriteEntries, kf)
processor.BatchSize = size
processor.InitialOffset = offset
processor.Verbose = true
processor.IgnoreMissingKeys = ignoreMissingKeys
if err = processor.RunWithWorkers(); err != nil {
if fn != "" {
if terr := os.Truncate(blobfn, offset); terr != nil {
return fmt.Errorf("processing and truncate failed: %v, %v", err, terr)
}
}
}
return err
}