Skip to content

Commit 75bc3f8

Browse files
authored
Merge pull request #13 from KKhanda/develop
Develop
2 parents 3a2b9c6 + c5d02a6 commit 75bc3f8

File tree

8 files changed

+97
-38
lines changed

8 files changed

+97
-38
lines changed

README.md

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
# Graph DB Go implementation
1+
# Bubba DB - graph database, developed with Go programming language
22
Project is developed as an assignment for Advanced Databases course in Innopolis University
33

44
## DB Storage structure
5-
<img src="docs/storage.png" height="250" width="500">
5+
Node storage:
6+
<img src="docs/node.png" height="250" width="500">
7+
Relationship storage:
8+
<img src="docs/relationship.png" height="250" width="500">
9+
Property storage:
10+
<img src="docs/property.png" height="250" width="500">
611

712
## Requirements
813
Apple macOS: Install [Go](https://storage.googleapis.com/golang/go1.9.darwin-amd64.pkg)
@@ -27,3 +32,21 @@ $ ./graph-db
2732
$ go test ./... -cover
2833
```
2934

35+
## API description
36+
Example of API usage is attached in "main.go" file.
37+
38+
API calls are described in package "/api" in "storage-controller.go" file:
39+
40+
* `api.CreateDatabase` - takes two parameters: database title and db mode ("local", "distributed")
41+
* `api.SwitchDatabase` - parameter is database title. Switches only if database exist.
42+
* `api.DropDatabase` - parameter is database title. Drops database if it exists.
43+
* `api.CreateNode` - parameter is title/label of node. Creates new node and writes it into database.
44+
* `api.CreateRelationship` - takes three parameters: pointer on two nodes, which are in relationships
45+
and title of relationship. Method created a new relationship for two nodes.
46+
* `api.CreatePropertyForNode` - takes four parameters: pointer on node, title of property,
47+
type of property value and value itself. Value type could be the following: 0 - integer, 1 - double, 2 - string.
48+
Method creates a property for a given node.
49+
* `api.CreatePropertyForRelationship` - same parameters as in previous method. Creates a property for given relationship.
50+
* `api.GetNode` - parameter is id of searched node. Returns pointer on node, which is searched by id.
51+
* `api.DeleteNode` - parameter is id of node, which should be deleted. Sets node as deleted in database.
52+

cmd/graph-db/main.go

Lines changed: 54 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,55 +3,73 @@ package main
33
import (
44
"graph-db/api"
55
"graph-db/internal/app/core/structs"
6-
"graph-db/internal/app/core"
7-
"graph-db/internal/pkg/utils"
8-
"graph-db/internal/app/core/globals"
9-
"log"
6+
"strings"
7+
"strconv"
108
)
119

1210
func printNode(node structs.Node) {
1311
var str string
14-
str += "Node labels: "
15-
for _, label := range node.GetLabel().GetLabelNames() {
12+
str += strings.Join([]string{"Node id: ", strconv.Itoa(node.GetId()), ", labels: "}, "")
13+
for index, label := range node.GetLabel().GetLabelNames() {
1614
if label != nil && label.GetTitle() != "" {
1715
str += label.GetTitle()
18-
str += ", "
16+
if index != len(node.GetLabel().GetLabelNames()) - 1 {
17+
if node.GetLabel().GetLabelNames()[index+1] != nil {
18+
str += ", "
19+
} else {
20+
str += "."
21+
}
22+
}
1923
}
2024
}
21-
str += "\n"
22-
str += "Relationships: \n"
23-
relationship := node.GetRelationship()
24-
if relationship != nil {
25-
str += "\t Title: "
26-
str += relationship.GetTitle().GetTitle()
25+
if node.GetProperty() != nil {
26+
str += "\nNode properties: \n"
27+
property := node.GetProperty()
28+
var prop string
29+
index := 1
30+
for property != nil {
31+
prop += strings.Join([]string{strconv.Itoa(index), ". Property title: \"", property.GetTitle().String(),
32+
"\"; value: ", (*property.GetValue()).String(), "\n"}, "")
33+
property = property.GetNextProperty()
34+
index++
35+
}
36+
str += prop[:len(prop) - 2]
37+
}
38+
if node.GetRelationship() != nil {
39+
str += "\nRelationships: \n"
40+
relationship := node.GetRelationship()
41+
var rel string
42+
index := 0
43+
for relationship != nil {
44+
rel += strings.Join([]string{"\t", strconv.Itoa(index + 1), ". Node with id ",
45+
strconv.Itoa(relationship.GetFirstNode().GetId()), " ",
46+
relationship.GetTitle().GetTitle(), " node with id ",
47+
strconv.Itoa(relationship.GetSecondNode().GetId()), "\n"}, "")
48+
property := relationship.GetProperty()
49+
index2 := 0
50+
if property != nil {
51+
rel += "\t\tRelationship properties:\n"
52+
}
53+
for property != nil {
54+
rel += strings.Join([]string{"\t\t\t", strconv.Itoa(index2 + 1), ". Property title: \"", property.GetTitle().String(),
55+
"\"; value: ", (*property.GetValue()).String(), "\n"}, "")
56+
property = property.GetNextProperty()
57+
index2++
58+
}
59+
if relationship.GetFirstNode().GetId() == node.GetId() {
60+
relationship = relationship.GetFirstNextRelationship()
61+
} else {
62+
relationship = relationship.GetSecondNextRelationship()
63+
}
64+
index++
65+
}
66+
str += rel
2767
}
2868
println(str)
2969
}
3070

3171
func main() {
32-
//err := core.InitDb("asd", "local")
33-
////err = core.SwitchDb("asd")
34-
//utils.CheckError(err)
35-
36-
dbTitle := "asd"
37-
var dfh core.DistributedFileHandler
38-
dfh.InitFileSystem()
39-
err := core.InitDb(dbTitle, "distributed")
40-
dfh.InitDatabaseStructure(dbTitle)
41-
if err != nil {
42-
log.Fatal("Error in initialization of database")
43-
}
44-
45-
bs := utils.StringToByteArray("Test")
46-
dfh.Write(globals.NodesStore, 20, bs, 0)
47-
newBs := make([]byte, 4)
48-
dfh.Read(globals.NodesStore, 20, &newBs, 0)
49-
50-
if string(newBs) != string(bs) {
51-
log.Fatal("Byte arrays are not the same!")
52-
} else {
53-
println("Congratulations!")
54-
}
72+
api.CreateDatabase("asd", "local") // flag "distributed" for distributed
5573

5674
node1 := api.CreateNode("Kevin")
5775
node2 := api.CreateNode("Sergey")

docs/node.png

10.7 KB
Loading

docs/property.png

10.5 KB
Loading

docs/relationship.png

15.6 KB
Loading

docs/storage.png

-42.8 KB
Binary file not shown.

internal/app/core/structs/properties.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ type PropertyTitle struct {
2525
counter int
2626
}
2727

28+
func (p PropertyTitle) String() string {
29+
return p.title
30+
}
31+
2832
func CreateProperty() *Property {
2933
var p Property
3034
id, err := globals.FileHandler.ReadId(globals.PropertiesId)

internal/app/core/structs/values.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ import (
44
"graph-db/internal/app/core/globals"
55
"graph-db/internal/pkg/utils"
66
"fmt"
7+
"strconv"
78
)
89

910
type Value interface {
1011
get() interface{}
1112
set(value interface{})
13+
String() string
1214
}
1315

1416
// Integer value
@@ -32,6 +34,10 @@ func (i *IntegerValue) SetValue(value int) {
3234
i.set(value)
3335
}
3436

37+
func (i *IntegerValue) String() string {
38+
return strconv.Itoa(i.value)
39+
}
40+
3541
func CreateIntegerValue(value int) *IntegerValue {
3642
result := &IntegerValue{value: value}
3743
return result
@@ -90,6 +96,10 @@ func (s *StringValue) SetValue(value string) {
9096
s.set(value)
9197
}
9298

99+
func (s *StringValue) String() string {
100+
return s.value
101+
}
102+
93103
func (s StringValue) GetNextChunk() *StringValue {
94104
if s.nextChunk != nil {
95105
return s.nextChunk
@@ -224,6 +234,10 @@ func (d *DoubleValue) write() {
224234
utils.CheckError(err)
225235
}
226236

237+
func (d *DoubleValue) String() string {
238+
return strconv.FormatFloat(d.value, 'f', 2, 32)
239+
}
240+
227241
func GetDoubleValue(value Value) float64 {
228242
return value.(*DoubleValue).GetValue()
229243
}

0 commit comments

Comments
 (0)