Skip to content

remove hex encoding from flatfs #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: go

go:
- 1.3
- 1.6.2
- release
- tip

Expand Down
26 changes: 9 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
build:
go build
test: deps
go test -race -v ./...

test: build
go test -race -cpu=5 -v ./...
export IPFS_API ?= v04x.ipfs.io

# saves/vendors third-party dependencies to Godeps/_workspace
# -r flag rewrites import paths to use the vendored path
# ./... performs operation on all packages in tree
vendor: godep
godep save -r ./...
gx:
go get -u github.com/whyrusleeping/gx
go get -u github.com/whyrusleeping/gx-go

deps:
go get ./...
deps: gx
gx --verbose install --global
gx-go rewrite

watch:
-make
@echo "[watching *.go; for recompilation]"
# for portability, use watchmedo -- pip install watchmedo
@watchmedo shell-command --patterns="*.go;" --recursive \
--command='make' .
25 changes: 9 additions & 16 deletions flatfs/flatfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package flatfs

import (
"encoding/hex"
"errors"
"io/ioutil"
"os"
Expand Down Expand Up @@ -33,8 +32,8 @@ var (

type Datastore struct {
path string
// length of the dir splay prefix, in bytes of hex digits
hexPrefixLen int
// length of the dir splay prefix
prefixLen int

// sychronize all writes and directory changes for added safety
sync bool
Expand All @@ -47,21 +46,19 @@ func New(path string, prefixLen int, sync bool) (*Datastore, error) {
return nil, ErrBadPrefixLen
}
fs := &Datastore{
path: path,
// convert from binary bytes to bytes of hex encoding
hexPrefixLen: prefixLen * hex.EncodedLen(1),
sync: sync,
path: path,
prefixLen: prefixLen,
sync: sync,
}
return fs, nil
}

var padding = strings.Repeat("_", maxPrefixLen*hex.EncodedLen(1))
var padding = strings.Repeat("_", maxPrefixLen)

func (fs *Datastore) encode(key datastore.Key) (dir, file string) {
safe := hex.EncodeToString(key.Bytes()[1:])
prefix := (safe + padding)[:fs.hexPrefixLen]
prefix := (key.String() + padding)[:fs.prefixLen]
dir = path.Join(fs.path, prefix)
file = path.Join(dir, safe+extension)
file = path.Join(dir, key.String()+extension)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@whyrusleeping, note that the point of key.Bytes()[1:] this was to remove the '/' prefix'...

Copy link
Contributor

@kevina kevina Jun 28, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And path.Join(dir, key.String()+extension) will now include the '/' from the datastore key.

return dir, file
}

Expand All @@ -70,11 +67,7 @@ func (fs *Datastore) decode(file string) (key datastore.Key, ok bool) {
return datastore.Key{}, false
}
name := file[:len(file)-len(extension)]
k, err := hex.DecodeString(name)
if err != nil {
return datastore.Key{}, false
}
return datastore.NewKey(string(k)), true
return datastore.NewKey(name), true
}

func (fs *Datastore) makePrefixDir(dir string) error {
Expand Down
4 changes: 2 additions & 2 deletions flatfs/flatfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ func TestStorage(t *testing.T) {
defer cleanup()

const prefixLen = 2
const prefix = "7175"
const target = prefix + string(os.PathSeparator) + "71757578.data"
const prefix = "q"
const target = prefix + string(os.PathSeparator) + "quux.data"
fs, err := flatfs.New(temp, prefixLen, false)
if err != nil {
t.Fatalf("New fail: %v\n", err)
Expand Down