This commit is contained in:
azw 2023-09-02 19:55:08 +08:00
commit 155220a3b2
6 changed files with 155 additions and 90 deletions

40
app.go
View File

@ -1,12 +1,13 @@
package f5
import (
"flag"
"fmt"
"os"
"q5"
"strings"
"sync"
"sync/atomic"
"time"
"os"
"q5"
)
type App interface {
@ -75,10 +76,7 @@ func (this *app) init(userApp UserApp) {
_httpCliMgr = new(HttpCliMgr)
_httpCliMgr.init()
{
var tmpNodeId, tmpInstanceId int
flag.IntVar(&tmpNodeId, "n", 0, "node id")
flag.IntVar(&tmpInstanceId, "i", 0, "instance id")
flag.Parse()
tmpNodeId, tmpInstanceId := parseArgs()
this.nodeId = int32(tmpNodeId)
this.instanceId = int32(tmpInstanceId)
}
@ -256,3 +254,31 @@ func (this *app) installTimer() {
}
})
}
func parseArgs() (int, int) {
args := os.Args[1:]
if len(args) <= 0 {
return 0, 0
}
var nodeId, instanceId int
for i := 0; i < len(args); i++ {
arg := args[i]
if strings.HasPrefix(arg, "-n") {
if len(arg) > 2 {
fmt.Sscanf(arg[2:], "%d", &nodeId)
} else if i+1 < len(args) {
fmt.Sscanf(args[i+1], "%d", &nodeId)
i++
}
} else if strings.HasPrefix(arg, "-i") {
if len(arg) > 2 {
fmt.Sscanf(arg[2:], "%d", &instanceId)
} else if i+1 < len(args) {
fmt.Sscanf(args[i+1], "%d", &instanceId)
i++
}
}
}
return nodeId, instanceId
}

8
constant_unix.go Normal file
View File

@ -0,0 +1,8 @@
//go:build unix
// +build unix
package f5
const (
SYS_LOG_ROOT = "/data/logs/%s/logs/"
)

8
constant_windows.go Normal file
View File

@ -0,0 +1,8 @@
//go:build windows
// +build windows
package f5
const (
SYS_LOG_ROOT = "d:/linux_root/data/logs/%s/logs/"
)

View File

@ -52,7 +52,7 @@ func (this *DataSet) GetByName(name string) *string {
func (this *DataSet) GetByIndex(index int32) *string {
this.GetColumns()
return this.values[index].(*string);
return this.values[index].(*string)
}
func NewDataSet(rows *sql.Rows) *DataSet {

View File

@ -1,13 +1,13 @@
package f5
import (
"q5"
"time"
"errors"
"fmt"
"math"
"q5"
"strings"
"sync"
"errors"
"time"
)
type DBStyle int32
@ -130,13 +130,35 @@ func (this *dbPool) Insert(
this.internalExec(dataSource, sql, params, cb)
}
func (this *dbPool) Replace(
dataSource string,
tblName string,
fieldsKv [][]string,
cb ExecResultCb) {
params := []string{}
sql := "REPLACE INTO `" + tblName + "` " + this.joinInsertFields(fieldsKv, &params)
this.internalExec(dataSource, sql, params, cb)
}
func (this *dbPool) Upsert(
dataSource string,
tblName string,
whereKv map[string]string,
updateKv map[string]string,
insertKv map[string]string,
whereKv [][]string,
updateKv [][]string,
insertKv [][]string,
cb ExecResultCb) {
this.OrmSelectOne(dataSource, tblName, whereKv,
func(err error, ds *DataSet) {
if err != nil {
cb(err, 0, 0)
return
}
if ds.Next() {
this.Update(dataSource, tblName, updateKv, whereKv, cb)
} else {
this.Insert(dataSource, tblName, insertKv, cb)
}
})
}
func (this *dbPool) PageQuery(
@ -160,16 +182,18 @@ func (this *dbPool) PageQuery(
}
this.internalQueryOne(
dataSource,
fmt.Sprintf("SELECT COUNT(*) FROM (%s)", finalySql),
fmt.Sprintf("SELECT COUNT(*) FROM (%s) as t", finalySql),
params,
func (err error, row *[]*string) {
func(err error, rows *DataSet) {
if err != nil {
cb(err, &pagination)
return
}
pagination.Total = q5.ToInt32(*(*row)[0])
pagination.TotalPages = int32(math.Ceil(q5.ToFloat64(*(*row)[0]) /
if rows != nil && rows.Next() {
pagination.Total = q5.ToInt32(*rows.GetByIndex(0))
pagination.TotalPages = int32(math.Ceil(q5.ToFloat64(*rows.GetByIndex(0)) /
float64(pagination.PerPage)))
}
start := pagination.PerPage * (pagination.CurrentPage - 1)
limit := pagination.PerPage
this.internalQuery(
@ -192,14 +216,15 @@ func (this *dbPool) borrowConn(name string) *dataSource {
for tryCount < 5 {
{
this.lock.Lock()
defer this.lock.Unlock()
if head, ok := this.dataSourceHash[name]; ok {
if !head.Empty() {
next := head.Next()
next.Del()
this.lock.Unlock()
return next.GetData().(*dataSource)
}
}
this.lock.Unlock()
}
time.Sleep(time.Second * 1)
tryCount++
@ -271,7 +296,7 @@ func (this *dbPool) joinInsertFields(fieldsKv [][]string, params *[]string) stri
*params = append(*params, items[1])
}
sql += ")"
return ""
return sql
}
func (this *dbPool) internalExec(dataSource string, sql string, params []string,
@ -334,20 +359,17 @@ func (this *dbPool) internalQueryOne(dataSource string, sql string, params []str
rows, err := ds.conn.Query(sql, q5.ToInterfaces(params)...)
this.returnConn(ds)
values := &[]*string{}
var dataSet *DataSet
if err == nil {
dataSet := NewDataSet(rows)
if dataSet.Next() {
values = dataSet.GetValues()
}
dataSet = NewDataSet(rows)
}
GetSysLog().Info("xxxxxxxxxxxxxxxxxxx")
if this.style == GO_STYLE_DB {
cb(err, values)
cb(err, dataSet)
} else {
_app.RegisterMainThreadCb(
func() {
cb(err, values)
cb(err, dataSet)
})
}
}

View File

@ -1,11 +1,11 @@
package f5
import (
"fmt"
"os"
"q5"
"sync"
"time"
"fmt"
"q5"
)
const (
@ -18,8 +18,9 @@ const (
LOG_EMERGENCY = iota
)
const SYS_LOG_ROOT = "/data/logs/%s/logs/"
const SYS_LOG_FILENAME = "log_%d_%s.log"
const (
SYS_LOG_FILENAME = "log_%d_%s.log"
)
type LogMsgNode struct {
category int32