syncthing/lib/db/leveldb.go

178 lines
3.6 KiB
Go
Raw Normal View History

2014-11-16 21:13:20 +01:00
// Copyright (C) 2014 The Syncthing Authors.
2014-09-29 21:43:32 +02:00
//
2015-03-07 21:36:35 +01: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-04 08:31:38 +02: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
package db
2014-07-06 14:46:48 +02:00
import (
"bytes"
"fmt"
2014-07-06 14:46:48 +02:00
2015-09-22 19:38:46 +02:00
"github.com/syncthing/syncthing/lib/protocol"
2015-08-06 11:29:25 +02:00
"github.com/syncthing/syncthing/lib/sync"
2014-07-06 14:46:48 +02:00
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/opt"
)
var (
clockTick int64
2015-04-28 22:32:10 +02:00
clockMut = sync.NewMutex()
)
func clock(v int64) int64 {
clockMut.Lock()
defer clockMut.Unlock()
if v > clockTick {
clockTick = v + 1
} else {
clockTick++
}
return clockTick
}
2014-07-06 14:46:48 +02:00
const (
2015-01-17 20:53:33 +01:00
KeyTypeDevice = iota
KeyTypeGlobal
KeyTypeBlock
KeyTypeDeviceStatistic
KeyTypeFolderStatistic
KeyTypeVirtualMtime
2014-07-06 14:46:48 +02:00
)
type fileVersion struct {
2015-03-25 22:37:35 +01:00
version protocol.Vector
2014-09-28 13:05:25 +02:00
device []byte
2014-07-06 14:46:48 +02:00
}
type versionList struct {
versions []fileVersion
}
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 23:06:48 +02:00
type fileList []protocol.FileInfo
2014-07-06 14:46:48 +02: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)
}
// Flush batches to disk when they contain this many records.
const batchFlushSize = 64
// deviceKey returns a byte slice encoding the following information:
// keyTypeDevice (1 byte)
// folder (64 bytes)
// device (32 bytes)
// name (variable size)
func deviceKey(folder, device, file []byte) []byte {
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 20:53:33 +01:00
k[0] = KeyTypeDevice
if len(folder) > 64 {
panic("folder name too long")
}
copy(k[1:], []byte(folder))
copy(k[1+64:], device[:])
2014-07-06 14:46:48 +02:00
copy(k[1+64+32:], []byte(file))
return k[:reqLen]
2014-07-06 14:46:48 +02:00
}
func deviceKeyName(key []byte) []byte {
2014-07-06 14:46:48 +02:00
return key[1+64+32:]
}
func deviceKeyFolder(key []byte) []byte {
folder := key[1 : 1+64]
izero := bytes.IndexByte(folder, 0)
if izero < 0 {
return folder
}
return folder[:izero]
2014-07-17 11:00:54 +02:00
}
func deviceKeyDevice(key []byte) []byte {
2014-07-17 11:00:54 +02:00
return key[1+64 : 1+64+32]
}
2014-07-06 14:46:48 +02: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 20:53:33 +01:00
k[0] = KeyTypeGlobal
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 14:46:48 +02:00
func globalKeyName(key []byte) []byte {
return key[1+64:]
}
func globalKeyFolder(key []byte) []byte {
folder := key[1 : 1+64]
izero := bytes.IndexByte(folder, 0)
if izero < 0 {
return folder
}
return folder[:izero]
}
func getFile(db dbReader, folder, device, file []byte) (protocol.FileInfo, bool) {
nk := deviceKey(folder, device, file)
2014-07-06 14:46:48 +02:00
bs, err := db.Get(nk, nil)
if err == leveldb.ErrNotFound {
return protocol.FileInfo{}, false
2014-07-06 14:46:48 +02:00
}
if err != nil {
panic(err)
}
2014-07-12 23:06:48 +02:00
var f protocol.FileInfo
2014-07-06 14:46:48 +02:00
err = f.UnmarshalXDR(bs)
if err != nil {
panic(err)
}
return f, true
2014-07-06 14:46:48 +02:00
}