feat: compare with cache before reporting a file as changed
This commit is contained in:
parent
12aa9a7bef
commit
18bdb7210d
47
internal/cache/cache.go
vendored
47
internal/cache/cache.go
vendored
|
@ -64,6 +64,19 @@ func Close() error {
|
||||||
return db.Close()
|
return db.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getFileInfo(bucket *bolt.Bucket, path string) (*FileInfo, error) {
|
||||||
|
b := bucket.Get([]byte(path))
|
||||||
|
if b != nil {
|
||||||
|
var cached FileInfo
|
||||||
|
if err := msgpack.Unmarshal(b, &cached); err != nil {
|
||||||
|
return nil, errors.Annotatef(err, "failed to unmarshal cache info for path '%v'", path)
|
||||||
|
}
|
||||||
|
return &cached, nil
|
||||||
|
} else {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func ChangeSet(ctx context.Context, root string, pathsCh chan<- string) error {
|
func ChangeSet(ctx context.Context, root string, pathsCh chan<- string) error {
|
||||||
return db.Update(func(tx *bolt.Tx) error {
|
return db.Update(func(tx *bolt.Tx) error {
|
||||||
bucket := tx.Bucket([]byte(modifiedBucket))
|
bucket := tx.Bucket([]byte(modifiedBucket))
|
||||||
|
@ -83,17 +96,12 @@ func ChangeSet(ctx context.Context, root string, pathsCh chan<- string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
b := bucket.Get([]byte(path))
|
cached, err := getFileInfo(bucket, path)
|
||||||
|
if err != nil {
|
||||||
var cached FileInfo
|
return err
|
||||||
|
|
||||||
if b != nil {
|
|
||||||
if err = msgpack.Unmarshal(b, &cached); err != nil {
|
|
||||||
return errors.Annotatef(err, "failed to unmarshal cache info for path '%v'", path)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
changedOrNew := !(cached.Modified == info.ModTime() && cached.Size == info.Size())
|
changedOrNew := cached == nil || !(cached.Modified == info.ModTime() && cached.Size == info.Size())
|
||||||
|
|
||||||
if !changedOrNew {
|
if !changedOrNew {
|
||||||
// no change
|
// no change
|
||||||
|
@ -107,23 +115,38 @@ func ChangeSet(ctx context.Context, root string, pathsCh chan<- string) error {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func WriteModTime(paths []string) error {
|
func Update(paths []string) (int, error) {
|
||||||
if len(paths) == 0 {
|
if len(paths) == 0 {
|
||||||
return nil
|
return 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return db.Update(func(tx *bolt.Tx) error {
|
var changes int
|
||||||
|
|
||||||
|
return changes, db.Update(func(tx *bolt.Tx) error {
|
||||||
bucket := tx.Bucket([]byte(modifiedBucket))
|
bucket := tx.Bucket([]byte(modifiedBucket))
|
||||||
|
|
||||||
for _, path := range paths {
|
for _, path := range paths {
|
||||||
if path == "" {
|
if path == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cached, err := getFileInfo(bucket, path)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
pathInfo, err := os.Stat(path)
|
pathInfo, err := os.Stat(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if cached == nil || !(cached.Modified == pathInfo.ModTime() && cached.Size == pathInfo.Size()) {
|
||||||
|
changes += 1
|
||||||
|
} else {
|
||||||
|
// no change to write
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
cacheInfo := FileInfo{
|
cacheInfo := FileInfo{
|
||||||
Size: pathInfo.Size(),
|
Size: pathInfo.Size(),
|
||||||
Modified: pathInfo.ModTime(),
|
Modified: pathInfo.ModTime(),
|
||||||
|
|
|
@ -80,7 +80,7 @@ func (f *Format) Run() error {
|
||||||
batchSize := 1024
|
batchSize := 1024
|
||||||
batch := make([]string, batchSize)
|
batch := make([]string, batchSize)
|
||||||
|
|
||||||
var pending, completed int
|
var pending, completed, changes int
|
||||||
|
|
||||||
LOOP:
|
LOOP:
|
||||||
for {
|
for {
|
||||||
|
@ -98,9 +98,11 @@ func (f *Format) Run() error {
|
||||||
}
|
}
|
||||||
batch = append(batch, path)
|
batch = append(batch, path)
|
||||||
if len(batch) == batchSize {
|
if len(batch) == batchSize {
|
||||||
if err := cache.WriteModTime(batch); err != nil {
|
count, err := cache.Update(batch)
|
||||||
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
changes += count
|
||||||
batch = batch[:0]
|
batch = batch[:0]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -113,11 +115,13 @@ func (f *Format) Run() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// final flush
|
// final flush
|
||||||
if err := cache.WriteModTime(batch); err != nil {
|
count, err := cache.Update(batch)
|
||||||
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
changes += count
|
||||||
|
|
||||||
println(fmt.Sprintf("%v files changed in %v", completed, time.Now().Sub(start)))
|
println(fmt.Sprintf("%v files changed in %v", changes, time.Now().Sub(start)))
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
Reference in New Issue
Block a user