-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbaccess.go
executable file
·255 lines (229 loc) · 5.68 KB
/
dbaccess.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package camcloud
import (
"log"
"github.com/gocql/gocql"
"fmt"
"time"
"strings"
)
type CamRecord struct {
Ts int64 `json:"t"`
Cam string `json:"c"`
Software string `json:"s"`
Vehicle_color string `json:"vc"`
Vehicle_type string `json:"vt"`
Vehicle_type_score float64 `json:"vts"`
Vehicle_plate string `json:"vp,omitempty"`
Object_label string `json:"ol"`
Object_score float64 `json:"os"`
Object_id int `json:"oi"`
Object_x int `json:"x"`
Object_y int `json:"y"`
Object_w int `json:"w"`
Object_h int `json:"h"`
}
type InsertCmd struct {
Record CamRecord
Result_channel chan<- string
// this channel(should be buffered) is passed in by user to us,
// we use it to send back insertion result
}
type control_cmd struct {
cmd string
result_chan chan<- string
}
type QueryCmd struct {
Cam string
Vehicle_color string
Vehicle_type string
Vehicle_plate string
Timestamp_start int64 // epoch
Timestamp_end int64 // epoch
Result_chan chan<- CamRecord
}
var db_session *gocql.Session
var db_inserter_data_channel chan InsertCmd
var db_query_channel chan QueryCmd
var db_accessor_control_channel chan control_cmd
// Initializes the maps and the channels
func initializeDBAccessor() {
// connect to the cluster
cluster := gocql.NewCluster(conf.ClusterIpAddress)
cluster.Keyspace = "camcloud"
cluster.Authenticator = gocql.PasswordAuthenticator{
Username: "yakhe",
Password: "qingkou",
}
var err error
db_session, err = cluster.CreateSession()
if err != nil {
db_session.Close()
log.Printf("initialize(): failed to create db session to cluster %s, err %s",
conf.ClusterIpAddress, err.Error())
}
// the channel only blocks if it's full
db_inserter_data_channel = make(chan InsertCmd, conf.DBInserterDataChannelDepth)
db_query_channel = make(chan QueryCmd, conf.DBQueryChannelDepth)
// control channel not buffered, so it's blocking
db_accessor_control_channel = make(chan control_cmd)
// Start the mutators
go db_inserter_routine()
log.Printf("dbaccess: started")
}
func uninitializeDBAccessor() {
rchan := make(chan string)
cmd := control_cmd{
cmd: "stop",
result_chan: rchan,
}
db_accessor_control_channel <- cmd
status := <- rchan
if status == "stopped" {
db_session.Close()
log.Printf("dbaccess: stopped")
} else {
log.Printf("Error: %s", status)
}
}
func db_inserter_routine() {
for {
select {
case query := <-db_query_channel:
processQueryCommand(query)
case data := <-db_inserter_data_channel:
processInsertCommand(data)
case stop := <-db_accessor_control_channel:
if stop.cmd == "stop" {
log.Printf("Received stop signal. Flushing buffer")
runBatchInserts() // Flush it
log.Printf("Flushed and stopped")
stop.result_chan <- "stopped"
return
}
}
}
}
const BUFFER_SIZE = 200
var cmd_buf [BUFFER_SIZE]InsertCmd
var cmd_counter = 0
func processInsertCommand(cmd InsertCmd) {
cmd_buf[cmd_counter] = cmd
cmd.Result_channel <- "received"
if cmd_counter == BUFFER_SIZE - 1 {
runBatchInserts()
cmd_counter = 0
} else {
cmd_counter = cmd_counter + 1
}
}
func runBatchInserts() {
stmt := `INSERT INTO records (
id,
ts,
cam,
software,
vehicle_color,
vehicle_type,
vehicle_type_score,
vehicle_plate,
object_label,
object_score,
object_id,
object_x,
object_y,
object_w,
object_h
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
batch := gocql.NewBatch(gocql.LoggedBatch)
for i := 0; i < cmd_counter; i++ {
cmd := cmd_buf[i].Record
batch.Query(
stmt,
gocql.TimeUUID(),
cmd.Ts,
cmd.Cam,
cmd.Software,
cmd.Vehicle_color,
cmd.Vehicle_type,
cmd.Vehicle_type_score,
cmd.Vehicle_plate,
cmd.Object_label,
cmd.Object_score,
cmd.Object_id,
cmd.Object_x,
cmd.Object_y,
cmd.Object_w,
cmd.Object_h,
)
}
fmt.Printf("inserting %d records\n", BUFFER_SIZE)
err := db_session.ExecuteBatch(batch)
if err != nil {
log.Fatal(err)
}
}
func addParamToWhereStmt(w string, param string, value string) string {
if value != "" && strings.ToLower(value) != "all" {
if w == "" {
w = param + "='" + value + "'"
} else {
w = w + " AND " + param + "='" + value + "'"
}
}
return w
}
func sendBackRecord(rec map[string]interface{}, rchan chan<- CamRecord) {
ts := rec["ts"].(time.Time)
crec := CamRecord {
Ts: ts.Unix(),
Cam: rec["cam"].(string),
Software: rec["software"].(string),
Object_id: rec["object_id"].(int),
Object_x: rec["object_x"].(int),
Object_y: rec["object_y"].(int),
Object_w: rec["object_w"].(int),
Object_h: rec["object_h"].(int),
Object_label: rec["object_label"].(string),
Object_score: rec["object_score"].(float64),
Vehicle_type: rec["vehicle_type"].(string),
Vehicle_type_score: rec["vehicle_type_score"].(float64),
Vehicle_color: rec["vehicle_color"].(string),
Vehicle_plate: rec["vehicle_plate"].(string),
}
rchan <- crec
}
func processQueryCommand(q QueryCmd) {
w := ""
w = addParamToWhereStmt(w, "cam", q.Cam)
w = addParamToWhereStmt(w, "vehicle_color", q.Vehicle_color)
w = addParamToWhereStmt(w, "vehicle_type", q.Vehicle_type)
w = addParamToWhereStmt(w, "vehicle_plate", q.Vehicle_plate)
stmt := `SELECT
ts,
cam,
software,
vehicle_color,
vehicle_type,
vehicle_type_score,
vehicle_plate,
object_label,
object_score,
object_id,
object_x,
object_y,
object_w,
object_h
FROM records
WHERE ` + w
m := &map[string]interface{}{}
iter := db_session.Query(stmt).Iter()
for iter.MapScan(*m) {
//fmt.Printf("%T: %#v\n", m, m)
//fmt.Printf("%#v\n", m)
sendBackRecord(*m, q.Result_chan)
m = &map[string]interface{}{}
}
iter.Close()
close(q.Result_chan)
return
}