Skip to content

Commit

Permalink
PutDoc, PutDocs, PutConnection, PutConnections for full create-or-rep…
Browse files Browse the repository at this point in the history
…lace functionality
  • Loading branch information
autom8ter committed Dec 24, 2020
1 parent 62057b2 commit b36ceb5
Show file tree
Hide file tree
Showing 98 changed files with 4,998 additions and 310 deletions.
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.12.0
current_version = 0.12.1
commit = False
tag = False

Expand Down
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- upgrate trigger to v0.10.0 - 20+ CEL macros added

## [0.12.0] - 2020-12-23
- refactor every instance of `TypeValidator` to `constraint` - better naming convention
- refactor every instance of `TypeValidator` to `constraint` - better naming convention

## [0.12.1] - 2020-12-23
- PutDoc, PutDocs, PutConnection, PutConnections for full create-or-replace functionality
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version := "0.12.0"
version := "0.12.1"

.DEFAULT_GOAL := help

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ https://graphikdb.github.io/graphik/

`git clone git@github.com:graphikDB/graphik.git`

`docker pull graphikdb/graphik:v0.12.0`
`docker pull graphikdb/graphik:v0.12.1`

Graphik is a Backend as a Service implemented as an identity-aware, permissioned, persistant document/graph database & pubsub server written in Go.

Expand Down Expand Up @@ -902,7 +902,7 @@ add this docker-compose.yml to ${pwd}:
version: '3.7'
services:
graphik:
image: graphikdb/graphik:v0.12.0
image: graphikdb/graphik:v0.12.1
env_file:
- .env
ports:
Expand Down
220 changes: 220 additions & 0 deletions database/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,147 @@ func (g *Graph) CreateDocs(ctx context.Context, constructors *apipb.DocConstruct
return docs, nil
}

func (g *Graph) PutDoc(ctx context.Context, doc *apipb.Doc) (*apipb.Doc, error) {
if g.raft.State() != raft2.Leader {
client, err := g.leaderClient(ctx)
if err != nil {
return nil, err
}
return client.PutDoc(invertContext(ctx), doc)
}
docs, err := g.PutDocs(ctx, &apipb.Docs{Docs: []*apipb.Doc{doc}})
if err != nil {
return nil, err
}
if len(docs.GetDocs()) == 0 {
return nil, status.Error(codes.Internal, "zero docs modified")
}
return docs.GetDocs()[0], nil
}

func (g *Graph) PutDocs(ctx context.Context, docs *apipb.Docs) (*apipb.Docs, error) {
if err := ctx.Err(); err != nil {
return nil, status.Error(codes.Canceled, err.Error())
}
if g.raft.State() != raft2.Leader {
client, err := g.leaderClient(ctx)
if err != nil {
return nil, err
}
return client.PutDocs(invertContext(ctx), docs)
}
user := g.getIdentity(ctx)
if user == nil {
return nil, status.Error(codes.Unauthenticated, "failed to get user")
}

var (
method = g.getMethod(ctx)
setDocs []*apipb.Doc
setConnections []*apipb.Connection
err error
)

if err := g.db.View(func(tx *bbolt.Tx) error {
for _, doc := range docs.GetDocs() {
if doc.GetRef().Gid == "" {
doc.Ref.Gid = ksuid.New().String()
}
path := &apipb.Ref{
Gtype: doc.GetRef().GetGtype(),
Gid: doc.GetRef().GetGid(),
}
var exists = false
if doc, err := g.getDoc(ctx, tx, path); err == nil || doc != nil {
exists = true
}
g.rangeTriggers(func(a *triggerCache) bool {
if a.trigger.GetTargetDocs() && (doc.GetRef().GetGtype() == a.trigger.GetGtype() || a.trigger.GetGtype() == apipb.Any) {
data, err := a.evalTrigger.Trigger(doc.AsMap())
if err == nil {
for k, v := range data {
val, _ := structpb.NewValue(v)
doc.GetAttributes().GetFields()[k] = val
}
}
}
return true
})
setDocs = append(setDocs, doc)
if doc.GetRef().GetGid() != user.GetRef().GetGid() && doc.GetRef().GetGtype() != user.GetRef().GetGtype() {
id := helpers.Hash([]byte(fmt.Sprintf("%s-%s", user.GetRef().String(), doc.GetRef().String())))
if !exists {
createdRef := &apipb.Ref{Gid: id, Gtype: "created"}
if !g.hasConnectionFrom(user.GetRef(), createdRef) {
setConnections = append(setConnections, &apipb.Connection{
Ref: createdRef,
Attributes: apipb.NewStruct(map[string]interface{}{}),
Directed: true,
From: user.GetRef(),
To: doc.GetRef(),
})
}
createdByRef := &apipb.Ref{Gtype: "created_by", Gid: id}
if !g.hasConnectionFrom(doc.GetRef(), createdByRef) {
setConnections = append(setConnections, &apipb.Connection{
Ref: createdByRef,
Attributes: apipb.NewStruct(map[string]interface{}{}),
Directed: true,
From: doc.GetRef(),
To: user.GetRef(),
})
if err != nil {
return err
}
}
} else {
editedRef := &apipb.Ref{Gid: id, Gtype: "edited"}
if !g.hasConnectionFrom(user.GetRef(), editedRef) {
setConnections = append(setConnections, &apipb.Connection{
Ref: editedRef,
Attributes: apipb.NewStruct(map[string]interface{}{}),
Directed: true,
From: user.GetRef(),
To: doc.GetRef(),
})
}
editedByRef := &apipb.Ref{Gtype: "edited_by", Gid: id}
if !g.hasConnectionFrom(doc.GetRef(), editedByRef) {
setConnections = append(setConnections, &apipb.Connection{
Ref: editedByRef,
Attributes: apipb.NewStruct(map[string]interface{}{}),
Directed: true,
To: user.GetRef(),
From: doc.GetRef(),
})
if err != nil {
return err
}
}
}
}
}
return nil
}); err != nil {
return nil, err
}
cmd, err := g.applyCommand(&apipb.RaftCommand{
User: user,
Method: method,
SetDocs: setDocs,
SetConnections: setConnections,
})
if err != nil {
return nil, err
}
docs = &apipb.Docs{
Docs: cmd.SetDocs,
SeekNext: "",
}
docs.Sort("")
return docs, nil
}

