Rename db.Set to db.FileSet

This commit is contained in:
Jakob Borg 2015-01-12 14:52:24 +01:00
parent 8d6db7be31
commit e6c9baf6ef
5 changed files with 45 additions and 45 deletions

View File

@ -39,7 +39,7 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
fs := db.NewSet(*folder, ldb) fs := db.NewFileSet(*folder, ldb)
if *device == "" { if *device == "" {
log.Printf("*** Global index for folder %q", *folder) log.Printf("*** Global index for folder %q", *folder)

View File

@ -91,7 +91,7 @@ func TestSanityCheck(t *testing.T) {
// Case 3 - marker missing // Case 3 - marker missing
set := db.NewSet("folder", ldb) set := db.NewFileSet("folder", ldb)
set.Update(protocol.LocalDeviceID, []protocol.FileInfo{ set.Update(protocol.LocalDeviceID, []protocol.FileInfo{
{Name: "dummyfile"}, {Name: "dummyfile"},
}) })

View File

@ -30,7 +30,7 @@ import (
"github.com/syndtr/goleveldb/leveldb" "github.com/syndtr/goleveldb/leveldb"
) )
type Set struct { type FileSet struct {
localVersion map[protocol.DeviceID]uint64 localVersion map[protocol.DeviceID]uint64
mutex sync.Mutex mutex sync.Mutex
folder string folder string
@ -54,8 +54,8 @@ type FileIntf interface {
// continue iteration, false to stop. // continue iteration, false to stop.
type Iterator func(f FileIntf) bool type Iterator func(f FileIntf) bool
func NewSet(folder string, db *leveldb.DB) *Set { func NewFileSet(folder string, db *leveldb.DB) *FileSet {
var s = Set{ var s = FileSet{
localVersion: make(map[protocol.DeviceID]uint64), localVersion: make(map[protocol.DeviceID]uint64),
folder: folder, folder: folder,
db: db, db: db,
@ -81,7 +81,7 @@ func NewSet(folder string, db *leveldb.DB) *Set {
return &s return &s
} }
func (s *Set) Replace(device protocol.DeviceID, fs []protocol.FileInfo) { func (s *FileSet) Replace(device protocol.DeviceID, fs []protocol.FileInfo) {
if debug { if debug {
l.Debugf("%s Replace(%v, [%d])", s.folder, device, len(fs)) l.Debugf("%s Replace(%v, [%d])", s.folder, device, len(fs))
} }
@ -99,7 +99,7 @@ func (s *Set) Replace(device protocol.DeviceID, fs []protocol.FileInfo) {
} }
} }
func (s *Set) ReplaceWithDelete(device protocol.DeviceID, fs []protocol.FileInfo) { func (s *FileSet) ReplaceWithDelete(device protocol.DeviceID, fs []protocol.FileInfo) {
if debug { if debug {
l.Debugf("%s ReplaceWithDelete(%v, [%d])", s.folder, device, len(fs)) l.Debugf("%s ReplaceWithDelete(%v, [%d])", s.folder, device, len(fs))
} }
@ -115,7 +115,7 @@ func (s *Set) ReplaceWithDelete(device protocol.DeviceID, fs []protocol.FileInfo
} }
} }
func (s *Set) Update(device protocol.DeviceID, fs []protocol.FileInfo) { func (s *FileSet) Update(device protocol.DeviceID, fs []protocol.FileInfo) {
if debug { if debug {
l.Debugf("%s Update(%v, [%d])", s.folder, device, len(fs)) l.Debugf("%s Update(%v, [%d])", s.folder, device, len(fs))
} }
@ -140,55 +140,55 @@ func (s *Set) Update(device protocol.DeviceID, fs []protocol.FileInfo) {
} }
} }
func (s *Set) WithNeed(device protocol.DeviceID, fn Iterator) { func (s *FileSet) WithNeed(device protocol.DeviceID, fn Iterator) {
if debug { if debug {
l.Debugf("%s WithNeed(%v)", s.folder, device) l.Debugf("%s WithNeed(%v)", s.folder, device)
} }
ldbWithNeed(s.db, []byte(s.folder), device[:], false, nativeFileIterator(fn)) ldbWithNeed(s.db, []byte(s.folder), device[:], false, nativeFileIterator(fn))
} }
func (s *Set) WithNeedTruncated(device protocol.DeviceID, fn Iterator) { func (s *FileSet) WithNeedTruncated(device protocol.DeviceID, fn Iterator) {
if debug { if debug {
l.Debugf("%s WithNeedTruncated(%v)", s.folder, device) l.Debugf("%s WithNeedTruncated(%v)", s.folder, device)
} }
ldbWithNeed(s.db, []byte(s.folder), device[:], true, nativeFileIterator(fn)) ldbWithNeed(s.db, []byte(s.folder), device[:], true, nativeFileIterator(fn))
} }
func (s *Set) WithHave(device protocol.DeviceID, fn Iterator) { func (s *FileSet) WithHave(device protocol.DeviceID, fn Iterator) {
if debug { if debug {
l.Debugf("%s WithHave(%v)", s.folder, device) l.Debugf("%s WithHave(%v)", s.folder, device)
} }
ldbWithHave(s.db, []byte(s.folder), device[:], false, nativeFileIterator(fn)) ldbWithHave(s.db, []byte(s.folder), device[:], false, nativeFileIterator(fn))
} }
func (s *Set) WithHaveTruncated(device protocol.DeviceID, fn Iterator) { func (s *FileSet) WithHaveTruncated(device protocol.DeviceID, fn Iterator) {
if debug { if debug {
l.Debugf("%s WithHaveTruncated(%v)", s.folder, device) l.Debugf("%s WithHaveTruncated(%v)", s.folder, device)
} }
ldbWithHave(s.db, []byte(s.folder), device[:], true, nativeFileIterator(fn)) ldbWithHave(s.db, []byte(s.folder), device[:], true, nativeFileIterator(fn))
} }
func (s *Set) WithGlobal(fn Iterator) { func (s *FileSet) WithGlobal(fn Iterator) {
if debug { if debug {
l.Debugf("%s WithGlobal()", s.folder) l.Debugf("%s WithGlobal()", s.folder)
} }
ldbWithGlobal(s.db, []byte(s.folder), false, nativeFileIterator(fn)) ldbWithGlobal(s.db, []byte(s.folder), false, nativeFileIterator(fn))
} }
func (s *Set) WithGlobalTruncated(fn Iterator) { func (s *FileSet) WithGlobalTruncated(fn Iterator) {
if debug { if debug {
l.Debugf("%s WithGlobalTruncated()", s.folder) l.Debugf("%s WithGlobalTruncated()", s.folder)
} }
ldbWithGlobal(s.db, []byte(s.folder), true, nativeFileIterator(fn)) ldbWithGlobal(s.db, []byte(s.folder), true, nativeFileIterator(fn))
} }
func (s *Set) Get(device protocol.DeviceID, file string) (protocol.FileInfo, bool) { func (s *FileSet) Get(device protocol.DeviceID, file string) (protocol.FileInfo, bool) {
f, ok := ldbGet(s.db, []byte(s.folder), device[:], []byte(osutil.NormalizedFilename(file))) f, ok := ldbGet(s.db, []byte(s.folder), device[:], []byte(osutil.NormalizedFilename(file)))
f.Name = osutil.NativeFilename(f.Name) f.Name = osutil.NativeFilename(f.Name)
return f, ok return f, ok
} }
func (s *Set) GetGlobal(file string) (protocol.FileInfo, bool) { func (s *FileSet) GetGlobal(file string) (protocol.FileInfo, bool) {
fi, ok := ldbGetGlobal(s.db, []byte(s.folder), []byte(osutil.NormalizedFilename(file)), false) fi, ok := ldbGetGlobal(s.db, []byte(s.folder), []byte(osutil.NormalizedFilename(file)), false)
if !ok { if !ok {
return protocol.FileInfo{}, false return protocol.FileInfo{}, false
@ -198,7 +198,7 @@ func (s *Set) GetGlobal(file string) (protocol.FileInfo, bool) {
return f, true return f, true
} }
func (s *Set) GetGlobalTruncated(file string) (FileInfoTruncated, bool) { func (s *FileSet) GetGlobalTruncated(file string) (FileInfoTruncated, bool) {
fi, ok := ldbGetGlobal(s.db, []byte(s.folder), []byte(osutil.NormalizedFilename(file)), true) fi, ok := ldbGetGlobal(s.db, []byte(s.folder), []byte(osutil.NormalizedFilename(file)), true)
if !ok { if !ok {
return FileInfoTruncated{}, false return FileInfoTruncated{}, false
@ -208,11 +208,11 @@ func (s *Set) GetGlobalTruncated(file string) (FileInfoTruncated, bool) {
return f, true return f, true
} }
func (s *Set) Availability(file string) []protocol.DeviceID { func (s *FileSet) Availability(file string) []protocol.DeviceID {
return ldbAvailability(s.db, []byte(s.folder), []byte(osutil.NormalizedFilename(file))) return ldbAvailability(s.db, []byte(s.folder), []byte(osutil.NormalizedFilename(file)))
} }
func (s *Set) LocalVersion(device protocol.DeviceID) uint64 { func (s *FileSet) LocalVersion(device protocol.DeviceID) uint64 {
s.mutex.Lock() s.mutex.Lock()
defer s.mutex.Unlock() defer s.mutex.Unlock()
return s.localVersion[device] return s.localVersion[device]

View File

@ -49,7 +49,7 @@ func genBlocks(n int) []protocol.BlockInfo {
return b return b
} }
func globalList(s *db.Set) []protocol.FileInfo { func globalList(s *db.FileSet) []protocol.FileInfo {
var fs []protocol.FileInfo var fs []protocol.FileInfo
s.WithGlobal(func(fi db.FileIntf) bool { s.WithGlobal(func(fi db.FileIntf) bool {
f := fi.(protocol.FileInfo) f := fi.(protocol.FileInfo)
@ -59,7 +59,7 @@ func globalList(s *db.Set) []protocol.FileInfo {
return fs return fs
} }
func haveList(s *db.Set, n protocol.DeviceID) []protocol.FileInfo { func haveList(s *db.FileSet, n protocol.DeviceID) []protocol.FileInfo {
var fs []protocol.FileInfo var fs []protocol.FileInfo
s.WithHave(n, func(fi db.FileIntf) bool { s.WithHave(n, func(fi db.FileIntf) bool {
f := fi.(protocol.FileInfo) f := fi.(protocol.FileInfo)
@ -69,7 +69,7 @@ func haveList(s *db.Set, n protocol.DeviceID) []protocol.FileInfo {
return fs return fs
} }
func needList(s *db.Set, n protocol.DeviceID) []protocol.FileInfo { func needList(s *db.FileSet, n protocol.DeviceID) []protocol.FileInfo {
var fs []protocol.FileInfo var fs []protocol.FileInfo
s.WithNeed(n, func(fi db.FileIntf) bool { s.WithNeed(n, func(fi db.FileIntf) bool {
f := fi.(protocol.FileInfo) f := fi.(protocol.FileInfo)
@ -111,7 +111,7 @@ func TestGlobalSet(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
m := db.NewSet("test", ldb) m := db.NewFileSet("test", ldb)
local0 := fileList{ local0 := fileList{
protocol.FileInfo{Name: "a", Version: 1000, Blocks: genBlocks(1)}, protocol.FileInfo{Name: "a", Version: 1000, Blocks: genBlocks(1)},
@ -272,7 +272,7 @@ func TestNeedWithInvalid(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
s := db.NewSet("test", ldb) s := db.NewFileSet("test", ldb)
localHave := fileList{ localHave := fileList{
protocol.FileInfo{Name: "a", Version: 1000, Blocks: genBlocks(1)}, protocol.FileInfo{Name: "a", Version: 1000, Blocks: genBlocks(1)},
@ -314,7 +314,7 @@ func TestUpdateToInvalid(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
s := db.NewSet("test", ldb) s := db.NewFileSet("test", ldb)
localHave := fileList{ localHave := fileList{
protocol.FileInfo{Name: "a", Version: 1000, Blocks: genBlocks(1)}, protocol.FileInfo{Name: "a", Version: 1000, Blocks: genBlocks(1)},
@ -351,7 +351,7 @@ func TestInvalidAvailability(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
s := db.NewSet("test", ldb) s := db.NewFileSet("test", ldb)
remote0Have := fileList{ remote0Have := fileList{
protocol.FileInfo{Name: "both", Version: 1001, Blocks: genBlocks(2)}, protocol.FileInfo{Name: "both", Version: 1001, Blocks: genBlocks(2)},
@ -391,7 +391,7 @@ func TestLocalDeleted(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
m := db.NewSet("test", ldb) m := db.NewFileSet("test", ldb)
lamport.Default = lamport.Clock{} lamport.Default = lamport.Clock{}
local1 := []protocol.FileInfo{ local1 := []protocol.FileInfo{
@ -474,7 +474,7 @@ func Benchmark10kReplace(b *testing.B) {
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
m := db.NewSet("test", ldb) m := db.NewFileSet("test", ldb)
m.ReplaceWithDelete(protocol.LocalDeviceID, local) m.ReplaceWithDelete(protocol.LocalDeviceID, local)
} }
} }
@ -490,7 +490,7 @@ func Benchmark10kUpdateChg(b *testing.B) {
b.Fatal(err) b.Fatal(err)
} }
m := db.NewSet("test", ldb) m := db.NewFileSet("test", ldb)
m.Replace(remoteDevice0, remote) m.Replace(remoteDevice0, remote)
var local []protocol.FileInfo var local []protocol.FileInfo
@ -521,7 +521,7 @@ func Benchmark10kUpdateSme(b *testing.B) {
if err != nil { if err != nil {
b.Fatal(err) b.Fatal(err)
} }
m := db.NewSet("test", ldb) m := db.NewFileSet("test", ldb)
m.Replace(remoteDevice0, remote) m.Replace(remoteDevice0, remote)
var local []protocol.FileInfo var local []protocol.FileInfo
@ -548,7 +548,7 @@ func Benchmark10kNeed2k(b *testing.B) {
b.Fatal(err) b.Fatal(err)
} }
m := db.NewSet("test", ldb) m := db.NewFileSet("test", ldb)
m.Replace(remoteDevice0, remote) m.Replace(remoteDevice0, remote)
var local []protocol.FileInfo var local []protocol.FileInfo
@ -581,7 +581,7 @@ func Benchmark10kHaveFullList(b *testing.B) {
b.Fatal(err) b.Fatal(err)
} }
m := db.NewSet("test", ldb) m := db.NewFileSet("test", ldb)
m.Replace(remoteDevice0, remote) m.Replace(remoteDevice0, remote)
var local []protocol.FileInfo var local []protocol.FileInfo
@ -614,7 +614,7 @@ func Benchmark10kGlobal(b *testing.B) {
b.Fatal(err) b.Fatal(err)
} }
m := db.NewSet("test", ldb) m := db.NewFileSet("test", ldb)
m.Replace(remoteDevice0, remote) m.Replace(remoteDevice0, remote)
var local []protocol.FileInfo var local []protocol.FileInfo
@ -642,7 +642,7 @@ func TestGlobalReset(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
m := db.NewSet("test", ldb) m := db.NewFileSet("test", ldb)
local := []protocol.FileInfo{ local := []protocol.FileInfo{
{Name: "a", Version: 1000}, {Name: "a", Version: 1000},
@ -683,7 +683,7 @@ func TestNeed(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
m := db.NewSet("test", ldb) m := db.NewFileSet("test", ldb)
local := []protocol.FileInfo{ local := []protocol.FileInfo{
{Name: "a", Version: 1000}, {Name: "a", Version: 1000},
@ -724,7 +724,7 @@ func TestLocalVersion(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
m := db.NewSet("test", ldb) m := db.NewFileSet("test", ldb)
local1 := []protocol.FileInfo{ local1 := []protocol.FileInfo{
{Name: "a", Version: 1000}, {Name: "a", Version: 1000},
@ -763,7 +763,7 @@ func TestListDropFolder(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
s0 := db.NewSet("test0", ldb) s0 := db.NewFileSet("test0", ldb)
local1 := []protocol.FileInfo{ local1 := []protocol.FileInfo{
{Name: "a", Version: 1000}, {Name: "a", Version: 1000},
{Name: "b", Version: 1000}, {Name: "b", Version: 1000},
@ -771,7 +771,7 @@ func TestListDropFolder(t *testing.T) {
} }
s0.Replace(protocol.LocalDeviceID, local1) s0.Replace(protocol.LocalDeviceID, local1)
s1 := db.NewSet("test1", ldb) s1 := db.NewFileSet("test1", ldb)
local2 := []protocol.FileInfo{ local2 := []protocol.FileInfo{
{Name: "d", Version: 1002}, {Name: "d", Version: 1002},
{Name: "e", Version: 1002}, {Name: "e", Version: 1002},
@ -814,7 +814,7 @@ func TestGlobalNeedWithInvalid(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
s := db.NewSet("test1", ldb) s := db.NewFileSet("test1", ldb)
rem0 := fileList{ rem0 := fileList{
protocol.FileInfo{Name: "a", Version: 1002, Blocks: genBlocks(4)}, protocol.FileInfo{Name: "a", Version: 1002, Blocks: genBlocks(4)},
@ -854,7 +854,7 @@ func TestLongPath(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
s := db.NewSet("test", ldb) s := db.NewFileSet("test", ldb)
var b bytes.Buffer var b bytes.Buffer
for i := 0; i < 100; i++ { for i := 0; i < 100; i++ {

View File

@ -94,7 +94,7 @@ type Model struct {
clientVersion string clientVersion string
folderCfgs map[string]config.FolderConfiguration // folder -> cfg folderCfgs map[string]config.FolderConfiguration // folder -> cfg
folderFiles map[string]*db.Set // folder -> files folderFiles map[string]*db.FileSet // folder -> files
folderDevices map[string][]protocol.DeviceID // folder -> deviceIDs folderDevices map[string][]protocol.DeviceID // folder -> deviceIDs
deviceFolders map[protocol.DeviceID][]string // deviceID -> folders deviceFolders map[protocol.DeviceID][]string // deviceID -> folders
deviceStatRefs map[protocol.DeviceID]*stats.DeviceStatisticsReference // deviceID -> statsRef deviceStatRefs map[protocol.DeviceID]*stats.DeviceStatisticsReference // deviceID -> statsRef
@ -134,7 +134,7 @@ func NewModel(cfg *config.Wrapper, deviceName, clientName, clientVersion string,
clientName: clientName, clientName: clientName,
clientVersion: clientVersion, clientVersion: clientVersion,
folderCfgs: make(map[string]config.FolderConfiguration), folderCfgs: make(map[string]config.FolderConfiguration),
folderFiles: make(map[string]*db.Set), folderFiles: make(map[string]*db.FileSet),
folderDevices: make(map[string][]protocol.DeviceID), folderDevices: make(map[string][]protocol.DeviceID),
deviceFolders: make(map[protocol.DeviceID][]string), deviceFolders: make(map[protocol.DeviceID][]string),
deviceStatRefs: make(map[protocol.DeviceID]*stats.DeviceStatisticsReference), deviceStatRefs: make(map[protocol.DeviceID]*stats.DeviceStatisticsReference),
@ -949,7 +949,7 @@ func (m *Model) receivedFile(folder, filename string) {
m.folderStatRef(folder).ReceivedFile(filename) m.folderStatRef(folder).ReceivedFile(filename)
} }
func sendIndexes(conn protocol.Connection, folder string, fs *db.Set, ignores *ignore.Matcher) { func sendIndexes(conn protocol.Connection, folder string, fs *db.FileSet, ignores *ignore.Matcher) {
deviceID := conn.ID() deviceID := conn.ID()
name := conn.Name() name := conn.Name()
var err error var err error
@ -974,7 +974,7 @@ func sendIndexes(conn protocol.Connection, folder string, fs *db.Set, ignores *i
} }
} }
func sendIndexTo(initial bool, minLocalVer uint64, conn protocol.Connection, folder string, fs *db.Set, ignores *ignore.Matcher) (uint64, error) { func sendIndexTo(initial bool, minLocalVer uint64, conn protocol.Connection, folder string, fs *db.FileSet, ignores *ignore.Matcher) (uint64, error) {
deviceID := conn.ID() deviceID := conn.ID()
name := conn.Name() name := conn.Name()
batch := make([]protocol.FileInfo, 0, indexBatchSize) batch := make([]protocol.FileInfo, 0, indexBatchSize)
@ -1081,7 +1081,7 @@ func (m *Model) AddFolder(cfg config.FolderConfiguration) {
m.fmut.Lock() m.fmut.Lock()
m.folderCfgs[cfg.ID] = cfg m.folderCfgs[cfg.ID] = cfg
m.folderFiles[cfg.ID] = db.NewSet(cfg.ID, m.db) m.folderFiles[cfg.ID] = db.NewFileSet(cfg.ID, m.db)
m.folderDevices[cfg.ID] = make([]protocol.DeviceID, len(cfg.Devices)) m.folderDevices[cfg.ID] = make([]protocol.DeviceID, len(cfg.Devices))
for i, device := range cfg.Devices { for i, device := range cfg.Devices {