diff --git a/mutable_tree.go b/mutable_tree.go index 6abb1ff..f374a0e 100644 --- a/mutable_tree.go +++ b/mutable_tree.go @@ -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 } diff --git a/nodedb.go b/nodedb.go index be94099..49bc846 100644 --- a/nodedb.go +++ b/nodedb.go @@ -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 @@ -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 { @@ -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) diff --git a/options.go b/options.go index 7d5d246..58dfcab 100644 --- a/options.go +++ b/options.go @@ -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.