cosmium/internal/repositories/documents.go

118 lines
4.2 KiB
Go
Raw Normal View History

2024-02-10 23:44:20 +00:00
package repositories
2024-02-11 23:54:12 +00:00
import (
2024-02-24 18:00:47 +00:00
"fmt"
2024-02-11 23:54:12 +00:00
"log"
2024-02-24 18:00:47 +00:00
"time"
2024-02-11 23:54:12 +00:00
2024-02-24 18:00:47 +00:00
"github.com/google/uuid"
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
"github.com/pikami/cosmium/internal/resourceid"
2024-02-11 23:54:12 +00:00
"github.com/pikami/cosmium/parsers"
"github.com/pikami/cosmium/parsers/nosql"
memoryexecutor "github.com/pikami/cosmium/query_executors/memory_executor"
"golang.org/x/exp/maps"
2024-02-11 23:54:12 +00:00
)
2024-02-10 23:44:20 +00:00
func GetAllDocuments(databaseId string, collectionId string) ([]repositorymodels.Document, repositorymodels.RepositoryStatus) {
if _, ok := storeState.Databases[databaseId]; !ok {
return make([]repositorymodels.Document, 0), repositorymodels.StatusNotFound
}
2024-02-10 23:44:20 +00:00
if _, ok := storeState.Collections[databaseId][collectionId]; !ok {
return make([]repositorymodels.Document, 0), repositorymodels.StatusNotFound
2024-02-10 23:44:20 +00:00
}
return maps.Values(storeState.Documents[databaseId][collectionId]), repositorymodels.StatusOk
2024-02-10 23:44:20 +00:00
}
func GetDocument(databaseId string, collectionId string, documentId string) (repositorymodels.Document, repositorymodels.RepositoryStatus) {
if _, ok := storeState.Databases[databaseId]; !ok {
return repositorymodels.Document{}, repositorymodels.StatusNotFound
}
if _, ok := storeState.Collections[databaseId][collectionId]; !ok {
return repositorymodels.Document{}, repositorymodels.StatusNotFound
}
2024-02-10 23:44:20 +00:00
if _, ok := storeState.Documents[databaseId][collectionId][documentId]; !ok {
return repositorymodels.Document{}, repositorymodels.StatusNotFound
2024-02-10 23:44:20 +00:00
}
return storeState.Documents[databaseId][collectionId][documentId], repositorymodels.StatusOk
2024-02-10 23:44:20 +00:00
}
func DeleteDocument(databaseId string, collectionId string, documentId string) repositorymodels.RepositoryStatus {
if _, ok := storeState.Databases[databaseId]; !ok {
return repositorymodels.StatusNotFound
2024-02-10 23:44:20 +00:00
}
if _, ok := storeState.Collections[databaseId][collectionId]; !ok {
return repositorymodels.StatusNotFound
2024-02-10 23:44:20 +00:00
}
if _, ok := storeState.Documents[databaseId][collectionId][documentId]; !ok {
return repositorymodels.StatusNotFound
2024-02-10 23:44:20 +00:00
}
delete(storeState.Documents[databaseId][collectionId], documentId)
return repositorymodels.StatusOk
}
2024-02-10 23:44:20 +00:00
func CreateDocument(databaseId string, collectionId string, document map[string]interface{}) (repositorymodels.Document, repositorymodels.RepositoryStatus) {
var ok bool
var documentId string
var database repositorymodels.Database
var collection repositorymodels.Collection
if documentId, ok = document["id"].(string); !ok || documentId == "" {
return repositorymodels.Document{}, repositorymodels.BadRequest
2024-02-10 23:44:20 +00:00
}
if database, ok = storeState.Databases[databaseId]; !ok {
return repositorymodels.Document{}, repositorymodels.StatusNotFound
}
2024-02-11 23:54:12 +00:00
if collection, ok = storeState.Collections[databaseId][collectionId]; !ok {
return repositorymodels.Document{}, repositorymodels.StatusNotFound
}
2024-02-11 23:54:12 +00:00
if _, ok := storeState.Documents[databaseId][collectionId][documentId]; ok {
return repositorymodels.Document{}, repositorymodels.Conflict
2024-02-10 23:44:20 +00:00
}
2024-02-24 18:00:47 +00:00
document["_ts"] = time.Now().Unix()
document["_rid"] = resourceid.NewCombined(database.ResourceID, collection.ResourceID, resourceid.New())
document["_etag"] = fmt.Sprintf("\"%s\"", uuid.New())
document["_self"] = fmt.Sprintf("dbs/%s/colls/%s/docs/%s/", database.ResourceID, collection.ResourceID, document["_rid"])
2024-02-10 23:44:20 +00:00
storeState.Documents[databaseId][collectionId][documentId] = document
return document, repositorymodels.StatusOk
2024-02-10 23:44:20 +00:00
}
2024-02-11 23:54:12 +00:00
2024-02-15 22:13:11 +00:00
func ExecuteQueryDocuments(databaseId string, collectionId string, query string, queryParameters map[string]interface{}) ([]memoryexecutor.RowType, repositorymodels.RepositoryStatus) {
2024-02-11 23:54:12 +00:00
parsedQuery, err := nosql.Parse("", []byte(query))
if err != nil {
log.Printf("Failed to parse query: %s\nerr: %v", query, err)
return nil, repositorymodels.BadRequest
2024-02-11 23:54:12 +00:00
}
collectionDocuments, status := GetAllDocuments(databaseId, collectionId)
if status != repositorymodels.StatusOk {
2024-02-11 23:54:12 +00:00
return nil, status
}
covDocs := make([]memoryexecutor.RowType, 0)
for _, doc := range collectionDocuments {
covDocs = append(covDocs, map[string]interface{}(doc))
}
2024-02-15 22:13:11 +00:00
if typedQuery, ok := parsedQuery.(parsers.SelectStmt); ok {
typedQuery.Parameters = queryParameters
return memoryexecutor.Execute(typedQuery, covDocs), repositorymodels.StatusOk
}
return nil, repositorymodels.BadRequest
2024-02-11 23:54:12 +00:00
}