diff --git a/fs/os_file.go b/fs/os_file.go index 7238677..f2e0ccd 100644 --- a/fs/os_file.go +++ b/fs/os_file.go @@ -1,7 +1,6 @@ package fs import ( - "io" "os" ) @@ -17,11 +16,11 @@ func openOSFile(name string) (File, error) { } // get the file size - size, err := fd.Seek(0, io.SeekEnd) + stat, err := fd.Stat() if err != nil { return nil, err } - return &OSFile{fd: fd, size: size}, nil + return &OSFile{fd: fd, size: stat.Size()}, nil } func (of *OSFile) Read(p []byte) (n int, err error) { diff --git a/table.go b/table.go index 80307e2..63c6672 100644 --- a/table.go +++ b/table.go @@ -100,18 +100,18 @@ func checkOptions(options Options) error { } func (t *Table) readMeta() error { + file, err := fs.Open(filepath.Join(t.options.DirPath, metaFileName), fs.OSFileSystem) + if err != nil { + return err + } + t.metaFile = file + t.meta = &tableMeta{} + // init meta file if not exist - if t.metaFile == nil { - file, err := fs.Open(filepath.Join(t.options.DirPath, metaFileName), fs.OSFileSystem) - if err != nil { - return err - } - t.meta = &tableMeta{ - NumBuckets: 1, - SlotValueLength: t.options.SlotValueLength, - } + if file.Size() == 0 { + t.meta.NumBuckets = 1 + t.meta.SlotValueLength = t.options.SlotValueLength t.meta.BucketSize = slotsPerBucket*(4+t.meta.SlotValueLength) + bucketNextOffsetLen - t.metaFile = file } else { decoder := json.NewDecoder(t.metaFile) if err := decoder.Decode(t.meta); err != nil {