syncthing/lib/db/leveldb.go

108 lines
2.0 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
func getFile(db dbReader, key []byte) (protocol.FileInfo, bool) {
bs, err := db.Get(key, nil)
2014-07-06 14:46:48 +02:00
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
}