diff options
author | EuAndreh <eu@euandre.org> | 2025-01-25 20:56:09 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2025-01-25 20:56:09 -0300 |
commit | a5cfd72d08f5eb6fda3f6e13a1c6b4d456b3887d (patch) | |
tree | 3d691ffe75dc56ee94ba81e717d24cb0a3163dca /tests/dedo.go | |
parent | src/dedo.go: Remove public Cursor.Bucket() and Tx.DB() public functions (diff) | |
download | dedo-a5cfd72d08f5eb6fda3f6e13a1c6b4d456b3887d.tar.gz dedo-a5cfd72d08f5eb6fda3f6e13a1c6b4d456b3887d.tar.xz |
src/dedo.go: Remove Tx.CopyFile() and Tx.Page() public functions
Diffstat (limited to 'tests/dedo.go')
-rw-r--r-- | tests/dedo.go | 207 |
1 files changed, 60 insertions, 147 deletions
diff --git a/tests/dedo.go b/tests/dedo.go index 441d4ec..d479b4c 100644 --- a/tests/dedo.go +++ b/tests/dedo.go @@ -31,6 +31,51 @@ import ( +/// Tx.copyFile() copies the entire database to file at the given path. A +/// reader transaction is maintained during the copy so it is safe to continue +/// using the database while a copy is in progress. +func (tx *Tx) copyFile(path string, mode os.FileMode) error { + f, err := os.OpenFile(path, os.O_RDWR | os.O_CREATE | os.O_TRUNC, mode) + if err != nil { + return err + } + + _, err = tx.WriteTo(f) + if err != nil { + _ = f.Close() + return err + } + + return f.Close() +} + +/// Tx.Page() returns page information for a given page number. This is only +/// safe for concurrent use when used by a writable transaction. +func (tx *Tx) pageInfo(id int) (*PageInfo, error) { + if tx.db == nil { + return nil, ErrTxClosed + } else if pgid(id) >= tx.meta.pgid { + return nil, nil + } + + // Build the page info. + p := tx.db.page(pgid(id)) + info := &PageInfo{ + ID: id, + Count: int(p.count), + OverflowCount: int(p.overflow), + } + + // Determine the type (of if it's free). + if tx.db.freelist.freed(pgid(id)) { + info.Type = "free" + } else { + info.Type = p.typ() + } + + return info, nil +} + func test_newDB() { g.TestStart("newDB()") @@ -3534,49 +3579,49 @@ func TestDB_Consistency(t *testing.T) { } err = db.Update(func(tx *Tx) error { - p, _ := tx.Page(0) + p, _ := tx.pageInfo(0) if p == nil { t.Fatal("expected page") } else if p.Type != "meta" { t.Fatalf("unexpected page type: %s", p.Type) } - p, _ = tx.Page(1) + p, _ = tx.pageInfo(1) if p == nil { t.Fatal("expected page") } else if p.Type != "meta" { t.Fatalf("unexpected page type: %s", p.Type) } - p, _ = tx.Page(2) + p, _ = tx.pageInfo(2) if p == nil { t.Fatal("expected page") } else if p.Type != "free" { t.Fatalf("unexpected page type: %s", p.Type) } - p, _ = tx.Page(3) + p, _ = tx.pageInfo(3) if p == nil { t.Fatal("expected page") } else if p.Type != "free" { t.Fatalf("unexpected page type: %s", p.Type) } - p, _ = tx.Page(4) + p, _ = tx.pageInfo(4) if p == nil { t.Fatal("expected page") } else if p.Type != "leaf" { t.Fatalf("unexpected page type: %s", p.Type) } - p, _ = tx.Page(5) + p, _ = tx.pageInfo(5) if p == nil { t.Fatal("expected page") } else if p.Type != "freelist" { t.Fatalf("unexpected page type: %s", p.Type) } - p, _ = tx.Page(6) + p, _ = tx.pageInfo(6) if p != nil { t.Fatal("unexpected page") } @@ -4232,7 +4277,7 @@ func (db *WDB) MustCheck() { if len(errors) > 0 { path := tempfile() defer os.Remove(path) - err := tx.CopyFile(path, 0600) + err := tx.copyFile(path, 0600) if err != nil { panic(err) } @@ -4264,7 +4309,7 @@ func (db *WDB) MustCheck() { func (db *WDB) CopyTempFile() { path := tempfile() err := db.View(func(tx *Tx) error { - return tx.CopyFile(path, 0600) + return tx.copyFile(path, 0600) }) if err != nil { panic(err) @@ -5970,71 +6015,6 @@ func TestTx_OnCommit_Rollback(t *testing.T) { } } -// Ensure that the database can be copied to a file path. -func TestTx_CopyFile(t *testing.T) { - db := MustOpenDB() - defer db.MustClose() - defer os.Remove(db.Path()) - - path := tempfile() - defer os.Remove(path) - err := db.Update(func(tx *Tx) error { - b, err := tx.CreateBucket([]byte("widgets")) - if err != nil { - t.Fatal(err) - } - - err = b.Put([]byte("foo"), []byte("bar")) - if err != nil { - t.Fatal(err) - } - - err = b.Put([]byte("baz"), []byte("bat")) - if err != nil { - t.Fatal(err) - } - - return nil - }) - if err != nil { - t.Fatal(err) - } - - err = db.View(func(tx *Tx) error { - return tx.CopyFile(path, 0600) - }) - if err != nil { - t.Fatal(err) - } - - db2, err := Open(path) - if err != nil { - t.Fatal(err) - } - - err = db2.View(func(tx *Tx) error { - v := tx.Bucket([]byte("widgets")).Get([]byte("foo")) - if !bytes.Equal(v, []byte("bar")) { - t.Fatalf("unexpected value: %v", v) - } - - v = tx.Bucket([]byte("widgets")).Get([]byte("baz")) - if !bytes.Equal(v, []byte("bat")) { - t.Fatalf("unexpected value: %v", v) - } - - return nil - }) - if err != nil { - t.Fatal(err) - } - - err = db2.Close() - if err != nil { - t.Fatal(err) - } -} - type failWriterError struct{} func (failWriterError) Error() string { @@ -6056,8 +6036,8 @@ func (f *failWriter) Write(p []byte) (n int, err error) { return n, err } -// Ensure that Copy handles write errors right. -func TestTx_CopyFile_Error_Meta(t *testing.T) { +// Ensure that WriteTo handles write errors right. +func TestTx_WriteTo_Error_Meta(t *testing.T) { db := MustOpenDB() defer db.MustClose() defer os.Remove(db.Path()) @@ -6094,8 +6074,8 @@ func TestTx_CopyFile_Error_Meta(t *testing.T) { } } -// Ensure that Copy handles write errors right. -func TestTx_CopyFile_Error_Normal(t *testing.T) { +// Ensure that WriteTo handles write errors right. +func TestTx_WriteTo_Error_Normal(t *testing.T) { db := MustOpenDB() defer db.MustClose() defer os.Remove(db.Path()) @@ -6196,72 +6176,6 @@ func ExampleTx_Rollback() { // The value for 'foo' is still: bar } -func ExampleTx_CopyFile() { - // Open the database. - db, err := Open(tempfile()) - if err != nil { - log.Fatal(err) - } - defer os.Remove(db.Path()) - - // Create a bucket and a key. - err = db.Update(func(tx *Tx) error { - b, err := tx.CreateBucket([]byte("widgets")) - if err != nil { - return err - } - - err = b.Put([]byte("foo"), []byte("bar")) - if err != nil { - return err - } - return nil - }) - if err != nil { - log.Fatal(err) - } - - // Copy the database to another file. - toFile := tempfile() - defer os.Remove(toFile) - err = db.View(func(tx *Tx) error { - return tx.CopyFile(toFile, 0666) - }) - if err != nil { - log.Fatal(err) - } - - // Open the cloned database. - db2, err := Open(toFile) - if err != nil { - log.Fatal(err) - } - - // Ensure that the key exists in the copy. - err = db2.View(func(tx *Tx) error { - value := tx.Bucket([]byte("widgets")).Get([]byte("foo")) - fmt.Printf("The value for 'foo' in the clone is: %s\n", value) - return nil - }) - if err != nil { - log.Fatal(err) - } - - // Close database to release file lock. - err = db.Close() - if err != nil { - log.Fatal(err) - } - - err = db2.Close() - if err != nil { - log.Fatal(err) - } - - // Output: - // The value for 'foo' in the clone is: bar -} - // MustOpen creates a Bolt database in a temporary location. func MustOpen2() *WDB2 { // Create temporary path. @@ -6481,9 +6395,8 @@ func MainTest() { { "TestTx_ForEach_WithError", TestTx_ForEach_WithError }, { "TestTx_OnCommit", TestTx_OnCommit }, { "TestTx_OnCommit_Rollback", TestTx_OnCommit_Rollback }, - { "TestTx_CopyFile", TestTx_CopyFile }, - { "TestTx_CopyFile_Error_Meta", TestTx_CopyFile_Error_Meta }, - { "TestTx_CopyFile_Error_Normal", TestTx_CopyFile_Error_Normal }, + { "TestTx_WriteTo_Error_Meta", TestTx_WriteTo_Error_Meta }, + { "TestTx_WriteTo_Error_Normal", TestTx_WriteTo_Error_Normal }, } deps := testdeps.TestDeps{} |