mirror of
https://github.com/octoleo/syncthing.git
synced 2024-12-23 03:18:59 +00:00
Cleanup the code in internal/files/leveldb.go a bit.
This commit introduces the following, cosmetic changes: - All callers of 'ldbGenericReplace' specify a non-nil 'deleteFn' argument. Thus, remove the checks for 'deleteFn' being nil. - Before this change, 'ldbInsert' took a 'FileInfo' argument and a separate argument for the file name, but all callers passed in the value of FileInfo.Name as the file name. Simplify this, by not not passing in the file name as a separate argument. This makes the function signature of 'ldbInsert' more similar to 'ldbUpdate'. - doc fixes
This commit is contained in:
parent
d65302742c
commit
28366677b0
@ -167,7 +167,7 @@ type fileIterator func(f protocol.FileIntf) bool
|
|||||||
func ldbGenericReplace(db *leveldb.DB, folder, device []byte, fs []protocol.FileInfo, deleteFn deletionHandler) uint64 {
|
func ldbGenericReplace(db *leveldb.DB, folder, device []byte, fs []protocol.FileInfo, deleteFn deletionHandler) uint64 {
|
||||||
runtime.GC()
|
runtime.GC()
|
||||||
|
|
||||||
sort.Sort(fileList(fs)) // sort list on name, same as on disk
|
sort.Sort(fileList(fs)) // sort list on name, same as in the database
|
||||||
|
|
||||||
start := deviceKey(folder, device, nil) // before all folder/device files
|
start := deviceKey(folder, device, nil) // before all folder/device files
|
||||||
limit := deviceKey(folder, device, []byte{0xff, 0xff, 0xff, 0xff}) // after all folder/device files
|
limit := deviceKey(folder, device, []byte{0xff, 0xff, 0xff, 0xff}) // after all folder/device files
|
||||||
@ -193,12 +193,6 @@ func ldbGenericReplace(db *leveldb.DB, folder, device []byte, fs []protocol.File
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
if !moreFs && deleteFn == nil {
|
|
||||||
// We don't have any more updated files to process and deletion
|
|
||||||
// has not been requested, so we can exit early
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
if moreFs {
|
if moreFs {
|
||||||
newName = []byte(fs[fsi].Name)
|
newName = []byte(fs[fsi].Name)
|
||||||
}
|
}
|
||||||
@ -215,8 +209,8 @@ func ldbGenericReplace(db *leveldb.DB, folder, device []byte, fs []protocol.File
|
|||||||
|
|
||||||
switch {
|
switch {
|
||||||
case moreFs && (!moreDb || cmp == -1):
|
case moreFs && (!moreDb || cmp == -1):
|
||||||
// Disk is missing this file. Insert it.
|
// Database is missing this file. Insert it.
|
||||||
if lv := ldbInsert(batch, folder, device, newName, fs[fsi]); lv > maxLocalVer {
|
if lv := ldbInsert(batch, folder, device, fs[fsi]); lv > maxLocalVer {
|
||||||
maxLocalVer = lv
|
maxLocalVer = lv
|
||||||
}
|
}
|
||||||
if fs[fsi].IsInvalid() {
|
if fs[fsi].IsInvalid() {
|
||||||
@ -234,7 +228,7 @@ func ldbGenericReplace(db *leveldb.DB, folder, device []byte, fs []protocol.File
|
|||||||
ef.UnmarshalXDR(dbi.Value())
|
ef.UnmarshalXDR(dbi.Value())
|
||||||
if fs[fsi].Version > ef.Version ||
|
if fs[fsi].Version > ef.Version ||
|
||||||
(fs[fsi].Version == ef.Version && fs[fsi].Flags != ef.Flags) {
|
(fs[fsi].Version == ef.Version && fs[fsi].Flags != ef.Flags) {
|
||||||
if lv := ldbInsert(batch, folder, device, newName, fs[fsi]); lv > maxLocalVer {
|
if lv := ldbInsert(batch, folder, device, fs[fsi]); lv > maxLocalVer {
|
||||||
maxLocalVer = lv
|
maxLocalVer = lv
|
||||||
}
|
}
|
||||||
if fs[fsi].IsInvalid() {
|
if fs[fsi].IsInvalid() {
|
||||||
@ -243,15 +237,12 @@ func ldbGenericReplace(db *leveldb.DB, folder, device []byte, fs []protocol.File
|
|||||||
ldbUpdateGlobal(snap, batch, folder, device, newName, fs[fsi].Version)
|
ldbUpdateGlobal(snap, batch, folder, device, newName, fs[fsi].Version)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Iterate both sides.
|
|
||||||
fsi++
|
fsi++
|
||||||
moreDb = dbi.Next()
|
moreDb = dbi.Next()
|
||||||
|
|
||||||
case moreDb && (!moreFs || cmp == 1):
|
case moreDb && (!moreFs || cmp == 1):
|
||||||
if deleteFn != nil {
|
if lv := deleteFn(snap, batch, folder, device, oldName, dbi); lv > maxLocalVer {
|
||||||
if lv := deleteFn(snap, batch, folder, device, oldName, dbi); lv > maxLocalVer {
|
maxLocalVer = lv
|
||||||
maxLocalVer = lv
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
moreDb = dbi.Next()
|
moreDb = dbi.Next()
|
||||||
}
|
}
|
||||||
@ -268,7 +259,7 @@ func ldbGenericReplace(db *leveldb.DB, folder, device []byte, fs []protocol.File
|
|||||||
func ldbReplace(db *leveldb.DB, folder, device []byte, fs []protocol.FileInfo) uint64 {
|
func ldbReplace(db *leveldb.DB, folder, device []byte, fs []protocol.FileInfo) uint64 {
|
||||||
// TODO: Return the remaining maxLocalVer?
|
// TODO: Return the remaining maxLocalVer?
|
||||||
return ldbGenericReplace(db, folder, device, fs, func(db dbReader, batch dbWriter, folder, device, name []byte, dbi iterator.Iterator) uint64 {
|
return ldbGenericReplace(db, folder, device, fs, func(db dbReader, batch dbWriter, folder, device, name []byte, dbi iterator.Iterator) uint64 {
|
||||||
// Disk has files that we are missing. Remove it.
|
// Database has a file that we are missing. Remove it.
|
||||||
if debug {
|
if debug {
|
||||||
l.Debugf("delete; folder=%q device=%v name=%q", folder, protocol.DeviceIDFromBytes(device), name)
|
l.Debugf("delete; folder=%q device=%v name=%q", folder, protocol.DeviceIDFromBytes(device), name)
|
||||||
}
|
}
|
||||||
@ -321,7 +312,7 @@ func ldbUpdate(db *leveldb.DB, folder, device []byte, fs []protocol.FileInfo) ui
|
|||||||
fk := deviceKey(folder, device, name)
|
fk := deviceKey(folder, device, name)
|
||||||
bs, err := snap.Get(fk, nil)
|
bs, err := snap.Get(fk, nil)
|
||||||
if err == leveldb.ErrNotFound {
|
if err == leveldb.ErrNotFound {
|
||||||
if lv := ldbInsert(batch, folder, device, name, f); lv > maxLocalVer {
|
if lv := ldbInsert(batch, folder, device, f); lv > maxLocalVer {
|
||||||
maxLocalVer = lv
|
maxLocalVer = lv
|
||||||
}
|
}
|
||||||
if f.IsInvalid() {
|
if f.IsInvalid() {
|
||||||
@ -340,7 +331,7 @@ func ldbUpdate(db *leveldb.DB, folder, device []byte, fs []protocol.FileInfo) ui
|
|||||||
// Flags might change without the version being bumped when we set the
|
// Flags might change without the version being bumped when we set the
|
||||||
// invalid flag on an existing file.
|
// invalid flag on an existing file.
|
||||||
if ef.Version != f.Version || ef.Flags != f.Flags {
|
if ef.Version != f.Version || ef.Flags != f.Flags {
|
||||||
if lv := ldbInsert(batch, folder, device, name, f); lv > maxLocalVer {
|
if lv := ldbInsert(batch, folder, device, f); lv > maxLocalVer {
|
||||||
maxLocalVer = lv
|
maxLocalVer = lv
|
||||||
}
|
}
|
||||||
if f.IsInvalid() {
|
if f.IsInvalid() {
|
||||||
@ -359,7 +350,7 @@ func ldbUpdate(db *leveldb.DB, folder, device []byte, fs []protocol.FileInfo) ui
|
|||||||
return maxLocalVer
|
return maxLocalVer
|
||||||
}
|
}
|
||||||
|
|
||||||
func ldbInsert(batch dbWriter, folder, device, name []byte, file protocol.FileInfo) uint64 {
|
func ldbInsert(batch dbWriter, folder, device []byte, file protocol.FileInfo) uint64 {
|
||||||
if debug {
|
if debug {
|
||||||
l.Debugf("insert; folder=%q device=%v %v", folder, protocol.DeviceIDFromBytes(device), file)
|
l.Debugf("insert; folder=%q device=%v %v", folder, protocol.DeviceIDFromBytes(device), file)
|
||||||
}
|
}
|
||||||
@ -368,6 +359,7 @@ func ldbInsert(batch dbWriter, folder, device, name []byte, file protocol.FileIn
|
|||||||
file.LocalVersion = clock(0)
|
file.LocalVersion = clock(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
name := []byte(file.Name)
|
||||||
nk := deviceKey(folder, device, name)
|
nk := deviceKey(folder, device, name)
|
||||||
batch.Put(nk, file.MarshalXDR())
|
batch.Put(nk, file.MarshalXDR())
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user