fork/gdu
git clone https://git.julia.blue/fork/gdu
35cba00
master
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 () { 17gob .RegisterName ("analyze.StoredDir" ,& StoredDir {}) 18gob .RegisterName ("analyze.Dir" ,& Dir {}) 19gob .RegisterName ("analyze.File" ,& File {}) 20gob .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 27// 28// All access to db is serialized by m. Open is reference counted: badger takes 29// an exclusive lock on the storage directory, so a second concurrent 30// badger.Open on the same path would panic, and an inner caller's closer must 31// not tear the database out from under an outer caller that is still using it. 32type Storage struct { 33db * badger.DB 34storagePath string 35topDir string 36m sync.RWMutex 37counter atomic.Int64 38openCount int 39} 40 41// NewStorage returns new instance of badger storage 42func NewStorage (storagePath ,topDir string )* Storage { 43st := & Storage { 44storagePath :storagePath , 45topDir :topDir , 46 } 47DefaultStorage = st 48return st 49} 50 51// GetTopDir returns top directory 52func (s * Storage )GetTopDir ()string { 53return s .topDir 54} 55 56// IsOpen returns true if badger DB is open 57func (s * Storage )IsOpen ()bool { 58s .m .RLock () 59defer s .m .RUnlock () 60return s .db != nil 61} 62 63// Open opens badger DB and returns a function closing it again. 64// 65// Opening is reference counted, so nested and concurrent callers all get a 66// usable database and only the last returned closer actually closes it. Each 67// closer is idempotent; calling it more than once is a no-op. 68func (s * Storage )Open ()func () { 69s .m .Lock () 70defer s .m .Unlock () 71 72if s .db == nil { 73s .openLocked () 74 } 75s .openCount ++ 76 77var once sync.Once 78return func () { 79once .Do (s .release ) 80 } 81} 82 83// release drops one reference taken by Open, closing the database once the 84// last one is gone. 85func (s * Storage )release () { 86s .m .Lock () 87defer s .m .Unlock () 88 89if s .openCount == 0 { 90return 91 } 92s .openCount -- 93if s .openCount > 0 || s .db == nil { 94return 95 } 96s .db .Close () 97s .db = nil 98} 99 100// openLocked opens the badger DB. The caller must hold m for writing. 101func (s * Storage )openLocked () { 102options := badger .DefaultOptions (s .storagePath ) 103options .Logger = nil 104 105if ! common .Is64Bit { 106// For 32-bit systems, we need to set ValueLogFileSize to a smaller value to 107// avoid "cannot allocate memory while mmapping" error 108options .ValueLogFileSize = (1 << 30 - 1 )/ 2 109 } 110 111db ,err := badger .Open (options ) 112if err != nil { 113panic (err ) 114 } 115s .db = db 116} 117 118// StoreDir saves item info into badger DB 119func (s * Storage )StoreDir (dir fs.Item )error { 120s .checkCount () 121s .m .RLock () 122defer s .m .RUnlock () 123 124return s .db .Update (func (txn * badger.Txn )error { 125b := & bytes.Buffer {} 126enc := gob .NewEncoder (b ) 127err := enc .Encode (dir ) 128if err != nil { 129return errors .Wrap (err ,"encoding dir value" ) 130 } 131 132return txn .Set ([]byte (dir .GetPath ()),b .Bytes ()) 133 }) 134} 135 136// LoadDir saves item info into badger DB 137func (s * Storage )LoadDir (dir fs.Item )error { 138s .checkCount () 139s .m .RLock () 140defer s .m .RUnlock () 141 142return s .db .View (func (txn * badger.Txn )error { 143path := dir .GetPath () 144item ,err := txn .Get ([]byte (path )) 145if err != nil { 146return errors .Wrap (err ,"reading stored value for path: " + path ) 147 } 148return item .Value (func (val []byte )error { 149b := bytes .NewBuffer (val ) 150dec := gob .NewDecoder (b ) 151return dec .Decode (dir ) 152 }) 153 }) 154} 155 156// GetDirForPath returns Dir for given path 157func (s * Storage )GetDirForPath (path string ) (item fs.Item ,err error ) { 158dirPath := filepath .Dir (path ) 159name := filepath .Base (path ) 160dir := & StoredDir { 161Dir :& Dir { 162File :& File { 163Name :name , 164 }, 165BasePath :dirPath , 166 }, 167 } 168err = s .LoadDir (dir ) 169if err != nil { 170return nil ,err 171 } 172return dir ,nil 173} 174 175func (s * Storage )checkCount () { 176s .counter .Add (1 ) 177if s .counter .Load ()>= 10000 { 178s .m .Lock () 179defer s .m .Unlock () 180if s .counter .Load ()< 10000 { 181// another goroutine already recycled the database 182return 183 } 184s .counter .Store (0 ) 185if s .db != nil { 186s .db .Close () 187 } 188s .openLocked () 189 } 190}