syncthing/lib/db/leveldb.go

110 lines
2.0 KiB
Go
Raw Normal View History

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,
// You can obtain one at http://mozilla.org/MPL/2.0/.
2014-09-04 06:31:38 +00:00
//go:generate -command genxdr go run ../../vendor/github.com/calmh/xdr/cmd/genxdr/main.go
//go:generate genxdr -o leveldb_xdr.go leveldb.go
package db
2014-07-06 12:46:48 +00:00
import (
"bytes"
"fmt"
2014-07-06 12:46:48 +00:00
2015-09-22 17:38:46 +00:00
"github.com/syncthing/syncthing/lib/protocol"
2015-08-06 09:29:25 +00:00
"github.com/syncthing/syncthing/lib/sync"
2014-07-06 12:46:48 +00:00
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/opt"
)
var (
clockTick int64
2015-04-28 20:32:10 +00: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 12:46:48 +00:00
const (
2015-01-17 19:53:33 +00:00
KeyTypeDevice = iota
KeyTypeGlobal
KeyTypeBlock
KeyTypeDeviceStatistic
KeyTypeFolderStatistic
KeyTypeVirtualMtime
KeyTypeFolderIdx
KeyTypeDeviceIdx
2014-07-06 12:46:48 +00:00
)
type fileVersion struct {
2015-03-25 21:37:35 +00:00
version protocol.Vector
2014-09-28 11:05:25 +00:00
device []byte
2014-07-06 12:46:48 +00: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 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)
}
// Flush batches to disk when they contain this many records.
const batchFlushSize = 64
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 {
return protocol.FileInfo{}, false
2014-07-06 12:46:48 +00:00
}
if err != nil {
panic(err)
}
2014-07-12 21:06:48 +00:00
var f protocol.FileInfo
2014-07-06 12:46:48 +00:00
err = f.UnmarshalXDR(bs)
if err != nil {
panic(err)
}
return f, true
2014-07-06 12:46:48 +00:00
}