-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.go
More file actions
81 lines (71 loc) · 1.86 KB
/
Copy pathengine.go
File metadata and controls
81 lines (71 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package smartcache
import (
"context"
"errors"
"log"
"sync"
)
func init() {
log.SetFlags(log.Lshortfile)
}
/**
Engine is define a structure for config and save all data.
When engine run will create a session to execute with condition.
*/
type Engine struct {
mCollection map[string]*Collection
mConfigCollection map[string]*CollectionConfig
lock *sync.RWMutex
}
type IEngine interface {
Select(ctx context.Context, collectionKey string) ICollection
AddCollection(cf ...*CollectionConfig) error
Collection() map[string]*Collection
CollectionConfig() map[string]*CollectionConfig
Info()
}
func Start(cfs ...*CollectionConfig) *Engine {
engine := &Engine{
lock: &sync.RWMutex{},
mCollection: make(map[string]*Collection),
mConfigCollection: make(map[string]*CollectionConfig),
}
if err := engine.AddCollection(cfs...); err != nil {
log.Panic(err)
}
return engine
}
func (e *Engine) Select(ctx context.Context, collectionKey string) *Session {
col, has := e.mCollection[collectionKey]
if !has {
// log.Print(E_not_found_any_collection_key)
return createSession(&SessionConfig{ctx: ctx, err: errors.New(E_not_found_any_collection_key)})
}
return createSession(&SessionConfig{collection: col, ctx: ctx})
}
func (e *Engine) AddCollection(cfs ...*CollectionConfig) error {
if len(cfs) == 0 {
return nil
}
for _, cf := range cfs {
col, err := CreateCollection(cf)
if err != nil {
return err
}
e.lock.RLock()
e.mCollection[col.key] = col
e.mConfigCollection[col.key] = cf
e.lock.RUnlock()
}
return nil
}
func (e *Engine) Collection() map[string]*Collection {
return e.mCollection
}
func (e *Engine) CollectionConfig() map[string]*CollectionConfig {
return e.mConfigCollection
}
func (e *Engine) Info() {
log.Print(len(e.mCollection), e.mCollection)
log.Print(len(e.mConfigCollection), e.mConfigCollection)
}