-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
78 lines (70 loc) · 1.85 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
package main
//TODO:: Handle database close, connection monitoring and closing.
import (
"encoding/json"
"fmt"
"log"
"os"
)
func main() {
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "Run err: %v", err)
}
}
func run() error {
//Create new database
db := Database{
make(map[string]*Collection),
make([]*Connection, 0),
}
//Some nonsense documents.
Alex := map[string]interface{}{
"Name": "Alex",
"Age": 99,
}
Bob := map[string]interface{}{
"Name": "Bob",
"Age": 27,
}
Steven := map[string]interface{}{
"Name": "Steven",
"Age": 9,
}
Chris := map[string]interface{}{
"Name": "Chris",
"Age": 14,
}
//Create a new collection named "TEST".
db.Create("TEST")
//Create a connection to the "TEST" collection.
conn := db.Connect("TEST")
//Insert these documents a whole bunch.
conn.Dispatch("INSERT", Alex, Bob, Steven, Chris)
conn.Dispatch("INSERT", Alex, Bob, Steven, Chris)
conn.Dispatch("INSERT", Alex, Bob, Steven, Chris)
conn.Dispatch("INSERT", Alex, Bob, Steven, Chris)
conn.Dispatch("INSERT", Alex, Bob, Steven, Chris)
conn.Dispatch("INSERT", Alex, Bob, Steven, Chris)
conn.Dispatch("INSERT", Alex, Bob, Steven, Chris)
conn.Dispatch("INSERT", Alex, Bob, Steven, Chris)
conn.Dispatch("INSERT", Alex, Bob, Steven, Chris)
conn.Dispatch("INSERT", Alex, Bob, Steven, Chris)
conn.Dispatch("INSERT", Alex, Bob, Steven, Chris)
//Find all documents with a "Name" key matching "Alex" OR "Age" key matching "27"
conn.Dispatch("FIND", map[string]interface{}{"Name": "Alex"}, map[string]interface{}{"Age": 27})
//Just printing some stuff out.
for {
if bytes := conn.GetBytes(); bytes != nil {
resp := make(map[string]interface{})
err := json.Unmarshal(bytes, &resp)
if err != nil {
log.Printf("%v\n%v\n", err, bytes)
} else {
for k, v := range resp {
log.Println(k, v)
}
}
}
}
return nil
}