syncthing/lib/db/leveldb.go

98 lines
1.8 KiB
Go
Raw Normal View History

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
package db
2014-07-06 05:46:48 -07:00
import (
"bytes"
"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"
)
var (
clockTick int64
2015-04-28 13:32:10 -07: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 05:46:48 -07:00
const (
2015-01-17 12:53:33 -07:00
KeyTypeDevice = iota
KeyTypeGlobal
KeyTypeBlock
KeyTypeDeviceStatistic
KeyTypeFolderStatistic
KeyTypeVirtualMtime
KeyTypeFolderIdx
KeyTypeDeviceIdx
2014-07-06 05:46:48 -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)
}
// 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 05:46:48 -07:00
if err == leveldb.ErrNotFound {
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
err = f.Unmarshal(bs)
2014-07-06 05:46:48 -07:00
if err != nil {
panic(err)
}
return f, true
2014-07-06 05:46:48 -07:00
}