-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
280 lines (237 loc) · 6.55 KB
/
main.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package main
import (
"bufio"
"errors"
"fmt"
"os"
"sort"
"strconv"
"strings"
"sync"
)
// [Previous type definitions and struct definitions remain the same...]
// AttributeType represents the possible data types for attribute values
type AttributeType int
const (
StringType AttributeType = iota
FloatType
BoolType
)
// AttributeMetadata stores the data type for an attribute
type AttributeMetadata struct {
dataType AttributeType
}
// Store represents the thread-safe key-value store
type Store struct {
data map[string]map[string]interface{}
attributeTypes map[string]AttributeMetadata
mutex sync.RWMutex
}
// [Previous helper functions and methods remain the same...]
// NewStore creates a new instance of the key-value store
func NewStore() *Store {
return &Store{
data: make(map[string]map[string]interface{}),
attributeTypes: make(map[string]AttributeMetadata),
}
}
// determineType returns the AttributeType for a given string value
func determineType(value string) (AttributeType, interface{}, error) {
// Try boolean first
if value == "true" || value == "false" {
return BoolType, value == "true", nil
}
// Try float (will also handle integers)
if floatVal, err := strconv.ParseFloat(value, 64); err == nil {
return FloatType, floatVal, nil
}
// Default to string
return StringType, value, nil
}
// Put adds or updates a key-value pair in the store
func (s *Store) Put(key string, attributes [][]string) error {
s.mutex.Lock()
defer s.mutex.Unlock()
newData := make(map[string]interface{})
for _, attr := range attributes {
attrKey := attr[0]
attrValue := attr[1]
valueType, parsedValue, err := determineType(attrValue)
if err != nil {
return err
}
if metadata, exists := s.attributeTypes[attrKey]; exists {
if metadata.dataType != valueType {
return errors.New("Data Type Error")
}
} else {
s.attributeTypes[attrKey] = AttributeMetadata{dataType: valueType}
}
newData[attrKey] = parsedValue
}
s.data[key] = newData
return nil
}
// Get retrieves a value from the store
func (s *Store) Get(key string) map[string]interface{} {
s.mutex.RLock()
defer s.mutex.RUnlock()
if value, exists := s.data[key]; exists {
return value
}
return nil
}
// Delete removes a key-value pair from the store
func (s *Store) Delete(key string) {
s.mutex.Lock()
defer s.mutex.Unlock()
delete(s.data, key)
}
// Search finds all keys that have the given attribute key-value pair
func (s *Store) Search(attrKey, attrValue string) []string {
s.mutex.RLock()
defer s.mutex.RUnlock()
var results []string
_, expectedValue, _ := determineType(attrValue)
for key, attributes := range s.data {
if value, exists := attributes[attrKey]; exists {
if fmt.Sprintf("%v", value) == fmt.Sprintf("%v", expectedValue) {
results = append(results, key)
}
}
}
sort.Strings(results)
return results
}
// Keys returns all keys in the store
func (s *Store) Keys() []string {
s.mutex.RLock()
defer s.mutex.RUnlock()
keys := make([]string, 0, len(s.data))
for k := range s.data {
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}
func formatValue(value interface{}) string {
if floatVal, ok := value.(float64); ok {
// For float values, check if they're whole numbers
if floatVal == float64(int(floatVal)) {
return fmt.Sprintf("%.1f", floatVal) // Always show one decimal place
}
return fmt.Sprintf("%.2f", floatVal) // Show two decimal places
}
return fmt.Sprintf("%v", value)
}
func displayMenu() {
fmt.Println("\nAvailable Commands:")
fmt.Println("1. put <key> <attribute1> <value1> [<attribute2> <value2> ...]")
fmt.Println(" Example: put user1 name John age 30")
fmt.Println("2. get <key>")
fmt.Println(" Example: get user1")
fmt.Println("3. delete <key>")
fmt.Println(" Example: delete user1")
fmt.Println("4. search <attribute> <value>")
fmt.Println(" Example: search age 30")
fmt.Println("5. keys")
fmt.Println(" Lists all keys in the store")
fmt.Println("6. help")
fmt.Println(" Display this menu")
fmt.Println("7. exit")
fmt.Println(" Exit the program")
fmt.Println("\nEnter your command:")
}
func main() {
store := NewStore()
scanner := bufio.NewScanner(os.Stdin)
fmt.Println("Welcome to the Key-Value Store CLI")
displayMenu()
for scanner.Scan() {
line := scanner.Text()
parts := strings.Fields(line)
if len(parts) == 0 {
displayMenu()
continue
}
command := parts[0]
switch command {
case "put":
if len(parts) < 4 || len(parts)%2 != 0 {
fmt.Println("Error: Incorrect number of parameters")
fmt.Println("Usage: put <key> <attribute1> <value1> [<attribute2> <value2> ...]")
continue
}
key := parts[1]
var attributes [][]string
for i := 2; i < len(parts); i += 2 {
attributes = append(attributes, []string{parts[i], parts[i+1]})
}
if err := store.Put(key, attributes); err != nil {
fmt.Println("Error: Data Type Error")
} else {
fmt.Println("Success: Put operation completed")
}
case "get":
if len(parts) != 2 {
fmt.Println("Error: Incorrect number of parameters")
fmt.Println("Usage: get <key>")
continue
}
key := parts[1]
value := store.Get(key)
if value == nil {
fmt.Printf("No entry found for key: %s\n", key)
continue
}
keys := make([]string, 0, len(value))
for k := range value {
keys = append(keys, k)
}
sort.Strings(keys)
var output []string
for _, k := range keys {
output = append(output, fmt.Sprintf("%s: %v", k, formatValue(value[k])))
}
fmt.Println(strings.Join(output, ", "))
case "delete":
if len(parts) != 2 {
fmt.Println("Error: Incorrect number of parameters")
fmt.Println("Usage: delete <key>")
continue
}
key := parts[1]
store.Delete(key)
fmt.Println("Success: Delete operation completed")
case "search":
if len(parts) != 3 {
fmt.Println("Error: Incorrect number of parameters")
fmt.Println("Usage: search <attribute> <value>")
continue
}
attrKey, attrValue := parts[1], parts[2]
results := store.Search(attrKey, attrValue)
if len(results) > 0 {
fmt.Println("Found keys:", strings.Join(results, ", "))
} else {
fmt.Println("No matching entries found")
}
case "keys":
keys := store.Keys()
if len(keys) > 0 {
fmt.Println("All keys:", strings.Join(keys, ", "))
} else {
fmt.Println("Store is empty")
}
case "help":
displayMenu()
case "exit":
fmt.Println("Goodbye!")
return
default:
fmt.Printf("Unknown command: %s\n", command)
fmt.Println("Type 'help' to see available commands")
}
fmt.Println("\nEnter your command:")
}
}