fork/gdu

git clone https://git.julia.blue/fork/gdu

Daniel Milderefactor: use atomic counter instead of mutex (#597)da4d262

ignore
3.0 KiB148 linesraw
1package analyze
2
3import (
4	"bytes"
5	"encoding/gob"
6	"path/filepath"
7	"sync"
8	"sync/atomic"
9
10	"github.com/dgraph-io/badger/v4"
11	"github.com/dundee/gdu/v5/internal/common"
12	"github.com/dundee/gdu/v5/pkg/fs"
13	"github.com/pkg/errors"
14)
15
16func init() {
17	gob.RegisterName("analyze.StoredDir", &StoredDir{})
18	gob.RegisterName("analyze.Dir", &Dir{})
19	gob.RegisterName("analyze.File", &File{})
20	gob.RegisterName("analyze.ParentDir", &ParentDir{})
21}
22
23// DefaultStorage is a default instance of badger storage
24var DefaultStorage *Storage
25
26// Storage represents a badger storage
27type Storage struct {
28	db          *badger.DB
29	storagePath string
30	topDir      string
31	m           sync.RWMutex
32	counter     atomic.Int64
33}
34
35// NewStorage returns new instance of badger storage
36func NewStorage(storagePath, topDir string) *Storage {
37	st := &Storage{
38		storagePath: storagePath,
39		topDir:      topDir,
40	}
41	DefaultStorage = st
42	return st
43}
44
45// GetTopDir returns top directory
46func (s *Storage) GetTopDir() string {
47	return s.topDir
48}
49
50// IsOpen returns true if badger DB is open
51func (s *Storage) IsOpen() bool {
52	s.m.RLock()
53	defer s.m.RUnlock()
54	return s.db != nil
55}
56
57// Open opens badger DB
58func (s *Storage) Open() func() {
59	options := badger.DefaultOptions(s.storagePath)
60	options.Logger = nil
61
62	if !common.Is64Bit {
63		// For 32-bit systems, we need to set ValueLogFileSize to a smaller value to
64		// avoid "cannot allocate memory while mmapping" error
65		options.ValueLogFileSize = (1<<30 - 1) / 2
66	}
67
68	db, err := badger.Open(options)
69	if err != nil {
70		panic(err)
71	}
72	s.db = db
73
74	return func() {
75		s.db.Close()
76		s.db = nil
77	}
78}
79
80// StoreDir saves item info into badger DB
81func (s *Storage) StoreDir(dir fs.Item) error {
82	s.checkCount()
83	s.m.RLock()
84	defer s.m.RUnlock()
85
86	return s.db.Update(func(txn *badger.Txn) error {
87		b := &bytes.Buffer{}
88		enc := gob.NewEncoder(b)
89		err := enc.Encode(dir)
90		if err != nil {
91			return errors.Wrap(err, "encoding dir value")
92		}
93
94		return txn.Set([]byte(dir.GetPath()), b.Bytes())
95	})
96}
97
98// LoadDir saves item info into badger DB
99func (s *Storage) LoadDir(dir fs.Item) error {
100	s.checkCount()
101	s.m.RLock()
102	defer s.m.RUnlock()
103
104	return s.db.View(func(txn *badger.Txn) error {
105		path := dir.GetPath()
106		item, err := txn.Get([]byte(path))
107		if err != nil {
108			return errors.Wrap(err, "reading stored value for path: "+path)
109		}
110		return item.Value(func(val []byte) error {
111			b := bytes.NewBuffer(val)
112			dec := gob.NewDecoder(b)
113			return dec.Decode(dir)
114		})
115	})
116}
117
118// GetDirForPath returns Dir for given path
119func (s *Storage) GetDirForPath(path string) (item fs.Item, err error) {
120	dirPath := filepath.Dir(path)
121	name := filepath.Base(path)
122	dir := &StoredDir{
123		&Dir{
124			File: &File{
125				Name: name,
126			},
127			BasePath: dirPath,
128		},
129		nil,
130		sync.Mutex{},
131	}
132	err = s.LoadDir(dir)
133	if err != nil {
134		return nil, err
135	}
136	return dir, nil
137}
138
139func (s *Storage) checkCount() {
140	s.counter.Add(1)
141	if s.counter.Load() >= 10000 {
142		s.m.Lock()
143		defer s.m.Unlock()
144		s.counter.Store(0)
145		s.db.Close()
146		s.Open()
147	}
148}