-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_impl.go
220 lines (192 loc) · 5.86 KB
/
db_impl.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package in_memory_db
import (
"fmt"
"math"
"sort"
"strings"
)
type InMemoryDB struct {
Store map[string]map[string]Value
InMemoryDBInterface
}
type Value struct {
value string
expiry int
}
func NewInMemoryDB() *InMemoryDB {
return &InMemoryDB{
Store: make(map[string]map[string]Value),
}
}
func isKeyPresent(m map[string]map[string]Value, key string) bool {
_, exist := m[key]
return exist
}
func isFieldPresent(m map[string]map[string]Value, key, field string) bool {
if isKeyPresent(m, key) {
_, exist := m[key][field]
return exist
}
return false
}
/*
--- Level 1 ---
1. Set(key, field, value string) - Should insert a
field-value pair to the record associated with key.
If the field in the record already exists, replace
the existing value with the specified value.
If record doesn't exist, create a new one.
2. Get(key, field string) *string - Should return
the value contained within field of record associated
with key. If record or field doesn't exist, should return nil
3. Delete(key, field string) bool - Should remove the field
from the record associated with key. Returns true if the field
was successfully deleted, and false if the key or the field do
not exist in the database
*/
func (db *InMemoryDB) Set(key, field, value string) {
// Custom implementation here
if !isKeyPresent(db.Store, key) {
db.Store[key] = make(map[string]Value)
}
db.Store[key][field] = Value{value: value}
}
func (db *InMemoryDB) Get(key, field string) *string {
// Custom implementation here
if !isFieldPresent(db.Store, key, field) {
return nil
}
val, exist := db.Store[key][field]
if !exist {
return nil
}
return &val.value
}
func (db *InMemoryDB) Delete(key, field string) bool {
// Custom implementation here
if !isFieldPresent(db.Store, key, field) {
return false
}
delete(db.Store[key], field)
return true
}
/*
--- Level 2 ---
1. Scan(key string) []string - Should return a list
of strings representing the fields of a record associated
with the key. The returned list should be in the following
format
["<field1>(<value1>)" , "<field2>(<value2>)", ...]
where the fields are lexicographically sorted. If specified
record doesn't exist, return empty list.
2. ScanByPrefix(key, prefix string) []string - Should return a list
of strings representing some fields of a records associated
with the key. Specifically, only fields that starts with the prefix
should be included. The returned list should be the same format as
the Scan operation with the fields sorted in lexicographical order.
*/
func (db *InMemoryDB) Scan(key string) []string {
if !isKeyPresent(db.Store, key) {
return []string{}
}
var res = []string{}
for field, val := range db.Store[key] {
res = append(res, fmt.Sprintf("%s(%s)", field, val.value))
}
sort.Strings(res)
return res
}
func (db *InMemoryDB) ScanByPrefix(key, prefix string) []string {
if !isKeyPresent(db.Store, key) {
return []string{}
}
var res = []string{}
for field, val := range db.Store[key] {
if strings.HasPrefix(field, prefix) {
res = append(res, fmt.Sprintf("%s(%s)", field, val.value))
}
}
sort.Strings(res)
return res
}
/*
--- Level 3 ---
1. SetAt(key, field, value string, timestamp int) []string -
Should insert a field-value pair or update the value of the
field in the record associated with key
2. SetAtWithTtl(key, field, value string, timestamp, ttl int) []string -
Should insert a field-value pair or update the value of the
field in the record associated with key. Also sets its Time-to-Live
starting at timestamp to be ttl. The ttl is the amount of time that this
field-value pair should exist in the database, meaning it will be avaialble
during the interval: [timestamp, timestamp + ttl]
3. DeleteAt(key, field string, timestamp int) bool
The same as Delete, but with timestamp of the operation
specified. Should return true if the field existed and was
successfully deleted and false if the key didn't exist.
4. GetAt(key, field string, timestamp int) *string
The same as Get, but with timestamp of the operation specified
5. ScanAt(key string, timestamp int) []string
The same Scan but with the timestamp of the operation specified
6. ScanPrefixAt(key, prefix string, timestamp int) []string
The same as ScanPrefix but with the timestamp of the operation specified.
*/
func (db *InMemoryDB) SetAt(key, field, value string, timestamp int) {
if !isKeyPresent(db.Store, key) {
db.Store[key] = make(map[string]Value)
}
db.Store[key][field] = Value{value: value, expiry: math.MaxInt}
}
func (db *InMemoryDB) SetAtWithTtl(key, field, value string, timestamp, ttl int) {
if !isKeyPresent(db.Store, key) {
db.Store[key] = make(map[string]Value)
}
db.Store[key][field] = Value{value: value, expiry: timestamp + ttl}
}
func (db *InMemoryDB) DeleteAt(key, field string, timestamp int) bool {
// Custom implementation here
if !isFieldPresent(db.Store, key, field) {
return false
}
val := db.Store[key][field]
delete(db.Store[key], field)
return timestamp <= val.expiry
}
func (db *InMemoryDB) GetAt(key, field string, timestamp int) *string {
if !isFieldPresent(db.Store, key, field) {
return nil
}
val := db.Store[key][field]
if timestamp <= val.expiry {
return &val.value
}
return nil
}
func (db *InMemoryDB) ScanAt(key string, timestamp int) []string {
if !isKeyPresent(db.Store, key) {
return []string{}
}
var res = []string{}
for field, val := range db.Store[key] {
if timestamp <= val.expiry {
res = append(res, fmt.Sprintf("%s(%s)", field, val.value))
}
}
sort.Strings(res)
return res
}
func (db *InMemoryDB) ScanPrefixAt(key, prefix string, timestamp int) []string {
if !isKeyPresent(db.Store, key) {
return []string{}
}
var res = []string{}
for field, val := range db.Store[key] {
if strings.HasPrefix(field, prefix) {
if timestamp <= val.expiry {
res = append(res, fmt.Sprintf("%s(%s)", field, val.value))
}
}
}
sort.Strings(res)
return res
}