From 56bf0ee4dc8447b6b2ad8a4c54b552eb4cc971f6 Mon Sep 17 00:00:00 2001 From: Ganesh Vernekar <15064823+codesome@users.noreply.github.com> Date: Wed, 5 Feb 2020 19:08:30 +0530 Subject: [PATCH] Add OpenMmapFileWithSize method (#6753) Signed-off-by: Ganesh Vernekar --- tsdb/fileutil/mmap.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/tsdb/fileutil/mmap.go b/tsdb/fileutil/mmap.go index 26fc80c58..896b27789 100644 --- a/tsdb/fileutil/mmap.go +++ b/tsdb/fileutil/mmap.go @@ -25,16 +25,23 @@ type MmapFile struct { } func OpenMmapFile(path string) (*MmapFile, error) { + return OpenMmapFileWithSize(path, 0) +} + +func OpenMmapFileWithSize(path string, size int) (*MmapFile, error) { f, err := os.Open(path) if err != nil { return nil, errors.Wrap(err, "try lock file") } - info, err := f.Stat() - if err != nil { - return nil, errors.Wrap(err, "stat") + if size <= 0 { + info, err := f.Stat() + if err != nil { + return nil, errors.Wrap(err, "stat") + } + size = int(info.Size()) } - b, err := mmap(f, int(info.Size())) + b, err := mmap(f, size) if err != nil { return nil, errors.Wrap(err, "mmap") }