summaryrefslogtreecommitdiff
path: root/ledger/store/trackerdb/store.go
blob: 4140f5be146116f18f77d68b84dc6044a9c952c7 (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright (C) 2019-2023 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package trackerdb

import (
	"context"
	"time"

	"github.com/algorand/go-algorand/logging"
	"github.com/algorand/go-algorand/util/db"
)

// Store is the interface for the tracker db.
type Store interface {
	ReaderWriter
	// settings
	SetSynchronousMode(ctx context.Context, mode db.SynchronousMode, fullfsync bool) (err error)
	IsSharedCacheConnection() bool
	// batch support
	Batch(fn BatchFn) (err error)
	BatchContext(ctx context.Context, fn BatchFn) (err error)
	BeginBatch(ctx context.Context) (Batch, error)
	// snapshot support
	Snapshot(fn SnapshotFn) (err error)
	SnapshotContext(ctx context.Context, fn SnapshotFn) (err error)
	BeginSnapshot(ctx context.Context) (Snapshot, error)
	// transaction support
	Transaction(fn TransactionFn) (err error)
	TransactionContext(ctx context.Context, fn TransactionFn) (err error)
	BeginTransaction(ctx context.Context) (Transaction, error)
	// maintenance
	Vacuum(ctx context.Context) (stats db.VacuumStats, err error)
	// testing
	ResetToV6Test(ctx context.Context) error
	// cleanup
	Close()
}

// Reader is the interface for the trackerdb read operations.
type Reader interface {
	MakeAccountsReader() (AccountsReaderExt, error)
	MakeAccountsOptimizedReader() (AccountsReader, error)
	MakeOnlineAccountsOptimizedReader() (OnlineAccountsReader, error)
	MakeSpVerificationCtxReader() SpVerificationCtxReader
	// catchpoint
	// Note: BuildMerkleTrie() needs this on the reader handle in sqlite to not get locked by write txns
	MakeCatchpointPendingHashesIterator(hashCount int) CatchpointPendingHashesIter
	// Note: Catchpoint tracker needs this on the reader handle in sqlite to not get locked by write txns
	MakeCatchpointReader() (CatchpointReader, error)
	MakeEncodedAccoutsBatchIter() EncodedAccountsBatchIter
	MakeKVsIter(ctx context.Context) (KVsIter, error)
}

// Writer is the interface for the trackerdb write operations.
type Writer interface {
	// trackerdb
	MakeAccountsWriter() (AccountsWriterExt, error)
	MakeAccountsOptimizedWriter(hasAccounts, hasResources, hasKvPairs, hasCreatables bool) (AccountsWriter, error)
	MakeOnlineAccountsOptimizedWriter(hasAccounts bool) (OnlineAccountsWriter, error)
	MakeSpVerificationCtxWriter() SpVerificationCtxWriter
	// testing
	Testing() WriterTestExt
}

// Catchpoint is currently holding most of the methods related to catchpoint.
//
// TODO: we still need to do a refactoring pass on catchpoint
//
//	there are two distinct set of methods present:
//	- read/write ops for managing catchpoint data
//	- read/write ops on trackerdb to support building catchpoints
//	we should split these two sets of methods into two separate interfaces
type Catchpoint interface {
	// reader
	MakeOrderedAccountsIter(accountCount int) OrderedAccountsIter
	// writer
	MakeCatchpointWriter() (CatchpointWriter, error)
	// reader/writer
	MakeCatchpointReaderWriter() (CatchpointReaderWriter, error)
	MakeMerkleCommitter(staging bool) (MerkleCommitter, error)
}

// ReaderWriter is the interface for the trackerdb read/write operations.
//
// Some of the operatiosn available here might not be present in neither the Reader nor the Writer interfaces.
// This is because some operations might require to be able to read and write at the same time.
type ReaderWriter interface {
	Reader
	Writer
	// init
	RunMigrations(ctx context.Context, params Params, log logging.Logger, targetVersion int32) (mgr InitParams, err error)
	// Note: at the moment, catchpoint methods are only accesible via reader/writer
	Catchpoint
}

// BatchScope is an atomic write-only scope to the store.
type BatchScope interface {
	Writer
	ResetTransactionWarnDeadline(ctx context.Context, deadline time.Time) (prevDeadline time.Time, err error)
}

// Batch is an atomic write-only accecssor to the store.
type Batch interface {
	BatchScope
	Commit() error
	Close() error
}

// SnapshotScope is an atomic read-only scope to the store.
type SnapshotScope interface {
	Reader
	ResetTransactionWarnDeadline(ctx context.Context, deadline time.Time) (prevDeadline time.Time, err error)
}

// Snapshot is an atomic read-only accecssor to the store.
type Snapshot interface {
	SnapshotScope
	Close() error
}

// TransactionScope is an atomic read/write scope to the store.
type TransactionScope interface {
	ReaderWriter
	ResetTransactionWarnDeadline(ctx context.Context, deadline time.Time) (prevDeadline time.Time, err error)
}

// Transaction is an atomic read/write accecssor to the store.
type Transaction interface {
	TransactionScope
	Commit() error
	Close() error
}

// BatchFn is the callback lambda used in `Batch`.
type BatchFn func(ctx context.Context, tx BatchScope) error

// SnapshotFn is the callback lambda used in `Snapshot`.
type SnapshotFn func(ctx context.Context, tx SnapshotScope) error

// TransactionFn is the callback lambda used in `Transaction`.
type TransactionFn func(ctx context.Context, tx TransactionScope) error