2014-11-16 13:13:20 -07:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-10-06 13:57:33 -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,
|
2017-02-08 23:52:18 -07:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2014-10-06 13:57:33 -07:00
|
|
|
|
2015-01-12 06:50:30 -07:00
|
|
|
package db
|
2014-10-06 13:57:33 -07:00
|
|
|
|
|
|
|
import (
|
2019-01-23 02:22:33 -07:00
|
|
|
"encoding/binary"
|
2014-10-06 13:57:33 -07:00
|
|
|
"testing"
|
|
|
|
|
2015-09-22 10:38:46 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
2016-01-03 11:08:19 -07:00
|
|
|
"github.com/syndtr/goleveldb/leveldb/util"
|
2014-10-06 13:57:33 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func genBlocks(n int) []protocol.BlockInfo {
|
|
|
|
b := make([]protocol.BlockInfo, n)
|
|
|
|
for i := range b {
|
|
|
|
h := make([]byte, 32)
|
|
|
|
for j := range h {
|
|
|
|
h[j] = byte(i + j)
|
|
|
|
}
|
2015-01-17 18:12:06 -07:00
|
|
|
b[i].Size = int32(i)
|
2014-10-06 13:57:33 -07:00
|
|
|
b[i].Hash = h
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
var f1, f2, f3 protocol.FileInfo
|
2015-09-04 03:01:00 -07:00
|
|
|
var folders = []string{"folder1", "folder2"}
|
2014-10-06 13:57:33 -07:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
blocks := genBlocks(30)
|
|
|
|
|
|
|
|
f1 = protocol.FileInfo{
|
|
|
|
Name: "f1",
|
|
|
|
Blocks: blocks[:10],
|
|
|
|
}
|
|
|
|
|
|
|
|
f2 = protocol.FileInfo{
|
|
|
|
Name: "f2",
|
|
|
|
Blocks: blocks[10:20],
|
|
|
|
}
|
|
|
|
|
|
|
|
f3 = protocol.FileInfo{
|
|
|
|
Name: "f3",
|
|
|
|
Blocks: blocks[20:],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-23 02:22:33 -07:00
|
|
|
func setup() (*instance, *BlockFinder) {
|
2014-10-06 13:57:33 -07:00
|
|
|
// Setup
|
|
|
|
|
2015-10-31 04:31:25 -07:00
|
|
|
db := OpenMemory()
|
2019-01-23 02:22:33 -07:00
|
|
|
return newInstance(db), NewBlockFinder(db)
|
2014-10-06 13:57:33 -07:00
|
|
|
}
|
|
|
|
|
2019-01-23 02:22:33 -07:00
|
|
|
func dbEmpty(db *instance) bool {
|
2016-01-03 11:08:19 -07:00
|
|
|
iter := db.NewIterator(util.BytesPrefix([]byte{KeyTypeBlock}), nil)
|
2014-10-06 13:57:33 -07:00
|
|
|
defer iter.Release()
|
2016-12-18 11:57:41 -07:00
|
|
|
return !iter.Next()
|
2014-10-06 13:57:33 -07:00
|
|
|
}
|
|
|
|
|
2019-01-23 02:22:33 -07:00
|
|
|
func addToBlockMap(db *instance, folder []byte, fs []protocol.FileInfo) {
|
|
|
|
t := db.newReadWriteTransaction()
|
|
|
|
defer t.close()
|
|
|
|
|
|
|
|
var keyBuf []byte
|
|
|
|
blockBuf := make([]byte, 4)
|
|
|
|
for _, f := range fs {
|
|
|
|
if !f.IsDirectory() && !f.IsDeleted() && !f.IsInvalid() {
|
|
|
|
name := []byte(f.Name)
|
|
|
|
for i, block := range f.Blocks {
|
|
|
|
binary.BigEndian.PutUint32(blockBuf, uint32(i))
|
2019-02-14 16:15:13 -07:00
|
|
|
keyBuf = t.keyer.GenerateBlockMapKey(keyBuf, folder, block.Hash, name)
|
2019-01-23 02:22:33 -07:00
|
|
|
t.Put(keyBuf, blockBuf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func discardFromBlockMap(db *instance, folder []byte, fs []protocol.FileInfo) {
|
|
|
|
t := db.newReadWriteTransaction()
|
|
|
|
defer t.close()
|
|
|
|
|
|
|
|
var keyBuf []byte
|
|
|
|
for _, ef := range fs {
|
|
|
|
if !ef.IsDirectory() && !ef.IsDeleted() && !ef.IsInvalid() {
|
|
|
|
name := []byte(ef.Name)
|
|
|
|
for _, block := range ef.Blocks {
|
2019-02-14 16:15:13 -07:00
|
|
|
keyBuf = t.keyer.GenerateBlockMapKey(keyBuf, folder, block.Hash, name)
|
2019-01-23 02:22:33 -07:00
|
|
|
t.Delete(keyBuf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-06 13:57:33 -07:00
|
|
|
func TestBlockMapAddUpdateWipe(t *testing.T) {
|
|
|
|
db, f := setup()
|
|
|
|
|
|
|
|
if !dbEmpty(db) {
|
|
|
|
t.Fatal("db not empty")
|
|
|
|
}
|
|
|
|
|
2019-01-23 02:22:33 -07:00
|
|
|
folder := []byte("folder1")
|
2014-10-06 13:57:33 -07:00
|
|
|
|
2016-07-04 03:40:29 -07:00
|
|
|
f3.Type = protocol.FileInfoTypeDirectory
|
2014-10-06 13:57:33 -07:00
|
|
|
|
2019-01-23 02:22:33 -07:00
|
|
|
addToBlockMap(db, folder, []protocol.FileInfo{f1, f2, f3})
|
2014-10-06 13:57:33 -07:00
|
|
|
|
2015-09-04 03:01:00 -07:00
|
|
|
f.Iterate(folders, f1.Blocks[0].Hash, func(folder, file string, index int32) bool {
|
2014-10-06 13:57:33 -07:00
|
|
|
if folder != "folder1" || file != "f1" || index != 0 {
|
|
|
|
t.Fatal("Mismatch")
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
2015-09-04 03:01:00 -07:00
|
|
|
f.Iterate(folders, f2.Blocks[0].Hash, func(folder, file string, index int32) bool {
|
2014-10-06 13:57:33 -07:00
|
|
|
if folder != "folder1" || file != "f2" || index != 0 {
|
|
|
|
t.Fatal("Mismatch")
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
2015-09-04 03:01:00 -07:00
|
|
|
f.Iterate(folders, f3.Blocks[0].Hash, func(folder, file string, index int32) bool {
|
2014-10-06 13:57:33 -07:00
|
|
|
t.Fatal("Unexpected block")
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
2019-01-23 02:22:33 -07:00
|
|
|
discardFromBlockMap(db, folder, []protocol.FileInfo{f1, f2, f3})
|
|
|
|
|
2016-07-04 03:40:29 -07:00
|
|
|
f1.Deleted = true
|
2018-06-24 00:50:18 -07:00
|
|
|
f2.LocalFlags = protocol.FlagLocalMustRescan // one of the invalid markers
|
2014-10-06 13:57:33 -07:00
|
|
|
|
2019-01-23 02:22:33 -07:00
|
|
|
addToBlockMap(db, folder, []protocol.FileInfo{f1, f2, f3})
|
2014-10-06 13:57:33 -07:00
|
|
|
|
2015-09-04 03:01:00 -07:00
|
|
|
f.Iterate(folders, f1.Blocks[0].Hash, func(folder, file string, index int32) bool {
|
2014-10-06 13:57:33 -07:00
|
|
|
t.Fatal("Unexpected block")
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
2015-09-04 03:01:00 -07:00
|
|
|
f.Iterate(folders, f2.Blocks[0].Hash, func(folder, file string, index int32) bool {
|
2014-10-06 13:57:33 -07:00
|
|
|
t.Fatal("Unexpected block")
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
2015-09-04 03:01:00 -07:00
|
|
|
f.Iterate(folders, f3.Blocks[0].Hash, func(folder, file string, index int32) bool {
|
2014-10-06 13:57:33 -07:00
|
|
|
if folder != "folder1" || file != "f3" || index != 0 {
|
|
|
|
t.Fatal("Mismatch")
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
2019-01-23 02:22:33 -07:00
|
|
|
db.dropFolder(folder)
|
2014-10-06 13:57:33 -07:00
|
|
|
|
|
|
|
if !dbEmpty(db) {
|
|
|
|
t.Fatal("db not empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should not add
|
2019-01-23 02:22:33 -07:00
|
|
|
addToBlockMap(db, folder, []protocol.FileInfo{f1, f2})
|
2014-10-06 13:57:33 -07:00
|
|
|
|
|
|
|
if !dbEmpty(db) {
|
|
|
|
t.Fatal("db not empty")
|
|
|
|
}
|
|
|
|
|
2016-07-04 03:40:29 -07:00
|
|
|
f1.Deleted = false
|
2018-06-24 00:50:18 -07:00
|
|
|
f1.LocalFlags = 0
|
2016-07-04 03:40:29 -07:00
|
|
|
f2.Deleted = false
|
2018-06-24 00:50:18 -07:00
|
|
|
f2.LocalFlags = 0
|
2016-07-04 03:40:29 -07:00
|
|
|
f3.Deleted = false
|
2018-06-24 00:50:18 -07:00
|
|
|
f3.LocalFlags = 0
|
2014-10-06 13:57:33 -07:00
|
|
|
}
|
|
|
|
|
2014-10-24 14:23:19 -07:00
|
|
|
func TestBlockFinderLookup(t *testing.T) {
|
2014-10-06 13:57:33 -07:00
|
|
|
db, f := setup()
|
|
|
|
|
2019-01-23 02:22:33 -07:00
|
|
|
folder1 := []byte("folder1")
|
|
|
|
folder2 := []byte("folder2")
|
2014-10-06 13:57:33 -07:00
|
|
|
|
2019-01-23 02:22:33 -07:00
|
|
|
addToBlockMap(db, folder1, []protocol.FileInfo{f1})
|
|
|
|
addToBlockMap(db, folder2, []protocol.FileInfo{f1})
|
2014-10-06 13:57:33 -07:00
|
|
|
|
|
|
|
counter := 0
|
2015-09-04 03:01:00 -07:00
|
|
|
f.Iterate(folders, f1.Blocks[0].Hash, func(folder, file string, index int32) bool {
|
2014-10-06 13:57:33 -07:00
|
|
|
counter++
|
|
|
|
switch counter {
|
|
|
|
case 1:
|
|
|
|
if folder != "folder1" || file != "f1" || index != 0 {
|
|
|
|
t.Fatal("Mismatch")
|
|
|
|
}
|
|
|
|
case 2:
|
|
|
|
if folder != "folder2" || file != "f1" || index != 0 {
|
|
|
|
t.Fatal("Mismatch")
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
t.Fatal("Unexpected block")
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
2015-09-04 03:01:00 -07:00
|
|
|
|
2014-10-06 13:57:33 -07:00
|
|
|
if counter != 2 {
|
|
|
|
t.Fatal("Incorrect count", counter)
|
|
|
|
}
|
|
|
|
|
2019-01-23 02:22:33 -07:00
|
|
|
discardFromBlockMap(db, folder1, []protocol.FileInfo{f1})
|
|
|
|
|
2016-07-04 03:40:29 -07:00
|
|
|
f1.Deleted = true
|
2014-10-06 13:57:33 -07:00
|
|
|
|
2019-01-23 02:22:33 -07:00
|
|
|
addToBlockMap(db, folder1, []protocol.FileInfo{f1})
|
2014-10-06 13:57:33 -07:00
|
|
|
|
|
|
|
counter = 0
|
2015-09-04 03:01:00 -07:00
|
|
|
f.Iterate(folders, f1.Blocks[0].Hash, func(folder, file string, index int32) bool {
|
2014-10-06 13:57:33 -07:00
|
|
|
counter++
|
|
|
|
switch counter {
|
|
|
|
case 1:
|
|
|
|
if folder != "folder2" || file != "f1" || index != 0 {
|
|
|
|
t.Fatal("Mismatch")
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
t.Fatal("Unexpected block")
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
2015-09-04 03:01:00 -07:00
|
|
|
|
2014-10-06 13:57:33 -07:00
|
|
|
if counter != 1 {
|
|
|
|
t.Fatal("Incorrect count")
|
|
|
|
}
|
2014-10-24 14:23:19 -07:00
|
|
|
|
2016-07-04 03:40:29 -07:00
|
|
|
f1.Deleted = false
|
2014-10-06 13:57:33 -07:00
|
|
|
}
|