surrealpatch/db/lives.go

136 lines
2.9 KiB
Go
Raw Normal View History

2017-11-16 20:53:39 +00:00
// Copyright © 2016 Abcum Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package db
import (
"context"
"github.com/abcum/surreal/sql"
)
// Lives checks if any table views are specified for
// this table, and executes them in name order.
func (d *document) lives(ctx context.Context, when method) (err error) {
// If this document has not changed
// then there is no need to update
// any registered live queries.
if !d.changed() {
return nil
}
2018-04-24 23:00:36 +00:00
// Get the ID of the current fibre
// connection so that we can check
// against the ID of live queries.
id := ctx.Value(ctxKeyId).(string)
2017-11-16 20:53:39 +00:00
// Get the foreign read-only tables
// specified for this table, and
// update values which have changed.
lvs, err := d.getLV()
if err != nil {
return err
}
if len(lvs) > 0 {
for _, lv := range lvs {
if sck, ok := sockets.Load(lv.FB); ok {
2017-11-16 20:53:39 +00:00
var out interface{}
2017-11-16 20:53:39 +00:00
// Create a new context for this socket
// which has the correct connection
// variables, and auth levels.
ctx = sck.(*socket).ctx(d.ns, d.db)
2017-11-16 20:53:39 +00:00
// Check whether the change was made by
// the same connection as the live query,
// and if it is then don't notify changes.
if id == lv.FB {
continue
}
2017-11-16 20:53:39 +00:00
// Check whether this live query has the
// necessary permissions to view this
// document, or continue to the next query.
ok, err = d.grant(ctx, when)
if err != nil {
continue
} else if !ok {
continue
}
// Check whether this document matches the
// filter conditions for the live query and
// if not, then continue to the next query.
ok, err = d.check(ctx, lv.Cond)
if err != nil {
continue
} else if !ok {
continue
}
switch lv.Diff {
// If the live query has specified to only
// receive diff changes, then there will be
2018-04-14 17:02:58 +00:00
// no projected fields for this query.
2017-11-16 20:53:39 +00:00
case true:
out, _ = d.yield(ctx, lv, sql.DIFF)
2017-11-16 20:53:39 +00:00
2018-04-14 17:02:58 +00:00
// If the query has projected fields which it
// wants to receive, then let's fetch these
2017-11-16 20:53:39 +00:00
// fields, and return them to the socket.
case false:
out, _ = d.yield(ctx, lv, sql.ILLEGAL)
2017-11-16 20:53:39 +00:00
}
switch when {
case _DELETE:
sck.(*socket).queue(id, lv.ID, "DELETE", d.id)
case _CREATE:
if out != nil {
sck.(*socket).queue(id, lv.ID, "CREATE", out)
}
case _UPDATE:
if out != nil {
sck.(*socket).queue(id, lv.ID, "UPDATE", out)
}
2017-11-16 20:53:39 +00:00
}
}
}
}
return
}