2014-07-12 15:45:33 -07:00
|
|
|
// Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
|
|
|
|
// All rights reserved. Use of this source code is governed by an MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
2014-06-01 13:50:14 -07:00
|
|
|
|
2014-03-28 06:36:57 -07:00
|
|
|
// Package files provides a set type to track local/remote files with newness checks.
|
|
|
|
package files
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/calmh/syncthing/protocol"
|
2014-07-06 05:46:48 -07:00
|
|
|
"github.com/syndtr/goleveldb/leveldb"
|
2014-03-28 06:36:57 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type fileRecord struct {
|
2014-07-12 14:06:48 -07:00
|
|
|
File protocol.FileInfo
|
2014-04-14 02:44:29 -07:00
|
|
|
Usage int
|
|
|
|
Global bool
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type bitset uint64
|
|
|
|
|
|
|
|
type Set struct {
|
2014-07-06 05:46:48 -07:00
|
|
|
changes map[protocol.NodeID]uint64
|
2014-07-06 10:21:58 -07:00
|
|
|
mutex sync.Mutex
|
2014-07-06 05:46:48 -07:00
|
|
|
repo string
|
|
|
|
db *leveldb.DB
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
|
|
|
|
2014-07-06 05:46:48 -07:00
|
|
|
func NewSet(repo string, db *leveldb.DB) *Set {
|
|
|
|
var s = Set{
|
|
|
|
changes: make(map[protocol.NodeID]uint64),
|
|
|
|
repo: repo,
|
|
|
|
db: db,
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
2014-07-06 05:46:48 -07:00
|
|
|
return &s
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
|
|
|
|
2014-07-12 14:06:48 -07:00
|
|
|
func (s *Set) Replace(node protocol.NodeID, fs []protocol.FileInfo) {
|
2014-03-28 06:36:57 -07:00
|
|
|
if debug {
|
2014-07-06 05:46:48 -07:00
|
|
|
l.Debugf("%s Replace(%v, [%d])", s.repo, node, len(fs))
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
2014-07-06 05:46:48 -07:00
|
|
|
s.mutex.Lock()
|
|
|
|
defer s.mutex.Unlock()
|
|
|
|
if ldbReplace(s.db, []byte(s.repo), node[:], fs) {
|
|
|
|
s.changes[node]++
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-12 14:06:48 -07:00
|
|
|
func (s *Set) ReplaceWithDelete(node protocol.NodeID, fs []protocol.FileInfo) {
|
2014-03-28 06:36:57 -07:00
|
|
|
if debug {
|
2014-07-06 05:46:48 -07:00
|
|
|
l.Debugf("%s ReplaceWithDelete(%v, [%d])", s.repo, node, len(fs))
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
2014-07-06 05:46:48 -07:00
|
|
|
s.mutex.Lock()
|
|
|
|
defer s.mutex.Unlock()
|
|
|
|
if ldbReplaceWithDelete(s.db, []byte(s.repo), node[:], fs) {
|
|
|
|
s.changes[node]++
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-12 14:06:48 -07:00
|
|
|
func (s *Set) Update(node protocol.NodeID, fs []protocol.FileInfo) {
|
2014-03-28 06:36:57 -07:00
|
|
|
if debug {
|
2014-07-06 05:46:48 -07:00
|
|
|
l.Debugf("%s Update(%v, [%d])", s.repo, node, len(fs))
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
2014-07-06 05:46:48 -07:00
|
|
|
s.mutex.Lock()
|
|
|
|
defer s.mutex.Unlock()
|
|
|
|
if ldbUpdate(s.db, []byte(s.repo), node[:], fs) {
|
|
|
|
s.changes[node]++
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-06 05:46:48 -07:00
|
|
|
func (s *Set) WithNeed(node protocol.NodeID, fn fileIterator) {
|
2014-03-28 06:36:57 -07:00
|
|
|
if debug {
|
2014-07-06 05:46:48 -07:00
|
|
|
l.Debugf("%s Need(%v)", s.repo, node)
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
2014-07-06 05:46:48 -07:00
|
|
|
ldbWithNeed(s.db, []byte(s.repo), node[:], fn)
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
|
|
|
|
2014-07-06 05:46:48 -07:00
|
|
|
func (s *Set) WithHave(node protocol.NodeID, fn fileIterator) {
|
2014-03-28 06:36:57 -07:00
|
|
|
if debug {
|
2014-07-06 05:46:48 -07:00
|
|
|
l.Debugf("%s WithHave(%v)", s.repo, node)
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
2014-07-06 05:46:48 -07:00
|
|
|
ldbWithHave(s.db, []byte(s.repo), node[:], fn)
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
|
|
|
|
2014-07-06 05:46:48 -07:00
|
|
|
func (s *Set) WithGlobal(fn fileIterator) {
|
2014-03-28 06:36:57 -07:00
|
|
|
if debug {
|
2014-07-06 05:46:48 -07:00
|
|
|
l.Debugf("%s WithGlobal()", s.repo)
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
2014-07-06 05:46:48 -07:00
|
|
|
ldbWithGlobal(s.db, []byte(s.repo), fn)
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
|
|
|
|
2014-07-12 14:06:48 -07:00
|
|
|
func (s *Set) Get(node protocol.NodeID, file string) protocol.FileInfo {
|
2014-07-06 05:46:48 -07:00
|
|
|
return ldbGet(s.db, []byte(s.repo), node[:], []byte(file))
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
|
|
|
|
2014-07-12 14:06:48 -07:00
|
|
|
func (s *Set) GetGlobal(file string) protocol.FileInfo {
|
2014-07-06 05:46:48 -07:00
|
|
|
return ldbGetGlobal(s.db, []byte(s.repo), []byte(file))
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
|
|
|
|
2014-07-06 05:46:48 -07:00
|
|
|
func (s *Set) Availability(file string) []protocol.NodeID {
|
|
|
|
return ldbAvailability(s.db, []byte(s.repo), []byte(file))
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
|
|
|
|
2014-07-06 05:46:48 -07:00
|
|
|
func (s *Set) Changes(node protocol.NodeID) uint64 {
|
2014-07-06 10:21:58 -07:00
|
|
|
s.mutex.Lock()
|
|
|
|
defer s.mutex.Unlock()
|
2014-07-06 05:46:48 -07:00
|
|
|
return s.changes[node]
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|