diff options
-rw-r--r-- | src/dedo.go | 124 | ||||
-rw-r--r-- | tests/dedo.go | 50 |
2 files changed, 87 insertions, 87 deletions
diff --git a/src/dedo.go b/src/dedo.go index f3916c9..6a3d2e6 100644 --- a/src/dedo.go +++ b/src/dedo.go @@ -14,7 +14,7 @@ /// /// == Basics /// -/// There are only a few types in Dedo: DB, Bucket, transactionT, and cursorT. The DB is +/// There are only a few types in Dedo: databaseT, bucketT, transactionT, and cursorT. The databaseT is /// a collection of buckets and is represented by a single file on disk. A /// bucket is a collection of unique keys that are associated with values. /// @@ -102,7 +102,7 @@ type DatabaseI interface{ /// quickly grow. type transactionT struct{ writable bool - db *DB + db *databaseT meta *meta root bucketT pages map[pgid]*page @@ -164,11 +164,11 @@ type InMemory struct{ *pds.Map[[]byte, *bucketT] } -/// DB represents a collection of buckets persisted to a file on disk. All data +/// databaseT represents a collection of buckets persisted to a file on disk. All data /// access is performed through transactions which can be obtained through the -/// DB. All the functions on DB will return a ErrDatabaseNotOpen if accessed +/// databaseT. All the functions on databaseT will return a ErrDatabaseNotOpen if accessed /// before Open() is called. -type DB struct { +type databaseT struct { /// MaxBatchSize is the maximum size of a batch. Default value is /// copied from DefaultMaxBatchSize in Open. /// @@ -225,7 +225,7 @@ type call struct { } type batch struct { - db *DB + db *databaseT timer *time.Timer start sync.Once calls []call @@ -359,10 +359,10 @@ const ( /// The data file format version. version = 2 - /// Represents a marker value to indicate that a file is a Dedo DB. + /// Represents a marker value to indicate that a file is a Dedo databaseT. defaultMagic uint32 = 0xFACADAAB - /// Default values if not set in a DB instance. + /// Default values if not set in a databaseT instance. DefaultMaxBatchSize int = 1000 DefaultMaxBatchDelay = 10 * time.Millisecond @@ -401,10 +401,10 @@ var ( /// /// These errors can be returned when opening or calling methods on a - /// DB. + /// databaseT. /// - /// ErrDatabaseNotOpen is returned when a DB instance is accessed before + /// ErrDatabaseNotOpen is returned when a databaseT instance is accessed before /// it is opened or after it is closed. ErrDatabaseNotOpen = errors.New("database not open") @@ -500,12 +500,12 @@ var ( /// fdatasync() flushes written data to a file descriptor. -func fdatasync(db *DB) error { +func fdatasync(db *databaseT) error { return db.file.Sync() } /// flock() acquires an advisory lock on a file descriptor. -func flock(db *DB) error { +func flock(db *databaseT) error { const lockFlags = syscall.LOCK_EX | syscall.LOCK_NB for { @@ -522,12 +522,12 @@ func flock(db *DB) error { } /// funlock() releases an advisory lock on a file descriptor. -func funlock(db *DB) error { +func funlock(db *databaseT) error { return syscall.Flock(int(db.file.Fd()), syscall.LOCK_UN) } -/// mmap memory() maps a DB's data file. -func mmap(db *DB, sz int) error { +/// mmap memory() maps a databaseT's data file. +func mmap(db *databaseT, sz int) error { fd := int(db.file.Fd()) b, err := syscall.Mmap(fd, 0, sz, syscall.PROT_READ, syscall.MAP_SHARED) if err != nil { @@ -546,8 +546,8 @@ func mmap(db *DB, sz int) error { return nil } -/// munmap() unmaps a DB's data file from memory. -func munmap(db *DB) error { +/// munmap() unmaps a databaseT's data file from memory. +func munmap(db *databaseT) error { if db.dataref == nil { return nil } @@ -1561,13 +1561,13 @@ func (r *elemRef) count() int { return int(r.page.count) } -/// DB.Path() returns the path to currently open database file. -func (db *DB) Path() string { +/// databaseT.Path() returns the path to currently open database file. +func (db *databaseT) Path() string { return db.path } -func newDB(path string, file *os.File, options OpenOptionsT) *DB { - return &DB{ +func newDB(path string, file *os.File, options OpenOptionsT) *databaseT { + return &databaseT{ MaxBatchSize: DefaultMaxBatchSize, MaxBatchDelay: DefaultMaxBatchDelay, opened: true, @@ -1596,7 +1596,7 @@ func openFile(path string) (*os.File, error) { return os.OpenFile(path, flagsRO, fileMode) } -func readPageSize(db *DB) (int, error) { +func readPageSize(db *databaseT) (int, error) { // Read the first meta page to determine the page size. const size4KiB = 0x1000 buf := [0x1000]byte{} @@ -1615,7 +1615,7 @@ func readPageSize(db *DB) (int, error) { } /// initDB() creates a new database file and initializes its meta pages. -func initDB(db *DB, size int64) error { +func initDB(db *databaseT, size int64) error { if size != 0 { pageSize, err := readPageSize(db) if err != nil { @@ -1828,9 +1828,9 @@ func Open(path string) (DatabaseI, error) { return OpenWith(path, defaultOptions) } -/// DB.mmap() opens the underlying memory-mapped file and initializes the meta +/// databaseT.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 { +func (db *databaseT) mmap(minsz int) error { db.mmaplock.Lock() defer db.mmaplock.Unlock() @@ -1884,8 +1884,8 @@ func (db *DB) mmap(minsz int) error { return nil } -/// DB.munmap() unmaps the data file from memory. -func (db *DB) munmap() error { +/// databaseT.munmap() unmaps the data file from memory. +func (db *databaseT) munmap() error { err := munmap(db) if err != nil { return fmt.Errorf("unmap error: " + err.Error()) @@ -1893,11 +1893,11 @@ func (db *DB) munmap() error { return nil } -/// DB. mmapSize() determines the appropriate size for the mmap given the +/// databaseT. mmapSize() determines the appropriate size for the mmap given the /// current size of the database. The minimum size is 32KB and doubles until it /// reaches 1GB. Returns an error if the new mmap size is greater than the max /// allowed. -func (db *DB) mmapSize(size int) (int, error) { +func (db *databaseT) mmapSize(size int) (int, error) { // Double the size from 32KB until 1GB. for i := uint(15); i <= 30; i++ { if size <= 1<<i { @@ -1932,9 +1932,9 @@ func (db *DB) mmapSize(size int) (int, error) { return int(sz), nil } -/// DB.Close() releases all database resources. All transactions must be closed +/// databaseT.Close() releases all database resources. All transactions must be closed /// before closing the database. -func (db *DB) Close() error { +func (db *databaseT) Close() error { db.rwlock.Lock() defer db.rwlock.Unlock() @@ -1947,7 +1947,7 @@ func (db *DB) Close() error { return db.close() } -func (db *DB) close() error { +func (db *databaseT) close() error { if !db.opened { return nil } @@ -1983,7 +1983,7 @@ func (db *DB) close() error { return nil } -/// DB.begin() starts a new transaction. Multiple read-only transactions can be +/// databaseT.begin() starts a new transaction. Multiple read-only transactions can be /// used concurrently but only one write transaction can be used at a time. /// Starting multiple write transactions will cause the calls to block and be /// serialized until the current write transaction finishes. @@ -1994,13 +1994,13 @@ func (db *DB) close() error { /// as it grows and it cannot do that while a read transaction is open. /// /// If a long running read transaction (for example, a snapshot transaction) is -/// needed, you might want to set DB.InitialMmapSize to a large enough value to +/// needed, you might want to set databaseT.InitialMmapSize to a large enough value to /// avoid potential blocking of write transaction. /// /// IMPORTANT: You must close read-only transactions after you are finished or /// else the database will not reclaim old pages. -func (db *DB) beginSnapshot() (*transactionT, error) { +func (db *databaseT) beginSnapshot() (*transactionT, error) { // Lock the meta pages while we initialize the transaction. We obtain // the meta lock before the mmap lock because that's the order that the // write transaction will obtain them. @@ -2031,7 +2031,7 @@ func (db *DB) beginSnapshot() (*transactionT, error) { return t, nil } -func (db *DB) beginTransaction() (*transactionT, error) { +func (db *databaseT) beginTransaction() (*transactionT, error) { // Obtain writer lock. This is released by the transaction when it // closes. This enforces only one writer transaction at a time. db.rwlock.Lock() @@ -2066,12 +2066,12 @@ func (db *DB) beginTransaction() (*transactionT, error) { return t, nil } -/// DB.removeTx() removes a transaction from the database. -func (db *DB) removeTx(tx *transactionT) { +/// databaseT.removeTx() removes a transaction from the database. +func (db *databaseT) removeTx(tx *transactionT) { // Release the read lock on the mmap. db.mmaplock.RUnlock() - // Use the meta lock to restrict access to the DB object. + // Use the meta lock to restrict access to the databaseT object. db.metalock.Lock() // Remove the transaction. @@ -2089,15 +2089,15 @@ func (db *DB) removeTx(tx *transactionT) { db.metalock.Unlock() } -/// DB.Update() executes a function within the context of a read-write managed +/// databaseT.Update() executes a function within the context of a read-write managed /// transaction. If no error is returned from the function then the transaction /// is committed. If an error is returned then the entire transaction is rolled /// back. Any error that is returned from the function or returned from the -/// commit is returned from the DB.Update() method. +/// commit is returned from the databaseT.Update() method. /// /// Attempting to manually commit or rollback within the function will cause a /// panic. -func (db *DB) Update(fn func(TransactionI) error) error { +func (db *databaseT) Update(fn func(TransactionI) error) error { t, err := db.beginTransaction() if err != nil { return err @@ -2121,12 +2121,12 @@ func (db *DB) Update(fn func(TransactionI) error) error { return t.commit() } -/// DB.View() executes a function within the context of a managed read-only +/// databaseT.View() executes a function within the context of a managed read-only /// transaction. Any error that is returned from the function is returned from -/// the DB.View() method. +/// the databaseT.View() method. /// /// Attempting to manually rollback within the function will cause a panic. -func (db *DB) View(fn func(SnapshotI) error) error { +func (db *databaseT) View(fn func(SnapshotI) error) error { t, err := db.beginSnapshot() if err != nil { return err @@ -2158,23 +2158,23 @@ func needsNewBatch(batch *batch, max int) bool { return (batch == nil) || (batch != nil && len(batch.calls) >= max) } -/// DB.Batch() calls fn as part of a batch. It behaves similar to Update, +/// databaseT.Batch() calls fn as part of a batch. It behaves similar to Update, /// except: /// -/// 1. concurrent DB.Batch() calls can be combined into a single Dedo +/// 1. concurrent databaseT.Batch() calls can be combined into a single Dedo /// transaction. /// -/// 2. the function passed to DB.Batch() may be called multiple times, +/// 2. the function passed to databaseT.Batch() may be called multiple times, /// regardless of whether it returns error or not. /// -/// This means that DB.Batch() function side effects must be idempotent and take +/// This means that databaseT.Batch() function side effects must be idempotent and take /// permanent effect only after a successful return is seen in caller. /// -/// The maximum batch size and delay can be adjusted with DB.MaxBatchSize and -/// DB.MaxBatchDelay, respectively. +/// The maximum batch size and delay can be adjusted with databaseT.MaxBatchSize and +/// databaseT.MaxBatchDelay, respectively. /// -/// DB.Batch() is only useful when there are multiple goroutines calling it. -func (db *DB) Batch(fn func(TransactionI) error) error { +/// databaseT.Batch() is only useful when there are multiple goroutines calling it. +func (db *databaseT) Batch(fn func(TransactionI) error) error { errCh := make(chan error, 1) db.batchMu.Lock() @@ -2209,7 +2209,7 @@ func (b *batch) trigger() { } /// batch.run() performs the transactions in the batch and communicates results -/// back to DB.Batch. +/// back to databaseT.Batch. func (b *batch) run() { b.db.batchMu.Lock() b.timer.Stop() @@ -2276,19 +2276,19 @@ func safelyCall(fn func(TransactionI) error, tx TransactionI) (err error) { /// db.page() retrieves a page reference from the mmap based on the current page /// size. -func (db *DB) page(id pgid) *page { +func (db *databaseT) page(id pgid) *page { pos := id * pgid(db.pageSize) return (*page)(unsafe.Pointer(&db.data[pos])) } /// pageInBuffer() retrieves a page reference from a given byte array based on /// the current page size. -func pageInBuffer(db *DB, b []byte, id pgid) *page { +func pageInBuffer(db *databaseT, b []byte, id pgid) *page { return (*page)(unsafe.Pointer(&b[id*pgid(db.pageSize)])) } /// db.meta() retrieves the current meta page reference. -func (db *DB) meta() *meta { +func (db *databaseT) meta() *meta { // We have to return the meta with the highest txid which doesn't fail // validation. Otherwise, we can cause errors when in fact the database // is in a consistent state. metaA is the one with the higher txid. @@ -2313,11 +2313,11 @@ func (db *DB) meta() *meta { // This should never be reached, because both meta1 and meta0 were // validated on mmap() and we do fsync() on every write. - panic("dedo.DB.meta(): invalid meta pages") + panic("dedo.databaseT.meta(): invalid meta pages") } /// db.allocate() returns a contiguous block of memory starting at a given page. -func (db *DB) allocate(count int) (*page, error) { +func (db *databaseT) allocate(count int) (*page, error) { // Allocate a temporary buffer for the page. var buf []byte if count == 1 { @@ -2351,7 +2351,7 @@ func (db *DB) allocate(count int) (*page, error) { } /// db.grow() grows the size of the database to the given sz. -func (db *DB) grow(sz int) error { +func (db *databaseT) grow(sz int) error { // Ignore if the new size is less than available file size. if sz <= db.filesz { return nil @@ -2384,7 +2384,7 @@ 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(db *DB) error { +func (m *meta) validate(db *databaseT) error { if m.magic != db.magic { return ErrInvalid } else if m.version != version { @@ -3449,7 +3449,7 @@ func mergepgids(dst, a, b pgids) { } /// transactionT.init() initializes the transaction. -func (tx *transactionT) init(db *DB) { +func (tx *transactionT) init(db *databaseT) { tx.db = db tx.pages = nil diff --git a/tests/dedo.go b/tests/dedo.go index 0d86055..245eb07 100644 --- a/tests/dedo.go +++ b/tests/dedo.go @@ -3258,7 +3258,7 @@ func TestOpen_ErrVersionMismatch(t *testing.T) { path := db.Path() // Close database. - err := db.DB.Close() + err := db.databaseT.Close() if err != nil { t.Fatal(err) } @@ -3299,7 +3299,7 @@ func TestOpen_ErrChecksum(t *testing.T) { path := db.Path() // Close database. - err := db.DB.Close() + err := db.databaseT.Close() if err != nil { t.Fatal(err) } @@ -3357,7 +3357,7 @@ func TestOpen_Size(t *testing.T) { } // Close database and grab the size. - err = db.DB.Close() + err = db.databaseT.Close() if err != nil { t.Fatal(err) } @@ -3442,7 +3442,7 @@ func TestOpen_Size_Large(t *testing.T) { } // Close database and grab the size. - err := db.DB.Close() + err := db.databaseT.Close() if err != nil { t.Fatal(err) } @@ -3575,7 +3575,7 @@ func TestDB_Open_InitialMmapSize(t *testing.T) { if err != nil { t.Fatal(err) } - db := idb.(*DB) + db := idb.(*databaseT) // create a long-running read transaction // that never gets closed while writing @@ -3626,7 +3626,7 @@ func TestDB_Open_InitialMmapSize(t *testing.T) { // Ensure that a database cannot open a transaction when it's not open. func TestDB_Begin_ErrDatabaseNotOpen(t *testing.T) { - var db DB + var db databaseT _, err := db.beginSnapshot() if err != ErrDatabaseNotOpen { t.Fatalf("unexpected error: %s", err) @@ -3646,7 +3646,7 @@ func TestDB_BeginRW(t *testing.T) { t.Fatal("expected tx") } - if tx.db != db.DB { + if tx.db != db.databaseT { t.Fatal("unexpected tx database") } else if !tx.writable { t.Fatal("expected writable tx") @@ -3658,9 +3658,9 @@ func TestDB_BeginRW(t *testing.T) { } } -// Ensure that opening a transaction while the DB is closed returns an error. +// Ensure that opening a transaction while the databaseT is closed returns an error. func TestDB_BeginRW_Closed(t *testing.T) { - var db DB + var db databaseT _, err := db.beginTransaction() if err != ErrDatabaseNotOpen { t.Fatalf("unexpected error: %s", err) @@ -3778,7 +3778,7 @@ func TestDB_Update(t *testing.T) { // Ensure a closed database returns an error while running a transaction block func TestDB_Update_Closed(t *testing.T) { - var db DB + var db databaseT err := db.Update(func(tx TransactionI) error { _, err := tx.CreateBucket([]byte("widgets")) if err != nil { @@ -4316,7 +4316,7 @@ func ExampleDB_Begin_ReadOnly() { if err != nil { log.Fatal(err) } - db := idb.(*DB) + db := idb.(*databaseT) defer os.Remove(db.Path()) // Create a bucket using a read-write transaction. @@ -4591,7 +4591,7 @@ func validateBatchBench(b *testing.B, db *WDB) { // DB is a test wrapper for DB. type WDB struct { - *DB + *databaseT } // MustOpenDB returns a new, open DB at a temporary location. @@ -4600,7 +4600,7 @@ func MustOpenDB() *WDB { if err != nil { panic(err) } - db := idb.(*DB) + db := idb.(*databaseT) return &WDB{db} } @@ -4612,7 +4612,7 @@ func (db *WDB) Close() error { db.MustCheck() // Close database and remove file. - return db.DB.Close() + return db.databaseT.Close() } // MustClose closes the database and deletes the underlying file. Panic on @@ -5103,7 +5103,7 @@ func TestNode_write_LeafPage(t *testing.T) { inodes: make(inodes, 0), bucket: &bucketT{ tx: &transactionT{ - db: &DB{}, + db: &databaseT{}, meta: &meta{ pgid: 1, }, @@ -5151,7 +5151,7 @@ func TestNode_split(t *testing.T) { inodes: make(inodes, 0), bucket: &bucketT{ tx: &transactionT{ - db: &DB{}, + db: &databaseT{}, meta: &meta{ pgid: 1, }, @@ -5187,7 +5187,7 @@ func TestNode_split_MinKeys(t *testing.T) { inodes: make(inodes, 0), bucket: &bucketT{ tx: &transactionT{ - db: &DB{}, + db: &databaseT{}, meta: &meta{ pgid: 1, }, @@ -5212,7 +5212,7 @@ func TestNode_split_SinglePage(t *testing.T) { inodes: make(inodes, 0), bucket: &bucketT{ tx: &transactionT{ - db: &DB{}, + db: &databaseT{}, meta: &meta{ pgid: 1, }, @@ -5310,7 +5310,7 @@ func TestPgids_merge_quick(t *testing.T) { // // -quick.count The number of iterations to perform. // -quick.seed The seed to use for randomizing. -// -quick.maxitems The maximum number of items to insert into a DB. +// -quick.maxitems The maximum number of items to insert into a databaseT. // -quick.maxksize The maximum size of a key. // -quick.maxvsize The maximum size of a value. // @@ -5655,7 +5655,7 @@ func simulatePutHandler(tx *transactionT, qdb *QuickDB) { } // QuickDB is an in-memory database that replicates the functionality of the -// Bolt DB type except that it is entirely in-memory. It is meant for testing +// Bolt databaseT type except that it is entirely in-memory. It is meant for testing // that the Bolt database is consistent. type QuickDB struct { sync.RWMutex @@ -6493,7 +6493,7 @@ func ExampleTx_Rollback() { if err != nil { log.Fatal(err) } - db := idb.(*DB) + db := idb.(*databaseT) defer os.Remove(db.Path()) // Create a bucket. @@ -6564,20 +6564,20 @@ func MustOpen2() *WDB2 { if err != nil { panic(err.Error()) } - db := idb.(*DB) - return &WDB2{DB: db, Path: f.Name()} + db := idb.(*databaseT) + return &WDB2{databaseT: db, Path: f.Name()} } // DB is a test wrapper for bolt.DB. type WDB2 struct { - *DB + *databaseT Path string } // Close closes and removes the database. func (db *WDB2) Close() error { defer os.Remove(db.Path) - return db.DB.Close() + return db.databaseT.Close() } func fillBucket(b *bucketT, prefix []byte) error { |