Remove RESYNC INDEX command
This commit is contained in:
parent
7d452b610e
commit
48b644f399
6 changed files with 3 additions and 173 deletions
78
db/resync.go
78
db/resync.go
|
@ -1,78 +0,0 @@
|
||||||
// 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 (
|
|
||||||
"github.com/abcum/surreal/kvs"
|
|
||||||
"github.com/abcum/surreal/sql"
|
|
||||||
"github.com/abcum/surreal/util/item"
|
|
||||||
"github.com/abcum/surreal/util/keys"
|
|
||||||
)
|
|
||||||
|
|
||||||
func executeResyncIndexStatement(txn kvs.TX, ast *sql.ResyncIndexStatement) (out []interface{}, err error) {
|
|
||||||
|
|
||||||
var local bool
|
|
||||||
|
|
||||||
if ast.EX {
|
|
||||||
return append(out, ast), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if txn == nil {
|
|
||||||
txn, err = db.Txn(true)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer txn.Rollback()
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, TB := range ast.What {
|
|
||||||
|
|
||||||
// Remove all index data
|
|
||||||
dbeg := &keys.Index{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: TB, IX: keys.Prefix, FD: keys.Ignore}
|
|
||||||
dend := &keys.Index{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: TB, IX: keys.Suffix, FD: keys.Ignore}
|
|
||||||
if err := txn.RDel(dbeg.Encode(), dend.Encode(), 0); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fetch the items
|
|
||||||
ibeg := &keys.Thing{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: TB, ID: keys.Prefix}
|
|
||||||
iend := &keys.Thing{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: TB, ID: keys.Suffix}
|
|
||||||
kvs, _ := txn.RGet(ibeg.Encode(), iend.Encode(), 0)
|
|
||||||
for _, kv := range kvs {
|
|
||||||
doc := item.New(kv, txn, nil)
|
|
||||||
if err := resync(doc, ast); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
if local {
|
|
||||||
txn.Commit()
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func resync(doc *item.Doc, ast *sql.ResyncIndexStatement) (err error) {
|
|
||||||
|
|
||||||
if err = doc.StoreIndex(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
|
|
||||||
}
|
|
26
sql/index.go
26
sql/index.go
|
@ -54,32 +54,6 @@ func (p *Parser) parseDefineIndexStatement(explain bool) (stmt *DefineIndexState
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Parser) parseResyncIndexStatement(explain bool) (stmt *ResyncIndexStatement, err error) {
|
|
||||||
|
|
||||||
stmt = &ResyncIndexStatement{}
|
|
||||||
|
|
||||||
stmt.EX = explain
|
|
||||||
|
|
||||||
stmt.KV = p.c.Get("KV").(string)
|
|
||||||
stmt.NS = p.c.Get("NS").(string)
|
|
||||||
stmt.DB = p.c.Get("DB").(string)
|
|
||||||
|
|
||||||
if _, _, err = p.shouldBe(ON); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if stmt.What, err = p.parseNames(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, _, err = p.shouldBe(EOF, SEMICOLON); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Parser) parseRemoveIndexStatement(explain bool) (stmt *RemoveIndexStatement, err error) {
|
func (p *Parser) parseRemoveIndexStatement(explain bool) (stmt *RemoveIndexStatement, err error) {
|
||||||
|
|
||||||
stmt = &RemoveIndexStatement{}
|
stmt = &RemoveIndexStatement{}
|
||||||
|
|
|
@ -116,7 +116,7 @@ func (p *Parser) ParseSingle() (Statement, error) {
|
||||||
explain = true
|
explain = true
|
||||||
}
|
}
|
||||||
|
|
||||||
tok, _, err := p.shouldBe(USE, LET, BEGIN, CANCEL, COMMIT, ROLLBACK, SELECT, CREATE, UPDATE, INSERT, UPSERT, MODIFY, DELETE, RELATE, RECORD, DEFINE, RESYNC, REMOVE)
|
tok, _, err := p.shouldBe(USE, LET, BEGIN, CANCEL, COMMIT, ROLLBACK, SELECT, CREATE, UPDATE, INSERT, UPSERT, MODIFY, DELETE, RELATE, RECORD, DEFINE, REMOVE)
|
||||||
|
|
||||||
switch tok {
|
switch tok {
|
||||||
|
|
||||||
|
@ -147,8 +147,6 @@ func (p *Parser) ParseSingle() (Statement, error) {
|
||||||
|
|
||||||
case DEFINE:
|
case DEFINE:
|
||||||
return p.parseDefineStatement(explain)
|
return p.parseDefineStatement(explain)
|
||||||
case RESYNC:
|
|
||||||
return p.parseResyncStatement(explain)
|
|
||||||
case REMOVE:
|
case REMOVE:
|
||||||
return p.parseRemoveStatement(explain)
|
return p.parseRemoveStatement(explain)
|
||||||
|
|
||||||
|
|
|
@ -1,29 +0,0 @@
|
||||||
// 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 sql
|
|
||||||
|
|
||||||
func (p *Parser) parseResyncStatement(explain bool) (Statement, error) {
|
|
||||||
|
|
||||||
// Inspect the next token.
|
|
||||||
tok, _, err := p.shouldBe(INDEX)
|
|
||||||
|
|
||||||
switch tok {
|
|
||||||
case INDEX:
|
|
||||||
return p.parseResyncIndexStatement(explain)
|
|
||||||
default:
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -140,11 +140,11 @@ func Test_Parse_Queries_Malformed(t *testing.T) {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
sql: `!`,
|
sql: `!`,
|
||||||
err: "Found `!` but expected `USE, LET, BEGIN, CANCEL, COMMIT, ROLLBACK, SELECT, CREATE, UPDATE, INSERT, UPSERT, MODIFY, DELETE, RELATE, RECORD, DEFINE, RESYNC, REMOVE`",
|
err: "Found `!` but expected `USE, LET, BEGIN, CANCEL, COMMIT, ROLLBACK, SELECT, CREATE, UPDATE, INSERT, UPSERT, MODIFY, DELETE, RELATE, RECORD, DEFINE, REMOVE`",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
sql: `SELECT * FROM person;;;`,
|
sql: `SELECT * FROM person;;;`,
|
||||||
err: "Found `;` but expected `USE, LET, BEGIN, CANCEL, COMMIT, ROLLBACK, SELECT, CREATE, UPDATE, INSERT, UPSERT, MODIFY, DELETE, RELATE, RECORD, DEFINE, RESYNC, REMOVE`",
|
err: "Found `;` but expected `USE, LET, BEGIN, CANCEL, COMMIT, ROLLBACK, SELECT, CREATE, UPDATE, INSERT, UPSERT, MODIFY, DELETE, RELATE, RECORD, DEFINE, REMOVE`",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1746,39 +1746,6 @@ func Test_Parse_Queries_Define(t *testing.T) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_Parse_Queries_Resync(t *testing.T) {
|
|
||||||
|
|
||||||
var tests = []tester{
|
|
||||||
{
|
|
||||||
sql: `RESYNC`,
|
|
||||||
err: "Found `` but expected `INDEX`",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
sql: `RESYNC INDEX`,
|
|
||||||
err: "Found `` but expected `ON`",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
sql: `RESYNC INDEX ON`,
|
|
||||||
err: "Found `` but expected `name`",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
sql: `RESYNC INDEX ON person`,
|
|
||||||
res: &Query{Statements: []Statement{&ResyncIndexStatement{
|
|
||||||
What: []string{"person"},
|
|
||||||
}}},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
sql: `RESYNC INDEX ON person something`,
|
|
||||||
err: "Found `something` but expected `EOF, ;`",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range tests {
|
|
||||||
testsql(t, test)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_Parse_Queries_Remove(t *testing.T) {
|
func Test_Parse_Queries_Remove(t *testing.T) {
|
||||||
|
|
||||||
var tests = []tester{
|
var tests = []tester{
|
||||||
|
|
|
@ -167,7 +167,6 @@ const (
|
||||||
REJECT
|
REJECT
|
||||||
RELATE
|
RELATE
|
||||||
REMOVE
|
REMOVE
|
||||||
RESYNC
|
|
||||||
RETURN
|
RETURN
|
||||||
ROLLBACK
|
ROLLBACK
|
||||||
RULES
|
RULES
|
||||||
|
@ -329,7 +328,6 @@ var tokens = [...]string{
|
||||||
REJECT: "REJECT",
|
REJECT: "REJECT",
|
||||||
RELATE: "RELATE",
|
RELATE: "RELATE",
|
||||||
REMOVE: "REMOVE",
|
REMOVE: "REMOVE",
|
||||||
RESYNC: "RESYNC",
|
|
||||||
RETURN: "RETURN",
|
RETURN: "RETURN",
|
||||||
ROLLBACK: "ROLLBACK",
|
ROLLBACK: "ROLLBACK",
|
||||||
RULES: "RULES",
|
RULES: "RULES",
|
||||||
|
|
Loading…
Reference in a new issue