Skip to content

Commit 548c8b7

Browse files
committed
Merge remote-tracking branch 'origin/develop' into develop
2 parents 8f3f9e2 + 2ae328a commit 548c8b7

File tree

6 files changed

+204
-52
lines changed

6 files changed

+204
-52
lines changed

api/api_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"graph-db/internal/app/core"
55
"graph-db/internal/app/core/globals"
66
"testing"
7+
"graph-db/internal/app/core/structs"
78
)
89

910
var fh core.FileHandler
@@ -75,4 +76,49 @@ func TestCreateRelationship(t *testing.T) {
7576
t.Errorf("First node previous relationship mismatch")
7677
}
7778
DropDatabase("test_db")
79+
}
80+
81+
func TestCreatePropertyForNode(t *testing.T) {
82+
core.InitDb("test_db", "local")
83+
node1 := CreateNode("node1")
84+
property1 := CreatePropertyForNode(&node1, "color", 2, "red")
85+
if property1.GetId() != 0 {
86+
t.Errorf("Property id mismatch")
87+
}
88+
if property1.GetNextProperty() != nil {
89+
t.Errorf("Next property is not null")
90+
}
91+
if property1.GetValueType() != 2 {
92+
t.Errorf("Value type mismatch")
93+
}
94+
if structs.GetStringValue(*property1.GetValue()) != "red" {
95+
t.Errorf("String value mismatch")
96+
}
97+
property2 := CreatePropertyForNode(&node1, "amount", 0, 780)
98+
if property2.GetId() != 1 {
99+
t.Errorf("Property id mismatch")
100+
}
101+
if property1.GetNextProperty() != property2 {
102+
t.Errorf("Next property is not first")
103+
}
104+
if property2.GetValueType() != 0 {
105+
t.Errorf("Value type mismatch")
106+
}
107+
if structs.GetIntegerValue(*property2.GetValue()) != 780 {
108+
t.Errorf("Integer value mismatch")
109+
}
110+
property3 := CreatePropertyForNode(&node1, "price", 1, 70.5)
111+
if property3.GetId() != 2 {
112+
t.Errorf("Property id mismatch")
113+
}
114+
if property2.GetNextProperty() != property3 {
115+
t.Errorf("Next property is not first")
116+
}
117+
if property3.GetValueType() != 1 {
118+
t.Errorf("Value type mismatch")
119+
}
120+
if structs.GetDoubleValue(*property3.GetValue()) != 70.5 {
121+
t.Errorf("Double value mismatch")
122+
}
123+
DropDatabase("test_db")
78124
}

