diff --git a/client/collection.go b/client/collection.go index 35df2cad33..56bf05a352 100644 --- a/client/collection.go +++ b/client/collection.go @@ -169,6 +169,15 @@ type Collection interface { // GetIndexes returns all the indexes that exist on the collection. GetIndexes(ctx context.Context) ([]IndexDescription, error) + + // CreateDocIndex creates an index for the given document. + CreateDocIndex(context.Context, *Document) error + + // UpdateDocIndex updates the index for the given document. + UpdateDocIndex(ctx context.Context, oldDoc, newDoc *Document) error + + // DeleteDocIndex deletes the index for the given document. + DeleteDocIndex(context.Context, *Document) error } // DocIDResult wraps the result of an attempt at a DocID retrieval operation. diff --git a/client/db.go b/client/db.go index 0e05cafbef..7b0cc8060f 100644 --- a/client/db.go +++ b/client/db.go @@ -215,15 +215,6 @@ type Store interface { // GetAllIndexes returns all the indexes that currently exist within this [Store]. GetAllIndexes(context.Context) (map[CollectionName][]IndexDescription, error) - // CreateDocIndex creates an index for the given document. - CreateDocIndex(context.Context, Collection, *Document) error - - // UpdateDocIndex updates the index for the given document. - UpdateDocIndex(ctx context.Context, col Collection, oldDoc, newDoc *Document) error - - // DeleteDocIndex deletes the index for the given document. - DeleteDocIndex(context.Context, Collection, *Document) error - // ExecRequest executes the given GQL request against the [Store]. ExecRequest(context.Context, string) *RequestResult } diff --git a/db/collection_index.go b/db/collection_index.go index 621d05153a..7fb036498a 100644 --- a/db/collection_index.go +++ b/db/collection_index.go @@ -107,24 +107,53 @@ func (db *db) fetchCollectionIndexDescriptions( return indexDescriptions, nil } -func (db *db) getCollectionIndexes( - ctx context.Context, - txn datastore.Txn, - col client.Collection, -) ([]CollectionIndex, error) { - indexDescriptions, err := db.fetchCollectionIndexDescriptions(ctx, txn, col.ID()) +func (c *collection) CreateDocIndex(ctx context.Context, doc *client.Document) error { + txn, err := c.getTxn(ctx, false) if err != nil { - return nil, err + return err } - colIndexes := make([]CollectionIndex, 0, len(indexDescriptions)) - for _, indexDesc := range indexDescriptions { - index, err := NewCollectionIndex(col, indexDesc) - if err != nil { - return nil, err - } - colIndexes = append(colIndexes, index) + defer c.discardImplicitTxn(ctx, txn) + + err = c.indexNewDoc(ctx, txn, doc) + if err != nil { + return err + } + + return c.commitImplicitTxn(ctx, txn) +} + +func (c *collection) UpdateDocIndex(ctx context.Context, oldDoc, newDoc *client.Document) error { + txn, err := c.getTxn(ctx, false) + if err != nil { + return err + } + defer c.discardImplicitTxn(ctx, txn) + + err = c.deleteIndexedDoc(ctx, txn, oldDoc) + if err != nil { + return err + } + err = c.indexNewDoc(ctx, txn, newDoc) + if err != nil { + return err } - return colIndexes, nil + + return c.commitImplicitTxn(ctx, txn) +} + +func (c *collection) DeleteDocIndex(ctx context.Context, doc *client.Document) error { + txn, err := c.getTxn(ctx, false) + if err != nil { + return err + } + defer c.discardImplicitTxn(ctx, txn) + + err = c.deleteIndexedDoc(ctx, txn, doc) + if err != nil { + return err + } + + return c.commitImplicitTxn(ctx, txn) } func (c *collection) indexNewDoc(ctx context.Context, txn datastore.Txn, doc *client.Document) error { @@ -169,6 +198,24 @@ func (c *collection) updateIndexedDoc( return nil } +func (c *collection) deleteIndexedDoc( + ctx context.Context, + txn datastore.Txn, + doc *client.Document, +) error { + err := c.loadIndexes(ctx, txn) + if err != nil { + return err + } + for _, index := range c.indexes { + err = index.Delete(ctx, txn, doc) + if err != nil { + return err + } + } + return nil +} + // CreateIndex creates a new index on the collection. // // If the index name is empty, a name will be automatically generated. diff --git a/db/txn_db.go b/db/txn_db.go index 8af7db48df..f2fbe7cea3 100644 --- a/db/txn_db.go +++ b/db/txn_db.go @@ -184,156 +184,6 @@ func (db *explicitTxnDB) GetAllIndexes( return db.getAllIndexDescriptions(ctx, db.txn) } -// CreateDocIndex creates a new index for the given document. -func (db *implicitTxnDB) CreateDocIndex( - ctx context.Context, - col client.Collection, - doc *client.Document, -) error { - txn, err := db.NewTxn(ctx, true) - if err != nil { - return err - } - defer txn.Discard(ctx) - - err = db.createDocIndex(ctx, txn, col, doc) - if err != nil { - return err - } - - return txn.Commit(ctx) -} - -// CreateDocIndex creates a new index for the given document. -func (db *explicitTxnDB) CreateDocIndex( - ctx context.Context, - col client.Collection, - doc *client.Document, -) error { - return db.createDocIndex(ctx, db.txn, col, doc) -} - -func (db *db) createDocIndex( - ctx context.Context, - txn datastore.Txn, - col client.Collection, - doc *client.Document, -) error { - indexes, err := db.getCollectionIndexes(ctx, txn, col) - if err != nil { - return err - } - - for _, index := range indexes { - err := index.Save(ctx, txn, doc) - if err != nil { - return err - } - } - return nil -} - -// UpdateDocIndex updates the indexes for the given document. -func (db *implicitTxnDB) UpdateDocIndex( - ctx context.Context, - col client.Collection, - oldDoc *client.Document, - newDoc *client.Document, -) error { - txn, err := db.NewTxn(ctx, true) - if err != nil { - return err - } - defer txn.Discard(ctx) - - err = db.updateDocIndex(ctx, txn, col, oldDoc, newDoc) - if err != nil { - return err - } - - return txn.Commit(ctx) -} - -// UpdateDocIndex updates the indexes for the given document. -func (db *explicitTxnDB) UpdateDocIndex( - ctx context.Context, - col client.Collection, - oldDoc *client.Document, - newDoc *client.Document, -) error { - return db.updateDocIndex(ctx, db.txn, col, oldDoc, newDoc) -} - -func (db *db) updateDocIndex( - ctx context.Context, - txn datastore.Txn, - col client.Collection, - oldDoc *client.Document, - newDoc *client.Document, -) error { - indexes, err := db.getCollectionIndexes(ctx, txn, col) - if err != nil { - return err - } - - for _, index := range indexes { - err := index.Update(ctx, txn, oldDoc, newDoc) - if err != nil { - return err - } - } - return nil -} - -// DeleteDocIndex deletes the indexes for the given document. -func (db *implicitTxnDB) DeleteDocIndex( - ctx context.Context, - col client.Collection, - doc *client.Document, -) error { - txn, err := db.NewTxn(ctx, true) - if err != nil { - return err - } - defer txn.Discard(ctx) - - err = db.deleteDocIndex(ctx, txn, col, doc) - if err != nil { - return err - } - - return txn.Commit(ctx) -} - -// DeleteDocIndex deletes the indexes for the given document. -func (db *explicitTxnDB) DeleteDocIndex( - ctx context.Context, - col client.Collection, - doc *client.Document, -) error { - return db.deleteDocIndex(ctx, db.txn, col, doc) -} - -func (db *db) deleteDocIndex( - ctx context.Context, - txn datastore.Txn, - col client.Collection, - doc *client.Document, -) error { - indexes, err := db.getCollectionIndexes(ctx, txn, col) - if err != nil { - return err - } - - for _, index := range indexes { - err := index.Delete(ctx, txn, doc) - if err != nil { - return err - } - } - return nil -} - // AddSchema takes the provided GQL schema in SDL format, and applies it to the database, // creating the necessary collections, request types, etc. // diff --git a/http/client.go b/http/client.go index edfb4acb8e..142a359c5b 100644 --- a/http/client.go +++ b/http/client.go @@ -434,28 +434,3 @@ func (c *Client) Events() events.Events { func (c *Client) MaxTxnRetries() int { panic("client side database") } - -func (c *Client) CreateDocIndex( - ctx context.Context, - col client.Collection, - doc *client.Document, -) error { - panic("client side database") -} - -func (c *Client) UpdateDocIndex( - ctx context.Context, - col client.Collection, - oldDoc *client.Document, - newDoc *client.Document, -) error { - panic("client side database") -} - -func (w *Client) DeleteDocIndex( - ctx context.Context, - col client.Collection, - newDoc *client.Document, -) error { - panic("client side database") -} diff --git a/http/client_collection.go b/http/client_collection.go index b44f5045fc..85c746cc59 100644 --- a/http/client_collection.go +++ b/http/client_collection.go @@ -457,3 +457,15 @@ func (c *Collection) GetIndexes(ctx context.Context) ([]client.IndexDescription, } return indexes, nil } + +func (c *Collection) CreateDocIndex(context.Context, *client.Document) error { + return nil +} + +func (c *Collection) UpdateDocIndex(ctx context.Context, oldDoc, newDoc *client.Document) error { + return nil +} + +func (c *Collection) DeleteDocIndex(context.Context, *client.Document) error { + return nil +} diff --git a/net/server.go b/net/server.go index 39e4b829cd..206ccb3b53 100644 --- a/net/server.go +++ b/net/server.go @@ -295,7 +295,7 @@ func (s *server) PushLog(ctx context.Context, req *pb.PushLogRequest) (*pb.PushL session.Wait() bp.mergeBlocks(ctx) - err = s.syncIndexedDocs(ctx, col.WithTxn(txn), docID, store) + err = s.syncIndexedDocs(ctx, col.WithTxn(txn), docID) if err != nil { return nil, err } @@ -358,7 +358,6 @@ func (s *server) syncIndexedDocs( ctx context.Context, col client.Collection, docID client.DocID, - store client.Store, ) error { preTxnCol, err := s.db.GetCollectionByName(ctx, col.Name().Value()) if err != nil { @@ -378,11 +377,11 @@ func (s *server) syncIndexedDocs( } if isDeletedDoc { - return store.DeleteDocIndex(ctx, col, oldDoc) + return preTxnCol.DeleteDocIndex(ctx, oldDoc) } else if isNewDoc { - return store.CreateDocIndex(ctx, col, doc) + return col.CreateDocIndex(ctx, doc) } else { - return store.UpdateDocIndex(ctx, col, oldDoc, doc) + return col.UpdateDocIndex(ctx, oldDoc, doc) } } diff --git a/tests/clients/cli/wrapper.go b/tests/clients/cli/wrapper.go index 393acf411d..89ba2cf3db 100644 --- a/tests/clients/cli/wrapper.go +++ b/tests/clients/cli/wrapper.go @@ -522,28 +522,3 @@ func (w *Wrapper) WaitForPushLogByPeerEvent(id peer.ID) error { func (w *Wrapper) WaitForPushLogFromPeerEvent(id peer.ID) error { return w.node.WaitForPushLogFromPeerEvent(id) } - -func (w *Wrapper) CreateDocIndex( - ctx context.Context, - col client.Collection, - doc *client.Document, -) error { - panic("client side database") -} - -func (w *Wrapper) UpdateDocIndex( - ctx context.Context, - col client.Collection, - oldDoc *client.Document, - newDoc *client.Document, -) error { - panic("client side database") -} - -func (w *Wrapper) DeleteDocIndex( - ctx context.Context, - col client.Collection, - newDoc *client.Document, -) error { - panic("client side database") -} diff --git a/tests/clients/cli/wrapper_collection.go b/tests/clients/cli/wrapper_collection.go index 08e5119312..ed90413e8f 100644 --- a/tests/clients/cli/wrapper_collection.go +++ b/tests/clients/cli/wrapper_collection.go @@ -460,3 +460,15 @@ func (c *Collection) GetIndexes(ctx context.Context) ([]client.IndexDescription, } return indexes, nil } + +func (c *Collection) CreateDocIndex(context.Context, *client.Document) error { + return nil +} + +func (c *Collection) UpdateDocIndex(ctx context.Context, oldDoc, newDoc *client.Document) error { + return nil +} + +func (c *Collection) DeleteDocIndex(context.Context, *client.Document) error { + return nil +} diff --git a/tests/clients/http/wrapper.go b/tests/clients/http/wrapper.go index 9c52928c13..b45105a7f7 100644 --- a/tests/clients/http/wrapper.go +++ b/tests/clients/http/wrapper.go @@ -226,28 +226,3 @@ func (w *Wrapper) WaitForPushLogByPeerEvent(id peer.ID) error { func (w *Wrapper) WaitForPushLogFromPeerEvent(id peer.ID) error { return w.node.WaitForPushLogFromPeerEvent(id) } - -func (w *Wrapper) CreateDocIndex( - ctx context.Context, - col client.Collection, - doc *client.Document, -) error { - return w.client.CreateDocIndex(ctx, col, doc) -} - -func (w *Wrapper) UpdateDocIndex( - ctx context.Context, - col client.Collection, - oldDoc *client.Document, - newDoc *client.Document, -) error { - return w.client.UpdateDocIndex(ctx, col, oldDoc, newDoc) -} - -func (w *Wrapper) DeleteDocIndex( - ctx context.Context, - col client.Collection, - doc *client.Document, -) error { - return w.client.DeleteDocIndex(ctx, col, doc) -}