2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-05-16 13:40:44 +01:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package pstore
|
|
|
|
|
|
|
|
import (
|
2018-06-19 16:59:09 +01:00
|
|
|
"context"
|
2018-06-04 22:30:42 +01:00
|
|
|
"io"
|
2018-05-16 13:40:44 +01:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2019-01-23 10:39:03 +00:00
|
|
|
"github.com/shirou/gopsutil/disk"
|
2018-05-16 13:40:44 +01:00
|
|
|
"github.com/zeebo/errs"
|
2018-05-31 20:42:45 +01:00
|
|
|
|
2018-06-04 22:30:42 +01:00
|
|
|
"storj.io/storj/pkg/ranger"
|
2018-05-16 13:40:44 +01:00
|
|
|
)
|
|
|
|
|
2019-01-11 11:26:39 +00:00
|
|
|
// Storage stores piecestore pieces
|
|
|
|
type Storage struct {
|
|
|
|
dir string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewStorage creates database for storing pieces
|
|
|
|
func NewStorage(dir string) *Storage {
|
|
|
|
return &Storage{dir}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes resources
|
|
|
|
func (storage *Storage) Close() error { return nil }
|
|
|
|
|
2019-01-23 10:39:03 +00:00
|
|
|
// DiskInfo contains statistics about the disk
|
|
|
|
type DiskInfo struct {
|
|
|
|
AvailableSpace int64 // TODO: use memory.Size
|
|
|
|
}
|
|
|
|
|
|
|
|
// Info returns information about the current state of the dir
|
|
|
|
func (storage *Storage) Info() (DiskInfo, error) {
|
|
|
|
rootPath := filepath.Dir(filepath.Clean(storage.dir))
|
|
|
|
diskSpace, err := disk.Usage(rootPath)
|
|
|
|
if err != nil {
|
|
|
|
return DiskInfo{}, err
|
|
|
|
}
|
|
|
|
return DiskInfo{
|
|
|
|
AvailableSpace: int64(diskSpace.Free),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
Added rpc server (#40)
* Added piecestore
* gofmt
* Added requested changes
* Added cli
* Removed ranger because I wanted something that can stand alone
* Add example of http server using piece store
* Changed piecestore code to make it more optial for error handelling
* Merged with piecestore
* Added missing package
* Forgot io import
* gofmt
* gofmt
* Forgot io
* Make path by hash exported
* updated to simplify again whoops
* Updated server to work real good
* Forgot ampersand
* Updated to match FilePiece
* Merged in cam's delete code
* Remove unused io
* Added RPC code
* Give the download request a reader
* Removed http server stuff; changed receive stream to say io.reader
* Added expiration date to shardInfo
* Change all instances of Shard to Piece; change protobuf name; moved client insance to outside functions
* added ttl info request
* Move scripts to http server pr; added close method for Retrieve api
* added rpc server tests for getting piece meta data and retrieval routes
* Resolved linter errors, moved to prc server to pkg, updated go.mod to use latest protobuf
* Imported cams test
* Bump gometalinter deadline
* WIP adding tests
* added tests for store and delete routes
* Add changes as requested by Kaloyan, also cleaned up some code
* Get the code actually working whoops
* More cleanup
* Separating database calls from api.go
* need to rename expiration
* Added some changes requested by JT
* Fix read size
* Fixed total amount to read
* added tests
* Simplify protobuf, add store tests, edited api to handle invalid stores properly, return errors instead of messages
* Moved rpc client and server to piece store
* Moved piecestore protobuf to the /protos folder
* Cleaned up messages
* Clean up linter errors
* Added missing sqlite import
* Add ability to do iterative reads and writes to pstore
* Incrementally read data
* Fix linter and import errors
* Solve linter Error
* Change return types
* begin test refactor
* refactored to implement only 1 db connection, moved SQLite row checking into separate function and removed defer on rows.Close(), fixed os.tempDir in rpc_test.go
* Cleaning up tests
* Added retrieve tests
* refactored delete tests
* Deleted old tests
* Updated cmd/piecestore to reflect changes to piecestore
* Refactored server tests and server/client store code
* gofmt
* WIP implementing TTL struct
* Read 4k at a time when Retrieving
* implemented ttl struct
* Accidentally removed fpiece dependency?
* Double resolve merge conflict
* Moved client to the examples since it is an example
* Change hash to id in protobuf. TODO: Change client and server to reflect these changes
* Update client and protobuf
* changed hash to id
* Handle eof properly in retrieve
* Id -> ID
* forgot to change import for client after moving
* Moved client and server main to examples
* Made changes requested by JT
* checkEntries is now private, created currentTime variable for checkEntries, added defer rows.Close()
* Print not fatal
* Handle overflow when reading from server
* added const IDLength
* removed types from comments
* Add reader/writer for download data from and uploading data to server
* goimports and comments
* fixed nits, casts, added OK const, DBCleanup now exits program on error
* Add stream reader and writer for server
* Fix errors
* i beforee except after c lol
* customizable data dir
* Forgot to rename variable
* customizable data dir
* Handle closing of server stream properly
* linter
* pass on inserting the same data twice
* linter
* linter
* Do the easy things JT asked for
* Handle overflow from reads properly; handle custom db path
* Handle overflow for server stream read; TODO Combine server and client stream reads
* Allow for TTL of 0 to stay forever
* Make Client cleaner for user
2018-06-02 19:14:59 +01:00
|
|
|
// IDLength -- Minimum ID length
|
|
|
|
const IDLength = 20
|
|
|
|
|
2018-05-16 13:40:44 +01:00
|
|
|
// Errors
|
|
|
|
var (
|
2019-01-11 11:26:39 +00:00
|
|
|
Error = errs.Class("piecestore error")
|
2019-01-25 18:05:21 +00:00
|
|
|
MkDir = errs.Class("piecestore MkdirAll")
|
|
|
|
Open = errs.Class("piecestore OpenFile")
|
2018-05-16 13:40:44 +01:00
|
|
|
)
|
|
|
|
|
2019-01-11 11:26:39 +00:00
|
|
|
// PiecePath creates piece storage path from id and dir
|
|
|
|
func (storage *Storage) PiecePath(pieceID string) (string, error) {
|
|
|
|
if len(pieceID) < IDLength {
|
|
|
|
return "", Error.New("invalid id length")
|
2018-05-16 13:40:44 +01:00
|
|
|
}
|
2019-01-11 11:26:39 +00:00
|
|
|
folder1, folder2, filename := pieceID[0:2], pieceID[2:4], pieceID[4:]
|
|
|
|
return filepath.Join(storage.dir, folder1, folder2, filename), nil
|
2018-05-16 13:40:44 +01:00
|
|
|
}
|
|
|
|
|
2019-01-11 11:26:39 +00:00
|
|
|
// Writer returns a writer that can be used to store piece.
|
|
|
|
func (storage *Storage) Writer(pieceID string) (io.WriteCloser, error) {
|
|
|
|
path, err := storage.PiecePath(pieceID)
|
2018-05-16 13:40:44 +01:00
|
|
|
if err != nil {
|
Added rpc server (#40)
* Added piecestore
* gofmt
* Added requested changes
* Added cli
* Removed ranger because I wanted something that can stand alone
* Add example of http server using piece store
* Changed piecestore code to make it more optial for error handelling
* Merged with piecestore
* Added missing package
* Forgot io import
* gofmt
* gofmt
* Forgot io
* Make path by hash exported
* updated to simplify again whoops
* Updated server to work real good
* Forgot ampersand
* Updated to match FilePiece
* Merged in cam's delete code
* Remove unused io
* Added RPC code
* Give the download request a reader
* Removed http server stuff; changed receive stream to say io.reader
* Added expiration date to shardInfo
* Change all instances of Shard to Piece; change protobuf name; moved client insance to outside functions
* added ttl info request
* Move scripts to http server pr; added close method for Retrieve api
* added rpc server tests for getting piece meta data and retrieval routes
* Resolved linter errors, moved to prc server to pkg, updated go.mod to use latest protobuf
* Imported cams test
* Bump gometalinter deadline
* WIP adding tests
* added tests for store and delete routes
* Add changes as requested by Kaloyan, also cleaned up some code
* Get the code actually working whoops
* More cleanup
* Separating database calls from api.go
* need to rename expiration
* Added some changes requested by JT
* Fix read size
* Fixed total amount to read
* added tests
* Simplify protobuf, add store tests, edited api to handle invalid stores properly, return errors instead of messages
* Moved rpc client and server to piece store
* Moved piecestore protobuf to the /protos folder
* Cleaned up messages
* Clean up linter errors
* Added missing sqlite import
* Add ability to do iterative reads and writes to pstore
* Incrementally read data
* Fix linter and import errors
* Solve linter Error
* Change return types
* begin test refactor
* refactored to implement only 1 db connection, moved SQLite row checking into separate function and removed defer on rows.Close(), fixed os.tempDir in rpc_test.go
* Cleaning up tests
* Added retrieve tests
* refactored delete tests
* Deleted old tests
* Updated cmd/piecestore to reflect changes to piecestore
* Refactored server tests and server/client store code
* gofmt
* WIP implementing TTL struct
* Read 4k at a time when Retrieving
* implemented ttl struct
* Accidentally removed fpiece dependency?
* Double resolve merge conflict
* Moved client to the examples since it is an example
* Change hash to id in protobuf. TODO: Change client and server to reflect these changes
* Update client and protobuf
* changed hash to id
* Handle eof properly in retrieve
* Id -> ID
* forgot to change import for client after moving
* Moved client and server main to examples
* Made changes requested by JT
* checkEntries is now private, created currentTime variable for checkEntries, added defer rows.Close()
* Print not fatal
* Handle overflow when reading from server
* added const IDLength
* removed types from comments
* Add reader/writer for download data from and uploading data to server
* goimports and comments
* fixed nits, casts, added OK const, DBCleanup now exits program on error
* Add stream reader and writer for server
* Fix errors
* i beforee except after c lol
* customizable data dir
* Forgot to rename variable
* customizable data dir
* Handle closing of server stream properly
* linter
* pass on inserting the same data twice
* linter
* linter
* Do the easy things JT asked for
* Handle overflow from reads properly; handle custom db path
* Handle overflow for server stream read; TODO Combine server and client stream reads
* Allow for TTL of 0 to stay forever
* Make Client cleaner for user
2018-06-02 19:14:59 +01:00
|
|
|
return nil, err
|
2018-05-16 13:40:44 +01:00
|
|
|
}
|
2019-01-11 11:26:39 +00:00
|
|
|
if err = os.MkdirAll(filepath.Dir(path), 0700); err != nil {
|
2019-01-25 18:05:21 +00:00
|
|
|
return nil, MkDir.Wrap(err)
|
2018-05-16 13:40:44 +01:00
|
|
|
}
|
2019-01-11 11:26:39 +00:00
|
|
|
file, err := os.OpenFile(path, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0600)
|
|
|
|
if err != nil {
|
2019-01-25 18:05:21 +00:00
|
|
|
return nil, Open.Wrap(err)
|
2019-01-11 11:26:39 +00:00
|
|
|
}
|
|
|
|
return file, nil
|
2018-05-16 13:40:44 +01:00
|
|
|
}
|
|
|
|
|
2019-01-11 11:26:39 +00:00
|
|
|
// Reader returns a reader for the specified piece at the location
|
|
|
|
func (storage *Storage) Reader(ctx context.Context, pieceID string, offset int64, length int64) (io.ReadCloser, error) {
|
|
|
|
path, err := storage.PiecePath(pieceID)
|
2018-05-16 13:40:44 +01:00
|
|
|
if err != nil {
|
Added rpc server (#40)
* Added piecestore
* gofmt
* Added requested changes
* Added cli
* Removed ranger because I wanted something that can stand alone
* Add example of http server using piece store
* Changed piecestore code to make it more optial for error handelling
* Merged with piecestore
* Added missing package
* Forgot io import
* gofmt
* gofmt
* Forgot io
* Make path by hash exported
* updated to simplify again whoops
* Updated server to work real good
* Forgot ampersand
* Updated to match FilePiece
* Merged in cam's delete code
* Remove unused io
* Added RPC code
* Give the download request a reader
* Removed http server stuff; changed receive stream to say io.reader
* Added expiration date to shardInfo
* Change all instances of Shard to Piece; change protobuf name; moved client insance to outside functions
* added ttl info request
* Move scripts to http server pr; added close method for Retrieve api
* added rpc server tests for getting piece meta data and retrieval routes
* Resolved linter errors, moved to prc server to pkg, updated go.mod to use latest protobuf
* Imported cams test
* Bump gometalinter deadline
* WIP adding tests
* added tests for store and delete routes
* Add changes as requested by Kaloyan, also cleaned up some code
* Get the code actually working whoops
* More cleanup
* Separating database calls from api.go
* need to rename expiration
* Added some changes requested by JT
* Fix read size
* Fixed total amount to read
* added tests
* Simplify protobuf, add store tests, edited api to handle invalid stores properly, return errors instead of messages
* Moved rpc client and server to piece store
* Moved piecestore protobuf to the /protos folder
* Cleaned up messages
* Clean up linter errors
* Added missing sqlite import
* Add ability to do iterative reads and writes to pstore
* Incrementally read data
* Fix linter and import errors
* Solve linter Error
* Change return types
* begin test refactor
* refactored to implement only 1 db connection, moved SQLite row checking into separate function and removed defer on rows.Close(), fixed os.tempDir in rpc_test.go
* Cleaning up tests
* Added retrieve tests
* refactored delete tests
* Deleted old tests
* Updated cmd/piecestore to reflect changes to piecestore
* Refactored server tests and server/client store code
* gofmt
* WIP implementing TTL struct
* Read 4k at a time when Retrieving
* implemented ttl struct
* Accidentally removed fpiece dependency?
* Double resolve merge conflict
* Moved client to the examples since it is an example
* Change hash to id in protobuf. TODO: Change client and server to reflect these changes
* Update client and protobuf
* changed hash to id
* Handle eof properly in retrieve
* Id -> ID
* forgot to change import for client after moving
* Moved client and server main to examples
* Made changes requested by JT
* checkEntries is now private, created currentTime variable for checkEntries, added defer rows.Close()
* Print not fatal
* Handle overflow when reading from server
* added const IDLength
* removed types from comments
* Add reader/writer for download data from and uploading data to server
* goimports and comments
* fixed nits, casts, added OK const, DBCleanup now exits program on error
* Add stream reader and writer for server
* Fix errors
* i beforee except after c lol
* customizable data dir
* Forgot to rename variable
* customizable data dir
* Handle closing of server stream properly
* linter
* pass on inserting the same data twice
* linter
* linter
* Do the easy things JT asked for
* Handle overflow from reads properly; handle custom db path
* Handle overflow for server stream read; TODO Combine server and client stream reads
* Allow for TTL of 0 to stay forever
* Make Client cleaner for user
2018-06-02 19:14:59 +01:00
|
|
|
return nil, err
|
2018-05-16 13:40:44 +01:00
|
|
|
}
|
2019-01-11 11:26:39 +00:00
|
|
|
info, err := os.Stat(path)
|
2018-05-16 13:40:44 +01:00
|
|
|
if err != nil {
|
Added rpc server (#40)
* Added piecestore
* gofmt
* Added requested changes
* Added cli
* Removed ranger because I wanted something that can stand alone
* Add example of http server using piece store
* Changed piecestore code to make it more optial for error handelling
* Merged with piecestore
* Added missing package
* Forgot io import
* gofmt
* gofmt
* Forgot io
* Make path by hash exported
* updated to simplify again whoops
* Updated server to work real good
* Forgot ampersand
* Updated to match FilePiece
* Merged in cam's delete code
* Remove unused io
* Added RPC code
* Give the download request a reader
* Removed http server stuff; changed receive stream to say io.reader
* Added expiration date to shardInfo
* Change all instances of Shard to Piece; change protobuf name; moved client insance to outside functions
* added ttl info request
* Move scripts to http server pr; added close method for Retrieve api
* added rpc server tests for getting piece meta data and retrieval routes
* Resolved linter errors, moved to prc server to pkg, updated go.mod to use latest protobuf
* Imported cams test
* Bump gometalinter deadline
* WIP adding tests
* added tests for store and delete routes
* Add changes as requested by Kaloyan, also cleaned up some code
* Get the code actually working whoops
* More cleanup
* Separating database calls from api.go
* need to rename expiration
* Added some changes requested by JT
* Fix read size
* Fixed total amount to read
* added tests
* Simplify protobuf, add store tests, edited api to handle invalid stores properly, return errors instead of messages
* Moved rpc client and server to piece store
* Moved piecestore protobuf to the /protos folder
* Cleaned up messages
* Clean up linter errors
* Added missing sqlite import
* Add ability to do iterative reads and writes to pstore
* Incrementally read data
* Fix linter and import errors
* Solve linter Error
* Change return types
* begin test refactor
* refactored to implement only 1 db connection, moved SQLite row checking into separate function and removed defer on rows.Close(), fixed os.tempDir in rpc_test.go
* Cleaning up tests
* Added retrieve tests
* refactored delete tests
* Deleted old tests
* Updated cmd/piecestore to reflect changes to piecestore
* Refactored server tests and server/client store code
* gofmt
* WIP implementing TTL struct
* Read 4k at a time when Retrieving
* implemented ttl struct
* Accidentally removed fpiece dependency?
* Double resolve merge conflict
* Moved client to the examples since it is an example
* Change hash to id in protobuf. TODO: Change client and server to reflect these changes
* Update client and protobuf
* changed hash to id
* Handle eof properly in retrieve
* Id -> ID
* forgot to change import for client after moving
* Moved client and server main to examples
* Made changes requested by JT
* checkEntries is now private, created currentTime variable for checkEntries, added defer rows.Close()
* Print not fatal
* Handle overflow when reading from server
* added const IDLength
* removed types from comments
* Add reader/writer for download data from and uploading data to server
* goimports and comments
* fixed nits, casts, added OK const, DBCleanup now exits program on error
* Add stream reader and writer for server
* Fix errors
* i beforee except after c lol
* customizable data dir
* Forgot to rename variable
* customizable data dir
* Handle closing of server stream properly
* linter
* pass on inserting the same data twice
* linter
* linter
* Do the easy things JT asked for
* Handle overflow from reads properly; handle custom db path
* Handle overflow for server stream read; TODO Combine server and client stream reads
* Allow for TTL of 0 to stay forever
* Make Client cleaner for user
2018-06-02 19:14:59 +01:00
|
|
|
return nil, err
|
2018-05-16 13:40:44 +01:00
|
|
|
}
|
2019-01-11 11:26:39 +00:00
|
|
|
if offset >= info.Size() || offset < 0 {
|
|
|
|
return nil, Error.New("invalid offset: %v", offset)
|
2018-05-16 13:40:44 +01:00
|
|
|
}
|
|
|
|
if length <= -1 {
|
2019-01-11 11:26:39 +00:00
|
|
|
length = info.Size()
|
2018-05-16 13:40:44 +01:00
|
|
|
}
|
|
|
|
// If trying to read past the end of the file, just read to the end
|
2019-01-11 11:26:39 +00:00
|
|
|
if info.Size() < offset+length {
|
|
|
|
length = info.Size() - offset
|
2018-05-16 13:40:44 +01:00
|
|
|
}
|
2019-01-11 11:26:39 +00:00
|
|
|
rr, err := ranger.FileRanger(path)
|
2018-06-04 22:30:42 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-06-19 16:59:09 +01:00
|
|
|
return rr.Range(ctx, offset, length)
|
2018-05-16 13:40:44 +01:00
|
|
|
}
|
|
|
|
|
2019-01-11 11:26:39 +00:00
|
|
|
// Delete deletes piece from storage
|
|
|
|
func (storage *Storage) Delete(pieceID string) error {
|
|
|
|
path, err := storage.PiecePath(pieceID)
|
2018-05-16 13:40:44 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-11 11:26:39 +00:00
|
|
|
err = os.Remove(path)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
err = nil
|
2018-05-16 13:40:44 +01:00
|
|
|
}
|
2019-01-11 11:26:39 +00:00
|
|
|
return err
|
2018-05-16 13:40:44 +01:00
|
|
|
}
|