Skip to content

Commit a18c751

Browse files
committed
init core after sperated repository
0 parents  commit a18c751

15 files changed

+2238
-0
lines changed

benchmark.sh

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
go test -v -bench=. -run=XXX

cache.go

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package core
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"fmt"
7+
"time"
8+
)
9+
10+
const (
11+
// default cache expired time
12+
CacheExpired = 60 * time.Minute
13+
// not use now
14+
CacheMaxMemory = 256
15+
// evey ten minutes to clear all expired nodes
16+
CacheGcInterval = 10 * time.Minute
17+
// each time when gc to removed max nodes
18+
CacheGcMaxRemoved = 20
19+
)
20+
21+
// CacheStore is a interface to store cache
22+
type CacheStore interface {
23+
// key is primary key or composite primary key or unique key's value
24+
// value is struct's pointer
25+
// key format : <tablename>-p-<pk1>-<pk2>...
26+
Put(key string, value interface{}) error
27+
Get(key string) (interface{}, error)
28+
Del(key string) error
29+
}
30+
31+
// Cacher is an interface to provide cache
32+
// id format : u-<pk1>-<pk2>...
33+
type Cacher interface {
34+
GetIds(tableName, sql string) interface{}
35+
GetBean(tableName string, id string) interface{}
36+
PutIds(tableName, sql string, ids interface{})
37+
PutBean(tableName string, id string, obj interface{})
38+
DelIds(tableName, sql string)
39+
DelBean(tableName string, id string)
40+
ClearIds(tableName string)
41+
ClearBeans(tableName string)
42+
}
43+
44+
func encodeIds(ids []PK) (string, error) {
45+
b, err := json.Marshal(ids)
46+
if err != nil {
47+
return "", err
48+
}
49+
return string(b), nil
50+
}
51+
52+
func decodeIds(s string) ([]PK, error) {
53+
pks := make([]PK, 0)
54+
err := json.Unmarshal([]byte(s), &pks)
55+
return pks, err
56+
}
57+
58+
func GetCacheSql(m Cacher, tableName, sql string, args interface{}) ([]PK, error) {
59+
bytes := m.GetIds(tableName, GenSqlKey(sql, args))
60+
if bytes == nil {
61+
return nil, errors.New("Not Exist")
62+
}
63+
return decodeIds(bytes.(string))
64+
}
65+
66+
func PutCacheSql(m Cacher, ids []PK, tableName, sql string, args interface{}) error {
67+
bytes, err := encodeIds(ids)
68+
if err != nil {
69+
return err
70+
}
71+
m.PutIds(tableName, GenSqlKey(sql, args), bytes)
72+
return nil
73+
}
74+
75+
func GenSqlKey(sql string, args interface{}) string {
76+
return fmt.Sprintf("%v-%v", sql, args)
77+
}

column.go

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package core
2+
3+
import (
4+
"fmt"
5+
"reflect"
6+
"strings"
7+
)
8+
9+
const (
10+
TWOSIDES = iota + 1
11+
ONLYTODB
12+
ONLYFROMDB
13+
)
14+
15+
// database column
16+
type Column struct {
17+
Name string
18+
FieldName string
19+
SQLType SQLType
20+
Length int
21+
Length2 int
22+
Nullable bool
23+
Default string
24+
Indexes map[string]bool
25+
IsPrimaryKey bool
26+
IsAutoIncrement bool
27+
MapType int
28+
IsCreated bool
29+
IsUpdated bool
30+
IsCascade bool
31+
IsVersion bool
32+
fieldPath []string
33+
}
34+
35+
func NewColumn(name, fieldName string, sqlType SQLType, len1, len2 int, nullable bool) *Column {
36+
return &Column{name, fieldName, sqlType, len1, len2, nullable, "", make(map[string]bool), false, false,
37+
TWOSIDES, false, false, false, false, nil}
38+
}
39+
40+
// generate column description string according dialect
41+
func (col *Column) String(d Dialect) string {
42+
sql := d.QuoteStr() + col.Name + d.QuoteStr() + " "
43+
44+
sql += d.SqlType(col) + " "
45+
46+
if col.IsPrimaryKey {
47+
sql += "PRIMARY KEY "
48+
if col.IsAutoIncrement {
49+
sql += d.AutoIncrStr() + " "
50+
}
51+
}
52+
53+
if col.Nullable {
54+
sql += "NULL "
55+
} else {
56+
sql += "NOT NULL "
57+
}
58+
59+
if col.Default != "" {
60+
sql += "DEFAULT " + col.Default + " "
61+
}
62+
63+
return sql
64+
}
65+
66+
func (col *Column) StringNoPk(d Dialect) string {
67+
sql := d.QuoteStr() + col.Name + d.QuoteStr() + " "
68+
69+
sql += d.SqlType(col) + " "
70+
71+
if col.Nullable {
72+
sql += "NULL "
73+
} else {
74+
sql += "NOT NULL "
75+
}
76+
77+
if col.Default != "" {
78+
sql += "DEFAULT " + col.Default + " "
79+
}
80+
81+
return sql
82+
}
83+
84+
// return col's filed of struct's value
85+
func (col *Column) ValueOf(bean interface{}) (*reflect.Value, error) {
86+
dataStruct := reflect.Indirect(reflect.ValueOf(bean))
87+
return col.ValueOfV(&dataStruct)
88+
}
89+
90+
func (col *Column) ValueOfV(dataStruct *reflect.Value) (*reflect.Value, error) {
91+
var fieldValue reflect.Value
92+
var err error
93+
if col.fieldPath == nil {
94+
col.fieldPath = strings.Split(col.FieldName, ".")
95+
}
96+
97+
if len(col.fieldPath) == 1 {
98+
fieldValue = dataStruct.FieldByName(col.FieldName)
99+
} else if len(col.fieldPath) == 2 {
100+
parentField := dataStruct.FieldByName(col.fieldPath[0])
101+
if parentField.IsValid() {
102+
fieldValue = parentField.FieldByName(col.fieldPath[1])
103+
} else {
104+
err = fmt.Errorf("field %v is not valid", col.FieldName)
105+
}
106+
} else {
107+
err = fmt.Errorf("Unsupported mutliderive %v", col.FieldName)
108+
}
109+
if err != nil {
110+
return nil, err
111+
}
112+
return &fieldValue, nil
113+
}

converstion.go

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package core
2+
3+
// Conversion is an interface. A type implements Conversion will according
4+
// the custom method to fill into database and retrieve from database.
5+
type Conversion interface {
6+
FromDB([]byte) error
7+
ToDB() ([]byte, error)
8+
}

0 commit comments

Comments
 (0)