Commit ba62308c authored by Jeromy Johnson's avatar Jeromy Johnson Committed by GitHub

Merge pull request #39 from ipfs/feat/unhex-flatfs

remove hex encoding from flatfs
parents 6e446b05 4614547a
language: go
go:
- 1.3
- 1.6.2
- release
- tip
......
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' .
......@@ -4,7 +4,6 @@
package flatfs
import (
"encoding/hex"
"errors"
"io/ioutil"
"os"
......@@ -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
......@@ -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)
return dir, file
}
......@@ -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 {
......
......@@ -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)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment