aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/dedo.go40
-rw-r--r--tests/dedo.go21
2 files changed, 44 insertions, 17 deletions
diff --git a/src/dedo.go b/src/dedo.go
index cf88bbc..86012c1 100644
--- a/src/dedo.go
+++ b/src/dedo.go
@@ -150,6 +150,7 @@ type DB struct {
rwtx *Tx
txs []*Tx
freelist *freelist
+ magic uint32
pagePool sync.Pool
@@ -161,6 +162,10 @@ type DB struct {
mmaplock sync.RWMutex /// Protects mmap access during remapping.
}
+type OpenOptionsT struct{
+ Magic uint32
+}
+
type call struct {
fn func(*Tx) error
err chan<- error
@@ -320,7 +325,7 @@ const (
version = 2
/// Represents a marker value to indicate that a file is a Dedo DB.
- magic uint32 = 0xFACADAAB
+ defaultMagic uint32 = 0xFACADAAB
/// Default values if not set in a DB instance.
DefaultMaxBatchSize int = 1000
@@ -346,6 +351,10 @@ const (
)
var (
+ defaultOptions = OpenOptionsT{
+ Magic: defaultMagic,
+ }
+
/// trySolo is a special sentinel error value used for signaling that a
/// transaction function should be re-run. It should never be seen by
/// callers.
@@ -1507,7 +1516,7 @@ func (db *DB) Path() string {
return db.path
}
-func newDB(path string, file *os.File) *DB {
+func newDB(path string, file *os.File, options OpenOptionsT) *DB {
return &DB{
MaxBatchSize: DefaultMaxBatchSize,
MaxBatchDelay: DefaultMaxBatchDelay,
@@ -1515,6 +1524,7 @@ func newDB(path string, file *os.File) *DB {
opened: true,
path: path,
file: file,
+ magic: options.Magic,
}
}
@@ -1547,7 +1557,7 @@ func readPageSize(db *DB) (int, error) {
}
m := pageInBuffer(db, buf[:], 0).meta()
- err = m.validate()
+ err = m.validate(db)
if err != nil {
return 0, err
}
@@ -1578,7 +1588,7 @@ func initDB(db *DB, size int64) error {
// Initialize the meta page.
m := p.meta()
- m.magic = magic
+ m.magic = db.magic
m.version = version
m.pageSize = uint32(db.pageSize)
m.freelist = 2
@@ -1616,7 +1626,7 @@ func initDB(db *DB, size int64) error {
/// Open creates and opens a database at the given path. If the file does not
/// exist then it will be created automatically.
-func Open(path string) (*DB, error) {
+func OpenWith(path string, options OpenOptionsT) (*DB, error) {
file, err := openFile(path)
if err != nil {
return nil, err
@@ -1628,7 +1638,7 @@ func Open(path string) (*DB, error) {
return nil, err
}
- db := newDB(path, file)
+ db := newDB(path, file, options)
err = flock(db)
if err != nil {
@@ -1663,6 +1673,10 @@ func Open(path string) (*DB, error) {
return db, nil
}
+func Open(path string) (*DB, error) {
+ return OpenWith(path, defaultOptions)
+}
+
/// DB.mmap() opens the underlying memory-mapped file and initializes the meta
/// references. minsz is the minimum size that the new mmap can be.
func (db *DB) mmap(minsz int) error {
@@ -1710,8 +1724,8 @@ func (db *DB) mmap(minsz int) error {
// Validate the meta pages. We only return an error if both meta pages
// fail validation, since meta0 failing validation means that it wasn't
// saved properly -- but we can recover using meta1. And vice-versa.
- err0 := db.meta0.validate()
- err1 := db.meta1.validate()
+ err0 := db.meta0.validate(db)
+ err1 := db.meta1.validate(db)
if err0 != nil && err1 != nil {
return err0
}
@@ -2143,12 +2157,12 @@ func (db *DB) meta() *meta {
// Use higher meta page if valid. Otherwise fallback to previous, if
// valid.
- err := metaA.validate()
+ err := metaA.validate(db)
if err == nil {
return metaA
}
- err = metaB.validate()
+ err = metaB.validate(db)
if err == nil {
return metaB
}
@@ -2226,8 +2240,8 @@ func (db *DB) grow(sz int) error {
/// meta.validate() checks the marker bytes and version of the meta page to
/// ensure it matches this binary.
-func (m *meta) validate() error {
- if m.magic != magic {
+func (m *meta) validate(db *DB) error {
+ if m.magic != db.magic {
return ErrInvalid
} else if m.version != version {
return ErrVersionMismatch
@@ -4036,7 +4050,7 @@ func Main() {
g.Init()
args, command, rc := getopt(os.Args, commands, os.Stderr)
if rc != 0 {
- os.Exit(2)
+ os.Exit(rc)
}
os.Exit(runCommand(args, command, os.Stdin, os.Stdout, os.Stderr))
}
diff --git a/tests/dedo.go b/tests/dedo.go
index 0c33a59..ba78b34 100644
--- a/tests/dedo.go
+++ b/tests/dedo.go
@@ -88,8 +88,8 @@ func test_newDB() {
g.TestStart("newDB()")
g.Testing("different path are given back", func() {
- db1 := newDB("path1", nil)
- db2 := newDB("path2", nil)
+ db1 := newDB("path1", nil, defaultOptions)
+ db2 := newDB("path2", nil, defaultOptions)
g.TAssertEqual(db1.path, "path1")
g.TAssertEqual(db2.path, "path2")
@@ -99,11 +99,24 @@ func test_newDB() {
f1 := new(os.File)
f2 := new(os.File)
- db1 := newDB("path", f1)
- db2 := newDB("path", f2)
+ db1 := newDB("", f1, defaultOptions)
+ db2 := newDB("", f2, defaultOptions)
g.TAssertEqual(db1 == db2, false)
})
+
+ g.Testing("different magic values", func() {
+ customOptions := OpenOptionsT{
+ Magic: 0xafacada1,
+ }
+
+ db1 := newDB("", nil, defaultOptions)
+ db2 := newDB("", nil, customOptions)
+
+ g.TAssertEqual(db1.magic, defaultMagic)
+ g.TAssertEqual(db2.magic, uint32(0xafacada1))
+ g.TAssertEqual(db1.magic == db2.magic, false)
+ })
}
func test_openFile() {