mirror of
https://github.com/octoleo/syncthing.git
synced 2024-11-09 14:50:56 +00:00
lib/db: Minor polish (#5469)
This commit is contained in:
parent
f3d735c56a
commit
df4d754197
@ -34,24 +34,23 @@ func (db *instance) updateFiles(folder, device []byte, fs []protocol.FileInfo, m
|
|||||||
t := db.newReadWriteTransaction()
|
t := db.newReadWriteTransaction()
|
||||||
defer t.close()
|
defer t.close()
|
||||||
|
|
||||||
var fk []byte
|
var dk, gk []byte
|
||||||
var gk []byte
|
devID := protocol.DeviceIDFromBytes(device)
|
||||||
for _, f := range fs {
|
for _, f := range fs {
|
||||||
name := []byte(f.Name)
|
name := []byte(f.Name)
|
||||||
fk = db.keyer.GenerateDeviceFileKey(fk, folder, device, name)
|
dk = db.keyer.GenerateDeviceFileKey(dk, folder, device, name)
|
||||||
|
|
||||||
ef, ok := t.getFileTrunc(fk, true)
|
ef, ok := t.getFileTrunc(dk, true)
|
||||||
if ok && unchanged(f, ef) {
|
if ok && unchanged(f, ef) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
devID := protocol.DeviceIDFromBytes(device)
|
|
||||||
if ok {
|
if ok {
|
||||||
meta.removeFile(devID, ef)
|
meta.removeFile(devID, ef)
|
||||||
}
|
}
|
||||||
meta.addFile(devID, f)
|
meta.addFile(devID, f)
|
||||||
|
|
||||||
t.insertFile(fk, folder, device, f)
|
t.insertFile(dk, folder, device, f)
|
||||||
|
|
||||||
gk = db.keyer.GenerateGlobalVersionKey(gk, folder, name)
|
gk = db.keyer.GenerateGlobalVersionKey(gk, folder, name)
|
||||||
t.updateGlobal(gk, folder, device, f, meta)
|
t.updateGlobal(gk, folder, device, f, meta)
|
||||||
@ -66,8 +65,7 @@ func (db *instance) addSequences(folder []byte, fs []protocol.FileInfo) {
|
|||||||
t := db.newReadWriteTransaction()
|
t := db.newReadWriteTransaction()
|
||||||
defer t.close()
|
defer t.close()
|
||||||
|
|
||||||
var sk []byte
|
var dk, sk []byte
|
||||||
var dk []byte
|
|
||||||
for _, f := range fs {
|
for _, f := range fs {
|
||||||
sk = db.keyer.GenerateSequenceKey(sk, folder, f.Sequence)
|
sk = db.keyer.GenerateSequenceKey(sk, folder, f.Sequence)
|
||||||
dk = db.keyer.GenerateDeviceFileKey(dk, folder, protocol.LocalDeviceID[:], []byte(f.Name))
|
dk = db.keyer.GenerateDeviceFileKey(dk, folder, protocol.LocalDeviceID[:], []byte(f.Name))
|
||||||
@ -159,7 +157,6 @@ func (db *instance) withAllFolderTruncated(folder []byte, fn func(device []byte,
|
|||||||
defer dbi.Release()
|
defer dbi.Release()
|
||||||
|
|
||||||
var gk []byte
|
var gk []byte
|
||||||
|
|
||||||
for dbi.Next() {
|
for dbi.Next() {
|
||||||
device, ok := db.keyer.DeviceFromDeviceFileKey(dbi.Key())
|
device, ok := db.keyer.DeviceFromDeviceFileKey(dbi.Key())
|
||||||
if !ok {
|
if !ok {
|
||||||
@ -230,7 +227,7 @@ func (db *instance) withGlobal(folder, prefix []byte, truncate bool, fn Iterator
|
|||||||
dbi := t.NewIterator(util.BytesPrefix(db.keyer.GenerateGlobalVersionKey(nil, folder, prefix)), nil)
|
dbi := t.NewIterator(util.BytesPrefix(db.keyer.GenerateGlobalVersionKey(nil, folder, prefix)), nil)
|
||||||
defer dbi.Release()
|
defer dbi.Release()
|
||||||
|
|
||||||
var fk []byte
|
var dk []byte
|
||||||
for dbi.Next() {
|
for dbi.Next() {
|
||||||
name := db.keyer.NameFromGlobalVersionKey(dbi.Key())
|
name := db.keyer.NameFromGlobalVersionKey(dbi.Key())
|
||||||
if len(prefix) > 0 && !bytes.HasPrefix(name, prefix) {
|
if len(prefix) > 0 && !bytes.HasPrefix(name, prefix) {
|
||||||
@ -242,9 +239,9 @@ func (db *instance) withGlobal(folder, prefix []byte, truncate bool, fn Iterator
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
fk = db.keyer.GenerateDeviceFileKey(fk, folder, vl.Versions[0].Device, name)
|
dk = db.keyer.GenerateDeviceFileKey(dk, folder, vl.Versions[0].Device, name)
|
||||||
|
|
||||||
f, ok := t.getFileTrunc(fk, truncate)
|
f, ok := t.getFileTrunc(dk, truncate)
|
||||||
if !ok {
|
if !ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -298,7 +295,8 @@ func (db *instance) withNeed(folder, device []byte, truncate bool, fn Iterator)
|
|||||||
dbi := t.NewIterator(util.BytesPrefix(db.keyer.GenerateGlobalVersionKey(nil, folder, nil).WithoutName()), nil)
|
dbi := t.NewIterator(util.BytesPrefix(db.keyer.GenerateGlobalVersionKey(nil, folder, nil).WithoutName()), nil)
|
||||||
defer dbi.Release()
|
defer dbi.Release()
|
||||||
|
|
||||||
var fk []byte
|
var dk []byte
|
||||||
|
devID := protocol.DeviceIDFromBytes(device)
|
||||||
for dbi.Next() {
|
for dbi.Next() {
|
||||||
vl, ok := unmarshalVersionList(dbi.Value())
|
vl, ok := unmarshalVersionList(dbi.Value())
|
||||||
if !ok {
|
if !ok {
|
||||||
@ -328,8 +326,8 @@ func (db *instance) withNeed(folder, device []byte, truncate bool, fn Iterator)
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
fk = db.keyer.GenerateDeviceFileKey(fk, folder, vl.Versions[i].Device, name)
|
dk = db.keyer.GenerateDeviceFileKey(dk, folder, vl.Versions[i].Device, name)
|
||||||
bs, err := t.Get(fk, nil)
|
bs, err := t.Get(dk, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Debugln("surprise error:", err)
|
l.Debugln("surprise error:", err)
|
||||||
continue
|
continue
|
||||||
@ -346,7 +344,7 @@ func (db *instance) withNeed(folder, device []byte, truncate bool, fn Iterator)
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
l.Debugf("need folder=%q device=%v name=%q have=%v invalid=%v haveV=%v globalV=%v globalDev=%v", folder, protocol.DeviceIDFromBytes(device), name, have, haveFV.Invalid, haveFV.Version, needVersion, needDevice)
|
l.Debugf("need folder=%q device=%v name=%q have=%v invalid=%v haveV=%v globalV=%v globalDev=%v", folder, devID, name, have, haveFV.Invalid, haveFV.Version, needVersion, needDevice)
|
||||||
|
|
||||||
if !fn(gf) {
|
if !fn(gf) {
|
||||||
return
|
return
|
||||||
@ -365,8 +363,7 @@ func (db *instance) withNeedLocal(folder []byte, truncate bool, fn Iterator) {
|
|||||||
dbi := t.NewIterator(util.BytesPrefix(db.keyer.GenerateNeedFileKey(nil, folder, nil).WithoutName()), nil)
|
dbi := t.NewIterator(util.BytesPrefix(db.keyer.GenerateNeedFileKey(nil, folder, nil).WithoutName()), nil)
|
||||||
defer dbi.Release()
|
defer dbi.Release()
|
||||||
|
|
||||||
var dk []byte
|
var gk, dk []byte
|
||||||
var gk []byte
|
|
||||||
var f FileIntf
|
var f FileIntf
|
||||||
var ok bool
|
var ok bool
|
||||||
for dbi.Next() {
|
for dbi.Next() {
|
||||||
@ -406,7 +403,6 @@ func (db *instance) dropDeviceFolder(device, folder []byte, meta *metadataTracke
|
|||||||
defer dbi.Release()
|
defer dbi.Release()
|
||||||
|
|
||||||
var gk []byte
|
var gk []byte
|
||||||
|
|
||||||
for dbi.Next() {
|
for dbi.Next() {
|
||||||
key := dbi.Key()
|
key := dbi.Key()
|
||||||
name := db.keyer.NameFromDeviceFileKey(key)
|
name := db.keyer.NameFromDeviceFileKey(key)
|
||||||
@ -424,7 +420,7 @@ func (db *instance) checkGlobals(folder []byte, meta *metadataTracker) {
|
|||||||
dbi := t.NewIterator(util.BytesPrefix(db.keyer.GenerateGlobalVersionKey(nil, folder, nil).WithoutName()), nil)
|
dbi := t.NewIterator(util.BytesPrefix(db.keyer.GenerateGlobalVersionKey(nil, folder, nil).WithoutName()), nil)
|
||||||
defer dbi.Release()
|
defer dbi.Release()
|
||||||
|
|
||||||
var fk []byte
|
var dk []byte
|
||||||
for dbi.Next() {
|
for dbi.Next() {
|
||||||
vl, ok := unmarshalVersionList(dbi.Value())
|
vl, ok := unmarshalVersionList(dbi.Value())
|
||||||
if !ok {
|
if !ok {
|
||||||
@ -439,8 +435,8 @@ func (db *instance) checkGlobals(folder []byte, meta *metadataTracker) {
|
|||||||
name := db.keyer.NameFromGlobalVersionKey(dbi.Key())
|
name := db.keyer.NameFromGlobalVersionKey(dbi.Key())
|
||||||
var newVL VersionList
|
var newVL VersionList
|
||||||
for i, version := range vl.Versions {
|
for i, version := range vl.Versions {
|
||||||
fk = db.keyer.GenerateDeviceFileKey(fk, folder, version.Device, name)
|
dk = db.keyer.GenerateDeviceFileKey(dk, folder, version.Device, name)
|
||||||
_, err := t.Get(fk, nil)
|
_, err := t.Get(dk, nil)
|
||||||
if err == leveldb.ErrNotFound {
|
if err == leveldb.ErrNotFound {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -451,7 +447,7 @@ func (db *instance) checkGlobals(folder []byte, meta *metadataTracker) {
|
|||||||
newVL.Versions = append(newVL.Versions, version)
|
newVL.Versions = append(newVL.Versions, version)
|
||||||
|
|
||||||
if i == 0 {
|
if i == 0 {
|
||||||
if fi, ok := t.getFileByKey(fk); ok {
|
if fi, ok := t.getFileByKey(dk); ok {
|
||||||
meta.addFile(protocol.GlobalDeviceID, fi)
|
meta.addFile(protocol.GlobalDeviceID, fi)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -466,8 +462,7 @@ func (db *instance) checkGlobals(folder []byte, meta *metadataTracker) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (db *instance) getIndexID(device, folder []byte) protocol.IndexID {
|
func (db *instance) getIndexID(device, folder []byte) protocol.IndexID {
|
||||||
key := db.keyer.GenerateIndexIDKey(nil, device, folder)
|
cur, err := db.Get(db.keyer.GenerateIndexIDKey(nil, device, folder), nil)
|
||||||
cur, err := db.Get(key, nil)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
@ -481,9 +476,8 @@ func (db *instance) getIndexID(device, folder []byte) protocol.IndexID {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (db *instance) setIndexID(device, folder []byte, id protocol.IndexID) {
|
func (db *instance) setIndexID(device, folder []byte, id protocol.IndexID) {
|
||||||
key := db.keyer.GenerateIndexIDKey(nil, device, folder)
|
|
||||||
bs, _ := id.Marshal() // marshalling can't fail
|
bs, _ := id.Marshal() // marshalling can't fail
|
||||||
if err := db.Put(key, bs, nil); err != nil {
|
if err := db.Put(db.keyer.GenerateIndexIDKey(nil, device, folder), bs, nil); err != nil {
|
||||||
panic("storing index ID: " + err.Error())
|
panic("storing index ID: " + err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -500,12 +494,7 @@ func (db *instance) dropPrefix(prefix []byte) {
|
|||||||
t := db.newReadWriteTransaction()
|
t := db.newReadWriteTransaction()
|
||||||
defer t.close()
|
defer t.close()
|
||||||
|
|
||||||
dbi := t.NewIterator(util.BytesPrefix(prefix), nil)
|
t.deleteKeyPrefix(prefix)
|
||||||
defer dbi.Release()
|
|
||||||
|
|
||||||
for dbi.Next() {
|
|
||||||
t.Delete(dbi.Key())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func unmarshalTrunc(bs []byte, truncate bool) (FileIntf, error) {
|
func unmarshalTrunc(bs []byte, truncate bool) (FileIntf, error) {
|
||||||
|
Loading…
Reference in New Issue
Block a user