Skip to content

Commit

Permalink
chore(embedded/appendable): sync directories
Browse files Browse the repository at this point in the history
Signed-off-by: Jeronimo Irazabal <[email protected]>

chore(embedded/appendable): fileutils methods

Signed-off-by: Jeronimo Irazabal <[email protected]>
  • Loading branch information
jeroiraz committed Nov 7, 2022
1 parent 6fbf99e commit d72a54a
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 24 deletions.
22 changes: 0 additions & 22 deletions embedded/appendable/appendable.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"compress/flate"
"crypto/sha256"
"io"
"os"
)

const DefaultCompressionFormat = NoCompression
Expand Down Expand Up @@ -74,24 +73,3 @@ func Checksum(rAt io.ReaderAt, off, n int64) (checksum [sha256.Size]byte, err er

return checksum, nil
}

func SyncPaths(paths ...string) error {
for _, path := range paths {
err := SyncPath(path)
if err != nil {
return err
}
}
return nil
}

func SyncPath(path string) error {
f, err := os.Open(path)
if err != nil {
return err
}

defer f.Close()

return f.Sync()
}
27 changes: 27 additions & 0 deletions embedded/appendable/fileutils/fileutils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
Copyright 2022 Codenotary Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package fileutils

func SyncDir(paths ...string) error {
for _, path := range paths {
err := syncDir(path)
if err != nil {
return err
}
}
return nil
}
33 changes: 33 additions & 0 deletions embedded/appendable/fileutils/fileutils_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//go:build !windows
// +build !windows

/*
Copyright 2022 Codenotary Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package fileutils

import "os"

func syncDir(path string) error {
f, err := os.Open(path)
if err != nil {
return err
}

defer f.Close()

return f.Sync()
}
24 changes: 24 additions & 0 deletions embedded/appendable/fileutils/fileutils_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//go:build windows
// +build windows

/*
Copyright 2022 Codenotary Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package fileutils

func syncDir(path string) error {
return nil
}
14 changes: 13 additions & 1 deletion embedded/appendable/multiapp/multi_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"sync"

"github.com/codenotary/immudb/embedded/appendable"
"github.com/codenotary/immudb/embedded/appendable/fileutils"
"github.com/codenotary/immudb/embedded/appendable/singleapp"
"github.com/codenotary/immudb/embedded/cache"
)
Expand Down Expand Up @@ -141,7 +142,7 @@ func OpenWithHooks(path string, hooks MultiFileAppendableHooks, opts *Options) (
return nil, err
}

err = appendable.SyncPaths(path, filepath.Dir(path))
err = fileutils.SyncDir(path, filepath.Dir(path))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -460,6 +461,8 @@ func (mf *MultiFileAppendable) DiscardUpto(off int64) error {

appID := appendableID(off, mf.fileSize)

var dirSyncNeeded bool

for i := int64(0); i < appID; i++ {
if i == mf.currAppID {
break
Expand All @@ -478,6 +481,15 @@ func (mf *MultiFileAppendable) DiscardUpto(off int64) error {
if err != nil && !os.IsNotExist(err) {
return err
}

dirSyncNeeded = true
}

if dirSyncNeeded {
err := fileutils.SyncDir(mf.path)
if err != nil {
return err
}
}

return nil
Expand Down
3 changes: 2 additions & 1 deletion embedded/appendable/singleapp/single_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"sync"

"github.com/codenotary/immudb/embedded/appendable"
"github.com/codenotary/immudb/embedded/appendable/fileutils"
)

var ErrorPathIsNotADirectory = errors.New("singleapp: path is not a directory")
Expand Down Expand Up @@ -139,7 +140,7 @@ func Open(fileName string, opts *Options) (*AppendableFile, error) {
return nil, err
}

err = appendable.SyncPath(filepath.Dir(fileName))
err = fileutils.SyncDir(filepath.Dir(fileName))
if err != nil {
return nil, err
}
Expand Down

0 comments on commit d72a54a

Please sign in to comment.