syncthing/files/set.go

119 lines
2.7 KiB
Go
Raw Normal View History

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