-
Notifications
You must be signed in to change notification settings - Fork 17
/
rowsreferences.go
184 lines (144 loc) · 4.61 KB
/
rowsreferences.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package transactions
import (
"bytes"
"github.com/gelembjuk/oursql/lib/utils"
"github.com/gelembjuk/oursql/node/blockchain"
"github.com/gelembjuk/oursql/node/database"
"github.com/gelembjuk/oursql/node/structures"
)
// This structure is used to manage association of data rows and transactions
// where a row was last changes in a blockchain
// The idea of this index is to help quickly to find where a row was changed before
// to find a transaction of previous change and understand if there is no other transation that is
// based on same previous TX
type rowsToTransactions struct {
DB database.DBManager
Logger *utils.LoggerMan
}
// Create tx index object to use in this structure
func (dr rowsToTransactions) getIndexManager() *transactionsIndex {
return newTransactionIndex(dr.DB, dr.Logger)
}
// Block Removed Fom Main branch
// We change index of row references and transactions
func (dr rowsToTransactions) UpdateOnBlockCancel(block *structures.Block) error {
drdb, err := dr.DB.GetDataReferencesObject()
if err != nil {
return err
}
dr.Logger.Trace.Printf("Data References on block remove %x", block.Hash)
for _, tx := range block.Transactions {
dr.Logger.Trace.Printf("Data References check tx %x", tx.GetID())
// there are 2 options. Previous TX can be upadte of this row or it can be table create
// we can update this reference or delete it
if !tx.IsSQLCommand() {
// nothing to do
continue
}
if len(tx.SQLCommand.ReferenceID) == 0 {
// no any reference here
continue
}
dr.Logger.Trace.Printf("TX %x , refID %s", tx.GetID(), string(tx.SQLCommand.ReferenceID))
curTX, err := drdb.GetTXForRefID(tx.SQLCommand.ReferenceID)
if err != nil {
// in which cases can it be? it should not happen
return err
}
if curTX == nil {
// there is no reference for this row. skipping it. nothing to do
continue
}
if bytes.Compare(curTX, tx.GetID()) != 0 {
// reference is for other TX. this should not happen
// TODO if this happens , it is needed to find why and fix something
continue
}
if len(tx.GetSQLBaseTX()) == 0 {
drdb.DeleteRefID(tx.SQLCommand.ReferenceID)
// delete and this is done
continue
}
// get previous TX to understand what is the type. Maybe that
txPrev, err := dr.getIndexManager().GetTransaction(tx.GetSQLBaseTX(), []byte{})
if err != nil {
return err
}
if txPrev != nil {
if bytes.Compare(tx.SQLCommand.ReferenceID, txPrev.SQLCommand.ReferenceID) == 0 {
// only if previous TX we worked with same row
drdb.SetTXForRefID(tx.SQLCommand.ReferenceID, txPrev.GetID())
}
}
// in other cases just delete it
drdb.DeleteRefID(tx.SQLCommand.ReferenceID)
}
return nil
}
// Block Added To Main branch
// We change index of row references and transactions
func (dr rowsToTransactions) UpdateOnBlockAdd(block *structures.Block) error {
drdb, err := dr.DB.GetDataReferencesObject()
if err != nil {
return err
}
//dr.Logger.Trace.Printf("Data References on block add %x", block.Hash)
for _, tx := range block.Transactions {
//dr.Logger.Trace.Printf("Data References check tx %x", tx.GetID())
if !tx.IsSQLCommand() {
// nothing to do
continue
}
if len(tx.SQLCommand.ReferenceID) == 0 {
// no any reference here
dr.Logger.Trace.Printf("NO Reference for %s", string(tx.GetSQLQuery()))
continue
}
//dr.Logger.Trace.Printf("TX %x , refID %s", tx.GetID(), string(tx.SQLCommand.ReferenceID))
// we set new association
err = drdb.SetTXForRefID(tx.SQLCommand.ReferenceID, tx.GetID())
if err != nil {
return err
}
}
return nil
}
func (dr rowsToTransactions) GetTXForRefID(RefID []byte) (txID []byte, err error) {
drdb, err := dr.DB.GetDataReferencesObject()
if err != nil {
return
}
return drdb.GetTXForRefID(RefID)
}
// Finds a base TX in blockchain by full read. It is only for side branches
func (dr rowsToTransactions) GetTXForRefIDByTIP(RefID []byte, AltRefID []byte, tip []byte) (txID []byte, err error) {
bci, err := blockchain.NewBlockchainIteratorFrom(dr.DB, tip)
if err != nil {
return nil, err
}
var altRefIDFound []byte
dr.Logger.Trace.Println("Search base TX in BC: Start")
for {
block, _ := bci.Next()
for j := len(block.Transactions) - 1; j >= 0; j-- {
tx := &block.Transactions[j]
if !tx.IsSQLCommand() {
continue
}
if bytes.Compare(RefID, tx.SQLCommand.ReferenceID) == 0 {
txID = tx.GetID()
return
}
if len(altRefIDFound) == 0 && bytes.Compare(AltRefID, tx.SQLCommand.ReferenceID) == 0 {
altRefIDFound = tx.GetID()
}
}
if len(block.PrevBlockHash) == 0 {
break
}
}
if len(altRefIDFound) > 0 {
txID = altRefIDFound
}
return
}