manually compute size when badger is being stupid

This commit is contained in:
vyzo 2021-07-26 09:46:21 +03:00
parent 74009bd67f
commit 2cfd73c879

View File

@ -4,6 +4,8 @@ import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"runtime"
"sync"
@ -85,6 +87,7 @@ type Blockstore struct {
viewers sync.WaitGroup
DB *badger.DB
opts Options
prefixing bool
prefix []byte
@ -110,7 +113,7 @@ func Open(opts Options) (*Blockstore, error) {
return nil, fmt.Errorf("failed to open badger blockstore: %w", err)
}
bs := &Blockstore{DB: db}
bs := &Blockstore{DB: db, opts: opts}
if p := opts.Prefix; p != "" {
bs.prefixing = true
bs.prefix = []byte(p)
@ -200,7 +203,27 @@ func (b *Blockstore) Size() (int64, error) {
defer b.viewers.Done()
lsm, vlog := b.DB.Size()
return lsm + vlog, nil
size := lsm + vlog
if size == 0 {
// badger reports a 0 size on symlinked directories... sigh
dir := b.opts.Dir
entries, err := os.ReadDir(dir)
if err != nil {
return 0, err
}
for _, e := range entries {
path := filepath.Join(dir, e.Name())
finfo, err := os.Stat(path)
if err != nil {
return 0, err
}
size += finfo.Size()
}
}
return size, nil
}
// View implements blockstore.Viewer, which leverages zero-copy read-only