api/storage_controller.go

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,23 @@ func CreateRelationship(firstNode *structs.Node, secondNode *structs.Node, title
4141
firstNode.SetRelationship(relationship)
4242
relationship.SetPreviousRelationship1(nil)
4343
} else {
44-
lastRelationship1 := *firstNode.GetRelationship()
44+
lastRelationship1 := firstNode.GetRelationship()
4545
for true {
4646
if lastRelationship1.GetFirstNode().GetId() == firstNode.GetId() {
4747
if lastRelationship1.GetFirstNextRelationship() == nil {
48-
firstNode.GetRelationship().SetNextRelationship1(relationship)
49-
relationship.SetPreviousRelationship1(firstNode.GetRelationship())
48+
lastRelationship1.SetNextRelationship1(relationship)
49+
relationship.SetPreviousRelationship1(lastRelationship1)
5050
break
5151
} else {
52-
lastRelationship1 = *lastRelationship1.GetFirstNextRelationship()
52+
lastRelationship1 = lastRelationship1.GetFirstNextRelationship()
5353
}
5454
} else {
5555
if lastRelationship1.GetSecondNextRelationship() == nil {
56-
firstNode.GetRelationship().SetNextRelationship2(relationship)
57-
relationship.SetPreviousRelationship1(firstNode.GetRelationship())
56+
lastRelationship1.SetNextRelationship2(relationship)
57+
relationship.SetPreviousRelationship1(lastRelationship1)
5858
break
5959
} else {
60-
lastRelationship1 = *lastRelationship1.GetSecondNextRelationship()
60+
lastRelationship1 = lastRelationship1.GetSecondNextRelationship()
6161
}
6262
}
6363
}
@@ -67,23 +67,23 @@ func CreateRelationship(firstNode *structs.Node, secondNode *structs.Node, title
6767
secondNode.SetRelationship(relationship)
6868
relationship.SetPreviousRelationship2(nil)
6969
} else {
70-
lastRelationship2 := *secondNode.GetRelationship()
70+
lastRelationship2 := secondNode.GetRelationship()
7171
for true {
7272
if lastRelationship2.GetFirstNode().GetId() == secondNode.GetId() {
7373
if lastRelationship2.GetFirstNextRelationship() == nil {
74-
secondNode.GetRelationship().SetNextRelationship1(relationship)
75-
relationship.SetPreviousRelationship2(secondNode.GetRelationship())
74+
lastRelationship2.SetNextRelationship1(relationship)
75+
relationship.SetPreviousRelationship2(lastRelationship2)
7676
break
7777
} else {
78-
lastRelationship2 = *lastRelationship2.GetFirstNextRelationship()
78+
lastRelationship2 = lastRelationship2.GetFirstNextRelationship()
7979
}
8080
} else {
8181
if lastRelationship2.GetSecondNextRelationship() == nil {
82-
secondNode.GetRelationship().SetNextRelationship2(relationship)
83-
relationship.SetPreviousRelationship2(secondNode.GetRelationship())
82+
lastRelationship2.SetNextRelationship2(relationship)
83+
relationship.SetPreviousRelationship2(lastRelationship2)
8484
break
8585
} else {
86-
lastRelationship2 = *lastRelationship2.GetSecondNextRelationship()
86+
lastRelationship2 = lastRelationship2.GetSecondNextRelationship()
8787
}
8888
}
8989
}
@@ -96,6 +96,31 @@ func CreateRelationship(firstNode *structs.Node, secondNode *structs.Node, title
9696
return relationship
9797
}
9898

99+
func CreatePropertyForNode(node *structs.Node, title string, valueType int, value interface{}) (property *structs.Property){
100+
property = structs.CreateProperty()
101+
property.SetValueType(int8(valueType))
102+
property.SetValue(int8(valueType), value)
103+
propertyTitle, err := structs.AddPropertyTitle(title)
104+
utils.CheckError(err)
105+
property.SetTitle(propertyTitle)
106+
107+
if node.GetProperty() == nil {
108+
node.SetProperty(property)
109+
} else {
110+
lastProperty := node.GetProperty()
111+
for true {
112+
if lastProperty.GetNextProperty() == nil {
113+
lastProperty.SetNextProperty(property)
114+
break
115+
} else {
116+
lastProperty = lastProperty.GetNextProperty()
117+
}
118+
}
119+
}
120+
121+
return property
122+
}
123+
99124
func GetNode(id int) (node *structs.Node) {
100125
//todo Recover from error or make node.Get() return error
101126
return node.Get(id)

cmd/graph-db/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ package main
22

33
import (
44
"graph-db/internal/app/core"
5-
"log"
65
"graph-db/internal/pkg/utils"
76
"graph-db/internal/app/core/globals"
7+
"log"
88
)
99

1010
func main() {
1111
//err := core.InitDb("asd", "local")
12-
//err := core.SwitchDb("asd")
12+
//err = core.SwitchDb("asd")
1313
//utils.CheckError(err)
1414

1515
dbTitle := "asd"

internal/app/core/structs/properties.go

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ func CreateProperty() *Property {
3030
id, err := globals.FileHandler.ReadId(globals.PropertiesId)
3131
utils.CheckError(err)
3232
p.id = id
33+
p.valueType = -1
3334
p.isUsed = true
3435
p.isWritten = false
3536
p.write()
@@ -70,7 +71,7 @@ func (p Property) GetNextProperty() *Property {
7071
err error
7172
bs = make([]byte, globals.PropertiesSize)
7273
)
73-
if len(p.byteString) < 0 {
74+
if len(p.byteString) <= 0 {
7475
offset := p.id * globals.PropertiesSize
7576
err = globals.FileHandler.Read(globals.PropertiesStore, offset, &bs, p.id)
7677
utils.CheckError(err)
@@ -105,7 +106,7 @@ func (p *Property) GetTitle() *PropertyTitle {
105106
err error
106107
bs = make([]byte, globals.PropertiesSize)
107108
)
108-
if len(p.byteString) < 0 {
109+
if len(p.byteString) <= 0 {
109110
offset := p.id * globals.PropertiesSize
110111
err = globals.FileHandler.Read(globals.PropertiesStore, offset, &bs, p.id)
111112
utils.CheckError(err)
@@ -135,7 +136,7 @@ func (p *Property) GetValueType() int8 {
135136

136137
func (p *Property) SetValueType(valueType int8) {
137138
p.valueType = valueType
138-
p.write()
139+
//p.write()
139140
}
140141

141142
func (p *Property) GetValue() *Value {
@@ -149,7 +150,7 @@ func (p *Property) GetValue() *Value {
149150
bs = make([]byte, globals.PropertiesSize)
150151
value Value
151152
)
152-
if len(p.byteString) < 0 {
153+
if len(p.byteString) <= 0 {
153154
offset := p.id * globals.PropertiesSize
154155
err = globals.FileHandler.Read(globals.PropertiesStore, offset, &bs, p.id)
155156
utils.CheckError(err)
@@ -188,13 +189,20 @@ func (p *Property) GetValue() *Value {
188189
}
189190
}
190191

191-
func (p *Property) SetValue(value *Value) {
192-
p.value = value
192+
func (p *Property) SetValue(valueType int8, value interface{}) {
193+
var val Value
194+
if valueType == 0 {
195+
val = CreateIntegerValue(value.(int))
196+
} else if valueType == 1 {
197+
val = CreateDoubleValue(value.(float64))
198+
} else {
199+
val = CreateStringValue(value.(string))
200+
}
201+
p.value = &val
193202
p.write()
194203
}
195204

196205
func (p *Property) toBytes() (bs []byte) {
197-
198206
var (
199207
isUsed []byte
200208
nextProperty *Property
@@ -212,7 +220,12 @@ func (p *Property) toBytes() (bs []byte) {
212220
nextPropertyBs := utils.Int32ToByteArray(int32(IfNilAssignMinusOne(nextProperty)))
213221
titleBs := utils.Int32ToByteArray(int32(IfNilAssignMinusOne(title)))
214222
valueTypeBs := utils.Int8ToByteArray(valueType)
215-
valueBs := utils.Int32ToByteArray(int32(IfNilAssignMinusOne(value)))
223+
var valueBs []byte
224+
if valueType == 0 {
225+
valueBs = utils.Int32ToByteArray(int32((*value).(*IntegerValue).GetValue()))
226+
} else {
227+
valueBs = utils.Int32ToByteArray(int32(IfNilAssignMinusOne(value)))
228+
}
216229

217230
bs = append(isUsed, nextPropertyBs...)
218231
bs = append(bs, titleBs...)
@@ -241,13 +254,21 @@ func (p *Property) fromBytes(bs []byte) {
241254

242255
id, err = utils.ByteArrayToInt32(bs[1:5])
243256
utils.CheckError(err)
244-
nextProperty.id = int(id)
245-
p.nextProperty = &nextProperty
257+
if id == -1 {
258+
p.nextProperty = nil
259+
} else {
260+
nextProperty.id = int(id)
261+
p.nextProperty = &nextProperty
262+
}
246263

247264
id, err = utils.ByteArrayToInt32(bs[5:9])
248265
utils.CheckError(err)
249-
title.id = int(id)
250-
p.title = &title
266+
if id == -1 {
267+
p.title = nil
268+
} else {
269+
title.id = int(id)
270+
p.title = &title
271+
}
251272

252273
vType := utils.ByteArrayToInt8(bs[9:10])
253274
valueType = int8(vType)

internal/app/core/structs/relationships.go

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func (r *Relationship) getRelationship(start int, end int) *Relationship {
134134
relationshipId int32
135135
bs = make([]byte, globals.RelationshipsSize)
136136
)
137-
if len(r.byteString) < 0 {
137+
if len(r.byteString) <= 0 {
138138
offset := r.id * globals.RelationshipsSize
139139
err = globals.FileHandler.Read(globals.RelationshipsStore, offset, &bs, r.id)
140140
utils.CheckError(err)
@@ -186,7 +186,7 @@ func (r *Relationship) GetTitle() *RelationshipTitle {
186186
err error
187187
bs = make([]byte, globals.RelationshipsSize)
188188
)
189-
if len(r.byteString) < 0 {
189+
if len(r.byteString) <= 0 {
190190
offset := r.id * globals.RelationshipsSize
191191
err = globals.FileHandler.Read(globals.RelationshipsTitlesStore, offset, &bs, r.id)
192192
utils.CheckError(err)
@@ -264,7 +264,7 @@ func (r *Relationship) GetProperty() *Property {
264264
err error
265265
bs = make([]byte, globals.RelationshipsSize)
266266
)
267-
if len(r.byteString) < 0 {
267+
if len(r.byteString) <= 0 {
268268
offset := r.id * globals.RelationshipsSize
269269
err = globals.FileHandler.Read(globals.RelationshipsTitlesStore, offset, &bs, r.id)
270270
utils.CheckError(err)
@@ -350,40 +350,72 @@ func (r *Relationship) fromBytes(bs []byte) {
350350

351351
id, err = utils.ByteArrayToInt32(bs[1:5])
352352
utils.CheckError(err)
353-
node1.id = int(id)
354-
r.node1 = &node1
353+
if id == -1 {
354+
r.node1 = nil
355+
} else {
356+
node1.id = int(id)
357+
r.node1 = &node1
358+
}
355359
id, err = utils.ByteArrayToInt32(bs[5:9])
356360
utils.CheckError(err)
357-
node2.id = int(id)
358-
r.node2 = &node2
361+
if id == -1 {
362+
r.node2 = nil
363+
} else {
364+
node2.id = int(id)
365+
r.node2 = &node2
366+
}
359367

360368
id, err = utils.ByteArrayToInt32(bs[9:13])
361369
utils.CheckError(err)
362-
title.id = int(id)
363-
r.title = &title
370+
if id == -1 {
371+
r.title = nil
372+
} else {
373+
title.id = int(id)
374+
r.title = &title
375+
}
364376

365377
id, err = utils.ByteArrayToInt32(bs[13:17])
366378
utils.CheckError(err)
367-
previousRelationship1.id = int(id)
368-
r.previousRelationship1 = &previousRelationship1
379+
if id == -1 {
380+
r.previousRelationship1 = nil
381+
} else {
382+
previousRelationship1.id = int(id)
383+
r.previousRelationship1 = &previousRelationship1
384+
}
369385
id, err = utils.ByteArrayToInt32(bs[17:21])
370386
utils.CheckError(err)
371-
previousRelationship2.id = int(id)
372-
r.previousRelationship2 = &previousRelationship2
387+
if id == -1 {
388+
r.previousRelationship2 = nil
389+
} else {
390+
previousRelationship2.id = int(id)
391+
r.previousRelationship2 = &previousRelationship2
392+
}
373393

374394
id, err = utils.ByteArrayToInt32(bs[21:25])
375395
utils.CheckError(err)
376-
nextRelationship1.id = int(id)
377-
r.nextRelationship1 = &nextRelationship1
396+
if id == -1 {
397+
r.nextRelationship1 = nil
398+
} else {
399+
nextRelationship1.id = int(id)
400+
r.nextRelationship1 = &nextRelationship1
401+
}
378402
id, err = utils.ByteArrayToInt32(bs[25:29])
379403
utils.CheckError(err)
380-
nextRelationship2.id = int(id)
381-
r.nextRelationship2 = &nextRelationship2
404+
if id == -1 {
405+
r.nextRelationship2 = nil
406+
} else {
407+
nextRelationship2.id = int(id)
408+
r.nextRelationship2 = &nextRelationship2
409+
}
382410

383411
id, err = utils.ByteArrayToInt32(bs[29:33])
384412
utils.CheckError(err)
385-
property.id = int(id)
386-
r.property = &property
413+
if id == -1 {
414+
r.property = nil
415+
} else {
416+
property.id = int(id)
417+
r.property = &property
418+
}
387419

388420
r.isFirst, err = utils.ByteArrayToBool(bs[33:34])
389421
utils.CheckError(err)

0 commit comments

Comments
 (0)