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
|
|
|
|
2014-12-06 06:23:10 -07:00
|
|
|
//go:generate -command genxdr go run ../../Godeps/_workspace/src/github.com/calmh/xdr/cmd/genxdr/main.go
|
|
|
|
//go:generate genxdr -o leveldb_xdr.go leveldb.go
|
|
|
|
|
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
|
2014-07-06 05:46:48 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type fileVersion struct {
|
2015-03-25 14:37:35 -07:00
|
|
|
version protocol.Vector
|
2014-09-28 04:05:25 -07:00
|
|
|
device []byte
|
2014-07-06 05:46:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type versionList struct {
|
|
|
|
versions []fileVersion
|
|
|
|
}
|
|
|
|
|
2014-10-09 01:44:18 -07:00
|
|
|
func (l versionList) String() string {
|
|
|
|
var b bytes.Buffer
|
|
|
|
var id protocol.DeviceID
|
|
|
|
b.WriteString("{")
|
|
|
|
for i, v := range l.versions {
|
|
|
|
if i > 0 {
|
|
|
|
b.WriteString(", ")
|
|
|
|
}
|
|
|
|
copy(id[:], v.device)
|
|
|
|
fmt.Fprintf(&b, "{%d, %v}", v.version, id)
|
|
|
|
}
|
|
|
|
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
|
|
|
|
|
2014-10-07 06:05:04 -07:00
|
|
|
// deviceKey returns a byte slice encoding the following information:
|
|
|
|
// keyTypeDevice (1 byte)
|
|
|
|
// folder (64 bytes)
|
|
|
|
// device (32 bytes)
|
|
|
|
// name (variable size)
|
2014-09-28 04:00:38 -07:00
|
|
|
func deviceKey(folder, device, file []byte) []byte {
|
2015-05-24 04:08:10 -07:00
|
|
|
return deviceKeyInto(nil, folder, device, file)
|
|
|
|
}
|
|
|
|
|
|
|
|
func deviceKeyInto(k []byte, folder, device, file []byte) []byte {
|
|
|
|
reqLen := 1 + 64 + 32 + len(file)
|
|
|
|
if len(k) < reqLen {
|
|
|
|
k = make([]byte, reqLen)
|
|
|
|
}
|
2015-01-17 12:53:33 -07:00
|
|
|
k[0] = KeyTypeDevice
|
2014-10-07 06:05:04 -07:00
|
|
|
if len(folder) > 64 {
|
|
|
|
panic("folder name too long")
|
|
|
|
}
|
2014-09-28 04:00:38 -07:00
|
|
|
copy(k[1:], []byte(folder))
|
|
|
|
copy(k[1+64:], device[:])
|
2014-07-06 05:46:48 -07:00
|
|
|
copy(k[1+64+32:], []byte(file))
|
2015-05-24 04:08:10 -07:00
|
|
|
return k[:reqLen]
|
2014-07-06 05:46:48 -07:00
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func deviceKeyName(key []byte) []byte {
|
2014-07-06 05:46:48 -07:00
|
|
|
return key[1+64+32:]
|
|
|
|
}
|
2014-10-07 06:05:04 -07:00
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func deviceKeyFolder(key []byte) []byte {
|
|
|
|
folder := key[1 : 1+64]
|
|
|
|
izero := bytes.IndexByte(folder, 0)
|
2014-10-07 06:05:04 -07:00
|
|
|
if izero < 0 {
|
|
|
|
return folder
|
|
|
|
}
|
2014-09-28 04:00:38 -07:00
|
|
|
return folder[:izero]
|
2014-07-17 02:00:54 -07:00
|
|
|
}
|
2014-10-07 06:05:04 -07:00
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func deviceKeyDevice(key []byte) []byte {
|
2014-07-17 02:00:54 -07:00
|
|
|
return key[1+64 : 1+64+32]
|
|
|
|
}
|
2014-07-06 05:46:48 -07:00
|
|
|
|
2014-10-07 06:05:04 -07:00
|
|
|
// globalKey returns a byte slice encoding the following information:
|
|
|
|
// keyTypeGlobal (1 byte)
|
|
|
|
// folder (64 bytes)
|
|
|
|
// name (variable size)
|
|
|
|
func globalKey(folder, file []byte) []byte {
|
|
|
|
k := make([]byte, 1+64+len(file))
|
2015-01-17 12:53:33 -07:00
|
|
|
k[0] = KeyTypeGlobal
|
2014-10-07 06:05:04 -07:00
|
|
|
if len(folder) > 64 {
|
|
|
|
panic("folder name too long")
|
|
|
|
}
|
|
|
|
copy(k[1:], []byte(folder))
|
|
|
|
copy(k[1+64:], []byte(file))
|
|
|
|
return k
|
|
|
|
}
|
|
|
|
|
2014-07-06 05:46:48 -07:00
|
|
|
func globalKeyName(key []byte) []byte {
|
|
|
|
return key[1+64:]
|
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func globalKeyFolder(key []byte) []byte {
|
|
|
|
folder := key[1 : 1+64]
|
|
|
|
izero := bytes.IndexByte(folder, 0)
|
2014-10-07 06:05:04 -07:00
|
|
|
if izero < 0 {
|
|
|
|
return folder
|
|
|
|
}
|
2014-09-28 04:00:38 -07:00
|
|
|
return folder[:izero]
|
2014-08-31 04:34:17 -07:00
|
|
|
}
|
|
|
|
|
2015-10-29 00:07:51 -07:00
|
|
|
func getFile(db dbReader, folder, device, file []byte) (protocol.FileInfo, bool) {
|
2014-09-28 04:00:38 -07:00
|
|
|
nk := deviceKey(folder, device, file)
|
2014-07-06 05:46:48 -07:00
|
|
|
bs, err := db.Get(nk, nil)
|
|
|
|
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
|
2014-07-06 05:46:48 -07:00
|
|
|
err = f.UnmarshalXDR(bs)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2015-01-06 14:12:45 -07:00
|
|
|
return f, true
|
2014-07-06 05:46:48 -07:00
|
|
|
}
|