syncthing/lib/db/leveldb.go

82 lines
1.5 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 https://mozilla.org/MPL/2.0/.
2014-09-04 08:31:38 +02:00
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"
2014-07-06 14:46:48 +02:00
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/opt"
)
const (
2015-01-17 20:53:33 +01:00
KeyTypeDevice = iota
KeyTypeGlobal
KeyTypeBlock
KeyTypeDeviceStatistic
KeyTypeFolderStatistic
KeyTypeVirtualMtime
KeyTypeFolderIdx
KeyTypeDeviceIdx
KeyTypeIndexID
2014-07-06 14:46:48 +02: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 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
err = f.Unmarshal(bs)
2014-07-06 14:46:48 +02:00
if err != nil {
panic(err)
}
return f, true
2014-07-06 14:46:48 +02:00
}