mirror of
https://github.com/octoleo/syncthing.git
synced 2024-11-09 23:00:58 +00:00
Refactor: the various db key functions should be instance methods
This commit is contained in:
parent
e6277d799f
commit
faf4267c73
@ -89,78 +89,8 @@ type dbReader interface {
|
|||||||
// Flush batches to disk when they contain this many records.
|
// Flush batches to disk when they contain this many records.
|
||||||
const batchFlushSize = 64
|
const batchFlushSize = 64
|
||||||
|
|
||||||
// deviceKey returns a byte slice encoding the following information:
|
func getFile(db dbReader, key []byte) (protocol.FileInfo, bool) {
|
||||||
// keyTypeDevice (1 byte)
|
bs, err := db.Get(key, nil)
|
||||||
// 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)
|
|
||||||
}
|
|
||||||
k[0] = KeyTypeDevice
|
|
||||||
if len(folder) > 64 {
|
|
||||||
panic("folder name too long")
|
|
||||||
}
|
|
||||||
copy(k[1:], []byte(folder))
|
|
||||||
copy(k[1+64:], device[:])
|
|
||||||
copy(k[1+64+32:], []byte(file))
|
|
||||||
return k[:reqLen]
|
|
||||||
}
|
|
||||||
|
|
||||||
func deviceKeyName(key []byte) []byte {
|
|
||||||
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]
|
|
||||||
}
|
|
||||||
|
|
||||||
func deviceKeyDevice(key []byte) []byte {
|
|
||||||
return key[1+64 : 1+64+32]
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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))
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
bs, err := db.Get(nk, nil)
|
|
||||||
if err == leveldb.ErrNotFound {
|
if err == leveldb.ErrNotFound {
|
||||||
return protocol.FileInfo{}, false
|
return protocol.FileInfo{}, false
|
||||||
}
|
}
|
||||||
|
81
lib/db/leveldb_convert.go
Normal file
81
lib/db/leveldb_convert.go
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
// Copyright (C) 2015 The Syncthing Authors.
|
||||||
|
//
|
||||||
|
// 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/.
|
||||||
|
|
||||||
|
package db
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
|
||||||
|
"github.com/syndtr/goleveldb/leveldb"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ConvertKeyFormat converts from the v0.11 to the v0.12 database format, to
|
||||||
|
// avoid having to do rescan. The change is in the key format for folder
|
||||||
|
// labels, so we basically just iterate over the database rewriting keys as
|
||||||
|
// necessary and then write out the folder ID mapping at the end.
|
||||||
|
func ConvertKeyFormat(from, to *leveldb.DB) error {
|
||||||
|
l.Infoln("Converting database key format")
|
||||||
|
files, globals, unchanged := 0, 0, 0
|
||||||
|
|
||||||
|
dbi := newDBInstance(to)
|
||||||
|
i := from.NewIterator(nil, nil)
|
||||||
|
for i.Next() {
|
||||||
|
key := i.Key()
|
||||||
|
switch key[0] {
|
||||||
|
case KeyTypeDevice:
|
||||||
|
newKey := dbi.deviceKey(oldDeviceKeyFolder(key), oldDeviceKeyDevice(key), oldDeviceKeyName(key))
|
||||||
|
if err := to.Put(newKey, i.Value(), nil); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
files++
|
||||||
|
case KeyTypeGlobal:
|
||||||
|
newKey := dbi.globalKey(oldGlobalKeyFolder(key), oldGlobalKeyName(key))
|
||||||
|
if err := to.Put(newKey, i.Value(), nil); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
globals++
|
||||||
|
default:
|
||||||
|
if err := to.Put(key, i.Value(), nil); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
unchanged++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
l.Infof("Converted %d files, %d globals (%d unchanged).", files, globals, unchanged)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func oldDeviceKeyFolder(key []byte) []byte {
|
||||||
|
folder := key[1 : 1+64]
|
||||||
|
izero := bytes.IndexByte(folder, 0)
|
||||||
|
if izero < 0 {
|
||||||
|
return folder
|
||||||
|
}
|
||||||
|
return folder[:izero]
|
||||||
|
}
|
||||||
|
|
||||||
|
func oldDeviceKeyDevice(key []byte) []byte {
|
||||||
|
return key[1+64 : 1+64+32]
|
||||||
|
}
|
||||||
|
|
||||||
|
func oldDeviceKeyName(key []byte) []byte {
|
||||||
|
return key[1+64+32:]
|
||||||
|
}
|
||||||
|
|
||||||
|
func oldGlobalKeyName(key []byte) []byte {
|
||||||
|
return key[1+64:]
|
||||||
|
}
|
||||||
|
|
||||||
|
func oldGlobalKeyFolder(key []byte) []byte {
|
||||||
|
folder := key[1 : 1+64]
|
||||||
|
izero := bytes.IndexByte(folder, 0)
|
||||||
|
if izero < 0 {
|
||||||
|
return folder
|
||||||
|
}
|
||||||
|
return folder[:izero]
|
||||||
|
}
|
@ -31,8 +31,8 @@ func newDBInstance(db *leveldb.DB) *dbInstance {
|
|||||||
func (db *dbInstance) genericReplace(folder, device []byte, fs []protocol.FileInfo, localSize, globalSize *sizeTracker, deleteFn deletionHandler) int64 {
|
func (db *dbInstance) genericReplace(folder, device []byte, fs []protocol.FileInfo, localSize, globalSize *sizeTracker, deleteFn deletionHandler) int64 {
|
||||||
sort.Sort(fileList(fs)) // sort list on name, same as in the database
|
sort.Sort(fileList(fs)) // sort list on name, same as in the database
|
||||||
|
|
||||||
start := deviceKey(folder, device, nil) // before all folder/device files
|
start := db.deviceKey(folder, device, nil) // before all folder/device files
|
||||||
limit := deviceKey(folder, device, []byte{0xff, 0xff, 0xff, 0xff}) // after all folder/device files
|
limit := db.deviceKey(folder, device, []byte{0xff, 0xff, 0xff, 0xff}) // after all folder/device files
|
||||||
|
|
||||||
t := db.newReadWriteTransaction()
|
t := db.newReadWriteTransaction()
|
||||||
defer t.close()
|
defer t.close()
|
||||||
@ -58,7 +58,7 @@ func (db *dbInstance) genericReplace(folder, device []byte, fs []protocol.FileIn
|
|||||||
}
|
}
|
||||||
|
|
||||||
if moreDb {
|
if moreDb {
|
||||||
oldName = deviceKeyName(dbi.Key())
|
oldName = db.deviceKeyName(dbi.Key())
|
||||||
}
|
}
|
||||||
|
|
||||||
cmp := bytes.Compare(newName, oldName)
|
cmp := bytes.Compare(newName, oldName)
|
||||||
@ -146,7 +146,7 @@ func (db *dbInstance) updateFiles(folder, device []byte, fs []protocol.FileInfo,
|
|||||||
isLocalDevice := bytes.Equal(device, protocol.LocalDeviceID[:])
|
isLocalDevice := bytes.Equal(device, protocol.LocalDeviceID[:])
|
||||||
for _, f := range fs {
|
for _, f := range fs {
|
||||||
name := []byte(f.Name)
|
name := []byte(f.Name)
|
||||||
fk = deviceKeyInto(fk[:cap(fk)], folder, device, name)
|
fk = db.deviceKeyInto(fk[:cap(fk)], folder, device, name)
|
||||||
bs, err := t.Get(fk, nil)
|
bs, err := t.Get(fk, nil)
|
||||||
if err == leveldb.ErrNotFound {
|
if err == leveldb.ErrNotFound {
|
||||||
if isLocalDevice {
|
if isLocalDevice {
|
||||||
@ -196,8 +196,8 @@ func (db *dbInstance) updateFiles(folder, device []byte, fs []protocol.FileInfo,
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (db *dbInstance) withHave(folder, device []byte, truncate bool, fn Iterator) {
|
func (db *dbInstance) withHave(folder, device []byte, truncate bool, fn Iterator) {
|
||||||
start := deviceKey(folder, device, nil) // before all folder/device files
|
start := db.deviceKey(folder, device, nil) // before all folder/device files
|
||||||
limit := deviceKey(folder, device, []byte{0xff, 0xff, 0xff, 0xff}) // after all folder/device files
|
limit := db.deviceKey(folder, device, []byte{0xff, 0xff, 0xff, 0xff}) // after all folder/device files
|
||||||
|
|
||||||
t := db.newReadOnlyTransaction()
|
t := db.newReadOnlyTransaction()
|
||||||
defer t.close()
|
defer t.close()
|
||||||
@ -217,8 +217,8 @@ func (db *dbInstance) withHave(folder, device []byte, truncate bool, fn Iterator
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (db *dbInstance) withAllFolderTruncated(folder []byte, fn func(device []byte, f FileInfoTruncated) bool) {
|
func (db *dbInstance) withAllFolderTruncated(folder []byte, fn func(device []byte, f FileInfoTruncated) bool) {
|
||||||
start := deviceKey(folder, nil, nil) // before all folder/device files
|
start := db.deviceKey(folder, nil, nil) // before all folder/device files
|
||||||
limit := deviceKey(folder, protocol.LocalDeviceID[:], []byte{0xff, 0xff, 0xff, 0xff}) // after all folder/device files
|
limit := db.deviceKey(folder, protocol.LocalDeviceID[:], []byte{0xff, 0xff, 0xff, 0xff}) // after all folder/device files
|
||||||
|
|
||||||
t := db.newReadWriteTransaction()
|
t := db.newReadWriteTransaction()
|
||||||
defer t.close()
|
defer t.close()
|
||||||
@ -227,7 +227,7 @@ func (db *dbInstance) withAllFolderTruncated(folder []byte, fn func(device []byt
|
|||||||
defer dbi.Release()
|
defer dbi.Release()
|
||||||
|
|
||||||
for dbi.Next() {
|
for dbi.Next() {
|
||||||
device := deviceKeyDevice(dbi.Key())
|
device := db.deviceKeyDevice(dbi.Key())
|
||||||
var f FileInfoTruncated
|
var f FileInfoTruncated
|
||||||
err := f.UnmarshalXDR(dbi.Value())
|
err := f.UnmarshalXDR(dbi.Value())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -250,11 +250,11 @@ func (db *dbInstance) withAllFolderTruncated(folder []byte, fn func(device []byt
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (db *dbInstance) getFile(folder, device, file []byte) (protocol.FileInfo, bool) {
|
func (db *dbInstance) getFile(folder, device, file []byte) (protocol.FileInfo, bool) {
|
||||||
return getFile(db, folder, device, file)
|
return getFile(db, db.deviceKey(folder, device, file))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *dbInstance) getGlobal(folder, file []byte, truncate bool) (FileIntf, bool) {
|
func (db *dbInstance) getGlobal(folder, file []byte, truncate bool) (FileIntf, bool) {
|
||||||
k := globalKey(folder, file)
|
k := db.globalKey(folder, file)
|
||||||
|
|
||||||
t := db.newReadOnlyTransaction()
|
t := db.newReadOnlyTransaction()
|
||||||
defer t.close()
|
defer t.close()
|
||||||
@ -277,7 +277,7 @@ func (db *dbInstance) getGlobal(folder, file []byte, truncate bool) (FileIntf, b
|
|||||||
panic("no versions?")
|
panic("no versions?")
|
||||||
}
|
}
|
||||||
|
|
||||||
k = deviceKey(folder, vl.versions[0].device, file)
|
k = db.deviceKey(folder, vl.versions[0].device, file)
|
||||||
bs, err = t.Get(k, nil)
|
bs, err = t.Get(k, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
@ -294,7 +294,7 @@ func (db *dbInstance) withGlobal(folder, prefix []byte, truncate bool, fn Iterat
|
|||||||
t := db.newReadOnlyTransaction()
|
t := db.newReadOnlyTransaction()
|
||||||
defer t.close()
|
defer t.close()
|
||||||
|
|
||||||
dbi := t.NewIterator(util.BytesPrefix(globalKey(folder, prefix)), nil)
|
dbi := t.NewIterator(util.BytesPrefix(db.globalKey(folder, prefix)), nil)
|
||||||
defer dbi.Release()
|
defer dbi.Release()
|
||||||
|
|
||||||
var fk []byte
|
var fk []byte
|
||||||
@ -308,8 +308,8 @@ func (db *dbInstance) withGlobal(folder, prefix []byte, truncate bool, fn Iterat
|
|||||||
l.Debugln(dbi.Key())
|
l.Debugln(dbi.Key())
|
||||||
panic("no versions?")
|
panic("no versions?")
|
||||||
}
|
}
|
||||||
name := globalKeyName(dbi.Key())
|
name := db.globalKeyName(dbi.Key())
|
||||||
fk = deviceKeyInto(fk[:cap(fk)], folder, vl.versions[0].device, name)
|
fk = db.deviceKeyInto(fk[:cap(fk)], folder, vl.versions[0].device, name)
|
||||||
bs, err := t.Get(fk, nil)
|
bs, err := t.Get(fk, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Debugf("folder: %q (%x)", folder, folder)
|
l.Debugf("folder: %q (%x)", folder, folder)
|
||||||
@ -334,7 +334,7 @@ func (db *dbInstance) withGlobal(folder, prefix []byte, truncate bool, fn Iterat
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (db *dbInstance) availability(folder, file []byte) []protocol.DeviceID {
|
func (db *dbInstance) availability(folder, file []byte) []protocol.DeviceID {
|
||||||
k := globalKey(folder, file)
|
k := db.globalKey(folder, file)
|
||||||
bs, err := db.Get(k, nil)
|
bs, err := db.Get(k, nil)
|
||||||
if err == leveldb.ErrNotFound {
|
if err == leveldb.ErrNotFound {
|
||||||
return nil
|
return nil
|
||||||
@ -362,8 +362,8 @@ func (db *dbInstance) availability(folder, file []byte) []protocol.DeviceID {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (db *dbInstance) withNeed(folder, device []byte, truncate bool, fn Iterator) {
|
func (db *dbInstance) withNeed(folder, device []byte, truncate bool, fn Iterator) {
|
||||||
start := globalKey(folder, nil)
|
start := db.globalKey(folder, nil)
|
||||||
limit := globalKey(folder, []byte{0xff, 0xff, 0xff, 0xff})
|
limit := db.globalKey(folder, []byte{0xff, 0xff, 0xff, 0xff})
|
||||||
|
|
||||||
t := db.newReadOnlyTransaction()
|
t := db.newReadOnlyTransaction()
|
||||||
defer t.close()
|
defer t.close()
|
||||||
@ -400,7 +400,7 @@ nextFile:
|
|||||||
}
|
}
|
||||||
|
|
||||||
if need || !have {
|
if need || !have {
|
||||||
name := globalKeyName(dbi.Key())
|
name := db.globalKeyName(dbi.Key())
|
||||||
needVersion := vl.versions[0].version
|
needVersion := vl.versions[0].version
|
||||||
|
|
||||||
nextVersion:
|
nextVersion:
|
||||||
@ -409,7 +409,7 @@ nextFile:
|
|||||||
// We haven't found a valid copy of the file with the needed version.
|
// We haven't found a valid copy of the file with the needed version.
|
||||||
continue nextFile
|
continue nextFile
|
||||||
}
|
}
|
||||||
fk = deviceKeyInto(fk[:cap(fk)], folder, vl.versions[i].device, name)
|
fk = db.deviceKeyInto(fk[:cap(fk)], folder, vl.versions[i].device, name)
|
||||||
bs, err := t.Get(fk, nil)
|
bs, err := t.Get(fk, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
var id protocol.DeviceID
|
var id protocol.DeviceID
|
||||||
@ -461,7 +461,7 @@ func (db *dbInstance) listFolders() []string {
|
|||||||
|
|
||||||
folderExists := make(map[string]bool)
|
folderExists := make(map[string]bool)
|
||||||
for dbi.Next() {
|
for dbi.Next() {
|
||||||
folder := string(globalKeyFolder(dbi.Key()))
|
folder := string(db.globalKeyFolder(dbi.Key()))
|
||||||
if !folderExists[folder] {
|
if !folderExists[folder] {
|
||||||
folderExists[folder] = true
|
folderExists[folder] = true
|
||||||
}
|
}
|
||||||
@ -483,7 +483,7 @@ func (db *dbInstance) dropFolder(folder []byte) {
|
|||||||
// Remove all items related to the given folder from the device->file bucket
|
// Remove all items related to the given folder from the device->file bucket
|
||||||
dbi := t.NewIterator(util.BytesPrefix([]byte{KeyTypeDevice}), nil)
|
dbi := t.NewIterator(util.BytesPrefix([]byte{KeyTypeDevice}), nil)
|
||||||
for dbi.Next() {
|
for dbi.Next() {
|
||||||
itemFolder := deviceKeyFolder(dbi.Key())
|
itemFolder := db.deviceKeyFolder(dbi.Key())
|
||||||
if bytes.Compare(folder, itemFolder) == 0 {
|
if bytes.Compare(folder, itemFolder) == 0 {
|
||||||
db.Delete(dbi.Key(), nil)
|
db.Delete(dbi.Key(), nil)
|
||||||
}
|
}
|
||||||
@ -493,7 +493,7 @@ func (db *dbInstance) dropFolder(folder []byte) {
|
|||||||
// Remove all items related to the given folder from the global bucket
|
// Remove all items related to the given folder from the global bucket
|
||||||
dbi = t.NewIterator(util.BytesPrefix([]byte{KeyTypeGlobal}), nil)
|
dbi = t.NewIterator(util.BytesPrefix([]byte{KeyTypeGlobal}), nil)
|
||||||
for dbi.Next() {
|
for dbi.Next() {
|
||||||
itemFolder := globalKeyFolder(dbi.Key())
|
itemFolder := db.globalKeyFolder(dbi.Key())
|
||||||
if bytes.Compare(folder, itemFolder) == 0 {
|
if bytes.Compare(folder, itemFolder) == 0 {
|
||||||
db.Delete(dbi.Key(), nil)
|
db.Delete(dbi.Key(), nil)
|
||||||
}
|
}
|
||||||
@ -505,8 +505,8 @@ func (db *dbInstance) checkGlobals(folder []byte, globalSize *sizeTracker) {
|
|||||||
t := db.newReadWriteTransaction()
|
t := db.newReadWriteTransaction()
|
||||||
defer t.close()
|
defer t.close()
|
||||||
|
|
||||||
start := globalKey(folder, nil)
|
start := db.globalKey(folder, nil)
|
||||||
limit := globalKey(folder, []byte{0xff, 0xff, 0xff, 0xff})
|
limit := db.globalKey(folder, []byte{0xff, 0xff, 0xff, 0xff})
|
||||||
dbi := t.NewIterator(&util.Range{Start: start, Limit: limit}, nil)
|
dbi := t.NewIterator(&util.Range{Start: start, Limit: limit}, nil)
|
||||||
defer dbi.Release()
|
defer dbi.Release()
|
||||||
|
|
||||||
@ -524,10 +524,10 @@ func (db *dbInstance) checkGlobals(folder []byte, globalSize *sizeTracker) {
|
|||||||
// there are global entries pointing to no longer existing files. Here
|
// there are global entries pointing to no longer existing files. Here
|
||||||
// we find those and clear them out.
|
// we find those and clear them out.
|
||||||
|
|
||||||
name := globalKeyName(gk)
|
name := db.globalKeyName(gk)
|
||||||
var newVL versionList
|
var newVL versionList
|
||||||
for i, version := range vl.versions {
|
for i, version := range vl.versions {
|
||||||
fk = deviceKeyInto(fk[:cap(fk)], folder, version.device, name)
|
fk = db.deviceKeyInto(fk[:cap(fk)], folder, version.device, name)
|
||||||
|
|
||||||
_, err := t.Get(fk, nil)
|
_, err := t.Get(fk, nil)
|
||||||
if err == leveldb.ErrNotFound {
|
if err == leveldb.ErrNotFound {
|
||||||
@ -555,6 +555,75 @@ func (db *dbInstance) checkGlobals(folder []byte, globalSize *sizeTracker) {
|
|||||||
l.Debugf("db check completed for %q", folder)
|
l.Debugf("db check completed for %q", folder)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// deviceKey returns a byte slice encoding the following information:
|
||||||
|
// keyTypeDevice (1 byte)
|
||||||
|
// folder (64 bytes)
|
||||||
|
// device (32 bytes)
|
||||||
|
// name (variable size)
|
||||||
|
func (db *dbInstance) deviceKey(folder, device, file []byte) []byte {
|
||||||
|
return db.deviceKeyInto(nil, folder, device, file)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *dbInstance) deviceKeyInto(k []byte, folder, device, file []byte) []byte {
|
||||||
|
reqLen := 1 + 64 + 32 + len(file)
|
||||||
|
if len(k) < reqLen {
|
||||||
|
k = make([]byte, reqLen)
|
||||||
|
}
|
||||||
|
k[0] = KeyTypeDevice
|
||||||
|
if len(folder) > 64 {
|
||||||
|
panic("folder name too long")
|
||||||
|
}
|
||||||
|
copy(k[1:], []byte(folder))
|
||||||
|
copy(k[1+64:], device[:])
|
||||||
|
copy(k[1+64+32:], []byte(file))
|
||||||
|
return k[:reqLen]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *dbInstance) deviceKeyName(key []byte) []byte {
|
||||||
|
return key[1+64+32:]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *dbInstance) deviceKeyFolder(key []byte) []byte {
|
||||||
|
folder := key[1 : 1+64]
|
||||||
|
izero := bytes.IndexByte(folder, 0)
|
||||||
|
if izero < 0 {
|
||||||
|
return folder
|
||||||
|
}
|
||||||
|
return folder[:izero]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *dbInstance) deviceKeyDevice(key []byte) []byte {
|
||||||
|
return key[1+64 : 1+64+32]
|
||||||
|
}
|
||||||
|
|
||||||
|
// globalKey returns a byte slice encoding the following information:
|
||||||
|
// keyTypeGlobal (1 byte)
|
||||||
|
// folder (64 bytes)
|
||||||
|
// name (variable size)
|
||||||
|
func (db *dbInstance) globalKey(folder, file []byte) []byte {
|
||||||
|
k := make([]byte, 1+64+len(file))
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *dbInstance) globalKeyName(key []byte) []byte {
|
||||||
|
return key[1+64:]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *dbInstance) globalKeyFolder(key []byte) []byte {
|
||||||
|
folder := key[1 : 1+64]
|
||||||
|
izero := bytes.IndexByte(folder, 0)
|
||||||
|
if izero < 0 {
|
||||||
|
return folder
|
||||||
|
}
|
||||||
|
return folder[:izero]
|
||||||
|
}
|
||||||
|
|
||||||
func unmarshalTrunc(bs []byte, truncate bool) (FileIntf, error) {
|
func unmarshalTrunc(bs []byte, truncate bool) (FileIntf, error) {
|
||||||
if truncate {
|
if truncate {
|
||||||
var tf FileInfoTruncated
|
var tf FileInfoTruncated
|
||||||
|
@ -16,17 +16,19 @@ func TestDeviceKey(t *testing.T) {
|
|||||||
dev := []byte("device67890123456789012345678901")
|
dev := []byte("device67890123456789012345678901")
|
||||||
name := []byte("name")
|
name := []byte("name")
|
||||||
|
|
||||||
key := deviceKey(fld, dev, name)
|
db := &dbInstance{}
|
||||||
|
|
||||||
fld2 := deviceKeyFolder(key)
|
key := db.deviceKey(fld, dev, name)
|
||||||
|
|
||||||
|
fld2 := db.deviceKeyFolder(key)
|
||||||
if bytes.Compare(fld2, fld) != 0 {
|
if bytes.Compare(fld2, fld) != 0 {
|
||||||
t.Errorf("wrong folder %q != %q", fld2, fld)
|
t.Errorf("wrong folder %q != %q", fld2, fld)
|
||||||
}
|
}
|
||||||
dev2 := deviceKeyDevice(key)
|
dev2 := db.deviceKeyDevice(key)
|
||||||
if bytes.Compare(dev2, dev) != 0 {
|
if bytes.Compare(dev2, dev) != 0 {
|
||||||
t.Errorf("wrong device %q != %q", dev2, dev)
|
t.Errorf("wrong device %q != %q", dev2, dev)
|
||||||
}
|
}
|
||||||
name2 := deviceKeyName(key)
|
name2 := db.deviceKeyName(key)
|
||||||
if bytes.Compare(name2, name) != 0 {
|
if bytes.Compare(name2, name) != 0 {
|
||||||
t.Errorf("wrong name %q != %q", name2, name)
|
t.Errorf("wrong name %q != %q", name2, name)
|
||||||
}
|
}
|
||||||
@ -36,13 +38,15 @@ func TestGlobalKey(t *testing.T) {
|
|||||||
fld := []byte("folder6789012345678901234567890123456789012345678901234567890123")
|
fld := []byte("folder6789012345678901234567890123456789012345678901234567890123")
|
||||||
name := []byte("name")
|
name := []byte("name")
|
||||||
|
|
||||||
key := globalKey(fld, name)
|
db := &dbInstance{}
|
||||||
|
|
||||||
fld2 := globalKeyFolder(key)
|
key := db.globalKey(fld, name)
|
||||||
|
|
||||||
|
fld2 := db.globalKeyFolder(key)
|
||||||
if bytes.Compare(fld2, fld) != 0 {
|
if bytes.Compare(fld2, fld) != 0 {
|
||||||
t.Errorf("wrong folder %q != %q", fld2, fld)
|
t.Errorf("wrong folder %q != %q", fld2, fld)
|
||||||
}
|
}
|
||||||
name2 := globalKeyName(key)
|
name2 := db.globalKeyName(key)
|
||||||
if bytes.Compare(name2, name) != 0 {
|
if bytes.Compare(name2, name) != 0 {
|
||||||
t.Errorf("wrong name %q != %q", name2, name)
|
t.Errorf("wrong name %q != %q", name2, name)
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,7 @@ import (
|
|||||||
// A readOnlyTransaction represents a database snapshot.
|
// A readOnlyTransaction represents a database snapshot.
|
||||||
type readOnlyTransaction struct {
|
type readOnlyTransaction struct {
|
||||||
*leveldb.Snapshot
|
*leveldb.Snapshot
|
||||||
db *leveldb.DB
|
db *dbInstance
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *dbInstance) newReadOnlyTransaction() readOnlyTransaction {
|
func (db *dbInstance) newReadOnlyTransaction() readOnlyTransaction {
|
||||||
@ -26,7 +26,7 @@ func (db *dbInstance) newReadOnlyTransaction() readOnlyTransaction {
|
|||||||
}
|
}
|
||||||
return readOnlyTransaction{
|
return readOnlyTransaction{
|
||||||
Snapshot: snap,
|
Snapshot: snap,
|
||||||
db: db.DB,
|
db: db,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,7 +35,7 @@ func (t readOnlyTransaction) close() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t readOnlyTransaction) getFile(folder, device, file []byte) (protocol.FileInfo, bool) {
|
func (t readOnlyTransaction) getFile(folder, device, file []byte) (protocol.FileInfo, bool) {
|
||||||
return getFile(t, folder, device, file)
|
return getFile(t, t.db.deviceKey(folder, device, file))
|
||||||
}
|
}
|
||||||
|
|
||||||
// A readWriteTransaction is a readOnlyTransaction plus a batch for writes.
|
// A readWriteTransaction is a readOnlyTransaction plus a batch for writes.
|
||||||
@ -78,7 +78,7 @@ func (t readWriteTransaction) insertFile(folder, device []byte, file protocol.Fi
|
|||||||
}
|
}
|
||||||
|
|
||||||
name := []byte(file.Name)
|
name := []byte(file.Name)
|
||||||
nk := deviceKey(folder, device, name)
|
nk := t.db.deviceKey(folder, device, name)
|
||||||
t.Put(nk, file.MustMarshalXDR())
|
t.Put(nk, file.MustMarshalXDR())
|
||||||
|
|
||||||
return file.LocalVersion
|
return file.LocalVersion
|
||||||
@ -90,7 +90,7 @@ func (t readWriteTransaction) insertFile(folder, device []byte, file protocol.Fi
|
|||||||
func (t readWriteTransaction) updateGlobal(folder, device []byte, file protocol.FileInfo, globalSize *sizeTracker) bool {
|
func (t readWriteTransaction) updateGlobal(folder, device []byte, file protocol.FileInfo, globalSize *sizeTracker) bool {
|
||||||
l.Debugf("update global; folder=%q device=%v file=%q version=%d", folder, protocol.DeviceIDFromBytes(device), file.Name, file.Version)
|
l.Debugf("update global; folder=%q device=%v file=%q version=%d", folder, protocol.DeviceIDFromBytes(device), file.Name, file.Version)
|
||||||
name := []byte(file.Name)
|
name := []byte(file.Name)
|
||||||
gk := globalKey(folder, name)
|
gk := t.db.globalKey(folder, name)
|
||||||
svl, err := t.Get(gk, nil)
|
svl, err := t.Get(gk, nil)
|
||||||
if err != nil && err != leveldb.ErrNotFound {
|
if err != nil && err != leveldb.ErrNotFound {
|
||||||
panic(err)
|
panic(err)
|
||||||
@ -197,7 +197,7 @@ done:
|
|||||||
func (t readWriteTransaction) removeFromGlobal(folder, device, file []byte, globalSize *sizeTracker) {
|
func (t readWriteTransaction) removeFromGlobal(folder, device, file []byte, globalSize *sizeTracker) {
|
||||||
l.Debugf("remove from global; folder=%q device=%v file=%q", folder, protocol.DeviceIDFromBytes(device), file)
|
l.Debugf("remove from global; folder=%q device=%v file=%q", folder, protocol.DeviceIDFromBytes(device), file)
|
||||||
|
|
||||||
gk := globalKey(folder, file)
|
gk := t.db.globalKey(folder, file)
|
||||||
svl, err := t.Get(gk, nil)
|
svl, err := t.Get(gk, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// We might be called to "remove" a global version that doesn't exist
|
// We might be called to "remove" a global version that doesn't exist
|
||||||
|
Loading…
Reference in New Issue
Block a user