Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
154 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
package everdb | ||
|
||
type Helloer struct { | ||
type Database struct { | ||
} | ||
|
||
func (h Helloer) HelloWorld() int { | ||
func (h Database) HelloWorld() int { | ||
return 5 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package everdb | ||
|
||
import "errors" | ||
import "os" | ||
|
||
const BLOCK_SIZE = 4096 | ||
|
||
type FileBlockDevice struct { | ||
file *os.File | ||
len uint32 | ||
} | ||
|
||
func NewFileBlockDevice(fname string, readonly bool, overwrite bool) (*FileBlockDevice, error) { | ||
if readonly && overwrite { | ||
return nil, errors.New("Both overwrite and readonly specified") | ||
} | ||
|
||
f := new(FileBlockDevice) | ||
var err error | ||
|
||
if readonly { | ||
f.file, err = os.OpenFile(fname, os.O_RDONLY, 0) | ||
} else if overwrite { | ||
f.file, err = os.OpenFile(fname, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0666) | ||
} else { | ||
f.file, err = os.OpenFile(fname, os.O_CREATE|os.O_RDWR, 0666) | ||
} | ||
|
||
if nil != err { | ||
return nil, err | ||
} | ||
|
||
fi, err := f.file.Stat() | ||
if nil != err { | ||
return nil, errors.New("Failed to stat file") | ||
} | ||
|
||
if fi.Size()%BLOCK_SIZE != 0 { | ||
return nil, errors.New("File is corrupt (not a multiple of BLOCK_SIZE") | ||
} | ||
|
||
f.len = (uint32)(fi.Size() / BLOCK_SIZE) | ||
|
||
return f, nil | ||
} | ||
|
||
func (f *FileBlockDevice) Len() uint32 { | ||
return f.len | ||
} | ||
|
||
func (f *FileBlockDevice) Resize(len uint32) error { | ||
sz := (int64)(len) * BLOCK_SIZE | ||
|
||
err := f.file.Truncate(sz) | ||
if nil != err { | ||
return err | ||
} | ||
|
||
fi, err := f.file.Stat() | ||
if nil != err { | ||
return errors.New("Failed to stat file") | ||
} | ||
if fi.Size() != sz { | ||
return errors.New("Failed to reize file") | ||
} | ||
f.len = len | ||
|
||
return nil | ||
} | ||
|
||
func (f *FileBlockDevice) Get(block int) ([]byte, error) { | ||
b := make([]byte, BLOCK_SIZE) | ||
n, err := f.file.ReadAt(b, (int64)(block)*BLOCK_SIZE) | ||
if nil != err { | ||
return nil, err | ||
} | ||
if n != BLOCK_SIZE { | ||
return nil, errors.New("Failed to read BLOCK_SIZE bytes") | ||
} | ||
return b, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package everdb | ||
|
||
import "testing" | ||
import "os" | ||
|
||
func TestMain(m *testing.M) { | ||
code := m.Run() | ||
|
||
os.Remove("test.db") | ||
os.Exit(code) | ||
} | ||
|
||
func equals(a, b []byte) bool { | ||
l := len(a) | ||
if l != len(b) { | ||
return false | ||
} | ||
for i := 0; i < l; i++ { | ||
if a[i] != b[i] { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
func TestFileBlockDevice(t *testing.T) { | ||
t.Parallel() | ||
|
||
h, err := NewFileBlockDevice("test.db", false, true) | ||
|
||
if nil != err { | ||
t.Error(err) | ||
} | ||
|
||
if val := h.Len(); val != 0 { | ||
t.Error("Initial block device was not empty", val) | ||
} | ||
|
||
err = h.Resize(1) | ||
|
||
if nil != err { | ||
t.Errorf("Resize failed: %v", err) | ||
} | ||
|
||
if h.Len() != 1 { | ||
t.Error("Resize failed") | ||
} | ||
|
||
zero := make([]byte, BLOCK_SIZE) | ||
for i := 0; i < BLOCK_SIZE; i++ { | ||
zero[i] = 0 | ||
} | ||
|
||
b, err := h.Get(0) | ||
|
||
if nil != err { | ||
t.Error("Failed to get block") | ||
} | ||
|
||
if !equals(b, zero) { | ||
t.Error("fresh block is not zeroed") | ||
} | ||
|
||
} |