dircache

package
v1.51.1-0...-e186a28 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2020 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package dircache provides a simple cache for caching directory to path lookups

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

The cache is safe for concurrent use

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

Algorithm:

Look in the cache for the path, if found return the pathID
If not found strip the last path off the path and recurse
Now have a parent directory id, so look in the parent for self and return it

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

Do not call FindPath with the root directory - it will return an error

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

func (*DirCache) FindRoot

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

FindRoot finds the root directory if not already found

Resets the root directory

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

func (*DirCache) FindRootAndPath

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

FindRootAndPath finds the root first if not found then finds leaf and directoryID from a path

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

func (*DirCache) Flush

func (dc *DirCache) Flush()

Flush the map of all data

func (*DirCache) FlushDir

func (dc *DirCache) FlushDir(dir string)

FlushDir flushes the map of all data starting with dir

If dir is empty 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

Call this from FindLeaf or CreateDir only

func (*DirCache) Get

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

Get an ID given a path

func (*DirCache) GetInv

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

GetInv gets a path given an ID

func (*DirCache) Put

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

Put a path, id into the map

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() string

RootID returns the ID of the root directory

This should be called after FindRoot

func (*DirCache) RootParentID

func (dc *DirCache) RootParentID() (string, error)

RootParentID returns the ID of the parent of the root directory

This should be called after FindRoot

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

Jump to

Keyboard shortcuts

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