2014-11-16 20:13:20 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 19:43:32 +00:00
|
|
|
//
|
2015-03-07 20:36:35 +00:00
|
|
|
// 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/.
|
2014-09-04 06:31:38 +00:00
|
|
|
|
2015-01-12 13:50:30 +00:00
|
|
|
package db
|
2014-07-06 12:46:48 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2014-10-09 08:44:18 +00:00
|
|
|
"fmt"
|
2014-07-06 12:46:48 +00:00
|
|
|
|
2015-09-22 17:38:46 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
2014-07-06 12:46:48 +00:00
|
|
|
"github.com/syndtr/goleveldb/leveldb"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb/opt"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-01-17 19:53:33 +00:00
|
|
|
KeyTypeDevice = iota
|
|
|
|
KeyTypeGlobal
|
|
|
|
KeyTypeBlock
|
|
|
|
KeyTypeDeviceStatistic
|
|
|
|
KeyTypeFolderStatistic
|
2015-05-13 14:57:29 +00:00
|
|
|
KeyTypeVirtualMtime
|
2016-01-03 18:08:19 +00:00
|
|
|
KeyTypeFolderIdx
|
|
|
|
KeyTypeDeviceIdx
|
2016-07-23 12:46:31 +00:00
|
|
|
KeyTypeIndexID
|
2017-12-14 09:51:17 +00:00
|
|
|
KeyTypeFolderMeta
|
2018-02-25 09:14:17 +00:00
|
|
|
KeyTypeMiscData
|
2014-07-06 12:46:48 +00:00
|
|
|
)
|
|
|
|
|
2016-05-31 19:29:26 +00:00
|
|
|
func (l VersionList) String() string {
|
2014-10-09 08:44:18 +00:00
|
|
|
var b bytes.Buffer
|
|
|
|
var id protocol.DeviceID
|
|
|
|
b.WriteString("{")
|
2016-07-04 10:40:29 +00:00
|
|
|
for i, v := range l.Versions {
|
2014-10-09 08:44:18 +00:00
|
|
|
if i > 0 {
|
|
|
|
b.WriteString(", ")
|
|
|
|
}
|
2016-07-04 10:40:29 +00:00
|
|
|
copy(id[:], v.Device)
|
2017-11-22 08:05:27 +00:00
|
|
|
fmt.Fprintf(&b, "{%v, %v}", v.Version, id)
|
2014-10-09 08:44:18 +00:00
|
|
|
}
|
|
|
|
b.WriteString("}")
|
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
2014-07-12 21:06:48 +00:00
|
|
|
type fileList []protocol.FileInfo
|
2014-07-06 12:46:48 +00:00
|
|
|
|
|
|
|
func (l fileList) Len() int {
|
|
|
|
return len(l)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l fileList) Swap(a, b int) {
|
|
|
|
l[a], l[b] = l[b], l[a]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l fileList) Less(a, b int) bool {
|
|
|
|
return l[a].Name < l[b].Name
|
|
|
|
}
|
|
|
|
|
|
|
|
type dbReader interface {
|
|
|
|
Get([]byte, *opt.ReadOptions) ([]byte, error)
|
|
|
|
}
|
|
|
|
|
2015-02-10 19:12:17 +00:00
|
|
|
// Flush batches to disk when they contain this many records.
|
|
|
|
const batchFlushSize = 64
|
|
|
|
|
2015-10-31 06:20:35 +00:00
|
|
|
func getFile(db dbReader, key []byte) (protocol.FileInfo, bool) {
|
|
|
|
bs, err := db.Get(key, nil)
|
2014-07-06 12:46:48 +00:00
|
|
|
if err == leveldb.ErrNotFound {
|
2015-01-06 21:12:45 +00:00
|
|
|
return protocol.FileInfo{}, false
|
2014-07-06 12:46:48 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
2017-04-25 22:52:37 +00:00
|
|
|
l.Debugln("surprise error:", err)
|
|
|
|
return protocol.FileInfo{}, false
|
2014-07-06 12:46:48 +00:00
|
|
|
}
|
|
|
|
|
2014-07-12 21:06:48 +00:00
|
|
|
var f protocol.FileInfo
|
2016-07-04 10:40:29 +00:00
|
|
|
err = f.Unmarshal(bs)
|
2014-07-06 12:46:48 +00:00
|
|
|
if err != nil {
|
2017-04-25 22:52:37 +00:00
|
|
|
l.Debugln("unmarshal error:", err)
|
|
|
|
return protocol.FileInfo{}, false
|
2014-07-06 12:46:48 +00:00
|
|
|
}
|
2015-01-06 21:12:45 +00:00
|
|
|
return f, true
|
2014-07-06 12:46:48 +00:00
|
|
|
}
|