-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtidb.go
More file actions
107 lines (92 loc) · 2.63 KB
/
tidb.go
File metadata and controls
107 lines (92 loc) · 2.63 KB
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
// Copyright 2017 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"bytes"
"database/sql"
"fmt"
log "github.com/sirupsen/logrus"
)
type tidb struct {
db *sql.DB
}
func (c *tidb) ReadRow(key uint64) (bool, error) {
res, err := c.db.Query(fmt.Sprintf("SELECT * FROM %s WHERE ycsb_key=%d", *tableName, key))
if err != nil {
return false, err
}
var rowsFound int
for res.Next() {
rowsFound++
}
log.Debugf("reader found %d rows for key %d", rowsFound, key)
if err := res.Close(); err != nil {
return false, err
}
return rowsFound == 0, nil
}
func (c *tidb) InsertRow(key uint64, fields []string) error {
// TODO(arjun): Consider using a prepared statement here.
var buf bytes.Buffer
buf.WriteString(fmt.Sprintf("INSERT INTO %s VALUES (", *tableName))
fmt.Fprintf(&buf, "%d", key)
for _, s := range fields {
fmt.Fprintf(&buf, ", '%s'", s)
}
buf.WriteString(")")
_, err := c.db.Exec(buf.String())
return err
}
func (c *tidb) Clone() Database {
return c
}
func setupTiDB(url string) (Database, error) {
// Open connection to server and create a database.
db, err := sql.Open("mysql", url)
if err != nil {
return nil, err
}
// Allow a maximum of concurrency+1 connections to the database.
db.SetMaxOpenConns(*concurrency + 1)
db.SetMaxIdleConns(*concurrency + 1)
// odidenko: we don't need to create a database here, because we are using
// the database from the connection URL.
// if _, err := db.Exec("CREATE DATABASE IF NOT EXISTS ycsb"); err != nil {
// log.Fatalf("Failed to create the database %v", err)
// }
if *drop {
log.Debugf("Dropping the table")
if _, err := db.Exec(fmt.Sprintf("DROP TABLE IF EXISTS %s", *tableName)); err != nil {
log.Fatalf("Failed to drop the table: %s", err)
return nil, err
}
}
// Create the initial table for storing blocks.
createStmt := `
ycsb_key BIGINT PRIMARY KEY NOT NULL,
FIELD1 TEXT,
FIELD2 TEXT,
FIELD3 TEXT,
FIELD4 TEXT,
FIELD5 TEXT,
FIELD6 TEXT,
FIELD7 TEXT,
FIELD8 TEXT,
FIELD9 TEXT,
FIELD10 TEXT
)`
if _, err := db.Exec(fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s (", *tableName) + createStmt); err != nil {
return nil, err
}
return &tidb{db: db}, nil
}