lib/db: Refactor getting global lists (#6529)

* lib/db: Refactor getting global lists

* VL -> Versions

* keyBuf

* don't touch db migration
This commit is contained in:
Simon Frei 2020-05-01 09:30:20 +02:00 committed by GitHub
parent ac7338f1f2
commit 22242d51be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -112,23 +112,34 @@ func (t readOnlyTransaction) fillFileInfo(fi *protocol.FileInfo) error {
return nil return nil
} }
func (t readOnlyTransaction) getGlobal(keyBuf, folder, file []byte, truncate bool) ([]byte, FileIntf, bool, error) { func (t readOnlyTransaction) getGlobalVersions(keyBuf, folder, file []byte) (VersionList, error) {
var err error var err error
keyBuf, err = t.keyer.GenerateGlobalVersionKey(keyBuf, folder, file) keyBuf, err = t.keyer.GenerateGlobalVersionKey(keyBuf, folder, file)
if err != nil { if err != nil {
return nil, nil, false, err return VersionList{}, err
}
return t.getGlobalVersionsByKey(keyBuf)
} }
bs, err := t.Get(keyBuf) func (t readOnlyTransaction) getGlobalVersionsByKey(key []byte) (VersionList, error) {
if backend.IsNotFound(err) { bs, err := t.Get(key)
return keyBuf, nil, false, nil
}
if err != nil { if err != nil {
return nil, nil, false, err return VersionList{}, err
} }
var vl VersionList var vl VersionList
if err := vl.Unmarshal(bs); err != nil { if err := vl.Unmarshal(bs); err != nil {
return VersionList{}, err
}
return vl, nil
}
func (t readOnlyTransaction) getGlobal(keyBuf, folder, file []byte, truncate bool) ([]byte, FileIntf, bool, error) {
vl, err := t.getGlobalVersions(keyBuf, folder, file)
if backend.IsNotFound(err) {
return keyBuf, nil, false, nil
} else if err != nil {
return nil, nil, false, err return nil, nil, false, err
} }
@ -291,11 +302,7 @@ func (t *readOnlyTransaction) withGlobal(folder, prefix []byte, truncate bool, f
} }
func (t *readOnlyTransaction) availability(folder, file []byte) ([]protocol.DeviceID, error) { func (t *readOnlyTransaction) availability(folder, file []byte) ([]protocol.DeviceID, error) {
k, err := t.keyer.GenerateGlobalVersionKey(nil, folder, file) vl, err := t.getGlobalVersions(nil, folder, file)
if err != nil {
return nil, err
}
bs, err := t.Get(k)
if backend.IsNotFound(err) { if backend.IsNotFound(err) {
return nil, nil return nil, nil
} }
@ -303,11 +310,6 @@ func (t *readOnlyTransaction) availability(folder, file []byte) ([]protocol.Devi
return nil, err return nil, err
} }
var vl VersionList
if err := vl.Unmarshal(bs); err != nil {
return nil, err
}
var devices []protocol.DeviceID var devices []protocol.DeviceID
for _, v := range vl.Versions { for _, v := range vl.Versions {
if !v.Version.Equal(vl.Versions[0].Version) { if !v.Version.Equal(vl.Versions[0].Version) {
@ -473,11 +475,8 @@ func (t readWriteTransaction) putFile(fkey []byte, fi protocol.FileInfo, truncat
func (t readWriteTransaction) updateGlobal(gk, keyBuf, folder, device []byte, file protocol.FileInfo, meta *metadataTracker) ([]byte, bool, error) { func (t readWriteTransaction) updateGlobal(gk, keyBuf, folder, device []byte, file protocol.FileInfo, meta *metadataTracker) ([]byte, bool, error) {
l.Debugf("update global; folder=%q device=%v file=%q version=%v invalid=%v", folder, protocol.DeviceIDFromBytes(device), file.Name, file.Version, file.IsInvalid()) l.Debugf("update global; folder=%q device=%v file=%q version=%v invalid=%v", folder, protocol.DeviceIDFromBytes(device), file.Name, file.Version, file.IsInvalid())
var fl VersionList fl, err := t.getGlobalVersionsByKey(gk)
svl, err := t.Get(gk) if err != nil && !backend.IsNotFound(err) {
if err == nil {
_ = fl.Unmarshal(svl) // Ignore error, continue with empty fl
} else if !backend.IsNotFound(err) {
return nil, false, err return nil, false, err
} }
@ -603,7 +602,7 @@ func need(global FileIntf, haveLocal bool, localVersion protocol.Vector) bool {
func (t readWriteTransaction) removeFromGlobal(gk, keyBuf, folder, device []byte, file []byte, meta *metadataTracker) ([]byte, error) { func (t readWriteTransaction) removeFromGlobal(gk, keyBuf, folder, device []byte, file []byte, meta *metadataTracker) ([]byte, error) {
l.Debugf("remove from global; folder=%q device=%v file=%q", folder, protocol.DeviceIDFromBytes(device), file) l.Debugf("remove from global; folder=%q device=%v file=%q", folder, protocol.DeviceIDFromBytes(device), file)
svl, err := t.Get(gk) fl, err := t.getGlobalVersionsByKey(gk)
if backend.IsNotFound(err) { if backend.IsNotFound(err) {
// We might be called to "remove" a global version that doesn't exist // We might be called to "remove" a global version that doesn't exist
// if the first update for the file is already marked invalid. // if the first update for the file is already marked invalid.
@ -612,12 +611,6 @@ func (t readWriteTransaction) removeFromGlobal(gk, keyBuf, folder, device []byte
return nil, err return nil, err
} }
var fl VersionList
err = fl.Unmarshal(svl)
if err != nil {
return nil, err
}
fl, _, removedAt := fl.pop(device) fl, _, removedAt := fl.pop(device)
if removedAt == -1 { if removedAt == -1 {
// There is no version for the given device // There is no version for the given device