Support primary key ID and unique key Name where api uses name

master
breel 2020-08-02 09:59:47 -06:00
parent 37fe9415e7
commit 8e6e86955e
14 changed files with 68 additions and 49 deletions

View File

@ -124,9 +124,9 @@ func (bdb *BoltDB) Insert(ctx context.Context, namespace string, doc interface{}
if err := bson.Unmarshal(b, &m); err != nil {
return err
}
if _, ok := m[entity.Name]; !ok {
return errors.New("primary key required to insert: did not find " + entity.Name)
} else if _, ok := m[entity.Name].(string); !ok {
if _, ok := m[entity.ID]; !ok {
return errors.New("primary key required to insert: did not find " + entity.ID)
} else if _, ok := m[entity.ID].(string); !ok {
return errors.New("primary key must be a string")
}
return bdb.db.Update(func(tx *bolt.Tx) error {
@ -134,7 +134,7 @@ func (bdb *BoltDB) Insert(ctx context.Context, namespace string, doc interface{}
if err != nil {
return err
}
k := []byte(m[entity.Name].(string))
k := []byte(m[entity.ID].(string))
v := bucket.Get(k)
if len(v) > 0 {
return errors.New("cannot insert: collision on primary key")
@ -272,7 +272,7 @@ func apply(doc bson.M, operator interface{}) (bson.M, error) {
func applyUnset(doc, operator bson.M) (bson.M, error) {
for k := range operator {
if k == entity.Name {
if k == entity.ID {
continue
}
nesting := strings.Split(k, ".")
@ -305,7 +305,7 @@ func applyUnset(doc, operator bson.M) (bson.M, error) {
func applySet(doc, operator bson.M) (bson.M, error) {
for k, v := range operator {
if k == entity.Name {
if k == entity.ID {
continue
}
nesting := strings.Split(k, ".")

View File

@ -283,15 +283,16 @@ func TestBoltDBInsert(t *testing.T) {
t.Fatal("could insert colliding object:", err)
}
ones[0].Name = "NEWNAME"
ones[0].ID = "NEWID"
ones[0].Name = "NEWID"
if err := driver.Insert(context.TODO(), testNS, ones[0]); err != nil {
t.Fatal("could not insert object with new Name:", err)
t.Fatal("could not insert object with new ID:", err)
}
if n, err := driver.Count(context.TODO(), testNS, ones[0].Query()); err != nil {
t.Fatal(err)
} else if n != 1 {
t.Fatal(err)
t.Fatal(n, err)
}
ch, err = driver.Find(context.TODO(), testNS, ones[0].Query())
@ -400,25 +401,27 @@ func fillBoltDB(t *testing.T, bdb *BoltDB) {
}
for i := 0; i < testN; i++ {
p := entity.One{
ID: "iddd-" + uuid.New().String()[:5],
Name: "name-" + uuid.New().String()[:5],
Type: "type-" + uuid.New().String()[:5],
Relationship: "rshp-" + uuid.New().String()[:5],
Title: "titl-" + uuid.New().String()[:5],
}
o := entity.One{
ID: "iddd-" + uuid.New().String()[:5],
Name: "name-" + uuid.New().String()[:5],
Type: "type-" + uuid.New().String()[:5],
Title: "titl-" + uuid.New().String()[:5],
Text: "text-" + uuid.New().String()[:5],
Modified: time.Now().UnixNano(),
Connections: map[string]entity.One{p.Name: p},
Connections: map[string]entity.One{p.ID: p},
Attachments: map[string]string{"filename": "/path/to/file"},
}
b, err := bson.Marshal(o)
if err != nil {
return err
}
if err := bucket.Put([]byte(o.Name), b); err != nil {
if err := bucket.Put([]byte(o.ID), b); err != nil {
return err
}
}

View File

@ -106,18 +106,18 @@ func (mp *Map) Insert(_ context.Context, namespace string, doc interface{}) erro
if err := bson.Unmarshal(b, &m); err != nil {
return err
}
if _, ok := m[entity.Name]; !ok {
return errors.New("primary key required to insert: did not find " + entity.Name)
} else if _, ok := m[entity.Name].(string); !ok {
if _, ok := m[entity.ID]; !ok {
return errors.New("primary key required to insert: did not find " + entity.ID)
} else if _, ok := m[entity.ID].(string); !ok {
return errors.New("primary key must be a string")
}
if _, ok := mp.db[namespace][m[entity.Name].(string)]; ok {
if _, ok := mp.db[namespace][m[entity.ID].(string)]; ok {
return errors.New("cannot insert: collision on primary key")
}
if _, ok := mp.db[namespace]; !ok {
mp.db[namespace] = make(map[string][]byte)
}
mp.db[namespace][m[entity.Name].(string)] = b
mp.db[namespace][m[entity.ID].(string)] = b
return nil
}

View File

@ -14,25 +14,27 @@ func tempMap(t *testing.T) *Map {
mp.db[testNS] = map[string][]byte{}
for i := 0; i < testN; i++ {
p := entity.One{
ID: "iddd-" + uuid.New().String()[:5],
Name: "name-" + uuid.New().String()[:5],
Type: "type-" + uuid.New().String()[:5],
Relationship: "rshp-" + uuid.New().String()[:5],
Title: "titl-" + uuid.New().String()[:5],
}
o := entity.One{
ID: "iddd-" + uuid.New().String()[:5],
Name: "name-" + uuid.New().String()[:5],
Type: "type-" + uuid.New().String()[:5],
Title: "titl-" + uuid.New().String()[:5],
Text: "text-" + uuid.New().String()[:5],
Modified: time.Now().UnixNano(),
Connections: map[string]entity.One{p.Name: p},
Connections: map[string]entity.One{p.ID: p},
Attachments: map[string]string{"filename": "/path/to/file"},
}
b, err := bson.Marshal(o)
if err != nil {
t.Fatal(err)
}
mp.db[testNS][o.Name] = b
mp.db[testNS][o.ID] = b
}
return mp
}

View File

@ -4,6 +4,7 @@ import (
"context"
"errors"
"local/dndex/config"
"local/dndex/storage/entity"
"log"
"time"
@ -77,9 +78,9 @@ func (m Mongo) Insert(ctx context.Context, namespace string, apply interface{})
if err := bson.Unmarshal(b, &mapp); err != nil {
return err
}
if _, ok := mapp["_id"]; !ok {
if _, ok := mapp[entity.ID]; !ok {
return errors.New("no _id in new object")
} else if _, ok := mapp["_id"].(string); !ok {
} else if _, ok := mapp[entity.ID].(string); !ok {
return errors.New("non-string _id in new object")
}
c := m.client.Database(m.db).Collection(namespace)

View File

@ -10,8 +10,8 @@ import (
)
const (
Name = "_id"
JSONName = "name"
ID = "_id"
Name = "name"
Relationship = "relationship"
Type = "type"
Title = "title"
@ -22,14 +22,15 @@ const (
)
type One struct {
Name string `bson:"_id,omitempty" json:"name,omitempty"`
ID string `bson:"_id,omitempty" json:"_id,omitempty"`
Name string `bson:"name,omitempty" json:"name,omitempty"`
Type string `bson:"type,omitempty" json:"type,omitempty"`
Title string `bson:"title,omitempty" json:"title,omitempty"`
Text string `bson:"text,omitempty" json:"text,omitempty"`
Relationship string `bson:"relationship,omitempty" json:"relationship,omitempty"`
Modified int64 `bson:"modified,omitempty" json:"modified,omitempty"`
Connections map[string]One `bson:"connections" json:"connections,omitempty"`
Attachments map[string]string `bson:"attachments,omitempty" json:"attachments,omitempty"`
Attachments map[string]string `bson:"attachments" json:"attachments,omitempty"`
}
func (o One) Query() One {
@ -75,10 +76,6 @@ func (o One) MarshalBSON() ([]byte, error) {
m[k] = strings.TrimSpace(v.(string))
}
}
if name, ok := m[JSONName]; ok {
m[Name] = name
delete(m, JSONName)
}
if !isMin {
connections := map[string]interface{}{}
switch m[Connections].(type) {

View File

@ -2,6 +2,7 @@ package storage
import (
"context"
"errors"
"fmt"
"local/dndex/storage/driver"
"local/dndex/storage/entity"
@ -56,6 +57,9 @@ func (g Graph) gatherOnes(ctx context.Context, ch <-chan bson.Raw) ([]entity.One
}
func (g Graph) Insert(ctx context.Context, namespace string, one entity.One) error {
if one.Name == "" || one.ID == "" {
return errors.New("cannot create document without both name and id")
}
if ones, err := g.ListCaseInsensitive(ctx, namespace, one.Name); err != nil {
return err
} else if len(ones) > 0 {

View File

@ -366,6 +366,7 @@ func TestIntegration(t *testing.T) {
func randomOne() entity.One {
return entity.One{
ID: "id-" + uuid.New().String()[:5],
Name: "name-" + uuid.New().String()[:5],
Type: "Humman",
Title: "Biggus",

View File

@ -47,6 +47,7 @@ func auth(g storage.RateLimitedGraph, w http.ResponseWriter, r *http.Request) er
func isPublic(g storage.RateLimitedGraph, r *http.Request) bool {
namespace, err := getAuthNamespace(r)
if err != nil {
return false
}
@ -103,8 +104,10 @@ func requestAuth(g storage.RateLimitedGraph, w http.ResponseWriter, r *http.Requ
}
userKey := ones[0]
id := uuid.New().String()
token := entity.One{
Name: uuid.New().String(),
ID: id,
Name: id,
Title: namespace,
}
if err := g.Insert(r.Context(), namespace, token); err != nil {

View File

@ -22,7 +22,7 @@ func TestAuth(t *testing.T) {
g := storage.NewRateLimitedGraph()
handler := jsonHandler(g)
if err := g.Insert(context.TODO(), "col."+AuthKey, entity.One{Name: UserKey, Title: "password"}); err != nil {
if err := g.Insert(context.TODO(), "col."+AuthKey, entity.One{ID: UserKey, Name: UserKey, Title: "password"}); err != nil {
t.Fatal(err)
}
@ -48,13 +48,13 @@ func TestAuth(t *testing.T) {
t.Run("auth: expired provided", func(t *testing.T) {
os.Setenv("AUTHLIFETIME", "1ms")
defer os.Setenv("AUTHLIFETIME", "1h")
one := entity.One{Name: uuid.New().String(), Title: "title"}
one := entity.One{ID: uuid.New().String(), Name: uuid.New().String(), Title: "title"}
if err := g.Insert(context.TODO(), "col", one); err != nil {
t.Fatal(err)
}
time.Sleep(time.Millisecond * 50)
r := httptest.NewRequest(http.MethodGet, "/who?namespace=col", nil)
r.Header.Set("Cookie", fmt.Sprintf("%s=%s", AuthKey, one.Name))
r.Header.Set("Cookie", fmt.Sprintf("%s=%s", AuthKey, one.ID))
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)
if w.Code != http.StatusSeeOther {
@ -82,7 +82,7 @@ func TestAuth(t *testing.T) {
t.Run("auth: provided", func(t *testing.T) {
os.Setenv("AUTHLIFETIME", "1h")
one := entity.One{Name: uuid.New().String(), Title: "title"}
one := entity.One{ID: uuid.New().String(), Name: uuid.New().String(), Title: "title"}
if err := g.Insert(context.TODO(), "col."+AuthKey, one); err != nil {
t.Fatal(err)
}

View File

@ -24,7 +24,7 @@ func TestPort(t *testing.T) {
t.Fatal(err)
}
for _, name := range []string{"A", "B", "C"} {
if err := g.Insert(context.TODO(), "col", entity.One{Name: name}); err != nil {
if err := g.Insert(context.TODO(), "col", entity.One{ID: "id-" + name, Name: name}); err != nil {
t.Fatal(err)
}
}

View File

@ -5,6 +5,8 @@ import (
"local/dndex/storage"
"local/dndex/storage/entity"
"net/http"
"github.com/google/uuid"
)
func register(g storage.RateLimitedGraph, w http.ResponseWriter, r *http.Request) error {
@ -33,6 +35,7 @@ func registerPost(g storage.RateLimitedGraph, w http.ResponseWriter, r *http.Req
return nil
}
one := entity.One{
ID: uuid.New().String(),
Name: UserKey,
Title: password,
}

View File

@ -170,7 +170,6 @@ func whoPut(namespace string, g storage.RateLimitedGraph, w http.ResponseWriter,
return nil
}
body = bytes.ReplaceAll(body, []byte(`"`+entity.JSONName+`"`), []byte(`"`+entity.Name+`"`))
op := bson.M{}
if err := json.Unmarshal(body, &op); err != nil {
return err

View File

@ -58,7 +58,7 @@ func TestWho(t *testing.T) {
if w.Code != http.StatusOK {
t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
}
o := entity.One{}
var o entity.One
if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil {
t.Fatal(err)
}
@ -105,7 +105,7 @@ func TestWho(t *testing.T) {
if w.Code != http.StatusOK {
t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
}
o := entity.One{}
var o entity.One
if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil {
t.Fatal(err)
}
@ -140,7 +140,7 @@ func TestWho(t *testing.T) {
if w.Code != http.StatusOK {
t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
}
o := entity.One{}
var o entity.One
if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil {
t.Fatal(err)
}
@ -175,7 +175,7 @@ func TestWho(t *testing.T) {
if w.Code != http.StatusOK {
t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
}
o := entity.One{}
var o entity.One
if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil {
t.Fatal(err)
}
@ -207,7 +207,7 @@ func TestWho(t *testing.T) {
if w.Code != http.StatusOK {
t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
}
o := entity.One{}
var o entity.One
if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil {
t.Fatal(err)
}
@ -260,7 +260,7 @@ func TestWho(t *testing.T) {
if w.Code != http.StatusOK {
t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
}
o := entity.One{}
var o entity.One
if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil {
t.Fatal(err)
}
@ -278,7 +278,7 @@ func TestWho(t *testing.T) {
handler, _, iwant, _, can := fresh(t)
defer can()
want := iwant
r := httptest.NewRequest(http.MethodPost, "/who?namespace=col&id="+want.Name, strings.NewReader(`{"title":"this should fail to insert"}`))
r := httptest.NewRequest(http.MethodPost, "/who?namespace=col&id="+want.Name, strings.NewReader(`{"title":"this should fail to insert", "_id":"trash"}`))
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)
if w.Code != http.StatusConflict {
@ -291,6 +291,7 @@ func TestWho(t *testing.T) {
defer can()
iwant := want
iwant.Name = ""
iwant.ID = "NEWBIE" + iwant.ID
b, err := json.Marshal(iwant)
if err != nil {
t.Fatal(err)
@ -301,7 +302,7 @@ func TestWho(t *testing.T) {
if w.Code != http.StatusOK {
t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
}
o := entity.One{}
var o entity.One
if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil {
t.Fatal(err)
}
@ -319,6 +320,7 @@ func TestWho(t *testing.T) {
handler, _, want, _, can := fresh(t)
defer can()
want.Name = "hello world #1 e ę"
want.ID = "hello world #1 e ę" + want.ID
want.Connections = nil
b, err := json.Marshal(want)
if err != nil {
@ -336,7 +338,7 @@ func TestWho(t *testing.T) {
if w.Code != http.StatusOK {
t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
}
o := entity.One{}
var o entity.One
if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil {
t.Fatal(err)
}
@ -369,6 +371,7 @@ func TestWho(t *testing.T) {
defer can()
want.Name = "hello world #4"
want.ID = "hello world #4" + want.ID
want.Connections = nil
b, err := json.Marshal(want)
if err != nil {
@ -410,6 +413,7 @@ func TestWho(t *testing.T) {
defer can()
want.Name = "hello world #1 e ę"
want.ID = "hello world #1 e ę" + want.ID
want.Connections = nil
b, err := json.Marshal(want)
if err != nil {
@ -499,6 +503,7 @@ func TestWho(t *testing.T) {
from := ones[4]
push := ones[10].Peer()
push.Relationship = "spawn"
push.ID = uuid.New().String()
b, err := json.Marshal(push)
if err != nil {
t.Fatal(err)
@ -509,7 +514,7 @@ func TestWho(t *testing.T) {
if w.Code != http.StatusOK {
t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
}
got := entity.One{}
var got entity.One
if err := json.NewDecoder(w.Body).Decode(&got); err != nil {
t.Fatal(err)
}
@ -558,7 +563,7 @@ func TestWho(t *testing.T) {
if w.Code != http.StatusOK {
t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
}
got := entity.One{}
var got entity.One
if err := json.NewDecoder(w.Body).Decode(&got); err != nil {
t.Fatal(err)
}
@ -675,7 +680,7 @@ func TestWho(t *testing.T) {
if w.Code != http.StatusOK {
t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
}
o := entity.One{}
var o entity.One
if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil {
t.Fatal(err)
}
@ -702,7 +707,7 @@ func TestWho(t *testing.T) {
if w.Code != http.StatusOK {
t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
}
o := entity.One{}
var o entity.One
if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil {
t.Fatal(err)
}
@ -744,7 +749,7 @@ func TestWho(t *testing.T) {
if w.Code != http.StatusOK {
t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
}
o := entity.One{}
var o entity.One
if err := json.NewDecoder(w.Body).Decode(&o); err != nil {
t.Fatal(err)
}
@ -943,6 +948,7 @@ func fillDB(t *testing.T, g storage.RateLimitedGraph) []entity.One {
func randomOne() entity.One {
return entity.One{
ID: "iddd-" + uuid.New().String()[:5],
Name: "name-" + uuid.New().String()[:5],
Type: "type-" + uuid.New().String()[:5],
Title: "titl-" + uuid.New().String()[:5],