dxdir

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2020 License: Apache-2.0 Imports: 13 Imported by: 2

Documentation

Index

Examples

Constants

View Source
const (
	DirFileName = ".dxdir"

	DefaultHealth = uint32(200)
)

Variables

View Source
var (
	// ErrAlreadyDeleted is the error that happens when save or delete a DxDir
	// that is already deleted
	ErrAlreadyDeleted = errors.New("DxDir has already been deleted")

	// ErrUploadDirectory indicates that we can't upload directory
	ErrUploadDirectory = errors.New("cannot upload directory")

	// ErrPathOverload is an error when a file already exists at that location
	ErrPathOverload = errors.New("a file already exists at that location")

	// ErrUnknownPath is an error when a file cannot be found with the given path
	ErrUnknownPath = errors.New("no file known with that path")
)

Functions

This section is empty.

Types

type DirSet

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

DirSet is the manager of all DxDirs

Example

ExampleDirSet shows an example usage of DirSet

package main

import (
	"fmt"
	"path/filepath"
	"reflect"

	"github.com/DxChainNetwork/godx/common/writeaheadlog"
	"github.com/DxChainNetwork/godx/storage"
)

var exampleDirSetDir = tempDir("example")

// ExampleDirSet shows an example usage of DirSet
func main() {
	// initialize
	ds, err := NewDirSet(exampleDirSetDir, newExampleWal())
	path := randomDxPath(2)
	entry, err := ds.NewDxDir(path)
	if err != nil {
		fmt.Println(err)
	}
	// create a random metadata and update
	newMeta := randomMetadata()
	// note the DxPath field is not updated
	newMeta.DxPath = path
	newMeta.RootPath = storage.SysPath(exampleDirSetDir)
	err = entry.UpdateMetadata(*newMeta)
	if err != nil {
		fmt.Println(err)
	}
	// Close the entry
	err = entry.Close()
	if err != nil {
		fmt.Println(err)
	}
	// Reopen the entry
	newEntry, err := ds.Open(path)
	if err != nil {
		fmt.Println(err)
	}
	newEntry.metadata.TimeModify = 0
	newMeta.TimeModify = 0
	if !reflect.DeepEqual(*newEntry.metadata, *newMeta) {
		fmt.Printf("After open, metadata not equal: \n\tExpect %+v\n\tGot %+v", newMeta, newEntry.metadata)
	}
}

// newExampleWal create a new wal for the example
func newExampleWal() *writeaheadlog.Wal {
	wal, txns, err := writeaheadlog.New(filepath.Join(string(exampleDirSetDir), "example.wal"))
	if err != nil {
		fmt.Println(err)
	}
	for _, txn := range txns {
		err := txn.Release()
		if err != nil {
			panic(err)
		}
	}
	return wal
}
Output:

func NewDirSet

func NewDirSet(rootDir storage.SysPath, wal *writeaheadlog.Wal) (*DirSet, error)

NewDirSet creates a New DirSet with the given parameters. If the root DxDir not exist, create a new DxDir

func (*DirSet) Delete

func (ds *DirSet) Delete(path storage.DxPath) error

Delete delete the dxdir. If file not exist, return os.ErrNotExist

func (*DirSet) Exists

func (ds *DirSet) Exists(path storage.DxPath) bool

Exists checks whether DxDir with path exists. If file not exist, return an os File Not Exist error

func (*DirSet) NewDxDir

func (ds *DirSet) NewDxDir(path storage.DxPath) (*DirSetEntryWithID, error)

NewDxDir creates a DxDir. Return a DirSetEntryWithID that extends DxDir and the error

func (*DirSet) Open

func (ds *DirSet) Open(path storage.DxPath) (*DirSetEntryWithID, error)

Open opens a New DxDir. If file not exist, return an os file Not Exist error

func (*DirSet) UpdateMetadata

func (ds *DirSet) UpdateMetadata(path storage.DxPath, metadata Metadata) error

UpdateMetadata update the metadata of the dxdir specified by DxPath

type DirSetEntryWithID

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

DirSetEntryWithID is the entry with the threadID. It extends DxDir

func (*DirSetEntryWithID) Close

func (entry *DirSetEntryWithID) Close() error

Close close the entry. If all threads with the entry is closed, remove the entry from the DirSet

type DxDir

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

DxDir is the data structure for the directory for the meta info for a directory.

func New

func New(dxPath storage.DxPath, rootPath storage.SysPath, wal *writeaheadlog.Wal) (*DxDir, error)

New create a DxDir with representing the dirPath metadata. Note that the only access method should be from dirSet

func (*DxDir) DecodeRLP

func (d *DxDir) DecodeRLP(st *rlp.Stream) error

DecodeRLP define the RLP decode rule for DxDir. Only metadata is decoded.

func (*DxDir) Delete

func (d *DxDir) Delete() error

Delete delete the dxfile

func (*DxDir) Deleted

func (d *DxDir) Deleted() bool

Deleted return the delete status

func (*DxDir) DxPath

func (d *DxDir) DxPath() storage.DxPath

DxPath return the DxPath of the Dxdir

func (*DxDir) EncodeRLP

func (d *DxDir) EncodeRLP(w io.Writer) error

EncodeRLP define the RLP rule for DxDir. Only the metadata is RLP encoded.

func (*DxDir) FilePath

func (d *DxDir) FilePath() string

filePath return the actual dxdir file path of a dxdir.

func (*DxDir) Metadata

func (d *DxDir) Metadata() Metadata

Metadata return the copy of the Metadata

func (*DxDir) UpdateMetadata

func (d *DxDir) UpdateMetadata(metadata Metadata) error

UpdateMetadata update the metadata with the given metadata. Not the DxPath field is not updated

type Metadata

type Metadata struct {
	// Total number of files in directory and its subdirectories
	NumFiles uint64

	// Total size of the directory and its subdirectories
	TotalSize uint64

	// Health is the min Health all files and subdirectories
	Health uint32

	// StuckHealth is the min StuckHealth for all files and subdirectories
	StuckHealth uint32

	// MinRedundancy is the minimum redundancy
	MinRedundancy uint32

	// TimeLastHealthCheck is the last health check time
	TimeLastHealthCheck uint64

	// TimeModify is the last content modification time
	TimeModify uint64

	// NumStuckSegments is the total number of segments that is stuck
	NumStuckSegments uint32

	// DxPath is the DxPath which is the path related to the root directory
	DxPath storage.DxPath

	// RootPath is the root path of the file directory
	RootPath storage.SysPath
}

Metadata is the necessary metadata to be saved in DxDir

Jump to

Keyboard shortcuts

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