lib/versioner: Reduce surface area (#6186)
* lib/versioner: Reduce surface area This is a refactor while I was anyway rooting around in the versioner. Instead of exporting every possible implementation and the factory and letting the caller do whatever, this now encapsulates all that and exposes a New() that takes a config.VersioningConfiguration. Given that and that we don't know (from the outside) how a versioner works or what state it keeps, we now just construct it once per folder and keep it around. Previously it was recreated for each restore request. * unparam * wip
This commit is contained in:
committed by
Audrius Butkevicius
parent
f747ba6d69
commit
4e151d380c
@@ -20,21 +20,21 @@ import (
|
||||
|
||||
func init() {
|
||||
// Register the constructor for this type of versioner
|
||||
Factories["trashcan"] = NewTrashcan
|
||||
factories["trashcan"] = newTrashcan
|
||||
}
|
||||
|
||||
type Trashcan struct {
|
||||
type trashcan struct {
|
||||
suture.Service
|
||||
folderFs fs.Filesystem
|
||||
versionsFs fs.Filesystem
|
||||
cleanoutDays int
|
||||
}
|
||||
|
||||
func NewTrashcan(folderID string, folderFs fs.Filesystem, params map[string]string) Versioner {
|
||||
func newTrashcan(folderFs fs.Filesystem, params map[string]string) Versioner {
|
||||
cleanoutDays, _ := strconv.Atoi(params["cleanoutDays"])
|
||||
// On error we default to 0, "do not clean out the trash can"
|
||||
|
||||
s := &Trashcan{
|
||||
s := &trashcan{
|
||||
folderFs: folderFs,
|
||||
versionsFs: fsFromParams(folderFs, params),
|
||||
cleanoutDays: cleanoutDays,
|
||||
@@ -47,13 +47,13 @@ func NewTrashcan(folderID string, folderFs fs.Filesystem, params map[string]stri
|
||||
|
||||
// Archive moves the named file away to a version archive. If this function
|
||||
// returns nil, the named file does not exist any more (has been archived).
|
||||
func (t *Trashcan) Archive(filePath string) error {
|
||||
func (t *trashcan) Archive(filePath string) error {
|
||||
return archiveFile(t.folderFs, t.versionsFs, filePath, func(name, tag string) string {
|
||||
return name
|
||||
})
|
||||
}
|
||||
|
||||
func (t *Trashcan) serve(ctx context.Context) {
|
||||
func (t *trashcan) serve(ctx context.Context) {
|
||||
l.Debugln(t, "starting")
|
||||
defer l.Debugln(t, "stopping")
|
||||
|
||||
@@ -79,11 +79,11 @@ func (t *Trashcan) serve(ctx context.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Trashcan) String() string {
|
||||
func (t *trashcan) String() string {
|
||||
return fmt.Sprintf("trashcan@%p", t)
|
||||
}
|
||||
|
||||
func (t *Trashcan) cleanoutArchive() error {
|
||||
func (t *trashcan) cleanoutArchive() error {
|
||||
if _, err := t.versionsFs.Lstat("."); fs.IsNotExist(err) {
|
||||
return nil
|
||||
}
|
||||
@@ -121,11 +121,11 @@ func (t *Trashcan) cleanoutArchive() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *Trashcan) GetVersions() (map[string][]FileVersion, error) {
|
||||
func (t *trashcan) GetVersions() (map[string][]FileVersion, error) {
|
||||
return retrieveVersions(t.versionsFs)
|
||||
}
|
||||
|
||||
func (t *Trashcan) Restore(filepath string, versionTime time.Time) error {
|
||||
func (t *trashcan) Restore(filepath string, versionTime time.Time) error {
|
||||
// If we have an untagged file A and want to restore it on top of existing file A, we can't first archive the
|
||||
// existing A as we'd overwrite the old A version, therefore when we archive existing file, we archive it with a
|
||||
// tag but when the restoration is finished, we rename it (untag it). This is only important if when restoring A,
|
||||
|
||||
Reference in New Issue
Block a user