Compare commits

...

25 Commits

Author SHA1 Message Date
Pijus Kamandulis d64bdeb385 Handle 'NOT IN' statement 2025-10-27 21:33:49 +02:00
Pijus Kamandulis 11f3a1ad01 Fix database and collection deletion 2025-10-12 23:26:44 +03:00
Pijus Kamandulis 4d67212f1b Update dependencies 2025-10-08 23:21:30 +03:00
Pijus Kamandulis 03cd04e996 Downgrade Go to 1.24.7 due to windows cross-compile issue 2025-10-08 23:14:44 +03:00
Pijus Kamandulis a3bea16a26 Updated GO version to 1.25.1 2025-09-16 22:35:44 +03:00
Pijus Kamandulis 67b6c86e14 Update dependencies 2025-09-16 22:28:22 +03:00
Pijus Kamandulis 4872ec72fd Added support for initial data loading when using badger db 2025-09-16 20:21:27 +03:00
Pijus Kamandulis 89b914310c Fix badger prefix scans 2025-09-16 19:27:52 +03:00
Pijus Kamandulis c988741f8e Fix query creation via explorer; Extract header names to constants 2025-09-16 19:13:45 +03:00
Pijus Kamandulis 51e3311ba4 Update dependencies; Fix authentication for UDF, SPROC and TRIGGER endpoints 2025-08-20 00:12:02 +03:00
zecka fb1c080034 docs(readme): fix docker example to create valid save.json if missing (#12)
* docs(readme): fix docker example to create valid save.json if missing

Prevent runtime errors by initializing save.json with '{}' if the file does not exist before running the container.

Fixes #11

* Update README.md

---------

Co-authored-by: Pijus Kamandulis <pikami@users.noreply.github.com>
2025-07-06 11:27:09 +03:00
Pijus Kamandulis fba9b3df5f Run badger garbage collector periodically 2025-05-30 00:25:17 +03:00
Pijus Kamandulis b743e23ff9 Added support for arithmetics inside queries 2025-05-30 00:15:55 +03:00
Pijus Kamandulis 11851297f5 Fix formatting for grammar file 2025-05-20 22:43:00 +03:00
Pijus Kamandulis 560ea5296d Add support for expressions in SELECT clause 2025-05-20 22:40:00 +03:00
Pijus Kamandulis e20a6ca7cd Extract constants instead of duplicating literals 2025-05-14 20:01:46 +03:00
Pijus Kamandulis 7e0c10479b Implement IIF function; Fix empty object select 2025-05-14 18:48:30 +03:00
Pijus Kamandulis 30195fae96 Update dependencies 2025-05-14 08:25:53 +03:00
Pijus Kamandulis 598f2837af Fix issues with persist flag; Use custom logger for badger 2025-04-03 23:48:20 +03:00
Pijus Kamandulis 28e3c0c3d8 Rename 'MapDS' to 'JsonDS'; Added some docs 2025-03-14 22:40:12 +02:00
Pijus Kamandulis 97eea30c97 Use msgpack instead of gob; Added data persistance for badger data store 2025-03-13 23:59:07 +02:00
Pijus Kamandulis 5fe60d831a Pinned 3rd party Github Actions 2025-03-12 23:48:42 +02:00
Pijus Kamandulis d309d99906 Update dependancies 2025-03-12 23:24:08 +02:00
Pijus Kamandulis b2516eda9f Stability improvements 2025-03-12 22:00:30 +02:00
Pijus Kamandulis 813b9faeaa Added support for Badger as an alternative storage backend 2025-03-12 21:06:10 +02:00
66 changed files with 5788 additions and 2742 deletions
@@ -12,10 +12,10 @@ jobs:
uses: actions/checkout@v3 uses: actions/checkout@v3
- name: Cross-Compile with xgo - name: Cross-Compile with xgo
uses: crazy-max/ghaction-xgo@v3.1.0 uses: crazy-max/ghaction-xgo@e22d3c8b089adba750d5a74738b8e95d96f0c991 # v3.1.0
with: with:
xgo_version: latest xgo_version: latest
go_version: 1.24.0 go_version: 1.24.7
dest: dist dest: dist
pkg: sharedlibrary pkg: sharedlibrary
prefix: cosmium prefix: cosmium
+4 -4
View File
@@ -21,13 +21,13 @@ jobs:
- name: Set up Go - name: Set up Go
uses: actions/setup-go@v5 uses: actions/setup-go@v5
with: with:
go-version: 1.24.0 go-version: 1.24.7
- name: Cross-Compile with xgo - name: Cross-Compile with xgo
uses: crazy-max/ghaction-xgo@v3.1.0 uses: crazy-max/ghaction-xgo@e22d3c8b089adba750d5a74738b8e95d96f0c991 # v3.1.0
with: with:
xgo_version: latest xgo_version: latest
go_version: 1.24.0 go_version: 1.24.7
dest: sharedlibrary_dist dest: sharedlibrary_dist
pkg: sharedlibrary pkg: sharedlibrary
prefix: cosmium prefix: cosmium
@@ -44,7 +44,7 @@ jobs:
password: ${{ secrets.GITHUB_TOKEN }} password: ${{ secrets.GITHUB_TOKEN }}
- name: Run GoReleaser - name: Run GoReleaser
uses: goreleaser/goreleaser-action@v5 uses: goreleaser/goreleaser-action@5742e2a039330cbb23ebf35f046f814d4c6ff811 # v5
with: with:
distribution: goreleaser distribution: goreleaser
version: ${{ env.GITHUB_REF_NAME }} version: ${{ env.GITHUB_REF_NAME }}
+1 -1
View File
@@ -9,7 +9,7 @@ SERVER_LOCATION=./cmd/server
SHARED_LIB_LOCATION=./sharedlibrary SHARED_LIB_LOCATION=./sharedlibrary
SHARED_LIB_OPT=-buildmode=c-shared SHARED_LIB_OPT=-buildmode=c-shared
XGO_TARGETS=linux/amd64,linux/arm64,windows/amd64,windows/arm64,darwin/amd64,darwin/arm64 XGO_TARGETS=linux/amd64,linux/arm64,windows/amd64,windows/arm64,darwin/amd64,darwin/arm64
GOVERSION=1.24.0 GOVERSION=1.24.7
DIST_DIR=dist DIST_DIR=dist
+15 -1
View File
@@ -64,7 +64,8 @@ There are two docker tags available:
If you wan to run the application using docker, configure it using environment variables see example: If you wan to run the application using docker, configure it using environment variables see example:
```sh ```sh
docker run --rm \ # Ensure save.json exists so Docker volume mounts correctly
[ -f save.json ] || echo '{}' > save.json && docker run --rm \
-e COSMIUM_PERSIST=/save.json \ -e COSMIUM_PERSIST=/save.json \
-v ./save.json:/save.json \ -v ./save.json:/save.json \
-p 8081:8081 \ -p 8081:8081 \
@@ -86,6 +87,7 @@ To disable SSL and run Cosmium on HTTP instead, you can use the `-DisableTls` fl
- **-Persist**: Saves data to the given path on application exit (When `-InitialData` argument is not supplied, it will try to load data from path supplied in `-Persist`) - **-Persist**: Saves data to the given path on application exit (When `-InitialData` argument is not supplied, it will try to load data from path supplied in `-Persist`)
- **-Port**: Listen port (default 8081) - **-Port**: Listen port (default 8081)
- **-LogLevel**: Sets the logging level (one of: debug, info, error, silent) (default info) - **-LogLevel**: Sets the logging level (one of: debug, info, error, silent) (default info)
- **-DataStore**: Allows selecting [storage backend](#data-storage-backends) (default "json")
These arguments allow you to configure various aspects of Cosmium's behavior according to your requirements. These arguments allow you to configure various aspects of Cosmium's behavior according to your requirements.
@@ -99,6 +101,18 @@ All mentioned arguments can also be set using environment variables:
- **COSMIUM_PORT** for `-Port` - **COSMIUM_PORT** for `-Port`
- **COSMIUM_LOGLEVEL** for `-LogLevel` - **COSMIUM_LOGLEVEL** for `-LogLevel`
### Data Storage Backends
Cosmium supports multiple storage backends for saving, loading, and managing data at runtime.
| Backend | Storage Location | Write Behavior | Memory Usage |
|----------|--------------------------|--------------------------|----------------------|
| `json` (default) | JSON file on disk 📄 | On application exit ⏳ | 🛑 More than Badger |
| `badger` | BadgerDB database on disk ⚡ | Immediately on write 🚀 | ✅ Less than JSON |
The `badger` backend is generally recommended as it uses less memory and writes data to disk immediately. However, if you need to load initial data from a JSON file, use the `json` backend.
# License # License
This project is [MIT licensed](./LICENSE). This project is [MIT licensed](./LICENSE).
+26
View File
@@ -15,6 +15,11 @@ const (
ExplorerBaseUrlLocation = "/_explorer" ExplorerBaseUrlLocation = "/_explorer"
) )
const (
DataStoreJson = "json"
DataStoreBadger = "badger"
)
func ParseFlags() ServerConfig { func ParseFlags() ServerConfig {
host := flag.String("Host", "localhost", "Hostname") host := flag.String("Host", "localhost", "Hostname")
port := flag.Int("Port", 8081, "Listen port") port := flag.Int("Port", 8081, "Listen port")
@@ -28,6 +33,8 @@ func ParseFlags() ServerConfig {
persistDataPath := flag.String("Persist", "", "Saves data to given path on application exit") persistDataPath := flag.String("Persist", "", "Saves data to given path on application exit")
logLevel := NewEnumValue("info", []string{"debug", "info", "error", "silent"}) logLevel := NewEnumValue("info", []string{"debug", "info", "error", "silent"})
flag.Var(logLevel, "LogLevel", fmt.Sprintf("Sets the logging level %s", logLevel.AllowedValuesList())) flag.Var(logLevel, "LogLevel", fmt.Sprintf("Sets the logging level %s", logLevel.AllowedValuesList()))
dataStore := NewEnumValue("json", []string{DataStoreJson, DataStoreBadger})
flag.Var(dataStore, "DataStore", fmt.Sprintf("Sets the data store %s", dataStore.AllowedValuesList()))
flag.Parse() flag.Parse()
setFlagsFromEnvironment() setFlagsFromEnvironment()
@@ -44,6 +51,7 @@ func ParseFlags() ServerConfig {
config.DisableTls = *disableTls config.DisableTls = *disableTls
config.AccountKey = *accountKey config.AccountKey = *accountKey
config.LogLevel = logLevel.value config.LogLevel = logLevel.value
config.DataStore = dataStore.value
config.PopulateCalculatedFields() config.PopulateCalculatedFields()
@@ -68,6 +76,24 @@ func (c *ServerConfig) PopulateCalculatedFields() {
default: default:
logger.SetLogLevel(logger.LogLevelInfo) logger.SetLogLevel(logger.LogLevelInfo)
} }
fileInfo, err := os.Stat(c.PersistDataFilePath)
if c.PersistDataFilePath != "" && !os.IsNotExist(err) {
if err != nil {
logger.ErrorLn("Failed to get file info for persist path:", err)
os.Exit(1)
}
if c.DataStore == DataStoreJson && fileInfo.IsDir() {
logger.ErrorLn("--Persist cannot be a directory when using json data store")
os.Exit(1)
}
if c.DataStore == DataStoreBadger && !fileInfo.IsDir() {
logger.ErrorLn("--Persist must be a directory when using Badger data store")
os.Exit(1)
}
}
} }
func (c *ServerConfig) ApplyDefaultsToEmptyFields() { func (c *ServerConfig) ApplyDefaultsToEmptyFields() {
+2
View File
@@ -17,4 +17,6 @@ type ServerConfig struct {
DisableTls bool `json:"disableTls"` DisableTls bool `json:"disableTls"`
LogLevel string `json:"logLevel"` LogLevel string `json:"logLevel"`
ExplorerBaseUrlLocation string `json:"explorerBaseUrlLocation"` ExplorerBaseUrlLocation string `json:"explorerBaseUrlLocation"`
DataStore string `json:"dataStore"`
} }
+11 -9
View File
@@ -5,6 +5,8 @@ import (
"net/http" "net/http"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/pikami/cosmium/api/headers"
"github.com/pikami/cosmium/internal/constants"
"github.com/pikami/cosmium/internal/datastore" "github.com/pikami/cosmium/internal/datastore"
) )
@@ -15,7 +17,7 @@ func (h *Handlers) GetAllCollections(c *gin.Context) {
if status == datastore.StatusOk { if status == datastore.StatusOk {
database, _ := h.dataStore.GetDatabase(databaseId) database, _ := h.dataStore.GetDatabase(databaseId)
c.Header("x-ms-item-count", fmt.Sprintf("%d", len(collections))) c.Header(headers.ItemCount, fmt.Sprintf("%d", len(collections)))
c.IndentedJSON(http.StatusOK, gin.H{ c.IndentedJSON(http.StatusOK, gin.H{
"_rid": database.ResourceID, "_rid": database.ResourceID,
"DocumentCollections": collections, "DocumentCollections": collections,
@@ -24,7 +26,7 @@ func (h *Handlers) GetAllCollections(c *gin.Context) {
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
func (h *Handlers) GetCollection(c *gin.Context) { func (h *Handlers) GetCollection(c *gin.Context) {
@@ -38,11 +40,11 @@ func (h *Handlers) GetCollection(c *gin.Context) {
} }
if status == datastore.StatusNotFound { if status == datastore.StatusNotFound {
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"}) c.IndentedJSON(http.StatusNotFound, constants.NotFoundResponse)
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
func (h *Handlers) DeleteCollection(c *gin.Context) { func (h *Handlers) DeleteCollection(c *gin.Context) {
@@ -56,11 +58,11 @@ func (h *Handlers) DeleteCollection(c *gin.Context) {
} }
if status == datastore.StatusNotFound { if status == datastore.StatusNotFound {
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"}) c.IndentedJSON(http.StatusNotFound, constants.NotFoundResponse)
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
func (h *Handlers) CreateCollection(c *gin.Context) { func (h *Handlers) CreateCollection(c *gin.Context) {
@@ -73,13 +75,13 @@ func (h *Handlers) CreateCollection(c *gin.Context) {
} }
if newCollection.ID == "" { if newCollection.ID == "" {
c.JSON(http.StatusBadRequest, gin.H{"message": "BadRequest"}) c.JSON(http.StatusBadRequest, constants.BadRequestResponse)
return return
} }
createdCollection, status := h.dataStore.CreateCollection(databaseId, newCollection) createdCollection, status := h.dataStore.CreateCollection(databaseId, newCollection)
if status == datastore.Conflict { if status == datastore.Conflict {
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"}) c.IndentedJSON(http.StatusConflict, constants.ConflictResponse)
return return
} }
@@ -88,5 +90,5 @@ func (h *Handlers) CreateCollection(c *gin.Context) {
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
+11 -9
View File
@@ -5,13 +5,15 @@ import (
"net/http" "net/http"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/pikami/cosmium/api/headers"
"github.com/pikami/cosmium/internal/constants"
"github.com/pikami/cosmium/internal/datastore" "github.com/pikami/cosmium/internal/datastore"
) )
func (h *Handlers) GetAllDatabases(c *gin.Context) { func (h *Handlers) GetAllDatabases(c *gin.Context) {
databases, status := h.dataStore.GetAllDatabases() databases, status := h.dataStore.GetAllDatabases()
if status == datastore.StatusOk { if status == datastore.StatusOk {
c.Header("x-ms-item-count", fmt.Sprintf("%d", len(databases))) c.Header(headers.ItemCount, fmt.Sprintf("%d", len(databases)))
c.IndentedJSON(http.StatusOK, gin.H{ c.IndentedJSON(http.StatusOK, gin.H{
"_rid": "", "_rid": "",
"Databases": databases, "Databases": databases,
@@ -20,7 +22,7 @@ func (h *Handlers) GetAllDatabases(c *gin.Context) {
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
func (h *Handlers) GetDatabase(c *gin.Context) { func (h *Handlers) GetDatabase(c *gin.Context) {
@@ -33,11 +35,11 @@ func (h *Handlers) GetDatabase(c *gin.Context) {
} }
if status == datastore.StatusNotFound { if status == datastore.StatusNotFound {
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"}) c.IndentedJSON(http.StatusNotFound, constants.NotFoundResponse)
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
func (h *Handlers) DeleteDatabase(c *gin.Context) { func (h *Handlers) DeleteDatabase(c *gin.Context) {
@@ -50,11 +52,11 @@ func (h *Handlers) DeleteDatabase(c *gin.Context) {
} }
if status == datastore.StatusNotFound { if status == datastore.StatusNotFound {
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"}) c.IndentedJSON(http.StatusNotFound, constants.NotFoundResponse)
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
func (h *Handlers) CreateDatabase(c *gin.Context) { func (h *Handlers) CreateDatabase(c *gin.Context) {
@@ -66,13 +68,13 @@ func (h *Handlers) CreateDatabase(c *gin.Context) {
} }
if newDatabase.ID == "" { if newDatabase.ID == "" {
c.JSON(http.StatusBadRequest, gin.H{"message": "BadRequest"}) c.JSON(http.StatusBadRequest, constants.BadRequestResponse)
return return
} }
createdDatabase, status := h.dataStore.CreateDatabase(newDatabase) createdDatabase, status := h.dataStore.CreateDatabase(newDatabase)
if status == datastore.Conflict { if status == datastore.Conflict {
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"}) c.IndentedJSON(http.StatusConflict, constants.ConflictResponse)
return return
} }
@@ -81,5 +83,5 @@ func (h *Handlers) CreateDatabase(c *gin.Context) {
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
+32 -26
View File
@@ -9,6 +9,7 @@ import (
jsonpatch "github.com/cosmiumdev/json-patch/v5" jsonpatch "github.com/cosmiumdev/json-patch/v5"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
apimodels "github.com/pikami/cosmium/api/api_models" apimodels "github.com/pikami/cosmium/api/api_models"
"github.com/pikami/cosmium/api/headers"
"github.com/pikami/cosmium/internal/constants" "github.com/pikami/cosmium/internal/constants"
"github.com/pikami/cosmium/internal/converters" "github.com/pikami/cosmium/internal/converters"
"github.com/pikami/cosmium/internal/datastore" "github.com/pikami/cosmium/internal/datastore"
@@ -26,7 +27,7 @@ func (h *Handlers) GetAllDocuments(c *gin.Context) {
if status == datastore.StatusOk { if status == datastore.StatusOk {
collection, _ := h.dataStore.GetCollection(databaseId, collectionId) collection, _ := h.dataStore.GetCollection(databaseId, collectionId)
c.Header("x-ms-item-count", fmt.Sprintf("%d", len(documents))) c.Header(headers.ItemCount, fmt.Sprintf("%d", len(documents)))
c.IndentedJSON(http.StatusOK, gin.H{ c.IndentedJSON(http.StatusOK, gin.H{
"_rid": collection.ID, "_rid": collection.ID,
"Documents": documents, "Documents": documents,
@@ -35,7 +36,7 @@ func (h *Handlers) GetAllDocuments(c *gin.Context) {
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
func (h *Handlers) GetDocument(c *gin.Context) { func (h *Handlers) GetDocument(c *gin.Context) {
@@ -50,11 +51,11 @@ func (h *Handlers) GetDocument(c *gin.Context) {
} }
if status == datastore.StatusNotFound { if status == datastore.StatusNotFound {
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"}) c.IndentedJSON(http.StatusNotFound, constants.NotFoundResponse)
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
func (h *Handlers) DeleteDocument(c *gin.Context) { func (h *Handlers) DeleteDocument(c *gin.Context) {
@@ -69,11 +70,11 @@ func (h *Handlers) DeleteDocument(c *gin.Context) {
} }
if status == datastore.StatusNotFound { if status == datastore.StatusNotFound {
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"}) c.IndentedJSON(http.StatusNotFound, constants.NotFoundResponse)
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
// TODO: Maybe move "replace" logic to data store // TODO: Maybe move "replace" logic to data store
@@ -90,13 +91,13 @@ func (h *Handlers) ReplaceDocument(c *gin.Context) {
status := h.dataStore.DeleteDocument(databaseId, collectionId, documentId) status := h.dataStore.DeleteDocument(databaseId, collectionId, documentId)
if status == datastore.StatusNotFound { if status == datastore.StatusNotFound {
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"}) c.IndentedJSON(http.StatusNotFound, constants.NotFoundResponse)
return return
} }
createdDocument, status := h.dataStore.CreateDocument(databaseId, collectionId, requestBody) createdDocument, status := h.dataStore.CreateDocument(databaseId, collectionId, requestBody)
if status == datastore.Conflict { if status == datastore.Conflict {
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"}) c.IndentedJSON(http.StatusConflict, constants.ConflictResponse)
return return
} }
@@ -105,7 +106,7 @@ func (h *Handlers) ReplaceDocument(c *gin.Context) {
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
func (h *Handlers) PatchDocument(c *gin.Context) { func (h *Handlers) PatchDocument(c *gin.Context) {
@@ -115,7 +116,7 @@ func (h *Handlers) PatchDocument(c *gin.Context) {
document, status := h.dataStore.GetDocument(databaseId, collectionId, documentId) document, status := h.dataStore.GetDocument(databaseId, collectionId, documentId)
if status == datastore.StatusNotFound { if status == datastore.StatusNotFound {
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"}) c.IndentedJSON(http.StatusNotFound, constants.NotFoundResponse)
return return
} }
@@ -166,13 +167,13 @@ func (h *Handlers) PatchDocument(c *gin.Context) {
status = h.dataStore.DeleteDocument(databaseId, collectionId, documentId) status = h.dataStore.DeleteDocument(databaseId, collectionId, documentId)
if status == datastore.StatusNotFound { if status == datastore.StatusNotFound {
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"}) c.IndentedJSON(http.StatusNotFound, constants.NotFoundResponse)
return return
} }
createdDocument, status := h.dataStore.CreateDocument(databaseId, collectionId, modifiedDocument) createdDocument, status := h.dataStore.CreateDocument(databaseId, collectionId, modifiedDocument)
if status == datastore.Conflict { if status == datastore.Conflict {
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"}) c.IndentedJSON(http.StatusConflict, constants.ConflictResponse)
return return
} }
@@ -181,7 +182,7 @@ func (h *Handlers) PatchDocument(c *gin.Context) {
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
func (h *Handlers) DocumentsPost(c *gin.Context) { func (h *Handlers) DocumentsPost(c *gin.Context) {
@@ -189,7 +190,7 @@ func (h *Handlers) DocumentsPost(c *gin.Context) {
collectionId := c.Param("collId") collectionId := c.Param("collId")
// Handle batch requests // Handle batch requests
isBatchRequest, _ := strconv.ParseBool(c.GetHeader("x-ms-cosmos-is-batch-request")) isBatchRequest, _ := strconv.ParseBool(c.GetHeader(headers.IsBatchRequest))
if isBatchRequest { if isBatchRequest {
h.handleBatchRequest(c) h.handleBatchRequest(c)
return return
@@ -201,25 +202,34 @@ func (h *Handlers) DocumentsPost(c *gin.Context) {
return return
} }
query := requestBody["query"] // Handle query plan requests
if query != nil { isQueryPlanRequest, _ := strconv.ParseBool(c.GetHeader(headers.IsQueryPlanRequest))
if isQueryPlanRequest {
c.IndentedJSON(http.StatusOK, constants.QueryPlanResponse)
return
}
// Handle query requests
isQueryRequest, _ := strconv.ParseBool(c.GetHeader(headers.IsQuery))
isQueryRequestAltHeader, _ := strconv.ParseBool(c.GetHeader(headers.Query))
if isQueryRequest || isQueryRequestAltHeader {
h.handleDocumentQuery(c, requestBody) h.handleDocumentQuery(c, requestBody)
return return
} }
if requestBody["id"] == "" { if requestBody["id"] == "" {
c.JSON(http.StatusBadRequest, gin.H{"message": "BadRequest"}) c.JSON(http.StatusBadRequest, constants.BadRequestResponse)
return return
} }
isUpsert, _ := strconv.ParseBool(c.GetHeader("x-ms-documentdb-is-upsert")) isUpsert, _ := strconv.ParseBool(c.GetHeader(headers.IsUpsert))
if isUpsert { if isUpsert {
h.dataStore.DeleteDocument(databaseId, collectionId, requestBody["id"].(string)) h.dataStore.DeleteDocument(databaseId, collectionId, requestBody["id"].(string))
} }
createdDocument, status := h.dataStore.CreateDocument(databaseId, collectionId, requestBody) createdDocument, status := h.dataStore.CreateDocument(databaseId, collectionId, requestBody)
if status == datastore.Conflict { if status == datastore.Conflict {
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"}) c.IndentedJSON(http.StatusConflict, constants.ConflictResponse)
return return
} }
@@ -228,7 +238,7 @@ func (h *Handlers) DocumentsPost(c *gin.Context) {
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
func parametersToMap(pairs []interface{}) map[string]interface{} { func parametersToMap(pairs []interface{}) map[string]interface{} {
@@ -247,11 +257,6 @@ func (h *Handlers) handleDocumentQuery(c *gin.Context, requestBody map[string]in
databaseId := c.Param("databaseId") databaseId := c.Param("databaseId")
collectionId := c.Param("collId") collectionId := c.Param("collId")
if c.GetHeader("x-ms-cosmos-is-query-plan-request") != "" {
c.IndentedJSON(http.StatusOK, constants.QueryPlanResponse)
return
}
var queryParameters map[string]interface{} var queryParameters map[string]interface{}
if paramsArray, ok := requestBody["parameters"].([]interface{}); ok { if paramsArray, ok := requestBody["parameters"].([]interface{}); ok {
queryParameters = parametersToMap(paramsArray) queryParameters = parametersToMap(paramsArray)
@@ -266,7 +271,7 @@ func (h *Handlers) handleDocumentQuery(c *gin.Context, requestBody map[string]in
} }
collection, _ := h.dataStore.GetCollection(databaseId, collectionId) collection, _ := h.dataStore.GetCollection(databaseId, collectionId)
c.Header("x-ms-item-count", fmt.Sprintf("%d", len(docs))) c.Header(headers.ItemCount, fmt.Sprintf("%d", len(docs)))
c.IndentedJSON(http.StatusOK, gin.H{ c.IndentedJSON(http.StatusOK, gin.H{
"_rid": collection.ResourceID, "_rid": collection.ResourceID,
"Documents": docs, "Documents": docs,
@@ -383,6 +388,7 @@ func (h *Handlers) executeQueryDocuments(databaseId string, collectionId string,
if status != datastore.StatusOk { if status != datastore.StatusOk {
return nil, status return nil, status
} }
defer allDocumentsIterator.Close()
rowsIterator := converters.NewDocumentToRowTypeIterator(allDocumentsIterator) rowsIterator := converters.NewDocumentToRowTypeIterator(allDocumentsIterator)
+16 -3
View File
@@ -6,6 +6,7 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/pikami/cosmium/api/config" "github.com/pikami/cosmium/api/config"
"github.com/pikami/cosmium/api/headers"
"github.com/pikami/cosmium/internal/authentication" "github.com/pikami/cosmium/internal/authentication"
"github.com/pikami/cosmium/internal/logger" "github.com/pikami/cosmium/internal/logger"
) )
@@ -22,8 +23,8 @@ func Authentication(config *config.ServerConfig) gin.HandlerFunc {
resourceType := urlToResourceType(requestUrl) resourceType := urlToResourceType(requestUrl)
resourceId := requestToResourceId(c) resourceId := requestToResourceId(c)
authHeader := c.Request.Header.Get("authorization") authHeader := c.Request.Header.Get(headers.Authorization)
date := c.Request.Header.Get("x-ms-date") date := c.Request.Header.Get(headers.XDate)
expectedSignature := authentication.GenerateSignature( expectedSignature := authentication.GenerateSignature(
c.Request.Method, resourceType, resourceId, date, config.AccountKey) c.Request.Method, resourceType, resourceId, date, config.AccountKey)
@@ -60,6 +61,9 @@ func requestToResourceId(c *gin.Context) string {
databaseId, _ := c.Params.Get("databaseId") databaseId, _ := c.Params.Get("databaseId")
collId, _ := c.Params.Get("collId") collId, _ := c.Params.Get("collId")
docId, _ := c.Params.Get("docId") docId, _ := c.Params.Get("docId")
triggerId, _ := c.Params.Get("triggerId")
sprocId, _ := c.Params.Get("sprocId")
udfId, _ := c.Params.Get("udfId")
resourceType := urlToResourceType(c.Request.URL.String()) resourceType := urlToResourceType(c.Request.URL.String())
var resourceId string var resourceId string
@@ -72,8 +76,17 @@ func requestToResourceId(c *gin.Context) string {
if docId != "" { if docId != "" {
resourceId += "/docs/" + docId resourceId += "/docs/" + docId
} }
if triggerId != "" {
resourceId += "/triggers/" + triggerId
}
if sprocId != "" {
resourceId += "/sprocs/" + sprocId
}
if udfId != "" {
resourceId += "/udfs/" + udfId
}
isFeed := c.Request.Header.Get("A-Im") == "Incremental Feed" isFeed := c.Request.Header.Get(headers.AIM) == "Incremental Feed"
if resourceType == "pkranges" && isFeed { if resourceType == "pkranges" && isFeed {
resourceId = collId resourceId = collId
} }
+2 -1
View File
@@ -4,10 +4,11 @@ import (
"net/http" "net/http"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/pikami/cosmium/api/headers"
) )
func GetOffers(c *gin.Context) { func GetOffers(c *gin.Context) {
c.Header("x-ms-item-count", "0") c.Header(headers.ItemCount, "0")
c.IndentedJSON(http.StatusOK, gin.H{ c.IndentedJSON(http.StatusOK, gin.H{
"_rid": "", "_rid": "",
"_count": 0, "_count": 0,
+10 -8
View File
@@ -5,6 +5,8 @@ import (
"net/http" "net/http"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/pikami/cosmium/api/headers"
"github.com/pikami/cosmium/internal/constants"
"github.com/pikami/cosmium/internal/datastore" "github.com/pikami/cosmium/internal/datastore"
"github.com/pikami/cosmium/internal/resourceid" "github.com/pikami/cosmium/internal/resourceid"
) )
@@ -13,18 +15,18 @@ func (h *Handlers) GetPartitionKeyRanges(c *gin.Context) {
databaseId := c.Param("databaseId") databaseId := c.Param("databaseId")
collectionId := c.Param("collId") collectionId := c.Param("collId")
if c.Request.Header.Get("if-none-match") != "" { if c.Request.Header.Get(headers.IfNoneMatch) != "" {
c.AbortWithStatus(http.StatusNotModified) c.AbortWithStatus(http.StatusNotModified)
return return
} }
partitionKeyRanges, status := h.dataStore.GetPartitionKeyRanges(databaseId, collectionId) partitionKeyRanges, status := h.dataStore.GetPartitionKeyRanges(databaseId, collectionId)
if status == datastore.StatusOk { if status == datastore.StatusOk {
c.Header("etag", "\"420\"") c.Header(headers.ETag, "\"420\"")
c.Header("lsn", "420") c.Header(headers.LSN, "420")
c.Header("x-ms-cosmos-llsn", "420") c.Header(headers.CosmosLsn, "420")
c.Header("x-ms-global-committed-lsn", "420") c.Header(headers.GlobalCommittedLsn, "420")
c.Header("x-ms-item-count", fmt.Sprintf("%d", len(partitionKeyRanges))) c.Header(headers.ItemCount, fmt.Sprintf("%d", len(partitionKeyRanges)))
collectionRid := collectionId collectionRid := collectionId
collection, _ := h.dataStore.GetCollection(databaseId, collectionId) collection, _ := h.dataStore.GetCollection(databaseId, collectionId)
@@ -42,9 +44,9 @@ func (h *Handlers) GetPartitionKeyRanges(c *gin.Context) {
} }
if status == datastore.StatusNotFound { if status == datastore.StatusNotFound {
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"}) c.IndentedJSON(http.StatusNotFound, constants.NotFoundResponse)
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
+15 -13
View File
@@ -5,6 +5,8 @@ import (
"net/http" "net/http"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/pikami/cosmium/api/headers"
"github.com/pikami/cosmium/internal/constants"
"github.com/pikami/cosmium/internal/datastore" "github.com/pikami/cosmium/internal/datastore"
) )
@@ -15,12 +17,12 @@ func (h *Handlers) GetAllStoredProcedures(c *gin.Context) {
sps, status := h.dataStore.GetAllStoredProcedures(databaseId, collectionId) sps, status := h.dataStore.GetAllStoredProcedures(databaseId, collectionId)
if status == datastore.StatusOk { if status == datastore.StatusOk {
c.Header("x-ms-item-count", fmt.Sprintf("%d", len(sps))) c.Header(headers.ItemCount, fmt.Sprintf("%d", len(sps)))
c.IndentedJSON(http.StatusOK, gin.H{"_rid": "", "StoredProcedures": sps, "_count": len(sps)}) c.IndentedJSON(http.StatusOK, gin.H{"_rid": "", "StoredProcedures": sps, "_count": len(sps)})
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
func (h *Handlers) GetStoredProcedure(c *gin.Context) { func (h *Handlers) GetStoredProcedure(c *gin.Context) {
@@ -36,11 +38,11 @@ func (h *Handlers) GetStoredProcedure(c *gin.Context) {
} }
if status == datastore.StatusNotFound { if status == datastore.StatusNotFound {
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"}) c.IndentedJSON(http.StatusNotFound, constants.NotFoundResponse)
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
func (h *Handlers) DeleteStoredProcedure(c *gin.Context) { func (h *Handlers) DeleteStoredProcedure(c *gin.Context) {
@@ -55,11 +57,11 @@ func (h *Handlers) DeleteStoredProcedure(c *gin.Context) {
} }
if status == datastore.StatusNotFound { if status == datastore.StatusNotFound {
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"}) c.IndentedJSON(http.StatusNotFound, constants.NotFoundResponse)
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
func (h *Handlers) ReplaceStoredProcedure(c *gin.Context) { func (h *Handlers) ReplaceStoredProcedure(c *gin.Context) {
@@ -69,19 +71,19 @@ func (h *Handlers) ReplaceStoredProcedure(c *gin.Context) {
var sp datastore.StoredProcedure var sp datastore.StoredProcedure
if err := c.BindJSON(&sp); err != nil { if err := c.BindJSON(&sp); err != nil {
c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "Invalid body"}) c.IndentedJSON(http.StatusBadRequest, constants.BadRequestResponse)
return return
} }
status := h.dataStore.DeleteStoredProcedure(databaseId, collectionId, spId) status := h.dataStore.DeleteStoredProcedure(databaseId, collectionId, spId)
if status == datastore.StatusNotFound { if status == datastore.StatusNotFound {
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"}) c.IndentedJSON(http.StatusNotFound, constants.NotFoundResponse)
return return
} }
createdSP, status := h.dataStore.CreateStoredProcedure(databaseId, collectionId, sp) createdSP, status := h.dataStore.CreateStoredProcedure(databaseId, collectionId, sp)
if status == datastore.Conflict { if status == datastore.Conflict {
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"}) c.IndentedJSON(http.StatusConflict, constants.ConflictResponse)
return return
} }
@@ -90,7 +92,7 @@ func (h *Handlers) ReplaceStoredProcedure(c *gin.Context) {
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
func (h *Handlers) CreateStoredProcedure(c *gin.Context) { func (h *Handlers) CreateStoredProcedure(c *gin.Context) {
@@ -99,13 +101,13 @@ func (h *Handlers) CreateStoredProcedure(c *gin.Context) {
var sp datastore.StoredProcedure var sp datastore.StoredProcedure
if err := c.BindJSON(&sp); err != nil { if err := c.BindJSON(&sp); err != nil {
c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "Invalid body"}) c.IndentedJSON(http.StatusBadRequest, constants.BadRequestResponse)
return return
} }
createdSP, status := h.dataStore.CreateStoredProcedure(databaseId, collectionId, sp) createdSP, status := h.dataStore.CreateStoredProcedure(databaseId, collectionId, sp)
if status == datastore.Conflict { if status == datastore.Conflict {
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"}) c.IndentedJSON(http.StatusConflict, constants.ConflictResponse)
return return
} }
@@ -114,5 +116,5 @@ func (h *Handlers) CreateStoredProcedure(c *gin.Context) {
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
+15 -13
View File
@@ -5,6 +5,8 @@ import (
"net/http" "net/http"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/pikami/cosmium/api/headers"
"github.com/pikami/cosmium/internal/constants"
"github.com/pikami/cosmium/internal/datastore" "github.com/pikami/cosmium/internal/datastore"
) )
@@ -15,12 +17,12 @@ func (h *Handlers) GetAllTriggers(c *gin.Context) {
triggers, status := h.dataStore.GetAllTriggers(databaseId, collectionId) triggers, status := h.dataStore.GetAllTriggers(databaseId, collectionId)
if status == datastore.StatusOk { if status == datastore.StatusOk {
c.Header("x-ms-item-count", fmt.Sprintf("%d", len(triggers))) c.Header(headers.ItemCount, fmt.Sprintf("%d", len(triggers)))
c.IndentedJSON(http.StatusOK, gin.H{"_rid": "", "Triggers": triggers, "_count": len(triggers)}) c.IndentedJSON(http.StatusOK, gin.H{"_rid": "", "Triggers": triggers, "_count": len(triggers)})
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
func (h *Handlers) GetTrigger(c *gin.Context) { func (h *Handlers) GetTrigger(c *gin.Context) {
@@ -36,11 +38,11 @@ func (h *Handlers) GetTrigger(c *gin.Context) {
} }
if status == datastore.StatusNotFound { if status == datastore.StatusNotFound {
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"}) c.IndentedJSON(http.StatusNotFound, constants.NotFoundResponse)
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
func (h *Handlers) DeleteTrigger(c *gin.Context) { func (h *Handlers) DeleteTrigger(c *gin.Context) {
@@ -55,11 +57,11 @@ func (h *Handlers) DeleteTrigger(c *gin.Context) {
} }
if status == datastore.StatusNotFound { if status == datastore.StatusNotFound {
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"}) c.IndentedJSON(http.StatusNotFound, constants.NotFoundResponse)
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
func (h *Handlers) ReplaceTrigger(c *gin.Context) { func (h *Handlers) ReplaceTrigger(c *gin.Context) {
@@ -69,19 +71,19 @@ func (h *Handlers) ReplaceTrigger(c *gin.Context) {
var trigger datastore.Trigger var trigger datastore.Trigger
if err := c.BindJSON(&trigger); err != nil { if err := c.BindJSON(&trigger); err != nil {
c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "Invalid body"}) c.IndentedJSON(http.StatusBadRequest, constants.BadRequestResponse)
return return
} }
status := h.dataStore.DeleteTrigger(databaseId, collectionId, triggerId) status := h.dataStore.DeleteTrigger(databaseId, collectionId, triggerId)
if status == datastore.StatusNotFound { if status == datastore.StatusNotFound {
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"}) c.IndentedJSON(http.StatusNotFound, constants.NotFoundResponse)
return return
} }
createdTrigger, status := h.dataStore.CreateTrigger(databaseId, collectionId, trigger) createdTrigger, status := h.dataStore.CreateTrigger(databaseId, collectionId, trigger)
if status == datastore.Conflict { if status == datastore.Conflict {
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"}) c.IndentedJSON(http.StatusConflict, constants.ConflictResponse)
return return
} }
@@ -90,7 +92,7 @@ func (h *Handlers) ReplaceTrigger(c *gin.Context) {
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
func (h *Handlers) CreateTrigger(c *gin.Context) { func (h *Handlers) CreateTrigger(c *gin.Context) {
@@ -99,13 +101,13 @@ func (h *Handlers) CreateTrigger(c *gin.Context) {
var trigger datastore.Trigger var trigger datastore.Trigger
if err := c.BindJSON(&trigger); err != nil { if err := c.BindJSON(&trigger); err != nil {
c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "Invalid body"}) c.IndentedJSON(http.StatusBadRequest, constants.BadRequestResponse)
return return
} }
createdTrigger, status := h.dataStore.CreateTrigger(databaseId, collectionId, trigger) createdTrigger, status := h.dataStore.CreateTrigger(databaseId, collectionId, trigger)
if status == datastore.Conflict { if status == datastore.Conflict {
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"}) c.IndentedJSON(http.StatusConflict, constants.ConflictResponse)
return return
} }
@@ -114,5 +116,5 @@ func (h *Handlers) CreateTrigger(c *gin.Context) {
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
+15 -13
View File
@@ -5,6 +5,8 @@ import (
"net/http" "net/http"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/pikami/cosmium/api/headers"
"github.com/pikami/cosmium/internal/constants"
"github.com/pikami/cosmium/internal/datastore" "github.com/pikami/cosmium/internal/datastore"
) )
@@ -15,12 +17,12 @@ func (h *Handlers) GetAllUserDefinedFunctions(c *gin.Context) {
udfs, status := h.dataStore.GetAllUserDefinedFunctions(databaseId, collectionId) udfs, status := h.dataStore.GetAllUserDefinedFunctions(databaseId, collectionId)
if status == datastore.StatusOk { if status == datastore.StatusOk {
c.Header("x-ms-item-count", fmt.Sprintf("%d", len(udfs))) c.Header(headers.ItemCount, fmt.Sprintf("%d", len(udfs)))
c.IndentedJSON(http.StatusOK, gin.H{"_rid": "", "UserDefinedFunctions": udfs, "_count": len(udfs)}) c.IndentedJSON(http.StatusOK, gin.H{"_rid": "", "UserDefinedFunctions": udfs, "_count": len(udfs)})
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
func (h *Handlers) GetUserDefinedFunction(c *gin.Context) { func (h *Handlers) GetUserDefinedFunction(c *gin.Context) {
@@ -36,11 +38,11 @@ func (h *Handlers) GetUserDefinedFunction(c *gin.Context) {
} }
if status == datastore.StatusNotFound { if status == datastore.StatusNotFound {
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"}) c.IndentedJSON(http.StatusNotFound, constants.NotFoundResponse)
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
func (h *Handlers) DeleteUserDefinedFunction(c *gin.Context) { func (h *Handlers) DeleteUserDefinedFunction(c *gin.Context) {
@@ -55,11 +57,11 @@ func (h *Handlers) DeleteUserDefinedFunction(c *gin.Context) {
} }
if status == datastore.StatusNotFound { if status == datastore.StatusNotFound {
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"}) c.IndentedJSON(http.StatusNotFound, constants.NotFoundResponse)
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
func (h *Handlers) ReplaceUserDefinedFunction(c *gin.Context) { func (h *Handlers) ReplaceUserDefinedFunction(c *gin.Context) {
@@ -69,19 +71,19 @@ func (h *Handlers) ReplaceUserDefinedFunction(c *gin.Context) {
var udf datastore.UserDefinedFunction var udf datastore.UserDefinedFunction
if err := c.BindJSON(&udf); err != nil { if err := c.BindJSON(&udf); err != nil {
c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "Invalid body"}) c.IndentedJSON(http.StatusBadRequest, constants.BadRequestResponse)
return return
} }
status := h.dataStore.DeleteUserDefinedFunction(databaseId, collectionId, udfId) status := h.dataStore.DeleteUserDefinedFunction(databaseId, collectionId, udfId)
if status == datastore.StatusNotFound { if status == datastore.StatusNotFound {
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"}) c.IndentedJSON(http.StatusNotFound, constants.NotFoundResponse)
return return
} }
createdUdf, status := h.dataStore.CreateUserDefinedFunction(databaseId, collectionId, udf) createdUdf, status := h.dataStore.CreateUserDefinedFunction(databaseId, collectionId, udf)
if status == datastore.Conflict { if status == datastore.Conflict {
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"}) c.IndentedJSON(http.StatusConflict, constants.ConflictResponse)
return return
} }
@@ -90,7 +92,7 @@ func (h *Handlers) ReplaceUserDefinedFunction(c *gin.Context) {
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
func (h *Handlers) CreateUserDefinedFunction(c *gin.Context) { func (h *Handlers) CreateUserDefinedFunction(c *gin.Context) {
@@ -99,13 +101,13 @@ func (h *Handlers) CreateUserDefinedFunction(c *gin.Context) {
var udf datastore.UserDefinedFunction var udf datastore.UserDefinedFunction
if err := c.BindJSON(&udf); err != nil { if err := c.BindJSON(&udf); err != nil {
c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "Invalid body"}) c.IndentedJSON(http.StatusBadRequest, constants.BadRequestResponse)
return return
} }
createdUdf, status := h.dataStore.CreateUserDefinedFunction(databaseId, collectionId, udf) createdUdf, status := h.dataStore.CreateUserDefinedFunction(databaseId, collectionId, udf)
if status == datastore.Conflict { if status == datastore.Conflict {
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"}) c.IndentedJSON(http.StatusConflict, constants.ConflictResponse)
return return
} }
@@ -114,5 +116,5 @@ func (h *Handlers) CreateUserDefinedFunction(c *gin.Context) {
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
+20
View File
@@ -0,0 +1,20 @@
package headers
const (
AIM = "A-Im"
Authorization = "authorization"
CosmosLsn = "x-ms-cosmos-llsn"
ETag = "etag"
GlobalCommittedLsn = "x-ms-global-committed-lsn"
IfNoneMatch = "if-none-match"
IsBatchRequest = "x-ms-cosmos-is-batch-request"
IsQueryPlanRequest = "x-ms-cosmos-is-query-plan-request"
IsUpsert = "x-ms-documentdb-is-upsert"
ItemCount = "x-ms-item-count"
LSN = "lsn"
XDate = "x-ms-date"
// Kinda retarded, but what can I do ¯\_(ツ)_/¯
IsQuery = "x-ms-documentdb-isquery" // Sent from python sdk and web explorer
Query = "x-ms-documentdb-query" // Sent from Go sdk
)
+7 -3
View File
@@ -19,7 +19,7 @@ func Test_Authentication(t *testing.T) {
t.Run("Should get 200 when correct account key is used", func(t *testing.T) { t.Run("Should get 200 when correct account key is used", func(t *testing.T) {
ts.DataStore.DeleteDatabase(testDatabaseName) ts.DataStore.DeleteDatabase(testDatabaseName)
client, err := azcosmos.NewClientFromConnectionString( client, err := azcosmos.NewClientFromConnectionString(
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, config.DefaultAccountKey), formatConnectionString(ts.URL, config.DefaultAccountKey),
&azcosmos.ClientOptions{}, &azcosmos.ClientOptions{},
) )
assert.Nil(t, err) assert.Nil(t, err)
@@ -35,7 +35,7 @@ func Test_Authentication(t *testing.T) {
t.Run("Should get 401 when wrong account key is used", func(t *testing.T) { t.Run("Should get 401 when wrong account key is used", func(t *testing.T) {
ts.DataStore.DeleteDatabase(testDatabaseName) ts.DataStore.DeleteDatabase(testDatabaseName)
client, err := azcosmos.NewClientFromConnectionString( client, err := azcosmos.NewClientFromConnectionString(
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, "AAAA"), formatConnectionString(ts.URL, "AAAA"),
&azcosmos.ClientOptions{}, &azcosmos.ClientOptions{},
) )
assert.Nil(t, err) assert.Nil(t, err)
@@ -72,7 +72,7 @@ func Test_Authentication_Disabled(t *testing.T) {
t.Run("Should get 200 when wrong account key is used, but authentication is dissabled", func(t *testing.T) { t.Run("Should get 200 when wrong account key is used, but authentication is dissabled", func(t *testing.T) {
ts.DataStore.DeleteDatabase(testDatabaseName) ts.DataStore.DeleteDatabase(testDatabaseName)
client, err := azcosmos.NewClientFromConnectionString( client, err := azcosmos.NewClientFromConnectionString(
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, "AAAA"), formatConnectionString(ts.URL, "AAAA"),
&azcosmos.ClientOptions{}, &azcosmos.ClientOptions{},
) )
assert.Nil(t, err) assert.Nil(t, err)
@@ -85,3 +85,7 @@ func Test_Authentication_Disabled(t *testing.T) {
assert.Equal(t, createResponse.DatabaseProperties.ID, testDatabaseName) assert.Equal(t, createResponse.DatabaseProperties.ID, testDatabaseName)
}) })
} }
func formatConnectionString(endpoint, key string) string {
return fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", endpoint, key)
}
+35 -13
View File
@@ -3,32 +3,29 @@ package tests_test
import ( import (
"context" "context"
"errors" "errors"
"fmt"
"net/http" "net/http"
"testing" "testing"
"github.com/Azure/azure-sdk-for-go/sdk/azcore" "github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos" "github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"
"github.com/pikami/cosmium/api/config"
"github.com/pikami/cosmium/internal/datastore" "github.com/pikami/cosmium/internal/datastore"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func Test_Collections(t *testing.T) { func Test_Collections(t *testing.T) {
ts := runTestServer() presets := []testPreset{PresetJsonStore, PresetBadgerStore}
defer ts.Server.Close()
client, err := azcosmos.NewClientFromConnectionString(
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, config.DefaultAccountKey),
&azcosmos.ClientOptions{},
)
assert.Nil(t, err)
setUp := func(ts *TestServer, client *azcosmos.Client) *azcosmos.DatabaseClient {
ts.DataStore.CreateDatabase(datastore.Database{ID: testDatabaseName}) ts.DataStore.CreateDatabase(datastore.Database{ID: testDatabaseName})
databaseClient, err := client.NewDatabase(testDatabaseName) databaseClient, err := client.NewDatabase(testDatabaseName)
assert.Nil(t, err) assert.Nil(t, err)
t.Run("Collection Create", func(t *testing.T) { return databaseClient
}
runTestsWithPresets(t, "Collection Create", presets, func(t *testing.T, ts *TestServer, client *azcosmos.Client) {
databaseClient := setUp(ts, client)
t.Run("Should create collection", func(t *testing.T) { t.Run("Should create collection", func(t *testing.T) {
createResponse, err := databaseClient.CreateContainer(context.TODO(), azcosmos.ContainerProperties{ createResponse, err := databaseClient.CreateContainer(context.TODO(), azcosmos.ContainerProperties{
ID: testCollectionName, ID: testCollectionName,
@@ -57,7 +54,9 @@ func Test_Collections(t *testing.T) {
}) })
}) })
t.Run("Collection Read", func(t *testing.T) { runTestsWithPresets(t, "Collection Read", presets, func(t *testing.T, ts *TestServer, client *azcosmos.Client) {
databaseClient := setUp(ts, client)
t.Run("Should read collection", func(t *testing.T) { t.Run("Should read collection", func(t *testing.T) {
ts.DataStore.CreateCollection(testDatabaseName, datastore.Collection{ ts.DataStore.CreateCollection(testDatabaseName, datastore.Collection{
ID: testCollectionName, ID: testCollectionName,
@@ -90,7 +89,9 @@ func Test_Collections(t *testing.T) {
}) })
}) })
t.Run("Collection Delete", func(t *testing.T) { runTestsWithPresets(t, "Collection Delete", presets, func(t *testing.T, ts *TestServer, client *azcosmos.Client) {
databaseClient := setUp(ts, client)
t.Run("Should delete collection", func(t *testing.T) { t.Run("Should delete collection", func(t *testing.T) {
ts.DataStore.CreateCollection(testDatabaseName, datastore.Collection{ ts.DataStore.CreateCollection(testDatabaseName, datastore.Collection{
ID: testCollectionName, ID: testCollectionName,
@@ -120,5 +121,26 @@ func Test_Collections(t *testing.T) {
panic(err) panic(err)
} }
}) })
t.Run("Should delete collection with exactly matching name", func(t *testing.T) {
ts.DataStore.CreateCollection(testDatabaseName, datastore.Collection{
ID: testCollectionName + "extra",
})
ts.DataStore.CreateCollection(testDatabaseName, datastore.Collection{
ID: testCollectionName,
})
collectionResponse, err := databaseClient.NewContainer(testCollectionName)
assert.Nil(t, err)
readResponse, err := collectionResponse.Delete(context.TODO(), &azcosmos.DeleteContainerOptions{})
assert.Nil(t, err)
assert.Equal(t, readResponse.RawResponse.StatusCode, http.StatusNoContent)
collections, status := ts.DataStore.GetAllCollections(testDatabaseName)
assert.Equal(t, status, datastore.StatusOk)
assert.Len(t, collections, 1)
assert.Equal(t, collections[0].ID, testCollectionName+"extra")
})
}) })
} }
+70 -10
View File
@@ -1,13 +1,18 @@
package tests_test package tests_test
import ( import (
"fmt"
"net/http/httptest" "net/http/httptest"
"testing"
"github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"
"github.com/pikami/cosmium/api" "github.com/pikami/cosmium/api"
"github.com/pikami/cosmium/api/config" "github.com/pikami/cosmium/api/config"
"github.com/pikami/cosmium/internal/datastore" "github.com/pikami/cosmium/internal/datastore"
mapdatastore "github.com/pikami/cosmium/internal/datastore/map_datastore" badgerdatastore "github.com/pikami/cosmium/internal/datastore/badger_datastore"
jsondatastore "github.com/pikami/cosmium/internal/datastore/json_datastore"
"github.com/pikami/cosmium/internal/logger" "github.com/pikami/cosmium/internal/logger"
"github.com/stretchr/testify/assert"
) )
type TestServer struct { type TestServer struct {
@@ -16,14 +21,29 @@ type TestServer struct {
URL string URL string
} }
func runTestServerCustomConfig(config *config.ServerConfig) *TestServer { func getDefaultTestServerConfig() *config.ServerConfig {
dataStore := mapdatastore.NewMapDataStore(mapdatastore.MapDataStoreOptions{}) return &config.ServerConfig{
AccountKey: config.DefaultAccountKey,
ExplorerPath: "/tmp/nothing",
ExplorerBaseUrlLocation: config.ExplorerBaseUrlLocation,
DataStore: "json",
}
}
api := api.NewApiServer(dataStore, config) func runTestServerCustomConfig(configuration *config.ServerConfig) *TestServer {
var dataStore datastore.DataStore
switch configuration.DataStore {
case config.DataStoreBadger:
dataStore = badgerdatastore.NewBadgerDataStore(badgerdatastore.BadgerDataStoreOptions{})
default:
dataStore = jsondatastore.NewJsonDataStore(jsondatastore.JsonDataStoreOptions{})
}
api := api.NewApiServer(dataStore, configuration)
server := httptest.NewServer(api.GetRouter()) server := httptest.NewServer(api.GetRouter())
config.DatabaseEndpoint = server.URL configuration.DatabaseEndpoint = server.URL
return &TestServer{ return &TestServer{
Server: server, Server: server,
@@ -33,11 +53,7 @@ func runTestServerCustomConfig(config *config.ServerConfig) *TestServer {
} }
func runTestServer() *TestServer { func runTestServer() *TestServer {
config := &config.ServerConfig{ config := getDefaultTestServerConfig()
AccountKey: config.DefaultAccountKey,
ExplorerPath: "/tmp/nothing",
ExplorerBaseUrlLocation: config.ExplorerBaseUrlLocation,
}
config.LogLevel = "debug" config.LogLevel = "debug"
logger.SetLogLevel(logger.LogLevelDebug) logger.SetLogLevel(logger.LogLevelDebug)
@@ -50,3 +66,47 @@ const (
testDatabaseName = "test-db" testDatabaseName = "test-db"
testCollectionName = "test-coll" testCollectionName = "test-coll"
) )
type testFunc func(t *testing.T, ts *TestServer, cosmosClient *azcosmos.Client)
type testPreset string
const (
PresetJsonStore testPreset = "JsonDS"
PresetBadgerStore testPreset = "BadgerDS"
)
func runTestsWithPreset(t *testing.T, name string, testPreset testPreset, f testFunc) {
serverConfig := getDefaultTestServerConfig()
serverConfig.LogLevel = "debug"
logger.SetLogLevel(logger.LogLevelDebug)
switch testPreset {
case PresetBadgerStore:
serverConfig.DataStore = config.DataStoreBadger
case PresetJsonStore:
serverConfig.DataStore = config.DataStoreJson
}
ts := runTestServerCustomConfig(serverConfig)
defer ts.Server.Close()
defer ts.DataStore.Close()
client, err := azcosmos.NewClientFromConnectionString(
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, config.DefaultAccountKey),
&azcosmos.ClientOptions{},
)
assert.Nil(t, err)
testName := fmt.Sprintf("%s_%s", testPreset, name)
t.Run(testName, func(t *testing.T) {
f(t, ts, client)
})
}
func runTestsWithPresets(t *testing.T, name string, testPresets []testPreset, f testFunc) {
for _, testPreset := range testPresets {
runTestsWithPreset(t, name, testPreset, f)
}
}
+25 -13
View File
@@ -3,28 +3,19 @@ package tests_test
import ( import (
"context" "context"
"errors" "errors"
"fmt"
"net/http" "net/http"
"testing" "testing"
"github.com/Azure/azure-sdk-for-go/sdk/azcore" "github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos" "github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"
"github.com/pikami/cosmium/api/config"
"github.com/pikami/cosmium/internal/datastore" "github.com/pikami/cosmium/internal/datastore"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func Test_Databases(t *testing.T) { func Test_Databases(t *testing.T) {
ts := runTestServer() presets := []testPreset{PresetJsonStore, PresetBadgerStore}
defer ts.Server.Close()
client, err := azcosmos.NewClientFromConnectionString( runTestsWithPresets(t, "Database Create", presets, func(t *testing.T, ts *TestServer, client *azcosmos.Client) {
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, config.DefaultAccountKey),
&azcosmos.ClientOptions{},
)
assert.Nil(t, err)
t.Run("Database Create", func(t *testing.T) {
t.Run("Should create database", func(t *testing.T) { t.Run("Should create database", func(t *testing.T) {
ts.DataStore.DeleteDatabase(testDatabaseName) ts.DataStore.DeleteDatabase(testDatabaseName)
@@ -55,7 +46,7 @@ func Test_Databases(t *testing.T) {
}) })
}) })
t.Run("Database Read", func(t *testing.T) { runTestsWithPresets(t, "Database Read", presets, func(t *testing.T, ts *TestServer, client *azcosmos.Client) {
t.Run("Should read database", func(t *testing.T) { t.Run("Should read database", func(t *testing.T) {
ts.DataStore.CreateDatabase(datastore.Database{ ts.DataStore.CreateDatabase(datastore.Database{
ID: testDatabaseName, ID: testDatabaseName,
@@ -88,7 +79,7 @@ func Test_Databases(t *testing.T) {
}) })
}) })
t.Run("Database Delete", func(t *testing.T) { runTestsWithPresets(t, "Database Delete", presets, func(t *testing.T, ts *TestServer, client *azcosmos.Client) {
t.Run("Should delete database", func(t *testing.T) { t.Run("Should delete database", func(t *testing.T) {
ts.DataStore.CreateDatabase(datastore.Database{ ts.DataStore.CreateDatabase(datastore.Database{
ID: testDatabaseName, ID: testDatabaseName,
@@ -118,5 +109,26 @@ func Test_Databases(t *testing.T) {
panic(err) panic(err)
} }
}) })
t.Run("Should delete database with exactly matching name", func(t *testing.T) {
ts.DataStore.CreateDatabase(datastore.Database{
ID: testDatabaseName + "extra",
})
ts.DataStore.CreateDatabase(datastore.Database{
ID: testDatabaseName,
})
databaseResponse, err := client.NewDatabase(testDatabaseName)
assert.Nil(t, err)
readResponse, err := databaseResponse.Delete(context.TODO(), &azcosmos.DeleteDatabaseOptions{})
assert.Nil(t, err)
assert.Equal(t, readResponse.RawResponse.StatusCode, http.StatusNoContent)
dbs, status := ts.DataStore.GetAllDatabases()
assert.Equal(t, status, datastore.StatusOk)
assert.Len(t, dbs, 1)
assert.Equal(t, dbs[0].ID, testDatabaseName+"extra")
})
}) })
} }
+14 -15
View File
@@ -53,9 +53,7 @@ func testCosmosQuery(t *testing.T,
} }
} }
func documents_InitializeDb(t *testing.T) (*TestServer, *azcosmos.ContainerClient) { func documents_InitializeDb(t *testing.T, ts *TestServer) *azcosmos.ContainerClient {
ts := runTestServer()
ts.DataStore.CreateDatabase(datastore.Database{ID: testDatabaseName}) ts.DataStore.CreateDatabase(datastore.Database{ID: testDatabaseName})
ts.DataStore.CreateCollection(testDatabaseName, datastore.Collection{ ts.DataStore.CreateCollection(testDatabaseName, datastore.Collection{
ID: testCollectionName, ID: testCollectionName,
@@ -79,12 +77,14 @@ func documents_InitializeDb(t *testing.T) (*TestServer, *azcosmos.ContainerClien
collectionClient, err := client.NewContainer(testDatabaseName, testCollectionName) collectionClient, err := client.NewContainer(testDatabaseName, testCollectionName)
assert.Nil(t, err) assert.Nil(t, err)
return ts, collectionClient return collectionClient
} }
func Test_Documents(t *testing.T) { func Test_Documents(t *testing.T) {
ts, collectionClient := documents_InitializeDb(t) presets := []testPreset{PresetJsonStore, PresetBadgerStore}
defer ts.Server.Close()
runTestsWithPresets(t, "Test_Documents", presets, func(t *testing.T, ts *TestServer, client *azcosmos.Client) {
collectionClient := documents_InitializeDb(t, ts)
t.Run("Should query document", func(t *testing.T) { t.Run("Should query document", func(t *testing.T) {
testCosmosQuery(t, collectionClient, testCosmosQuery(t, collectionClient,
@@ -227,11 +227,10 @@ func Test_Documents(t *testing.T) {
} }
} }
}) })
} })
func Test_Documents_Patch(t *testing.T) { runTestsWithPresets(t, "Test_Documents_Patch", presets, func(t *testing.T, ts *TestServer, client *azcosmos.Client) {
ts, collectionClient := documents_InitializeDb(t) collectionClient := documents_InitializeDb(t, ts)
defer ts.Server.Close()
t.Run("Should PATCH document", func(t *testing.T) { t.Run("Should PATCH document", func(t *testing.T) {
context := context.TODO() context := context.TODO()
@@ -377,11 +376,10 @@ func Test_Documents_Patch(t *testing.T) {
assert.NotNil(t, r) assert.NotNil(t, r)
assert.Nil(t, err2) assert.Nil(t, err2)
}) })
} })
func Test_Documents_TransactionalBatch(t *testing.T) { runTestsWithPresets(t, "Test_Documents_TransactionalBatch", presets, func(t *testing.T, ts *TestServer, client *azcosmos.Client) {
ts, collectionClient := documents_InitializeDb(t) collectionClient := documents_InitializeDb(t, ts)
defer ts.Server.Close()
t.Run("Should execute CREATE transactional batch", func(t *testing.T) { t.Run("Should execute CREATE transactional batch", func(t *testing.T) {
context := context.TODO() context := context.TODO()
@@ -427,7 +425,7 @@ func Test_Documents_TransactionalBatch(t *testing.T) {
assert.Equal(t, int32(http.StatusNoContent), operationResponse.StatusCode) assert.Equal(t, int32(http.StatusNoContent), operationResponse.StatusCode)
_, status := ts.DataStore.GetDocument(testDatabaseName, testCollectionName, "12345") _, status := ts.DataStore.GetDocument(testDatabaseName, testCollectionName, "12345")
assert.Equal(t, datastore.StatusNotFound, int(status)) assert.Equal(t, datastore.StatusNotFound, status)
}) })
t.Run("Should execute REPLACE transactional batch", func(t *testing.T) { t.Run("Should execute REPLACE transactional batch", func(t *testing.T) {
@@ -513,4 +511,5 @@ func Test_Documents_TransactionalBatch(t *testing.T) {
json.Unmarshal(operationResponse.ResourceBody, &itemResponseBody) json.Unmarshal(operationResponse.ResourceBody, &itemResponseBody)
assert.Equal(t, "67890", itemResponseBody["id"]) assert.Equal(t, "67890", itemResponseBody["id"])
}) })
})
} }
+5 -3
View File
@@ -8,13 +8,15 @@ import (
"time" "time"
"github.com/pikami/cosmium/api/config" "github.com/pikami/cosmium/api/config"
"github.com/pikami/cosmium/api/headers"
"github.com/pikami/cosmium/internal/authentication" "github.com/pikami/cosmium/internal/authentication"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
// Request document with trailing slash like python cosmosdb client does. // Request document with trailing slash like python cosmosdb client does.
func Test_Documents_Read_Trailing_Slash(t *testing.T) { func Test_Documents_Read_Trailing_Slash(t *testing.T) {
ts, _ := documents_InitializeDb(t) ts := runTestServer()
documents_InitializeDb(t, ts)
defer ts.Server.Close() defer ts.Server.Close()
t.Run("Read doc with client that appends slash to path", func(t *testing.T) { t.Run("Read doc with client that appends slash to path", func(t *testing.T) {
@@ -25,8 +27,8 @@ func Test_Documents_Read_Trailing_Slash(t *testing.T) {
signature := authentication.GenerateSignature("GET", "docs", path, date, config.DefaultAccountKey) signature := authentication.GenerateSignature("GET", "docs", path, date, config.DefaultAccountKey)
httpClient := &http.Client{} httpClient := &http.Client{}
req, _ := http.NewRequest("GET", testUrl, nil) req, _ := http.NewRequest("GET", testUrl, nil)
req.Header.Add("x-ms-date", date) req.Header.Add(headers.XDate, date)
req.Header.Add("authorization", "sig="+url.QueryEscape(signature)) req.Header.Add(headers.Authorization, "sig="+url.QueryEscape(signature))
res, err := httpClient.Do(req) res, err := httpClient.Do(req)
assert.Nil(t, err) assert.Nil(t, err)
+17 -4
View File
@@ -8,16 +8,29 @@ import (
"github.com/pikami/cosmium/api" "github.com/pikami/cosmium/api"
"github.com/pikami/cosmium/api/config" "github.com/pikami/cosmium/api/config"
"github.com/pikami/cosmium/internal/datastore" "github.com/pikami/cosmium/internal/datastore"
mapdatastore "github.com/pikami/cosmium/internal/datastore/map_datastore" badgerdatastore "github.com/pikami/cosmium/internal/datastore/badger_datastore"
jsondatastore "github.com/pikami/cosmium/internal/datastore/json_datastore"
"github.com/pikami/cosmium/internal/logger"
) )
func main() { func main() {
configuration := config.ParseFlags() configuration := config.ParseFlags()
var dataStore datastore.DataStore = mapdatastore.NewMapDataStore(mapdatastore.MapDataStoreOptions{ var dataStore datastore.DataStore
switch configuration.DataStore {
case config.DataStoreBadger:
dataStore = badgerdatastore.NewBadgerDataStore(badgerdatastore.BadgerDataStoreOptions{
InitialDataFilePath: configuration.InitialDataFilePath, InitialDataFilePath: configuration.InitialDataFilePath,
PersistDataFilePath: configuration.PersistDataFilePath, PersistDataFilePath: configuration.PersistDataFilePath,
}) })
logger.InfoLn("Using Badger data store")
default:
dataStore = jsondatastore.NewJsonDataStore(jsondatastore.JsonDataStoreOptions{
InitialDataFilePath: configuration.InitialDataFilePath,
PersistDataFilePath: configuration.PersistDataFilePath,
})
logger.InfoLn("Using in-memory data store")
}
server := api.NewApiServer(dataStore, &configuration) server := api.NewApiServer(dataStore, &configuration)
err := server.Start() err := server.Start()
@@ -25,10 +38,10 @@ func main() {
panic(err) panic(err)
} }
waitForExit(server, dataStore, configuration) waitForExit(server, dataStore)
} }
func waitForExit(server *api.ApiServer, dataStore datastore.DataStore, config config.ServerConfig) { func waitForExit(server *api.ApiServer, dataStore datastore.DataStore) {
sigs := make(chan os.Signal, 1) sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
+1 -1
View File
@@ -79,7 +79,7 @@ Cosmium strives to support the core features of Cosmos DB, including:
| Function | Implemented | | Function | Implemented |
| -------- | ----------- | | -------- | ----------- |
| IIF | No | | IIF | Yes |
### Date and time Functions ### Date and time Functions
+45 -24
View File
@@ -1,46 +1,67 @@
module github.com/pikami/cosmium module github.com/pikami/cosmium
go 1.24.0 go 1.24.7
require ( require (
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.17.0 github.com/Azure/azure-sdk-for-go/sdk/azcore v1.19.1
github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos v1.3.0 github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos v1.4.1
github.com/cosmiumdev/json-patch/v5 v5.9.3 github.com/cosmiumdev/json-patch/v5 v5.9.11
github.com/gin-gonic/gin v1.10.0 github.com/dgraph-io/badger/v4 v4.8.0
github.com/gin-gonic/gin v1.11.0
github.com/google/uuid v1.6.0 github.com/google/uuid v1.6.0
github.com/stretchr/testify v1.10.0 github.com/stretchr/testify v1.11.1
golang.org/x/exp v0.0.0-20250218142911-aa4b98e5adaa github.com/vmihailenco/msgpack/v5 v5.4.1
golang.org/x/exp v0.0.0-20251002181428-27f1f14c8bb9
) )
require ( require (
github.com/Azure/azure-sdk-for-go v68.0.0+incompatible // indirect github.com/Azure/azure-sdk-for-go v68.0.0+incompatible // indirect
github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0 // indirect github.com/Azure/azure-sdk-for-go/sdk/internal v1.11.2 // indirect
github.com/bytedance/sonic v1.12.9 // indirect github.com/bytedance/gopkg v0.1.3 // indirect
github.com/bytedance/sonic/loader v0.2.3 // indirect github.com/bytedance/sonic v1.14.1 // indirect
github.com/cloudwego/base64x v0.1.5 // indirect github.com/bytedance/sonic/loader v0.3.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/cloudwego/base64x v0.1.6 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gabriel-vasile/mimetype v1.4.8 // indirect github.com/dgraph-io/ristretto/v2 v2.3.0 // indirect
github.com/gin-contrib/sse v1.0.0 // indirect github.com/dustin/go-humanize v1.0.1 // indirect
github.com/gabriel-vasile/mimetype v1.4.10 // indirect
github.com/gin-contrib/sse v1.1.0 // indirect
github.com/go-logr/logr v1.4.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.25.0 // indirect github.com/go-playground/validator/v10 v10.28.0 // indirect
github.com/goccy/go-json v0.10.5 // indirect github.com/goccy/go-json v0.10.5 // indirect
github.com/goccy/go-yaml v1.18.0 // indirect
github.com/google/flatbuffers v25.9.23+incompatible // indirect
github.com/json-iterator/go v1.1.12 // indirect github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.10 // indirect github.com/klauspost/compress v1.18.0 // indirect
github.com/klauspost/cpuid/v2 v2.3.0 // indirect
github.com/leodido/go-urn v1.4.0 // indirect github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-isatty v0.0.20 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect github.com/pelletier/go-toml/v2 v2.2.4 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/quic-go/qpack v0.5.1 // indirect
github.com/quic-go/quic-go v0.55.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect github.com/ugorji/go/codec v1.3.0 // indirect
golang.org/x/arch v0.14.0 // indirect github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
golang.org/x/crypto v0.35.0 // indirect go.opentelemetry.io/auto/sdk v1.2.1 // indirect
golang.org/x/net v0.35.0 // indirect go.opentelemetry.io/otel v1.38.0 // indirect
golang.org/x/sys v0.30.0 // indirect go.opentelemetry.io/otel/metric v1.38.0 // indirect
golang.org/x/text v0.22.0 // indirect go.opentelemetry.io/otel/trace v1.38.0 // indirect
google.golang.org/protobuf v1.36.5 // indirect go.uber.org/mock v0.6.0 // indirect
golang.org/x/arch v0.22.0 // indirect
golang.org/x/crypto v0.43.0 // indirect
golang.org/x/mod v0.29.0 // indirect
golang.org/x/net v0.45.0 // indirect
golang.org/x/sync v0.17.0 // indirect
golang.org/x/sys v0.37.0 // indirect
golang.org/x/text v0.30.0 // indirect
golang.org/x/tools v0.37.0 // indirect
google.golang.org/protobuf v1.36.10 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect
) )
+101 -64
View File
@@ -1,57 +1,76 @@
github.com/Azure/azure-sdk-for-go v68.0.0+incompatible h1:fcYLmCpyNYRnvJbPerq7U0hS+6+I79yEDJBqVNcqUzU= github.com/Azure/azure-sdk-for-go v68.0.0+incompatible h1:fcYLmCpyNYRnvJbPerq7U0hS+6+I79yEDJBqVNcqUzU=
github.com/Azure/azure-sdk-for-go v68.0.0+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc= github.com/Azure/azure-sdk-for-go v68.0.0+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc=
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.17.0 h1:g0EZJwz7xkXQiZAI5xi9f3WWFYBlX1CPTrR+NDToRkQ= github.com/Azure/azure-sdk-for-go/sdk/azcore v1.19.1 h1:5YTBM8QDVIBN3sxBil89WfdAAqDZbyJTgh688DSxX5w=
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.17.0/go.mod h1:XCW7KnZet0Opnr7HccfUw1PLc4CjHqpcaxW8DHklNkQ= github.com/Azure/azure-sdk-for-go/sdk/azcore v1.19.1/go.mod h1:YD5h/ldMsG0XiIw7PdyNhLxaM317eFh5yNLccNfGdyw=
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.8.0 h1:B/dfvscEQtew9dVuoxqxrUKKv8Ih2f55PydknDamU+g= github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.10.1 h1:B+blDbyVIG3WaikNxPnhPiJ1MThR03b3vKGtER95TP4=
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.8.0/go.mod h1:fiPSssYvltE08HJchL04dOy+RD4hgrjph0cwGGMntdI= github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.10.1/go.mod h1:JdM5psgjfBf5fo2uWOZhflPWyDBZ/O/CNAH9CtsuZE4=
github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos v1.3.0 h1:RGcdpSElvcXCwxydI0xzOBu1Gvp88OoiTGfbtO/z1m0= github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos v1.4.1 h1:ToPLhnXvatKVN4ZkcxLOwcXOJhdu4iQl8w0efeuDz9Y=
github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos v1.3.0/go.mod h1:YwUyrNUtcZcibA99JcfCP6UUp95VVQKO2MJfBzgJDwA= github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos v1.4.1/go.mod h1:Krtog/7tz27z75TwM5cIS8bxEH4dcBUezcq+kGVeZEo=
github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0 h1:ywEEhmNahHBihViHepv3xPBn1663uRv2t2q/ESv9seY= github.com/Azure/azure-sdk-for-go/sdk/internal v1.11.2 h1:9iefClla7iYpfYWdzPCRDozdmndjTm8DXdpCzPajMgA=
github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0/go.mod h1:iZDifYGJTIgIIkYRNWPENUnqx6bJ2xnSDFI2tjwZNuY= github.com/Azure/azure-sdk-for-go/sdk/internal v1.11.2/go.mod h1:XtLgD3ZD34DAaVIIAyG3objl5DynM3CQ/vMcbBNJZGI=
github.com/AzureAD/microsoft-authentication-library-for-go v1.3.2 h1:kYRSnvJju5gYVyhkij+RTJ/VR6QIUaCfWeaFm2ycsjQ= github.com/AzureAD/microsoft-authentication-library-for-go v1.4.2 h1:oygO0locgZJe7PpYPXT5A29ZkwJaPqcva7BVeemZOZs=
github.com/AzureAD/microsoft-authentication-library-for-go v1.3.2/go.mod h1:wP83P5OoQ5p6ip3ScPr0BAq0BvuPAvacpEuSzyouqAI= github.com/AzureAD/microsoft-authentication-library-for-go v1.4.2/go.mod h1:wP83P5OoQ5p6ip3ScPr0BAq0BvuPAvacpEuSzyouqAI=
github.com/bytedance/sonic v1.12.9 h1:Od1BvK55NnewtGaJsTDeAOSnLVO2BTSLOe0+ooKokmQ= github.com/bytedance/gopkg v0.1.3 h1:TPBSwH8RsouGCBcMBktLt1AymVo2TVsBVCY4b6TnZ/M=
github.com/bytedance/sonic v1.12.9/go.mod h1:uVvFidNmlt9+wa31S1urfwwthTWteBgG0hWuoKAXTx8= github.com/bytedance/gopkg v0.1.3/go.mod h1:576VvJ+eJgyCzdjS+c4+77QF3p7ubbtiKARP3TxducM=
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= github.com/bytedance/sonic v1.14.1 h1:FBMC0zVz5XUmE4z9wF4Jey0An5FueFvOsTKKKtwIl7w=
github.com/bytedance/sonic/loader v0.2.3 h1:yctD0Q3v2NOGfSWPLPvG2ggA2kV6TS6s4wioyEqssH0= github.com/bytedance/sonic v1.14.1/go.mod h1:gi6uhQLMbTdeP0muCnrjHLeCUPyb70ujhnNlhOylAFc=
github.com/bytedance/sonic/loader v0.2.3/go.mod h1:N8A3vUdtUebEY2/VQC0MyhYeKUFosQU6FxH2JmUe6VI= github.com/bytedance/sonic/loader v0.3.0 h1:dskwH8edlzNMctoruo8FPTJDF3vLtDT0sXZwvZJyqeA=
github.com/cloudwego/base64x v0.1.5 h1:XPciSp1xaq2VCSt6lF0phncD4koWyULpl5bUxbfCyP4= github.com/bytedance/sonic/loader v0.3.0/go.mod h1:N8A3vUdtUebEY2/VQC0MyhYeKUFosQU6FxH2JmUe6VI=
github.com/cloudwego/base64x v0.1.5/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w= github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY= github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cosmiumdev/json-patch/v5 v5.9.3 h1:l+Og3+5edqV2NHDo58sz72eS733lbXVYP61seYK43Do= github.com/cloudwego/base64x v0.1.6 h1:t11wG9AECkCDk5fMSoxmufanudBtJ+/HemLstXDLI2M=
github.com/cosmiumdev/json-patch/v5 v5.9.3/go.mod h1:WzSTCdia0WrlZtjnL19P4RiwWtfdyArm/E7stgEeP5g= github.com/cloudwego/base64x v0.1.6/go.mod h1:OFcloc187FXDaYHvrNIjxSe8ncn0OOM8gEHfghB2IPU=
github.com/cosmiumdev/json-patch/v5 v5.9.11 h1:WD2Wqaz/vO987z2FFdqgkj15HgYZ/Y5TpqE3I4T/iOQ=
github.com/cosmiumdev/json-patch/v5 v5.9.11/go.mod h1:YPZmckmv4ZY+oxKIOjgq3sIudHVB6VEMcicCS9LtVLM=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gabriel-vasile/mimetype v1.4.8 h1:FfZ3gj38NjllZIeJAmMhr+qKL8Wu+nOoI3GqacKw1NM= github.com/dgraph-io/badger/v4 v4.8.0 h1:JYph1ChBijCw8SLeybvPINizbDKWZ5n/GYbz2yhN/bs=
github.com/gabriel-vasile/mimetype v1.4.8/go.mod h1:ByKUIKGjh1ODkGM1asKUbQZOLGrPjydw3hYPU2YU9t8= github.com/dgraph-io/badger/v4 v4.8.0/go.mod h1:U6on6e8k/RTbUWxqKR0MvugJuVmkxSNc79ap4917h4w=
github.com/gin-contrib/sse v1.0.0 h1:y3bT1mUWUxDpW4JLQg/HnTqV4rozuW4tC9eFKTxYI9E= github.com/dgraph-io/ristretto/v2 v2.3.0 h1:qTQ38m7oIyd4GAed/QkUZyPFNMnvVWyazGXRwvOt5zk=
github.com/gin-contrib/sse v1.0.0/go.mod h1:zNuFdwarAygJBht0NTKiSi3jRf6RbqeILZ9Sp6Slhe0= github.com/dgraph-io/ristretto/v2 v2.3.0/go.mod h1:gpoRV3VzrEY1a9dWAYV6T1U7YzfgttXdd/ZzL1s9OZM=
github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU= github.com/dgryski/go-farm v0.0.0-20240924180020-3414d57e47da h1:aIftn67I1fkbMa512G+w+Pxci9hJPB8oMnkcP3iZF38=
github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y= github.com/dgryski/go-farm v0.0.0-20240924180020-3414d57e47da/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/gabriel-vasile/mimetype v1.4.10 h1:zyueNbySn/z8mJZHLt6IPw0KoZsiQNszIpU+bX4+ZK0=
github.com/gabriel-vasile/mimetype v1.4.10/go.mod h1:d+9Oxyo1wTzWdyVUPMmXFvp4F9tea18J8ufA774AB3s=
github.com/gin-contrib/sse v1.1.0 h1:n0w2GMuUpWDVp7qSpvze6fAu9iRxJY4Hmj6AmBOU05w=
github.com/gin-contrib/sse v1.1.0/go.mod h1:hxRZ5gVpWMT7Z0B0gSNYqqsSCNIJMjzvm6fqCz9vjwM=
github.com/gin-gonic/gin v1.11.0 h1:OW/6PLjyusp2PPXtyxKHU0RbX6I/l28FTdDlae5ueWk=
github.com/gin-gonic/gin v1.11.0/go.mod h1:+iq/FyxlGzII0KHiBGjuNn4UNENUlKbGlNmc+W50Dls=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI=
github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.25.0 h1:5Dh7cjvzR7BRZadnsVOzPhWsrwUr0nmsZJxEAnFLNO8= github.com/go-playground/validator/v10 v10.28.0 h1:Q7ibns33JjyW48gHkuFT91qX48KG0ktULL6FgHdG688=
github.com/go-playground/validator/v10 v10.25.0/go.mod h1:GGzBIJMuE98Ic/kJsBXbz1x/7cByt++cQ+YOuDM5wus= github.com/go-playground/validator/v10 v10.28.0/go.mod h1:GoI6I1SjPBh9p7ykNE/yj3fFYbyDOpwMn5KXd+m2hUU=
github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4= github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4=
github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M= github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk= github.com/goccy/go-yaml v1.18.0 h1:8W7wMFS12Pcas7KU+VVkaiCng+kG8QiFeFwzFb+rwuw=
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk= github.com/goccy/go-yaml v1.18.0/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/golang-jwt/jwt/v5 v5.2.2 h1:Rl4B7itRWVtYIHFrSNd7vhTiz9UpLdi6gZhZ3wEeDy8=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/golang-jwt/jwt/v5 v5.2.2/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/google/flatbuffers v25.9.23+incompatible h1:rGZKv+wOb6QPzIdkM2KxhBZCDrA0DeN6DNmRDrqIsQU=
github.com/google/flatbuffers v25.9.23+incompatible/go.mod h1:1AeVuKshWv4vARoZatz6mlQ0JxURH0Kv5+zNeJKJCa8=
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo=
github.com/klauspost/cpuid/v2 v2.2.10 h1:tBs3QSyvjDyFTq3uoc/9xFpCuOsJQFNPiAhYdw2skhE= github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ=
github.com/klauspost/cpuid/v2 v2.2.10/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0= github.com/klauspost/cpuid/v2 v2.3.0 h1:S4CRMLnYUhGeDFDqkGriYKdfoFlDnMtqTiI/sFzhA9Y=
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M= github.com/klauspost/cpuid/v2 v2.3.0/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
@@ -67,51 +86,69 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M= github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc= github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c h1:+mdjkGKdHQG3305AYmdv1U2eRNDiU2ErMBj1gwrq8eQ= github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c h1:+mdjkGKdHQG3305AYmdv1U2eRNDiU2ErMBj1gwrq8eQ=
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjLxUqIJNnCWiEdr3bn6IUYi15bNlnbCCU= github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjLxUqIJNnCWiEdr3bn6IUYi15bNlnbCCU=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8= github.com/quic-go/qpack v0.5.1 h1:giqksBPnT/HDtZ6VhtFKgoLOWmlyo9Ei6u9PqzIMbhI=
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4= github.com/quic-go/qpack v0.5.1/go.mod h1:+PC4XFrEskIVkcLzpEkbLqq1uCoxPhQuvK5rH1ZgaEg=
github.com/quic-go/quic-go v0.55.0 h1:zccPQIqYCXDt5NmcEabyYvOnomjs8Tlwl7tISjJh9Mk=
github.com/quic-go/quic-go v0.55.0/go.mod h1:DR51ilwU1uE164KuWXhinFcKWGlEjzys2l8zUl5Ss1U=
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE= github.com/ugorji/go/codec v1.3.0 h1:Qd2W2sQawAfG8XSvzwhBeoGq71zXOC/Q1E9y/wUcsUA=
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= github.com/ugorji/go/codec v1.3.0/go.mod h1:pRBVtBSKl77K30Bv8R2P+cLSGaTtex6fsA2Wjqmfxj4=
golang.org/x/arch v0.14.0 h1:z9JUEZWr8x4rR0OU6c4/4t6E6jOZ8/QBS2bBYBm4tx4= github.com/vmihailenco/msgpack/v5 v5.4.1 h1:cQriyiUvjTwOHg8QZaPihLWeRAAVoCpE00IUPn0Bjt8=
golang.org/x/arch v0.14.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= github.com/vmihailenco/msgpack/v5 v5.4.1/go.mod h1:GaZTsDaehaPpQVyxrf5mtQlH+pc21PIudVV/E3rRQok=
golang.org/x/crypto v0.35.0 h1:b15kiHdrGCHrP6LvwaQ3c03kgNhhiMgvlhxHQhmg2Xs= github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
golang.org/x/crypto v0.35.0/go.mod h1:dy7dXNW32cAb/6/PRuTNsix8T+vJAqvuIy5Bli/x0YQ= github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
golang.org/x/exp v0.0.0-20250218142911-aa4b98e5adaa h1:t2QcU6V556bFjYgu4L6C+6VrCPyJZ+eyRsABUPs1mz4= go.opentelemetry.io/auto/sdk v1.2.1 h1:jXsnJ4Lmnqd11kwkBV2LgLoFMZKizbCi5fNZ/ipaZ64=
golang.org/x/exp v0.0.0-20250218142911-aa4b98e5adaa/go.mod h1:BHOTPb3L19zxehTsLoJXVaTktb06DFgmdW6Wb9s8jqk= go.opentelemetry.io/auto/sdk v1.2.1/go.mod h1:KRTj+aOaElaLi+wW1kO/DZRXwkF4C5xPbEe3ZiIhN7Y=
golang.org/x/net v0.35.0 h1:T5GQRQb2y08kTAByq9L4/bz8cipCdA8FbRTXewonqY8= go.opentelemetry.io/otel v1.38.0 h1:RkfdswUDRimDg0m2Az18RKOsnI8UDzppJAtj01/Ymk8=
golang.org/x/net v0.35.0/go.mod h1:EglIi67kWsHKlRzzVMUD93VMSWGFOMSZgxFjparz1Qk= go.opentelemetry.io/otel v1.38.0/go.mod h1:zcmtmQ1+YmQM9wrNsTGV/q/uyusom3P8RxwExxkZhjM=
go.opentelemetry.io/otel/metric v1.38.0 h1:Kl6lzIYGAh5M159u9NgiRkmoMKjvbsKtYRwgfrA6WpA=
go.opentelemetry.io/otel/metric v1.38.0/go.mod h1:kB5n/QoRM8YwmUahxvI3bO34eVtQf2i4utNVLr9gEmI=
go.opentelemetry.io/otel/trace v1.38.0 h1:Fxk5bKrDZJUH+AMyyIXGcFAPah0oRcT+LuNtJrmcNLE=
go.opentelemetry.io/otel/trace v1.38.0/go.mod h1:j1P9ivuFsTceSWe1oY+EeW3sc+Pp42sO++GHkg4wwhs=
go.uber.org/mock v0.6.0 h1:hyF9dfmbgIX5EfOdasqLsWD6xqpNZlXblLB/Dbnwv3Y=
go.uber.org/mock v0.6.0/go.mod h1:KiVJ4BqZJaMj4svdfmHM0AUx4NJYO8ZNpPnZn1Z+BBU=
golang.org/x/arch v0.22.0 h1:c/Zle32i5ttqRXjdLyyHZESLD/bB90DCU1g9l/0YBDI=
golang.org/x/arch v0.22.0/go.mod h1:dNHoOeKiyja7GTvF9NJS1l3Z2yntpQNzgrjh1cU103A=
golang.org/x/crypto v0.43.0 h1:dduJYIi3A3KOfdGOHX8AVZ/jGiyPa3IbBozJ5kNuE04=
golang.org/x/crypto v0.43.0/go.mod h1:BFbav4mRNlXJL4wNeejLpWxB7wMbc79PdRGhWKncxR0=
golang.org/x/exp v0.0.0-20251002181428-27f1f14c8bb9 h1:TQwNpfvNkxAVlItJf6Cr5JTsVZoC/Sj7K3OZv2Pc14A=
golang.org/x/exp v0.0.0-20251002181428-27f1f14c8bb9/go.mod h1:TwQYMMnGpvZyc+JpB/UAuTNIsVJifOlSkrZkhcvpVUk=
golang.org/x/mod v0.29.0 h1:HV8lRxZC4l2cr3Zq1LvtOsi/ThTgWnUk/y64QSs8GwA=
golang.org/x/mod v0.29.0/go.mod h1:NyhrlYXJ2H4eJiRy/WDBO6HMqZQ6q9nk4JzS3NuCK+w=
golang.org/x/net v0.45.0 h1:RLBg5JKixCy82FtLJpeNlVM0nrSqpCRYzVU1n8kj0tM=
golang.org/x/net v0.45.0/go.mod h1:ECOoLqd5U3Lhyeyo/QDCEVQ4sNgYsqvCZ722XogGieY=
golang.org/x/sync v0.17.0 h1:l60nONMj9l5drqw6jlhIELNv9I0A4OFgRsG9k2oT9Ug=
golang.org/x/sync v0.17.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc= golang.org/x/sys v0.37.0 h1:fdNQudmxPjkdUTPnLn5mdQv7Zwvbvpaxqs831goi9kQ=
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.37.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM= golang.org/x/text v0.30.0 h1:yznKA/E9zq54KzlzBEAWn1NXSQ8DIp/NYMy88xJjl4k=
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY= golang.org/x/text v0.30.0/go.mod h1:yDdHFIX9t+tORqspjENWgzaCVXgk0yYnYuSZ8UzzBVM=
google.golang.org/protobuf v1.36.5 h1:tPhr+woSbjfYvY6/GPufUoYizxw1cF/yFoxJ2fmpwlM= golang.org/x/tools v0.37.0 h1:DVSRzp7FwePZW356yEAChSdNcQo6Nsp+fex1SUW09lE=
google.golang.org/protobuf v1.36.5/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE= golang.org/x/tools v0.37.0/go.mod h1:MBN5QPQtLMHVdvsbtarmTNukZDdgwdwlO5qGacAzF0w=
google.golang.org/protobuf v1.36.10 h1:AYd7cD/uASjIL6Q9LiTjz8JLcrh/88q5UObnmY3aOOE=
google.golang.org/protobuf v1.36.10/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
+5
View File
@@ -30,3 +30,8 @@ var QueryPlanResponse = gin.H{
}, },
}, },
} }
var UnknownErrorResponse = gin.H{"message": "Unknown error"}
var NotFoundResponse = gin.H{"message": "NotFound"}
var ConflictResponse = gin.H{"message": "Conflict"}
var BadRequestResponse = gin.H{"message": "BadRequest"}
@@ -0,0 +1,123 @@
package badgerdatastore
import (
"encoding/json"
"log"
"os"
"time"
"github.com/dgraph-io/badger/v4"
"github.com/pikami/cosmium/internal/datastore"
"github.com/pikami/cosmium/internal/logger"
)
type BadgerDataStore struct {
db *badger.DB
gcTicker *time.Ticker
}
type BadgerDataStoreOptions struct {
InitialDataFilePath string
PersistDataFilePath string
}
func NewBadgerDataStore(options BadgerDataStoreOptions) *BadgerDataStore {
badgerOpts := badger.DefaultOptions(options.PersistDataFilePath)
badgerOpts = badgerOpts.WithLogger(newBadgerLogger())
if options.PersistDataFilePath == "" {
badgerOpts = badgerOpts.WithInMemory(true)
}
db, err := badger.Open(badgerOpts)
if err != nil {
panic(err)
}
gcTicker := time.NewTicker(5 * time.Minute)
ds := &BadgerDataStore{
db: db,
gcTicker: gcTicker,
}
ds.initializeDataStore(options.InitialDataFilePath)
go ds.runGarbageCollector()
return ds
}
func (r *BadgerDataStore) Close() {
if r.gcTicker != nil {
r.gcTicker.Stop()
r.gcTicker = nil
}
r.db.Close()
r.db = nil
}
func (r *BadgerDataStore) DumpToJson() (string, error) {
logger.ErrorLn("Badger datastore does not support state export currently.")
return "{}", nil
}
func (r *BadgerDataStore) runGarbageCollector() {
for range r.gcTicker.C {
again:
err := r.db.RunValueLogGC(0.7)
if err == nil {
goto again
}
}
}
func (r *BadgerDataStore) initializeDataStore(initialDataFilePath string) {
if initialDataFilePath == "" {
return
}
stat, err := os.Stat(initialDataFilePath)
if err != nil {
panic(err)
}
if stat.IsDir() {
logger.ErrorLn("Argument '-Persist' must be a path to file, not a directory.")
os.Exit(1)
}
jsonData, err := os.ReadFile(initialDataFilePath)
if err != nil {
log.Fatalf("Error reading state JSON file: %v", err)
return
}
var state datastore.InitialDataModel
if err := json.Unmarshal([]byte(jsonData), &state); err != nil {
log.Fatalf("Error parsing state JSON file: %v", err)
return
}
for dbName, dbModel := range state.Databases {
r.CreateDatabase(dbModel)
for colName, colModel := range state.Collections[dbName] {
r.CreateCollection(dbName, colModel)
for _, docModel := range state.Documents[dbName][colName] {
r.CreateDocument(dbName, colName, docModel)
}
for _, triggerModel := range state.Triggers[dbName][colName] {
r.CreateTrigger(dbName, colName, triggerModel)
}
for _, spModel := range state.StoredProcedures[dbName][colName] {
r.CreateStoredProcedure(dbName, colName, spModel)
}
for _, udfModel := range state.UserDefinedFunctions[dbName][colName] {
r.CreateUserDefinedFunction(dbName, colName, udfModel)
}
}
}
}
@@ -0,0 +1,28 @@
package badgerdatastore
import (
"github.com/dgraph-io/badger/v4"
"github.com/pikami/cosmium/internal/logger"
)
type badgerLogger struct{}
func newBadgerLogger() badger.Logger {
return &badgerLogger{}
}
func (l *badgerLogger) Errorf(format string, v ...interface{}) {
logger.Errorf(format, v...)
}
func (l *badgerLogger) Warningf(format string, v ...interface{}) {
logger.Infof(format, v...)
}
func (l *badgerLogger) Infof(format string, v ...interface{}) {
logger.Infof(format, v...)
}
func (l *badgerLogger) Debugf(format string, v ...interface{}) {
logger.Debugf(format, v...)
}
@@ -0,0 +1,105 @@
package badgerdatastore
import (
"fmt"
"time"
"github.com/google/uuid"
"github.com/pikami/cosmium/internal/datastore"
"github.com/pikami/cosmium/internal/logger"
"github.com/pikami/cosmium/internal/resourceid"
structhidrators "github.com/pikami/cosmium/internal/struct_hidrators"
)
func (r *BadgerDataStore) GetAllCollections(databaseId string) ([]datastore.Collection, datastore.DataStoreStatus) {
exists, err := keyExists(r.db.NewTransaction(false), generateDatabaseKey(databaseId))
if err != nil {
logger.ErrorLn("Error while checking if database exists:", err)
return nil, datastore.Unknown
}
if !exists {
return nil, datastore.StatusNotFound
}
prefix := generateKey(resourceid.ResourceTypeCollection, databaseId, "", "") + "/"
colls, status := listByPrefix[datastore.Collection](r.db, prefix)
if status == datastore.StatusOk {
return colls, datastore.StatusOk
}
return nil, status
}
func (r *BadgerDataStore) GetCollection(databaseId string, collectionId string) (datastore.Collection, datastore.DataStoreStatus) {
collectionKey := generateCollectionKey(databaseId, collectionId)
txn := r.db.NewTransaction(false)
defer txn.Discard()
var collection datastore.Collection
status := getKey(txn, collectionKey, &collection)
return collection, status
}
func (r *BadgerDataStore) DeleteCollection(databaseId string, collectionId string) datastore.DataStoreStatus {
collectionKey := generateCollectionKey(databaseId, collectionId)
txn := r.db.NewTransaction(true)
defer txn.Discard()
prefixes := []string{
generateKey(resourceid.ResourceTypeDocument, databaseId, collectionId, "") + "/",
generateKey(resourceid.ResourceTypeTrigger, databaseId, collectionId, "") + "/",
generateKey(resourceid.ResourceTypeStoredProcedure, databaseId, collectionId, "") + "/",
generateKey(resourceid.ResourceTypeUserDefinedFunction, databaseId, collectionId, "") + "/",
}
for _, prefix := range prefixes {
if err := deleteKeysByPrefix(txn, prefix); err != nil {
return datastore.Unknown
}
}
deleteKey(txn, collectionKey)
err := txn.Commit()
if err != nil {
logger.ErrorLn("Error while committing transaction:", err)
return datastore.Unknown
}
return datastore.StatusOk
}
func (r *BadgerDataStore) CreateCollection(databaseId string, newCollection datastore.Collection) (datastore.Collection, datastore.DataStoreStatus) {
collectionKey := generateCollectionKey(databaseId, newCollection.ID)
txn := r.db.NewTransaction(true)
defer txn.Discard()
collectionExists, err := keyExists(txn, collectionKey)
if err != nil || collectionExists {
return datastore.Collection{}, datastore.Conflict
}
var database datastore.Database
status := getKey(txn, generateDatabaseKey(databaseId), &database)
if status != datastore.StatusOk {
return datastore.Collection{}, status
}
newCollection = structhidrators.Hidrate(newCollection).(datastore.Collection)
newCollection.TimeStamp = time.Now().Unix()
newCollection.ResourceID = resourceid.NewCombined(database.ResourceID, resourceid.New(resourceid.ResourceTypeCollection))
newCollection.ETag = fmt.Sprintf("\"%s\"", uuid.New())
newCollection.Self = fmt.Sprintf("dbs/%s/colls/%s/", database.ResourceID, newCollection.ResourceID)
status = insertKey(txn, collectionKey, newCollection)
if status != datastore.StatusOk {
return datastore.Collection{}, status
}
return newCollection, datastore.StatusOk
}
@@ -0,0 +1,81 @@
package badgerdatastore
import (
"fmt"
"time"
"github.com/google/uuid"
"github.com/pikami/cosmium/internal/datastore"
"github.com/pikami/cosmium/internal/logger"
"github.com/pikami/cosmium/internal/resourceid"
)
func (r *BadgerDataStore) GetAllDatabases() ([]datastore.Database, datastore.DataStoreStatus) {
dbs, status := listByPrefix[datastore.Database](r.db, DatabaseKeyPrefix)
if status == datastore.StatusOk {
return dbs, datastore.StatusOk
}
return nil, status
}
func (r *BadgerDataStore) GetDatabase(id string) (datastore.Database, datastore.DataStoreStatus) {
databaseKey := generateDatabaseKey(id)
txn := r.db.NewTransaction(false)
defer txn.Discard()
var database datastore.Database
status := getKey(txn, databaseKey, &database)
return database, status
}
func (r *BadgerDataStore) DeleteDatabase(id string) datastore.DataStoreStatus {
databaseKey := generateDatabaseKey(id)
txn := r.db.NewTransaction(true)
defer txn.Discard()
prefixes := []string{
generateKey(resourceid.ResourceTypeCollection, id, "", "") + "/",
generateKey(resourceid.ResourceTypeDocument, id, "", "") + "/",
generateKey(resourceid.ResourceTypeTrigger, id, "", "") + "/",
generateKey(resourceid.ResourceTypeStoredProcedure, id, "", "") + "/",
generateKey(resourceid.ResourceTypeUserDefinedFunction, id, "", "") + "/",
}
for _, prefix := range prefixes {
if err := deleteKeysByPrefix(txn, prefix); err != nil {
return datastore.Unknown
}
}
deleteKey(txn, databaseKey)
err := txn.Commit()
if err != nil {
logger.ErrorLn("Error while committing transaction:", err)
return datastore.Unknown
}
return datastore.StatusOk
}
func (r *BadgerDataStore) CreateDatabase(newDatabase datastore.Database) (datastore.Database, datastore.DataStoreStatus) {
databaseKey := generateDatabaseKey(newDatabase.ID)
txn := r.db.NewTransaction(true)
defer txn.Discard()
newDatabase.TimeStamp = time.Now().Unix()
newDatabase.ResourceID = resourceid.New(resourceid.ResourceTypeDatabase)
newDatabase.ETag = fmt.Sprintf("\"%s\"", uuid.New())
newDatabase.Self = fmt.Sprintf("dbs/%s/", newDatabase.ResourceID)
status := insertKey(txn, databaseKey, newDatabase)
if status != datastore.StatusOk {
return datastore.Database{}, status
}
return newDatabase, datastore.StatusOk
}
@@ -0,0 +1,223 @@
package badgerdatastore
import (
"github.com/dgraph-io/badger/v4"
"github.com/pikami/cosmium/internal/datastore"
"github.com/pikami/cosmium/internal/logger"
"github.com/pikami/cosmium/internal/resourceid"
"github.com/vmihailenco/msgpack/v5"
)
const (
DatabaseKeyPrefix = "DB:"
CollectionKeyPrefix = "COL:"
DocumentKeyPrefix = "DOC:"
TriggerKeyPrefix = "TRG:"
StoredProcedureKeyPrefix = "SP:"
UserDefinedFunctionKeyPrefix = "UDF:"
)
func generateKey(
resourceType resourceid.ResourceType,
databaseId string,
collectionId string,
resourceId string,
) string {
result := ""
switch resourceType {
case resourceid.ResourceTypeDatabase:
result += DatabaseKeyPrefix
case resourceid.ResourceTypeCollection:
result += CollectionKeyPrefix
case resourceid.ResourceTypeDocument:
result += DocumentKeyPrefix
case resourceid.ResourceTypeTrigger:
result += TriggerKeyPrefix
case resourceid.ResourceTypeStoredProcedure:
result += StoredProcedureKeyPrefix
case resourceid.ResourceTypeUserDefinedFunction:
result += UserDefinedFunctionKeyPrefix
}
if databaseId != "" {
result += databaseId
}
if collectionId != "" {
result += "/colls/" + collectionId
}
if resourceId != "" {
result += "/" + resourceId
}
return result
}
func generateDatabaseKey(databaseId string) string {
return generateKey(resourceid.ResourceTypeDatabase, databaseId, "", "")
}
func generateCollectionKey(databaseId string, collectionId string) string {
return generateKey(resourceid.ResourceTypeCollection, databaseId, collectionId, "")
}
func generateDocumentKey(databaseId string, collectionId string, documentId string) string {
return generateKey(resourceid.ResourceTypeDocument, databaseId, collectionId, documentId)
}
func generateTriggerKey(databaseId string, collectionId string, triggerId string) string {
return generateKey(resourceid.ResourceTypeTrigger, databaseId, collectionId, triggerId)
}
func generateStoredProcedureKey(databaseId string, collectionId string, storedProcedureId string) string {
return generateKey(resourceid.ResourceTypeStoredProcedure, databaseId, collectionId, storedProcedureId)
}
func generateUserDefinedFunctionKey(databaseId string, collectionId string, udfId string) string {
return generateKey(resourceid.ResourceTypeUserDefinedFunction, databaseId, collectionId, udfId)
}
func insertKey(txn *badger.Txn, key string, value interface{}) datastore.DataStoreStatus {
_, err := txn.Get([]byte(key))
if err == nil {
return datastore.Conflict
}
if err != badger.ErrKeyNotFound {
logger.ErrorLn("Error while checking if key exists:", err)
return datastore.Unknown
}
buf, err := msgpack.Marshal(value)
if err != nil {
logger.ErrorLn("Error while encoding value:", err)
return datastore.Unknown
}
err = txn.Set([]byte(key), buf)
if err != nil {
logger.ErrorLn("Error while setting key:", err)
return datastore.Unknown
}
err = txn.Commit()
if err != nil {
logger.ErrorLn("Error while committing transaction:", err)
return datastore.Unknown
}
return datastore.StatusOk
}
func getKey(txn *badger.Txn, key string, value interface{}) datastore.DataStoreStatus {
item, err := txn.Get([]byte(key))
if err != nil {
if err == badger.ErrKeyNotFound {
return datastore.StatusNotFound
}
logger.ErrorLn("Error while getting key:", err)
return datastore.Unknown
}
val, err := item.ValueCopy(nil)
if err != nil {
logger.ErrorLn("Error while copying value:", err)
return datastore.Unknown
}
if value == nil {
logger.ErrorLn("getKey called with nil value")
return datastore.Unknown
}
err = msgpack.Unmarshal(val, &value)
if err != nil {
logger.ErrorLn("Error while decoding value:", err)
return datastore.Unknown
}
return datastore.StatusOk
}
func keyExists(txn *badger.Txn, key string) (bool, error) {
_, err := txn.Get([]byte(key))
if err == nil {
return true, nil
}
if err == badger.ErrKeyNotFound {
return false, nil
}
return false, err
}
func listByPrefix[T any](db *badger.DB, prefix string) ([]T, datastore.DataStoreStatus) {
results := make([]T, 0)
err := db.View(func(txn *badger.Txn) error {
opts := badger.DefaultIteratorOptions
opts.Prefix = []byte(prefix)
it := txn.NewIterator(opts)
defer it.Close()
for it.Rewind(); it.Valid(); it.Next() {
item := it.Item()
var entry T
status := getKey(txn, string(item.Key()), &entry)
if status != datastore.StatusOk {
logger.ErrorLn("Failed to retrieve entry:", string(item.Key()))
continue
}
results = append(results, entry)
}
return nil
})
if err != nil {
logger.ErrorLn("Error while listing entries:", err)
return nil, datastore.Unknown
}
return results, datastore.StatusOk
}
func deleteKeysByPrefix(txn *badger.Txn, prefix string) error {
opts := badger.DefaultIteratorOptions
opts.Prefix = []byte(prefix)
it := txn.NewIterator(opts)
defer it.Close()
for it.Rewind(); it.Valid(); it.Next() {
key := it.Item().KeyCopy(nil)
if err := txn.Delete(key); err != nil {
logger.ErrorLn("Failed to delete key:", string(key), "Error:", err)
return err
}
}
return nil
}
func deleteKey(txn *badger.Txn, key string) error {
_, err := txn.Get([]byte(key))
if err == badger.ErrKeyNotFound {
return nil
}
if err != nil {
logger.ErrorLn("Error while checking if key exists:", err)
return err
}
err = txn.Delete([]byte(key))
if err != nil {
logger.ErrorLn("Error while deleting key:", err)
return err
}
return nil
}
@@ -0,0 +1,58 @@
package badgerdatastore
import (
"github.com/dgraph-io/badger/v4"
"github.com/pikami/cosmium/internal/datastore"
"github.com/pikami/cosmium/internal/logger"
"github.com/vmihailenco/msgpack/v5"
)
type BadgerDocumentIterator struct {
txn *badger.Txn
it *badger.Iterator
prefix string
}
func NewBadgerDocumentIterator(txn *badger.Txn, prefix string) *BadgerDocumentIterator {
opts := badger.DefaultIteratorOptions
opts.Prefix = []byte(prefix)
it := txn.NewIterator(opts)
it.Rewind()
return &BadgerDocumentIterator{
txn: txn,
it: it,
prefix: prefix,
}
}
func (i *BadgerDocumentIterator) Next() (datastore.Document, datastore.DataStoreStatus) {
if !i.it.Valid() {
i.it.Close()
return datastore.Document{}, datastore.IterEOF
}
item := i.it.Item()
val, err := item.ValueCopy(nil)
if err != nil {
logger.ErrorLn("Error while copying value:", err)
return datastore.Document{}, datastore.Unknown
}
current := &datastore.Document{}
err = msgpack.Unmarshal(val, &current)
if err != nil {
logger.ErrorLn("Error while decoding value:", err)
return datastore.Document{}, datastore.Unknown
}
i.it.Next()
return *current, datastore.StatusOk
}
func (i *BadgerDocumentIterator) Close() {
i.it.Close()
i.txn.Discard()
}
@@ -0,0 +1,129 @@
package badgerdatastore
import (
"fmt"
"time"
"github.com/google/uuid"
"github.com/pikami/cosmium/internal/datastore"
"github.com/pikami/cosmium/internal/logger"
"github.com/pikami/cosmium/internal/resourceid"
)
func (r *BadgerDataStore) GetAllDocuments(databaseId string, collectionId string) ([]datastore.Document, datastore.DataStoreStatus) {
txn := r.db.NewTransaction(false)
defer txn.Discard()
dbExists, err := keyExists(txn, generateDatabaseKey(databaseId))
if err != nil || !dbExists {
return nil, datastore.StatusNotFound
}
collExists, err := keyExists(txn, generateCollectionKey(databaseId, collectionId))
if err != nil || !collExists {
return nil, datastore.StatusNotFound
}
prefix := generateKey(resourceid.ResourceTypeDocument, databaseId, collectionId, "") + "/"
docs, status := listByPrefix[datastore.Document](r.db, prefix)
if status == datastore.StatusOk {
return docs, datastore.StatusOk
}
return nil, status
}
func (r *BadgerDataStore) GetDocumentIterator(databaseId string, collectionId string) (datastore.DocumentIterator, datastore.DataStoreStatus) {
txn := r.db.NewTransaction(false)
dbExists, err := keyExists(txn, generateDatabaseKey(databaseId))
if err != nil || !dbExists {
return nil, datastore.StatusNotFound
}
collExists, err := keyExists(txn, generateCollectionKey(databaseId, collectionId))
if err != nil || !collExists {
return nil, datastore.StatusNotFound
}
prefix := generateKey(resourceid.ResourceTypeDocument, databaseId, collectionId, "") + "/"
iter := NewBadgerDocumentIterator(txn, prefix)
return iter, datastore.StatusOk
}
func (r *BadgerDataStore) GetDocument(databaseId string, collectionId string, documentId string) (datastore.Document, datastore.DataStoreStatus) {
documentKey := generateDocumentKey(databaseId, collectionId, documentId)
txn := r.db.NewTransaction(false)
defer txn.Discard()
var document datastore.Document
status := getKey(txn, documentKey, &document)
return document, status
}
func (r *BadgerDataStore) DeleteDocument(databaseId string, collectionId string, documentId string) datastore.DataStoreStatus {
documentKey := generateDocumentKey(databaseId, collectionId, documentId)
txn := r.db.NewTransaction(true)
defer txn.Discard()
exists, err := keyExists(txn, documentKey)
if err != nil {
return datastore.Unknown
}
if !exists {
return datastore.StatusNotFound
}
err = txn.Delete([]byte(documentKey))
if err != nil {
logger.ErrorLn("Error while deleting document:", err)
return datastore.Unknown
}
err = txn.Commit()
if err != nil {
logger.ErrorLn("Error while committing transaction:", err)
return datastore.Unknown
}
return datastore.StatusOk
}
func (r *BadgerDataStore) CreateDocument(databaseId string, collectionId string, document map[string]interface{}) (datastore.Document, datastore.DataStoreStatus) {
txn := r.db.NewTransaction(true)
defer txn.Discard()
var database datastore.Database
status := getKey(txn, generateDatabaseKey(databaseId), &database)
if status != datastore.StatusOk {
return datastore.Document{}, status
}
var collection datastore.Collection
status = getKey(txn, generateCollectionKey(databaseId, collectionId), &collection)
if status != datastore.StatusOk {
return datastore.Document{}, status
}
var ok bool
var documentId string
if documentId, ok = document["id"].(string); !ok || documentId == "" {
documentId = fmt.Sprint(uuid.New())
document["id"] = documentId
}
document["_ts"] = time.Now().Unix()
document["_rid"] = resourceid.NewCombined(collection.ResourceID, resourceid.New(resourceid.ResourceTypeDocument))
document["_etag"] = fmt.Sprintf("\"%s\"", uuid.New())
document["_self"] = fmt.Sprintf("dbs/%s/colls/%s/docs/%s/", database.ResourceID, collection.ResourceID, document["_rid"])
status = insertKey(txn, generateDocumentKey(databaseId, collectionId, documentId), document)
if status != datastore.StatusOk {
return datastore.Document{}, status
}
return document, datastore.StatusOk
}
@@ -0,0 +1,53 @@
package badgerdatastore
import (
"fmt"
"github.com/google/uuid"
"github.com/pikami/cosmium/internal/datastore"
"github.com/pikami/cosmium/internal/resourceid"
)
// I have no idea what this is tbh
func (r *BadgerDataStore) GetPartitionKeyRanges(databaseId string, collectionId string) ([]datastore.PartitionKeyRange, datastore.DataStoreStatus) {
databaseRid := databaseId
collectionRid := collectionId
var timestamp int64 = 0
txn := r.db.NewTransaction(false)
defer txn.Discard()
var database datastore.Database
status := getKey(txn, generateDatabaseKey(databaseId), &database)
if status != datastore.StatusOk {
databaseRid = database.ResourceID
}
var collection datastore.Collection
status = getKey(txn, generateCollectionKey(databaseId, collectionId), &collection)
if status != datastore.StatusOk {
collectionRid = collection.ResourceID
timestamp = collection.TimeStamp
}
pkrResourceId := resourceid.NewCombined(collectionRid, resourceid.New(resourceid.ResourceTypePartitionKeyRange))
pkrSelf := fmt.Sprintf("dbs/%s/colls/%s/pkranges/%s/", databaseRid, collectionRid, pkrResourceId)
etag := fmt.Sprintf("\"%s\"", uuid.New())
return []datastore.PartitionKeyRange{
{
ResourceID: pkrResourceId,
ID: "0",
Etag: etag,
MinInclusive: "",
MaxExclusive: "FF",
RidPrefix: 0,
Self: pkrSelf,
ThroughputFraction: 1,
Status: "online",
Parents: []interface{}{},
TimeStamp: timestamp,
Lsn: 17,
},
}, datastore.StatusOk
}
@@ -0,0 +1,108 @@
package badgerdatastore
import (
"fmt"
"time"
"github.com/google/uuid"
"github.com/pikami/cosmium/internal/datastore"
"github.com/pikami/cosmium/internal/logger"
"github.com/pikami/cosmium/internal/resourceid"
)
func (r *BadgerDataStore) GetAllStoredProcedures(databaseId string, collectionId string) ([]datastore.StoredProcedure, datastore.DataStoreStatus) {
txn := r.db.NewTransaction(false)
defer txn.Discard()
dbExists, err := keyExists(txn, generateDatabaseKey(databaseId))
if err != nil || !dbExists {
return nil, datastore.StatusNotFound
}
collExists, err := keyExists(txn, generateCollectionKey(databaseId, collectionId))
if err != nil || !collExists {
return nil, datastore.StatusNotFound
}
prefix := generateKey(resourceid.ResourceTypeStoredProcedure, databaseId, collectionId, "") + "/"
storedProcedures, status := listByPrefix[datastore.StoredProcedure](r.db, prefix)
if status == datastore.StatusOk {
return storedProcedures, datastore.StatusOk
}
return nil, status
}
func (r *BadgerDataStore) GetStoredProcedure(databaseId string, collectionId string, storedProcedureId string) (datastore.StoredProcedure, datastore.DataStoreStatus) {
storedProcedureKey := generateStoredProcedureKey(databaseId, collectionId, storedProcedureId)
txn := r.db.NewTransaction(false)
defer txn.Discard()
var storedProcedure datastore.StoredProcedure
status := getKey(txn, storedProcedureKey, &storedProcedure)
return storedProcedure, status
}
func (r *BadgerDataStore) DeleteStoredProcedure(databaseId string, collectionId string, storedProcedureId string) datastore.DataStoreStatus {
storedProcedureKey := generateStoredProcedureKey(databaseId, collectionId, storedProcedureId)
txn := r.db.NewTransaction(true)
defer txn.Discard()
exists, err := keyExists(txn, storedProcedureKey)
if err != nil {
return datastore.Unknown
}
if !exists {
return datastore.StatusNotFound
}
err = txn.Delete([]byte(storedProcedureKey))
if err != nil {
logger.ErrorLn("Error while deleting stored procedure:", err)
return datastore.Unknown
}
err = txn.Commit()
if err != nil {
logger.ErrorLn("Error while committing transaction:", err)
return datastore.Unknown
}
return datastore.StatusOk
}
func (r *BadgerDataStore) CreateStoredProcedure(databaseId string, collectionId string, storedProcedure datastore.StoredProcedure) (datastore.StoredProcedure, datastore.DataStoreStatus) {
txn := r.db.NewTransaction(true)
defer txn.Discard()
if storedProcedure.ID == "" {
return datastore.StoredProcedure{}, datastore.BadRequest
}
var database datastore.Database
status := getKey(txn, generateDatabaseKey(databaseId), &database)
if status != datastore.StatusOk {
return datastore.StoredProcedure{}, status
}
var collection datastore.Collection
status = getKey(txn, generateCollectionKey(databaseId, collectionId), &collection)
if status != datastore.StatusOk {
return datastore.StoredProcedure{}, status
}
storedProcedure.TimeStamp = time.Now().Unix()
storedProcedure.ResourceID = resourceid.NewCombined(collection.ResourceID, resourceid.New(resourceid.ResourceTypeStoredProcedure))
storedProcedure.ETag = fmt.Sprintf("\"%s\"", uuid.New())
storedProcedure.Self = fmt.Sprintf("dbs/%s/colls/%s/sprocs/%s/", database.ResourceID, collection.ResourceID, storedProcedure.ResourceID)
status = insertKey(txn, generateStoredProcedureKey(databaseId, collectionId, storedProcedure.ID), storedProcedure)
if status != datastore.StatusOk {
return datastore.StoredProcedure{}, status
}
return storedProcedure, datastore.StatusOk
}
@@ -0,0 +1,108 @@
package badgerdatastore
import (
"fmt"
"time"
"github.com/google/uuid"
"github.com/pikami/cosmium/internal/datastore"
"github.com/pikami/cosmium/internal/logger"
"github.com/pikami/cosmium/internal/resourceid"
)
func (r *BadgerDataStore) GetAllTriggers(databaseId string, collectionId string) ([]datastore.Trigger, datastore.DataStoreStatus) {
txn := r.db.NewTransaction(false)
defer txn.Discard()
dbExists, err := keyExists(txn, generateDatabaseKey(databaseId))
if err != nil || !dbExists {
return nil, datastore.StatusNotFound
}
collExists, err := keyExists(txn, generateCollectionKey(databaseId, collectionId))
if err != nil || !collExists {
return nil, datastore.StatusNotFound
}
prefix := generateKey(resourceid.ResourceTypeTrigger, databaseId, collectionId, "") + "/"
triggers, status := listByPrefix[datastore.Trigger](r.db, prefix)
if status == datastore.StatusOk {
return triggers, datastore.StatusOk
}
return nil, status
}
func (r *BadgerDataStore) GetTrigger(databaseId string, collectionId string, triggerId string) (datastore.Trigger, datastore.DataStoreStatus) {
triggerKey := generateTriggerKey(databaseId, collectionId, triggerId)
txn := r.db.NewTransaction(false)
defer txn.Discard()
var trigger datastore.Trigger
status := getKey(txn, triggerKey, &trigger)
return trigger, status
}
func (r *BadgerDataStore) DeleteTrigger(databaseId string, collectionId string, triggerId string) datastore.DataStoreStatus {
triggerKey := generateTriggerKey(databaseId, collectionId, triggerId)
txn := r.db.NewTransaction(true)
defer txn.Discard()
exists, err := keyExists(txn, triggerKey)
if err != nil {
return datastore.Unknown
}
if !exists {
return datastore.StatusNotFound
}
err = txn.Delete([]byte(triggerKey))
if err != nil {
logger.ErrorLn("Error while deleting trigger:", err)
return datastore.Unknown
}
err = txn.Commit()
if err != nil {
logger.ErrorLn("Error while committing transaction:", err)
return datastore.Unknown
}
return datastore.StatusOk
}
func (r *BadgerDataStore) CreateTrigger(databaseId string, collectionId string, trigger datastore.Trigger) (datastore.Trigger, datastore.DataStoreStatus) {
txn := r.db.NewTransaction(true)
defer txn.Discard()
if trigger.ID == "" {
return datastore.Trigger{}, datastore.BadRequest
}
var database datastore.Database
status := getKey(txn, generateDatabaseKey(databaseId), &database)
if status != datastore.StatusOk {
return datastore.Trigger{}, status
}
var collection datastore.Collection
status = getKey(txn, generateCollectionKey(databaseId, collectionId), &collection)
if status != datastore.StatusOk {
return datastore.Trigger{}, status
}
trigger.TimeStamp = time.Now().Unix()
trigger.ResourceID = resourceid.NewCombined(collection.ResourceID, resourceid.New(resourceid.ResourceTypeTrigger))
trigger.ETag = fmt.Sprintf("\"%s\"", uuid.New())
trigger.Self = fmt.Sprintf("dbs/%s/colls/%s/triggers/%s/", database.ResourceID, collection.ResourceID, trigger.ResourceID)
status = insertKey(txn, generateTriggerKey(databaseId, collectionId, trigger.ID), trigger)
if status != datastore.StatusOk {
return datastore.Trigger{}, status
}
return trigger, datastore.StatusOk
}
@@ -0,0 +1,108 @@
package badgerdatastore
import (
"fmt"
"time"
"github.com/google/uuid"
"github.com/pikami/cosmium/internal/datastore"
"github.com/pikami/cosmium/internal/logger"
"github.com/pikami/cosmium/internal/resourceid"
)
func (r *BadgerDataStore) GetAllUserDefinedFunctions(databaseId string, collectionId string) ([]datastore.UserDefinedFunction, datastore.DataStoreStatus) {
txn := r.db.NewTransaction(false)
defer txn.Discard()
dbExists, err := keyExists(txn, generateDatabaseKey(databaseId))
if err != nil || !dbExists {
return nil, datastore.StatusNotFound
}
collExists, err := keyExists(txn, generateCollectionKey(databaseId, collectionId))
if err != nil || !collExists {
return nil, datastore.StatusNotFound
}
prefix := generateKey(resourceid.ResourceTypeUserDefinedFunction, databaseId, collectionId, "") + "/"
udfs, status := listByPrefix[datastore.UserDefinedFunction](r.db, prefix)
if status == datastore.StatusOk {
return udfs, datastore.StatusOk
}
return nil, status
}
func (r *BadgerDataStore) GetUserDefinedFunction(databaseId string, collectionId string, udfId string) (datastore.UserDefinedFunction, datastore.DataStoreStatus) {
udfKey := generateUserDefinedFunctionKey(databaseId, collectionId, udfId)
txn := r.db.NewTransaction(false)
defer txn.Discard()
var udf datastore.UserDefinedFunction
status := getKey(txn, udfKey, &udf)
return udf, status
}
func (r *BadgerDataStore) DeleteUserDefinedFunction(databaseId string, collectionId string, udfId string) datastore.DataStoreStatus {
udfKey := generateUserDefinedFunctionKey(databaseId, collectionId, udfId)
txn := r.db.NewTransaction(true)
defer txn.Discard()
exists, err := keyExists(txn, udfKey)
if err != nil {
return datastore.Unknown
}
if !exists {
return datastore.StatusNotFound
}
err = txn.Delete([]byte(udfKey))
if err != nil {
logger.ErrorLn("Error while deleting user defined function:", err)
return datastore.Unknown
}
err = txn.Commit()
if err != nil {
logger.ErrorLn("Error while committing transaction:", err)
return datastore.Unknown
}
return datastore.StatusOk
}
func (r *BadgerDataStore) CreateUserDefinedFunction(databaseId string, collectionId string, udf datastore.UserDefinedFunction) (datastore.UserDefinedFunction, datastore.DataStoreStatus) {
txn := r.db.NewTransaction(true)
defer txn.Discard()
if udf.ID == "" {
return datastore.UserDefinedFunction{}, datastore.BadRequest
}
var database datastore.Database
status := getKey(txn, generateDatabaseKey(databaseId), &database)
if status != datastore.StatusOk {
return datastore.UserDefinedFunction{}, status
}
var collection datastore.Collection
status = getKey(txn, generateCollectionKey(databaseId, collectionId), &collection)
if status != datastore.StatusOk {
return datastore.UserDefinedFunction{}, status
}
udf.TimeStamp = time.Now().Unix()
udf.ResourceID = resourceid.NewCombined(collection.ResourceID, resourceid.New(resourceid.ResourceTypeUserDefinedFunction))
udf.ETag = fmt.Sprintf("\"%s\"", uuid.New())
udf.Self = fmt.Sprintf("dbs/%s/colls/%s/udfs/%s/", database.ResourceID, collection.ResourceID, udf.ResourceID)
status = insertKey(txn, generateUserDefinedFunctionKey(databaseId, collectionId, udf.ID), udf)
if status != datastore.StatusOk {
return datastore.UserDefinedFunction{}, status
}
return udf, datastore.StatusOk
}
+1
View File
@@ -40,4 +40,5 @@ type DataStore interface {
type DocumentIterator interface { type DocumentIterator interface {
Next() (Document, DataStoreStatus) Next() (Document, DataStoreStatus)
Close()
} }
+21
View File
@@ -0,0 +1,21 @@
package datastore
type InitialDataModel struct {
// Map databaseId -> Database
Databases map[string]Database `json:"databases"`
// Map databaseId -> collectionId -> Collection
Collections map[string]map[string]Collection `json:"collections"`
// Map databaseId -> collectionId -> documentId -> Documents
Documents map[string]map[string]map[string]Document `json:"documents"`
// Map databaseId -> collectionId -> triggerId -> Trigger
Triggers map[string]map[string]map[string]Trigger `json:"triggers"`
// Map databaseId -> collectionId -> spId -> StoredProcedure
StoredProcedures map[string]map[string]map[string]StoredProcedure `json:"sprocs"`
// Map databaseId -> collectionId -> udfId -> UserDefinedFunction
UserDefinedFunctions map[string]map[string]map[string]UserDefinedFunction `json:"udfs"`
}
@@ -1,4 +1,4 @@
package mapdatastore package jsondatastore
import "github.com/pikami/cosmium/internal/datastore" import "github.com/pikami/cosmium/internal/datastore"
@@ -15,3 +15,7 @@ func (i *ArrayDocumentIterator) Next() (datastore.Document, datastore.DataStoreS
return i.documents[i.index], datastore.StatusOk return i.documents[i.index], datastore.StatusOk
} }
func (i *ArrayDocumentIterator) Close() {
i.documents = []datastore.Document{}
}
@@ -1,4 +1,4 @@
package mapdatastore package jsondatastore
import ( import (
"fmt" "fmt"
@@ -11,7 +11,7 @@ import (
"golang.org/x/exp/maps" "golang.org/x/exp/maps"
) )
func (r *MapDataStore) GetAllCollections(databaseId string) ([]datastore.Collection, datastore.DataStoreStatus) { func (r *JsonDataStore) GetAllCollections(databaseId string) ([]datastore.Collection, datastore.DataStoreStatus) {
r.storeState.RLock() r.storeState.RLock()
defer r.storeState.RUnlock() defer r.storeState.RUnlock()
@@ -22,7 +22,7 @@ func (r *MapDataStore) GetAllCollections(databaseId string) ([]datastore.Collect
return maps.Values(r.storeState.Collections[databaseId]), datastore.StatusOk return maps.Values(r.storeState.Collections[databaseId]), datastore.StatusOk
} }
func (r *MapDataStore) GetCollection(databaseId string, collectionId string) (datastore.Collection, datastore.DataStoreStatus) { func (r *JsonDataStore) GetCollection(databaseId string, collectionId string) (datastore.Collection, datastore.DataStoreStatus) {
r.storeState.RLock() r.storeState.RLock()
defer r.storeState.RUnlock() defer r.storeState.RUnlock()
@@ -37,7 +37,7 @@ func (r *MapDataStore) GetCollection(databaseId string, collectionId string) (da
return r.storeState.Collections[databaseId][collectionId], datastore.StatusOk return r.storeState.Collections[databaseId][collectionId], datastore.StatusOk
} }
func (r *MapDataStore) DeleteCollection(databaseId string, collectionId string) datastore.DataStoreStatus { func (r *JsonDataStore) DeleteCollection(databaseId string, collectionId string) datastore.DataStoreStatus {
r.storeState.Lock() r.storeState.Lock()
defer r.storeState.Unlock() defer r.storeState.Unlock()
@@ -58,7 +58,7 @@ func (r *MapDataStore) DeleteCollection(databaseId string, collectionId string)
return datastore.StatusOk return datastore.StatusOk
} }
func (r *MapDataStore) CreateCollection(databaseId string, newCollection datastore.Collection) (datastore.Collection, datastore.DataStoreStatus) { func (r *JsonDataStore) CreateCollection(databaseId string, newCollection datastore.Collection) (datastore.Collection, datastore.DataStoreStatus) {
r.storeState.Lock() r.storeState.Lock()
defer r.storeState.Unlock() defer r.storeState.Unlock()
@@ -1,4 +1,4 @@
package mapdatastore package jsondatastore
import ( import (
"fmt" "fmt"
@@ -10,14 +10,14 @@ import (
"golang.org/x/exp/maps" "golang.org/x/exp/maps"
) )
func (r *MapDataStore) GetAllDatabases() ([]datastore.Database, datastore.DataStoreStatus) { func (r *JsonDataStore) GetAllDatabases() ([]datastore.Database, datastore.DataStoreStatus) {
r.storeState.RLock() r.storeState.RLock()
defer r.storeState.RUnlock() defer r.storeState.RUnlock()
return maps.Values(r.storeState.Databases), datastore.StatusOk return maps.Values(r.storeState.Databases), datastore.StatusOk
} }
func (r *MapDataStore) GetDatabase(id string) (datastore.Database, datastore.DataStoreStatus) { func (r *JsonDataStore) GetDatabase(id string) (datastore.Database, datastore.DataStoreStatus) {
r.storeState.RLock() r.storeState.RLock()
defer r.storeState.RUnlock() defer r.storeState.RUnlock()
@@ -28,7 +28,7 @@ func (r *MapDataStore) GetDatabase(id string) (datastore.Database, datastore.Dat
return datastore.Database{}, datastore.StatusNotFound return datastore.Database{}, datastore.StatusNotFound
} }
func (r *MapDataStore) DeleteDatabase(id string) datastore.DataStoreStatus { func (r *JsonDataStore) DeleteDatabase(id string) datastore.DataStoreStatus {
r.storeState.Lock() r.storeState.Lock()
defer r.storeState.Unlock() defer r.storeState.Unlock()
@@ -46,7 +46,7 @@ func (r *MapDataStore) DeleteDatabase(id string) datastore.DataStoreStatus {
return datastore.StatusOk return datastore.StatusOk
} }
func (r *MapDataStore) CreateDatabase(newDatabase datastore.Database) (datastore.Database, datastore.DataStoreStatus) { func (r *JsonDataStore) CreateDatabase(newDatabase datastore.Database) (datastore.Database, datastore.DataStoreStatus) {
r.storeState.Lock() r.storeState.Lock()
defer r.storeState.Unlock() defer r.storeState.Unlock()
@@ -1,4 +1,4 @@
package mapdatastore package jsondatastore
import ( import (
"fmt" "fmt"
@@ -10,7 +10,7 @@ import (
"golang.org/x/exp/maps" "golang.org/x/exp/maps"
) )
func (r *MapDataStore) GetAllDocuments(databaseId string, collectionId string) ([]datastore.Document, datastore.DataStoreStatus) { func (r *JsonDataStore) GetAllDocuments(databaseId string, collectionId string) ([]datastore.Document, datastore.DataStoreStatus) {
r.storeState.RLock() r.storeState.RLock()
defer r.storeState.RUnlock() defer r.storeState.RUnlock()
@@ -25,7 +25,7 @@ func (r *MapDataStore) GetAllDocuments(databaseId string, collectionId string) (
return maps.Values(r.storeState.Documents[databaseId][collectionId]), datastore.StatusOk return maps.Values(r.storeState.Documents[databaseId][collectionId]), datastore.StatusOk
} }
func (r *MapDataStore) GetDocument(databaseId string, collectionId string, documentId string) (datastore.Document, datastore.DataStoreStatus) { func (r *JsonDataStore) GetDocument(databaseId string, collectionId string, documentId string) (datastore.Document, datastore.DataStoreStatus) {
r.storeState.RLock() r.storeState.RLock()
defer r.storeState.RUnlock() defer r.storeState.RUnlock()
@@ -44,7 +44,7 @@ func (r *MapDataStore) GetDocument(databaseId string, collectionId string, docum
return r.storeState.Documents[databaseId][collectionId][documentId], datastore.StatusOk return r.storeState.Documents[databaseId][collectionId][documentId], datastore.StatusOk
} }
func (r *MapDataStore) DeleteDocument(databaseId string, collectionId string, documentId string) datastore.DataStoreStatus { func (r *JsonDataStore) DeleteDocument(databaseId string, collectionId string, documentId string) datastore.DataStoreStatus {
r.storeState.Lock() r.storeState.Lock()
defer r.storeState.Unlock() defer r.storeState.Unlock()
@@ -65,7 +65,7 @@ func (r *MapDataStore) DeleteDocument(databaseId string, collectionId string, do
return datastore.StatusOk return datastore.StatusOk
} }
func (r *MapDataStore) CreateDocument(databaseId string, collectionId string, document map[string]interface{}) (datastore.Document, datastore.DataStoreStatus) { func (r *JsonDataStore) CreateDocument(databaseId string, collectionId string, document map[string]interface{}) (datastore.Document, datastore.DataStoreStatus) {
r.storeState.Lock() r.storeState.Lock()
defer r.storeState.Unlock() defer r.storeState.Unlock()
@@ -100,7 +100,7 @@ func (r *MapDataStore) CreateDocument(databaseId string, collectionId string, do
return document, datastore.StatusOk return document, datastore.StatusOk
} }
func (r *MapDataStore) GetDocumentIterator(databaseId string, collectionId string) (datastore.DocumentIterator, datastore.DataStoreStatus) { func (r *JsonDataStore) GetDocumentIterator(databaseId string, collectionId string) (datastore.DocumentIterator, datastore.DataStoreStatus) {
documents, status := r.GetAllDocuments(databaseId, collectionId) documents, status := r.GetAllDocuments(databaseId, collectionId)
if status != datastore.StatusOk { if status != datastore.StatusOk {
return nil, status return nil, status
@@ -1,21 +1,21 @@
package mapdatastore package jsondatastore
import "github.com/pikami/cosmium/internal/datastore" import "github.com/pikami/cosmium/internal/datastore"
type MapDataStore struct { type JsonDataStore struct {
storeState State storeState State
initialDataFilePath string initialDataFilePath string
persistDataFilePath string persistDataFilePath string
} }
type MapDataStoreOptions struct { type JsonDataStoreOptions struct {
InitialDataFilePath string InitialDataFilePath string
PersistDataFilePath string PersistDataFilePath string
} }
func NewMapDataStore(options MapDataStoreOptions) *MapDataStore { func NewJsonDataStore(options JsonDataStoreOptions) *JsonDataStore {
dataStore := &MapDataStore{ dataStore := &JsonDataStore{
storeState: State{ storeState: State{
Databases: make(map[string]datastore.Database), Databases: make(map[string]datastore.Database),
Collections: make(map[string]map[string]datastore.Collection), Collections: make(map[string]map[string]datastore.Collection),
@@ -1,4 +1,4 @@
package mapdatastore package jsondatastore
import ( import (
"fmt" "fmt"
@@ -9,7 +9,7 @@ import (
) )
// I have no idea what this is tbh // I have no idea what this is tbh
func (r *MapDataStore) GetPartitionKeyRanges(databaseId string, collectionId string) ([]datastore.PartitionKeyRange, datastore.DataStoreStatus) { func (r *JsonDataStore) GetPartitionKeyRanges(databaseId string, collectionId string) ([]datastore.PartitionKeyRange, datastore.DataStoreStatus) {
r.storeState.RLock() r.storeState.RLock()
defer r.storeState.RUnlock() defer r.storeState.RUnlock()
@@ -1,4 +1,4 @@
package mapdatastore package jsondatastore
import ( import (
"encoding/json" "encoding/json"
@@ -33,7 +33,7 @@ type State struct {
UserDefinedFunctions map[string]map[string]map[string]datastore.UserDefinedFunction `json:"udfs"` UserDefinedFunctions map[string]map[string]map[string]datastore.UserDefinedFunction `json:"udfs"`
} }
func (r *MapDataStore) InitializeDataStore() { func (r *JsonDataStore) InitializeDataStore() {
if r.initialDataFilePath != "" { if r.initialDataFilePath != "" {
r.LoadStateFS(r.initialDataFilePath) r.LoadStateFS(r.initialDataFilePath)
return return
@@ -55,7 +55,7 @@ func (r *MapDataStore) InitializeDataStore() {
} }
} }
func (r *MapDataStore) LoadStateFS(filePath string) { func (r *JsonDataStore) LoadStateFS(filePath string) {
data, err := os.ReadFile(filePath) data, err := os.ReadFile(filePath)
if err != nil { if err != nil {
log.Fatalf("Error reading state JSON file: %v", err) log.Fatalf("Error reading state JSON file: %v", err)
@@ -68,7 +68,7 @@ func (r *MapDataStore) LoadStateFS(filePath string) {
} }
} }
func (r *MapDataStore) LoadStateJSON(jsonData string) error { func (r *JsonDataStore) LoadStateJSON(jsonData string) error {
r.storeState.Lock() r.storeState.Lock()
defer r.storeState.Unlock() defer r.storeState.Unlock()
@@ -94,7 +94,7 @@ func (r *MapDataStore) LoadStateJSON(jsonData string) error {
return nil return nil
} }
func (r *MapDataStore) SaveStateFS(filePath string) { func (r *JsonDataStore) SaveStateFS(filePath string) {
r.storeState.RLock() r.storeState.RLock()
defer r.storeState.RUnlock() defer r.storeState.RUnlock()
@@ -115,7 +115,7 @@ func (r *MapDataStore) SaveStateFS(filePath string) {
logger.Infof("User defined functions: %d\n", getLength(r.storeState.UserDefinedFunctions)) logger.Infof("User defined functions: %d\n", getLength(r.storeState.UserDefinedFunctions))
} }
func (r *MapDataStore) DumpToJson() (string, error) { func (r *JsonDataStore) DumpToJson() (string, error) {
r.storeState.RLock() r.storeState.RLock()
defer r.storeState.RUnlock() defer r.storeState.RUnlock()
@@ -129,7 +129,7 @@ func (r *MapDataStore) DumpToJson() (string, error) {
} }
func (r *MapDataStore) Close() { func (r *JsonDataStore) Close() {
if r.persistDataFilePath != "" { if r.persistDataFilePath != "" {
r.SaveStateFS(r.persistDataFilePath) r.SaveStateFS(r.persistDataFilePath)
} }
@@ -163,7 +163,7 @@ func getLength(v interface{}) int {
return count return count
} }
func (r *MapDataStore) ensureStoreStateNoNullReferences() { func (r *JsonDataStore) ensureStoreStateNoNullReferences() {
if r.storeState.Databases == nil { if r.storeState.Databases == nil {
r.storeState.Databases = make(map[string]datastore.Database) r.storeState.Databases = make(map[string]datastore.Database)
} }
@@ -1,4 +1,4 @@
package mapdatastore package jsondatastore
import ( import (
"fmt" "fmt"
@@ -10,14 +10,14 @@ import (
"golang.org/x/exp/maps" "golang.org/x/exp/maps"
) )
func (r *MapDataStore) GetAllStoredProcedures(databaseId string, collectionId string) ([]datastore.StoredProcedure, datastore.DataStoreStatus) { func (r *JsonDataStore) GetAllStoredProcedures(databaseId string, collectionId string) ([]datastore.StoredProcedure, datastore.DataStoreStatus) {
r.storeState.RLock() r.storeState.RLock()
defer r.storeState.RUnlock() defer r.storeState.RUnlock()
return maps.Values(r.storeState.StoredProcedures[databaseId][collectionId]), datastore.StatusOk return maps.Values(r.storeState.StoredProcedures[databaseId][collectionId]), datastore.StatusOk
} }
func (r *MapDataStore) GetStoredProcedure(databaseId string, collectionId string, spId string) (datastore.StoredProcedure, datastore.DataStoreStatus) { func (r *JsonDataStore) GetStoredProcedure(databaseId string, collectionId string, spId string) (datastore.StoredProcedure, datastore.DataStoreStatus) {
r.storeState.RLock() r.storeState.RLock()
defer r.storeState.RUnlock() defer r.storeState.RUnlock()
@@ -36,7 +36,7 @@ func (r *MapDataStore) GetStoredProcedure(databaseId string, collectionId string
return datastore.StoredProcedure{}, datastore.StatusNotFound return datastore.StoredProcedure{}, datastore.StatusNotFound
} }
func (r *MapDataStore) DeleteStoredProcedure(databaseId string, collectionId string, spId string) datastore.DataStoreStatus { func (r *JsonDataStore) DeleteStoredProcedure(databaseId string, collectionId string, spId string) datastore.DataStoreStatus {
r.storeState.Lock() r.storeState.Lock()
defer r.storeState.Unlock() defer r.storeState.Unlock()
@@ -57,7 +57,7 @@ func (r *MapDataStore) DeleteStoredProcedure(databaseId string, collectionId str
return datastore.StatusOk return datastore.StatusOk
} }
func (r *MapDataStore) CreateStoredProcedure(databaseId string, collectionId string, sp datastore.StoredProcedure) (datastore.StoredProcedure, datastore.DataStoreStatus) { func (r *JsonDataStore) CreateStoredProcedure(databaseId string, collectionId string, sp datastore.StoredProcedure) (datastore.StoredProcedure, datastore.DataStoreStatus) {
r.storeState.Lock() r.storeState.Lock()
defer r.storeState.Unlock() defer r.storeState.Unlock()
@@ -1,4 +1,4 @@
package mapdatastore package jsondatastore
import ( import (
"fmt" "fmt"
@@ -10,14 +10,14 @@ import (
"golang.org/x/exp/maps" "golang.org/x/exp/maps"
) )
func (r *MapDataStore) GetAllTriggers(databaseId string, collectionId string) ([]datastore.Trigger, datastore.DataStoreStatus) { func (r *JsonDataStore) GetAllTriggers(databaseId string, collectionId string) ([]datastore.Trigger, datastore.DataStoreStatus) {
r.storeState.RLock() r.storeState.RLock()
defer r.storeState.RUnlock() defer r.storeState.RUnlock()
return maps.Values(r.storeState.Triggers[databaseId][collectionId]), datastore.StatusOk return maps.Values(r.storeState.Triggers[databaseId][collectionId]), datastore.StatusOk
} }
func (r *MapDataStore) GetTrigger(databaseId string, collectionId string, triggerId string) (datastore.Trigger, datastore.DataStoreStatus) { func (r *JsonDataStore) GetTrigger(databaseId string, collectionId string, triggerId string) (datastore.Trigger, datastore.DataStoreStatus) {
r.storeState.RLock() r.storeState.RLock()
defer r.storeState.RUnlock() defer r.storeState.RUnlock()
@@ -36,7 +36,7 @@ func (r *MapDataStore) GetTrigger(databaseId string, collectionId string, trigge
return datastore.Trigger{}, datastore.StatusNotFound return datastore.Trigger{}, datastore.StatusNotFound
} }
func (r *MapDataStore) DeleteTrigger(databaseId string, collectionId string, triggerId string) datastore.DataStoreStatus { func (r *JsonDataStore) DeleteTrigger(databaseId string, collectionId string, triggerId string) datastore.DataStoreStatus {
r.storeState.Lock() r.storeState.Lock()
defer r.storeState.Unlock() defer r.storeState.Unlock()
@@ -57,7 +57,7 @@ func (r *MapDataStore) DeleteTrigger(databaseId string, collectionId string, tri
return datastore.StatusOk return datastore.StatusOk
} }
func (r *MapDataStore) CreateTrigger(databaseId string, collectionId string, trigger datastore.Trigger) (datastore.Trigger, datastore.DataStoreStatus) { func (r *JsonDataStore) CreateTrigger(databaseId string, collectionId string, trigger datastore.Trigger) (datastore.Trigger, datastore.DataStoreStatus) {
r.storeState.Lock() r.storeState.Lock()
defer r.storeState.Unlock() defer r.storeState.Unlock()
@@ -1,4 +1,4 @@
package mapdatastore package jsondatastore
import ( import (
"fmt" "fmt"
@@ -10,14 +10,14 @@ import (
"golang.org/x/exp/maps" "golang.org/x/exp/maps"
) )
func (r *MapDataStore) GetAllUserDefinedFunctions(databaseId string, collectionId string) ([]datastore.UserDefinedFunction, datastore.DataStoreStatus) { func (r *JsonDataStore) GetAllUserDefinedFunctions(databaseId string, collectionId string) ([]datastore.UserDefinedFunction, datastore.DataStoreStatus) {
r.storeState.RLock() r.storeState.RLock()
defer r.storeState.RUnlock() defer r.storeState.RUnlock()
return maps.Values(r.storeState.UserDefinedFunctions[databaseId][collectionId]), datastore.StatusOk return maps.Values(r.storeState.UserDefinedFunctions[databaseId][collectionId]), datastore.StatusOk
} }
func (r *MapDataStore) GetUserDefinedFunction(databaseId string, collectionId string, udfId string) (datastore.UserDefinedFunction, datastore.DataStoreStatus) { func (r *JsonDataStore) GetUserDefinedFunction(databaseId string, collectionId string, udfId string) (datastore.UserDefinedFunction, datastore.DataStoreStatus) {
r.storeState.RLock() r.storeState.RLock()
defer r.storeState.RUnlock() defer r.storeState.RUnlock()
@@ -36,7 +36,7 @@ func (r *MapDataStore) GetUserDefinedFunction(databaseId string, collectionId st
return datastore.UserDefinedFunction{}, datastore.StatusNotFound return datastore.UserDefinedFunction{}, datastore.StatusNotFound
} }
func (r *MapDataStore) DeleteUserDefinedFunction(databaseId string, collectionId string, udfId string) datastore.DataStoreStatus { func (r *JsonDataStore) DeleteUserDefinedFunction(databaseId string, collectionId string, udfId string) datastore.DataStoreStatus {
r.storeState.Lock() r.storeState.Lock()
defer r.storeState.Unlock() defer r.storeState.Unlock()
@@ -57,7 +57,7 @@ func (r *MapDataStore) DeleteUserDefinedFunction(databaseId string, collectionId
return datastore.StatusOk return datastore.StatusOk
} }
func (r *MapDataStore) CreateUserDefinedFunction(databaseId string, collectionId string, udf datastore.UserDefinedFunction) (datastore.UserDefinedFunction, datastore.DataStoreStatus) { func (r *JsonDataStore) CreateUserDefinedFunction(databaseId string, collectionId string, udf datastore.UserDefinedFunction) (datastore.UserDefinedFunction, datastore.DataStoreStatus) {
r.storeState.Lock() r.storeState.Lock()
defer r.storeState.Unlock() defer r.storeState.Unlock()
+6 -5
View File
@@ -11,11 +11,12 @@ type Database struct {
type DataStoreStatus int type DataStoreStatus int
const ( const (
StatusOk = 1 StatusOk DataStoreStatus = 1
StatusNotFound = 2 StatusNotFound DataStoreStatus = 2
Conflict = 3 Conflict DataStoreStatus = 3
BadRequest = 4 BadRequest DataStoreStatus = 4
IterEOF = 5 IterEOF DataStoreStatus = 5
Unknown DataStoreStatus = 6
) )
type TriggerOperation string type TriggerOperation string
+10
View File
@@ -34,6 +34,8 @@ const (
SelectItemTypeConstant SelectItemTypeConstant
SelectItemTypeFunctionCall SelectItemTypeFunctionCall
SelectItemTypeSubQuery SelectItemTypeSubQuery
SelectItemTypeExpression
SelectItemTypeBinaryExpression
) )
type SelectItem struct { type SelectItem struct {
@@ -64,6 +66,12 @@ type ComparisonExpression struct {
Operation string Operation string
} }
type BinaryExpression struct {
Left interface{}
Right interface{}
Operation string
}
type ConstantType int type ConstantType int
const ( const (
@@ -134,6 +142,8 @@ const (
FunctionCallSetIntersect FunctionCallType = "SetIntersect" FunctionCallSetIntersect FunctionCallType = "SetIntersect"
FunctionCallSetUnion FunctionCallType = "SetUnion" FunctionCallSetUnion FunctionCallType = "SetUnion"
FunctionCallIif FunctionCallType = "Iif"
FunctionCallMathAbs FunctionCallType = "MathAbs" FunctionCallMathAbs FunctionCallType = "MathAbs"
FunctionCallMathAcos FunctionCallType = "MathAcos" FunctionCallMathAcos FunctionCallType = "MathAcos"
FunctionCallMathAsin FunctionCallType = "MathAsin" FunctionCallMathAsin FunctionCallType = "MathAsin"
+366
View File
@@ -0,0 +1,366 @@
package nosql_test
import (
"testing"
"github.com/pikami/cosmium/parsers"
testutils "github.com/pikami/cosmium/test_utils"
)
func Test_Parse_Arithmetics(t *testing.T) {
t.Run("Should parse multiplication before addition", func(t *testing.T) {
testQueryParse(
t,
`SELECT c.a + c.b * c.c FROM c`,
parsers.SelectStmt{
SelectItems: []parsers.SelectItem{
{
Type: parsers.SelectItemTypeBinaryExpression,
Value: parsers.BinaryExpression{
Operation: "+",
Left: testutils.SelectItem_Path("c", "a"),
Right: parsers.SelectItem{
Type: parsers.SelectItemTypeBinaryExpression,
Value: parsers.BinaryExpression{
Operation: "*",
Left: testutils.SelectItem_Path("c", "b"),
Right: testutils.SelectItem_Path("c", "c"),
},
},
},
},
},
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
},
)
})
t.Run("Should parse division before subtraction", func(t *testing.T) {
testQueryParse(
t,
`SELECT c.x - c.y / c.z FROM c`,
parsers.SelectStmt{
SelectItems: []parsers.SelectItem{
{
Type: parsers.SelectItemTypeBinaryExpression,
Value: parsers.BinaryExpression{
Operation: "-",
Left: testutils.SelectItem_Path("c", "x"),
Right: parsers.SelectItem{
Type: parsers.SelectItemTypeBinaryExpression,
Value: parsers.BinaryExpression{
Operation: "/",
Left: testutils.SelectItem_Path("c", "y"),
Right: testutils.SelectItem_Path("c", "z"),
},
},
},
},
},
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
},
)
})
t.Run("Should handle complex mixed operations", func(t *testing.T) {
testQueryParse(
t,
`SELECT c.a + c.b * c.c - c.d / c.e FROM c`,
parsers.SelectStmt{
SelectItems: []parsers.SelectItem{
{
Type: parsers.SelectItemTypeBinaryExpression,
Value: parsers.BinaryExpression{
Operation: "-",
Left: parsers.SelectItem{
Type: parsers.SelectItemTypeBinaryExpression,
Value: parsers.BinaryExpression{
Operation: "+",
Left: testutils.SelectItem_Path("c", "a"),
Right: parsers.SelectItem{
Type: parsers.SelectItemTypeBinaryExpression,
Value: parsers.BinaryExpression{
Operation: "*",
Left: testutils.SelectItem_Path("c", "b"),
Right: testutils.SelectItem_Path("c", "c"),
},
},
},
},
Right: parsers.SelectItem{
Type: parsers.SelectItemTypeBinaryExpression,
Value: parsers.BinaryExpression{
Operation: "/",
Left: testutils.SelectItem_Path("c", "d"),
Right: testutils.SelectItem_Path("c", "e"),
},
},
},
},
},
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
},
)
})
t.Run("Should respect parentheses overriding precedence", func(t *testing.T) {
testQueryParse(
t,
`SELECT (c.a + c.b) * c.c FROM c`,
parsers.SelectStmt{
SelectItems: []parsers.SelectItem{
{
Type: parsers.SelectItemTypeBinaryExpression,
Value: parsers.BinaryExpression{
Operation: "*",
Left: parsers.SelectItem{
Type: parsers.SelectItemTypeBinaryExpression,
Value: parsers.BinaryExpression{
Operation: "+",
Left: testutils.SelectItem_Path("c", "a"),
Right: testutils.SelectItem_Path("c", "b"),
},
},
Right: testutils.SelectItem_Path("c", "c"),
},
},
},
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
},
)
})
t.Run("Should handle nested parentheses", func(t *testing.T) {
testQueryParse(
t,
`SELECT ((c.a + c.b) * c.c) - c.d FROM c`,
parsers.SelectStmt{
SelectItems: []parsers.SelectItem{
{
Type: parsers.SelectItemTypeBinaryExpression,
Value: parsers.BinaryExpression{
Operation: "-",
Left: parsers.SelectItem{
Type: parsers.SelectItemTypeBinaryExpression,
Value: parsers.BinaryExpression{
Operation: "*",
Left: parsers.SelectItem{
Type: parsers.SelectItemTypeBinaryExpression,
Value: parsers.BinaryExpression{
Operation: "+",
Left: testutils.SelectItem_Path("c", "a"),
Right: testutils.SelectItem_Path("c", "b"),
},
},
Right: testutils.SelectItem_Path("c", "c"),
},
},
Right: testutils.SelectItem_Path("c", "d"),
},
},
},
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
},
)
})
t.Run("Should be left associative for same precedence operators", func(t *testing.T) {
testQueryParse(
t,
`SELECT c.a - c.b - c.c FROM c`,
parsers.SelectStmt{
SelectItems: []parsers.SelectItem{
{
Type: parsers.SelectItemTypeBinaryExpression,
Value: parsers.BinaryExpression{
Operation: "-",
Left: parsers.SelectItem{
Type: parsers.SelectItemTypeBinaryExpression,
Value: parsers.BinaryExpression{
Operation: "-",
Left: testutils.SelectItem_Path("c", "a"),
Right: testutils.SelectItem_Path("c", "b"),
},
},
Right: testutils.SelectItem_Path("c", "c"),
},
},
},
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
},
)
})
t.Run("Should be left associative with multiplication and division", func(t *testing.T) {
testQueryParse(
t,
`SELECT c.a * c.b / c.c FROM c`,
parsers.SelectStmt{
SelectItems: []parsers.SelectItem{
{
Type: parsers.SelectItemTypeBinaryExpression,
Value: parsers.BinaryExpression{
Operation: "/",
Left: parsers.SelectItem{
Type: parsers.SelectItemTypeBinaryExpression,
Value: parsers.BinaryExpression{
Operation: "*",
Left: testutils.SelectItem_Path("c", "a"),
Right: testutils.SelectItem_Path("c", "b"),
},
},
Right: testutils.SelectItem_Path("c", "c"),
},
},
},
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
},
)
})
t.Run("Should handle math with constants", func(t *testing.T) {
testQueryParse(
t,
`SELECT 10 + 20 * 5 FROM c`,
parsers.SelectStmt{
SelectItems: []parsers.SelectItem{
{
Type: parsers.SelectItemTypeBinaryExpression,
Value: parsers.BinaryExpression{
Operation: "+",
Left: testutils.SelectItem_Constant_Int(10),
Right: parsers.SelectItem{
Type: parsers.SelectItemTypeBinaryExpression,
Value: parsers.BinaryExpression{
Operation: "*",
Left: testutils.SelectItem_Constant_Int(20),
Right: testutils.SelectItem_Constant_Int(5),
},
},
},
},
},
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
},
)
})
t.Run("Should handle math with floating point numbers", func(t *testing.T) {
testQueryParse(
t,
`SELECT c.price * 1.08 FROM c`,
parsers.SelectStmt{
SelectItems: []parsers.SelectItem{
{
Type: parsers.SelectItemTypeBinaryExpression,
Value: parsers.BinaryExpression{
Operation: "*",
Left: testutils.SelectItem_Path("c", "price"),
Right: testutils.SelectItem_Constant_Float(1.08),
},
},
},
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
},
)
})
t.Run("Should handle parentheses around single value", func(t *testing.T) {
testQueryParse(
t,
`SELECT (c.value) FROM c`,
parsers.SelectStmt{
SelectItems: []parsers.SelectItem{
testutils.SelectItem_Path("c", "value"),
},
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
},
)
})
t.Run("Should handle function calls in math expressions", func(t *testing.T) {
testQueryParse(
t,
`SELECT LENGTH(c.name) * 2 + 10 FROM c`,
parsers.SelectStmt{
SelectItems: []parsers.SelectItem{
{
Type: parsers.SelectItemTypeBinaryExpression,
Value: parsers.BinaryExpression{
Operation: "+",
Left: parsers.SelectItem{
Type: parsers.SelectItemTypeBinaryExpression,
Value: parsers.BinaryExpression{
Operation: "*",
Left: parsers.SelectItem{
Type: parsers.SelectItemTypeFunctionCall,
Value: parsers.FunctionCall{
Type: parsers.FunctionCallLength,
Arguments: []interface{}{testutils.SelectItem_Path("c", "name")},
},
},
Right: testutils.SelectItem_Constant_Int(2),
},
},
Right: testutils.SelectItem_Constant_Int(10),
},
},
},
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
},
)
})
t.Run("Should handle multiple select items with math", func(t *testing.T) {
testQueryParse(
t,
`SELECT c.a + c.b, c.x * c.y FROM c`,
parsers.SelectStmt{
SelectItems: []parsers.SelectItem{
{
Type: parsers.SelectItemTypeBinaryExpression,
Value: parsers.BinaryExpression{
Operation: "+",
Left: testutils.SelectItem_Path("c", "a"),
Right: testutils.SelectItem_Path("c", "b"),
},
},
{
Type: parsers.SelectItemTypeBinaryExpression,
Value: parsers.BinaryExpression{
Operation: "*",
Left: testutils.SelectItem_Path("c", "x"),
Right: testutils.SelectItem_Path("c", "y"),
},
},
},
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
},
)
})
t.Run("Should handle math in WHERE clause", func(t *testing.T) {
testQueryParse(
t,
`SELECT c.id FROM c WHERE c.price * 1.08 > 100`,
parsers.SelectStmt{
SelectItems: []parsers.SelectItem{
testutils.SelectItem_Path("c", "id"),
},
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
Filters: parsers.ComparisonExpression{
Operation: ">",
Left: parsers.SelectItem{
Type: parsers.SelectItemTypeBinaryExpression,
Value: parsers.BinaryExpression{
Operation: "*",
Left: testutils.SelectItem_Path("c", "price"),
Right: testutils.SelectItem_Constant_Float(1.08),
},
},
Right: testutils.SelectItem_Constant_Int(100),
},
},
)
})
}
+54
View File
@@ -112,6 +112,37 @@ func Test_Parse(t *testing.T) {
) )
}) })
t.Run("Should parse NOT IN function", func(t *testing.T) {
testQueryParse(
t,
`SELECT c.id FROM c WHERE c.id NOT IN ("123", "456")`,
parsers.SelectStmt{
SelectItems: []parsers.SelectItem{
{
Path: []string{"c", "id"},
Type: parsers.SelectItemTypeField,
},
},
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
Filters: parsers.SelectItem{
Type: parsers.SelectItemTypeFunctionCall,
Invert: true,
Value: parsers.FunctionCall{
Type: parsers.FunctionCallIn,
Arguments: []interface{}{
parsers.SelectItem{
Path: []string{"c", "id"},
Type: parsers.SelectItemTypeField,
},
testutils.SelectItem_Constant_String("123"),
testutils.SelectItem_Constant_String("456"),
},
},
},
},
)
})
t.Run("Should parse IN function with function call", func(t *testing.T) { t.Run("Should parse IN function with function call", func(t *testing.T) {
testQueryParse( testQueryParse(
t, t,
@@ -163,4 +194,27 @@ func Test_Parse(t *testing.T) {
}, },
) )
}) })
t.Run("Should parse IIF function", func(t *testing.T) {
testQueryParse(
t,
`SELECT IIF(true, c.pk, c.id) FROM c`,
parsers.SelectStmt{
SelectItems: []parsers.SelectItem{
{
Type: parsers.SelectItemTypeFunctionCall,
Value: parsers.FunctionCall{
Type: parsers.FunctionCallIif,
Arguments: []interface{}{
testutils.SelectItem_Constant_Bool(true),
testutils.SelectItem_Path("c", "pk"),
testutils.SelectItem_Path("c", "id"),
},
},
},
},
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
},
)
})
} }
+2465 -1836
View File
File diff suppressed because it is too large Load Diff
+119 -17
View File
@@ -178,6 +178,32 @@ func combineExpressions(ex1 interface{}, exs interface{}, operation parsers.Logi
}, nil }, nil
} }
func makeMathExpression(left interface{}, operations interface{}) (interface{}, error) {
if operations == nil || len(operations.([]interface{})) == 0 {
return left, nil
}
result := left.(parsers.SelectItem)
ops := operations.([]interface{})
for _, op := range ops {
opData := op.([]interface{})
operation := opData[0].(string)
right := opData[1].(parsers.SelectItem)
result = parsers.SelectItem{
Type: parsers.SelectItemTypeBinaryExpression,
Value: parsers.BinaryExpression{
Left: result,
Right: right,
Operation: operation,
},
}
}
return result, nil
}
} }
Input <- selectStmt:SelectStmt { Input <- selectStmt:SelectStmt {
@@ -204,7 +230,7 @@ TopClause <- Top ws count:Integer {
return count, nil return count, nil
} }
FromClause <- From ws table:TableName selectItem:(ws In ws column:SelectItem { return column, nil }) { FromClause <- From ws table:TableName selectItem:(ws In ws column:SelectItemWithAlias { return column, nil }) {
tableTyped := table.(parsers.Table) tableTyped := table.(parsers.Table)
if selectItem != nil { if selectItem != nil {
@@ -213,7 +239,7 @@ FromClause <- From ws table:TableName selectItem:(ws In ws column:SelectItem { r
} }
return tableTyped, nil return tableTyped, nil
} / From ws column:SelectItem { } / From ws column:SelectItemWithAlias {
tableSelectItem := column.(parsers.SelectItem) tableSelectItem := column.(parsers.SelectItem)
table := parsers.Table{ table := parsers.Table{
Value: tableSelectItem.Alias, Value: tableSelectItem.Alias,
@@ -251,7 +277,7 @@ SubQuerySelectItem <- subQuery:SubQuery asClause:(ws alias:AsClause { return ali
return selectItem, nil return selectItem, nil
} }
JoinClause <- Join ws table:TableName ws In ws column:SelectItem { JoinClause <- Join ws table:TableName ws In ws column:SelectItemWithAlias {
return makeJoin(table, column) return makeJoin(table, column)
} / Join ws subQuery:SubQuerySelectItem { } / Join ws subQuery:SubQuerySelectItem {
return makeJoin(nil, subQuery) return makeJoin(nil, subQuery)
@@ -269,11 +295,34 @@ SelectAsterisk <- "*" {
return makeColumnList(selectItem, make([]interface{}, 0)) return makeColumnList(selectItem, make([]interface{}, 0))
} }
ColumnList <- column:SelectItem other_columns:(ws "," ws coll:SelectItem {return coll, nil })* { ColumnList <- column:ExpressionOrSelectItem other_columns:(ws "," ws coll:ExpressionOrSelectItem {return coll, nil })* {
return makeColumnList(column, other_columns) return makeColumnList(column, other_columns)
} }
SelectValueSpec <- "VALUE"i ws column:SelectItem { ExpressionOrSelectItem <- expression:OrExpression asClause:AsClause? {
switch typedValue := expression.(type) {
case parsers.ComparisonExpression, parsers.LogicalExpression:
selectItem := parsers.SelectItem{
Type: parsers.SelectItemTypeExpression,
Value: typedValue,
}
if aliasValue, ok := asClause.(string); ok {
selectItem.Alias = aliasValue
}
return selectItem, nil
case parsers.SelectItem:
if aliasValue, ok := asClause.(string); ok {
typedValue.Alias = aliasValue
}
return typedValue, nil
default:
return typedValue, nil
}
} / item:SelectItemWithAlias { return item, nil }
SelectValueSpec <- "VALUE"i ws column:SelectItemWithAlias {
selectItem := column.(parsers.SelectItem) selectItem := column.(parsers.SelectItem)
selectItem.IsTopLevel = true selectItem.IsTopLevel = true
return makeColumnList(selectItem, make([]interface{}, 0)) return makeColumnList(selectItem, make([]interface{}, 0))
@@ -289,6 +338,11 @@ SelectArray <- "[" ws columns:ColumnList ws "]" {
SelectObject <- "{" ws field:SelectObjectField ws other_fields:(ws "," ws coll:SelectObjectField {return coll, nil })* ws "}" { SelectObject <- "{" ws field:SelectObjectField ws other_fields:(ws "," ws coll:SelectObjectField {return coll, nil })* ws "}" {
return makeSelectObject(field, other_fields) return makeSelectObject(field, other_fields)
} / "{" ws "}" {
return parsers.SelectItem{
SelectItems: []parsers.SelectItem{},
Type: parsers.SelectItemTypeObject,
}, nil
} }
SelectObjectField <- name:(Identifier / "\"" key:Identifier "\"" { return key, nil }) ws ":" ws selectItem:SelectItem { SelectObjectField <- name:(Identifier / "\"" key:Identifier "\"" { return key, nil }) ws ":" ws selectItem:SelectItem {
@@ -301,7 +355,15 @@ SelectProperty <- name:Identifier path:(DotFieldAccess / ArrayFieldAccess)* {
return makeSelectItem(name, path, parsers.SelectItemTypeField) return makeSelectItem(name, path, parsers.SelectItemTypeField)
} }
SelectItem <- selectItem:(SubQuerySelectItem / Literal / FunctionCall / SelectArray / SelectObject / SelectProperty) asClause:AsClause? { SelectItemWithAlias <- selectItem:SelectItem asClause:AsClause? {
item := selectItem.(parsers.SelectItem)
if aliasValue, ok := asClause.(string); ok {
item.Alias = aliasValue
}
return item, nil
}
SelectItem <- selectItem:(SubQuerySelectItem / Literal / FunctionCall / SelectArray / SelectObject / SelectProperty) {
var itemResult parsers.SelectItem var itemResult parsers.SelectItem
switch typedValue := selectItem.(type) { switch typedValue := selectItem.(type) {
case parsers.SelectItem: case parsers.SelectItem:
@@ -318,10 +380,6 @@ SelectItem <- selectItem:(SubQuerySelectItem / Literal / FunctionCall / SelectAr
} }
} }
if aliasValue, ok := asClause.(string); ok {
itemResult.Alias = aliasValue
}
return itemResult, nil return itemResult, nil
} }
@@ -355,10 +413,20 @@ AndExpression <- ex1:ComparisonExpression ex2:(ws And ws ex:ComparisonExpression
return combineExpressions(ex1, ex2, parsers.LogicalExpressionTypeAnd) return combineExpressions(ex1, ex2, parsers.LogicalExpressionTypeAnd)
} }
ComparisonExpression <- "(" ws ex:OrExpression ws ")" { return ex, nil } ComparisonExpression <- left:AddSubExpression ws op:ComparisonOperator ws right:AddSubExpression {
/ left:SelectItem ws op:ComparisonOperator ws right:SelectItem {
return parsers.ComparisonExpression{Left:left,Right:right,Operation:op.(string)}, nil return parsers.ComparisonExpression{Left:left,Right:right,Operation:op.(string)}, nil
} / inv:(Not ws)? ex:SelectItem { } / ex:AddSubExpression { return ex, nil }
AddSubExpression <- left:MulDivExpression operations:(ws op:AddOrSubtractOperation ws right:MulDivExpression { return []interface{}{op, right}, nil })* {
return makeMathExpression(left, operations)
}
MulDivExpression <- left:SelectItemWithParentheses operations:(ws op:MultiplyOrDivideOperation ws right:SelectItemWithParentheses { return []interface{}{op, right}, nil })* {
return makeMathExpression(left, operations)
}
SelectItemWithParentheses <- "(" ws ex:OrExpression ws ")" { return ex, nil }
/ inv:(Not ws)? ex:SelectItem {
if inv != nil { if inv != nil {
ex1 := ex.(parsers.SelectItem) ex1 := ex.(parsers.SelectItem)
ex1.Invert = true ex1.Invert = true
@@ -415,6 +483,10 @@ ComparisonOperator <- ("<=" / ">=" / "=" / "!=" / "<" / ">") {
return string(c.text), nil return string(c.text), nil
} }
AddOrSubtractOperation <- ("+" / "-") { return string(c.text), nil }
MultiplyOrDivideOperation <- ("*" / "/") { return string(c.text), nil }
Literal <- FloatLiteral / IntegerLiteral / StringLiteral / BooleanLiteral / ParameterConstant / NullConstant Literal <- FloatLiteral / IntegerLiteral / StringLiteral / BooleanLiteral / ParameterConstant / NullConstant
ParameterConstant <- "@" Identifier { ParameterConstant <- "@" Identifier {
@@ -442,6 +514,7 @@ BooleanLiteral <- ("true"i / "false"i) {
FunctionCall <- StringFunctions FunctionCall <- StringFunctions
/ TypeCheckingFunctions / TypeCheckingFunctions
/ ArrayFunctions / ArrayFunctions
/ ConditionalFunctions
/ InFunction / InFunction
/ AggregateFunctions / AggregateFunctions
/ MathFunctions / MathFunctions
@@ -489,6 +562,8 @@ ArrayFunctions <- ArrayConcatExpression
/ SetIntersectExpression / SetIntersectExpression
/ SetUnionExpression / SetUnionExpression
ConditionalFunctions <- IifExpression
MathFunctions <- MathAbsExpression MathFunctions <- MathAbsExpression
/ MathAcosExpression / MathAcosExpression
/ MathAsinExpression / MathAsinExpression
@@ -681,6 +756,10 @@ SetUnionExpression <- "SetUnion"i ws "(" ws set1:SelectItem ws "," ws set2:Selec
return createFunctionCall(parsers.FunctionCallSetUnion, []interface{}{set1, set2}) return createFunctionCall(parsers.FunctionCallSetUnion, []interface{}{set1, set2})
} }
IifExpression <- "IIF"i ws "(" ws condition:SelectItem ws "," ws trueValue:SelectItem ws "," ws falseValue:SelectItem ws ")" {
return createFunctionCall(parsers.FunctionCallIif, []interface{}{condition, trueValue, falseValue})
}
MathAbsExpression <- "ABS"i ws "(" ws ex:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathAbs, []interface{}{ex}) } MathAbsExpression <- "ABS"i ws "(" ws ex:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathAbs, []interface{}{ex}) }
MathAcosExpression <- "ACOS"i ws "(" ws ex:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathAcos, []interface{}{ex}) } MathAcosExpression <- "ACOS"i ws "(" ws ex:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathAcos, []interface{}{ex}) }
MathAsinExpression <- "ASIN"i ws "(" ws ex:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathAsin, []interface{}{ex}) } MathAsinExpression <- "ASIN"i ws "(" ws ex:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathAsin, []interface{}{ex}) }
@@ -724,10 +803,33 @@ MathNumberBinExpression <- "NumberBin"i ws "(" ws ex1:SelectItem others:(ws ","
MathPiExpression <- "PI"i ws "(" ws ")" { return createFunctionCall(parsers.FunctionCallMathPi, []interface{}{}) } MathPiExpression <- "PI"i ws "(" ws ")" { return createFunctionCall(parsers.FunctionCallMathPi, []interface{}{}) }
MathRandExpression <- "RAND"i ws "(" ws ")" { return createFunctionCall(parsers.FunctionCallMathRand, []interface{}{}) } MathRandExpression <- "RAND"i ws "(" ws ")" { return createFunctionCall(parsers.FunctionCallMathRand, []interface{}{}) }
InFunction <- ex1:SelectProperty ws In ws "(" ws ex2:SelectItem others:(ws "," ws ex:SelectItem { return ex, nil })* ws ")" { InFunction <- ex1:SelectProperty ws notIn:("NOT"i ws)? In ws "(" ws ex2:SelectItem others:(ws "," ws ex:SelectItem { return ex, nil })* ws ")" {
return createFunctionCall(parsers.FunctionCallIn, append([]interface{}{ex1, ex2}, others.([]interface{})...)) arguments := append([]interface{}{ex1, ex2}, others.([]interface{})...)
} / "(" ws ex1:SelectItem ws In ws "(" ws ex2:SelectItem others:(ws "," ws ex:SelectItem { return ex, nil })* ws ")" ws ")" { functionCall, _ := createFunctionCall(parsers.FunctionCallIn, arguments)
return createFunctionCall(parsers.FunctionCallIn, append([]interface{}{ex1, ex2}, others.([]interface{})...))
if notIn != nil {
return parsers.SelectItem{
Type: parsers.SelectItemTypeFunctionCall,
Value: functionCall,
Invert: true,
}, nil
}
return functionCall, nil
}
/ "(" ws ex1:SelectItem ws notIn:("NOT"i ws)? In ws "(" ws ex2:SelectItem others:(ws "," ws ex:SelectItem { return ex, nil })* ws ")" ws ")" {
arguments := append([]interface{}{ex1, ex2}, others.([]interface{})...)
functionCall, _ := createFunctionCall(parsers.FunctionCallIn, arguments)
if notIn != nil {
return parsers.SelectItem{
Type: parsers.SelectItemTypeFunctionCall,
Value: functionCall,
Invert: true,
}, nil
}
return functionCall, nil
} }
AvgAggregateExpression <- "AVG"i "(" ws ex:SelectItem ws ")" { AvgAggregateExpression <- "AVG"i "(" ws ex:SelectItem ws ")" {
+86
View File
@@ -178,4 +178,90 @@ func Test_Parse_Select(t *testing.T) {
}, },
) )
}) })
t.Run("Should parse SELECT empty object", func(t *testing.T) {
testQueryParse(
t,
`SELECT {} AS obj FROM c`,
parsers.SelectStmt{
SelectItems: []parsers.SelectItem{
{
Alias: "obj",
Type: parsers.SelectItemTypeObject,
SelectItems: []parsers.SelectItem{},
},
},
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
},
)
})
t.Run("Should parse comparison expressions in SELECT", func(t *testing.T) {
testQueryParse(
t,
`SELECT c["id"] = "123", c["pk"] > 456 FROM c`,
parsers.SelectStmt{
SelectItems: []parsers.SelectItem{
{
Type: parsers.SelectItemTypeExpression,
Value: parsers.ComparisonExpression{
Operation: "=",
Left: testutils.SelectItem_Path("c", "id"),
Right: testutils.SelectItem_Constant_String("123"),
},
},
{
Type: parsers.SelectItemTypeExpression,
Value: parsers.ComparisonExpression{
Operation: ">",
Left: testutils.SelectItem_Path("c", "pk"),
Right: testutils.SelectItem_Constant_Int(456),
},
},
},
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
},
)
})
t.Run("Should parse logical expressions in SELECT", func(t *testing.T) {
testQueryParse(
t,
`SELECT c["id"] = "123" OR c["pk"] > 456, c["isCool"] AND c["hasRizz"] AS isRizzler FROM c`,
parsers.SelectStmt{
SelectItems: []parsers.SelectItem{
{
Type: parsers.SelectItemTypeExpression,
Value: parsers.LogicalExpression{
Operation: parsers.LogicalExpressionTypeOr,
Expressions: []interface{}{
parsers.ComparisonExpression{
Operation: "=",
Left: testutils.SelectItem_Path("c", "id"),
Right: testutils.SelectItem_Constant_String("123"),
},
parsers.ComparisonExpression{
Operation: ">",
Left: testutils.SelectItem_Path("c", "pk"),
Right: testutils.SelectItem_Constant_Int(456),
},
},
},
},
{
Type: parsers.SelectItemTypeExpression,
Alias: "isRizzler",
Value: parsers.LogicalExpression{
Operation: parsers.LogicalExpressionTypeAnd,
Expressions: []interface{}{
testutils.SelectItem_Path("c", "isCool"),
testutils.SelectItem_Path("c", "hasRizz"),
},
},
},
},
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
},
)
})
} }
@@ -0,0 +1,91 @@
package memoryexecutor_test
import (
"testing"
"github.com/pikami/cosmium/parsers"
memoryexecutor "github.com/pikami/cosmium/query_executors/memory_executor"
testutils "github.com/pikami/cosmium/test_utils"
)
func Test_Execute_Arithmetics(t *testing.T) {
mockData := []memoryexecutor.RowType{
map[string]interface{}{"id": 1, "a": 420},
map[string]interface{}{"id": 2, "a": 6.9},
map[string]interface{}{"id": 3},
}
t.Run("Should execute simple arithmetics", func(t *testing.T) {
testQueryExecute(
t,
parsers.SelectStmt{
SelectItems: []parsers.SelectItem{
{
Path: []string{"c", "id"},
Type: parsers.SelectItemTypeField,
},
{
Type: parsers.SelectItemTypeBinaryExpression,
Alias: "result",
Value: parsers.BinaryExpression{
Operation: "+",
Left: testutils.SelectItem_Path("c", "a"),
Right: parsers.SelectItem{
Type: parsers.SelectItemTypeBinaryExpression,
Value: parsers.BinaryExpression{
Operation: "*",
Left: testutils.SelectItem_Constant_Float(2.0),
Right: testutils.SelectItem_Constant_Int(3),
},
},
},
},
},
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
},
mockData,
[]memoryexecutor.RowType{
map[string]interface{}{"id": 1, "result": 426.0},
map[string]interface{}{"id": 2, "result": 12.9},
map[string]interface{}{"id": 3, "result": nil},
},
)
})
t.Run("Should execute arithmetics in WHERE clause", func(t *testing.T) {
testQueryExecute(
t,
parsers.SelectStmt{
SelectItems: []parsers.SelectItem{
testutils.SelectItem_Path("c", "id"),
{
Alias: "result",
Type: parsers.SelectItemTypeBinaryExpression,
Value: parsers.BinaryExpression{
Operation: "*",
Left: testutils.SelectItem_Path("c", "a"),
Right: testutils.SelectItem_Constant_Int(2),
},
},
},
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
Filters: parsers.ComparisonExpression{
Operation: ">",
Left: parsers.SelectItem{
Type: parsers.SelectItemTypeBinaryExpression,
Value: parsers.BinaryExpression{
Operation: "*",
Left: testutils.SelectItem_Path("c", "a"),
Right: testutils.SelectItem_Constant_Int(2),
},
},
Right: testutils.SelectItem_Constant_Int(500),
},
},
mockData,
[]memoryexecutor.RowType{
map[string]interface{}{"id": 1, "result": 840.0},
},
)
})
}
+78 -16
View File
@@ -69,6 +69,28 @@ func (r rowContext) resolveSelectItem(selectItem parsers.SelectItem) interface{}
return nil return nil
} }
if selectItem.Type == parsers.SelectItemTypeExpression {
if typedExpression, ok := selectItem.Value.(parsers.ComparisonExpression); ok {
return r.filters_ComparisonExpression(typedExpression)
}
if typedExpression, ok := selectItem.Value.(parsers.LogicalExpression); ok {
return r.filters_LogicalExpression(typedExpression)
}
logger.ErrorLn("parsers.SelectItem has incorrect Value type (expected parsers.ComparisonExpression)")
return nil
}
if selectItem.Type == parsers.SelectItemTypeBinaryExpression {
if typedSelectItem, ok := selectItem.Value.(parsers.BinaryExpression); ok {
return r.selectItem_SelectItemTypeBinaryExpression(typedSelectItem)
}
logger.ErrorLn("parsers.SelectItem has incorrect Value type (expected parsers.BinaryExpression)")
return nil
}
return r.selectItem_SelectItemTypeField(selectItem) return r.selectItem_SelectItemTypeField(selectItem)
} }
@@ -209,6 +231,9 @@ func (r rowContext) selectItem_SelectItemTypeFunctionCall(functionCall parsers.F
case parsers.FunctionCallSetUnion: case parsers.FunctionCallSetUnion:
return r.set_Union(functionCall.Arguments) return r.set_Union(functionCall.Arguments)
case parsers.FunctionCallIif:
return r.misc_Iif(functionCall.Arguments)
case parsers.FunctionCallMathAbs: case parsers.FunctionCallMathAbs:
return r.math_Abs(functionCall.Arguments) return r.math_Abs(functionCall.Arguments)
case parsers.FunctionCallMathAcos: case parsers.FunctionCallMathAcos:
@@ -301,6 +326,45 @@ func (r rowContext) selectItem_SelectItemTypeFunctionCall(functionCall parsers.F
return nil return nil
} }
func (r rowContext) selectItem_SelectItemTypeBinaryExpression(binaryExpression parsers.BinaryExpression) interface{} {
if binaryExpression.Left == nil || binaryExpression.Right == nil {
logger.Debug("parsers.BinaryExpression has nil Left or Right value")
return nil
}
leftValue := r.resolveSelectItem(binaryExpression.Left.(parsers.SelectItem))
rightValue := r.resolveSelectItem(binaryExpression.Right.(parsers.SelectItem))
if leftValue == nil || rightValue == nil {
return nil
}
leftNumber, leftIsNumber := numToFloat64(leftValue)
rightNumber, rightIsNumber := numToFloat64(rightValue)
if !leftIsNumber || !rightIsNumber {
logger.Debug("Binary expression operands are not numbers, returning nil")
return nil
}
switch binaryExpression.Operation {
case "+":
return leftNumber + rightNumber
case "-":
return leftNumber - rightNumber
case "*":
return leftNumber * rightNumber
case "/":
if rightNumber == 0 {
logger.Debug("Division by zero in binary expression")
return nil
}
return leftNumber / rightNumber
default:
return nil
}
}
func (r rowContext) selectItem_SelectItemTypeField(selectItem parsers.SelectItem) interface{} { func (r rowContext) selectItem_SelectItemTypeField(selectItem parsers.SelectItem) interface{} {
value := r.tables[selectItem.Path[0]] value := r.tables[selectItem.Path[0]]
@@ -336,6 +400,7 @@ func (r rowContext) selectItem_SelectItemTypeField(selectItem parsers.SelectItem
} }
func compareValues(val1, val2 interface{}) int { func compareValues(val1, val2 interface{}) int {
// Handle nil values
if val1 == nil && val2 == nil { if val1 == nil && val2 == nil {
return 0 return 0
} else if val1 == nil { } else if val1 == nil {
@@ -344,27 +409,24 @@ func compareValues(val1, val2 interface{}) int {
return 1 return 1
} }
// Handle number values
val1Number, val1IsNumber := numToFloat64(val1)
val2Number, val2IsNumber := numToFloat64(val2)
if val1IsNumber && val2IsNumber {
if val1Number < val2Number {
return -1
} else if val1Number > val2Number {
return 1
}
return 0
}
// Handle different types
if reflect.TypeOf(val1) != reflect.TypeOf(val2) { if reflect.TypeOf(val1) != reflect.TypeOf(val2) {
return 1 return 1
} }
switch val1 := val1.(type) { switch val1 := val1.(type) {
case int:
val2 := val2.(int)
if val1 < val2 {
return -1
} else if val1 > val2 {
return 1
}
return 0
case float64:
val2 := val2.(float64)
if val1 < val2 {
return -1
} else if val1 > val2 {
return 1
}
return 0
case string: case string:
val2 := val2.(string) val2 := val2.(string)
return strings.Compare(val1, val2) return strings.Compare(val1, val2)
@@ -0,0 +1,92 @@
package memoryexecutor_test
import (
"testing"
"github.com/pikami/cosmium/parsers"
memoryexecutor "github.com/pikami/cosmium/query_executors/memory_executor"
testutils "github.com/pikami/cosmium/test_utils"
)
func Test_Execute_Expressions(t *testing.T) {
mockData := []memoryexecutor.RowType{
map[string]interface{}{"id": "123", "age": 10, "isCool": true},
map[string]interface{}{"id": "456", "age": 20, "isCool": false},
map[string]interface{}{"id": "789", "age": 30, "isCool": true},
}
t.Run("Should execute comparison expressions in SELECT", func(t *testing.T) {
testQueryExecute(
t,
parsers.SelectStmt{
SelectItems: []parsers.SelectItem{
{
Path: []string{"c", "id"},
Type: parsers.SelectItemTypeField,
},
{
Alias: "isAdult",
Type: parsers.SelectItemTypeExpression,
Value: parsers.ComparisonExpression{
Operation: ">=",
Left: testutils.SelectItem_Path("c", "age"),
Right: testutils.SelectItem_Constant_Int(18),
},
},
{
Alias: "isNotCool",
Type: parsers.SelectItemTypeExpression,
Value: parsers.ComparisonExpression{
Operation: "!=",
Left: testutils.SelectItem_Path("c", "isCool"),
Right: testutils.SelectItem_Constant_Bool(true),
},
},
},
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
},
mockData,
[]memoryexecutor.RowType{
map[string]interface{}{"id": "123", "isAdult": false, "isNotCool": false},
map[string]interface{}{"id": "456", "isAdult": true, "isNotCool": true},
map[string]interface{}{"id": "789", "isAdult": true, "isNotCool": false},
},
)
})
t.Run("Should execute logical expressions in SELECT", func(t *testing.T) {
testQueryExecute(
t,
parsers.SelectStmt{
SelectItems: []parsers.SelectItem{
{
Path: []string{"c", "id"},
Type: parsers.SelectItemTypeField,
},
{
Alias: "isCoolAndAdult",
Type: parsers.SelectItemTypeExpression,
Value: parsers.LogicalExpression{
Operation: parsers.LogicalExpressionTypeAnd,
Expressions: []interface{}{
testutils.SelectItem_Path("c", "isCool"),
parsers.ComparisonExpression{
Operation: ">=",
Left: testutils.SelectItem_Path("c", "age"),
Right: testutils.SelectItem_Constant_Int(18),
},
},
},
},
},
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
},
mockData,
[]memoryexecutor.RowType{
map[string]interface{}{"id": "123", "isCoolAndAdult": false},
map[string]interface{}{"id": "456", "isCoolAndAdult": false},
map[string]interface{}{"id": "789", "isCoolAndAdult": true},
},
)
})
}
@@ -605,10 +605,30 @@ func numToInt(ex interface{}) (int, bool) {
func numToFloat64(num interface{}) (float64, bool) { func numToFloat64(num interface{}) (float64, bool) {
switch val := num.(type) { switch val := num.(type) {
case float64:
return val, true
case int: case int:
return float64(val), true return float64(val), true
case int8:
return float64(val), true
case int16:
return float64(val), true
case int32:
return float64(val), true
case int64:
return float64(val), true
case uint:
return float64(val), true
case uint8:
return float64(val), true
case uint16:
return float64(val), true
case uint32:
return float64(val), true
case uint64:
return float64(val), true
case float32:
return float64(val), true
case float64:
return val, true
default: default:
return 0, false return 0, false
} }
@@ -16,3 +16,16 @@ func (r rowContext) misc_In(arguments []interface{}) bool {
return false return false
} }
func (r rowContext) misc_Iif(arguments []interface{}) interface{} {
if len(arguments) != 3 {
return nil
}
condition := r.resolveSelectItem(arguments[0].(parsers.SelectItem))
if condition != nil && condition == true {
return r.resolveSelectItem(arguments[1].(parsers.SelectItem))
}
return r.resolveSelectItem(arguments[2].(parsers.SelectItem))
}
@@ -149,6 +149,41 @@ func Test_Execute(t *testing.T) {
) )
}) })
t.Run("Should execute NOT IN function", func(t *testing.T) {
testQueryExecute(
t,
parsers.SelectStmt{
SelectItems: []parsers.SelectItem{
{
Path: []string{"c", "id"},
Type: parsers.SelectItemTypeField,
},
},
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
Filters: parsers.SelectItem{
Type: parsers.SelectItemTypeFunctionCall,
Invert: true,
Value: parsers.FunctionCall{
Type: parsers.FunctionCallIn,
Arguments: []interface{}{
parsers.SelectItem{
Path: []string{"c", "id"},
Type: parsers.SelectItemTypeField,
},
testutils.SelectItem_Constant_String("123"),
testutils.SelectItem_Constant_String("456"),
},
},
},
},
mockData,
[]memoryexecutor.RowType{
map[string]interface{}{"id": "12345"},
map[string]interface{}{"id": "67890"},
},
)
})
t.Run("Should execute IN function with function call", func(t *testing.T) { t.Run("Should execute IN function with function call", func(t *testing.T) {
testQueryExecute( testQueryExecute(
t, t,
@@ -210,4 +245,35 @@ func Test_Execute(t *testing.T) {
}, },
) )
}) })
t.Run("Should execute function IIF()", func(t *testing.T) {
testQueryExecute(
t,
parsers.SelectStmt{
SelectItems: []parsers.SelectItem{
testutils.SelectItem_Path("c", "id"),
{
Alias: "coolness",
Type: parsers.SelectItemTypeFunctionCall,
Value: parsers.FunctionCall{
Type: parsers.FunctionCallIif,
Arguments: []interface{}{
testutils.SelectItem_Path("c", "isCool"),
testutils.SelectItem_Constant_String("real cool"),
testutils.SelectItem_Constant_String("not cool"),
},
},
},
},
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
},
mockData,
[]memoryexecutor.RowType{
map[string]interface{}{"id": "12345", "coolness": "not cool"},
map[string]interface{}{"id": "67890", "coolness": "real cool"},
map[string]interface{}{"id": "456", "coolness": "real cool"},
map[string]interface{}{"id": "123", "coolness": "real cool"},
},
)
})
} }
@@ -205,4 +205,27 @@ func Test_Execute_Select(t *testing.T) {
}, },
) )
}) })
t.Run("Should execute SELECT empty object", func(t *testing.T) {
testQueryExecute(
t,
parsers.SelectStmt{
SelectItems: []parsers.SelectItem{
{
Alias: "obj",
Type: parsers.SelectItemTypeObject,
SelectItems: []parsers.SelectItem{},
},
},
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
},
mockData,
[]memoryexecutor.RowType{
map[string]interface{}{"obj": map[string]interface{}{}},
map[string]interface{}{"obj": map[string]interface{}{}},
map[string]interface{}{"obj": map[string]interface{}{}},
map[string]interface{}{"obj": map[string]interface{}{}},
},
)
})
} }
+1 -1
View File
@@ -27,7 +27,7 @@ const (
ResponseServerInstanceAlreadyExists = 104 ResponseServerInstanceAlreadyExists = 104
ResponseServerInstanceNotFound = 105 ResponseServerInstanceNotFound = 105
ResponseFailedToStartServer = 106 ResponseFailedToStartServer = 106
ResponseCurentDataStoreDoesNotSupportStateLoading = 107 ResponseCurrentDataStoreDoesNotSupportStateLoading = 107
ResponseDataStoreNotFound = 200 ResponseDataStoreNotFound = 200
ResponseDataStoreConflict = 201 ResponseDataStoreConflict = 201
+17 -5
View File
@@ -11,7 +11,9 @@ import (
"github.com/pikami/cosmium/api" "github.com/pikami/cosmium/api"
"github.com/pikami/cosmium/api/config" "github.com/pikami/cosmium/api/config"
mapdatastore "github.com/pikami/cosmium/internal/datastore/map_datastore" "github.com/pikami/cosmium/internal/datastore"
badgerdatastore "github.com/pikami/cosmium/internal/datastore/badger_datastore"
jsondatastore "github.com/pikami/cosmium/internal/datastore/json_datastore"
) )
//export CreateServerInstance //export CreateServerInstance
@@ -32,10 +34,19 @@ func CreateServerInstance(serverName *C.char, configurationJSON *C.char) int {
configuration.ApplyDefaultsToEmptyFields() configuration.ApplyDefaultsToEmptyFields()
configuration.PopulateCalculatedFields() configuration.PopulateCalculatedFields()
dataStore := mapdatastore.NewMapDataStore(mapdatastore.MapDataStoreOptions{ var dataStore datastore.DataStore
switch configuration.DataStore {
case config.DataStoreBadger:
dataStore = badgerdatastore.NewBadgerDataStore(badgerdatastore.BadgerDataStoreOptions{
InitialDataFilePath: configuration.InitialDataFilePath, InitialDataFilePath: configuration.InitialDataFilePath,
PersistDataFilePath: configuration.PersistDataFilePath, PersistDataFilePath: configuration.PersistDataFilePath,
}) })
default:
dataStore = jsondatastore.NewJsonDataStore(jsondatastore.JsonDataStoreOptions{
InitialDataFilePath: configuration.InitialDataFilePath,
PersistDataFilePath: configuration.PersistDataFilePath,
})
}
server := api.NewApiServer(dataStore, &configuration) server := api.NewApiServer(dataStore, &configuration)
err = server.Start() err = server.Start()
@@ -57,6 +68,7 @@ func StopServerInstance(serverName *C.char) int {
if serverInstance, ok := getInstance(serverNameStr); ok { if serverInstance, ok := getInstance(serverNameStr); ok {
serverInstance.server.Stop() serverInstance.server.Stop()
serverInstance.dataStore.Close()
removeInstance(serverNameStr) removeInstance(serverNameStr)
return ResponseSuccess return ResponseSuccess
} }
@@ -85,14 +97,14 @@ func LoadServerInstanceState(serverName *C.char, stateJSON *C.char) int {
stateJSONStr := C.GoString(stateJSON) stateJSONStr := C.GoString(stateJSON)
if serverInstance, ok := getInstance(serverNameStr); ok { if serverInstance, ok := getInstance(serverNameStr); ok {
if mapDS, ok := serverInstance.dataStore.(*mapdatastore.MapDataStore); ok { if jsonDS, ok := serverInstance.dataStore.(*jsondatastore.JsonDataStore); ok {
err := mapDS.LoadStateJSON(stateJSONStr) err := jsonDS.LoadStateJSON(stateJSONStr)
if err != nil { if err != nil {
return ResponseFailedToLoadState return ResponseFailedToLoadState
} }
return ResponseSuccess return ResponseSuccess
} }
return ResponseCurentDataStoreDoesNotSupportStateLoading return ResponseCurrentDataStoreDoesNotSupportStateLoading
} }
return ResponseServerInstanceNotFound return ResponseServerInstanceNotFound