-
Notifications
You must be signed in to change notification settings - Fork 226
/
staging_area.go
64 lines (54 loc) · 2.03 KB
/
staging_area.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package model
import (
"github.com/pkg/errors"
)
// StagingShard is an interface that enables every store to have it's own Commit logic
// See StagingArea for more details
type StagingShard interface {
Commit(dbTx DBTransaction) error
}
// StagingShardID is used to identify each of the store's staging shards
type StagingShardID uint64
// StagingArea is single changeset inside the consensus database, similar to a transaction in a classic database.
// Each StagingArea consists of multiple StagingShards, one for each dataStore that has any changes within it.
// To enable maximum flexibility for all stores, each has to define it's own Commit method, and pass it to the
// StagingArea through the relevant StagingShard.
//
// When the StagingArea is being Committed, it goes over all it's shards, and commits those one-by-one.
// Since Commit happens in a DatabaseTransaction, a StagingArea is atomic.
type StagingArea struct {
shards map[StagingShardID]StagingShard
isCommitted bool
}
// NewStagingArea creates a new, empty staging area.
func NewStagingArea() *StagingArea {
return &StagingArea{
shards: make(map[StagingShardID]StagingShard),
isCommitted: false,
}
}
// GetOrCreateShard attempts to retrieve a shard with the given name.
// If it does not exist - a new shard is created using `createFunc`.
func (sa *StagingArea) GetOrCreateShard(shardID StagingShardID, createFunc func() StagingShard) StagingShard {
shard, ok := sa.shards[shardID]
if !ok {
shard = createFunc()
sa.shards[shardID] = shard
}
return shard
}
// Commit goes over all the Shards in the StagingArea and commits them, inside the provided database transaction.
// Note: the transaction itself is not committed, this is the callers responsibility to commit it.
func (sa *StagingArea) Commit(dbTx DBTransaction) error {
if sa.isCommitted {
return errors.New("Attempt to call Commit on already committed stagingArea")
}
for _, shard := range sa.shards {
err := shard.Commit(dbTx)
if err != nil {
return err
}
}
sa.isCommitted = true
return nil
}