This repository has been archived by the owner on Apr 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
113 lines (86 loc) · 2.29 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
package main
import (
"fmt"
"time"
"math/rand"
)
var db *Storage
type Person struct {
FirstName string
LastName string
Age int
City string
}
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
var lastRecord Person
func randomString(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}
func generateData(amount int) (persons []Person) {
persons = make([]Person, amount)
for i := 0; i < amount; i++ {
persons[i] = Person{randomString(4), randomString(3), rand.Int(), "Ottawa-" + randomString(2)}
}
return persons
}
func writeData(records *[]Person) {
primaries := []string{"FirstName", "LastName"}
var err error
for _, x := range *records {
err = db.Write(GetIdentifier(x, primaries), x)
if err != nil {
panic(err)
}
}
lastRecord = (*records)[len(*records) - 1]
}
func main() {
rand.Seed(time.Now().UnixNano())
// Initialization
db = &Storage{}
db.Init()
// Single write example
start := time.Now()
x := Person{"Daniil", "Furmanov", 10, "Moscow"}
db.Write(GetIdentifier(x, nil), x)
elapsed := time.Since(start)
fmt.Printf("Single write took %s\n", elapsed)
// Generate large amount of data
fmt.Println("Generating data...")
start = time.Now()
data := generateData(1000000)
elapsed = time.Since(start)
fmt.Printf("Generation took %s\n", elapsed)
// Write this data
fmt.Println("Writing the data...")
start = time.Now()
writeData(&data)
elapsed = time.Since(start)
fmt.Printf("Database now contains %d records. Writing took %s\n", db.Size(), elapsed)
// Search Example
searchPattern := map[string]interface{}{
"FirstName": lastRecord.FirstName,
"LastName": lastRecord.LastName,
}
segment := GetIdentifierFromMap(searchPattern, []string{"FirstName", "LastName"})
start = time.Now()
fmt.Println("Searching data... segment " + segment)
seg, err := db.FindSegment(segment)
if err != nil {
fmt.Println("Segment not found")
return
}
fmt.Println("Segment found. Path:", seg.Path, "; Size:", seg.Size, "; Records count:", len(seg.Records))
records := seg.FindRecords(searchPattern)
elapsed = time.Since(start)
if len(records) > 0 {
fmt.Println(records[0].Data)
} else {
fmt.Println("Zero results found")
}
fmt.Printf("Search took %s\n", elapsed)
}