Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-ds-flatfs
Commits
b72b00c3
Commit
b72b00c3
authored
Nov 08, 2015
by
Jeromy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add option to disable syncing
parent
3488e4dd
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
45 additions
and
32 deletions
+45
-32
flatfs.go
flatfs.go
+29
-16
flatfs_test.go
flatfs_test.go
+16
-16
No files found.
flatfs.go
View file @
b72b00c3
...
...
@@ -34,11 +34,14 @@ type Datastore struct {
path
string
// length of the dir splay prefix, in bytes of hex digits
hexPrefixLen
int
// sychronize all writes and directory changes for added safety
sync
bool
}
var
_
datastore
.
Datastore
=
(
*
Datastore
)(
nil
)
func
New
(
path
string
,
prefixLen
int
)
(
*
Datastore
,
error
)
{
func
New
(
path
string
,
prefixLen
int
,
sync
bool
)
(
*
Datastore
,
error
)
{
if
prefixLen
<=
0
||
prefixLen
>
maxPrefixLen
{
return
nil
,
ErrBadPrefixLen
}
...
...
@@ -81,8 +84,10 @@ func (fs *Datastore) makePrefixDir(dir string) error {
// it, the creation of the prefix dir itself might not be
// durable yet. Sync the root dir after a successful mkdir of
// a prefix dir, just to be paranoid.
if
err
:=
syncDir
(
fs
.
path
);
err
!=
nil
{
return
err
if
fs
.
sync
{
if
err
:=
syncDir
(
fs
.
path
);
err
!=
nil
{
return
err
}
}
return
nil
}
...
...
@@ -149,8 +154,10 @@ func (fs *Datastore) doPut(key datastore.Key, val []byte) error {
if
_
,
err
:=
tmp
.
Write
(
val
);
err
!=
nil
{
return
err
}
if
err
:=
tmp
.
Sync
();
err
!=
nil
{
return
err
if
fs
.
sync
{
if
err
:=
tmp
.
Sync
();
err
!=
nil
{
return
err
}
}
if
err
:=
tmp
.
Close
();
err
!=
nil
{
return
err
...
...
@@ -163,8 +170,10 @@ func (fs *Datastore) doPut(key datastore.Key, val []byte) error {
}
removed
=
true
if
err
:=
syncDir
(
dir
);
err
!=
nil
{
return
err
if
fs
.
sync
{
if
err
:=
syncDir
(
dir
);
err
!=
nil
{
return
err
}
}
return
nil
}
...
...
@@ -214,8 +223,10 @@ func (fs *Datastore) putMany(data map[datastore.Key]interface{}) error {
// Now we sync everything
// sync and close files
for
fi
,
_
:=
range
files
{
if
err
:=
fi
.
Sync
();
err
!=
nil
{
return
err
if
fs
.
sync
{
if
err
:=
fi
.
Sync
();
err
!=
nil
{
return
err
}
}
if
err
:=
fi
.
Close
();
err
!=
nil
{
...
...
@@ -237,15 +248,17 @@ func (fs *Datastore) putMany(data map[datastore.Key]interface{}) error {
}
// now sync the dirs for those files
for
_
,
dir
:=
range
dirsToSync
{
if
err
:=
syncDir
(
dir
);
err
!=
nil
{
return
err
if
fs
.
sync
{
for
_
,
dir
:=
range
dirsToSync
{
if
err
:=
syncDir
(
dir
);
err
!=
nil
{
return
err
}
}
}
// sync top flatfs dir
if
err
:=
syncDir
(
fs
.
path
);
err
!=
nil
{
return
err
// sync top flatfs dir
if
err
:=
syncDir
(
fs
.
path
);
err
!=
nil
{
return
err
}
}
return
nil
...
...
flatfs_test.go
View file @
b72b00c3
...
...
@@ -35,7 +35,7 @@ func TestBadPrefixLen(t *testing.T) {
defer
cleanup
()
for
i
:=
0
;
i
>
-
3
;
i
--
{
_
,
err
:=
flatfs
.
New
(
temp
,
0
)
_
,
err
:=
flatfs
.
New
(
temp
,
i
,
false
)
if
g
,
e
:=
err
,
flatfs
.
ErrBadPrefixLen
;
g
!=
e
{
t
.
Errorf
(
"expected ErrBadPrefixLen, got: %v"
,
g
)
}
...
...
@@ -46,7 +46,7 @@ func TestPutBadValueType(t *testing.T) {
temp
,
cleanup
:=
tempdir
(
t
)
defer
cleanup
()
fs
,
err
:=
flatfs
.
New
(
temp
,
2
)
fs
,
err
:=
flatfs
.
New
(
temp
,
2
,
false
)
if
err
!=
nil
{
t
.
Fatalf
(
"New fail: %v
\n
"
,
err
)
}
...
...
@@ -61,7 +61,7 @@ func TestPut(t *testing.T) {
temp
,
cleanup
:=
tempdir
(
t
)
defer
cleanup
()
fs
,
err
:=
flatfs
.
New
(
temp
,
2
)
fs
,
err
:=
flatfs
.
New
(
temp
,
2
,
false
)
if
err
!=
nil
{
t
.
Fatalf
(
"New fail: %v
\n
"
,
err
)
}
...
...
@@ -76,7 +76,7 @@ func TestGet(t *testing.T) {
temp
,
cleanup
:=
tempdir
(
t
)
defer
cleanup
()
fs
,
err
:=
flatfs
.
New
(
temp
,
2
)
fs
,
err
:=
flatfs
.
New
(
temp
,
2
,
false
)
if
err
!=
nil
{
t
.
Fatalf
(
"New fail: %v
\n
"
,
err
)
}
...
...
@@ -104,7 +104,7 @@ func TestPutOverwrite(t *testing.T) {
temp
,
cleanup
:=
tempdir
(
t
)
defer
cleanup
()
fs
,
err
:=
flatfs
.
New
(
temp
,
2
)
fs
,
err
:=
flatfs
.
New
(
temp
,
2
,
false
)
if
err
!=
nil
{
t
.
Fatalf
(
"New fail: %v
\n
"
,
err
)
}
...
...
@@ -136,7 +136,7 @@ func TestGetNotFoundError(t *testing.T) {
temp
,
cleanup
:=
tempdir
(
t
)
defer
cleanup
()
fs
,
err
:=
flatfs
.
New
(
temp
,
2
)
fs
,
err
:=
flatfs
.
New
(
temp
,
2
,
false
)
if
err
!=
nil
{
t
.
Fatalf
(
"New fail: %v
\n
"
,
err
)
}
...
...
@@ -154,7 +154,7 @@ func TestStorage(t *testing.T) {
const
prefixLen
=
2
const
prefix
=
"7175"
const
target
=
prefix
+
string
(
os
.
PathSeparator
)
+
"71757578.data"
fs
,
err
:=
flatfs
.
New
(
temp
,
prefixLen
)
fs
,
err
:=
flatfs
.
New
(
temp
,
prefixLen
,
false
)
if
err
!=
nil
{
t
.
Fatalf
(
"New fail: %v
\n
"
,
err
)
}
...
...
@@ -209,7 +209,7 @@ func TestHasNotFound(t *testing.T) {
temp
,
cleanup
:=
tempdir
(
t
)
defer
cleanup
()
fs
,
err
:=
flatfs
.
New
(
temp
,
2
)
fs
,
err
:=
flatfs
.
New
(
temp
,
2
,
false
)
if
err
!=
nil
{
t
.
Fatalf
(
"New fail: %v
\n
"
,
err
)
}
...
...
@@ -227,7 +227,7 @@ func TestHasFound(t *testing.T) {
temp
,
cleanup
:=
tempdir
(
t
)
defer
cleanup
()
fs
,
err
:=
flatfs
.
New
(
temp
,
2
)
fs
,
err
:=
flatfs
.
New
(
temp
,
2
,
false
)
if
err
!=
nil
{
t
.
Fatalf
(
"New fail: %v
\n
"
,
err
)
}
...
...
@@ -249,7 +249,7 @@ func TestDeleteNotFound(t *testing.T) {
temp
,
cleanup
:=
tempdir
(
t
)
defer
cleanup
()
fs
,
err
:=
flatfs
.
New
(
temp
,
2
)
fs
,
err
:=
flatfs
.
New
(
temp
,
2
,
false
)
if
err
!=
nil
{
t
.
Fatalf
(
"New fail: %v
\n
"
,
err
)
}
...
...
@@ -264,7 +264,7 @@ func TestDeleteFound(t *testing.T) {
temp
,
cleanup
:=
tempdir
(
t
)
defer
cleanup
()
fs
,
err
:=
flatfs
.
New
(
temp
,
2
)
fs
,
err
:=
flatfs
.
New
(
temp
,
2
,
false
)
if
err
!=
nil
{
t
.
Fatalf
(
"New fail: %v
\n
"
,
err
)
}
...
...
@@ -289,7 +289,7 @@ func TestQuerySimple(t *testing.T) {
temp
,
cleanup
:=
tempdir
(
t
)
defer
cleanup
()
fs
,
err
:=
flatfs
.
New
(
temp
,
2
)
fs
,
err
:=
flatfs
.
New
(
temp
,
2
,
false
)
if
err
!=
nil
{
t
.
Fatalf
(
"New fail: %v
\n
"
,
err
)
}
...
...
@@ -325,7 +325,7 @@ func TestBatchPut(t *testing.T) {
temp
,
cleanup
:=
tempdir
(
t
)
defer
cleanup
()
fs
,
err
:=
flatfs
.
New
(
temp
,
2
)
fs
,
err
:=
flatfs
.
New
(
temp
,
2
,
false
)
if
err
!=
nil
{
t
.
Fatalf
(
"New fail: %v
\n
"
,
err
)
}
...
...
@@ -337,7 +337,7 @@ func TestBatchDelete(t *testing.T) {
temp
,
cleanup
:=
tempdir
(
t
)
defer
cleanup
()
fs
,
err
:=
flatfs
.
New
(
temp
,
2
)
fs
,
err
:=
flatfs
.
New
(
temp
,
2
,
false
)
if
err
!=
nil
{
t
.
Fatalf
(
"New fail: %v
\n
"
,
err
)
}
...
...
@@ -360,7 +360,7 @@ func BenchmarkConsecutivePut(b *testing.B) {
temp
,
cleanup
:=
tempdir
(
b
)
defer
cleanup
()
fs
,
err
:=
flatfs
.
New
(
temp
,
2
)
fs
,
err
:=
flatfs
.
New
(
temp
,
2
,
false
)
if
err
!=
nil
{
b
.
Fatalf
(
"New fail: %v
\n
"
,
err
)
}
...
...
@@ -390,7 +390,7 @@ func BenchmarkBatchedPut(b *testing.B) {
temp
,
cleanup
:=
tempdir
(
b
)
defer
cleanup
()
fs
,
err
:=
flatfs
.
New
(
temp
,
2
)
fs
,
err
:=
flatfs
.
New
(
temp
,
2
,
false
)
if
err
!=
nil
{
b
.
Fatalf
(
"New fail: %v
\n
"
,
err
)
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment