-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql-antipode.go
122 lines (100 loc) · 2.91 KB
/
mysql-antipode.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
package antipode
import (
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
_ "github.com/go-sql-driver/mysql"
)
// It assumes by default that the table where the queries will be executed has
// exactly two columns called k and value
type MySQL struct {
dsn string
datastore string
}
func CreateMySQL(host string, port string, user string, password string, datastore string) MySQL {
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", user, password, host, port, datastore)
return MySQL{dsn, datastore}
}
func (m MySQL) write(ctx context.Context, table string, key string, obj AntiObj) error {
// Connect to MySQL database
db, err := sql.Open("mysql", m.dsn)
if err != nil {
return err
}
defer db.Close()
jsonAntiObj, err := json.Marshal(obj)
if err != nil {
return err
}
// Prepare the statement and execute the query
query := fmt.Sprintf("INSERT INTO %s VALUES (?, ?)", table)
stmt, err := db.Prepare(query)
if err != nil {
return err
}
defer stmt.Close()
_, err = stmt.Exec(key, jsonAntiObj)
return err
}
func (m MySQL) read(ctx context.Context, table string, key string) (AntiObj, error) {
// Connect to MySQL database
db, err := sql.Open("mysql", m.dsn)
if err != nil {
return AntiObj{}, err
}
defer db.Close()
// Query the database for the value associated with the key
var value []byte
query := fmt.Sprintf("SELECT value FROM %s WHERE k = ?", table)
err = db.QueryRow(query, key).Scan(&value)
if err != nil {
return AntiObj{}, err
}
var antiObj AntiObj
err = json.Unmarshal(value, &antiObj)
if err != nil {
return AntiObj{}, err
}
return antiObj, err
}
func (m MySQL) barrier(ctx context.Context, lineage []WriteIdentifier, datastoreID string) error {
// Connect to MySQL database
db, err := sql.Open("mysql", m.dsn)
if err != nil {
return err
}
defer db.Close()
for _, writeIdentifier := range lineage {
fmt.Println("key after for: ", writeIdentifier.Key)
if writeIdentifier.Dtstid == datastoreID {
for {
// Query the database for the value associated with the writeIdentifier.Key
var value []byte
query := fmt.Sprintf("SELECT value FROM %s WHERE k = ?", writeIdentifier.TableId)
err = db.QueryRow(query, writeIdentifier.Key).Scan(&value)
if !errors.Is(err, sql.ErrNoRows) && err != nil {
return err
} else if errors.Is(err, sql.ErrNoRows) { //the version replication process is not yet completed
fmt.Println("replication in progress")
continue
} else {
var antiObj AntiObj
err = json.Unmarshal(value, &antiObj)
if err != nil {
return err
}
if antiObj.Version == writeIdentifier.Version { //the version replication process is already completed
fmt.Println("replication done: ", antiObj.Version)
break
} else { //the version replication process is not yet completed
fmt.Println("replication of the new version in progress")
continue
}
}
}
}
}
return nil
}