Skip to content

Commit

Permalink
repository: refactored the Chunker functionality into its own subpack…
Browse files Browse the repository at this point in the history
…age.
  • Loading branch information
cbrand committed Mar 25, 2015
1 parent bf766c6 commit 7f7502a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 12 deletions.
6 changes: 3 additions & 3 deletions repository/chunker.go → repository/chunker/chunker.go
@@ -1,4 +1,4 @@
package repository
package chunker

import (
"io"
Expand All @@ -13,9 +13,9 @@ type Chunker struct {
chunkSize uint64
}

// NewChunker returns a new chunker instance for the given file path
// New returns a new chunker instance for the given file path
// and the given chunk size.
func NewChunker(path string, chunkSize uint64) (*Chunker, error) {
func New(path string, chunkSize uint64) (*Chunker, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
Expand Down
@@ -1,4 +1,4 @@
package repository
package chunker

import (
"io/ioutil"
Expand All @@ -18,12 +18,12 @@ func (t *ChunkerTests) SetUpTest(c *C) {
}

func (t *ChunkerTests) TestHandleError(c *C) {
_, err := NewChunker(filepath.Join(t.dir, "non-existing"), 16)
_, err := New(filepath.Join(t.dir, "non-existing"), 16)
c.Assert(err, NotNil)
}

func (t *ChunkerTests) TestHandleBadChunkSiez(c *C) {
_, err := NewChunker("test", 15)
_, err := New("test", 15)
c.Assert(err, NotNil)
}

Expand All @@ -33,7 +33,7 @@ func (t *ChunkerTests) TestNormal(c *C) {
0600)
c.Assert(err, IsNil)

ch, err := NewChunker(path, 16)
ch, err := New(path, 16)
c.Assert(err, IsNil)
defer ch.Close()
c.Assert(ch.HasNext(), Equals, true)
Expand Down
11 changes: 11 additions & 0 deletions repository/chunker/errors.go
@@ -0,0 +1,11 @@
package chunker

import (
"errors"
)

var (
// ErrBadChunkSize will be thrown if a too little chunk size is requested.
// This is used by the Chunker implementation.
ErrBadChunkSize = errors.New("bad chunk size (must be >16 bytes)")
)
5 changes: 3 additions & 2 deletions repository/clientRepository.go
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/hoffie/larasync/helpers/atomic"
"github.com/hoffie/larasync/helpers/crypto"
"github.com/hoffie/larasync/helpers/path"
"github.com/hoffie/larasync/repository/chunker"
"github.com/hoffie/larasync/repository/nib"
"github.com/hoffie/larasync/repository/tracker"
)
Expand Down Expand Up @@ -79,7 +80,7 @@ func (r *ClientRepository) getFileChunkIDs(path string) ([]string, error) {
// splitFileToChunks takes a file path and splits its contents into chunks
// identified by their content ids.
func (r *ClientRepository) splitFileToChunks(path string, handler func(string, []byte) error) ([]string, error) {
chunker, err := NewChunker(path, chunkSize)
chunker, err := chunker.New(path, chunkSize)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -109,7 +110,7 @@ func (r *ClientRepository) splitFileToChunks(path string, handler func(string, [

// fileToChunkIds returnes te current chunk hashes for the given path.
func (r *ClientRepository) fileToChunkIds(path string) ([]string, error) {
chunker, err := NewChunker(path, chunkSize)
chunker, err := chunker.New(path, chunkSize)
if err != nil {
return nil, err
}
Expand Down
3 changes: 0 additions & 3 deletions repository/errors.go
Expand Up @@ -11,9 +11,6 @@ var (
// which couldn't be encoded to the correct size to pass it as a
// Public Key signature.
ErrInvalidPublicKeySize = errors.New("Invalid public key size.")
// ErrBadChunkSize will be thrown if a too little chunk size is requested.
// This is used by the Chunker implementation.
ErrBadChunkSize = errors.New("bad chunk size (must be >16 bytes)")
// ErrSignatureVerification gets returned if a signature of a signed NIB could
// not be verified.
ErrSignatureVerification = errors.New("Signature verification failed")
Expand Down

0 comments on commit 7f7502a

Please sign in to comment.