2014-11-16 13:13:20 -07:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 12:43:32 -07:00
|
|
|
//
|
2015-03-07 13:36:35 -07: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,
|
|
|
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
2014-09-03 23:31:38 -07:00
|
|
|
|
2015-01-12 06:50:30 -07:00
|
|
|
package db
|
2014-07-06 05:46:48 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2014-10-09 01:44:18 -07:00
|
|
|
"fmt"
|
2014-07-06 05:46:48 -07:00
|
|
|
|
2015-09-22 10:38:46 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
2015-08-06 02:29:25 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/sync"
|
2014-07-06 05:46:48 -07:00
|
|
|
"github.com/syndtr/goleveldb/leveldb"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb/opt"
|
|
|
|
)
|
|
|
|
|
2014-07-15 04:04:37 -07:00
|
|
|
var (
|
2015-01-17 18:12:06 -07:00
|
|
|
clockTick int64
|
2015-04-28 13:32:10 -07:00
|
|
|
clockMut = sync.NewMutex()
|
2014-07-15 04:04:37 -07:00
|
|
|
)
|
|
|
|
|
2015-01-17 18:12:06 -07:00
|
|
|
func clock(v int64) int64 {
|
2014-07-15 04:04:37 -07:00
|
|
|
clockMut.Lock()
|
|
|
|
defer clockMut.Unlock()
|
|
|
|
if v > clockTick {
|
|
|
|
clockTick = v + 1
|
|
|
|
} else {
|
|
|
|
clockTick++
|
|
|
|
}
|
|
|
|
return clockTick
|
|
|
|
}
|
|
|
|
|
2014-07-06 05:46:48 -07:00
|
|
|
const (
|
2015-01-17 12:53:33 -07:00
|
|
|
KeyTypeDevice = iota
|
|
|
|
KeyTypeGlobal
|
|
|
|
KeyTypeBlock
|
|
|
|
KeyTypeDeviceStatistic
|
|
|
|
KeyTypeFolderStatistic
|
2015-05-13 07:57:29 -07:00
|
|
|
KeyTypeVirtualMtime
|
2016-01-03 11:08:19 -07:00
|
|
|
KeyTypeFolderIdx
|
|
|
|
KeyTypeDeviceIdx
|
2014-07-06 05:46:48 -07:00
|
|
|
)
|
|
|
|
|
2016-05-31 12:29:26 -07:00
|
|
|
func (l VersionList) String() string {
|
2014-10-09 01:44:18 -07:00
|
|
|
var b bytes.Buffer
|
|
|
|
var id protocol.DeviceID
|
|
|
|
b.WriteString("{")
|
2016-07-04 03:40:29 -07:00
|
|
|
for i, v := range l.Versions {
|
2014-10-09 01:44:18 -07:00
|
|
|
if i > 0 {
|
|
|
|
b.WriteString(", ")
|
|
|
|
}
|
2016-07-04 03:40:29 -07:00
|
|
|
copy(id[:], v.Device)
|
|
|
|
fmt.Fprintf(&b, "{%d, %v}", v.Version, id)
|
2014-10-09 01:44:18 -07:00
|
|
|
}
|
|
|
|
b.WriteString("}")
|
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
2014-07-12 14:06:48 -07:00
|
|
|
type fileList []protocol.FileInfo
|
2014-07-06 05:46:48 -07: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 12:12:17 -07:00
|
|
|
// Flush batches to disk when they contain this many records.
|
|
|
|
const batchFlushSize = 64
|
|
|
|
|
2015-10-30 23:20:35 -07:00
|
|
|
func getFile(db dbReader, key []byte) (protocol.FileInfo, bool) {
|
|
|
|
bs, err := db.Get(key, nil)
|
2014-07-06 05:46:48 -07:00
|
|
|
if err == leveldb.ErrNotFound {
|
2015-01-06 14:12:45 -07:00
|
|
|
return protocol.FileInfo{}, false
|
2014-07-06 05:46:48 -07:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2014-07-12 14:06:48 -07:00
|
|
|
var f protocol.FileInfo
|
2016-07-04 03:40:29 -07:00
|
|
|
err = f.Unmarshal(bs)
|
2014-07-06 05:46:48 -07:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2015-01-06 14:12:45 -07:00
|
|
|
return f, true
|
2014-07-06 05:46:48 -07:00
|
|
|
}
|