dircache

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 21, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package dircache provides a simple cache for caching directory ID to path lookups and the inverse.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SplitPath

func SplitPath(path string) (directory, leaf string)

SplitPath splits a path into directory, leaf

Path shouldn't start or end with a /

If there are no slashes then directory will be "" and leaf = path

Types

type DirCache

type DirCache struct {
	// contains filtered or unexported fields
}

DirCache caches paths to directory IDs and vice versa

func New

func New(root string, trueRootID string, fs DirCacher) *DirCache

New makes a DirCache

This is created with the true root ID and the root path.

In order to use the cache FindRoot() must be called on it without error. This isn't done at initialization as it isn't known whether the root and intermediate directories need to be created or not.

Most of the utility functions will call FindRoot() on the caller's behalf with the create flag passed in.

The cache is safe for concurrent use

func (*DirCache) DirMove

func (dc *DirCache) DirMove(
	ctx context.Context, srcDC *DirCache, srcRoot, srcRemote, dstRoot, dstRemote string) (srcID, srcDirectoryID, srcLeaf, dstDirectoryID, dstLeaf string, err error)

DirMove prepares to move the directory (srcDC, srcRoot, srcRemote) into the directory (dc, dstRoot, dstRemote)

It does all the checking, creates intermediate directories and returns leafs and IDs ready for the move.

This returns:

- srcID - ID of the source directory - srcDirectoryID - ID of the parent of the source directory - srcLeaf - leaf name of the source directory - dstDirectoryID - ID of the parent of the destination directory - dstLeaf - leaf name of the destination directory

These should be used to do the actual move then srcDC.FlushDir(srcRemote) should be called.

func (*DirCache) FindDir

func (dc *DirCache) FindDir(ctx context.Context, path string, create bool) (pathID string, err error)

FindDir finds the directory passed in returning the directory ID starting from pathID

Path shouldn't start or end with a /

If create is set it will make the directory if not found.

It will call FindRoot if it hasn't been called already

func (*DirCache) FindPath

func (dc *DirCache) FindPath(ctx context.Context, path string, create bool) (leaf, directoryID string, err error)

FindPath finds the leaf and directoryID from a path

If called with path == "" then it will return the ID of the parent directory of the root and the leaf name of the root in that directory. Note that it won't create the root directory in this case even if create is true.

If create is set parent directories will be created if they don't exist

It will call FindRoot if it hasn't been called already

func (*DirCache) FindRoot

func (dc *DirCache) FindRoot(ctx context.Context, create bool) error

FindRoot finds the root directory if not already found

If successful this changes the root of the cache from the true root to the root specified by the path passed into New.

Resets the root directory.

If create is set it will make the directory if not found

func (*DirCache) Flush

func (dc *DirCache) Flush()

Flush the cache of all data

func (*DirCache) FlushDir

func (dc *DirCache) FlushDir(dir string)

FlushDir flushes the map of all data starting with the path dir.

If dir is empty string then this is equivalent to calling ResetRoot

func (*DirCache) FoundRoot

func (dc *DirCache) FoundRoot() bool

FoundRoot returns whether the root directory has been found yet

func (*DirCache) Get

func (dc *DirCache) Get(path string) (id string, ok bool)

Get a directory ID given a path

Returns the ID and a boolean as to whether it was found or not in the cache.

func (*DirCache) GetInv

func (dc *DirCache) GetInv(id string) (path string, ok bool)

GetInv gets a path given a directory ID

Returns the path and a boolean as to whether it was found or not in the cache.

func (*DirCache) Put

func (dc *DirCache) Put(path, id string)

Put a (path, directory ID) pair into the cache

func (*DirCache) ResetRoot

func (dc *DirCache) ResetRoot()

ResetRoot resets the root directory to the absolute root and clears the DirCache

func (*DirCache) RootID

func (dc *DirCache) RootID(ctx context.Context, create bool) (ID string, err error)

RootID returns the ID of the root directory

If create is set it will make the root directory if not found

func (*DirCache) RootParentID

func (dc *DirCache) RootParentID(ctx context.Context, create bool) (ID string, err error)

RootParentID returns the ID of the parent of the root directory

If create is set it will make the root parent directory if not found (but not the root)

func (*DirCache) SetRootIDAlias

func (dc *DirCache) SetRootIDAlias(rootID string)

SetRootIDAlias sets the rootID to that passed in. This assumes that the new ID is just an alias for the old ID so does not flush anything.

This should be called from FindLeaf (and only from FindLeaf) if it is discovered that the root ID is incorrect. For example some backends use "0" as a root ID, but it has a real ID which is needed for some operations.

func (*DirCache) String

func (dc *DirCache) String() string

String returns the directory cache in string form for debugging

type DirCacher

type DirCacher interface {
	FindLeaf(ctx context.Context, pathID, leaf string) (pathIDOut string, found bool, err error)
	CreateDir(ctx context.Context, pathID, leaf string) (newID string, err error)
}

DirCacher describes an interface for doing the low level directory work

This should be implemented by the backend and will be called by the dircache package when appropriate.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL