aboutsummaryrefslogtreecommitdiff
path: root/lib/db/util.go
diff options
context:
space:
mode:
Diffstat (limited to 'lib/db/util.go')
-rw-r--r--lib/db/util.go12
1 files changed, 7 insertions, 5 deletions
diff --git a/lib/db/util.go b/lib/db/util.go
index c67ca5508..5e9adb267 100644
--- a/lib/db/util.go
+++ b/lib/db/util.go
@@ -22,11 +22,10 @@ type FileInfoBatch struct {
flushFn func([]protocol.FileInfo) error
}
+// NewFileInfoBatch returns a new FileInfoBatch that calls fn when it's time
+// to flush.
func NewFileInfoBatch(fn func([]protocol.FileInfo) error) *FileInfoBatch {
- return &FileInfoBatch{
- infos: make([]protocol.FileInfo, 0, MaxBatchSizeFiles),
- flushFn: fn,
- }
+ return &FileInfoBatch{flushFn: fn}
}
func (b *FileInfoBatch) SetFlushFunc(fn func([]protocol.FileInfo) error) {
@@ -34,6 +33,9 @@ func (b *FileInfoBatch) SetFlushFunc(fn func([]protocol.FileInfo) error) {
}
func (b *FileInfoBatch) Append(f protocol.FileInfo) {
+ if b.infos == nil {
+ b.infos = make([]protocol.FileInfo, 0, MaxBatchSizeFiles)
+ }
b.infos = append(b.infos, f)
b.size += f.ProtoSize()
}
@@ -61,7 +63,7 @@ func (b *FileInfoBatch) Flush() error {
}
func (b *FileInfoBatch) Reset() {
- b.infos = b.infos[:0]
+ b.infos = nil
b.size = 0
}