Documentation ¶
Overview ¶
Package dircache provides a simple cache for caching directory ID to path lookups and the inverse.
Index ¶
- func SplitPath(path string) (directory, leaf string)
- type DirCache
- func (dc *DirCache) DirMove(ctx context.Context, srcDC *DirCache, ...) (srcID, srcDirectoryID, srcLeaf, dstDirectoryID, dstLeaf string, err error)
- func (dc *DirCache) FindDir(ctx context.Context, path string, create bool) (pathID string, err error)
- func (dc *DirCache) FindPath(ctx context.Context, path string, create bool) (leaf, directoryID string, err error)
- func (dc *DirCache) FindRoot(ctx context.Context, create bool) error
- func (dc *DirCache) Flush()
- func (dc *DirCache) FlushDir(dir string)
- func (dc *DirCache) FoundRoot() bool
- func (dc *DirCache) Get(path string) (id string, ok bool)
- func (dc *DirCache) GetInv(id string) (path string, ok bool)
- func (dc *DirCache) Put(path, id string)
- func (dc *DirCache) ResetRoot()
- func (dc *DirCache) RootID(ctx context.Context, create bool) (ID string, err error)
- func (dc *DirCache) RootParentID(ctx context.Context, create bool) (ID string, err error)
- func (dc *DirCache) SetRootIDAlias(rootID string)
- func (dc *DirCache) String() string
- type DirCacher
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type DirCache ¶
type DirCache struct {
// contains filtered or unexported fields
}
DirCache caches paths to directory IDs and vice versa
func New ¶
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 ¶
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) FlushDir ¶
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) Get ¶
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 ¶
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) ResetRoot ¶
func (dc *DirCache) ResetRoot()
ResetRoot resets the root directory to the absolute root and clears the DirCache
func (*DirCache) RootID ¶
RootID returns the ID of the root directory
If create is set it will make the root directory if not found
func (*DirCache) RootParentID ¶
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 ¶
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.
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.