diff options
author | EuAndreh <eu@euandre.org> | 2025-02-10 13:36:19 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2025-02-10 13:36:19 -0300 |
commit | ed7da251686fe9da0685c28c090100fbe3908974 (patch) | |
tree | f8d6b374dabb71251dba0c48517d450b910a3da7 /src/dedo.go | |
parent | src/dedo.go: Remove duplicate code of bucketT.NextID() (diff) | |
download | dedo-ed7da251686fe9da0685c28c090100fbe3908974.tar.gz dedo-ed7da251686fe9da0685c28c090100fbe3908974.tar.xz |
src/dedo.go: Simple rename from DB to databaseT
Diffstat (limited to 'src/dedo.go')
-rw-r--r-- | src/dedo.go | 124 |
1 files changed, 62 insertions, 62 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 |