maatma: redesign pong storage, to reduce CPU and IO consumption
This commit is contained in:
parent
e6d71ee7f5
commit
656e16083d
@ -114,8 +114,10 @@ CREATE TABLE IF NOT EXISTS student_challenge_errors(
|
||||
if _, err := db.Exec(`
|
||||
CREATE TABLE IF NOT EXISTS student_pong(
|
||||
id_student INTEGER NOT NULL,
|
||||
time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
last TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
count INTEGER UNSIGNED DEFAULT 1,
|
||||
state BOOLEAN NOT NULL DEFAULT 0,
|
||||
CONSTRAINT one_pong UNIQUE (id_student,state),
|
||||
FOREIGN KEY(id_student) REFERENCES students(id_student)
|
||||
) DEFAULT CHARACTER SET = utf8 COLLATE = utf8_bin;
|
||||
`); err != nil {
|
||||
|
@ -6,18 +6,19 @@ import (
|
||||
|
||||
type Pong struct {
|
||||
Date time.Time
|
||||
Count uint
|
||||
State bool
|
||||
}
|
||||
|
||||
func (s *Student) LastPongs() (pongs []*Pong, err error) {
|
||||
if rows, errr := DBQuery("SELECT time, state FROM student_pong WHERE id_student = ? ORDER BY time DESC", s.Id); errr != nil {
|
||||
if rows, errr := DBQuery("SELECT last, count, state FROM student_pong WHERE id_student = ? ORDER BY last DESC", s.Id); errr != nil {
|
||||
return nil, errr
|
||||
} else {
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
p := &Pong{}
|
||||
if err = rows.Scan(&p.Date, &p.State); err != nil {
|
||||
if err = rows.Scan(&p.Date, &p.Count, &p.State); err != nil {
|
||||
return
|
||||
}
|
||||
pongs = append(pongs, p)
|
||||
@ -31,6 +32,9 @@ func (s *Student) LastPongs() (pongs []*Pong, err error) {
|
||||
}
|
||||
|
||||
func (s *Student) OnPong(state bool) (err error) {
|
||||
_, err = DBExec("INSERT INTO student_pong (id_student, time, state) VALUES (?, ?, ?)", s.Id, time.Now(), state)
|
||||
_, err = DBExec("INSERT INTO student_pong (id_student, last, state) VALUES (?, ?, ?)", s.Id, time.Now(), state)
|
||||
if err != nil {
|
||||
_, err = DBExec("UPDATE student_pong SET last = CURRENT_TIMESTAMP, count = count + 1 WHERE id_student = ? AND state = ?", s.Id, state)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
Reference in New Issue
Block a user