Skip to content

Commit

Permalink
Move index methods from Store to Collection
Browse files Browse the repository at this point in the history
  • Loading branch information
islamaliev committed Mar 7, 2024
1 parent 710d8c5 commit ef94bf8
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 239 deletions.
9 changes: 9 additions & 0 deletions client/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 0 additions & 9 deletions client/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
67 changes: 67 additions & 0 deletions db/collection_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,55 @@ func (db *db) getCollectionIndexes(
return colIndexes, nil

Check warning on line 127 in db/collection_index.go

View check run for this annotation

Codecov / codecov/patch

db/collection_index.go#L127

Added line #L127 was not covered by tests
}

func (c *collection) CreateDocIndex(ctx context.Context, doc *client.Document) error {
txn, err := c.getTxn(ctx, false)
if err != nil {
return err
}

Check warning on line 134 in db/collection_index.go

View check run for this annotation

Codecov / codecov/patch

db/collection_index.go#L133-L134

Added lines #L133 - L134 were not covered by tests
defer c.discardImplicitTxn(ctx, txn)

err = c.indexNewDoc(ctx, txn, doc)
if err != nil {
return err
}

Check warning on line 140 in db/collection_index.go

View check run for this annotation

Codecov / codecov/patch

db/collection_index.go#L139-L140

Added lines #L139 - L140 were not covered by tests

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
}

Check warning on line 149 in db/collection_index.go

View check run for this annotation

Codecov / codecov/patch

db/collection_index.go#L148-L149

Added lines #L148 - L149 were not covered by tests
defer c.discardImplicitTxn(ctx, txn)

err = c.deleteIndexedDoc(ctx, txn, oldDoc)
if err != nil {
return err
}

Check warning on line 155 in db/collection_index.go

View check run for this annotation

Codecov / codecov/patch

db/collection_index.go#L154-L155

Added lines #L154 - L155 were not covered by tests
err = c.indexNewDoc(ctx, txn, newDoc)
if err != nil {
return err
}

Check warning on line 159 in db/collection_index.go

View check run for this annotation

Codecov / codecov/patch

db/collection_index.go#L158-L159

Added lines #L158 - L159 were not covered by tests

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
}

Check warning on line 168 in db/collection_index.go

View check run for this annotation

Codecov / codecov/patch

db/collection_index.go#L167-L168

Added lines #L167 - L168 were not covered by tests
defer c.discardImplicitTxn(ctx, txn)

err = c.deleteIndexedDoc(ctx, txn, doc)
if err != nil {
return err
}

Check warning on line 174 in db/collection_index.go

View check run for this annotation

Codecov / codecov/patch

db/collection_index.go#L173-L174

Added lines #L173 - L174 were not covered by tests

return c.commitImplicitTxn(ctx, txn)
}

func (c *collection) indexNewDoc(ctx context.Context, txn datastore.Txn, doc *client.Document) error {
err := c.loadIndexes(ctx, txn)
if err != nil {
Expand Down Expand Up @@ -169,6 +218,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
}

Check warning on line 229 in db/collection_index.go

View check run for this annotation

Codecov / codecov/patch

db/collection_index.go#L228-L229

Added lines #L228 - L229 were not covered by tests
for _, index := range c.indexes {
err = index.Delete(ctx, txn, doc)
if err != nil {
return err
}

Check warning on line 234 in db/collection_index.go

View check run for this annotation

Codecov / codecov/patch

db/collection_index.go#L233-L234

Added lines #L233 - L234 were not covered by tests
}
return nil
}

// CreateIndex creates a new index on the collection.
//
// If the index name is empty, a name will be automatically generated.
Expand Down
150 changes: 0 additions & 150 deletions db/txn_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,156 +184,6 @@ func (db *explicitTxnDB) GetAllIndexes(
return db.getAllIndexDescriptions(ctx, db.txn)

Check warning on line 184 in db/txn_db.go

View check run for this annotation

Codecov / codecov/patch

db/txn_db.go#L184

Added line #L184 was not covered by tests
}

// 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.
//
Expand Down
25 changes: 0 additions & 25 deletions http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
12 changes: 12 additions & 0 deletions http/client_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check warning on line 462 in http/client_collection.go

View check run for this annotation

Codecov / codecov/patch

http/client_collection.go#L461-L462

Added lines #L461 - L462 were not covered by tests
}

func (c *Collection) UpdateDocIndex(ctx context.Context, oldDoc, newDoc *client.Document) error {
return nil

Check warning on line 466 in http/client_collection.go

View check run for this annotation

Codecov / codecov/patch

http/client_collection.go#L465-L466

Added lines #L465 - L466 were not covered by tests
}

func (c *Collection) DeleteDocIndex(context.Context, *client.Document) error {
return nil

Check warning on line 470 in http/client_collection.go

View check run for this annotation

Codecov / codecov/patch

http/client_collection.go#L469-L470

Added lines #L469 - L470 were not covered by tests
}
9 changes: 4 additions & 5 deletions net/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Check warning on line 301 in net/server.go

View check run for this annotation

Codecov / codecov/patch

net/server.go#L300-L301

Added lines #L300 - L301 were not covered by tests
Expand Down Expand Up @@ -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 {
Expand All @@ -378,11 +377,11 @@ func (s *server) syncIndexedDocs(
}

Check warning on line 377 in net/server.go

View check run for this annotation

Codecov / codecov/patch

net/server.go#L376-L377

Added lines #L376 - L377 were not covered by tests

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)
}
}

Expand Down
25 changes: 0 additions & 25 deletions tests/clients/cli/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
12 changes: 12 additions & 0 deletions tests/clients/cli/wrapper_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Loading

0 comments on commit ef94bf8

Please sign in to comment.