Skip to content

Commit

Permalink
Feat: added Shutdown calls
Browse files Browse the repository at this point in the history
  • Loading branch information
hmoog committed Apr 11, 2022
1 parent 6de8e17 commit d47bb04
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/ledger/branchdag/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type Storage struct {
// conflictMemberStorage is an object storage used to persist ConflictMember objects.
conflictMemberStorage *objectstorage.ObjectStorage[*ConflictMember]

// shutdownOnce is used to ensure that the shutdown routine is executed only a single time.
// shutdownOnce is used to ensure that the Shutdown routine is executed only a single time.
shutdownOnce sync.Once
}

Expand Down
35 changes: 35 additions & 0 deletions packages/ledger/storage.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package ledger

import (
"sync"

"github.com/cockroachdb/errors"
"github.com/iotaledger/hive.go/byteutils"
"github.com/iotaledger/hive.go/cerrors"
"github.com/iotaledger/hive.go/generics/dataflow"
"github.com/iotaledger/hive.go/generics/objectstorage"
"github.com/iotaledger/hive.go/marshalutil"
Expand Down Expand Up @@ -33,6 +36,9 @@ type Storage struct {

// ledger contains a reference to the Ledger that created the storage.
ledger *Ledger

// shutdownOnce is used to ensure that the Shutdown routine is executed only a single time.
shutdownOnce sync.Once
}

// newStorage returns a new storage instance for the given Ledger.
Expand Down Expand Up @@ -163,6 +169,35 @@ func (s *Storage) CachedConsumers(outputID utxo.OutputID) (cachedConsumers objec
return
}

// Prune resets the database and deletes all entities.
func (s *Storage) Prune() (err error) {
for _, storagePrune := range []func() error{
s.transactionStorage.Prune,
s.transactionMetadataStorage.Prune,
s.outputStorage.Prune,
s.outputMetadataStorage.Prune,
s.consumerStorage.Prune,
} {
if err = storagePrune(); err != nil {
err = errors.Errorf("failed to prune the object storage (%v): %w", err, cerrors.ErrFatal)
return
}
}

return
}

// Shutdown shuts down the KVStores that are used to persist data.
func (s *Storage) Shutdown() {
s.shutdownOnce.Do(func() {
s.transactionStorage.Shutdown()
s.transactionMetadataStorage.Shutdown()
s.outputStorage.Shutdown()
s.outputMetadataStorage.Shutdown()
s.consumerStorage.Shutdown()
})
}

// storeTransactionCommand is a ChainedCommand that stores a Transaction.
func (s *Storage) storeTransactionCommand(params *dataFlowParams, next dataflow.Next[*dataFlowParams]) (err error) {
created := false
Expand Down

0 comments on commit d47bb04

Please sign in to comment.