lib/db: Various polish (#5471)

naming: buf -> keyBuf
dedup: use getFileTrunc
manually inline insertFile
This commit is contained in:
Simon Frei 2019-01-20 10:24:39 +01:00 committed by Jakob Borg
parent 29e4b417f2
commit a371b15398
4 changed files with 15 additions and 27 deletions

View File

@ -50,7 +50,8 @@ func (db *instance) updateFiles(folder, device []byte, fs []protocol.FileInfo, m
}
meta.addFile(devID, f)
t.insertFile(dk, folder, device, f)
l.Debugf("insert; folder=%q device=%v %v", folder, devID, f)
t.Put(dk, mustMarshal(&f))
gk = db.keyer.GenerateGlobalVersionKey(gk, folder, name)
keyBuf, _ = t.updateGlobal(gk, keyBuf, folder, device, f, meta)
@ -156,7 +157,7 @@ func (db *instance) withAllFolderTruncated(folder []byte, fn func(device []byte,
dbi := t.NewIterator(util.BytesPrefix(db.keyer.GenerateDeviceFileKey(nil, folder, nil, nil).WithoutNameAndDevice()), nil)
defer dbi.Release()
var gk, buf []byte
var gk, keyBuf []byte
for dbi.Next() {
device, ok := db.keyer.DeviceFromDeviceFileKey(dbi.Key())
if !ok {
@ -181,7 +182,7 @@ func (db *instance) withAllFolderTruncated(folder []byte, fn func(device []byte,
l.Infof("Dropping invalid filename %q from database", f.Name)
name := []byte(f.Name)
gk = db.keyer.GenerateGlobalVersionKey(gk, folder, name)
buf = t.removeFromGlobal(gk, buf, folder, device, name, nil)
keyBuf = t.removeFromGlobal(gk, keyBuf, folder, device, name, nil)
t.Delete(dbi.Key())
t.checkFlush()
continue
@ -326,15 +327,8 @@ func (db *instance) withNeed(folder, device []byte, truncate bool, fn Iterator)
}
dk = db.keyer.GenerateDeviceFileKey(dk, folder, vl.Versions[i].Device, name)
bs, err := t.Get(dk, nil)
if err != nil {
l.Debugln("surprise error:", err)
continue
}
gf, err := unmarshalTrunc(bs, truncate)
if err != nil {
l.Debugln("unmarshal error:", err)
gf, ok := t.getFileTrunc(dk, truncate)
if !ok {
continue
}
@ -362,11 +356,11 @@ func (db *instance) withNeedLocal(folder []byte, truncate bool, fn Iterator) {
dbi := t.NewIterator(util.BytesPrefix(db.keyer.GenerateNeedFileKey(nil, folder, nil).WithoutName()), nil)
defer dbi.Release()
var buf []byte
var keyBuf []byte
var f FileIntf
var ok bool
for dbi.Next() {
buf, f, ok = t.getGlobal(buf, folder, db.keyer.NameFromGlobalVersionKey(dbi.Key()), truncate)
keyBuf, f, ok = t.getGlobal(keyBuf, folder, db.keyer.NameFromGlobalVersionKey(dbi.Key()), truncate)
if !ok {
continue
}
@ -401,11 +395,11 @@ func (db *instance) dropDeviceFolder(device, folder []byte, meta *metadataTracke
dbi := t.NewIterator(util.BytesPrefix(db.keyer.GenerateDeviceFileKey(nil, folder, device, nil)), nil)
defer dbi.Release()
var gk, buf []byte
var gk, keyBuf []byte
for dbi.Next() {
name := db.keyer.NameFromDeviceFileKey(dbi.Key())
gk = db.keyer.GenerateGlobalVersionKey(gk, folder, name)
buf = t.removeFromGlobal(gk, buf, folder, device, name, meta)
keyBuf = t.removeFromGlobal(gk, keyBuf, folder, device, name, meta)
t.Delete(dbi.Key())
t.checkFlush()
}

View File

@ -272,12 +272,12 @@ func (m *metadataTracker) Counts(dev protocol.DeviceID, flag uint32) Counts {
return m.counts.Counts[idx]
}
// nextSeq allocates a new sequence number for the given device
func (m *metadataTracker) nextSeq(dev protocol.DeviceID) int64 {
// nextLocalSeq allocates a new local sequence number
func (m *metadataTracker) nextLocalSeq() int64 {
m.mut.Lock()
defer m.mut.Unlock()
c := m.countsPtr(dev, 0)
c := m.countsPtr(protocol.LocalDeviceID, 0)
c.Sequence++
return c.Sequence
}

View File

@ -163,12 +163,12 @@ func (s *FileSet) Update(device protocol.DeviceID, fs []protocol.FileInfo) {
fs = fs[:0]
folder := []byte(s.folder)
for _, nf := range oldFs {
ef, ok := s.db.getFileDirty(folder, device[:], []byte(osutil.NormalizedFilename(nf.Name)))
ef, ok := s.db.getFileDirty(folder, device[:], []byte(nf.Name))
if ok && unchanged(nf, ef) {
continue
}
nf.Sequence = s.meta.nextSeq(protocol.LocalDeviceID)
nf.Sequence = s.meta.nextLocalSeq()
fs = append(fs, nf)
if ok {

View File

@ -119,12 +119,6 @@ func (t readWriteTransaction) flush() {
}
}
func (t readWriteTransaction) insertFile(fk, folder, device []byte, file protocol.FileInfo) {
l.Debugf("insert; folder=%q device=%v %v", folder, protocol.DeviceIDFromBytes(device), file)
t.Put(fk, mustMarshal(&file))
}
// updateGlobal adds this device+version to the version list for the given
// file. If the device is already present in the list, the version is updated.
// If the file does not have an entry in the global list, it is created.