-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
167 lines (138 loc) · 4.31 KB
/
db.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
package rnas
import (
"database/sql"
"encoding/json"
"fmt"
log "github.com/sirupsen/logrus"
)
var _db *sql.DB
// Initialize the database tables
func InitDB(db *sql.DB) {
// Create table if not exists
createConfigTableSQL := `
CREATE TABLE IF NOT EXISTS configs (
id TEXT PRIMARY KEY,
config TEXT
);
`
_, err := db.Exec(createConfigTableSQL)
if err != nil {
log.Fatal("failed to create table:", err)
}
// foreign key cascad
_, err = db.Exec("PRAGMA foreign_keys = ON")
if err != nil {
log.Fatal("Error enabling foreign keys:", err)
}
createFileStripesTable := `
CREATE TABLE IF NOT EXISTS file_stripes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
filepath TEXT UNIQUE,
k INTEGER,
m INTEGER,
size INTEGER,
stripe_depth INTEGER,
min_depth INTEGER,
config_name TEXT
);`
if _, err := db.Exec(createFileStripesTable); err != nil {
log.Fatal("failed to create file_stripes table:", err)
}
createShardsTable := `
CREATE TABLE IF NOT EXISTS shards (
id INTEGER PRIMARY KEY AUTOINCREMENT,
file_id INTEGER,
shard_index INTEGER,
size INTEGER,
server_id TEXT,
is_data_shard BOOLEAN,
shard_hashname TEXT,
FOREIGN KEY (file_id) REFERENCES file_stripes(id) ON DELETE CASCADE,
UNIQUE(file_id, shard_index)
);`
if _, err := db.Exec(createShardsTable); err != nil {
log.Fatal("failed to create shards table:", err)
}
_db = db
}
// Save file stripe configuration to the database
func saveFileStripe(file *FileStripe) error {
result, err := _db.Exec(
`INSERT OR REPLACE INTO file_stripes
(filepath, k, m, config_name, size, stripe_depth, min_depth) VALUES (?, ?, ?, ?, ?, ?, ?)`,
file.Filepath, file.K, file.M, file.ConfigName, file.Size, file.StripeDepth, file.MinDepth)
if err != nil {
return fmt.Errorf("failed to insert file stripe config: %v", err)
}
fileID, err := result.LastInsertId()
if err != nil {
return fmt.Errorf("failed to get last insert ID: %v", err)
}
file.ID = int(fileID)
return nil
}
func getFileStripe(filepath string) (*FileStripe, error) {
fs := &FileStripe{}
row := _db.QueryRow(
`SELECT id, k, m, config_name, size, stripe_depth, min_depth FROM file_stripes WHERE filepath = ?`, filepath)
err := row.Scan(&fs.ID, &fs.K, &fs.M, &fs.ConfigName, &fs.Size, &fs.StripeDepth, &fs.MinDepth)
if err != nil {
return nil, fmt.Errorf("failed to query file_strips: %v", err)
}
return fs, nil
}
// Save shard information to the database
func saveShard(shard *Shard) error {
_, err := _db.Exec(`INSERT INTO shards (file_id, shard_index, server_id, shard_hashname, is_data_shard, size) VALUES (?, ?, ?, ?, ?, ?)`,
shard.fileID, shard.shardIndex, shard.serverID, shard.shardHashname, shard.dataShard, shard.size)
if err != nil {
return fmt.Errorf("failed to insert shard info: %v", err)
}
return nil
}
// Read shard information by file ID
func getShards(fileID int) ([]Shard, error) {
rows, err := _db.Query(`SELECT file_id, shard_index, server_id, shard_hashname, is_data_shard,size FROM shards WHERE file_id = ?`, fileID)
if err != nil {
return nil, fmt.Errorf("failed to query shards: %v", err)
}
defer rows.Close()
var shards []Shard
for rows.Next() {
shard := Shard{}
if err := rows.Scan(&shard.fileID, &shard.shardIndex, &shard.serverID, &shard.shardHashname, &shard.dataShard, &shard.size); err != nil {
return nil, fmt.Errorf("failed to scan shard row: %v", err)
}
shards = append(shards, shard)
}
return shards, nil
}
func SaveConfigToDB(config *Config) error {
// Insert config into the table
configData, err := json.Marshal(config)
if err != nil {
return fmt.Errorf("failed to marshal config: %v", err)
}
_, err = _db.Exec("INSERT OR REPLACE INTO configs (id, config) VALUES (?, ?)", config.Name, string(configData))
if err != nil {
return fmt.Errorf("failed to insert config: %v", err)
}
return nil
}
func LoadConfigFromDB(configName string) (Config, error) {
var config Config
row := _db.QueryRow("SELECT config FROM configs WHERE id = ?", configName)
var configData string
err := row.Scan(&configData)
if err != nil {
if err == sql.ErrNoRows {
return config, fmt.Errorf("config '%s' not found", configName)
}
return config, fmt.Errorf("failed to query config: %v", err)
}
err = json.Unmarshal([]byte(configData), &config)
if err != nil {
return config, fmt.Errorf("failed to unmarshal config: %v", err)
}
return config, nil
}