surrealpatch/db/lives.go

140 lines
3.1 KiB
Go
Raw Normal View History

2021-12-14 08:12:26 +00:00
// Copyright © 2016 SurrealDB Ltd.
2017-11-16 20:53:39 +00:00
//
// 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"
2021-12-14 08:12:26 +00:00
"github.com/surrealdb/surrealdb/sql"
2017-11-16 20:53:39 +00:00
)
// 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) {
// Check if this query has been run
// in forced mode, because of an
// index or foreign table update.
forced := d.forced(ctx)
2017-11-16 20:53:39 +00:00
// If this document has not changed
// then there is no need to update
// any registered live queries.
if !forced && !d.changed {
2017-11-16 20:53:39 +00:00
return nil
}
// Get the foreign read-only tables
// specified for this table, and
// update values which have changed.
2019-11-20 13:20:27 +00:00
lvs, err := d.i.e.tx.AllLV(ctx, d.key.NS, d.key.DB, d.key.TB)
2017-11-16 20:53:39 +00:00
if err != nil {
return err
}
// Loop over the currently running
// live queries so that we can pass
// change notifications to the socket.
2017-11-16 20:53:39 +00:00
for _, lv := range lvs {
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.
2017-11-16 20:53:39 +00:00
2019-01-31 10:03:50 +00:00
if d.i.e.id == lv.FB {
continue
}
2017-11-16 20:53:39 +00:00
// Load the socket which owns the live
// query so that we can check the socket
// permissions, and send the notifications.
if sck, ok := sockets.Load(lv.FB); ok {
2017-11-16 20:53:39 +00:00
var out interface{}
// Create a new context for this socket
// which has the correct connection
// variables, and auth levels.
2019-01-31 10:03:50 +00:00
ctx = sck.(*socket).ctx()
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.
2017-11-16 20:53:39 +00:00
ok, err = d.grant(ctx, when)
if err != nil {
continue
} else if !ok {
continue
}
2017-11-16 20:53:39 +00:00
// Check whether this document matches the
// filter conditions for the live query and
// if not, then continue to the next query.
2017-11-16 20:53:39 +00:00
ok, err = d.check(ctx, lv.Cond)
if err != nil {
continue
} else if !ok {
continue
}
2017-11-16 20:53:39 +00:00
switch lv.Diff {
2017-11-16 20:53:39 +00:00
// If the live query has specified to only
// receive diff changes, then there will be
// no projected fields for this query.
2017-11-16 20:53:39 +00:00
case true:
2017-11-16 20:53:39 +00:00
out, _ = d.yield(ctx, lv, sql.DIFF)
2017-11-16 20:53:39 +00:00
// If the query has projected fields which it
// wants to receive, then let's fetch these
// fields, and return them to the socket.
2017-11-16 20:53:39 +00:00
case false:
2017-11-16 20:53:39 +00:00
out, _ = d.yield(ctx, lv, sql.ILLEGAL)
2017-11-16 20:53:39 +00:00
}
2017-11-16 20:53:39 +00:00
switch when {
case _DELETE:
2019-01-31 10:03:50 +00:00
sck.(*socket).queue(d.i.e.id, lv.ID, "DELETE", d.id)
case _CREATE:
if out != nil {
2019-01-31 10:03:50 +00:00
sck.(*socket).queue(d.i.e.id, lv.ID, "CREATE", out)
}
case _UPDATE:
if out != nil {
2019-01-31 10:03:50 +00:00
sck.(*socket).queue(d.i.e.id, lv.ID, "UPDATE", out)
}
2017-11-16 20:53:39 +00:00
}
}
}
return
}