-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmemdb.go
156 lines (124 loc) · 3.73 KB
/
memdb.go
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package memdb
import (
"context"
"fmt"
"github.com/hashicorp/go-memdb"
"go.opentelemetry.io/otel/attribute"
"github.com/lonnblad/shipment-service-backend/storage"
"github.com/lonnblad/shipment-service-backend/trace"
)
var _ storage.ShipmentStorage = &ShipmentStorage{}
const (
writeMode = true
readMode = false
tableShipments = "shipment"
tableShipmentsIndexKeyTenant = "tenant"
tableShipmentsIndexFieldTenant = "TenantID"
tableShipmentsIndexKeyShipment = "id"
tableShipmentsIndexFieldShipment = "ID"
)
// Create the DB schema
var schema = &memdb.DBSchema{
Tables: map[string]*memdb.TableSchema{
tableShipments: {
Name: tableShipments,
Indexes: map[string]*memdb.IndexSchema{
tableShipmentsIndexKeyShipment: {
Name: tableShipmentsIndexKeyShipment,
Unique: true,
Indexer: &memdb.CompoundIndex{
Indexes: []memdb.Indexer{
&memdb.UUIDFieldIndex{Field: tableShipmentsIndexFieldTenant},
&memdb.UUIDFieldIndex{Field: tableShipmentsIndexFieldShipment},
},
},
},
tableShipmentsIndexKeyTenant: {
Name: tableShipmentsIndexKeyTenant,
Unique: false,
Indexer: &memdb.UUIDFieldIndex{Field: tableShipmentsIndexFieldTenant},
},
},
},
},
}
// ShipmentStorage implements storage.ShipmentStorage
type ShipmentStorage struct {
db *memdb.MemDB
}
// NewShipmentStorage will return a pointer to a new in-mem ShipmentStorage
func NewShipmentStorage() (_ *ShipmentStorage, err error) {
db, err := memdb.NewMemDB(schema)
if err != nil {
err = fmt.Errorf("failed to create a new memdb: %w", err)
return
}
return &ShipmentStorage{db: db}, nil
}
func (s *ShipmentStorage) StoreShipment(ctx context.Context, shipment storage.Shipment) error {
_, span := trace.Tracer().Start(ctx, "memdb.StoreShipment")
defer span.End()
span.SetAttributes(
attribute.String("shipment.tenant_id", shipment.TenantID),
attribute.String("shipment.id", shipment.ID),
)
txn := s.db.Txn(writeMode)
defer txn.Commit()
err := txn.Insert(tableShipments, shipment)
if err != nil {
txn.Abort()
return fmt.Errorf("failed to insert shipment: %w", err)
}
return nil
}
func (s *ShipmentStorage) GetShipment(ctx context.Context, tenantID, shipmentID string) (_ storage.Shipment, err error) {
_, span := trace.Tracer().Start(ctx, "memdb.GetShipment")
defer span.End()
span.SetAttributes(
attribute.String("tenant_id", tenantID),
attribute.String("shipment_id", shipmentID),
)
txn := s.db.Txn(readMode)
obj, err := txn.First(tableShipments, tableShipmentsIndexKeyShipment, tenantID, shipmentID)
if err != nil {
err = fmt.Errorf("could not look up shipment: %w", err)
return
}
if obj == nil {
err = fmt.Errorf("could not find up shipment: %w", err)
return
}
return obj.(storage.Shipment), nil
}
func (s *ShipmentStorage) ListShipments(ctx context.Context, tenantID string, limit, offset int) (_ []storage.Shipment, err error) {
_, span := trace.Tracer().Start(ctx, "memdb.ListShipments")
defer span.End()
span.SetAttributes(
attribute.String("tenant_id", tenantID),
attribute.Int("limit", limit),
attribute.Int("offset", offset),
)
txn := s.db.Txn(readMode)
it, err := txn.Get(tableShipments, tableShipmentsIndexKeyTenant, tenantID)
if err != nil {
err = fmt.Errorf("could not look up shipments: %w", err)
return
}
shipments := make([]storage.Shipment, 0, limit)
var limitCounter = 0
if limitCounter == limit {
return shipments, nil
}
var offsetCounter = 0
for obj := it.Next(); obj != nil; obj = it.Next() {
if offsetCounter++; offsetCounter <= offset {
continue
}
shipment := obj.(storage.Shipment)
shipments = append(shipments, shipment)
if limitCounter++; limitCounter == limit {
break
}
}
return shipments, nil
}