Skip to content

Commit

Permalink
Skip orphan metadata if versioning is not needed
Browse files Browse the repository at this point in the history
  • Loading branch information
codchen committed May 15, 2023
1 parent 9e58712 commit f042bf5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
7 changes: 7 additions & 0 deletions mutable_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,13 @@ func (tree *MutableTree) SetInitialVersion(version uint64) {
func (tree *MutableTree) DeleteVersions(versions ...int64) error {
logger.Debug("DELETING VERSIONS: %v\n", versions)

if tree.ndb.ShouldNotUseVersion() {
// no need to delete versions since there is no version to be
// deleted except the current one, which shouldn't be deleted
// in any circumstance
return nil
}

if len(versions) == 0 {
return nil
}
Expand Down
24 changes: 24 additions & 0 deletions nodedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,18 @@ func (ndb *nodeDB) SaveOrphans(version int64, orphans map[string]int64) error {
ndb.mtx.Lock()
defer ndb.mtx.Unlock()

// instead of saving orphan metadata and later read orphan metadata->delete
// orphan data->delete orphan metadata, we directly delete orphan data here
// without doing anything for orphan metadata, if versioning is not needed.
if ndb.ShouldNotUseVersion() {
for orphan := range orphans {
if err := ndb.deleteOrphanedData([]byte(orphan)); err != nil {
return err
}
}
return nil
}

toVersion, err := ndb.getPreviousVersion(version)
if err != nil {
return err
Expand All @@ -649,6 +661,14 @@ func (ndb *nodeDB) SaveOrphans(version int64, orphans map[string]int64) error {
return nil
}

func (ndb *nodeDB) deleteOrphanedData(hash []byte) error {
if err := ndb.batch.Delete(ndb.nodeKey(hash)); err != nil {
return err
}
ndb.nodeCache.Remove(hash)
return nil
}

// Saves a single orphan to disk.
func (ndb *nodeDB) saveOrphan(hash []byte, fromVersion, toVersion int64) error {
if fromVersion > toVersion {
Expand Down Expand Up @@ -1053,6 +1073,10 @@ func (ndb *nodeDB) traverseNodes(fn func(hash []byte, node *Node) error) error {
return nil
}

func (ndb *nodeDB) ShouldNotUseVersion() bool {
return ndb.opts.NoVersioning
}

func (ndb *nodeDB) String() (string, error) {
buf := bufPool.Get().(*bytes.Buffer)
defer bufPool.Put(buf)
Expand Down
4 changes: 4 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ type Options struct {

// When Stat is not nil, statistical logic needs to be executed
Stat *Statistics

// When set to true, the DB will only keep the most recent version and immediately delete
// obsolete data upon new data's commit
NoVersioning bool
}

// DefaultOptions returns the default options for IAVL.
Expand Down

0 comments on commit f042bf5

Please sign in to comment.