lib/db: Add "dirty" function terminology to getGlobal (ref #5462) (#5463)

This commit is contained in:
Simon Frei 2019-01-18 13:01:39 +01:00 committed by Jakob Borg
parent 1e69997ecd
commit 1f87b874af
3 changed files with 27 additions and 27 deletions

View File

@ -224,35 +224,14 @@ func (db *instance) getFileDirty(folder, device, file []byte) (protocol.FileInfo
return f, true return f, true
} }
func (db *instance) getGlobal(folder, file []byte, truncate bool) (FileIntf, bool) { func (db *instance) getGlobalDirty(folder, file []byte, truncate bool) (FileIntf, bool) {
t := db.newReadOnlyTransaction() t := db.newReadOnlyTransaction()
defer t.close() defer t.close()
_, _, f, ok := db.getGlobalInto(t, nil, nil, folder, file, truncate) _, _, f, ok := t.getGlobalInto(nil, nil, folder, file, truncate)
return f, ok return f, ok
} }
func (db *instance) getGlobalInto(t readOnlyTransaction, gk, dk, folder, file []byte, truncate bool) ([]byte, []byte, FileIntf, bool) {
gk = db.keyer.GenerateGlobalVersionKey(gk, folder, file)
bs, err := t.Get(gk, nil)
if err != nil {
return gk, dk, nil, false
}
vl, ok := unmarshalVersionList(bs)
if !ok {
return gk, dk, nil, false
}
dk = db.keyer.GenerateDeviceFileKey(dk, folder, vl.Versions[0].Device, file)
if fi, ok := t.getFileTrunc(dk, truncate); ok {
return gk, dk, fi, true
}
return gk, dk, nil, false
}
func (db *instance) withGlobal(folder, prefix []byte, truncate bool, fn Iterator) { func (db *instance) withGlobal(folder, prefix []byte, truncate bool, fn Iterator) {
t := db.newReadOnlyTransaction() t := db.newReadOnlyTransaction()
defer t.close() defer t.close()
@ -265,7 +244,7 @@ func (db *instance) withGlobal(folder, prefix []byte, truncate bool, fn Iterator
prefix = append(prefix, '/') prefix = append(prefix, '/')
} }
if _, _, f, ok := db.getGlobalInto(t, nil, nil, folder, unslashedPrefix, truncate); ok && !fn(f) { if _, _, f, ok := t.getGlobalInto(nil, nil, folder, unslashedPrefix, truncate); ok && !fn(f) {
return return
} }
} }
@ -413,7 +392,7 @@ func (db *instance) withNeedLocal(folder []byte, truncate bool, fn Iterator) {
var f FileIntf var f FileIntf
var ok bool var ok bool
for dbi.Next() { for dbi.Next() {
gk, dk, f, ok = db.getGlobalInto(t, gk, dk, folder, db.keyer.NameFromGlobalVersionKey(dbi.Key()), truncate) gk, dk, f, ok = t.getGlobalInto(gk, dk, folder, db.keyer.NameFromGlobalVersionKey(dbi.Key()), truncate)
if !ok { if !ok {
continue continue
} }

View File

@ -250,7 +250,7 @@ func (s *FileSet) Get(device protocol.DeviceID, file string) (protocol.FileInfo,
} }
func (s *FileSet) GetGlobal(file string) (protocol.FileInfo, bool) { func (s *FileSet) GetGlobal(file string) (protocol.FileInfo, bool) {
fi, ok := s.db.getGlobal([]byte(s.folder), []byte(osutil.NormalizedFilename(file)), false) fi, ok := s.db.getGlobalDirty([]byte(s.folder), []byte(osutil.NormalizedFilename(file)), false)
if !ok { if !ok {
return protocol.FileInfo{}, false return protocol.FileInfo{}, false
} }
@ -260,7 +260,7 @@ func (s *FileSet) GetGlobal(file string) (protocol.FileInfo, bool) {
} }
func (s *FileSet) GetGlobalTruncated(file string) (FileInfoTruncated, bool) { func (s *FileSet) GetGlobalTruncated(file string) (FileInfoTruncated, bool) {
fi, ok := s.db.getGlobal([]byte(s.folder), []byte(osutil.NormalizedFilename(file)), true) fi, ok := s.db.getGlobalDirty([]byte(s.folder), []byte(osutil.NormalizedFilename(file)), true)
if !ok { if !ok {
return FileInfoTruncated{}, false return FileInfoTruncated{}, false
} }

View File

@ -65,6 +65,27 @@ func (t readOnlyTransaction) getFileTrunc(key []byte, trunc bool) (FileIntf, boo
return f, true return f, true
} }
func (t readOnlyTransaction) getGlobalInto(gk, dk, folder, file []byte, truncate bool) ([]byte, []byte, FileIntf, bool) {
gk = t.db.keyer.GenerateGlobalVersionKey(gk, folder, file)
bs, err := t.Get(gk, nil)
if err != nil {
return gk, dk, nil, false
}
vl, ok := unmarshalVersionList(bs)
if !ok {
return gk, dk, nil, false
}
dk = t.db.keyer.GenerateDeviceFileKey(dk, folder, vl.Versions[0].Device, file)
if fi, ok := t.getFileTrunc(dk, truncate); ok {
return gk, dk, fi, true
}
return gk, dk, nil, false
}
// A readWriteTransaction is a readOnlyTransaction plus a batch for writes. // A readWriteTransaction is a readOnlyTransaction plus a batch for writes.
// The batch will be committed on close() or by checkFlush() if it exceeds the // The batch will be committed on close() or by checkFlush() if it exceeds the
// batch size. // batch size.