storage

package
v0.0.0-...-1538d9d Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2021 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const LocalStorageSize = 100

Variables

View Source
var FILENOTFOUND = errors.New("P2PFile not in storage")

Functions

This section is empty.

Types

type Chunk

type Chunk struct {
	Id ChunkID
	B  []byte
}

Chunk represents a chunk of content

func FileDataToChunks

func FileDataToChunks(fileData []byte, chunkSize int) (chunks []Chunk)

FileDataToChunks: Transform array of bytes (content of a file) into Chunk's array with 'chunkSize' size.

func NewChunkFromData

func NewChunkFromData(id ChunkID, size int, data []byte) Chunk

func (*Chunk) ID

func (c *Chunk) ID() ChunkID

type ChunkID

type ChunkID uint32

ChunkID identifies a chunk of content

type ChunkIDs

type ChunkIDs []ChunkID

TODO: modify Sort

func (ChunkIDs) Len

func (a ChunkIDs) Len() int

func (ChunkIDs) Less

func (a ChunkIDs) Less(i, j int) bool

func (ChunkIDs) Swap

func (a ChunkIDs) Swap(i, j int)

type FileHash

type FileHash []byte

FileHash:

func NewHashFromFile

func NewHashFromFile(data []byte) FileHash

NewHashFromFile: Create a New Hashfrom file's content with SHA256

func (FileHash) Decode

func (h FileHash) Decode() string

func (FileHash) Eq

func (h FileHash) Eq(lhs FileHash) bool

Eq: Compare two FileHash

func (FileHash) String

func (h FileHash) String() string

String: Return string representation from FileHash

type FileHashTmp

type FileHashTmp int

func (FileHashTmp) String

func (f FileHashTmp) String() string

type FileID

type FileID interface {
	String() string
}

type FileState

type FileState int

FileState: State of a file, representing if it's complete, updated or unchanged

const (
	FSComplete  FileState = 0
	FSUpdated   FileState = 1
	FSUnchanged FileState = 2
)

TODO

type LocalStorage

type LocalStorage interface {
	AddFile(fileData []byte) (FileID, error)
	AddReceivedFileChunks(hash FileID, chunks []Chunk) error
	GetRequestedChunks(hash FileID, ids []ChunkID) ([]Chunk, error)
	GetChunkIDsInStorage(hash FileID) ([]ChunkID, error)
	GetFileData(hash FileID) ([]byte, error)
	DeleteData(hash FileID) error
}

func NewP2PStorage

func NewP2PStorage() LocalStorage

type LocalStorageConfig

type LocalStorageConfig struct {
	ChunkSize int
}

type P2PFile

type P2PFile struct {
	Hash   FileHash
	State  FileState
	Data   []byte
	Chunks map[ChunkID]Chunk
}

P2PFile: Represent a P2PFile with all basic file infos.

  • Hash: unique ID for the file based on file content
  • state: FileState representing if the file is complete, updated or unchanged.
  • Data: File content formed with chunks' data. Used to avoid excess data merging when requesting data.
  • Chunks: All stored Chunks accesible by ChunkID.

func NewFile

func NewFile(hash FileHash, state FileState, fileData []byte, chunkSize int) P2PFile

TODO

func (*P2PFile) AddChunks

func (f *P2PFile) AddChunks(chunks []Chunk) error

TODO

func (P2PFile) Complete

func (f P2PFile) Complete() bool

Complete: Check P2PFile state, return true if file is completed.

func (*P2PFile) DeleteData

func (f *P2PFile) DeleteData()

DeleteData: Delete file Data to free ressources

func (P2PFile) GetChunksIDs

func (f P2PFile) GetChunksIDs() []ChunkID

GetChunksIDs: Return all ChunkID's in a P2PFile.

func (*P2PFile) GetData

func (f *P2PFile) GetData() []byte

GetData: Return stored data in a P2PFile. if the file is completed or unchanged since last call to GetData, directly return stored data. otherwise (FileState = FSUpdated), call UpdateData to reform data from received Chunk.

func (P2PFile) GetRequestedChunks

func (f P2PFile) GetRequestedChunks(ids []ChunkID) []Chunk

func (*P2PFile) UpdateData

func (f *P2PFile) UpdateData()

TODO:

type P2PFileStorage

type P2PFileStorage struct {
	Haves    map[PeerID][]ChunkID
	FileSize int
}

type P2PRemoteStorage

type P2PRemoteStorage map[string]P2PFileStorage

func NewP2PRemoteStorage

func NewP2PRemoteStorage() *P2PRemoteStorage

func (P2PRemoteStorage) AddFileChunksForPeer

func (s P2PRemoteStorage) AddFileChunksForPeer(peer PeerID, hash FileID, chunkIDS []ChunkID, FileSize int) error

func (P2PRemoteStorage) GetFileSize

func (s P2PRemoteStorage) GetFileSize(hash FileID) (int, error)

func (P2PRemoteStorage) GetPeersFileChunks

func (s P2PRemoteStorage) GetPeersFileChunks(hash FileID) (map[PeerID][]ChunkID, error)

type P2PStorage

type P2PStorage struct {
	// Map in not thread safe by design, so we have to use a mutex
	sync.Mutex
	Config     LocalStorageConfig
	LocalFiles map[string]P2PFile
}

func (*P2PStorage) AddFile

func (s *P2PStorage) AddFile(fileData []byte) (FileID, error)

AddFile Add a file to local storage. Return the hashed file when successfull

func (*P2PStorage) AddReceivedFileChunks

func (s *P2PStorage) AddReceivedFileChunks(hash FileID, chunks []Chunk) error

AddFile Add a file to local storage. Return the hashed file when successfull

func (*P2PStorage) DeleteData

func (s *P2PStorage) DeleteData(hash FileID) error

func (*P2PStorage) GetChunkIDsInStorage

func (s *P2PStorage) GetChunkIDsInStorage(hash FileID) ([]ChunkID, error)

func (*P2PStorage) GetFileData

func (s *P2PStorage) GetFileData(hash FileID) ([]byte, error)

GetChunkIDsInStorage Search for requested chunk with file Hash

func (*P2PStorage) GetRequestedChunks

func (s *P2PStorage) GetRequestedChunks(hash FileID, ids []ChunkID) ([]Chunk, error)

GetChunkIDsInStorage Search for requested chunk with file Hash

type PeerID

type PeerID interface {
	String() string
}

PeerID identifies a peer

type PeerStorage

type PeerStorage interface {
	AddFileChunksForPeer(peer PeerID, hash FileID, chunkIDS []ChunkID, FileSize int) error
	GetPeersFileChunks(hash FileID) (map[PeerID][]ChunkID, error)
	GetFileSize(hash FileID) (int, error)
}

Jump to

Keyboard shortcuts

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