2025-01-19 16:00:18 +01:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
)
|
|
|
|
|
2025-01-22 10:37:11 +01:00
|
|
|
type DbMode uint8
|
|
|
|
|
2025-01-19 16:00:18 +01:00
|
|
|
const (
|
|
|
|
DBTYPE_NONE = iota
|
|
|
|
DBTYPE_MEM
|
|
|
|
DBTYPE_FS
|
|
|
|
DBTYPE_GDBM
|
|
|
|
DBTYPE_POSTGRES
|
|
|
|
)
|
|
|
|
|
2025-01-22 10:37:11 +01:00
|
|
|
const (
|
2025-01-22 13:31:35 +01:00
|
|
|
DBMODE_ANY DbMode = iota
|
|
|
|
DBMODE_BINARY
|
2025-01-22 10:37:11 +01:00
|
|
|
DBMODE_TEXT
|
|
|
|
)
|
|
|
|
|
2025-01-19 16:00:18 +01:00
|
|
|
const (
|
|
|
|
STORETYPE_STATE = iota
|
|
|
|
STORETYPE_RESOURCE
|
|
|
|
STORETYPE_USER
|
|
|
|
_STORETYPE_MAX
|
|
|
|
)
|
|
|
|
|
2025-01-22 13:31:35 +01:00
|
|
|
var (
|
2025-01-22 19:37:54 +01:00
|
|
|
DbModeDebug = []string{"ANY", "BIN", "TXT"}
|
|
|
|
DbTypeDebug = []string{"NONE", "MEM", "FS", "GDBM", "POSTGRES"}
|
2025-01-22 13:31:35 +01:00
|
|
|
DbStoreDebug = []string{"STATE", "RESOURCE", "USER"}
|
|
|
|
)
|
|
|
|
|
2025-01-19 16:00:18 +01:00
|
|
|
type Conns map[int8]ConnData
|
|
|
|
|
|
|
|
func NewConns() Conns {
|
|
|
|
c := make(Conns)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2025-01-19 19:26:39 +01:00
|
|
|
func (c Conns) Set(conn ConnData, typ int8) {
|
2025-01-19 16:00:18 +01:00
|
|
|
if typ < 0 || typ >= _STORETYPE_MAX {
|
|
|
|
panic(fmt.Errorf("invalid store type: %d", typ))
|
|
|
|
}
|
|
|
|
c[typ] = conn
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c Conns) Have(conn *ConnData) int8 {
|
2025-01-21 16:28:55 +01:00
|
|
|
for i := range _STORETYPE_MAX {
|
2025-01-19 16:00:18 +01:00
|
|
|
ii := int8(i)
|
|
|
|
v, ok := c[ii]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2025-01-22 13:31:35 +01:00
|
|
|
if v.Raw() == conn.Raw() {
|
|
|
|
if v.Mode() == DBMODE_ANY || v.Mode() == conn.Mode() {
|
|
|
|
return ii
|
|
|
|
}
|
2025-01-19 16:00:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
type ConnData struct {
|
2025-01-21 16:28:55 +01:00
|
|
|
typ int
|
|
|
|
str string
|
2025-01-19 16:00:18 +01:00
|
|
|
domain string
|
2025-01-22 19:37:54 +01:00
|
|
|
mode DbMode
|
2025-01-19 16:00:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cd *ConnData) DbType() int {
|
|
|
|
return cd.typ
|
|
|
|
}
|
|
|
|
|
2025-01-22 13:31:35 +01:00
|
|
|
func (cd ConnData) String() string {
|
|
|
|
return fmt.Sprintf("conn: %s, mod %s, typ %s", cd.str, DbModeDebug[uint8(cd.mode)], DbTypeDebug[uint8(cd.typ)])
|
2025-01-19 16:00:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cd *ConnData) Domain() string {
|
|
|
|
return cd.domain
|
|
|
|
}
|
|
|
|
|
2025-01-22 10:37:11 +01:00
|
|
|
func (cd *ConnData) Mode() DbMode {
|
|
|
|
return cd.mode
|
|
|
|
}
|
|
|
|
|
2025-01-19 16:00:18 +01:00
|
|
|
func (cd *ConnData) Path() string {
|
|
|
|
v, _ := url.Parse(cd.str)
|
|
|
|
v.RawQuery = ""
|
|
|
|
return v.String()
|
|
|
|
}
|
2025-01-22 13:31:35 +01:00
|
|
|
|
|
|
|
func (cd *ConnData) Raw() string {
|
|
|
|
return cd.str
|
|
|
|
}
|