mirror of
https://github.com/pikami/cosmium.git
synced 2026-11-21 01:42:15 +00:00
Compare commits
3
Commits
0.1.32
...
9f90f3f120
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9f90f3f120 | ||
|
|
2fddaf7498 | ||
|
|
af271092c3 |
No files matched your search
@@ -47,7 +47,7 @@ jobs:
|
||||
uses: goreleaser/goreleaser-action@5742e2a039330cbb23ebf35f046f814d4c6ff811 # v5
|
||||
with:
|
||||
distribution: goreleaser
|
||||
version: ${{ env.GITHUB_REF_NAME }}
|
||||
version: v2.20.0
|
||||
args: release --clean
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.PUBLISHER_TOKEN }}
|
||||
+5
-3
@@ -1,3 +1,5 @@
|
||||
version: 2
|
||||
|
||||
builds:
|
||||
- binary: cosmium
|
||||
main: ./cmd/server
|
||||
@@ -30,13 +32,13 @@ brews:
|
||||
|
||||
archives:
|
||||
- id: bundle
|
||||
format: tar.gz
|
||||
formats: [tar.gz]
|
||||
format_overrides:
|
||||
- goos: windows
|
||||
format: zip
|
||||
formats: [zip]
|
||||
- id: shared-libraries
|
||||
meta: true
|
||||
format: "tar.gz"
|
||||
formats: [tar.gz]
|
||||
wrap_in_directory: true
|
||||
name_template: "{{ .ProjectName }}_{{ .Version }}_shared-libraries"
|
||||
files:
|
||||
|
||||
@@ -67,7 +67,7 @@ func (h *Handlers) DeleteDocument(c *gin.Context) {
|
||||
collectionId := c.Param("collId")
|
||||
documentId := c.Param("docId")
|
||||
|
||||
status := h.dataStore.DeleteDocument(databaseId, collectionId, documentId)
|
||||
status := h.dataStore.DeleteDocumentIfMatch(databaseId, collectionId, documentId, c.GetHeader(headers.IfMatch))
|
||||
if status == datastore.StatusOk {
|
||||
c.Status(http.StatusNoContent)
|
||||
return
|
||||
@@ -78,6 +78,12 @@ func (h *Handlers) DeleteDocument(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if status == datastore.PreconditionFailed {
|
||||
c.Header(headers.ErrorCode, "PreconditionFailed")
|
||||
c.JSON(http.StatusPreconditionFailed, constants.PreconditionFailedResponse)
|
||||
return
|
||||
}
|
||||
|
||||
c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
|
||||
}
|
||||
|
||||
@@ -424,6 +430,8 @@ func dataStoreStatusToResponseCode(status datastore.DataStoreStatus) int {
|
||||
return http.StatusConflict
|
||||
case datastore.BadRequest:
|
||||
return http.StatusBadRequest
|
||||
case datastore.PreconditionFailed:
|
||||
return http.StatusPreconditionFailed
|
||||
default:
|
||||
return http.StatusInternalServerError
|
||||
}
|
||||
|
||||
@@ -670,3 +670,128 @@ func Test_Documents(t *testing.T) {
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Documents_Delete_IfMatch(t *testing.T) {
|
||||
presets := []testPreset{PresetJsonStore, PresetBadgerStore}
|
||||
|
||||
runTestsWithPresets(t, "Test_Documents_Delete_IfMatch", presets, func(t *testing.T, ts *TestServer, client *azcosmos.Client) {
|
||||
collectionClient := documents_InitializeDb(t, ts)
|
||||
|
||||
t.Run("Should fail delete with stale etag", func(t *testing.T) {
|
||||
ctx := context.TODO()
|
||||
|
||||
readResponse, err := collectionClient.ReadItem(ctx, azcosmos.PartitionKey{}, "12345", nil)
|
||||
assert.Nil(t, err)
|
||||
staleETag := readResponse.ETag
|
||||
assert.NotEmpty(t, staleETag)
|
||||
|
||||
item := map[string]interface{}{"id": "12345", "pk": "123", "isCool": true}
|
||||
bytes, err := json.Marshal(item)
|
||||
assert.Nil(t, err)
|
||||
|
||||
_, err = collectionClient.ReplaceItem(ctx, azcosmos.PartitionKey{}, "12345", bytes, nil)
|
||||
assert.Nil(t, err)
|
||||
|
||||
updated, err := collectionClient.ReadItem(ctx, azcosmos.PartitionKey{}, "12345", nil)
|
||||
assert.Nil(t, err)
|
||||
assert.NotEmpty(t, updated.ETag)
|
||||
assert.NotEqual(t, string(staleETag), string(updated.ETag))
|
||||
|
||||
_, err = collectionClient.DeleteItem(
|
||||
ctx,
|
||||
azcosmos.PartitionKey{},
|
||||
"12345",
|
||||
&azcosmos.ItemOptions{IfMatchEtag: &staleETag},
|
||||
)
|
||||
assert.NotNil(t, err)
|
||||
|
||||
var respErr *azcore.ResponseError
|
||||
if errors.As(err, &respErr) {
|
||||
assert.Equal(t, http.StatusPreconditionFailed, respErr.StatusCode)
|
||||
assert.Equal(t, "PreconditionFailed", respErr.RawResponse.Header.Get("x-ms-error-code"))
|
||||
|
||||
responseBody, readErr := io.ReadAll(respErr.RawResponse.Body)
|
||||
assert.Nil(t, readErr)
|
||||
assert.JSONEq(t,
|
||||
`{"code":"PreconditionFailed","message":"Operation cannot be performed because one of the specified precondition is not met."}`,
|
||||
string(responseBody),
|
||||
)
|
||||
} else {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
document, status := ts.DataStore.GetDocument(testDatabaseName, testCollectionName, "12345")
|
||||
assert.Equal(t, datastore.StatusOk, status)
|
||||
assert.Equal(t, true, document["isCool"])
|
||||
assert.Equal(t, string(updated.ETag), document["_etag"])
|
||||
})
|
||||
|
||||
t.Run("Should delete with matching etag", func(t *testing.T) {
|
||||
ctx := context.TODO()
|
||||
|
||||
readResponse, err := collectionClient.ReadItem(ctx, azcosmos.PartitionKey{}, "67890", nil)
|
||||
assert.Nil(t, err)
|
||||
etag := readResponse.ETag
|
||||
assert.NotEmpty(t, etag)
|
||||
|
||||
response, err := collectionClient.DeleteItem(
|
||||
ctx,
|
||||
azcosmos.PartitionKey{},
|
||||
"67890",
|
||||
&azcosmos.ItemOptions{IfMatchEtag: &etag},
|
||||
)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, http.StatusNoContent, response.RawResponse.StatusCode)
|
||||
|
||||
_, status := ts.DataStore.GetDocument(testDatabaseName, testCollectionName, "67890")
|
||||
assert.Equal(t, datastore.StatusNotFound, status)
|
||||
})
|
||||
|
||||
t.Run("Should delete unconditionally", func(t *testing.T) {
|
||||
ctx := context.TODO()
|
||||
_, status := ts.DataStore.CreateDocument(testDatabaseName, testCollectionName, map[string]interface{}{
|
||||
"id": "unconditional-delete", "pk": "pk", "isCool": false,
|
||||
})
|
||||
assert.Equal(t, datastore.StatusOk, status)
|
||||
|
||||
response, err := collectionClient.DeleteItem(ctx, azcosmos.PartitionKey{}, "unconditional-delete", nil)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, http.StatusNoContent, response.RawResponse.StatusCode)
|
||||
|
||||
_, status = ts.DataStore.GetDocument(testDatabaseName, testCollectionName, "unconditional-delete")
|
||||
assert.Equal(t, datastore.StatusNotFound, status)
|
||||
})
|
||||
|
||||
t.Run("Should return not found when deleting a missing document", func(t *testing.T) {
|
||||
_, err := collectionClient.DeleteItem(context.TODO(), azcosmos.PartitionKey{}, "missing-document", nil)
|
||||
assertDocumentDeleteNotFound(t, err)
|
||||
})
|
||||
|
||||
t.Run("Should return not found when deleting a missing document with if-match", func(t *testing.T) {
|
||||
etag := azcore.ETag("\"stale-etag\"")
|
||||
_, err := collectionClient.DeleteItem(
|
||||
context.TODO(),
|
||||
azcosmos.PartitionKey{},
|
||||
"missing-document-if-match",
|
||||
&azcosmos.ItemOptions{IfMatchEtag: &etag},
|
||||
)
|
||||
assertDocumentDeleteNotFound(t, err)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func assertDocumentDeleteNotFound(t *testing.T, err error) {
|
||||
t.Helper()
|
||||
assert.NotNil(t, err)
|
||||
|
||||
var respErr *azcore.ResponseError
|
||||
if errors.As(err, &respErr) {
|
||||
assert.Equal(t, http.StatusNotFound, respErr.StatusCode)
|
||||
responseBody, readErr := io.ReadAll(respErr.RawResponse.Body)
|
||||
assert.Nil(t, readErr)
|
||||
assert.JSONEq(t, `{"message":"NotFound"}`, string(responseBody))
|
||||
return
|
||||
}
|
||||
|
||||
panic(err)
|
||||
}
|
||||
@@ -64,20 +64,36 @@ func (r *BadgerDataStore) GetDocument(databaseId string, collectionId string, do
|
||||
}
|
||||
|
||||
func (r *BadgerDataStore) DeleteDocument(databaseId string, collectionId string, documentId string) datastore.DataStoreStatus {
|
||||
return r.deleteDocument(databaseId, collectionId, documentId, "")
|
||||
}
|
||||
|
||||
func (r *BadgerDataStore) DeleteDocumentIfMatch(databaseId string, collectionId string, documentId string, ifMatch string) datastore.DataStoreStatus {
|
||||
return r.deleteDocument(databaseId, collectionId, documentId, ifMatch)
|
||||
}
|
||||
|
||||
func (r *BadgerDataStore) deleteDocument(databaseId string, collectionId string, documentId string, ifMatch 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 {
|
||||
var document datastore.Document
|
||||
status := getKey(txn, documentKey, &document)
|
||||
if status == datastore.StatusNotFound {
|
||||
return datastore.StatusNotFound
|
||||
}
|
||||
if status != datastore.StatusOk {
|
||||
return status
|
||||
}
|
||||
|
||||
err = txn.Delete([]byte(documentKey))
|
||||
if ifMatch != "" {
|
||||
etag, _ := document["_etag"].(string)
|
||||
if etag != ifMatch {
|
||||
return datastore.PreconditionFailed
|
||||
}
|
||||
}
|
||||
|
||||
err := txn.Delete([]byte(documentKey))
|
||||
if err != nil {
|
||||
logger.ErrorLn("Error while deleting document:", err)
|
||||
return datastore.Unknown
|
||||
|
||||
@@ -15,6 +15,11 @@ type DataStore interface {
|
||||
GetDocumentIterator(databaseId string, collectionId string) (DocumentIterator, DataStoreStatus)
|
||||
GetDocument(databaseId string, collectionId string, documentId string) (Document, DataStoreStatus)
|
||||
DeleteDocument(databaseId string, collectionId string, documentId string) DataStoreStatus
|
||||
// DeleteDocumentIfMatch removes a document when ifMatch is empty or equals its _etag.
|
||||
// The comparison and delete happen together, so a concurrent update cannot land between them.
|
||||
// A mismatched etag returns PreconditionFailed and leaves the document in place.
|
||||
// A missing document returns StatusNotFound.
|
||||
DeleteDocumentIfMatch(databaseId string, collectionId string, documentId string, ifMatch string) DataStoreStatus
|
||||
CreateDocument(databaseId string, collectionId string, document map[string]interface{}) (Document, DataStoreStatus)
|
||||
|
||||
GetAllTriggers(databaseId string, collectionId string) ([]Trigger, DataStoreStatus)
|
||||
|
||||
@@ -45,6 +45,14 @@ func (r *JsonDataStore) GetDocument(databaseId string, collectionId string, docu
|
||||
}
|
||||
|
||||
func (r *JsonDataStore) DeleteDocument(databaseId string, collectionId string, documentId string) datastore.DataStoreStatus {
|
||||
return r.deleteDocument(databaseId, collectionId, documentId, "")
|
||||
}
|
||||
|
||||
func (r *JsonDataStore) DeleteDocumentIfMatch(databaseId string, collectionId string, documentId string, ifMatch string) datastore.DataStoreStatus {
|
||||
return r.deleteDocument(databaseId, collectionId, documentId, ifMatch)
|
||||
}
|
||||
|
||||
func (r *JsonDataStore) deleteDocument(databaseId string, collectionId string, documentId string, ifMatch string) datastore.DataStoreStatus {
|
||||
r.storeState.Lock()
|
||||
defer r.storeState.Unlock()
|
||||
|
||||
@@ -56,10 +64,18 @@ func (r *JsonDataStore) DeleteDocument(databaseId string, collectionId string, d
|
||||
return datastore.StatusNotFound
|
||||
}
|
||||
|
||||
if _, ok := r.storeState.Documents[databaseId][collectionId][documentId]; !ok {
|
||||
document, ok := r.storeState.Documents[databaseId][collectionId][documentId]
|
||||
if !ok {
|
||||
return datastore.StatusNotFound
|
||||
}
|
||||
|
||||
if ifMatch != "" {
|
||||
etag, _ := document["_etag"].(string)
|
||||
if etag != ifMatch {
|
||||
return datastore.PreconditionFailed
|
||||
}
|
||||
}
|
||||
|
||||
delete(r.storeState.Documents[databaseId][collectionId], documentId)
|
||||
|
||||
return datastore.StatusOk
|
||||
|
||||
@@ -17,6 +17,7 @@ const (
|
||||
BadRequest DataStoreStatus = 4
|
||||
IterEOF DataStoreStatus = 5
|
||||
Unknown DataStoreStatus = 6
|
||||
PreconditionFailed DataStoreStatus = 7
|
||||
)
|
||||
|
||||
type TriggerOperation string
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package memoryexecutor
|
||||
|
||||
import (
|
||||
"math"
|
||||
"strings"
|
||||
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
)
|
||||
@@ -44,57 +44,72 @@ func (r rowContext) aggregate_Count(arguments []interface{}) interface{} {
|
||||
}
|
||||
|
||||
func (r rowContext) aggregate_Max(arguments []interface{}) interface{} {
|
||||
selectExpression := arguments[0].(parsers.SelectItem)
|
||||
max := 0.0
|
||||
count := 0
|
||||
|
||||
for _, item := range r.grouppedRows {
|
||||
value := item.resolveSelectItem(selectExpression)
|
||||
if numericValue, ok := value.(float64); ok {
|
||||
if numericValue > max {
|
||||
max = numericValue
|
||||
}
|
||||
count++
|
||||
} else if numericValue, ok := value.(int); ok {
|
||||
if float64(numericValue) > max {
|
||||
max = float64(numericValue)
|
||||
}
|
||||
count++
|
||||
}
|
||||
}
|
||||
|
||||
if count > 0 {
|
||||
return max
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
return r.aggregateMinMax(arguments, true)
|
||||
}
|
||||
|
||||
func (r rowContext) aggregate_Min(arguments []interface{}) interface{} {
|
||||
return r.aggregateMinMax(arguments, false)
|
||||
}
|
||||
|
||||
func (r rowContext) aggregateMinMax(arguments []interface{}, isMax bool) interface{} {
|
||||
selectExpression := arguments[0].(parsers.SelectItem)
|
||||
min := math.MaxFloat64
|
||||
count := 0
|
||||
var extremeNumber float64
|
||||
hasNumber := false
|
||||
var extremeString string
|
||||
hasString := false
|
||||
|
||||
for _, item := range r.grouppedRows {
|
||||
value := item.resolveSelectItem(selectExpression)
|
||||
if numericValue, ok := value.(float64); ok {
|
||||
if numericValue < min {
|
||||
min = numericValue
|
||||
|
||||
if numericValue, ok := aggregateNumber(value); ok {
|
||||
if !hasNumber || numberIsExtreme(numericValue, extremeNumber, isMax) {
|
||||
extremeNumber = numericValue
|
||||
hasNumber = true
|
||||
}
|
||||
count++
|
||||
} else if numericValue, ok := value.(int); ok {
|
||||
if float64(numericValue) < min {
|
||||
min = float64(numericValue)
|
||||
continue
|
||||
}
|
||||
|
||||
if strValue, ok := value.(string); ok {
|
||||
if !hasString || stringIsExtreme(strValue, extremeString, isMax) {
|
||||
extremeString = strValue
|
||||
hasString = true
|
||||
}
|
||||
count++
|
||||
}
|
||||
}
|
||||
|
||||
if count > 0 {
|
||||
return min
|
||||
} else {
|
||||
return nil
|
||||
if hasNumber {
|
||||
return extremeNumber
|
||||
}
|
||||
if hasString {
|
||||
return extremeString
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func aggregateNumber(value interface{}) (float64, bool) {
|
||||
switch numericValue := value.(type) {
|
||||
case float64:
|
||||
return numericValue, true
|
||||
case int:
|
||||
return float64(numericValue), true
|
||||
default:
|
||||
return 0, false
|
||||
}
|
||||
}
|
||||
|
||||
func numberIsExtreme(candidate, current float64, isMax bool) bool {
|
||||
if isMax {
|
||||
return candidate > current
|
||||
}
|
||||
return candidate < current
|
||||
}
|
||||
|
||||
func stringIsExtreme(candidate, current string, isMax bool) bool {
|
||||
comparison := strings.Compare(candidate, current)
|
||||
if isMax {
|
||||
return comparison > 0
|
||||
}
|
||||
return comparison < 0
|
||||
}
|
||||
|
||||
func (r rowContext) aggregate_Sum(arguments []interface{}) interface{} {
|
||||
|
||||
@@ -176,6 +176,94 @@ func Test_Execute_AggregateFunctions(t *testing.T) {
|
||||
)
|
||||
})
|
||||
|
||||
mixedTypeData := []memoryexecutor.RowType{
|
||||
map[string]interface{}{"key": "numbers", "value": 10},
|
||||
map[string]interface{}{"key": "numbers", "value": "zebra"},
|
||||
map[string]interface{}{"key": "numbers", "value": 2},
|
||||
map[string]interface{}{"key": "strings", "value": "mango"},
|
||||
map[string]interface{}{"key": "strings", "value": "apple"},
|
||||
map[string]interface{}{"key": "strings", "value": true},
|
||||
map[string]interface{}{"key": "empty", "value": "b"},
|
||||
map[string]interface{}{"key": "empty", "value": ""},
|
||||
map[string]interface{}{"key": "single", "value": "zebra"},
|
||||
map[string]interface{}{"key": "single", "value": 5},
|
||||
map[string]interface{}{"key": "single", "value": "apple"},
|
||||
map[string]interface{}{"key": "other", "value": true},
|
||||
map[string]interface{}{"key": "other", "flag": false},
|
||||
}
|
||||
|
||||
t.Run("Should execute function MAX() on strings and mixed types", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "key"}},
|
||||
{
|
||||
Alias: "max",
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallAggregateMax,
|
||||
Arguments: []interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
GroupBy: []parsers.SelectItem{
|
||||
{Path: []string{"c", "key"}},
|
||||
},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
mixedTypeData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"key": "numbers", "max": 10.0},
|
||||
map[string]interface{}{"key": "strings", "max": "mango"},
|
||||
map[string]interface{}{"key": "empty", "max": "b"},
|
||||
map[string]interface{}{"key": "single", "max": 5.0},
|
||||
map[string]interface{}{"key": "other", "max": nil},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute function MIN() on strings and mixed types", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "key"}},
|
||||
{
|
||||
Alias: "min",
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallAggregateMin,
|
||||
Arguments: []interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
GroupBy: []parsers.SelectItem{
|
||||
{Path: []string{"c", "key"}},
|
||||
},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
mixedTypeData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"key": "numbers", "min": 2.0},
|
||||
map[string]interface{}{"key": "strings", "min": "apple"},
|
||||
map[string]interface{}{"key": "empty", "min": ""},
|
||||
map[string]interface{}{"key": "single", "min": 5.0},
|
||||
map[string]interface{}{"key": "other", "min": nil},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute function SUM()", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
|
||||
Reference in new issue
Block a user