func (g *Graph) CreateConnection(ctx context.Context, constructor *apipb.ConnectionConstructor) (*apipb.Connection, error) {
if g.raft.State() != raft2.Leader {
client, err := g.leaderClient(ctx)
Expand Down Expand Up @@ -631,6 +772,78 @@ func (g *Graph) CreateConnections(ctx context.Context, constructors *apipb.Conne
return connectionss, nil
}

func (g *Graph) PutConnection(ctx context.Context, connection *apipb.Connection) (*apipb.Connection, error) {
if g.raft.State() != raft2.Leader {
client, err := g.leaderClient(ctx)
if err != nil {
return nil, err
}
return client.PutConnection(invertContext(ctx), connection)
}
connections, err := g.PutConnections(ctx, &apipb.Connections{Connections: []*apipb.Connection{connection}})
if err != nil {
return nil, err
}
if len(connections.GetConnections()) == 0 {
return nil, status.Error(codes.Unknown, "zero connections modified")
}
return connections.GetConnections()[0], nil
}

func (g *Graph) PutConnections(ctx context.Context, connections *apipb.Connections) (*apipb.Connections, error) {
if g.raft.State() != raft2.Leader {
client, err := g.leaderClient(ctx)
if err != nil {
return nil, err
}
return client.PutConnections(invertContext(ctx), connections)
}
user := g.getIdentity(ctx)
if user == nil {
return nil, status.Error(codes.Unauthenticated, "failed to get user")
}
var err error
if err := ctx.Err(); err != nil {
return nil, err
}
var setConnections []*apipb.Connection
if err := g.db.View(func(tx *bbolt.Tx) error {
for _, connection := range connections.GetConnections() {
g.rangeTriggers(func(a *triggerCache) bool {
if a.trigger.GetTargetConnections() && (connection.GetRef().GetGtype() == a.trigger.GetGtype() || a.trigger.GetGtype() == apipb.Any) {
data, err := a.evalTrigger.Trigger(connection.AsMap())
if err == nil {
for k, v := range data {
val, _ := structpb.NewValue(v)
connection.GetAttributes().GetFields()[k] = val
}
}
}
return true
})
setConnections = append(setConnections, connection)
}
return nil
}); err != nil {
return nil, err
}

cmd, err := g.applyCommand(&apipb.RaftCommand{
SetConnections: setConnections,
User: user,
Method: g.getMethod(ctx),
})
if err != nil {
return nil, err
}
connectionss := &apipb.Connections{
Connections: cmd.SetConnections,
SeekNext: "",
}
connectionss.Sort("")
return connectionss, nil
}

func (g *Graph) Broadcast(ctx context.Context, message *apipb.OutboundMessage) (*empty.Empty, error) {
if g.raft.State() != raft2.Leader {
client, err := g.leaderClient(ctx)
Expand Down Expand Up @@ -799,6 +1012,13 @@ func (g *Graph) GetDoc(ctx context.Context, path *apipb.Ref) (*apipb.Doc, error)
}

func (g *Graph) CreateDoc(ctx context.Context, constructor *apipb.DocConstructor) (*apipb.Doc, error) {
if g.raft.State() != raft2.Leader {
client, err := g.leaderClient(ctx)
if err != nil {
return nil, err
}
return client.CreateDoc(invertContext(ctx), constructor)
}
docs, err := g.CreateDocs(ctx, &apipb.DocConstructors{Docs: []*apipb.DocConstructor{constructor}})
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: '3.7'
services:
graphik:
image: graphikdb/graphik:v0.12.0
image: graphikdb/graphik:v0.12.1
env_file:
- .env
ports:
Expand Down
20 changes: 20 additions & 0 deletions gen/gql/docs/aggfilter.doc.html
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,26 @@ <h4 class="slds-text-title--caps slds-p-around--medium">Input Objects</h4>
PeerInput
</a>
</li>
<li title="PutConnection">
<a href="putconnection.doc.html" class="slds-navigation-list--vertical__action slds-text-link--reset slds-truncate">
PutConnection
</a>
</li>
<li title="PutConnections">
<a href="putconnections.doc.html" class="slds-navigation-list--vertical__action slds-text-link--reset slds-truncate">
PutConnections
</a>
</li>
<li title="PutDoc">
<a href="putdoc.doc.html" class="slds-navigation-list--vertical__action slds-text-link--reset slds-truncate">
PutDoc
</a>
</li>
<li title="PutDocs">
<a href="putdocs.doc.html" class="slds-navigation-list--vertical__action slds-text-link--reset slds-truncate">
PutDocs
</a>
</li>
<li title="RefConstructor">
<a href="refconstructor.doc.html" class="slds-navigation-list--vertical__action slds-text-link--reset slds-truncate">
RefConstructor
Expand Down
20 changes: 20 additions & 0 deletions gen/gql/docs/aggregate.doc.html
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,26 @@ <h4 class="slds-text-title--caps slds-p-around--medium">Input Objects</h4>
PeerInput
</a>
</li>
<li title="PutConnection">
<a href="putconnection.doc.html" class="slds-navigation-list--vertical__action slds-text-link--reset slds-truncate">
PutConnection
</a>
</li>
<li title="PutConnections">
<a href="putconnections.doc.html" class="slds-navigation-list--vertical__action slds-text-link--reset slds-truncate">
PutConnections
</a>
</li>
<li title="PutDoc">
<a href="putdoc.doc.html" class="slds-navigation-list--vertical__action slds-text-link--reset slds-truncate">
PutDoc
</a>
</li>
<li title="PutDocs">
<a href="putdocs.doc.html" class="slds-navigation-list--vertical__action slds-text-link--reset slds-truncate">
PutDocs
</a>
</li>
<li title="RefConstructor">
<a href="refconstructor.doc.html" class="slds-navigation-list--vertical__action slds-text-link--reset slds-truncate">
RefConstructor
Expand Down
20 changes: 20 additions & 0 deletions gen/gql/docs/algorithm.doc.html
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,26 @@ <h4 class="slds-text-title--caps slds-p-around--medium">Input Objects</h4>
PeerInput
</a>
</li>
<li title="PutConnection">
<a href="putconnection.doc.html" class="slds-navigation-list--vertical__action slds-text-link--reset slds-truncate">
PutConnection
</a>
</li>
<li title="PutConnections">
<a href="putconnections.doc.html" class="slds-navigation-list--vertical__action slds-text-link--reset slds-truncate">
PutConnections
</a>
</li>
<li title="PutDoc">
<a href="putdoc.doc.html" class="slds-navigation-list--vertical__action slds-text-link--reset slds-truncate">
PutDoc
</a>
</li>
<li title="PutDocs">
<a href="putdocs.doc.html" class="slds-navigation-list--vertical__action slds-text-link--reset slds-truncate">
PutDocs
</a>
</li>
<li title="RefConstructor">
<a href="refconstructor.doc.html" class="slds-navigation-list--vertical__action slds-text-link--reset slds-truncate">
RefConstructor
Expand Down
20 changes: 20 additions & 0 deletions gen/gql/docs/authorizer.doc.html
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,26 @@ <h4 class="slds-text-title--caps slds-p-around--medium">Input Objects</h4>
PeerInput
</a>
</li>
<li title="PutConnection">
<a href="putconnection.doc.html" class="slds-navigation-list--vertical__action slds-text-link--reset slds-truncate">
PutConnection
</a>
</li>
<li title="PutConnections">
<a href="putconnections.doc.html" class="slds-navigation-list--vertical__action slds-text-link--reset slds-truncate">
PutConnections
</a>
</li>
<li title="PutDoc">
<a href="putdoc.doc.html" class="slds-navigation-list--vertical__action slds-text-link--reset slds-truncate">
PutDoc
</a>
</li>
<li title="PutDocs">
<a href="putdocs.doc.html" class="slds-navigation-list--vertical__action slds-text-link--reset slds-truncate">
PutDocs
</a>
</li>
<li title="RefConstructor">
<a href="refconstructor.doc.html" class="slds-navigation-list--vertical__action slds-text-link--reset slds-truncate">
RefConstructor
Expand Down
Loading

0 comments on commit b36ceb5

Please sign in to comment.