Also keep GlobalSize in RAM

This commit is contained in:
Jakob Borg
2015-10-21 09:10:26 +02:00
parent d4f81e8791
commit c268e4ad1b
3 changed files with 107 additions and 57 deletions

View File

@@ -27,7 +27,8 @@ type FileSet struct {
folder string
db *leveldb.DB
blockmap *BlockMap
sizeTracker
localSize sizeTracker
globalSize sizeTracker
}
// FileIntf is the set of methods implemented by both protocol.FileInfo and
@@ -57,6 +58,7 @@ func (s *sizeTracker) addFile(f FileIntf) {
if f.IsInvalid() {
return
}
s.mut.Lock()
if f.IsDeleted() {
s.deleted++
@@ -71,6 +73,7 @@ func (s *sizeTracker) removeFile(f FileIntf) {
if f.IsInvalid() {
return
}
s.mut.Lock()
if f.IsDeleted() {
s.deleted--
@@ -78,6 +81,9 @@ func (s *sizeTracker) removeFile(f FileIntf) {
s.files--
}
s.bytes -= f.Size()
if s.deleted < 0 || s.files < 0 {
panic("bug: removed more than added")
}
s.mut.Unlock()
}
@@ -96,7 +102,7 @@ func NewFileSet(folder string, db *leveldb.DB) *FileSet {
mutex: sync.NewMutex(),
}
ldbCheckGlobals(db, []byte(folder))
ldbCheckGlobals(db, []byte(folder), &s.globalSize)
var deviceID protocol.DeviceID
ldbWithAllFolderTruncated(db, []byte(folder), func(device []byte, f FileInfoTruncated) bool {
@@ -105,7 +111,7 @@ func NewFileSet(folder string, db *leveldb.DB) *FileSet {
s.localVersion[deviceID] = f.LocalVersion
}
if deviceID == protocol.LocalDeviceID {
s.addFile(f)
s.localSize.addFile(f)
}
return true
})
@@ -120,7 +126,7 @@ func (s *FileSet) Replace(device protocol.DeviceID, fs []protocol.FileInfo) {
normalizeFilenames(fs)
s.mutex.Lock()
defer s.mutex.Unlock()
s.localVersion[device] = ldbReplace(s.db, []byte(s.folder), device[:], fs)
s.localVersion[device] = ldbReplace(s.db, []byte(s.folder), device[:], fs, &s.localSize, &s.globalSize)
if len(fs) == 0 {
// Reset the local version if all files were removed.
s.localVersion[device] = 0
@@ -149,7 +155,7 @@ func (s *FileSet) Update(device protocol.DeviceID, fs []protocol.FileInfo) {
s.blockmap.Discard(discards)
s.blockmap.Update(updates)
}
if lv := ldbUpdate(s.db, []byte(s.folder), device[:], fs, &s.sizeTracker); lv > s.localVersion[device] {
if lv := ldbUpdate(s.db, []byte(s.folder), device[:], fs, &s.localSize, &s.globalSize); lv > s.localVersion[device] {
s.localVersion[device] = lv
}
}
@@ -225,6 +231,14 @@ func (s *FileSet) LocalVersion(device protocol.DeviceID) int64 {
return s.localVersion[device]
}
func (s *FileSet) LocalSize() (files, deleted int, bytes int64) {
return s.localSize.Size()
}
func (s *FileSet) GlobalSize() (files, deleted int, bytes int64) {
return s.globalSize.Size()
}
// ListFolders returns the folder IDs seen in the database.
func ListFolders(db *leveldb.DB) []string {
return ldbListFolders(db)