Dever v0.0.2
This commit is contained in:
commit
e6e175ebcf
816 files changed
+120349
No files matched your search
@@ -0,0 +1,134 @@
|
||||
// 独立 database/sql CMD 对端;事务出错回滚,成功编码后提交。
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
sqlite3 "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
type Input struct {
|
||||
Numbers []int64 "json:\"numbers\""
|
||||
Title string "json:\"title\""
|
||||
}
|
||||
type Output struct {
|
||||
Checksum int64 "json:\"checksum\""
|
||||
Count int64 "json:\"count\""
|
||||
}
|
||||
type Article struct {
|
||||
ID, CreatedAt, Number int64
|
||||
Title string
|
||||
Amount int64
|
||||
Note sql.NullString
|
||||
}
|
||||
|
||||
func article(row *sql.Row) (value Article, err error) {
|
||||
err = row.Scan(&value.ID, &value.CreatedAt, &value.Number, &value.Title, &value.Amount, &value.Note)
|
||||
return
|
||||
}
|
||||
|
||||
func run() error {
|
||||
if os.Args[1] == "--sqlite-version" {
|
||||
version, _, _ := sqlite3.Version()
|
||||
fmt.Println(version)
|
||||
return nil
|
||||
}
|
||||
var input Input
|
||||
decoder := json.NewDecoder(bytes.NewBufferString(os.Args[2]))
|
||||
decoder.DisallowUnknownFields()
|
||||
if err := decoder.Decode(&input); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := decoder.Decode(new(any)); err != io.EOF {
|
||||
return fmt.Errorf("unexpected trailing JSON: %v", err)
|
||||
}
|
||||
if input.Numbers == nil {
|
||||
return fmt.Errorf("numbers is required")
|
||||
}
|
||||
input.Title = strings.TrimSpace(input.Title)
|
||||
if n := utf8.RuneCountInString(input.Title); n < 1 || n > 120 {
|
||||
return fmt.Errorf("title range")
|
||||
}
|
||||
sql.Register("model-sqlite", &sqlite3.SQLiteDriver{ConnectHook: func(conn *sqlite3.SQLiteConn) error {
|
||||
return conn.RegisterFunc("_dever_length", utf8.RuneCountInString, true)
|
||||
}})
|
||||
db, err := sql.Open("model-sqlite", "file:"+os.Args[1]+"?mode=rw&_journal_mode=WAL&_foreign_keys=on&_synchronous=NORMAL&_busy_timeout=30000&_txlock=immediate")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer db.Close()
|
||||
db.SetMaxOpenConns(1)
|
||||
tx, err := db.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
if _, err = tx.Exec("DELETE FROM benchmark_article WHERE number >= ?", 0); err != nil {
|
||||
return err
|
||||
}
|
||||
create, err := tx.Prepare("INSERT INTO benchmark_article (created_at,number,title,amount,note) VALUES (?,?,?,?,NULL) RETURNING id,created_at,number,title,amount,note")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer create.Close()
|
||||
read, err := tx.Prepare("SELECT id,created_at,number,title,amount,note FROM benchmark_article WHERE id = ?")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer read.Close()
|
||||
update, err := tx.Prepare("UPDATE benchmark_article SET amount = ? WHERE id = ? RETURNING id,created_at,number,title,amount,note")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer update.Close()
|
||||
var out Output
|
||||
for _, number := range input.Numbers {
|
||||
created, err := article(create.QueryRow(time.Now().UnixMilli(), number, input.Title, number))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
found, err := article(read.QueryRow(created.ID))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if found.Amount == math.MaxInt64 {
|
||||
return fmt.Errorf("amount overflow")
|
||||
}
|
||||
updated, err := article(update.QueryRow(found.Amount+1, found.ID))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if (updated.Amount > 0 && out.Checksum > math.MaxInt64-updated.Amount) || (updated.Amount < 0 && out.Checksum < math.MinInt64-updated.Amount) {
|
||||
return fmt.Errorf("checksum overflow")
|
||||
}
|
||||
out.Checksum += updated.Amount
|
||||
}
|
||||
if err = tx.QueryRow("SELECT COUNT(*) FROM benchmark_article").Scan(&out.Count); err != nil {
|
||||
return err
|
||||
}
|
||||
encoded, err := json.Marshal(out)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = tx.Commit(); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = fmt.Println(string(encoded))
|
||||
return err
|
||||
}
|
||||
|
||||
func main() {
|
||||
if err := run(); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user