Change connections storage from arr to map

master
Bel LaPointe 2020-07-22 22:12:40 -06:00
parent 82975a7101
commit 1a6973b02c
4 changed files with 61 additions and 59 deletions

View File

@ -6,7 +6,6 @@ import (
"time" "time"
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
) )
const ( const (
@ -29,7 +28,7 @@ type One struct {
Text string `bson:"text,omitempty" json:"text,omitempty"` Text string `bson:"text,omitempty" json:"text,omitempty"`
Relationship string `bson:"relationship,omitempty" json:"relationship,omitempty"` Relationship string `bson:"relationship,omitempty" json:"relationship,omitempty"`
Modified int64 `bson:"modified,omitempty" json:"modified,omitempty"` Modified int64 `bson:"modified,omitempty" json:"modified,omitempty"`
Connections []One `bson:"connections,omitempty" json:"connections,omitempty"` Connections map[string]One `bson:"connections,omitempty" json:"connections,omitempty"`
} }
func (o One) Query() One { func (o One) Query() One {
@ -38,14 +37,17 @@ func (o One) Query() One {
func (o One) Peers() []string { func (o One) Peers() []string {
names := make([]string, len(o.Connections)) names := make([]string, len(o.Connections))
for i := range o.Connections { i := 0
names[i] = o.Connections[i].Name for k := range o.Connections {
names[i] = o.Connections[k].Name
i += 1
} }
return names return names
} }
func (o One) MarshalBSON() ([]byte, error) { func (o One) MarshalBSON() ([]byte, error) {
if fmt.Sprint(o) != fmt.Sprint(o.Query()) { isMin := fmt.Sprint(o) == fmt.Sprint(o.Query())
if !isMin {
o.Modified = time.Now().UnixNano() o.Modified = time.Now().UnixNano()
} }
b, err := json.Marshal(o) b, err := json.Marshal(o)
@ -60,23 +62,17 @@ func (o One) MarshalBSON() ([]byte, error) {
m[Name] = name m[Name] = name
delete(m, JSONName) delete(m, JSONName)
} }
var connections primitive.A if !isMin {
connections := map[string]interface{}{}
switch m[Connections].(type) { switch m[Connections].(type) {
case nil: case nil:
case []interface{}: case map[string]interface{}:
connections = primitive.A(m[Connections].([]interface{})) connections = m[Connections].(map[string]interface{})
case primitive.A:
connections = m[Connections].(primitive.A)
default: default:
return nil, fmt.Errorf("bad type for %q: %T", Connections, m[Connections]) return nil, fmt.Errorf("bad connections type %T", m[Connections])
} }
curL := len(connections) for k := range connections {
wantL := len(o.Connections) b, err := bson.Marshal(o.Connections[k])
for i := curL; i < wantL; i++ {
connections = append(connections, nil)
}
for i, connection := range o.Connections {
b, err := bson.Marshal(connection)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -84,8 +80,9 @@ func (o One) MarshalBSON() ([]byte, error) {
if err := bson.Unmarshal(b, &m); err != nil { if err := bson.Unmarshal(b, &m); err != nil {
return nil, err return nil, err
} }
connections[i] = m connections[k] = m
} }
m[Connections] = connections m[Connections] = connections
}
return bson.Marshal(m) return bson.Marshal(m)
} }

View File

@ -31,7 +31,7 @@ func TestOneMarshalBSON(t *testing.T) {
one: (One{Name: "hello", Type: "world", Modified: 1}), one: (One{Name: "hello", Type: "world", Modified: 1}),
}, },
"w/ connections": { "w/ connections": {
one: (One{Name: "hello", Type: "world", Modified: 1, Connections: []One{One{Name: "hi", Relationship: "mom"}}}), one: (One{Name: "hello", Type: "world", Modified: 1, Connections: map[string]One{"hi": One{Name: "hi", Relationship: "mom"}}}),
}, },
} }
@ -56,8 +56,10 @@ func TestOneMarshalBSON(t *testing.T) {
} }
c.one.Modified = 0 c.one.Modified = 0
one.Modified = 0 one.Modified = 0
for i := range one.Connections { for k := range one.Connections {
one.Connections[i].Modified = 0 temp := one.Connections[k]
temp.Modified = 0
one.Connections[k] = temp
} }
if fmt.Sprint(c.one) != fmt.Sprint(one) { if fmt.Sprint(c.one) != fmt.Sprint(one) {
t.Error(c.one, one) t.Error(c.one, one)

View File

@ -2,6 +2,7 @@ package storage
import ( import (
"context" "context"
"fmt"
"local/dndex/storage/entity" "local/dndex/storage/entity"
"local/dndex/storage/operator" "local/dndex/storage/operator"
"os" "os"
@ -34,7 +35,7 @@ func TestIntegration(t *testing.T) {
randomOne(), randomOne(),
randomOne(), randomOne(),
} }
ones[0].Connections = []entity.One{entity.One{Name: ones[2].Name, Relationship: ":("}} ones[0].Connections = map[string]entity.One{ones[2].Name: entity.One{Name: ones[2].Name, Relationship: ":("}}
t.Run("graph.Insert(...)", func(t *testing.T) { t.Run("graph.Insert(...)", func(t *testing.T) {
for _, one := range ones { for _, one := range ones {
@ -68,7 +69,7 @@ func TestIntegration(t *testing.T) {
}) })
t.Run("graph.Update(foo, --bar)", func(t *testing.T) { t.Run("graph.Update(foo, --bar)", func(t *testing.T) {
err := graph.Update(ctx, ones[0].Query(), operator.Set{entity.Connections, []interface{}{}}) err := graph.Update(ctx, ones[0].Query(), operator.Set{entity.Connections, map[string]interface{}{}})
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -90,8 +91,11 @@ func TestIntegration(t *testing.T) {
} }
}) })
t.Run("graph.Update(foo, ++...); graph.Update(foo, --if :()", func(t *testing.T) { t.Run("graph.Update(foo, +=2); graph.Update(foo, -=1)", func(t *testing.T) {
err := graph.Update(ctx, ones[0].Query(), operator.Set{entity.Connections, []entity.One{entity.One{Name: "hello", Relationship: ":("}}}) err := graph.Update(ctx, ones[0].Query(), operator.Set{entity.Connections, map[string]entity.One{
"hello": entity.One{Name: "hello", Relationship: ":("},
"world": entity.One{Name: "world", Relationship: ":("},
}})
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -104,29 +108,24 @@ func TestIntegration(t *testing.T) {
if len(some1) != 1 { if len(some1) != 1 {
t.Fatal(len(some1)) t.Fatal(len(some1))
} }
if len(some1[0].Peers()) != 1 { if len(some1[0].Peers()) != 2 {
t.Fatal(some1[0].Peers()) t.Fatal(some1[0].Peers())
} }
err = graph.Update(ctx, ones[0].Query(), operator.PopIf{entity.Connections, map[string]string{entity.Relationship: ":("}}) err = graph.Update(ctx, ones[0].Query(), operator.Unset(fmt.Sprintf("%s.world", entity.Connections)))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
some2, err := graph.List(ctx, ones[0].Name) some2, err := graph.List(ctx, ones[0].Name)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
t.Logf("sm2 = %+v", some2[0]) t.Logf("sm2 = %+v", some2[0])
if len(some2) != 1 {
if len(some1) != len(some2) {
t.Fatal(len(some2)) t.Fatal(len(some2))
} }
if len(some1[0].Connections) == len(some2[0].Connections) { if len(some2[0].Peers()) != 1 {
t.Fatal(len(some2[0].Connections)) t.Fatal(some2[0].Peers())
}
if len(some2[0].Connections) == len(ones) {
t.Fatal(len(some2[0].Connections))
} }
}) })
} }
@ -139,6 +138,6 @@ func randomOne() entity.One {
Image: "/path/to.jpg", Image: "/path/to.jpg",
Text: "tee hee xd", Text: "tee hee xd",
Modified: time.Now().UnixNano(), Modified: time.Now().UnixNano(),
Connections: []entity.One{}, Connections: map[string]entity.One{},
} }
} }

View File

@ -94,10 +94,10 @@ func fillDB(t *testing.T, g storage.Graph) []entity.One {
for i := range ones { for i := range ones {
ones[i] = randomOne() ones[i] = randomOne()
if i > 0 { if i > 0 {
ones[i].Connections = []entity.One{entity.One{ ones[i].Connections[ones[i-1].Name] = entity.One{
Name: ones[i-1].Name, Name: ones[i-1].Name,
Relationship: ":D", Relationship: ":D",
}} }
} }
} }
for i := range ones { for i := range ones {
@ -110,8 +110,12 @@ func fillDB(t *testing.T, g storage.Graph) []entity.One {
t.Fatal(len(results)) t.Fatal(len(results))
} else if len(results[0].Connections) != len(ones[i].Connections) { } else if len(results[0].Connections) != len(ones[i].Connections) {
t.Fatal(len(results[0].Connections), len(ones[i].Connections)) t.Fatal(len(results[0].Connections), len(ones[i].Connections))
} else if len(results[0].Connections) > 0 && len(results[0].Connections[0].Name) == 0 { } else if len(results[0].Connections) > 0 {
t.Fatalf("name is gone: %+v", results) for k, v := range results[0].Connections {
if k == "" || v.Name == "" {
t.Fatalf("name is gone: %q:%+v", k, v)
}
}
} }
} }
return ones return ones
@ -125,6 +129,6 @@ func randomOne() entity.One {
Image: "imge-" + uuid.New().String()[:5], Image: "imge-" + uuid.New().String()[:5],
Text: "text-" + uuid.New().String()[:5], Text: "text-" + uuid.New().String()[:5],
Modified: time.Now().UnixNano(), Modified: time.Now().UnixNano(),
Connections: []entity.One{}, Connections: map[string]entity.One{},
} }
} }