Skip to content

Commit

Permalink
fix collection and namespace switching
Browse files Browse the repository at this point in the history
  • Loading branch information
bubbajoe committed Jun 2, 2024
1 parent cd90c20 commit dc7946c
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 34 deletions.
2 changes: 1 addition & 1 deletion functional-tests/admin_tests/url_shortener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const requestHandler = (ctx: any) => {
return;
}
// get the document with the ID from the collection
return getDocument("short_link", pathId)
return getDocument(pathId, "short_link")
.then((doc: any) => {
// check if the document contains the URL
if (!doc?.data?.url) {
Expand Down
4 changes: 2 additions & 2 deletions internal/admin/routes/collection_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func ConfigureCollectionAPI(server chi.Router, logger *zap.Logger, cs changestat
return
}

document, err := dm.GetDocumentByID(collectionName, namespaceName, documentId)
document, err := dm.GetDocumentByID(documentId, collectionName, namespaceName)

Check warning on line 200 in internal/admin/routes/collection_routes.go

View check run for this annotation

Codecov / codecov/patch

internal/admin/routes/collection_routes.go#L200

Added line #L200 was not covered by tests
if err != nil {
util.JsonError(w, http.StatusNotFound, err.Error())
return
Expand Down Expand Up @@ -351,7 +351,7 @@ func ConfigureCollectionAPI(server chi.Router, logger *zap.Logger, cs changestat
util.JsonError(w, http.StatusBadRequest, "document_id is required")
return
}
document, err := dm.GetDocumentByID(collectionName, namespaceName, documentId)
document, err := dm.GetDocumentByID(documentId, collectionName, namespaceName)

Check warning on line 354 in internal/admin/routes/collection_routes.go

View check run for this annotation

Codecov / codecov/patch

internal/admin/routes/collection_routes.go#L354

Added line #L354 was not covered by tests
if err != nil {
util.JsonError(w, http.StatusNotFound, err.Error())
return
Expand Down
6 changes: 3 additions & 3 deletions internal/proxy/proxy_documents.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ func (ps *ProxyState) GetDocuments(collection, namespace string, limit, offset i
}

// GetDocumentByID is a function that returns a document in a collection by its ID.
func (ps *ProxyState) GetDocumentByID(namespace, collection, docId string) (*spec.Document, error) {
func (ps *ProxyState) GetDocumentByID(docId, collection, namespace string) (*spec.Document, error) {

Check warning on line 25 in internal/proxy/proxy_documents.go

View check run for this annotation

Codecov / codecov/patch

internal/proxy/proxy_documents.go#L25

Added line #L25 was not covered by tests
if _, ok := ps.rm.GetNamespace(namespace); !ok {
return nil, spec.ErrNamespaceNotFound(namespace)
}
if _, ok := ps.rm.GetCollection(namespace, collection); !ok {
if _, ok := ps.rm.GetCollection(collection, namespace); !ok {
return nil, spec.ErrCollectionNotFound(collection)
}
return ps.store.FetchDocument(namespace, collection, docId)
return ps.store.FetchDocument(docId, collection, namespace)

Check warning on line 32 in internal/proxy/proxy_documents.go

View check run for this annotation

Codecov / codecov/patch

internal/proxy/proxy_documents.go#L32

Added line #L32 was not covered by tests
}
14 changes: 7 additions & 7 deletions internal/proxy/proxystore/proxy_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ func (store *ProxyStore) DeleteChangeLogs(logs []*spec.ChangeLog) (int, error) {
return removed, nil
}

func createDocumentKey(nsName, colName, docId string) string {
func createDocumentKey(docId, colName, nsName string) string {
return "doc/" + nsName + "/" + colName + "/" + docId
}

func (store *ProxyStore) FetchDocument(nsName, colName, docId string) (*spec.Document, error) {
docBytes, err := store.storage.Get(createDocumentKey(nsName, colName, docId))
func (store *ProxyStore) FetchDocument(docId, colName, nsName string) (*spec.Document, error) {
docBytes, err := store.storage.Get(createDocumentKey(docId, colName, nsName))

Check warning on line 100 in internal/proxy/proxystore/proxy_store.go

View check run for this annotation

Codecov / codecov/patch

internal/proxy/proxystore/proxy_store.go#L99-L100

Added lines #L99 - L100 were not covered by tests
if err != nil {
if err == storage.ErrStoreLocked {
return nil, err
Expand All @@ -119,7 +119,7 @@ func (store *ProxyStore) FetchDocuments(
limit, offset int,
) ([]*spec.Document, error) {
docs := make([]*spec.Document, 0)
docPrefix := createDocumentKey(namespaceName, collectionName, "")
docPrefix := createDocumentKey("", collectionName, namespaceName)
err := store.storage.IterateValuesPrefix(docPrefix, func(key string, val []byte) error {
if offset -= 1; offset > 0 {
return nil
Expand All @@ -145,15 +145,15 @@ func (store *ProxyStore) StoreDocument(doc *spec.Document) error {
return err
}
store.logger.Debug("storing document")
err = store.storage.Set(createDocumentKey(doc.NamespaceName, doc.CollectionName, doc.ID), docBytes)
err = store.storage.Set(createDocumentKey(doc.ID, doc.CollectionName, doc.NamespaceName), docBytes)
if err != nil {
return err
}
return nil
}

func (store *ProxyStore) DeleteDocument(id, colName, nsName string) error {
err := store.storage.Delete(createDocumentKey(nsName, colName, id))
err := store.storage.Delete(createDocumentKey(id, colName, nsName))
if err != nil {
if err == badger.ErrKeyNotFound {
return nil
Expand All @@ -164,7 +164,7 @@ func (store *ProxyStore) DeleteDocument(id, colName, nsName string) error {
}

func (store *ProxyStore) DeleteDocuments(doc *spec.Document) error {
err := store.storage.IterateTxnPrefix(createDocumentKey(doc.NamespaceName, doc.CollectionName, ""),
err := store.storage.IterateTxnPrefix(createDocumentKey("", doc.CollectionName, doc.NamespaceName),

Check warning on line 167 in internal/proxy/proxystore/proxy_store.go

View check run for this annotation

Codecov / codecov/patch

internal/proxy/proxystore/proxy_store.go#L167

Added line #L167 was not covered by tests
func(txn storage.StorageTxn, key string) error {
return txn.Delete(key)
})
Expand Down
37 changes: 17 additions & 20 deletions pkg/modules/dgate/state/state_mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ func (hp *ResourcesModule) Exports() *modules.Exports {
return &modules.Exports{
Named: map[string]any{
"getCollection": hp.fetchCollection,
"getDocument": hp.fetchDocument,
"getDocuments": hp.fetchDocuments,
"getDocument": hp.getDocument,
"getDocuments": hp.getDocuments,
"addCollection": writeFunc[*spec.Collection](hp, spec.AddCollectionCommand),
"addDocument": writeFunc[*spec.Document](hp, spec.AddDocumentCommand),
"deleteCollection": writeFunc[*spec.Collection](hp, spec.DeleteCollectionCommand),
Expand All @@ -48,7 +48,7 @@ func (hp *ResourcesModule) fetchCollection(name string) *goja.Promise {
))
return
}
collection, ok := rm.GetCollection(namespace.(string), name)
collection, ok := rm.GetCollection(name, namespace.(string))

Check warning on line 51 in pkg/modules/dgate/state/state_mod.go

View check run for this annotation

Codecov / codecov/patch

pkg/modules/dgate/state/state_mod.go#L51

Added line #L51 was not covered by tests
if !ok {
reject(goja.Null())
return
Expand All @@ -58,7 +58,7 @@ func (hp *ResourcesModule) fetchCollection(name string) *goja.Promise {
return docPromise
}

func (hp *ResourcesModule) fetchDocument(collection, docId string) *goja.Promise {
func (hp *ResourcesModule) getDocument(docId, collection string) *goja.Promise {

Check warning on line 61 in pkg/modules/dgate/state/state_mod.go

View check run for this annotation

Codecov / codecov/patch

pkg/modules/dgate/state/state_mod.go#L61

Added line #L61 was not covered by tests
ctx := hp.modCtx.Context()
state := hp.modCtx.State()
loop := hp.modCtx.EventLoop()
Expand All @@ -71,7 +71,7 @@ func (hp *ResourcesModule) fetchDocument(collection, docId string) *goja.Promise
return
}
doc, err := state.DocumentManager().
GetDocumentByID(collection, namespace.(string), docId)
GetDocumentByID(docId, collection, namespace.(string))

Check warning on line 74 in pkg/modules/dgate/state/state_mod.go

View check run for this annotation

Codecov / codecov/patch

pkg/modules/dgate/state/state_mod.go#L74

Added line #L74 was not covered by tests
if err != nil {
reject(rt.NewGoError(err))
return
Expand All @@ -81,25 +81,20 @@ func (hp *ResourcesModule) fetchDocument(collection, docId string) *goja.Promise
return docPromise
}

func (hp *ResourcesModule) fetchDocuments(args goja.FunctionCall) (*goja.Promise, error) {
type FetchDocumentsPayload struct {
Collection string `json:"collection"`
Limit int `json:"limit"`
Offset int `json:"offset"`
}

func (hp *ResourcesModule) getDocuments(payload FetchDocumentsPayload) (*goja.Promise, error) {

Check warning on line 90 in pkg/modules/dgate/state/state_mod.go

View check run for this annotation

Codecov / codecov/patch

pkg/modules/dgate/state/state_mod.go#L90

Added line #L90 was not covered by tests
ctx := hp.modCtx.Context()
state := hp.modCtx.State()
loop := hp.modCtx.EventLoop()
rt := hp.modCtx.Runtime()

collection := ""
if args.Argument(0) == goja.Undefined() {
if payload.Collection == "" {

Check warning on line 96 in pkg/modules/dgate/state/state_mod.go

View check run for this annotation

Codecov / codecov/patch

pkg/modules/dgate/state/state_mod.go#L96

Added line #L96 was not covered by tests
return nil, errors.New("collection name is required")
} else {
collection = args.Argument(0).String()
}
limit := 0
if args.Argument(1) != goja.Undefined() {
limit = int(args.Argument(1).ToInteger())
}
offset := 0
if args.Argument(2) != goja.Undefined() {
offset = int(args.Argument(2).ToInteger())
}

namespaceVal := ctx.Value(spec.Name("namespace"))
Expand All @@ -112,8 +107,10 @@ func (hp *ResourcesModule) fetchDocuments(args goja.FunctionCall) (*goja.Promise
loop.RunOnLoop(func(rt *goja.Runtime) {
doc, err := state.DocumentManager().
GetDocuments(
collection, namespace,
limit, offset,
payload.Collection,
namespace,
payload.Limit,
payload.Offset,
)

Check warning on line 114 in pkg/modules/dgate/state/state_mod.go

View check run for this annotation

Codecov / codecov/patch

pkg/modules/dgate/state/state_mod.go#L109-L114

Added lines #L109 - L114 were not covered by tests
if err != nil {
reject(rt.NewGoError(err))
Expand Down
2 changes: 1 addition & 1 deletion pkg/resources/document_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import (
)

type DocumentManager interface {
GetDocumentByID(collection, namespace, id string) (*spec.Document, error)
GetDocumentByID(id, collection, namespace string) (*spec.Document, error)
GetDocuments(collection, namespace string, limit, offset int) ([]*spec.Document, error)
}

0 comments on commit dc7946c

Please sign in to comment.