core

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2018 License: AGPL-3.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// MoveDirUnknown should only be used for init purposes.
	MoveDirUnknown = iota
	// MoveDirSrcToDst means that this file was moved from source to dest.
	// (Therefore it is the new destination file and probably not a ghost)
	MoveDirSrcToDst
	// MoveDirDstToSrc means that this place was moved somewhere else.
	// (Therefore it is a likely a ghost and the new file lives somewhere else)
	MoveDirDstToSrc
	// MoveDirNone tells us that this file did not move.
	MoveDirNone
)

Variables

View Source
var (
	// ErrIsGhost is returned by Remove() when calling it on a ghost.
	ErrIsGhost = errors.New("Is a ghost")
)

Functions

func AssertDir

func AssertDir(t *testing.T, lkr *Linker, path string, shouldExist bool)

AssertDir asserts the existence of a directory.

func Copy

func Copy(lkr *Linker, nd n.ModNode, dstPath string) (newNode n.ModNode, err error)

Copy copies the node `nd` to the path at `dstPath`.

func Log

func Log(lkr *Linker, fn func(cmt *n.Commit) error) error

Log will call `fn` on every commit we currently have, starting with the most current one (CURR, then HEAD, ...). If `fn“ will return an error, the iteration is being stopped.

func Mkdir

func Mkdir(lkr *Linker, repoPath string, createParents bool) (dir *n.Directory, err error)

Mkdir creates the directory at repoPath and any intermediate directories if createParents is true. It will fail if there is already a file at `repoPath` and it is not a directory.

func Move

func Move(lkr *Linker, nd n.ModNode, dstPath string) error

Move moves the node `nd` to the path at `dstPath` and leaves a ghost at the old place.

func MustCommit

func MustCommit(t *testing.T, lkr *Linker, msg string) *n.Commit

MustCommit commits the current state with `msg`.

func MustCommitIfPossible

func MustCommitIfPossible(t *testing.T, lkr *Linker, msg string) *n.Commit

MustCommitIfPossible with is like MustCommit, but allows empty changesets.

func MustLookupDirectory

func MustLookupDirectory(t *testing.T, lkr *Linker, path string) *n.Directory

MustLookupDirectory loads an existing dir or fails.

func MustMkdir

func MustMkdir(t *testing.T, lkr *Linker, repoPath string) *n.Directory

MustMkdir creates a directory or fails on `t`.

func MustModify

func MustModify(t *testing.T, lkr *Linker, file *n.File, seed int)

MustModify changes the content of an existing node.

func MustMove

func MustMove(t *testing.T, lkr *Linker, nd n.ModNode, destPath string) n.ModNode

MustMove moves the node `nd` to `destPath` or fails `t`.

func MustRemove

func MustRemove(t *testing.T, lkr *Linker, nd n.ModNode) n.ModNode

MustRemove removes the node `nd` or fails.

func MustTouch

func MustTouch(t *testing.T, lkr *Linker, touchPath string, seed byte) *n.File

MustTouch creates a new node at `touchPath` and sets its content hash to a hash derived from `seed`.

func MustTouchAndCommit

func MustTouchAndCommit(t *testing.T, lkr *Linker, path string, seed byte) (*n.File, *n.Commit)

MustTouchAndCommit is a combined MustTouch and MustCommit.

func Remove

func Remove(lkr *Linker, nd n.ModNode, createGhost, force bool) (parentDir *n.Directory, ghost *n.Ghost, err error)

Remove removes a single node from a directory. `nd` is the node that shall be removed and may not be root. The parent directory is returned.

func Stage

func Stage(lkr *Linker, repoPath string, contentHash, backendHash h.Hash, size uint64, key []byte) (file *n.File, err error)

Stage adds a file to brigs DAG.

func StageFromFileNode

func StageFromFileNode(lkr *Linker, f *n.File) (*n.File, error)

StageFromFileNode is a convinience helper that will call Stage() with all necessary params from `f`.

func WithDummyKv

func WithDummyKv(t *testing.T, fn func(kv db.Database))

WithDummyKv creates a testing key value store and passes it to `fn`.

func WithDummyLinker

func WithDummyLinker(t *testing.T, fn func(lkr *Linker))

WithDummyLinker creates a testing linker and passes it to `fn`.

func WithLinkerPair

func WithLinkerPair(t *testing.T, fn func(lkrSrc, lkrDst *Linker))

WithLinkerPair creates two linkers, useful for testing syncing.

func WithReloadingLinker

func WithReloadingLinker(t *testing.T, fn1 func(lkr *Linker), fn2 func(lkr *Linker))

WithReloadingLinker creates a testing linker and passes it to `fn1`. It then closes the linker and lets it load a second time and passes it to `fn2`. This is useful to test persistency issues.

Types

type GarbageCollector

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

GarbageCollector implements a small mark & sweep garbage collector. It exists more for the sake of fault tolerance than it being an essential part of brig. This is different from the ipfs garbage collector.

func NewGarbageCollector

func NewGarbageCollector(lkr *Linker, kv db.Database, kc func(nd n.Node) bool) *GarbageCollector

NewGarbageCollector will return a new GC, operating on `lkr` and `kv`. It will call `kc` on every collected node.

func (*GarbageCollector) Run

func (gc *GarbageCollector) Run(allObjects bool) error

Run will trigger a GC run. If `allObjects` is false, only the staging commit will be checked. Otherwise all objects in the key value store.

type Linker

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

Linker implements the basic logic of brig's data model It uses an underlying key/value database to storea a Merkle-DAG with versioned metadata, similar to what git does internally.

func NewLinker

func NewLinker(kv db.Database) *Linker

NewLinker returns a new lkr, ready to use. It assumes the key value store is working and does no check on this.

func (*Linker) AddMoveMapping

func (lkr *Linker) AddMoveMapping(fromInode, toInode uint64) (err error)

AddMoveMapping takes note that the the node with `fromInode` has been moved to `toInode` in the staging commit.

func (*Linker) Atomic

func (lkr *Linker) Atomic(fn func() (bool, error)) (err error)

Atomic is like AtomicWithBatch but does not require using a batch. Use this for read-only operations. It's only syntactic sugar though.

func (*Linker) AtomicWithBatch

func (lkr *Linker) AtomicWithBatch(fn func(batch db.Batch) (bool, error)) (err error)

AtomicWithBatch will execute `fn` in one transaction. If anything goes wrong (i.e. `fn` returns an error)

func (*Linker) CheckoutCommit

func (lkr *Linker) CheckoutCommit(cmt *n.Commit, force bool) error

CheckoutCommit resets the current staging commit back to the commit referenced by cmt. If force is false, it will check if there any staged errors in the staging area and return ErrStageNotEmpty if there are any. If force is true, all changes will be overwritten.

func (*Linker) CommitByHash

func (lkr *Linker) CommitByHash(hash h.Hash) (*n.Commit, error)

CommitByHash lookups a commit by it's hash. If the commit could not be found, nil is returned.

func (*Linker) CommitByIndex

func (lkr *Linker) CommitByIndex(index int64) (*n.Commit, error)

CommitByIndex returns the commit referenced by `index`. `0` will return the very first commit. Negative numbers will yield a ErrNoSuchKey error.

func (*Linker) DirectoryByHash

func (lkr *Linker) DirectoryByHash(hash h.Hash) (*n.Directory, error)

DirectoryByHash calls NodeByHash and attempts to convert it to a Directory as convinience.

func (*Linker) ExpandAbbrev

func (lkr *Linker) ExpandAbbrev(abbrev string) (h.Hash, error)

ExpandAbbrev tries to find an object reference that stats with `abbrev`. If so, it will return the respective hash for it. If none is found, it is considered as an error. If more than one was found ie.ErrAmbigious is returned.

func (*Linker) FileByHash

func (lkr *Linker) FileByHash(hash h.Hash) (*n.File, error)

FileByHash calls NodeByHash and converts the result to a File.

func (*Linker) FilesByContents

func (lkr *Linker) FilesByContents(contents []h.Hash) (map[string]*n.File, error)

FilesByContents checks what files are associated with the content hashes in `contents`. It returns a map of content hash b58 to file. This method is quite heavy and should not be used in loops. There is room for optimizations.

func (*Linker) HaveStagedChanges

func (lkr *Linker) HaveStagedChanges() (bool, error)

HaveStagedChanges returns true if there were changes in the staging area. If an error occurs, the first return value is undefined.

func (*Linker) Head

func (lkr *Linker) Head() (*n.Commit, error)

Head is just a shortcut for ResolveRef("HEAD").

func (*Linker) IterAll

func (lkr *Linker) IterAll(from, to *n.Commit, fn func(n.ModNode, *n.Commit) error) error

IterAll goes over all nodes in the commit range `from` until (including) `to`. Already visited nodes will not be visited again if they did not change. If `from` is nil, HEAD is assumed. If `to` is nil, INIT is assumed.

func (*Linker) KV added in v0.2.0

func (lkr *Linker) KV() db.Database

KV returns the key value store passed when constructing the linker.

func (*Linker) ListRefs

func (lkr *Linker) ListRefs() ([]string, error)

ListRefs lists all currently known refs.

func (*Linker) LookupDirectory

func (lkr *Linker) LookupDirectory(repoPath string) (*n.Directory, error)

LookupDirectory calls LookupNode and converts the result to a Directory.

func (*Linker) LookupFile

func (lkr *Linker) LookupFile(repoPath string) (*n.File, error)

LookupFile calls LookupNode and converts the result to a file.

func (*Linker) LookupGhost

func (lkr *Linker) LookupGhost(repoPath string) (*n.Ghost, error)

LookupGhost calls LookupNode and converts the result to a ghost.

func (*Linker) LookupModNode

func (lkr *Linker) LookupModNode(repoPath string) (n.ModNode, error)

LookupModNode is like LookupNode but returns a readily cast ModNode.

func (*Linker) LookupModNodeAt

func (lkr *Linker) LookupModNodeAt(cmt *n.Commit, repoPath string) (n.ModNode, error)

LookupModNodeAt is like LookupNodeAt but with readily cast type.

func (*Linker) LookupNode

func (lkr *Linker) LookupNode(repoPath string) (n.Node, error)

LookupNode takes the root node and tries to resolve the path from there. Deleted paths are recognized in contrast to ResolveNode. If a path does not exist NoSuchFile is returned.

func (*Linker) LookupNodeAt

func (lkr *Linker) LookupNodeAt(cmt *n.Commit, repoPath string) (n.Node, error)

LookupNodeAt works like LookupNode but returns the node at the state of `cmt`.

func (*Linker) MakeCommit

func (lkr *Linker) MakeCommit(author string, message string) error

MakeCommit creates a new full commit in the version history. The current staging commit is finalized with `author` and `message` and gets saved. A new, identical staging commit is created pointing to the root of the now new HEAD.

If nothing changed since the last call to MakeCommit, it will return ErrNoChange, which can be reacted upon.

func (*Linker) MemIndexAdd

func (lkr *Linker) MemIndexAdd(nd n.Node, updatePathIndex bool)

MemIndexAdd adds `nd` to the in memory index.

func (*Linker) MemIndexClear

func (lkr *Linker) MemIndexClear()

MemIndexClear resets the memory index to zero. This should not be called mid-flight in operations, but should be okay to call between atomic operations.

func (*Linker) MemIndexPurge

func (lkr *Linker) MemIndexPurge(nd n.Node)

MemIndexPurge removes `nd` from the memory index.

func (*Linker) MemIndexSwap

func (lkr *Linker) MemIndexSwap(nd n.Node, oldHash h.Hash, updatePathIndex bool)

MemIndexSwap updates an entry of the in memory index, by deleting the old entry referenced by oldHash (may be nil). This is necessary to ensure that old hashes do not resolve to the new, updated instance. If the old instance is needed, it will be loaded as new instance. You should not need to call this function, except when implementing own Nodes.

func (*Linker) MemSetRoot

func (lkr *Linker) MemSetRoot(root *n.Directory)

MemSetRoot sets the current root, but does not store it yet. It's supposed to be called after in-memory modifications. Only implementors of new Nodes might need to call this function.

func (*Linker) MetadataGet

func (lkr *Linker) MetadataGet(key string) ([]byte, error)

MetadataGet retriesves a previously put key value pair. It will return nil if no such value could be retrieved.

func (*Linker) MetadataPut

func (lkr *Linker) MetadataPut(key string, value []byte) error

MetadataPut remembers a value persisntenly identified by `key`. It can be used as single-level key value store for user purposes.

func (*Linker) MoveEntryPoint

func (lkr *Linker) MoveEntryPoint(nd n.Node) (n.Node, MoveDir, error)

MoveEntryPoint tells us if a node participated in a move. If so, the new node and the corresponding move direction is returned.

func (*Linker) MoveMapping

func (lkr *Linker) MoveMapping(cmt *n.Commit, nd n.Node) (n.Node, MoveDir, error)

MoveMapping will lookup if the node pointed to by `nd` was part of a moving operation and if so, to what node it was moved and if it was the source or the dest node.

func (*Linker) NextInode

func (lkr *Linker) NextInode() uint64

NextInode returns a unique identifier, used to identify a single node. You should not need to call this function, except when implementing own nodes.

func (*Linker) NodeByHash

func (lkr *Linker) NodeByHash(hash h.Hash) (n.Node, error)

NodeByHash returns the node identified by hash. If no such hash could be found, nil is returned.

func (*Linker) NodeByInode

func (lkr *Linker) NodeByInode(uid uint64) (n.Node, error)

NodeByInode resolves a node by it's unique ID. It will return nil if no corresponding node was found.

func (*Linker) Owner

func (lkr *Linker) Owner() (string, error)

Owner returns the owner of the linker.

func (*Linker) RemoveRef

func (lkr *Linker) RemoveRef(refname string) error

RemoveRef removes the ref named `refname`.

func (*Linker) ResolveDirectory

func (lkr *Linker) ResolveDirectory(dirpath string) (*n.Directory, error)

ResolveDirectory calls ResolveNode and converts the result to a Directory. This only accesses nodes from the filesystem and does not differentiate between ghosts and living nodes.

func (*Linker) ResolveNode

func (lkr *Linker) ResolveNode(nodePath string) (n.Node, error)

ResolveNode resolves a path to a hash and resolves the corresponding node by calling NodeByHash(). If no node could be resolved, nil is returned. It does not matter if the node was deleted in the meantime. If so, a Ghost node is returned which stores the last known state.

func (*Linker) ResolveRef

func (lkr *Linker) ResolveRef(refname string) (n.Node, error)

ResolveRef resolves the hash associated with `refname`. If the ref could not be resolved, ErrNoSuchRef is returned. Typically, Node will be a Commit. But there are no technical restrictions on which node typ to use. NOTE: ResolveRef("HEAD") != ResolveRef("head") due to case.

func (*Linker) Root

func (lkr *Linker) Root() (*n.Directory, error)

Root returns the current root directory of CURR. It is never nil when err is nil.

func (*Linker) SaveRef

func (lkr *Linker) SaveRef(refname string, nd n.Node) error

SaveRef stores a reference to `nd` persistently. The caller is responsbiel to ensure that the node is already in the blockstore, otherwise it won't be resolvable.

func (*Linker) SetMergeMarker

func (lkr *Linker) SetMergeMarker(with string, remoteHead h.Hash) error

SetMergeMarker sets the current status to be a merge commit. Note that this function only will have a result when MakeCommit() is called afterwards. Otherwise, the changes will not be written to disk.

func (*Linker) SetOwner

func (lkr *Linker) SetOwner(owner string) error

SetOwner will set the owner to `owner`.

func (*Linker) StageNode

func (lkr *Linker) StageNode(nd n.Node) error

StageNode inserts a modified node to the staging area, making sure the modification is persistent and part of the staging commit. All parent directories of the node in question will be staged automatically. If there was no modification it will be a (quite expensive) NOOP.

func (*Linker) Status

func (lkr *Linker) Status() (*n.Commit, error)

Status returns the current staging commit. It is never nil, unless err is nil.

type MoveDir

type MoveDir int

MoveDir describes the direction of a move.

func (MoveDir) Invert

func (md MoveDir) Invert() MoveDir

Invert changes the direction of a move, if it has one.

func (MoveDir) String

func (md MoveDir) String() string

Jump to

Keyboard shortcuts

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