2015-10-29 07:07:51 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2015-10-29 07:07:51 +00:00
|
|
|
|
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-06-13 17:44:03 +00:00
|
|
|
"sync/atomic"
|
2015-10-29 07:07:51 +00:00
|
|
|
|
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb"
|
|
|
|
)
|
|
|
|
|
|
|
|
// A readOnlyTransaction represents a database snapshot.
|
|
|
|
type readOnlyTransaction struct {
|
|
|
|
*leveldb.Snapshot
|
2015-10-31 11:31:25 +00:00
|
|
|
db *Instance
|
2015-10-29 07:07:51 +00:00
|
|
|
}
|
|
|
|
|
2015-10-31 11:31:25 +00:00
|
|
|
func (db *Instance) newReadOnlyTransaction() readOnlyTransaction {
|
2015-10-29 07:07:51 +00:00
|
|
|
snap, err := db.GetSnapshot()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return readOnlyTransaction{
|
|
|
|
Snapshot: snap,
|
2015-10-31 06:20:35 +00:00
|
|
|
db: db,
|
2015-10-29 07:07:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t readOnlyTransaction) close() {
|
|
|
|
t.Release()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t readOnlyTransaction) getFile(folder, device, file []byte) (protocol.FileInfo, bool) {
|
2018-05-01 21:39:15 +00:00
|
|
|
return t.db.getFile(t.db.deviceKey(folder, device, file))
|
2015-10-29 07:07:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// A readWriteTransaction is a readOnlyTransaction plus a batch for writes.
|
|
|
|
// The batch will be committed on close() or by checkFlush() if it exceeds the
|
|
|
|
// batch size.
|
|
|
|
type readWriteTransaction struct {
|
|
|
|
readOnlyTransaction
|
|
|
|
*leveldb.Batch
|
|
|
|
}
|
|
|
|
|
2015-10-31 11:31:25 +00:00
|
|
|
func (db *Instance) newReadWriteTransaction() readWriteTransaction {
|
2015-10-29 07:07:51 +00:00
|
|
|
t := db.newReadOnlyTransaction()
|
|
|
|
return readWriteTransaction{
|
|
|
|
readOnlyTransaction: t,
|
|
|
|
Batch: new(leveldb.Batch),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t readWriteTransaction) close() {
|
2016-06-13 17:44:03 +00:00
|
|
|
t.flush()
|
2015-10-29 07:07:51 +00:00
|
|
|
t.readOnlyTransaction.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t readWriteTransaction) checkFlush() {
|
|
|
|
if t.Batch.Len() > batchFlushSize {
|
2016-06-13 17:44:03 +00:00
|
|
|
t.flush()
|
2015-10-29 07:07:51 +00:00
|
|
|
t.Batch.Reset()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-13 17:44:03 +00:00
|
|
|
func (t readWriteTransaction) flush() {
|
|
|
|
if err := t.db.Write(t.Batch, nil); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
atomic.AddInt64(&t.db.committed, int64(t.Batch.Len()))
|
|
|
|
}
|
|
|
|
|
2018-05-01 21:39:15 +00:00
|
|
|
func (t readWriteTransaction) insertFile(fk, folder, device []byte, file protocol.FileInfo) {
|
2015-10-29 07:07:51 +00:00
|
|
|
l.Debugf("insert; folder=%q device=%v %v", folder, protocol.DeviceIDFromBytes(device), file)
|
|
|
|
|
2018-05-01 21:39:15 +00:00
|
|
|
t.Put(fk, mustMarshal(&file))
|
2015-10-29 07:07:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2018-05-01 21:39:15 +00:00
|
|
|
func (t readWriteTransaction) updateGlobal(gk, folder, device []byte, file protocol.FileInfo, meta *metadataTracker) bool {
|
2017-11-22 08:05:27 +00:00
|
|
|
l.Debugf("update global; folder=%q device=%v file=%q version=%v invalid=%v", folder, protocol.DeviceIDFromBytes(device), file.Name, file.Version, file.Invalid)
|
2015-10-29 07:07:51 +00:00
|
|
|
name := []byte(file.Name)
|
2017-04-25 22:52:37 +00:00
|
|
|
svl, _ := t.Get(gk, nil) // skip error, we check len(svl) != 0 later
|
2015-10-29 07:07:51 +00:00
|
|
|
|
2016-05-31 19:29:26 +00:00
|
|
|
var fl VersionList
|
2015-10-29 07:07:51 +00:00
|
|
|
var oldFile protocol.FileInfo
|
|
|
|
var hasOldFile bool
|
|
|
|
// Remove the device from the current version list
|
2016-04-09 07:46:19 +00:00
|
|
|
if len(svl) != 0 {
|
2017-04-25 22:52:37 +00:00
|
|
|
fl.Unmarshal(svl) // skip error, range handles success case
|
2016-07-04 10:40:29 +00:00
|
|
|
for i := range fl.Versions {
|
|
|
|
if bytes.Equal(fl.Versions[i].Device, device) {
|
2017-11-11 19:18:17 +00:00
|
|
|
if fl.Versions[i].Version.Equal(file.Version) && fl.Versions[i].Invalid == file.Invalid {
|
2015-10-29 07:07:51 +00:00
|
|
|
// No need to do anything
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if i == 0 {
|
|
|
|
// Keep the current newest file around so we can subtract it from
|
2017-12-14 09:51:17 +00:00
|
|
|
// the metadata if we replace it.
|
2016-07-04 10:40:29 +00:00
|
|
|
oldFile, hasOldFile = t.getFile(folder, fl.Versions[0].Device, name)
|
2015-10-29 07:07:51 +00:00
|
|
|
}
|
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
fl.Versions = append(fl.Versions[:i], fl.Versions[i+1:]...)
|
2015-10-29 07:07:51 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
nv := FileVersion{
|
|
|
|
Device: device,
|
|
|
|
Version: file.Version,
|
2017-11-11 19:18:17 +00:00
|
|
|
Invalid: file.Invalid,
|
2015-10-29 07:07:51 +00:00
|
|
|
}
|
|
|
|
|
2017-11-11 19:18:17 +00:00
|
|
|
insertedAt := -1
|
2015-10-29 07:07:51 +00:00
|
|
|
// Find a position in the list to insert this file. The file at the front
|
|
|
|
// of the list is the newer, the "global".
|
2017-11-11 19:18:17 +00:00
|
|
|
insert:
|
2016-07-04 10:40:29 +00:00
|
|
|
for i := range fl.Versions {
|
|
|
|
switch fl.Versions[i].Version.Compare(file.Version) {
|
2017-11-11 19:18:17 +00:00
|
|
|
case protocol.Equal:
|
|
|
|
if nv.Invalid {
|
|
|
|
continue insert
|
|
|
|
}
|
|
|
|
fallthrough
|
|
|
|
|
|
|
|
case protocol.Lesser:
|
2015-10-29 07:07:51 +00:00
|
|
|
// The version at this point in the list is equal to or lesser
|
|
|
|
// ("older") than us. We insert ourselves in front of it.
|
2016-07-04 10:40:29 +00:00
|
|
|
fl.Versions = insertVersion(fl.Versions, i, nv)
|
2015-10-29 07:07:51 +00:00
|
|
|
insertedAt = i
|
2017-11-11 19:18:17 +00:00
|
|
|
break insert
|
2015-10-29 07:07:51 +00:00
|
|
|
|
|
|
|
case protocol.ConcurrentLesser, protocol.ConcurrentGreater:
|
|
|
|
// The version at this point is in conflict with us. We must pull
|
|
|
|
// the actual file metadata to determine who wins. If we win, we
|
|
|
|
// insert ourselves in front of the loser here. (The "Lesser" and
|
|
|
|
// "Greater" in the condition above is just based on the device
|
|
|
|
// IDs in the version vector, which is not the only thing we use
|
|
|
|
// to determine the winner.)
|
2017-04-25 22:52:37 +00:00
|
|
|
//
|
|
|
|
// A surprise missing file entry here is counted as a win for us.
|
2018-05-01 21:39:15 +00:00
|
|
|
if of, ok := t.getFile(folder, fl.Versions[i].Device, name); !ok || file.WinsConflict(of) {
|
2016-07-04 10:40:29 +00:00
|
|
|
fl.Versions = insertVersion(fl.Versions, i, nv)
|
2015-10-29 07:07:51 +00:00
|
|
|
insertedAt = i
|
2017-11-11 19:18:17 +00:00
|
|
|
break insert
|
2015-10-29 07:07:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-11 19:18:17 +00:00
|
|
|
if insertedAt == -1 {
|
|
|
|
// We didn't find a position for an insert above, so append to the end.
|
|
|
|
fl.Versions = append(fl.Versions, nv)
|
|
|
|
insertedAt = len(fl.Versions) - 1
|
|
|
|
}
|
2015-10-29 07:07:51 +00:00
|
|
|
|
|
|
|
if insertedAt == 0 {
|
|
|
|
// We just inserted a new newest version. Fixup the global size
|
|
|
|
// calculation.
|
2018-01-27 09:09:13 +00:00
|
|
|
if !file.Version.Equal(oldFile.Version) || file.Invalid != oldFile.Invalid {
|
2017-12-14 09:51:17 +00:00
|
|
|
meta.addFile(globalDeviceID, file)
|
2015-10-29 07:07:51 +00:00
|
|
|
if hasOldFile {
|
|
|
|
// We have the old file that was removed at the head of the list.
|
2017-12-14 09:51:17 +00:00
|
|
|
meta.removeFile(globalDeviceID, oldFile)
|
2016-07-04 10:40:29 +00:00
|
|
|
} else if len(fl.Versions) > 1 {
|
2015-10-29 07:07:51 +00:00
|
|
|
// The previous newest version is now at index 1, grab it from there.
|
2017-04-25 22:52:37 +00:00
|
|
|
if oldFile, ok := t.getFile(folder, fl.Versions[1].Device, name); ok {
|
|
|
|
// A failure to get the file here is surprising and our
|
|
|
|
// global size data will be incorrect until a restart...
|
2017-12-14 09:51:17 +00:00
|
|
|
meta.removeFile(globalDeviceID, oldFile)
|
2015-10-29 07:07:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
l.Debugf("new global after update: %v", fl)
|
2016-07-04 10:40:29 +00:00
|
|
|
t.Put(gk, mustMarshal(&fl))
|
2015-10-29 07:07:51 +00:00
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// removeFromGlobal removes the device from the global version list for the
|
|
|
|
// given file. If the version list is empty after this, the file entry is
|
|
|
|
// removed entirely.
|
2018-05-01 21:39:15 +00:00
|
|
|
func (t readWriteTransaction) removeFromGlobal(gk, folder, device, file []byte, meta *metadataTracker) {
|
2015-10-29 07:07:51 +00:00
|
|
|
l.Debugf("remove from global; folder=%q device=%v file=%q", folder, protocol.DeviceIDFromBytes(device), file)
|
|
|
|
|
|
|
|
svl, err := t.Get(gk, nil)
|
|
|
|
if err != nil {
|
|
|
|
// We might be called to "remove" a global version that doesn't exist
|
|
|
|
// if the first update for the file is already marked invalid.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-05-31 19:29:26 +00:00
|
|
|
var fl VersionList
|
2016-07-04 10:40:29 +00:00
|
|
|
err = fl.Unmarshal(svl)
|
2015-10-29 07:07:51 +00:00
|
|
|
if err != nil {
|
2017-04-25 22:52:37 +00:00
|
|
|
l.Debugln("unmarshal error:", err)
|
|
|
|
return
|
2015-10-29 07:07:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
removed := false
|
2016-07-04 10:40:29 +00:00
|
|
|
for i := range fl.Versions {
|
|
|
|
if bytes.Equal(fl.Versions[i].Device, device) {
|
2017-12-14 09:51:17 +00:00
|
|
|
if i == 0 && meta != nil {
|
2015-10-29 07:07:51 +00:00
|
|
|
f, ok := t.getFile(folder, device, file)
|
|
|
|
if !ok {
|
2017-04-25 22:52:37 +00:00
|
|
|
// didn't exist anyway, apparently
|
|
|
|
continue
|
2015-10-29 07:07:51 +00:00
|
|
|
}
|
2017-12-14 09:51:17 +00:00
|
|
|
meta.removeFile(globalDeviceID, f)
|
2015-10-29 07:07:51 +00:00
|
|
|
removed = true
|
|
|
|
}
|
2016-07-04 10:40:29 +00:00
|
|
|
fl.Versions = append(fl.Versions[:i], fl.Versions[i+1:]...)
|
2015-10-29 07:07:51 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
if len(fl.Versions) == 0 {
|
2015-10-29 07:07:51 +00:00
|
|
|
t.Delete(gk)
|
2017-11-11 19:18:17 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
l.Debugf("new global after remove: %v", fl)
|
|
|
|
t.Put(gk, mustMarshal(&fl))
|
|
|
|
if removed {
|
|
|
|
if f, ok := t.getFile(folder, fl.Versions[0].Device, file); ok {
|
|
|
|
// A failure to get the file here is surprising and our
|
|
|
|
// global size data will be incorrect until a restart...
|
2017-12-14 09:51:17 +00:00
|
|
|
meta.addFile(globalDeviceID, f)
|
2015-10-29 07:07:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
func insertVersion(vl []FileVersion, i int, v FileVersion) []FileVersion {
|
|
|
|
t := append(vl, FileVersion{})
|
2015-10-29 07:07:51 +00:00
|
|
|
copy(t[i+1:], t[i:])
|
|
|
|
t[i] = v
|
|
|
|
return t
|
|
|
|
}
|
2016-07-04 10:40:29 +00:00
|
|
|
|
|
|
|
type marshaller interface {
|
|
|
|
Marshal() ([]byte, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func mustMarshal(f marshaller) []byte {
|
|
|
|
bs, err := f.Marshal()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return bs
|
|
|
|
}
|