OSDN Git Service

delete miner
[bytom/vapor.git] / vendor / github.com / go-kit / kit / examples / shipping / inmem / inmem.go
1 // Package inmem provides in-memory implementations of all the domain repositories.
2 package inmem
3
4 import (
5         "sync"
6
7         "github.com/go-kit/kit/examples/shipping/cargo"
8         "github.com/go-kit/kit/examples/shipping/location"
9         "github.com/go-kit/kit/examples/shipping/voyage"
10 )
11
12 type cargoRepository struct {
13         mtx    sync.RWMutex
14         cargos map[cargo.TrackingID]*cargo.Cargo
15 }
16
17 func (r *cargoRepository) Store(c *cargo.Cargo) error {
18         r.mtx.Lock()
19         defer r.mtx.Unlock()
20         r.cargos[c.TrackingID] = c
21         return nil
22 }
23
24 func (r *cargoRepository) Find(id cargo.TrackingID) (*cargo.Cargo, error) {
25         r.mtx.RLock()
26         defer r.mtx.RUnlock()
27         if val, ok := r.cargos[id]; ok {
28                 return val, nil
29         }
30         return nil, cargo.ErrUnknown
31 }
32
33 func (r *cargoRepository) FindAll() []*cargo.Cargo {
34         r.mtx.RLock()
35         defer r.mtx.RUnlock()
36         c := make([]*cargo.Cargo, 0, len(r.cargos))
37         for _, val := range r.cargos {
38                 c = append(c, val)
39         }
40         return c
41 }
42
43 // NewCargoRepository returns a new instance of a in-memory cargo repository.
44 func NewCargoRepository() cargo.Repository {
45         return &cargoRepository{
46                 cargos: make(map[cargo.TrackingID]*cargo.Cargo),
47         }
48 }
49
50 type locationRepository struct {
51         locations map[location.UNLocode]*location.Location
52 }
53
54 func (r *locationRepository) Find(locode location.UNLocode) (*location.Location, error) {
55         if l, ok := r.locations[locode]; ok {
56                 return l, nil
57         }
58         return nil, location.ErrUnknown
59 }
60
61 func (r *locationRepository) FindAll() []*location.Location {
62         l := make([]*location.Location, 0, len(r.locations))
63         for _, val := range r.locations {
64                 l = append(l, val)
65         }
66         return l
67 }
68
69 // NewLocationRepository returns a new instance of a in-memory location repository.
70 func NewLocationRepository() location.Repository {
71         r := &locationRepository{
72                 locations: make(map[location.UNLocode]*location.Location),
73         }
74
75         r.locations[location.SESTO] = location.Stockholm
76         r.locations[location.AUMEL] = location.Melbourne
77         r.locations[location.CNHKG] = location.Hongkong
78         r.locations[location.JNTKO] = location.Tokyo
79         r.locations[location.NLRTM] = location.Rotterdam
80         r.locations[location.DEHAM] = location.Hamburg
81
82         return r
83 }
84
85 type voyageRepository struct {
86         voyages map[voyage.Number]*voyage.Voyage
87 }
88
89 func (r *voyageRepository) Find(voyageNumber voyage.Number) (*voyage.Voyage, error) {
90         if v, ok := r.voyages[voyageNumber]; ok {
91                 return v, nil
92         }
93
94         return nil, voyage.ErrUnknown
95 }
96
97 // NewVoyageRepository returns a new instance of a in-memory voyage repository.
98 func NewVoyageRepository() voyage.Repository {
99         r := &voyageRepository{
100                 voyages: make(map[voyage.Number]*voyage.Voyage),
101         }
102
103         r.voyages[voyage.V100.Number] = voyage.V100
104         r.voyages[voyage.V300.Number] = voyage.V300
105         r.voyages[voyage.V400.Number] = voyage.V400
106
107         r.voyages[voyage.V0100S.Number] = voyage.V0100S
108         r.voyages[voyage.V0200T.Number] = voyage.V0200T
109         r.voyages[voyage.V0300A.Number] = voyage.V0300A
110         r.voyages[voyage.V0301S.Number] = voyage.V0301S
111         r.voyages[voyage.V0400S.Number] = voyage.V0400S
112
113         return r
114 }
115
116 type handlingEventRepository struct {
117         mtx    sync.RWMutex
118         events map[cargo.TrackingID][]cargo.HandlingEvent
119 }
120
121 func (r *handlingEventRepository) Store(e cargo.HandlingEvent) {
122         r.mtx.Lock()
123         defer r.mtx.Unlock()
124         // Make array if it's the first event with this tracking ID.
125         if _, ok := r.events[e.TrackingID]; !ok {
126                 r.events[e.TrackingID] = make([]cargo.HandlingEvent, 0)
127         }
128         r.events[e.TrackingID] = append(r.events[e.TrackingID], e)
129 }
130
131 func (r *handlingEventRepository) QueryHandlingHistory(id cargo.TrackingID) cargo.HandlingHistory {
132         r.mtx.RLock()
133         defer r.mtx.RUnlock()
134         return cargo.HandlingHistory{HandlingEvents: r.events[id]}
135 }
136
137 // NewHandlingEventRepository returns a new instance of a in-memory handling event repository.
138 func NewHandlingEventRepository() cargo.HandlingEventRepository {
139         return &handlingEventRepository{
140                 events: make(map[cargo.TrackingID][]cargo.HandlingEvent),
141         }
142 }