Compare commits

...

114 Commits

Author SHA1 Message Date
Pijus Kamandulis 90de21c7b0 1.26.x (#17)
* Upgrade to go 1.26.3

* Update dependencies

* Update actions
2026-06-11 23:27:46 +03:00
Pijus Kamandulis 7c9c8ec9dc Update packages 2026-06-11 22:58:05 +03:00
Pijus Kamandulis be761badae Initial RNTBD server implementation 2026-06-11 22:43:05 +03:00
Pijus Kamandulis 36fd7f48cc Add document ETag optimistic concurrency (#16)
* Add ETag optimistic concurrency for document replace

Co-authored-by: Pijus Kamandulis <pikami@users.noreply.github.com>

* Expose precondition error code header

Co-authored-by: Pijus Kamandulis <pikami@users.noreply.github.com>

* Stop Badger GC before closing datastore

Co-authored-by: Pijus Kamandulis <pikami@users.noreply.github.com>

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
2026-06-05 23:26:25 +03:00
Pijus Kamandulis 05e8cd2842 Implement REGEXMATCH function (#15)
Co-authored-by: Cursor Agent <cursoragent@cursor.com>
2026-05-30 21:31:45 +03:00
Pijus Kamandulis c3726a6633 Fix ARRAY_CONTAINS panic when optional partial-match argument is omitted (#14)
* Fix ARRAY_CONTAINS panic when partial match arg is omitted

The NoSQL parser always emits a third (nil) argument for the optional
partial-match flag of ARRAY_CONTAINS. The executor checked only
len(arguments) > 2 before type-asserting arguments[2] to
parsers.SelectItem, which panicked on the nil value whenever the query
omitted the partial-match argument (e.g. ARRAY_CONTAINS(c.arr, 2)).

Guard the type assertion with a nil check and add an API test covering
ARRAY_CONTAINS with and without the optional partial-match argument.

Co-authored-by: Pijus Kamandulis <pikami@users.noreply.github.com>

* Remove comments from ARRAY_CONTAINS API test

Co-authored-by: Pijus Kamandulis <pikami@users.noreply.github.com>

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
2026-05-30 17:43:05 +03:00
Pijus Kamandulis d76cc88175 Fix 'NOT (bool)' statements 2026-04-04 15:04:42 +03:00
Pijus Kamandulis 3daba9d0eb Update dependencies 2026-01-29 21:47:35 +02:00
Pijus Kamandulis d3d238fa98 Implement continuation tokens 2026-01-29 21:45:46 +02:00
Pijus Kamandulis cae6fda95c Added cross-platform shared library tests 2025-11-27 21:26:54 +02:00
Pijus Kamandulis 46c446c273 Update dependencies 2025-11-27 00:28:37 +02:00
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
Pijus Kamandulis e526b2269e Refactored query engine utilizing iterators 2025-03-11 17:36:28 +02:00
Pijus Kamandulis 221f029a1d DataStore is interface now. Liskov would be proud. 2025-03-09 18:34:07 +02:00
Pijus Kamandulis bd4fe5abec Update azcosmos package 2025-02-25 20:43:23 +02:00
Pijus Kamandulis f062e03f0c Update packages 2025-02-25 19:56:02 +02:00
Pijus Kamandulis 058b3271b7 OrderBy should bring NULL values to front 2025-02-25 19:47:29 +02:00
Pijus Kamandulis 1711c8fb5c Implement NOT logical operator 2025-02-25 19:33:32 +02:00
Pijus Kamandulis 851b3ca3a8 Fix IN clause with function calls 2025-02-20 18:45:20 +02:00
Pijus Kamandulis d27c633e1d Better handling when passing null to string functions 2025-02-18 20:11:11 +02:00
Pijus Kamandulis 3987df89c0 Upgrade to golang 1.24.0 2025-02-18 19:16:21 +02:00
Pijus Kamandulis 6e3f4169a1 Fix 'ComparisonOperator' parsing 2025-02-18 19:12:08 +02:00
Pijus Kamandulis 14c5400d23 Keep old explorer images tagged with version 2025-02-09 22:42:51 +02:00
Pijus Kamandulis 1cf5ae92f4 Shared library stability improvements 2025-02-09 11:45:10 +02:00
Pijus Kamandulis 5d99b653cc Generate more realistic resource ids 2025-02-09 00:36:35 +02:00
Pijus Kamandulis 787cdb33cf Fix OFFSET clause 2025-02-08 15:28:06 +02:00
Pijus Kamandulis 5caa829ac1 Implement 'Transactional batch operations' 2025-02-04 20:35:15 +02:00
Pijus Kamandulis 887d456ad4 Return error code if server fails to start 2025-02-03 22:58:45 +02:00
Pijus Kamandulis da1566875b Wait for server shutdown when stopping server 2025-02-03 22:21:54 +02:00
Pijus Kamandulis 3fee3bc816 Fix ARRAY_CONTAINS partial matches for nested objects 2025-02-03 19:29:29 +02:00
Pijus Kamandulis 8657c48fc8 Added support for table alias; Make AS keyword optional #9 2025-02-03 19:02:12 +02:00
Pijus Kamandulis e080888c20 Add shared-libraries to release 2025-01-28 23:28:23 +02:00
Pijus Kamandulis b8d79fd945 Upgrade upload-artifact pipeline action 2025-01-28 21:31:05 +02:00
Pijus Kamandulis f25cb7fb03 Stamp binaries with version control information 2025-01-28 21:15:49 +02:00
Pijus Kamandulis 125f10d8a2 Add more error handling and mutex guards 2025-01-27 21:09:37 +02:00
Pijus Kamandulis d6b816b55a Fix docker tag 2025-01-25 21:17:32 +02:00
Pijus Kamandulis 12215fba76 Update dependancies 2025-01-25 21:11:42 +02:00
Pijus Kamandulis a1793c17ab Added docker image with explorer included 2025-01-25 20:20:55 +02:00
Pijus Kamandulis 96d3a0a7ae Handle server close; Update logger 2025-01-15 00:26:15 +02:00
Pijus Kamandulis 8b8b087aab Added setting for LogLevel 2025-01-09 21:07:41 +02:00
Pijus Kamandulis c2c9dc03b3 Fixed issue with wrong signature generation for pkrange requests 2025-01-09 20:19:28 +02:00
Pijus Kamandulis d86bac7d79 Implement CRUD for UDFs, SPs and Triggers 2025-01-06 20:45:43 +02:00
Pijus Kamandulis 69b76c1c3e Simplify constant initialization in unit tests 2024-12-26 20:43:57 +02:00
Pijus Kamandulis 8e3db3e44d Added support for 'ARRAY_CONTAINS', 'ARRAY_CONTAINS_ANY' and 'ARRAY_CONTAINS_ALL' functions 2024-12-26 20:27:59 +02:00
Pijus Kamandulis f5b8453995 Support patch operations 'set' and 'incr' #7 2024-12-25 23:32:50 +02:00
Pijus Kamandulis 928ca29fe4 Support parameter in bracket #8 2024-12-25 21:28:42 +02:00
Pijus Kamandulis 39cd9e2357 Update dependancies 2024-12-20 20:27:42 +02:00
Pijus Kamandulis bcf4b513b6 Expose repository functions to sharedlibs 2024-12-20 20:25:32 +02:00
Pijus Kamandulis 363f822e5a Added some tests for sharedlibrary 2024-12-19 23:21:45 +02:00
Pijus Kamandulis be7a615931 Cross-Compile Shared Libraries 2024-12-19 00:48:17 +02:00
Pijus Kamandulis 83f086a2dc Configuration fixes 2024-12-18 23:28:04 +02:00
Pijus Kamandulis 777034181f Refactor to support multiple server instances in shared library 2024-12-18 19:39:57 +02:00
Pijus Kamandulis 84c33e3c8e Upgrade dependancies 2024-12-18 00:34:10 +02:00
Pijus Kamandulis 5e677431a3 Prepare for sharedlibrary builds 2024-12-18 00:28:59 +02:00
Pijus Kamandulis a4659d90a9 Enable multi-platform docker builds 2024-12-08 18:55:20 +02:00
Pijus Kamandulis 503e6bb8ad Update compatibility matrix 2024-12-08 18:17:37 +02:00
Pijus Kamandulis e5ddc143f0 Improved concurrency handling 2024-12-08 17:54:58 +02:00
Pijus Kamandulis 66ea859f34 Add support for subqueries 2024-12-07 22:29:26 +02:00
Pijus Kamandulis 3584f9b5ce Enable ARM builds for Windows and Linux 2024-11-16 20:09:24 +02:00
Pijus Kamandulis c7d01b4593 Fix cosmos explorer incorrect redirect 2024-11-14 18:42:17 +02:00
erikzeneco 2834f3f641 check isUpsert header in POST document request (#5)
* check isUpsert header in POST document request

* Verify response code on "CreateItem that already exists" test

---------

Co-authored-by: Pijus Kamandulis <pikami@users.noreply.github.com>
2024-11-01 21:11:59 +02:00
Pijus Kamandulis a6b5d32ff7 Merge pull request #4 from pikami/erikzeneco/serve_request_paths_with_trailing_slashes
Erikzeneco/serve request paths with trailing slashes
2024-10-29 18:06:56 +02:00
Pijus Kamandulis 0e98e3481a Strip trailing slash using middleware 2024-10-28 20:20:52 +02:00
Erik Zentveld 827046f634 re-add removed blank lines 2024-10-28 16:18:49 +01:00
Erik Zentveld 475d586dc5 Merge branch 'master' into serve_request_paths_with_trailing_slashes 2024-10-28 14:37:01 +01:00
Erik Zentveld 9abef691d6 serve request paths with trailing slashes, as sent by python client 2024-10-28 13:29:26 +01:00
Pijus Kamandulis 62dcbc1f2b Merge pull request #1 from erikzeneco/master
Update README.md
2024-10-16 18:34:14 +03:00
erikzeneco 2f42651fb7 Update README.md
Use envPrefix for parameter passed in as environment variable with docker.
2024-10-16 16:24:53 +02:00
Pijus Kamandulis 20af73ee9c Partial JOIN implementation 2024-07-17 21:56:17 +03:00
Pijus Kamandulis 3bdff9b643 Implement Mathematical Functions 2024-06-19 00:44:46 +03:00
Pijus Kamandulis b808e97c72 Fix array access 2024-06-03 19:00:52 +03:00
Pijus Kamandulis e623a563f4 Update dependencies 2024-06-01 19:55:06 +03:00
Pijus Kamandulis 2cd61aa620 Implement document PATCH operation 2024-06-01 19:52:07 +03:00
Pijus Kamandulis 0cec7816c1 Fixed authentication key generation for partition key ranges
Fixed collection rid generation

Improved compatibility with SDKs
2024-06-01 02:32:52 +03:00
Pijus Kamandulis ec5f82cd54 Fix docker tags 2024-04-06 19:14:37 +03:00
Pijus Kamandulis 26dcd68ace Added Docker to releaser configuration 2024-04-06 19:07:14 +03:00
Pijus Kamandulis 86c0275410 Added ability to configure using environment variables 2024-03-11 23:35:47 +02:00
Pijus Kamandulis 398584368f Implement OFFSET LIMIT 2024-03-11 22:09:55 +02:00
Pijus Kamandulis 5b66828bd0 Added some docs 2024-03-11 20:47:44 +02:00
Pijus Kamandulis 6ed74688ca Implement AVG, COUNT, MAX, MIN, SUM functions 2024-03-11 19:10:41 +02:00
Pijus Kamandulis b72bba86c8 Implement 'GROUP BY' statement 2024-03-11 17:50:20 +02:00
Pijus Kamandulis 18edb925bf Added instructions for installing using Homebrew 2024-02-27 22:46:13 +02:00
Pijus Kamandulis 6ccb7c4bdd Implement custom logger with log levels 2024-02-27 22:38:59 +02:00
Pijus Kamandulis b9e38575bc Load state from '-Persist' path if '-InitialData' not supplied 2024-02-27 22:11:33 +02:00
Pijus Kamandulis 3aeae98404 Added pre-generated TLS certificate 2024-02-27 21:58:57 +02:00
Pijus Kamandulis 5ff923ce2c Implement DISTINCT clause 2024-02-27 21:10:03 +02:00
Pijus Kamandulis f3f3966dd5 Fix server info response 2024-02-27 20:27:51 +02:00
Pijus Kamandulis 19f62f8173 Fix partition key ranges endpoint 2024-02-27 20:08:48 +02:00
Pijus Kamandulis d426dc23c0 Added release workflow 2024-02-26 22:49:56 +02:00
Pijus Kamandulis 1158f93102 Generate legit ResourceIds for SDK compatibility 2024-02-26 21:03:47 +02:00
155 changed files with 21206 additions and 3322 deletions
+14
View File
@@ -0,0 +1,14 @@
#### Summary
Bug report in one concise sentence
#### Steps to reproduce
How can we reproduce the issue (what version are you using?)
#### Expected behavior
Describe your issue in detail
#### Observed behavior (that appears unintentional)
What did you see happen? Please include relevant error messages.
#### Possible fixes
If you can, link to the line of code that might be responsible for the problem
+18
View File
@@ -0,0 +1,18 @@
<!-- Thank you for contributing a pull request! Here are a few tips to help you:
1. If applicable, please check if unit tests are added for new features
2. Read the contribution guide lines https://github.com/pikami/cosmium/docs/CONTRIBUTING.md
-->
#### Summary
<!--
A description of what this pull request does, as well as QA test steps (if applicable).
-->
#### Ticket Link
<!--
If applicable, please include a link to the GitHub issue:
Fixes https://github.com/pikami/cosmium/issues/XXX
-->
@@ -0,0 +1,89 @@
name: Cross-Compile Shared Libraries
on:
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
outputs:
artifact: shared-libraries
steps:
- name: Checkout code
uses: actions/checkout@v6
- name: Cross-Compile with xgo
uses: crazy-max/ghaction-xgo@de82f877ff4552f03b66c146f608233849e9c3dc # v4.0.0
with:
xgo_version: latest
go_version: 1.26.3
dest: dist
pkg: sharedlibrary
prefix: cosmium
targets: linux/amd64,linux/arm64,windows/amd64,windows/arm64,darwin/amd64,darwin/arm64
v: true
buildmode: c-shared
buildvcs: true
- name: Upload artifact
uses: actions/upload-artifact@v7
with:
name: shared-libraries
path: dist/*
test:
needs: build
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
include:
- os: ubuntu-latest
lib_ext: amd64.so
- os: windows-latest
lib_ext: amd64.dll
- os: macos-latest
lib_ext: arm64.dylib
steps:
- uses: actions/checkout@v6
- name: Download shared libraries
uses: actions/download-artifact@v8
with:
name: shared-libraries
path: libs
- name: Install MinGW (GCC) on Windows
if: runner.os == 'Windows'
run: choco install mingw --no-progress
- name: Build test loader (Windows)
if: runner.os == 'Windows'
run: |
mkdir build
gcc -Wall -o build/test_loader.exe sharedlibrary/tests/*.c
- name: Build test loader (Unix)
if: runner.os != 'Windows'
run: |
mkdir build
gcc -Wall -ldl -o build/test_loader sharedlibrary/tests/*.c
- name: Run test (Unix)
if: runner.os != 'Windows'
run: |
LIB=$(ls libs/*${{ matrix.lib_ext }} | head -n 1)
echo "Testing library: $LIB"
chmod +x build/test_loader
./build/test_loader "$LIB"
- name: Run test (Windows)
if: runner.os == 'Windows'
shell: pwsh
run: |
$lib = Get-ChildItem "libs/*${{ matrix.lib_ext }}" | Select-Object -First 1
Write-Host "Testing library: $($lib.FullName)"
.\build\test_loader.exe $lib.FullName
+53
View File
@@ -0,0 +1,53 @@
name: goreleaser
on:
push:
tags:
- "*"
permissions:
contents: write
packages: write
jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v6
with:
fetch-depth: 0
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: 1.26.3
- name: Cross-Compile with xgo
uses: crazy-max/ghaction-xgo@de82f877ff4552f03b66c146f608233849e9c3dc # v4.0.0
with:
xgo_version: latest
go_version: 1.26.3
dest: sharedlibrary_dist
pkg: sharedlibrary
prefix: cosmium
targets: linux/amd64,linux/arm64,windows/amd64,windows/arm64,darwin/amd64,darwin/arm64
v: true
buildmode: c-shared
buildvcs: true
- name: Docker Login
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@5742e2a039330cbb23ebf35f046f814d4c6ff811 # v5
with:
distribution: goreleaser
version: ${{ env.GITHUB_REF_NAME }}
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.PUBLISHER_TOKEN }}
+5
View File
@@ -1,2 +1,7 @@
dist/ dist/
sharedlibrary_dist/
ignored/ ignored/
explorer_www/
main
save.json
.vscode/
+146
View File
@@ -0,0 +1,146 @@
builds:
- binary: cosmium
main: ./cmd/server
goos:
- darwin
- linux
- windows
goarch:
- amd64
- arm64
env:
- CGO_ENABLED=0
release:
prerelease: auto
universal_binaries:
- replace: true
brews:
- name: cosmium
homepage: 'https://github.com/pikami/cosmium'
repository:
owner: pikami
name: homebrew-brew
commit_author:
name: pikami
email: git@pikami.org
skip_upload: auto
archives:
- id: bundle
format: tar.gz
format_overrides:
- goos: windows
format: zip
- id: shared-libraries
meta: true
format: "tar.gz"
wrap_in_directory: true
name_template: "{{ .ProjectName }}_{{ .Version }}_shared-libraries"
files:
- LICENSE
- README.md
- sharedlibrary_dist/**
dockers:
- id: docker-linux-amd64
goos: linux
goarch: amd64
image_templates:
- "ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}-amd64"
dockerfile: Dockerfile
use: buildx
build_flag_templates:
- "--platform=linux/amd64"
- "--pull"
- "--label=org.opencontainers.image.title={{.ProjectName}}"
- "--label=org.opencontainers.image.description=Lightweight Cosmos DB emulator"
- "--label=org.opencontainers.image.url=https://github.com/pikami/cosmium"
- "--label=org.opencontainers.image.source=https://github.com/pikami/cosmium"
- "--label=org.opencontainers.image.created={{.Date}}"
- "--label=org.opencontainers.image.revision={{.FullCommit}}"
- "--label=org.opencontainers.image.version={{.Version}}"
- id: docker-linux-arm64
goos: linux
goarch: arm64
image_templates:
- "ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}-arm64"
- "ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}-arm64v8"
dockerfile: Dockerfile
use: buildx
build_flag_templates:
- "--platform=linux/arm64"
- "--pull"
- "--label=org.opencontainers.image.title={{.ProjectName}}"
- "--label=org.opencontainers.image.description=Lightweight Cosmos DB emulator"
- "--label=org.opencontainers.image.url=https://github.com/pikami/cosmium"
- "--label=org.opencontainers.image.source=https://github.com/pikami/cosmium"
- "--label=org.opencontainers.image.created={{.Date}}"
- "--label=org.opencontainers.image.revision={{.FullCommit}}"
- "--label=org.opencontainers.image.version={{.Version}}"
- id: docker-explorer-linux-amd64
goos: linux
goarch: amd64
image_templates:
- "ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}-explorer-amd64"
dockerfile: Explorer.Dockerfile
use: buildx
build_flag_templates:
- "--platform=linux/amd64"
- "--pull"
- "--label=org.opencontainers.image.title={{.ProjectName}}"
- "--label=org.opencontainers.image.description=Lightweight Cosmos DB emulator"
- "--label=org.opencontainers.image.url=https://github.com/pikami/cosmium"
- "--label=org.opencontainers.image.source=https://github.com/pikami/cosmium"
- "--label=org.opencontainers.image.created={{.Date}}"
- "--label=org.opencontainers.image.revision={{.FullCommit}}"
- "--label=org.opencontainers.image.version={{.Version}}"
- id: docker-explorer-linux-arm64
goos: linux
goarch: arm64
image_templates:
- "ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}-explorer-arm64"
- "ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}-explorer-arm64v8"
dockerfile: Explorer.Dockerfile
use: buildx
build_flag_templates:
- "--platform=linux/arm64"
- "--pull"
- "--label=org.opencontainers.image.title={{.ProjectName}}"
- "--label=org.opencontainers.image.description=Lightweight Cosmos DB emulator"
- "--label=org.opencontainers.image.url=https://github.com/pikami/cosmium"
- "--label=org.opencontainers.image.source=https://github.com/pikami/cosmium"
- "--label=org.opencontainers.image.created={{.Date}}"
- "--label=org.opencontainers.image.revision={{.FullCommit}}"
- "--label=org.opencontainers.image.version={{.Version}}"
docker_manifests:
- name_template: 'ghcr.io/pikami/{{ .ProjectName }}:latest'
skip_push: auto
image_templates:
- "ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}-amd64"
- "ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}-arm64"
- "ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}-arm64v8"
- name_template: 'ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}'
skip_push: auto
image_templates:
- "ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}-amd64"
- "ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}-arm64"
- "ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}-arm64v8"
- name_template: 'ghcr.io/pikami/{{ .ProjectName }}:explorer'
skip_push: auto
image_templates:
- "ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}-explorer-amd64"
- "ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}-explorer-arm64"
- "ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}-explorer-arm64v8"
- name_template: 'ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}-explorer'
skip_push: auto
image_templates:
- "ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}-explorer-amd64"
- "ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}-explorer-arm64"
- "ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}-explorer-arm64v8"
checksum:
name_template: 'checksums.txt'
+6
View File
@@ -0,0 +1,6 @@
FROM alpine:latest
WORKDIR /app
COPY cosmium /app/cosmium
ENTRYPOINT ["/app/cosmium"]
+9
View File
@@ -0,0 +1,9 @@
FROM ghcr.io/cosmiumdev/cosmos-explorer-base:latest AS explorer-base
FROM alpine:latest
COPY --from=explorer-base /cosmos-explorer /cosmos-explorer
WORKDIR /app
COPY cosmium /app/cosmium
ENTRYPOINT ["/app/cosmium", "-ExplorerDir", "/cosmos-explorer"]
+46 -5
View File
@@ -4,28 +4,69 @@ GOTEST=$(GOCMD) test
GOCLEAN=$(GOCMD) clean GOCLEAN=$(GOCMD) clean
BINARY_NAME=cosmium BINARY_NAME=cosmium
SERVER_LOCATION=./cmd/server
SHARED_LIB_LOCATION=./sharedlibrary
SHARED_LIB_OPT=-buildmode=c-shared
XGO_TARGETS=linux/amd64,linux/arm64,windows/amd64,windows/arm64,darwin/amd64,darwin/arm64
GOVERSION=1.26.3
DIST_DIR=dist DIST_DIR=dist
SHARED_LIB_TEST_CC=gcc
SHARED_LIB_TEST_CFLAGS=-Wall -ldl
SHARED_LIB_TEST_TARGET=$(DIST_DIR)/sharedlibrary_test
SHARED_LIB_TEST_DIR=./sharedlibrary/tests
SHARED_LIB_TEST_SOURCES=$(wildcard $(SHARED_LIB_TEST_DIR)/*.c)
all: test build-all all: test build-all
build-all: build-darwin-arm64 build-darwin-amd64 build-linux-amd64 build-windows-amd64 build-all: build-darwin-arm64 build-darwin-amd64 build-linux-amd64 build-linux-arm64 build-windows-amd64 build-windows-arm64
build-darwin-arm64: build-darwin-arm64:
@echo "Building macOS ARM binary..." @echo "Building macOS ARM binary..."
@GOOS=darwin GOARCH=arm64 $(GOBUILD) -o $(DIST_DIR)/$(BINARY_NAME)-darwin-arm64 . @GOOS=darwin GOARCH=arm64 $(GOBUILD) -o $(DIST_DIR)/$(BINARY_NAME)-darwin-arm64 $(SERVER_LOCATION)
build-darwin-amd64: build-darwin-amd64:
@echo "Building macOS x64 binary..." @echo "Building macOS x64 binary..."
@GOOS=darwin GOARCH=amd64 $(GOBUILD) -o $(DIST_DIR)/$(BINARY_NAME)-darwin-amd64 . @GOOS=darwin GOARCH=amd64 $(GOBUILD) -o $(DIST_DIR)/$(BINARY_NAME)-darwin-amd64 $(SERVER_LOCATION)
build-linux-amd64: build-linux-amd64:
@echo "Building Linux x64 binary..." @echo "Building Linux x64 binary..."
@GOOS=linux GOARCH=amd64 $(GOBUILD) -o $(DIST_DIR)/$(BINARY_NAME)-linux-amd64 . @GOOS=linux GOARCH=amd64 $(GOBUILD) -o $(DIST_DIR)/$(BINARY_NAME)-linux-amd64 $(SERVER_LOCATION)
build-linux-arm64:
@echo "Building Linux ARM binary..."
@GOOS=linux GOARCH=arm64 $(GOBUILD) -o $(DIST_DIR)/$(BINARY_NAME)-linux-arm64 $(SERVER_LOCATION)
build-windows-amd64: build-windows-amd64:
@echo "Building Windows x64 binary..." @echo "Building Windows x64 binary..."
@GOOS=windows GOARCH=amd64 $(GOBUILD) -o $(DIST_DIR)/$(BINARY_NAME)-windows-amd64.exe . @GOOS=windows GOARCH=amd64 $(GOBUILD) -o $(DIST_DIR)/$(BINARY_NAME)-windows-amd64.exe $(SERVER_LOCATION)
build-windows-arm64:
@echo "Building Windows ARM binary..."
@GOOS=windows GOARCH=arm64 $(GOBUILD) -o $(DIST_DIR)/$(BINARY_NAME)-windows-arm64.exe $(SERVER_LOCATION)
build-sharedlib-linux-amd64:
@echo "Building shared library for Linux x64..."
@GOOS=linux GOARCH=amd64 $(GOBUILD) $(SHARED_LIB_OPT) -o $(DIST_DIR)/$(BINARY_NAME)-linux-amd64.so $(SHARED_LIB_LOCATION)
build-sharedlib-darwin-arm64:
@echo "Building shared library for macOS ARM..."
@GOOS=darwin GOARCH=arm64 $(GOBUILD) $(SHARED_LIB_OPT) -o $(DIST_DIR)/$(BINARY_NAME)-darwin-arm64.so $(SHARED_LIB_LOCATION)
build-sharedlib-tests: build-sharedlib-linux-amd64
@echo "Building shared library tests..."
@$(SHARED_LIB_TEST_CC) $(SHARED_LIB_TEST_CFLAGS) -o $(SHARED_LIB_TEST_TARGET) $(SHARED_LIB_TEST_SOURCES)
run-sharedlib-tests: build-sharedlib-tests
@echo "Running shared library tests..."
@$(SHARED_LIB_TEST_TARGET) $(DIST_DIR)/$(BINARY_NAME)-linux-amd64.so
xgo-compile-sharedlib:
@echo "Building shared libraries using xgo..."
@mkdir -p $(DIST_DIR)
@xgo -targets=$(XGO_TARGETS) -go $(GOVERSION) -buildmode=c-shared -dest=$(DIST_DIR) -out=$(BINARY_NAME) -pkg=$(SHARED_LIB_LOCATION) .
generate-parser-nosql: generate-parser-nosql:
pigeon -o ./parsers/nosql/nosql.go ./parsers/nosql/nosql.peg pigeon -o ./parsers/nosql/nosql.go ./parsers/nosql/nosql.peg
+76 -18
View File
@@ -1,11 +1,23 @@
# Cosmium # Cosmium
Cosmium is a lightweight Cosmos DB emulator designed to facilitate local development and testing. While it aims to provide developers with a solution for running a local database during development, it's important to note that it's not 100% compatible with Cosmos DB. However, it serves as a convenient tool for E2E or integration tests during the CI/CD pipeline. Cosmium is a lightweight Cosmos DB emulator designed to facilitate local development and testing. While it aims to provide developers with a solution for running a local database during development, it's important to note that it's not 100% compatible with Cosmos DB. However, it serves as a convenient tool for E2E or integration tests during the CI/CD pipeline. Read more about compatibility [here](./docs/COMPATIBILITY.md).
One of Cosmium's notable features is its ability to save and load state to a single JSON file. This feature makes it easy to load different test cases or share state with other developers, enhancing collaboration and efficiency in development workflows. One of Cosmium's notable features is its ability to save and load state to a single JSON file. This feature makes it easy to load different test cases or share state with other developers, enhancing collaboration and efficiency in development workflows.
# Getting Started # Getting Started
### Downloading Cosmium
### Installation via Homebrew
You can install Cosmium using Homebrew by adding the `pikami/brew` tap and then installing the package.
```sh
brew tap pikami/brew
brew install cosmium
```
This will download and install Cosmium on your system, making it easy to manage and update using Homebrew.
### Downloading Cosmium Binaries
You can download the latest version of Cosmium from the [GitHub Releases page](https://github.com/pikami/cosmium/releases). Choose the appropriate release for your operating system and architecture. You can download the latest version of Cosmium from the [GitHub Releases page](https://github.com/pikami/cosmium/releases). Choose the appropriate release for your operating system and architecture.
@@ -13,24 +25,23 @@ You can download the latest version of Cosmium from the [GitHub Releases page](h
Cosmium is available for the following platforms: Cosmium is available for the following platforms:
* **Linux**: cosmium-linux-amd64 - **Linux**: cosmium-linux-amd64
* **macOS**: cosmium-darwin-amd64 - **Linux on ARM**: cosmium-linux-arm64
* **macOS on Apple Silicon**: cosmium-darwin-arm64 - **macOS**: cosmium-darwin-amd64
* **Windows**: cosmium-windows-amd64.exe - **macOS on Apple Silicon**: cosmium-darwin-arm64
- **Windows**: cosmium-windows-amd64.exe
- **Windows on ARM**: cosmium-windows-arm64.exe
### Running Cosmium ### Running Cosmium
Once downloaded, you can launch Cosmium using the following command: Once downloaded, you can launch Cosmium using the following command:
```sh ```sh
./cosmium-linux-amd64 \ cosmium -Persist "./save.json"
-Cert "cert.crt" \
-CertKey "cert.key" \
-Persist "./save.json" \
-InitialData "./save.json"
``` ```
Connection String Example: Connection String Example:
``` ```
AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==; AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==;
``` ```
@@ -39,22 +50,69 @@ AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJI
If you want to run Cosmos DB Explorer alongside Cosmium, you'll need to build it yourself and point the `-ExplorerDir` argument to the dist directory. Please refer to the [Cosmos DB Explorer repository](https://github.com/Azure/cosmos-explorer) for instructions on building the application. If you want to run Cosmos DB Explorer alongside Cosmium, you'll need to build it yourself and point the `-ExplorerDir` argument to the dist directory. Please refer to the [Cosmos DB Explorer repository](https://github.com/Azure/cosmos-explorer) for instructions on building the application.
There's also a prebuilt docker image that includes the explorer: `ghcr.io/pikami/cosmium:explorer`
Once running, the explorer can be reached by navigating following URL: `https://127.0.0.1:8081/_explorer/` (might be different depending on your configuration). Once running, the explorer can be reached by navigating following URL: `https://127.0.0.1:8081/_explorer/` (might be different depending on your configuration).
### Running with docker (optional)
There are two docker tags available:
- ghcr.io/pikami/cosmium:latest - Cosmium core service
- ghcr.io/pikami/cosmium:explorer - Cosmium with database explorer available on `https://127.0.0.1:8081/_explorer/`
If you wan to run the application using docker, configure it using environment variables see example:
```sh
# Ensure save.json exists so Docker volume mounts correctly
[ -f save.json ] || echo '{}' > save.json && docker run --rm \
-e COSMIUM_PERSIST=/save.json \
-v ./save.json:/save.json \
-p 8081:8081 \
ghcr.io/pikami/cosmium # or `ghcr.io/pikami/cosmium:explorer`
```
### SSL Certificate ### SSL Certificate
By default, Cosmium runs on HTTP. However, if you provide an SSL certificate, it will use HTTPS. Most applications will require HTTPS, so you can specify paths to the SSL certificate and key (PEM format) using the `-Cert` and `-CertKey` arguments, respectively. By default, Cosmium uses a pre-generated SSL certificate. You can provide your own certificates by specifying paths to the SSL certificate and key (PEM format) using the `-Cert` and `-CertKey` arguments, respectively.
To disable SSL and run Cosmium on HTTP instead, you can use the `-DisableTls` flag. However most applications will require HTTPS.
### Other Available Arguments ### Other Available Arguments
* **-AccountKey**: Account key for authentication (default "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==") - **-AccountKey**: Account key for authentication (default "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==")
* **-DisableAuth**: Disable authentication - **-DisableAuth**: Disable authentication
* **-Host**: Hostname (default "localhost") - **-Host**: Hostname (default "localhost")
* **-InitialData**: Path to JSON containing initial state - **-InitialData**: Path to JSON containing initial state
* **-Persist**: Saves data to the given path on application exit - **-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)
- **-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.
All mentioned arguments can also be set using environment variables:
- **COSMIUM_ACCOUNTKEY** for `-AccountKey`
- **COSMIUM_DISABLEAUTH** for `-DisableAuth`
- **COSMIUM_HOST** for `-Host`
- **COSMIUM_INITIALDATA** for `-InitialData`
- **COSMIUM_PERSIST** for `-Persist`
- **COSMIUM_PORT** for `-Port`
- **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).
+24
View File
@@ -0,0 +1,24 @@
package apimodels
const (
BatchOperationTypeCreate = "Create"
BatchOperationTypeDelete = "Delete"
BatchOperationTypeReplace = "Replace"
BatchOperationTypeUpsert = "Upsert"
BatchOperationTypeRead = "Read"
BatchOperationTypePatch = "Patch"
)
type BatchOperation struct {
OperationType string `json:"operationType"`
Id string `json:"id"`
ResourceBody map[string]interface{} `json:"resourceBody"`
}
type BatchOperationResult struct {
StatusCode int `json:"statusCode"`
RequestCharge float64 `json:"requestCharge"`
ResourceBody map[string]interface{} `json:"resourceBody"`
Etag string `json:"etag"`
Message string `json:"message"`
}
+39
View File
@@ -0,0 +1,39 @@
package api
import (
"github.com/gin-gonic/gin"
"github.com/pikami/cosmium/api/config"
"github.com/pikami/cosmium/internal/datastore"
)
type ApiServer struct {
stopServer chan interface{}
onServerShutdown chan interface{}
isActive bool
router *gin.Engine
config *config.ServerConfig
}
func NewApiServer(dataStore datastore.DataStore, config *config.ServerConfig) *ApiServer {
stopChan := make(chan interface{})
onServerShutdownChan := make(chan interface{})
apiServer := &ApiServer{
stopServer: stopChan,
onServerShutdown: onServerShutdownChan,
config: config,
}
apiServer.CreateRouter(dataStore)
return apiServer
}
func (s *ApiServer) GetRouter() *gin.Engine {
return s.router
}
func (s *ApiServer) Stop() {
s.stopServer <- true
<-s.onServerShutdown
}
+102 -14
View File
@@ -3,38 +3,126 @@ package config
import ( import (
"flag" "flag"
"fmt" "fmt"
"os"
"strings"
"github.com/pikami/cosmium/internal/logger"
) )
const ( const (
DefaultAccountKey = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==" DefaultAccountKey = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="
EnvPrefix = "COSMIUM_"
ExplorerBaseUrlLocation = "/_explorer"
) )
var Config = ServerConfig{} const (
DataStoreJson = "json"
DataStoreBadger = "badger"
)
func ParseFlags() { 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")
rntbdPort := flag.Int("RntbdPort", 10000, "RNTBD listen port")
explorerPath := flag.String("ExplorerDir", "", "Path to cosmos-explorer files") explorerPath := flag.String("ExplorerDir", "", "Path to cosmos-explorer files")
tlsCertificatePath := flag.String("Cert", "", "Hostname") tlsCertificatePath := flag.String("Cert", "", "Hostname")
tlsCertificateKey := flag.String("CertKey", "", "Hostname") tlsCertificateKey := flag.String("CertKey", "", "Hostname")
initialDataPath := flag.String("InitialData", "", "Path to JSON containing initial state") initialDataPath := flag.String("InitialData", "", "Path to JSON containing initial state")
accountKey := flag.String("AccountKey", DefaultAccountKey, "Account key for authentication") accountKey := flag.String("AccountKey", DefaultAccountKey, "Account key for authentication")
disableAuthentication := flag.Bool("DisableAuth", false, "Disable authentication") disableAuthentication := flag.Bool("DisableAuth", false, "Disable authentication")
disableTls := flag.Bool("DisableTls", false, "Disable TLS, serve over HTTP")
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"})
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()))
enableRntbd := flag.Bool("ExperimentalEnableRntbd", false, "EXPERIMENTAL: Enable RNTBD (CosmosDB Direct Connection Mode)")
flag.Parse() flag.Parse()
setFlagsFromEnvironment()
Config.Host = *host config := ServerConfig{}
Config.Port = *port config.Host = *host
Config.ExplorerPath = *explorerPath config.Port = *port
Config.TLS_CertificatePath = *tlsCertificatePath config.RntbdPort = *rntbdPort
Config.TLS_CertificateKey = *tlsCertificateKey config.ExplorerPath = *explorerPath
Config.InitialDataFilePath = *initialDataPath config.TLS_CertificatePath = *tlsCertificatePath
Config.PersistDataFilePath = *persistDataPath config.TLS_CertificateKey = *tlsCertificateKey
Config.DisableAuth = *disableAuthentication config.InitialDataFilePath = *initialDataPath
config.PersistDataFilePath = *persistDataPath
config.DisableAuth = *disableAuthentication
config.DisableTls = *disableTls
config.AccountKey = *accountKey
config.LogLevel = logLevel.value
config.DataStore = dataStore.value
config.EnableRntbd = *enableRntbd
Config.DatabaseAccount = Config.Host config.PopulateCalculatedFields()
Config.DatabaseDomain = Config.Host
Config.DatabaseEndpoint = fmt.Sprintf("https://%s:%d/", Config.Host, Config.Port) return config
Config.AccountKey = *accountKey }
func (c *ServerConfig) PopulateCalculatedFields() {
c.DatabaseAccount = c.Host
c.DatabaseDomain = c.Host
c.DatabaseEndpoint = fmt.Sprintf("https://%s:%d/", c.Host, c.Port)
c.RntbdEndpoint = fmt.Sprintf("rntbd://%s:%d/", c.Host, c.RntbdPort)
c.ExplorerBaseUrlLocation = ExplorerBaseUrlLocation
switch c.LogLevel {
case "debug":
logger.SetLogLevel(logger.LogLevelDebug)
case "info":
logger.SetLogLevel(logger.LogLevelInfo)
case "error":
logger.SetLogLevel(logger.LogLevelError)
case "silent":
logger.SetLogLevel(logger.LogLevelSilent)
default:
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() {
if c.Host == "" {
c.Host = "localhost"
}
if c.Port == 0 {
c.Port = 8081
}
if c.AccountKey == "" {
c.AccountKey = DefaultAccountKey
}
}
func setFlagsFromEnvironment() (err error) {
flag.VisitAll(func(f *flag.Flag) {
name := EnvPrefix + strings.ToUpper(strings.Replace(f.Name, "-", "_", -1))
if value, ok := os.LookupEnv(name); ok {
err2 := flag.Set(f.Name, value)
if err2 != nil {
err = fmt.Errorf("failed setting flag from environment: %w", err2)
}
}
})
return
} }
+36
View File
@@ -0,0 +1,36 @@
package config
import (
"fmt"
"strings"
)
type EnumValue struct {
allowedValues []string
value string
}
func (e *EnumValue) String() string {
return e.value
}
func (e *EnumValue) Set(v string) error {
for _, allowed := range e.allowedValues {
if v == allowed {
e.value = v
return nil
}
}
return fmt.Errorf("invalid value %q, must be one of: %s", v, strings.Join(e.allowedValues, ", "))
}
func NewEnumValue(defaultValue string, allowedValues []string) *EnumValue {
return &EnumValue{
allowedValues: allowedValues,
value: defaultValue,
}
}
func (e *EnumValue) AllowedValuesList() string {
return fmt.Sprintf("(one of: %s)", strings.Join(e.allowedValues, ", "))
}
+20 -12
View File
@@ -1,17 +1,25 @@
package config package config
type ServerConfig struct { type ServerConfig struct {
DatabaseAccount string DatabaseAccount string `json:"databaseAccount"`
DatabaseDomain string DatabaseDomain string `json:"databaseDomain"`
DatabaseEndpoint string DatabaseEndpoint string `json:"databaseEndpoint"`
AccountKey string RntbdEndpoint string `json:"rntbdEndpoint"`
AccountKey string `json:"accountKey"`
ExplorerPath string ExplorerPath string `json:"explorerPath"`
Port int Port int `json:"port"`
Host string RntbdPort int `json:"rntbdPort"`
TLS_CertificatePath string Host string `json:"host"`
TLS_CertificateKey string TLS_CertificatePath string `json:"tlsCertificatePath"`
InitialDataFilePath string TLS_CertificateKey string `json:"tlsCertificateKey"`
PersistDataFilePath string InitialDataFilePath string `json:"initialDataFilePath"`
DisableAuth bool PersistDataFilePath string `json:"persistDataFilePath"`
DisableAuth bool `json:"disableAuth"`
DisableTls bool `json:"disableTls"`
LogLevel string `json:"logLevel"`
ExplorerBaseUrlLocation string `json:"explorerBaseUrlLocation"`
EnableRntbd bool `json:"enableRntbd"`
DataStore string `json:"dataStore"`
} }
+36 -27
View File
@@ -1,64 +1,73 @@
package handlers package handlers
import ( import (
"fmt"
"net/http" "net/http"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/pikami/cosmium/internal/repositories" "github.com/pikami/cosmium/api/headers"
repositorymodels "github.com/pikami/cosmium/internal/repository_models" "github.com/pikami/cosmium/internal/constants"
"github.com/pikami/cosmium/internal/datastore"
) )
func GetAllCollections(c *gin.Context) { func (h *Handlers) GetAllCollections(c *gin.Context) {
databaseId := c.Param("databaseId") databaseId := c.Param("databaseId")
collections, status := repositories.GetAllCollections(databaseId) collections, status := h.dataStore.GetAllCollections(databaseId)
if status == repositorymodels.StatusOk { if status == datastore.StatusOk {
c.IndentedJSON(http.StatusOK, gin.H{"_rid": "", "DocumentCollections": collections, "_count": len(collections)}) database, _ := h.dataStore.GetDatabase(databaseId)
c.Header(headers.ItemCount, fmt.Sprintf("%d", len(collections)))
c.IndentedJSON(http.StatusOK, gin.H{
"_rid": database.ResourceID,
"DocumentCollections": collections,
"_count": len(collections),
})
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
func GetCollection(c *gin.Context) { func (h *Handlers) GetCollection(c *gin.Context) {
databaseId := c.Param("databaseId") databaseId := c.Param("databaseId")
id := c.Param("collId") id := c.Param("collId")
collection, status := repositories.GetCollection(databaseId, id) collection, status := h.dataStore.GetCollection(databaseId, id)
if status == repositorymodels.StatusOk { if status == datastore.StatusOk {
c.IndentedJSON(http.StatusOK, collection) c.IndentedJSON(http.StatusOK, collection)
return return
} }
if status == repositorymodels.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 DeleteCollection(c *gin.Context) { func (h *Handlers) DeleteCollection(c *gin.Context) {
databaseId := c.Param("databaseId") databaseId := c.Param("databaseId")
id := c.Param("collId") id := c.Param("collId")
status := repositories.DeleteCollection(databaseId, id) status := h.dataStore.DeleteCollection(databaseId, id)
if status == repositorymodels.StatusOk { if status == datastore.StatusOk {
c.Status(http.StatusNoContent) c.Status(http.StatusNoContent)
return return
} }
if status == repositorymodels.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 CreateCollection(c *gin.Context) { func (h *Handlers) CreateCollection(c *gin.Context) {
databaseId := c.Param("databaseId") databaseId := c.Param("databaseId")
var newCollection repositorymodels.Collection var newCollection datastore.Collection
if err := c.BindJSON(&newCollection); err != nil { if err := c.BindJSON(&newCollection); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()}) c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()})
@@ -66,20 +75,20 @@ func 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 := repositories.CreateCollection(databaseId, newCollection) createdCollection, status := h.dataStore.CreateCollection(databaseId, newCollection)
if status == repositorymodels.Conflict { if status == datastore.Conflict {
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"}) c.IndentedJSON(http.StatusConflict, constants.ConflictResponse)
return return
} }
if status == repositorymodels.StatusOk { if status == datastore.StatusOk {
c.IndentedJSON(http.StatusCreated, createdCollection) c.IndentedJSON(http.StatusCreated, createdCollection)
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
+8 -3
View File
@@ -4,9 +4,14 @@ import (
"net/http" "net/http"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/pikami/cosmium/internal/repositories"
) )
func CosmiumExport(c *gin.Context) { func (h *Handlers) CosmiumExport(c *gin.Context) {
c.IndentedJSON(http.StatusOK, repositories.GetState()) dataStoreState, err := h.dataStore.DumpToJson()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.Data(http.StatusOK, "application/json", []byte(dataStoreState))
} }
+34 -27
View File
@@ -1,59 +1,66 @@
package handlers package handlers
import ( import (
"fmt"
"net/http" "net/http"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/pikami/cosmium/internal/repositories" "github.com/pikami/cosmium/api/headers"
repositorymodels "github.com/pikami/cosmium/internal/repository_models" "github.com/pikami/cosmium/internal/constants"
"github.com/pikami/cosmium/internal/datastore"
) )
func GetAllDatabases(c *gin.Context) { func (h *Handlers) GetAllDatabases(c *gin.Context) {
databases, status := repositories.GetAllDatabases() databases, status := h.dataStore.GetAllDatabases()
if status == repositorymodels.StatusOk { if status == datastore.StatusOk {
c.IndentedJSON(http.StatusOK, gin.H{"_rid": "", "Databases": databases, "_count": len(databases)}) c.Header(headers.ItemCount, fmt.Sprintf("%d", len(databases)))
c.IndentedJSON(http.StatusOK, gin.H{
"_rid": "",
"Databases": databases,
"_count": len(databases),
})
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
func GetDatabase(c *gin.Context) { func (h *Handlers) GetDatabase(c *gin.Context) {
id := c.Param("databaseId") id := c.Param("databaseId")
database, status := repositories.GetDatabase(id) database, status := h.dataStore.GetDatabase(id)
if status == repositorymodels.StatusOk { if status == datastore.StatusOk {
c.IndentedJSON(http.StatusOK, database) c.IndentedJSON(http.StatusOK, database)
return return
} }
if status == repositorymodels.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 DeleteDatabase(c *gin.Context) { func (h *Handlers) DeleteDatabase(c *gin.Context) {
id := c.Param("databaseId") id := c.Param("databaseId")
status := repositories.DeleteDatabase(id) status := h.dataStore.DeleteDatabase(id)
if status == repositorymodels.StatusOk { if status == datastore.StatusOk {
c.Status(http.StatusNoContent) c.Status(http.StatusNoContent)
return return
} }
if status == repositorymodels.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 CreateDatabase(c *gin.Context) { func (h *Handlers) CreateDatabase(c *gin.Context) {
var newDatabase repositorymodels.Database var newDatabase datastore.Database
if err := c.BindJSON(&newDatabase); err != nil { if err := c.BindJSON(&newDatabase); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()}) c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()})
@@ -61,20 +68,20 @@ func 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 := repositories.CreateDatabase(newDatabase) createdDatabase, status := h.dataStore.CreateDatabase(newDatabase)
if status == repositorymodels.Conflict { if status == datastore.Conflict {
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"}) c.IndentedJSON(http.StatusConflict, constants.ConflictResponse)
return return
} }
if status == repositorymodels.StatusOk { if status == datastore.StatusOk {
c.IndentedJSON(http.StatusCreated, createdDatabase) c.IndentedJSON(http.StatusCreated, createdDatabase)
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
+354 -51
View File
@@ -1,67 +1,88 @@
package handlers package handlers
import ( import (
"encoding/json"
"fmt"
"net/http" "net/http"
"strconv"
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"
"github.com/pikami/cosmium/api/headers"
"github.com/pikami/cosmium/internal/constants" "github.com/pikami/cosmium/internal/constants"
"github.com/pikami/cosmium/internal/repositories" continuationtoken "github.com/pikami/cosmium/internal/continuation_token"
repositorymodels "github.com/pikami/cosmium/internal/repository_models" "github.com/pikami/cosmium/internal/converters"
"github.com/pikami/cosmium/internal/datastore"
"github.com/pikami/cosmium/internal/logger"
"github.com/pikami/cosmium/parsers"
"github.com/pikami/cosmium/parsers/nosql"
memoryexecutor "github.com/pikami/cosmium/query_executors/memory_executor"
) )
func GetAllDocuments(c *gin.Context) { func (h *Handlers) GetAllDocuments(c *gin.Context) {
databaseId := c.Param("databaseId") databaseId := c.Param("databaseId")
collectionId := c.Param("collId") collectionId := c.Param("collId")
documents, status := repositories.GetAllDocuments(databaseId, collectionId) documents, status := h.dataStore.GetAllDocuments(databaseId, collectionId)
if status == repositorymodels.StatusOk { if status == datastore.StatusOk {
c.IndentedJSON(http.StatusOK, gin.H{"_rid": "", "Documents": documents, "_count": len(documents)}) collection, _ := h.dataStore.GetCollection(databaseId, collectionId)
c.Header(headers.ItemCount, fmt.Sprintf("%d", len(documents)))
c.IndentedJSON(http.StatusOK, gin.H{
"_rid": collection.ID,
"Documents": documents,
"_count": len(documents),
})
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
func GetDocument(c *gin.Context) { func (h *Handlers) GetDocument(c *gin.Context) {
databaseId := c.Param("databaseId") databaseId := c.Param("databaseId")
collectionId := c.Param("collId") collectionId := c.Param("collId")
documentId := c.Param("docId") documentId := c.Param("docId")
document, status := repositories.GetDocument(databaseId, collectionId, documentId) document, status := h.dataStore.GetDocument(databaseId, collectionId, documentId)
if status == repositorymodels.StatusOk { if status == datastore.StatusOk {
if etag, ok := document["_etag"].(string); ok {
c.Header(headers.ETag, etag)
}
c.IndentedJSON(http.StatusOK, document) c.IndentedJSON(http.StatusOK, document)
return return
} }
if status == repositorymodels.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 DeleteDocument(c *gin.Context) { func (h *Handlers) DeleteDocument(c *gin.Context) {
databaseId := c.Param("databaseId") databaseId := c.Param("databaseId")
collectionId := c.Param("collId") collectionId := c.Param("collId")
documentId := c.Param("docId") documentId := c.Param("docId")
status := repositories.DeleteDocument(databaseId, collectionId, documentId) status := h.dataStore.DeleteDocument(databaseId, collectionId, documentId)
if status == repositorymodels.StatusOk { if status == datastore.StatusOk {
c.Status(http.StatusNoContent) c.Status(http.StatusNoContent)
return return
} }
if status == repositorymodels.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 repository // TODO: Maybe move "replace" logic to data store
func ReplaceDocument(c *gin.Context) { func (h *Handlers) ReplaceDocument(c *gin.Context) {
databaseId := c.Param("databaseId") databaseId := c.Param("databaseId")
collectionId := c.Param("collId") collectionId := c.Param("collId")
documentId := c.Param("docId") documentId := c.Param("docId")
@@ -72,29 +93,54 @@ func ReplaceDocument(c *gin.Context) {
return return
} }
status := repositories.DeleteDocument(databaseId, collectionId, documentId) existingDocument, status := h.dataStore.GetDocument(databaseId, collectionId, documentId)
if status == repositorymodels.StatusNotFound { if status == datastore.StatusNotFound {
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"}) c.IndentedJSON(http.StatusNotFound, constants.NotFoundResponse)
return
}
if status != datastore.StatusOk {
c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
return return
} }
createdDocument, status := repositories.CreateDocument(databaseId, collectionId, requestBody) if ifMatch := c.GetHeader(headers.IfMatch); ifMatch != "" {
if status == repositorymodels.Conflict { if existingDocument["_etag"] != ifMatch {
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"}) c.Header(headers.ErrorCode, "PreconditionFailed")
c.JSON(http.StatusPreconditionFailed, constants.PreconditionFailedResponse)
return
}
}
status = h.dataStore.DeleteDocument(databaseId, collectionId, documentId)
if status == datastore.StatusNotFound {
c.IndentedJSON(http.StatusNotFound, constants.NotFoundResponse)
return return
} }
if status == repositorymodels.StatusOk { createdDocument, status := h.dataStore.CreateDocument(databaseId, collectionId, requestBody)
if status == datastore.Conflict {
c.IndentedJSON(http.StatusConflict, constants.ConflictResponse)
return
}
if status == datastore.StatusOk {
c.IndentedJSON(http.StatusCreated, createdDocument) c.IndentedJSON(http.StatusCreated, createdDocument)
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
func DocumentsPost(c *gin.Context) { func (h *Handlers) PatchDocument(c *gin.Context) {
databaseId := c.Param("databaseId") databaseId := c.Param("databaseId")
collectionId := c.Param("collId") collectionId := c.Param("collId")
documentId := c.Param("docId")
document, status := h.dataStore.GetDocument(databaseId, collectionId, documentId)
if status == datastore.StatusNotFound {
c.IndentedJSON(http.StatusNotFound, constants.NotFoundResponse)
return
}
var requestBody map[string]interface{} var requestBody map[string]interface{}
if err := c.BindJSON(&requestBody); err != nil { if err := c.BindJSON(&requestBody); err != nil {
@@ -102,46 +148,119 @@ func DocumentsPost(c *gin.Context) {
return return
} }
query := requestBody["query"] operations := requestBody["operations"]
if query != nil { operationsBytes, err := json.Marshal(operations)
if c.GetHeader("x-ms-cosmos-is-query-plan-request") != "" { if err != nil {
c.IndentedJSON(http.StatusOK, constants.QueryPlanResponse) c.JSON(http.StatusBadRequest, gin.H{"message": "Could not decode operations"})
return return
} }
var queryParameters map[string]interface{} patch, err := jsonpatch.DecodePatch(operationsBytes)
if paramsArray, ok := requestBody["parameters"].([]interface{}); ok { if err != nil {
queryParameters = parametersToMap(paramsArray) c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()})
}
docs, status := repositories.ExecuteQueryDocuments(databaseId, collectionId, query.(string), queryParameters)
if status != repositorymodels.StatusOk {
// TODO: Currently we return everything if the query fails
GetAllDocuments(c)
return return
} }
c.IndentedJSON(http.StatusOK, gin.H{"_rid": "", "Documents": docs, "_count": len(docs)}) currentDocumentBytes, err := json.Marshal(document)
if err != nil {
logger.ErrorLn("Failed to marshal existing document:", err)
c.JSON(http.StatusInternalServerError, gin.H{"message": "Failed to marshal existing document"})
return return
} }
if requestBody["id"] == "" { modifiedDocumentBytes, err := patch.Apply(currentDocumentBytes)
c.JSON(http.StatusBadRequest, gin.H{"message": "BadRequest"}) if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()})
return return
} }
createdDocument, status := repositories.CreateDocument(databaseId, collectionId, requestBody) var modifiedDocument map[string]interface{}
if status == repositorymodels.Conflict { err = json.Unmarshal(modifiedDocumentBytes, &modifiedDocument)
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"}) if err != nil {
logger.ErrorLn("Failed to unmarshal modified document:", err)
c.JSON(http.StatusInternalServerError, gin.H{"message": "Failed to unmarshal modified document"})
return return
} }
if status == repositorymodels.StatusOk { if modifiedDocument["id"] != document["id"] {
c.JSON(http.StatusUnprocessableEntity, gin.H{"message": "The ID field cannot be modified"})
return
}
status = h.dataStore.DeleteDocument(databaseId, collectionId, documentId)
if status == datastore.StatusNotFound {
c.IndentedJSON(http.StatusNotFound, constants.NotFoundResponse)
return
}
createdDocument, status := h.dataStore.CreateDocument(databaseId, collectionId, modifiedDocument)
if status == datastore.Conflict {
c.IndentedJSON(http.StatusConflict, constants.ConflictResponse)
return
}
if status == datastore.StatusOk {
c.IndentedJSON(http.StatusCreated, createdDocument) c.IndentedJSON(http.StatusCreated, createdDocument)
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
}
func (h *Handlers) DocumentsPost(c *gin.Context) {
databaseId := c.Param("databaseId")
collectionId := c.Param("collId")
// Handle batch requests
isBatchRequest, _ := strconv.ParseBool(c.GetHeader(headers.IsBatchRequest))
if isBatchRequest {
h.handleBatchRequest(c)
return
}
var requestBody map[string]interface{}
if err := c.BindJSON(&requestBody); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()})
return
}
// Handle query plan requests
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)
return
}
if requestBody["id"] == "" {
c.JSON(http.StatusBadRequest, constants.BadRequestResponse)
return
}
isUpsert, _ := strconv.ParseBool(c.GetHeader(headers.IsUpsert))
if isUpsert {
h.dataStore.DeleteDocument(databaseId, collectionId, requestBody["id"].(string))
}
createdDocument, status := h.dataStore.CreateDocument(databaseId, collectionId, requestBody)
if status == datastore.Conflict {
c.IndentedJSON(http.StatusConflict, constants.ConflictResponse)
return
}
if status == datastore.StatusOk {
c.IndentedJSON(http.StatusCreated, createdDocument)
return
}
c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
func parametersToMap(pairs []interface{}) map[string]interface{} { func parametersToMap(pairs []interface{}) map[string]interface{} {
@@ -155,3 +274,187 @@ func parametersToMap(pairs []interface{}) map[string]interface{} {
return result return result
} }
func (h *Handlers) handleDocumentQuery(c *gin.Context, requestBody map[string]interface{}) {
databaseId := c.Param("databaseId")
collectionId := c.Param("collId")
var queryParameters map[string]interface{}
if paramsArray, ok := requestBody["parameters"].([]interface{}); ok {
queryParameters = parametersToMap(paramsArray)
}
collection, collectionStatus := h.dataStore.GetCollection(databaseId, collectionId)
if collectionStatus == datastore.StatusNotFound {
c.IndentedJSON(http.StatusNotFound, constants.NotFoundResponse)
return
}
if collectionStatus != datastore.StatusOk {
c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
return
}
continuationToken := continuationtoken.GenerateDefault(collection.ResourceID)
continuationTokenHeader := c.GetHeader(headers.ContinuationToken)
if continuationTokenHeader != "" {
continuationToken = continuationtoken.FromString(continuationTokenHeader)
}
pageMaxItemCount, maxItemCountError := strconv.Atoi(c.GetHeader(headers.MaxItemCount))
if maxItemCountError != nil {
pageMaxItemCount = 1000
}
queryText := requestBody["query"].(string)
executeQueryResult, status := h.executeQueryDocuments(
databaseId, collectionId, queryText, queryParameters, pageMaxItemCount, continuationToken.Token.TotalResults)
if status != datastore.StatusOk {
// TODO: Currently we return everything if the query fails
logger.Infof("Query failed: %s", queryText)
h.GetAllDocuments(c)
return
}
resultCount := len(executeQueryResult.Rows)
if executeQueryResult.HasMorePages {
nextContinuationToken := continuationtoken.Generate(
collection.ResourceID, continuationToken.Token.PageIndex+1, continuationToken.Token.TotalResults+resultCount)
c.Header(headers.ContinuationToken, nextContinuationToken.ToString())
}
c.Header(headers.ItemCount, fmt.Sprintf("%d", resultCount))
c.IndentedJSON(http.StatusOK, gin.H{
"_rid": collection.ResourceID,
"Documents": executeQueryResult.Rows,
"_count": resultCount,
})
}
func (h *Handlers) handleBatchRequest(c *gin.Context) {
databaseId := c.Param("databaseId")
collectionId := c.Param("collId")
batchOperations := make([]apimodels.BatchOperation, 0)
if err := c.BindJSON(&batchOperations); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()})
return
}
batchOperationResults := make([]apimodels.BatchOperationResult, len(batchOperations))
for idx, operation := range batchOperations {
switch operation.OperationType {
case apimodels.BatchOperationTypeCreate:
createdDocument, status := h.dataStore.CreateDocument(databaseId, collectionId, operation.ResourceBody)
responseCode := dataStoreStatusToResponseCode(status)
if status == datastore.StatusOk {
responseCode = http.StatusCreated
}
batchOperationResults[idx] = apimodels.BatchOperationResult{
StatusCode: responseCode,
ResourceBody: createdDocument,
}
case apimodels.BatchOperationTypeDelete:
status := h.dataStore.DeleteDocument(databaseId, collectionId, operation.Id)
responseCode := dataStoreStatusToResponseCode(status)
if status == datastore.StatusOk {
responseCode = http.StatusNoContent
}
batchOperationResults[idx] = apimodels.BatchOperationResult{
StatusCode: responseCode,
}
case apimodels.BatchOperationTypeReplace:
deleteStatus := h.dataStore.DeleteDocument(databaseId, collectionId, operation.Id)
if deleteStatus == datastore.StatusNotFound {
batchOperationResults[idx] = apimodels.BatchOperationResult{
StatusCode: http.StatusNotFound,
}
continue
}
createdDocument, createStatus := h.dataStore.CreateDocument(databaseId, collectionId, operation.ResourceBody)
responseCode := dataStoreStatusToResponseCode(createStatus)
if createStatus == datastore.StatusOk {
responseCode = http.StatusCreated
}
batchOperationResults[idx] = apimodels.BatchOperationResult{
StatusCode: responseCode,
ResourceBody: createdDocument,
}
case apimodels.BatchOperationTypeUpsert:
documentId := operation.ResourceBody["id"].(string)
h.dataStore.DeleteDocument(databaseId, collectionId, documentId)
createdDocument, createStatus := h.dataStore.CreateDocument(databaseId, collectionId, operation.ResourceBody)
responseCode := dataStoreStatusToResponseCode(createStatus)
if createStatus == datastore.StatusOk {
responseCode = http.StatusCreated
}
batchOperationResults[idx] = apimodels.BatchOperationResult{
StatusCode: responseCode,
ResourceBody: createdDocument,
}
case apimodels.BatchOperationTypeRead:
document, status := h.dataStore.GetDocument(databaseId, collectionId, operation.Id)
batchOperationResults[idx] = apimodels.BatchOperationResult{
StatusCode: dataStoreStatusToResponseCode(status),
ResourceBody: document,
}
case apimodels.BatchOperationTypePatch:
batchOperationResults[idx] = apimodels.BatchOperationResult{
StatusCode: http.StatusNotImplemented,
Message: "Patch operation is not implemented",
}
default:
batchOperationResults[idx] = apimodels.BatchOperationResult{
StatusCode: http.StatusBadRequest,
Message: "Unknown operation type",
}
}
}
c.JSON(http.StatusOK, batchOperationResults)
}
func dataStoreStatusToResponseCode(status datastore.DataStoreStatus) int {
switch status {
case datastore.StatusOk:
return http.StatusOK
case datastore.StatusNotFound:
return http.StatusNotFound
case datastore.Conflict:
return http.StatusConflict
case datastore.BadRequest:
return http.StatusBadRequest
default:
return http.StatusInternalServerError
}
}
func (h *Handlers) executeQueryDocuments(
databaseId string,
collectionId string,
query string,
queryParameters map[string]interface{},
pageMaxItemCount int,
pageCursor int,
) (memoryexecutor.ExecuteQueryResult, datastore.DataStoreStatus) {
parsedQuery, err := nosql.Parse("", []byte(query))
if err != nil {
logger.Errorf("Failed to parse query: %s\nerr: %v", query, err)
return memoryexecutor.ExecuteQueryResult{}, datastore.BadRequest
}
allDocumentsIterator, status := h.dataStore.GetDocumentIterator(databaseId, collectionId)
if status != datastore.StatusOk {
return memoryexecutor.ExecuteQueryResult{}, status
}
defer allDocumentsIterator.Close()
rowsIterator := converters.NewDocumentToRowTypeIterator(allDocumentsIterator)
if typedQuery, ok := parsedQuery.(parsers.SelectStmt); ok {
typedQuery.Parameters = queryParameters
return memoryexecutor.ExecuteQuery(typedQuery, rowsIterator, pageCursor, pageMaxItemCount), datastore.StatusOk
}
return memoryexecutor.ExecuteQueryResult{}, datastore.BadRequest
}
+5 -6
View File
@@ -4,15 +4,14 @@ import (
"fmt" "fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/pikami/cosmium/api/config"
) )
func RegisterExplorerHandlers(router *gin.Engine) { func (h *Handlers) RegisterExplorerHandlers(router *gin.Engine) {
explorer := router.Group("/_explorer") explorer := router.Group(h.config.ExplorerBaseUrlLocation)
{ {
explorer.Use(func(ctx *gin.Context) { explorer.Use(func(ctx *gin.Context) {
if ctx.Param("filepath") == "/config.json" { if ctx.Param("filepath") == "/config.json" {
endpoint := fmt.Sprintf("https://%s:%d", config.Config.Host, config.Config.Port) endpoint := fmt.Sprintf("https://%s:%d", h.config.Host, h.config.Port)
ctx.JSON(200, gin.H{ ctx.JSON(200, gin.H{
"BACKEND_ENDPOINT": endpoint, "BACKEND_ENDPOINT": endpoint,
"MONGO_BACKEND_ENDPOINT": endpoint, "MONGO_BACKEND_ENDPOINT": endpoint,
@@ -25,8 +24,8 @@ func RegisterExplorerHandlers(router *gin.Engine) {
} }
}) })
if config.Config.ExplorerPath != "" { if h.config.ExplorerPath != "" {
explorer.Static("/", config.Config.ExplorerPath) explorer.Static("/", h.config.ExplorerPath)
} }
} }
} }
+18
View File
@@ -0,0 +1,18 @@
package handlers
import (
"github.com/pikami/cosmium/api/config"
"github.com/pikami/cosmium/internal/datastore"
)
type Handlers struct {
dataStore datastore.DataStore
config *config.ServerConfig
}
func NewHandlers(dataStore datastore.DataStore, config *config.ServerConfig) *Handlers {
return &Handlers{
dataStore: dataStore,
config: config,
}
}
+51 -20
View File
@@ -1,24 +1,48 @@
package middleware package middleware
import ( import (
"fmt"
"net/url" "net/url"
"strings" "strings"
"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"
) )
func Authentication() gin.HandlerFunc { func Authentication(config *config.ServerConfig) gin.HandlerFunc {
return func(c *gin.Context) { return func(c *gin.Context) {
requestUrl := c.Request.URL.String() requestUrl := c.Request.URL.String()
if config.Config.DisableAuth || if config.DisableAuth ||
strings.HasPrefix(requestUrl, "/_explorer") || strings.HasPrefix(requestUrl, config.ExplorerBaseUrlLocation) ||
strings.HasPrefix(requestUrl, "/cosmium") { strings.HasPrefix(requestUrl, "/cosmium") {
return return
} }
resourceType := urlToResourceType(requestUrl)
resourceId := requestToResourceId(c)
authHeader := c.Request.Header.Get(headers.Authorization)
date := c.Request.Header.Get(headers.XDate)
expectedSignature := authentication.GenerateSignature(
c.Request.Method, resourceType, resourceId, date, config.AccountKey)
decoded, _ := url.QueryUnescape(authHeader)
params, _ := url.ParseQuery(decoded)
clientSignature := strings.Replace(params.Get("sig"), " ", "+", -1)
if clientSignature != expectedSignature {
logger.Errorf("Got wrong signature from client.\n- Expected: %s\n- Got: %s\n", expectedSignature, clientSignature)
c.IndentedJSON(401, gin.H{
"code": "Unauthorized",
"message": "Wrong signature.",
})
c.Abort()
}
}
}
func urlToResourceType(requestUrl string) string {
var resourceType string var resourceType string
parts := strings.Split(requestUrl, "/") parts := strings.Split(requestUrl, "/")
switch len(parts) { switch len(parts) {
@@ -30,9 +54,18 @@ func Authentication() gin.HandlerFunc {
resourceType = parts[5] resourceType = parts[5]
} }
return resourceType
}
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())
var resourceId string var resourceId string
if databaseId != "" { if databaseId != "" {
resourceId += "dbs/" + databaseId resourceId += "dbs/" + databaseId
@@ -43,22 +76,20 @@ func Authentication() gin.HandlerFunc {
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
}
authHeader := c.Request.Header.Get("authorization") isFeed := c.Request.Header.Get(headers.AIM) == "Incremental Feed"
date := c.Request.Header.Get("x-ms-date") if resourceType == "pkranges" && isFeed {
expectedSignature := authentication.GenerateSignature( resourceId = collId
c.Request.Method, resourceType, resourceId, date, config.Config.AccountKey) }
decoded, _ := url.QueryUnescape(authHeader) return resourceId
params, _ := url.ParseQuery(decoded)
clientSignature := strings.Replace(params.Get("sig"), " ", "+", -1)
if clientSignature != expectedSignature {
fmt.Printf("Got wrong signature from client.\n- Expected: %s\n- Got: %s\n", expectedSignature, clientSignature)
c.IndentedJSON(401, gin.H{
"code": "Unauthorized",
"message": "Wrong signature.",
})
c.Abort()
}
}
} }
+2 -2
View File
@@ -2,10 +2,10 @@ package middleware
import ( import (
"bytes" "bytes"
"fmt"
"io" "io"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/pikami/cosmium/internal/logger"
) )
func RequestLogger() gin.HandlerFunc { func RequestLogger() gin.HandlerFunc {
@@ -16,7 +16,7 @@ func RequestLogger() gin.HandlerFunc {
bodyStr := readBody(rdr1) bodyStr := readBody(rdr1)
if bodyStr != "" { if bodyStr != "" {
fmt.Println(bodyStr) logger.DebugLn(bodyStr)
} }
c.Request.Body = rdr2 c.Request.Body = rdr2
@@ -0,0 +1,21 @@
package middleware
import (
"strings"
"github.com/gin-gonic/gin"
"github.com/pikami/cosmium/api/config"
)
func StripTrailingSlashes(r *gin.Engine, config *config.ServerConfig) gin.HandlerFunc {
return func(c *gin.Context) {
path := c.Request.URL.Path
if len(path) > 1 && path[len(path)-1] == '/' && !strings.Contains(path, config.ExplorerBaseUrlLocation) {
c.Request.URL.Path = path[:len(path)-1]
r.HandleContext(c)
c.Abort()
return
}
c.Next()
}
}
+2
View File
@@ -4,9 +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(headers.ItemCount, "0")
c.IndentedJSON(http.StatusOK, gin.H{ c.IndentedJSON(http.StatusOK, gin.H{
"_rid": "", "_rid": "",
"_count": 0, "_count": 0,
+24 -15
View File
@@ -5,39 +5,48 @@ import (
"net/http" "net/http"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/pikami/cosmium/internal/repositories" "github.com/pikami/cosmium/api/headers"
repositorymodels "github.com/pikami/cosmium/internal/repository_models" "github.com/pikami/cosmium/internal/constants"
"github.com/pikami/cosmium/internal/datastore"
"github.com/pikami/cosmium/internal/resourceid"
) )
func GetPartitionKeyRanges(c *gin.Context) { 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 := repositories.GetPartitionKeyRanges(databaseId, collectionId) partitionKeyRanges, status := h.dataStore.GetPartitionKeyRanges(databaseId, collectionId)
if status == repositorymodels.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
collection, _ := h.dataStore.GetCollection(databaseId, collectionId)
if collection.ResourceID != "" {
collectionRid = collection.ResourceID
}
rid := resourceid.NewCombined(collectionRid, resourceid.New(resourceid.ResourceTypePartitionKeyRange))
c.IndentedJSON(http.StatusOK, gin.H{ c.IndentedJSON(http.StatusOK, gin.H{
"_rid": "", "_rid": rid,
"_count": len(partitionKeyRanges), "_count": len(partitionKeyRanges),
"PartitionKeyRanges": partitionKeyRanges, "PartitionKeyRanges": partitionKeyRanges,
}) })
return return
} }
if status == repositorymodels.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)
} }
+78 -3
View File
@@ -1,12 +1,87 @@
package handlers package handlers
import ( import (
"fmt"
"net/http" "net/http"
"strings"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/pikami/cosmium/internal/constants"
) )
func GetServerInfo(c *gin.Context) { func (h *Handlers) GetServerInfo(c *gin.Context) {
c.IndentedJSON(http.StatusOK, constants.ServerInfoResponse) c.IndentedJSON(http.StatusOK, gin.H{
"_self": "",
"id": h.config.DatabaseAccount,
"_rid": fmt.Sprintf("%s.%s", h.config.DatabaseAccount, h.config.DatabaseDomain),
"media": "//media/",
"addresses": "//addresses/",
"_dbs": "//dbs/",
"writableLocations": []map[string]interface{}{
{
"name": "South Central US",
"databaseAccountEndpoint": h.config.DatabaseEndpoint,
},
},
"readableLocations": []map[string]interface{}{
{
"name": "South Central US",
"databaseAccountEndpoint": h.config.DatabaseEndpoint,
},
},
"enableMultipleWriteLocations": false,
"continuousBackupEnabled": false,
"enableNRegionSynchronousCommit": false,
"userReplicationPolicy": map[string]interface{}{
"asyncReplication": false,
"minReplicaSetSize": 1,
"maxReplicasetSize": 4,
},
"userConsistencyPolicy": map[string]interface{}{"defaultConsistencyLevel": "Session"},
"systemReplicationPolicy": map[string]interface{}{"minReplicaSetSize": 1, "maxReplicasetSize": 4},
"readPolicy": map[string]interface{}{"primaryReadCoefficient": 1, "secondaryReadCoefficient": 1},
"queryEngineConfiguration": "{\"allowNewKeywords\":true,\"maxJoinsPerSqlQuery\":10,\"maxQueryRequestTimeoutFraction\":0.9,\"maxSqlQueryInputLength\":524288,\"maxUdfRefPerSqlQuery\":10,\"queryMaxInMemorySortDocumentCount\":-1000,\"spatialMaxGeometryPointCount\":256,\"sqlAllowNonFiniteNumbers\":false,\"sqlDisableOptimizationFlags\":0,\"enableSpatialIndexing\":true,\"maxInExpressionItemsCount\":2147483647,\"maxLogicalAndPerSqlQuery\":2147483647,\"maxLogicalOrPerSqlQuery\":2147483647,\"maxSpatialQueryCells\":2147483647,\"sqlAllowAggregateFunctions\":true,\"sqlAllowGroupByClause\":true,\"sqlAllowLike\":true,\"sqlAllowSubQuery\":true,\"sqlAllowScalarSubQuery\":true,\"sqlAllowTop\":true}",
})
}
type Address struct {
IsPrimary bool `json:"isPrimary"`
PhyscialUri string `json:"physcialUri"`
IsAuxiliary bool `json:"isAuxiliary"`
PartitionTargetReplicaSetSize int `json:"partitionTargetReplicaSetSize"`
Protocol string `json:"protocol"`
PartitionKeyRangeId string `json:"partitionKeyRangeId"`
PartitionIndex string `json:"partitionIndex"`
}
func (h *Handlers) GetAddresses(c *gin.Context) {
addresses := []Address{}
if h.config.EnableRntbd {
addresses = append(addresses, Address{
IsPrimary: true,
PhyscialUri: h.config.RntbdEndpoint,
IsAuxiliary: false,
PartitionTargetReplicaSetSize: 1,
Protocol: "rntbd",
PartitionKeyRangeId: "0",
PartitionIndex: "0@0",
})
}
if !strings.Contains(c.Request.RequestURI, "protocol%20eq%20rntbd") {
addresses = append(addresses, Address{
IsPrimary: true,
PhyscialUri: h.config.DatabaseEndpoint,
IsAuxiliary: false,
PartitionTargetReplicaSetSize: 1,
Protocol: "https",
PartitionKeyRangeId: "0",
PartitionIndex: "0@0",
})
}
c.IndentedJSON(http.StatusOK, gin.H{
"Addresss": addresses,
"_count": len(addresses),
})
} }
+103 -6
View File
@@ -1,23 +1,120 @@
package handlers package handlers
import ( import (
"fmt"
"net/http" "net/http"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/pikami/cosmium/internal/repositories" "github.com/pikami/cosmium/api/headers"
repositorymodels "github.com/pikami/cosmium/internal/repository_models" "github.com/pikami/cosmium/internal/constants"
"github.com/pikami/cosmium/internal/datastore"
) )
func GetAllStoredProcedures(c *gin.Context) { func (h *Handlers) GetAllStoredProcedures(c *gin.Context) {
databaseId := c.Param("databaseId") databaseId := c.Param("databaseId")
collectionId := c.Param("collId") collectionId := c.Param("collId")
sps, status := repositories.GetAllStoredProcedures(databaseId, collectionId) sps, status := h.dataStore.GetAllStoredProcedures(databaseId, collectionId)
if status == repositorymodels.StatusOk { if status == datastore.StatusOk {
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) {
databaseId := c.Param("databaseId")
collectionId := c.Param("collId")
spId := c.Param("spId")
sp, status := h.dataStore.GetStoredProcedure(databaseId, collectionId, spId)
if status == datastore.StatusOk {
c.IndentedJSON(http.StatusOK, sp)
return
}
if status == datastore.StatusNotFound {
c.IndentedJSON(http.StatusNotFound, constants.NotFoundResponse)
return
}
c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
}
func (h *Handlers) DeleteStoredProcedure(c *gin.Context) {
databaseId := c.Param("databaseId")
collectionId := c.Param("collId")
spId := c.Param("spId")
status := h.dataStore.DeleteStoredProcedure(databaseId, collectionId, spId)
if status == datastore.StatusOk {
c.Status(http.StatusNoContent)
return
}
if status == datastore.StatusNotFound {
c.IndentedJSON(http.StatusNotFound, constants.NotFoundResponse)
return
}
c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
}
func (h *Handlers) ReplaceStoredProcedure(c *gin.Context) {
databaseId := c.Param("databaseId")
collectionId := c.Param("collId")
spId := c.Param("spId")
var sp datastore.StoredProcedure
if err := c.BindJSON(&sp); err != nil {
c.IndentedJSON(http.StatusBadRequest, constants.BadRequestResponse)
return
}
status := h.dataStore.DeleteStoredProcedure(databaseId, collectionId, spId)
if status == datastore.StatusNotFound {
c.IndentedJSON(http.StatusNotFound, constants.NotFoundResponse)
return
}
createdSP, status := h.dataStore.CreateStoredProcedure(databaseId, collectionId, sp)
if status == datastore.Conflict {
c.IndentedJSON(http.StatusConflict, constants.ConflictResponse)
return
}
if status == datastore.StatusOk {
c.IndentedJSON(http.StatusOK, createdSP)
return
}
c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
}
func (h *Handlers) CreateStoredProcedure(c *gin.Context) {
databaseId := c.Param("databaseId")
collectionId := c.Param("collId")
var sp datastore.StoredProcedure
if err := c.BindJSON(&sp); err != nil {
c.IndentedJSON(http.StatusBadRequest, constants.BadRequestResponse)
return
}
createdSP, status := h.dataStore.CreateStoredProcedure(databaseId, collectionId, sp)
if status == datastore.Conflict {
c.IndentedJSON(http.StatusConflict, constants.ConflictResponse)
return
}
if status == datastore.StatusOk {
c.IndentedJSON(http.StatusCreated, createdSP)
return
}
c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
+103 -6
View File
@@ -1,23 +1,120 @@
package handlers package handlers
import ( import (
"fmt"
"net/http" "net/http"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/pikami/cosmium/internal/repositories" "github.com/pikami/cosmium/api/headers"
repositorymodels "github.com/pikami/cosmium/internal/repository_models" "github.com/pikami/cosmium/internal/constants"
"github.com/pikami/cosmium/internal/datastore"
) )
func GetAllTriggers(c *gin.Context) { func (h *Handlers) GetAllTriggers(c *gin.Context) {
databaseId := c.Param("databaseId") databaseId := c.Param("databaseId")
collectionId := c.Param("collId") collectionId := c.Param("collId")
triggers, status := repositories.GetAllTriggers(databaseId, collectionId) triggers, status := h.dataStore.GetAllTriggers(databaseId, collectionId)
if status == repositorymodels.StatusOk { if status == datastore.StatusOk {
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) {
databaseId := c.Param("databaseId")
collectionId := c.Param("collId")
triggerId := c.Param("triggerId")
trigger, status := h.dataStore.GetTrigger(databaseId, collectionId, triggerId)
if status == datastore.StatusOk {
c.IndentedJSON(http.StatusOK, trigger)
return
}
if status == datastore.StatusNotFound {
c.IndentedJSON(http.StatusNotFound, constants.NotFoundResponse)
return
}
c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
}
func (h *Handlers) DeleteTrigger(c *gin.Context) {
databaseId := c.Param("databaseId")
collectionId := c.Param("collId")
triggerId := c.Param("triggerId")
status := h.dataStore.DeleteTrigger(databaseId, collectionId, triggerId)
if status == datastore.StatusOk {
c.Status(http.StatusNoContent)
return
}
if status == datastore.StatusNotFound {
c.IndentedJSON(http.StatusNotFound, constants.NotFoundResponse)
return
}
c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
}
func (h *Handlers) ReplaceTrigger(c *gin.Context) {
databaseId := c.Param("databaseId")
collectionId := c.Param("collId")
triggerId := c.Param("triggerId")
var trigger datastore.Trigger
if err := c.BindJSON(&trigger); err != nil {
c.IndentedJSON(http.StatusBadRequest, constants.BadRequestResponse)
return
}
status := h.dataStore.DeleteTrigger(databaseId, collectionId, triggerId)
if status == datastore.StatusNotFound {
c.IndentedJSON(http.StatusNotFound, constants.NotFoundResponse)
return
}
createdTrigger, status := h.dataStore.CreateTrigger(databaseId, collectionId, trigger)
if status == datastore.Conflict {
c.IndentedJSON(http.StatusConflict, constants.ConflictResponse)
return
}
if status == datastore.StatusOk {
c.IndentedJSON(http.StatusOK, createdTrigger)
return
}
c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
}
func (h *Handlers) CreateTrigger(c *gin.Context) {
databaseId := c.Param("databaseId")
collectionId := c.Param("collId")
var trigger datastore.Trigger
if err := c.BindJSON(&trigger); err != nil {
c.IndentedJSON(http.StatusBadRequest, constants.BadRequestResponse)
return
}
createdTrigger, status := h.dataStore.CreateTrigger(databaseId, collectionId, trigger)
if status == datastore.Conflict {
c.IndentedJSON(http.StatusConflict, constants.ConflictResponse)
return
}
if status == datastore.StatusOk {
c.IndentedJSON(http.StatusCreated, createdTrigger)
return
}
c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
+103 -6
View File
@@ -1,23 +1,120 @@
package handlers package handlers
import ( import (
"fmt"
"net/http" "net/http"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/pikami/cosmium/internal/repositories" "github.com/pikami/cosmium/api/headers"
repositorymodels "github.com/pikami/cosmium/internal/repository_models" "github.com/pikami/cosmium/internal/constants"
"github.com/pikami/cosmium/internal/datastore"
) )
func GetAllUserDefinedFunctions(c *gin.Context) { func (h *Handlers) GetAllUserDefinedFunctions(c *gin.Context) {
databaseId := c.Param("databaseId") databaseId := c.Param("databaseId")
collectionId := c.Param("collId") collectionId := c.Param("collId")
udfs, status := repositories.GetAllUserDefinedFunctions(databaseId, collectionId) udfs, status := h.dataStore.GetAllUserDefinedFunctions(databaseId, collectionId)
if status == repositorymodels.StatusOk { if status == datastore.StatusOk {
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) {
databaseId := c.Param("databaseId")
collectionId := c.Param("collId")
udfId := c.Param("udfId")
udf, status := h.dataStore.GetUserDefinedFunction(databaseId, collectionId, udfId)
if status == datastore.StatusOk {
c.IndentedJSON(http.StatusOK, udf)
return
}
if status == datastore.StatusNotFound {
c.IndentedJSON(http.StatusNotFound, constants.NotFoundResponse)
return
}
c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
}
func (h *Handlers) DeleteUserDefinedFunction(c *gin.Context) {
databaseId := c.Param("databaseId")
collectionId := c.Param("collId")
udfId := c.Param("udfId")
status := h.dataStore.DeleteUserDefinedFunction(databaseId, collectionId, udfId)
if status == datastore.StatusOk {
c.Status(http.StatusNoContent)
return
}
if status == datastore.StatusNotFound {
c.IndentedJSON(http.StatusNotFound, constants.NotFoundResponse)
return
}
c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
}
func (h *Handlers) ReplaceUserDefinedFunction(c *gin.Context) {
databaseId := c.Param("databaseId")
collectionId := c.Param("collId")
udfId := c.Param("udfId")
var udf datastore.UserDefinedFunction
if err := c.BindJSON(&udf); err != nil {
c.IndentedJSON(http.StatusBadRequest, constants.BadRequestResponse)
return
}
status := h.dataStore.DeleteUserDefinedFunction(databaseId, collectionId, udfId)
if status == datastore.StatusNotFound {
c.IndentedJSON(http.StatusNotFound, constants.NotFoundResponse)
return
}
createdUdf, status := h.dataStore.CreateUserDefinedFunction(databaseId, collectionId, udf)
if status == datastore.Conflict {
c.IndentedJSON(http.StatusConflict, constants.ConflictResponse)
return
}
if status == datastore.StatusOk {
c.IndentedJSON(http.StatusOK, createdUdf)
return
}
c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
}
func (h *Handlers) CreateUserDefinedFunction(c *gin.Context) {
databaseId := c.Param("databaseId")
collectionId := c.Param("collId")
var udf datastore.UserDefinedFunction
if err := c.BindJSON(&udf); err != nil {
c.IndentedJSON(http.StatusBadRequest, constants.BadRequestResponse)
return
}
createdUdf, status := h.dataStore.CreateUserDefinedFunction(databaseId, collectionId, udf)
if status == datastore.Conflict {
c.IndentedJSON(http.StatusConflict, constants.ConflictResponse)
return
}
if status == datastore.StatusOk {
c.IndentedJSON(http.StatusCreated, createdUdf)
return
}
c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
+30
View File
@@ -0,0 +1,30 @@
package headers
const (
AIM = "A-Im"
Authorization = "authorization"
CosmosLsn = "x-ms-cosmos-llsn"
ErrorCode = "x-ms-error-code"
ETag = "etag"
GlobalCommittedLsn = "x-ms-global-committed-lsn"
IfMatch = "if-match"
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"
MaxItemCount = "x-ms-max-item-count"
ContinuationToken = "x-ms-continuation"
// 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
// I kinda don't use these, but I've seen them in the wild xd
SupportedCapabilities = "x-ms-cosmos-sdk-supportedcapabilities"
ClientRetryAttemptCount = "x-ms-client-retry-attempt-count"
RemainingTimeInMsOnClient = "x-ms-remaining-time-in-ms-on-client"
ConsistencyLevel = "x-ms-consistency-level"
)
+125 -24
View File
@@ -1,45 +1,146 @@
package api package api
import ( import (
"context"
"fmt"
"net/http"
"sync"
"time"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/pikami/cosmium/api/handlers" "github.com/pikami/cosmium/api/handlers"
"github.com/pikami/cosmium/api/handlers/middleware" "github.com/pikami/cosmium/api/handlers/middleware"
"github.com/pikami/cosmium/internal/datastore"
"github.com/pikami/cosmium/internal/logger"
tlsprovider "github.com/pikami/cosmium/internal/tls_provider"
) )
func CreateRouter() *gin.Engine { var ginMux sync.Mutex
router := gin.Default()
func (s *ApiServer) CreateRouter(dataStore datastore.DataStore) {
routeHandlers := handlers.NewHandlers(dataStore, s.config)
ginMux.Lock()
gin.DefaultWriter = logger.InfoWriter()
gin.DefaultErrorWriter = logger.ErrorWriter()
if s.config.LogLevel != "debug" {
gin.SetMode(gin.ReleaseMode)
}
ginMux.Unlock()
router := gin.Default(func(e *gin.Engine) {
e.RedirectTrailingSlash = false
e.RemoveExtraSlash = true
})
if s.config.LogLevel == "debug" {
router.Use(middleware.RequestLogger()) router.Use(middleware.RequestLogger())
router.Use(middleware.Authentication()) }
router.GET("/dbs/:databaseId/colls/:collId/pkranges", handlers.GetPartitionKeyRanges) router.Use(middleware.StripTrailingSlashes(router, s.config))
router.Use(middleware.Authentication(s.config))
router.POST("/dbs/:databaseId/colls/:collId/docs", handlers.DocumentsPost) router.GET("/dbs/:databaseId/colls/:collId/pkranges", routeHandlers.GetPartitionKeyRanges)
router.GET("/dbs/:databaseId/colls/:collId/docs", handlers.GetAllDocuments)
router.GET("/dbs/:databaseId/colls/:collId/docs/:docId", handlers.GetDocument)
router.PUT("/dbs/:databaseId/colls/:collId/docs/:docId", handlers.ReplaceDocument)
router.DELETE("/dbs/:databaseId/colls/:collId/docs/:docId", handlers.DeleteDocument)
router.POST("/dbs/:databaseId/colls", handlers.CreateCollection) router.POST("/dbs/:databaseId/colls/:collId/docs", routeHandlers.DocumentsPost)
router.GET("/dbs/:databaseId/colls", handlers.GetAllCollections) router.GET("/dbs/:databaseId/colls/:collId/docs", routeHandlers.GetAllDocuments)
router.GET("/dbs/:databaseId/colls/:collId", handlers.GetCollection) router.GET("/dbs/:databaseId/colls/:collId/docs/:docId", routeHandlers.GetDocument)
router.DELETE("/dbs/:databaseId/colls/:collId", handlers.DeleteCollection) router.PUT("/dbs/:databaseId/colls/:collId/docs/:docId", routeHandlers.ReplaceDocument)
router.PATCH("/dbs/:databaseId/colls/:collId/docs/:docId", routeHandlers.PatchDocument)
router.DELETE("/dbs/:databaseId/colls/:collId/docs/:docId", routeHandlers.DeleteDocument)
router.POST("/dbs", handlers.CreateDatabase) router.POST("/dbs/:databaseId/colls", routeHandlers.CreateCollection)
router.GET("/dbs", handlers.GetAllDatabases) router.GET("/dbs/:databaseId/colls", routeHandlers.GetAllCollections)
router.GET("/dbs/:databaseId", handlers.GetDatabase) router.GET("/dbs/:databaseId/colls/:collId", routeHandlers.GetCollection)
router.DELETE("/dbs/:databaseId", handlers.DeleteDatabase) router.DELETE("/dbs/:databaseId/colls/:collId", routeHandlers.DeleteCollection)
router.GET("/dbs/:databaseId/colls/:collId/udfs", handlers.GetAllUserDefinedFunctions) router.POST("/dbs", routeHandlers.CreateDatabase)
router.GET("/dbs/:databaseId/colls/:collId/sprocs", handlers.GetAllStoredProcedures) router.GET("/dbs", routeHandlers.GetAllDatabases)
router.GET("/dbs/:databaseId/colls/:collId/triggers", handlers.GetAllTriggers) router.GET("/dbs/:databaseId", routeHandlers.GetDatabase)
router.DELETE("/dbs/:databaseId", routeHandlers.DeleteDatabase)
router.POST("/dbs/:databaseId/colls/:collId/triggers", routeHandlers.CreateTrigger)
router.GET("/dbs/:databaseId/colls/:collId/triggers", routeHandlers.GetAllTriggers)
router.GET("/dbs/:databaseId/colls/:collId/triggers/:triggerId", routeHandlers.GetTrigger)
router.PUT("/dbs/:databaseId/colls/:collId/triggers/:triggerId", routeHandlers.ReplaceTrigger)
router.DELETE("/dbs/:databaseId/colls/:collId/triggers/:triggerId", routeHandlers.DeleteTrigger)
router.POST("/dbs/:databaseId/colls/:collId/sprocs", routeHandlers.CreateStoredProcedure)
router.GET("/dbs/:databaseId/colls/:collId/sprocs", routeHandlers.GetAllStoredProcedures)
router.GET("/dbs/:databaseId/colls/:collId/sprocs/:sprocId", routeHandlers.GetStoredProcedure)
router.PUT("/dbs/:databaseId/colls/:collId/sprocs/:sprocId", routeHandlers.ReplaceStoredProcedure)
router.DELETE("/dbs/:databaseId/colls/:collId/sprocs/:sprocId", routeHandlers.DeleteStoredProcedure)
router.POST("/dbs/:databaseId/colls/:collId/udfs", routeHandlers.CreateUserDefinedFunction)
router.GET("/dbs/:databaseId/colls/:collId/udfs", routeHandlers.GetAllUserDefinedFunctions)
router.GET("/dbs/:databaseId/colls/:collId/udfs/:udfId", routeHandlers.GetUserDefinedFunction)
router.PUT("/dbs/:databaseId/colls/:collId/udfs/:udfId", routeHandlers.ReplaceUserDefinedFunction)
router.DELETE("/dbs/:databaseId/colls/:collId/udfs/:udfId", routeHandlers.DeleteUserDefinedFunction)
router.GET("/offers", handlers.GetOffers) router.GET("/offers", handlers.GetOffers)
router.GET("/", handlers.GetServerInfo) router.GET("/", routeHandlers.GetServerInfo)
router.GET("//addresses", routeHandlers.GetAddresses)
router.GET("/cosmium/export", handlers.CosmiumExport) router.GET("/cosmium/export", routeHandlers.CosmiumExport)
handlers.RegisterExplorerHandlers(router) routeHandlers.RegisterExplorerHandlers(router)
return router s.router = router
}
func (s *ApiServer) Start() error {
listenAddress := fmt.Sprintf(":%d", s.config.Port)
s.isActive = true
server := &http.Server{
Addr: listenAddress,
Handler: s.router.Handler(),
}
errChan := make(chan error, 1)
go func() {
<-s.stopServer
logger.InfoLn("Shutting down server...")
err := server.Shutdown(context.TODO())
if err != nil {
logger.ErrorLn("Failed to shutdown server:", err)
}
s.onServerShutdown <- true
}()
go func() {
var err error
if s.config.DisableTls {
logger.Infof("Listening and serving HTTP on %s\n", server.Addr)
err = server.ListenAndServe()
} else if s.config.TLS_CertificatePath != "" && s.config.TLS_CertificateKey != "" {
logger.Infof("Listening and serving HTTPS on %s\n", server.Addr)
err = server.ListenAndServeTLS(
s.config.TLS_CertificatePath,
s.config.TLS_CertificateKey)
} else {
tlsConfig := tlsprovider.GetDefaultTlsConfig()
server.TLSConfig = tlsConfig
logger.Infof("Listening and serving HTTPS on %s\n", server.Addr)
err = server.ListenAndServeTLS("", "")
}
if err != nil && err != http.ErrServerClosed {
logger.ErrorLn("Failed to start server:", err)
errChan <- err
} else {
errChan <- nil
}
s.isActive = false
}()
select {
case err := <-errChan:
return err
case <-time.After(50 * time.Millisecond):
return nil
}
} }
+36 -32
View File
@@ -2,27 +2,24 @@ package tests_test
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
"testing" "testing"
"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/api/config"
"github.com/pikami/cosmium/internal/repositories"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func Test_Authentication(t *testing.T) { func Test_Authentication(t *testing.T) {
ts := runTestServer() ts := runTestServer()
defer ts.Close() defer ts.Server.Close()
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) {
repositories.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,28 +32,10 @@ func Test_Authentication(t *testing.T) {
assert.Equal(t, createResponse.DatabaseProperties.ID, testDatabaseName) assert.Equal(t, createResponse.DatabaseProperties.ID, testDatabaseName)
}) })
t.Run("Should get 200 when wrong account key is used, but authentication is dissabled", func(t *testing.T) {
config.Config.DisableAuth = true
repositories.DeleteDatabase(testDatabaseName)
client, err := azcosmos.NewClientFromConnectionString(
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, "AAAA"),
&azcosmos.ClientOptions{},
)
assert.Nil(t, err)
createResponse, err := client.CreateDatabase(
context.TODO(),
azcosmos.DatabaseProperties{ID: testDatabaseName},
&azcosmos.CreateDatabaseOptions{})
assert.Nil(t, err)
assert.Equal(t, createResponse.DatabaseProperties.ID, testDatabaseName)
config.Config.DisableAuth = false
})
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) {
repositories.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)
@@ -66,12 +45,7 @@ func Test_Authentication(t *testing.T) {
azcosmos.DatabaseProperties{ID: testDatabaseName}, azcosmos.DatabaseProperties{ID: testDatabaseName},
&azcosmos.CreateDatabaseOptions{}) &azcosmos.CreateDatabaseOptions{})
var respErr *azcore.ResponseError assert.Contains(t, err.Error(), "401 Unauthorized")
if errors.As(err, &respErr) {
assert.Equal(t, respErr.StatusCode, http.StatusUnauthorized)
} else {
panic(err)
}
}) })
t.Run("Should allow unauthorized requests to /_explorer", func(t *testing.T) { t.Run("Should allow unauthorized requests to /_explorer", func(t *testing.T) {
@@ -85,3 +59,33 @@ func Test_Authentication(t *testing.T) {
assert.Contains(t, string(responseBody), "BACKEND_ENDPOINT") assert.Contains(t, string(responseBody), "BACKEND_ENDPOINT")
}) })
} }
func Test_Authentication_Disabled(t *testing.T) {
ts := runTestServerCustomConfig(&config.ServerConfig{
AccountKey: config.DefaultAccountKey,
ExplorerPath: "/tmp/nothing",
ExplorerBaseUrlLocation: config.ExplorerBaseUrlLocation,
DisableAuth: true,
})
defer ts.Server.Close()
t.Run("Should get 200 when wrong account key is used, but authentication is dissabled", func(t *testing.T) {
ts.DataStore.DeleteDatabase(testDatabaseName)
client, err := azcosmos.NewClientFromConnectionString(
formatConnectionString(ts.URL, "AAAA"),
&azcosmos.ClientOptions{},
)
assert.Nil(t, err)
createResponse, err := client.CreateDatabase(
context.TODO(),
azcosmos.DatabaseProperties{ID: testDatabaseName},
&azcosmos.CreateDatabaseOptions{})
assert.Nil(t, err)
assert.Equal(t, createResponse.DatabaseProperties.ID, testDatabaseName)
})
}
func formatConnectionString(endpoint, key string) string {
return fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", endpoint, key)
}
+42 -21
View File
@@ -3,33 +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/repositories"
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
"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.Close()
client, err := azcosmos.NewClientFromConnectionString( setUp := func(ts *TestServer, client *azcosmos.Client) *azcosmos.DatabaseClient {
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, config.Config.AccountKey), ts.DataStore.CreateDatabase(datastore.Database{ID: testDatabaseName})
&azcosmos.ClientOptions{},
)
assert.Nil(t, err)
repositories.CreateDatabase(repositorymodels.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,
@@ -40,7 +36,7 @@ func Test_Collections(t *testing.T) {
}) })
t.Run("Should return conflict when collection exists", func(t *testing.T) { t.Run("Should return conflict when collection exists", func(t *testing.T) {
repositories.CreateCollection(testDatabaseName, repositorymodels.Collection{ ts.DataStore.CreateCollection(testDatabaseName, datastore.Collection{
ID: testCollectionName, ID: testCollectionName,
}) })
@@ -58,9 +54,11 @@ 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) {
repositories.CreateCollection(testDatabaseName, repositorymodels.Collection{ ts.DataStore.CreateCollection(testDatabaseName, datastore.Collection{
ID: testCollectionName, ID: testCollectionName,
}) })
@@ -74,7 +72,7 @@ func Test_Collections(t *testing.T) {
}) })
t.Run("Should return not found when collection does not exist", func(t *testing.T) { t.Run("Should return not found when collection does not exist", func(t *testing.T) {
repositories.DeleteCollection(testDatabaseName, testCollectionName) ts.DataStore.DeleteCollection(testDatabaseName, testCollectionName)
collectionResponse, err := databaseClient.NewContainer(testCollectionName) collectionResponse, err := databaseClient.NewContainer(testCollectionName)
assert.Nil(t, err) assert.Nil(t, err)
@@ -91,9 +89,11 @@ 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) {
repositories.CreateCollection(testDatabaseName, repositorymodels.Collection{ ts.DataStore.CreateCollection(testDatabaseName, datastore.Collection{
ID: testCollectionName, ID: testCollectionName,
}) })
@@ -106,7 +106,7 @@ func Test_Collections(t *testing.T) {
}) })
t.Run("Should return not found when collection does not exist", func(t *testing.T) { t.Run("Should return not found when collection does not exist", func(t *testing.T) {
repositories.DeleteCollection(testDatabaseName, testCollectionName) ts.DataStore.DeleteCollection(testDatabaseName, testCollectionName)
collectionResponse, err := databaseClient.NewContainer(testCollectionName) collectionResponse, err := databaseClient.NewContainer(testCollectionName)
assert.Nil(t, err) assert.Nil(t, err)
@@ -121,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")
})
}) })
} }
+95 -4
View File
@@ -1,17 +1,64 @@
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"
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/stretchr/testify/assert"
) )
func runTestServer() *httptest.Server { type TestServer struct {
config.Config.AccountKey = config.DefaultAccountKey Server *httptest.Server
config.Config.ExplorerPath = "/tmp/nothing" DataStore datastore.DataStore
URL string
}
return httptest.NewServer(api.CreateRouter()) func getDefaultTestServerConfig() *config.ServerConfig {
return &config.ServerConfig{
AccountKey: config.DefaultAccountKey,
ExplorerPath: "/tmp/nothing",
ExplorerBaseUrlLocation: config.ExplorerBaseUrlLocation,
DataStore: "json",
}
}
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())
configuration.DatabaseEndpoint = server.URL
return &TestServer{
Server: server,
DataStore: dataStore,
URL: server.URL,
}
}
func runTestServer() *TestServer {
config := getDefaultTestServerConfig()
config.LogLevel = "debug"
logger.SetLogLevel(logger.LogLevelDebug)
return runTestServerCustomConfig(config)
} }
const ( const (
@@ -19,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)
}
}
+32 -21
View File
@@ -3,31 +3,21 @@ 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/repositories"
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
"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.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.Config.AccountKey),
&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) {
repositories.DeleteDatabase(testDatabaseName) ts.DataStore.DeleteDatabase(testDatabaseName)
createResponse, err := client.CreateDatabase(context.TODO(), azcosmos.DatabaseProperties{ createResponse, err := client.CreateDatabase(context.TODO(), azcosmos.DatabaseProperties{
ID: testDatabaseName, ID: testDatabaseName,
@@ -38,7 +28,7 @@ func Test_Databases(t *testing.T) {
}) })
t.Run("Should return conflict when database exists", func(t *testing.T) { t.Run("Should return conflict when database exists", func(t *testing.T) {
repositories.CreateDatabase(repositorymodels.Database{ ts.DataStore.CreateDatabase(datastore.Database{
ID: testDatabaseName, ID: testDatabaseName,
}) })
@@ -56,9 +46,9 @@ 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) {
repositories.CreateDatabase(repositorymodels.Database{ ts.DataStore.CreateDatabase(datastore.Database{
ID: testDatabaseName, ID: testDatabaseName,
}) })
@@ -72,7 +62,7 @@ func Test_Databases(t *testing.T) {
}) })
t.Run("Should return not found when database does not exist", func(t *testing.T) { t.Run("Should return not found when database does not exist", func(t *testing.T) {
repositories.DeleteDatabase(testDatabaseName) ts.DataStore.DeleteDatabase(testDatabaseName)
databaseResponse, err := client.NewDatabase(testDatabaseName) databaseResponse, err := client.NewDatabase(testDatabaseName)
assert.Nil(t, err) assert.Nil(t, err)
@@ -89,9 +79,9 @@ 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) {
repositories.CreateDatabase(repositorymodels.Database{ ts.DataStore.CreateDatabase(datastore.Database{
ID: testDatabaseName, ID: testDatabaseName,
}) })
@@ -104,7 +94,7 @@ func Test_Databases(t *testing.T) {
}) })
t.Run("Should return not found when database does not exist", func(t *testing.T) { t.Run("Should return not found when database does not exist", func(t *testing.T) {
repositories.DeleteDatabase(testDatabaseName) ts.DataStore.DeleteDatabase(testDatabaseName)
databaseResponse, err := client.NewDatabase(testDatabaseName) databaseResponse, err := client.NewDatabase(testDatabaseName)
assert.Nil(t, err) assert.Nil(t, err)
@@ -119,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")
})
}) })
} }
@@ -0,0 +1,47 @@
package tests_test
import (
"testing"
"github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"
)
func Test_Documents_ArrayContains(t *testing.T) {
presets := []testPreset{PresetJsonStore, PresetBadgerStore}
runTestsWithPresets(t, "Test_Documents_ArrayContains", presets, func(t *testing.T, ts *TestServer, client *azcosmos.Client) {
collectionClient := documents_InitializeDb(t, ts)
t.Run("Should execute ARRAY_CONTAINS() without partial match argument", func(t *testing.T) {
testCosmosQuery(t, collectionClient,
`SELECT VALUE ARRAY_CONTAINS(["apple", "banana", "cherry"], "banana") FROM c ORDER BY c.id`,
nil,
[]interface{}{true, true},
)
})
t.Run("Should execute ARRAY_CONTAINS() returning false for missing item", func(t *testing.T) {
testCosmosQuery(t, collectionClient,
`SELECT VALUE ARRAY_CONTAINS(["apple", "banana", "cherry"], "grape") FROM c ORDER BY c.id`,
nil,
[]interface{}{false, false},
)
})
t.Run("Should execute ARRAY_CONTAINS() with object full match", func(t *testing.T) {
testCosmosQuery(t, collectionClient,
`SELECT VALUE ARRAY_CONTAINS([{"name": "apple", "color": "red"}], {"name": "apple"}) FROM c ORDER BY c.id`,
nil,
[]interface{}{false, false},
)
})
t.Run("Should execute ARRAY_CONTAINS() with object partial match", func(t *testing.T) {
testCosmosQuery(t, collectionClient,
`SELECT VALUE ARRAY_CONTAINS([{"name": "apple", "color": "red"}], {"name": "apple"}, true) FROM c ORDER BY c.id`,
nil,
[]interface{}{true, true},
)
})
})
}
@@ -0,0 +1,73 @@
package tests_test
import (
"fmt"
"testing"
"github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"
"github.com/pikami/cosmium/api/config"
"github.com/pikami/cosmium/internal/datastore"
"github.com/stretchr/testify/assert"
)
func documents_InitializeSingleDocumentDb(t *testing.T, ts *TestServer) *azcosmos.ContainerClient {
ts.DataStore.CreateDatabase(datastore.Database{ID: testDatabaseName})
ts.DataStore.CreateCollection(testDatabaseName, datastore.Collection{
ID: testCollectionName,
PartitionKey: struct {
Paths []string "json:\"paths\""
Kind string "json:\"kind\""
Version int "json:\"Version\""
}{
Paths: []string{"/pk"},
},
})
ts.DataStore.CreateDocument(testDatabaseName, testCollectionName, map[string]interface{}{"id": "regexmatch-test", "pk": "regexmatch-test"})
client, err := azcosmos.NewClientFromConnectionString(
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, config.DefaultAccountKey),
&azcosmos.ClientOptions{},
)
assert.Nil(t, err)
collectionClient, err := client.NewContainer(testDatabaseName, testCollectionName)
assert.Nil(t, err)
return collectionClient
}
func Test_Documents_RegexMatch(t *testing.T) {
presets := []testPreset{PresetJsonStore, PresetBadgerStore}
runTestsWithPresets(t, "Test_Documents_RegexMatch", presets, func(t *testing.T, ts *TestServer, client *azcosmos.Client) {
collectionClient := documents_InitializeSingleDocumentDb(t, ts)
t.Run("Should execute REGEXMATCH()", func(t *testing.T) {
testCosmosQuery(t, collectionClient,
`SELECT VALUE {
noModifiers: REGEXMATCH("abcd", "ABC"),
caseInsensitive: REGEXMATCH("abcd", "ABC", "i"),
wildcardCharacter: REGEXMATCH("abcd", "ab.", ""),
ignoreWhiteSpace: REGEXMATCH("abcd", "ab c", "x"),
caseInsensitiveAndIgnoreWhiteSpace: REGEXMATCH("abcd", "aB c", "ix"),
containNumberBetweenZeroAndNine: REGEXMATCH("03a", "[0-9]"),
containPrefix: REGEXMATCH("salt3824908", "salt{1}"),
containsFiveLetterWordStartingWithS: REGEXMATCH("shame", "s....", "i")
}`,
nil,
[]interface{}{
map[string]interface{}{
"noModifiers": false,
"caseInsensitive": true,
"wildcardCharacter": true,
"ignoreWhiteSpace": true,
"caseInsensitiveAndIgnoreWhiteSpace": true,
"containNumberBetweenZeroAndNine": true,
"containPrefix": true,
"containsFiveLetterWordStartingWithS": true,
},
},
)
})
})
}
+505 -11
View File
@@ -3,14 +3,20 @@ package tests_test
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"io"
"net/http"
"reflect" "reflect"
"sync"
"testing" "testing"
"time"
"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/api/config"
"github.com/pikami/cosmium/internal/repositories" continuationtoken "github.com/pikami/cosmium/internal/continuation_token"
repositorymodels "github.com/pikami/cosmium/internal/repository_models" "github.com/pikami/cosmium/internal/datastore"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@@ -49,9 +55,9 @@ func testCosmosQuery(t *testing.T,
} }
} }
func Test_Documents(t *testing.T) { func documents_InitializeDb(t *testing.T, ts *TestServer) *azcosmos.ContainerClient {
repositories.CreateDatabase(repositorymodels.Database{ID: testDatabaseName}) ts.DataStore.CreateDatabase(datastore.Database{ID: testDatabaseName})
repositories.CreateCollection(testDatabaseName, repositorymodels.Collection{ ts.DataStore.CreateCollection(testDatabaseName, datastore.Collection{
ID: testCollectionName, ID: testCollectionName,
PartitionKey: struct { PartitionKey: struct {
Paths []string "json:\"paths\"" Paths []string "json:\"paths\""
@@ -61,14 +67,11 @@ func Test_Documents(t *testing.T) {
Paths: []string{"/pk"}, Paths: []string{"/pk"},
}, },
}) })
repositories.CreateDocument(testDatabaseName, testCollectionName, map[string]interface{}{"id": "12345", "pk": "123", "isCool": false}) ts.DataStore.CreateDocument(testDatabaseName, testCollectionName, map[string]interface{}{"id": "12345", "pk": "123", "isCool": false, "arr": []int{1, 2, 3}})
repositories.CreateDocument(testDatabaseName, testCollectionName, map[string]interface{}{"id": "67890", "pk": "456", "isCool": true}) ts.DataStore.CreateDocument(testDatabaseName, testCollectionName, map[string]interface{}{"id": "67890", "pk": "456", "isCool": true, "arr": []int{6, 7, 8}})
ts := runTestServer()
defer ts.Close()
client, err := azcosmos.NewClientFromConnectionString( client, err := azcosmos.NewClientFromConnectionString(
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, config.Config.AccountKey), fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, config.DefaultAccountKey),
&azcosmos.ClientOptions{}, &azcosmos.ClientOptions{},
) )
assert.Nil(t, err) assert.Nil(t, err)
@@ -76,6 +79,15 @@ func Test_Documents(t *testing.T) {
collectionClient, err := client.NewContainer(testDatabaseName, testCollectionName) collectionClient, err := client.NewContainer(testDatabaseName, testCollectionName)
assert.Nil(t, err) assert.Nil(t, err)
return collectionClient
}
func Test_Documents(t *testing.T) {
presets := []testPreset{PresetJsonStore, PresetBadgerStore}
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,
"SELECT c.id, c[\"pk\"] FROM c ORDER BY c.id", "SELECT c.id, c[\"pk\"] FROM c ORDER BY c.id",
@@ -136,4 +148,486 @@ func Test_Documents(t *testing.T) {
}, },
) )
}) })
t.Run("Should query document with query parameters as accessor", func(t *testing.T) {
testCosmosQuery(t, collectionClient,
`select c.id
FROM c
WHERE c[@param]="67890"
ORDER BY c.id`,
[]azcosmos.QueryParameter{
{Name: "@param", Value: "id"},
},
[]interface{}{
map[string]interface{}{"id": "67890"},
},
)
})
t.Run("Should query array accessor", func(t *testing.T) {
testCosmosQuery(t, collectionClient,
`SELECT c.id,
c["arr"][0] AS arr0,
c["arr"][1] AS arr1,
c["arr"][2] AS arr2,
c["arr"][3] AS arr3
FROM c ORDER BY c.id`,
nil,
[]interface{}{
map[string]interface{}{"id": "12345", "arr0": 1.0, "arr1": 2.0, "arr2": 3.0, "arr3": nil},
map[string]interface{}{"id": "67890", "arr0": 6.0, "arr1": 7.0, "arr2": 8.0, "arr3": nil},
},
)
})
t.Run("Should handle parallel writes", func(t *testing.T) {
var wg sync.WaitGroup
rutineCount := 100
results := make(chan error, rutineCount)
createCall := func(i int) {
defer wg.Done()
item := map[string]interface{}{
"id": fmt.Sprintf("id-%d", i),
"pk": fmt.Sprintf("pk-%d", i),
"val": i,
}
bytes, err := json.Marshal(item)
if err != nil {
results <- err
return
}
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
_, err = collectionClient.CreateItem(
ctx,
azcosmos.PartitionKey{},
bytes,
&azcosmos.ItemOptions{
EnableContentResponseOnWrite: false,
},
)
results <- err
collectionClient.ReadItem(ctx, azcosmos.PartitionKey{}, fmt.Sprintf("id-%d", i), nil)
collectionClient.DeleteItem(ctx, azcosmos.PartitionKey{}, fmt.Sprintf("id-%d", i), nil)
}
for i := 0; i < rutineCount; i++ {
wg.Add(1)
go createCall(i)
}
wg.Wait()
close(results)
for err := range results {
if err != nil {
t.Errorf("Error creating item: %v", err)
}
}
})
})
runTestsWithPresets(t, "Test_Documents_Patch", presets, func(t *testing.T, ts *TestServer, client *azcosmos.Client) {
collectionClient := documents_InitializeDb(t, ts)
t.Run("Should PATCH document", func(t *testing.T) {
context := context.TODO()
expectedData := map[string]interface{}{"id": "67890", "pk": "666", "newField": "newValue", "incr": 15., "setted": "isSet"}
patch := azcosmos.PatchOperations{}
patch.AppendAdd("/newField", "newValue")
patch.AppendIncrement("/incr", 15)
patch.AppendRemove("/isCool")
patch.AppendReplace("/pk", "666")
patch.AppendSet("/setted", "isSet")
itemResponse, err := collectionClient.PatchItem(
context,
azcosmos.PartitionKey{},
"67890",
patch,
&azcosmos.ItemOptions{
EnableContentResponseOnWrite: false,
},
)
assert.Nil(t, err)
var itemResponseBody map[string]interface{}
json.Unmarshal(itemResponse.Value, &itemResponseBody)
assert.Equal(t, expectedData["id"], itemResponseBody["id"])
assert.Equal(t, expectedData["pk"], itemResponseBody["pk"])
assert.Empty(t, itemResponseBody["isCool"])
assert.Equal(t, expectedData["newField"], itemResponseBody["newField"])
assert.Equal(t, expectedData["incr"], itemResponseBody["incr"])
assert.Equal(t, expectedData["setted"], itemResponseBody["setted"])
})
t.Run("Should not allow to PATCH document ID", func(t *testing.T) {
context := context.TODO()
patch := azcosmos.PatchOperations{}
patch.AppendReplace("/id", "newValue")
_, err := collectionClient.PatchItem(
context,
azcosmos.PartitionKey{},
"67890",
patch,
&azcosmos.ItemOptions{
EnableContentResponseOnWrite: false,
},
)
assert.NotNil(t, err)
var respErr *azcore.ResponseError
if errors.As(err, &respErr) {
assert.Equal(t, http.StatusUnprocessableEntity, respErr.StatusCode)
} else {
panic(err)
}
})
t.Run("CreateItem", func(t *testing.T) {
context := context.TODO()
item := map[string]interface{}{
"Id": "6789011",
"pk": "456",
"newField": "newValue2",
}
bytes, err := json.Marshal(item)
assert.Nil(t, err)
r, err2 := collectionClient.CreateItem(
context,
azcosmos.PartitionKey{},
bytes,
&azcosmos.ItemOptions{
EnableContentResponseOnWrite: false,
},
)
assert.NotNil(t, r)
assert.Nil(t, err2)
})
t.Run("CreateItem that already exists", func(t *testing.T) {
context := context.TODO()
item := map[string]interface{}{"id": "12345", "pk": "123", "isCool": false, "arr": []int{1, 2, 3}}
bytes, err := json.Marshal(item)
assert.Nil(t, err)
r, err := collectionClient.CreateItem(
context,
azcosmos.PartitionKey{},
bytes,
&azcosmos.ItemOptions{
EnableContentResponseOnWrite: false,
},
)
assert.NotNil(t, r)
assert.NotNil(t, err)
var respErr *azcore.ResponseError
if errors.As(err, &respErr) {
assert.Equal(t, http.StatusConflict, respErr.StatusCode)
} else {
panic(err)
}
})
t.Run("UpsertItem new", func(t *testing.T) {
context := context.TODO()
item := map[string]interface{}{"id": "123456", "pk": "1234", "isCool": false, "arr": []int{1, 2, 3}}
bytes, err := json.Marshal(item)
assert.Nil(t, err)
r, err2 := collectionClient.UpsertItem(
context,
azcosmos.PartitionKey{},
bytes,
&azcosmos.ItemOptions{
EnableContentResponseOnWrite: false,
},
)
assert.NotNil(t, r)
assert.Nil(t, err2)
})
t.Run("UpsertItem that already exists", func(t *testing.T) {
context := context.TODO()
item := map[string]interface{}{"id": "12345", "pk": "123", "isCool": false, "arr": []int{1, 2, 3, 4}}
bytes, err := json.Marshal(item)
assert.Nil(t, err)
r, err2 := collectionClient.UpsertItem(
context,
azcosmos.PartitionKey{},
bytes,
&azcosmos.ItemOptions{
EnableContentResponseOnWrite: false,
},
)
assert.NotNil(t, r)
assert.Nil(t, err2)
})
})
runTestsWithPresets(t, "Test_Documents_ETag_OptimisticConcurrency", presets, func(t *testing.T, ts *TestServer, client *azcosmos.Client) {
collectionClient := documents_InitializeDb(t, ts)
t.Run("Should fail replace with incorrect etag", func(t *testing.T) {
context := context.TODO()
item := map[string]interface{}{"id": "12345", "pk": "123", "isCool": true}
bytes, err := json.Marshal(item)
assert.Nil(t, err)
wrongETag := azcore.ETag("\"incorrect-etag\"")
_, err = collectionClient.ReplaceItem(
context,
azcosmos.PartitionKey{},
"12345",
bytes,
&azcosmos.ItemOptions{IfMatchEtag: &wrongETag},
)
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, false, document["isCool"])
})
t.Run("Should replace with correct etag", func(t *testing.T) {
context := context.TODO()
readResponse, err := collectionClient.ReadItem(context, azcosmos.PartitionKey{}, "12345", nil)
assert.Nil(t, err)
assert.NotEmpty(t, readResponse.ETag)
var item map[string]interface{}
err = json.Unmarshal(readResponse.Value, &item)
assert.Nil(t, err)
assert.Equal(t, string(readResponse.ETag), item["_etag"])
item["pk"] = "999"
item["isCool"] = true
bytes, err := json.Marshal(item)
assert.Nil(t, err)
etag := readResponse.ETag
_, err = collectionClient.ReplaceItem(
context,
azcosmos.PartitionKey{},
"12345",
bytes,
&azcosmos.ItemOptions{IfMatchEtag: &etag},
)
assert.Nil(t, err)
document, status := ts.DataStore.GetDocument(testDatabaseName, testCollectionName, "12345")
assert.Equal(t, datastore.StatusOk, status)
assert.Equal(t, "999", document["pk"])
assert.Equal(t, true, document["isCool"])
})
})
runTestsWithPresets(t, "Test_Documents_TransactionalBatch", presets, func(t *testing.T, ts *TestServer, client *azcosmos.Client) {
collectionClient := documents_InitializeDb(t, ts)
t.Run("Should execute CREATE transactional batch", func(t *testing.T) {
context := context.TODO()
batch := collectionClient.NewTransactionalBatch(azcosmos.NewPartitionKeyString("pk"))
newItem := map[string]interface{}{
"id": "678901",
}
bytes, err := json.Marshal(newItem)
assert.Nil(t, err)
batch.CreateItem(bytes, nil)
response, err := collectionClient.ExecuteTransactionalBatch(context, batch, &azcosmos.TransactionalBatchOptions{})
assert.Nil(t, err)
assert.True(t, response.Success)
assert.Equal(t, 1, len(response.OperationResults))
operationResponse := response.OperationResults[0]
assert.NotNil(t, operationResponse)
assert.NotNil(t, operationResponse.ResourceBody)
assert.Equal(t, int32(http.StatusCreated), operationResponse.StatusCode)
var itemResponseBody map[string]interface{}
json.Unmarshal(operationResponse.ResourceBody, &itemResponseBody)
assert.Equal(t, newItem["id"], itemResponseBody["id"])
createdDoc, _ := ts.DataStore.GetDocument(testDatabaseName, testCollectionName, newItem["id"].(string))
assert.Equal(t, newItem["id"], createdDoc["id"])
})
t.Run("Should execute DELETE transactional batch", func(t *testing.T) {
context := context.TODO()
batch := collectionClient.NewTransactionalBatch(azcosmos.NewPartitionKeyString("pk"))
batch.DeleteItem("12345", nil)
response, err := collectionClient.ExecuteTransactionalBatch(context, batch, &azcosmos.TransactionalBatchOptions{})
assert.Nil(t, err)
assert.True(t, response.Success)
assert.Equal(t, 1, len(response.OperationResults))
operationResponse := response.OperationResults[0]
assert.NotNil(t, operationResponse)
assert.Equal(t, int32(http.StatusNoContent), operationResponse.StatusCode)
_, status := ts.DataStore.GetDocument(testDatabaseName, testCollectionName, "12345")
assert.Equal(t, datastore.StatusNotFound, status)
})
t.Run("Should execute REPLACE transactional batch", func(t *testing.T) {
context := context.TODO()
batch := collectionClient.NewTransactionalBatch(azcosmos.NewPartitionKeyString("pk"))
newItem := map[string]interface{}{
"id": "67890",
"pk": "666",
}
bytes, err := json.Marshal(newItem)
assert.Nil(t, err)
batch.ReplaceItem("67890", bytes, nil)
response, err := collectionClient.ExecuteTransactionalBatch(context, batch, &azcosmos.TransactionalBatchOptions{})
assert.Nil(t, err)
assert.True(t, response.Success)
assert.Equal(t, 1, len(response.OperationResults))
operationResponse := response.OperationResults[0]
assert.NotNil(t, operationResponse)
assert.NotNil(t, operationResponse.ResourceBody)
assert.Equal(t, int32(http.StatusCreated), operationResponse.StatusCode)
var itemResponseBody map[string]interface{}
json.Unmarshal(operationResponse.ResourceBody, &itemResponseBody)
assert.Equal(t, newItem["id"], itemResponseBody["id"])
assert.Equal(t, newItem["pk"], itemResponseBody["pk"])
updatedDoc, _ := ts.DataStore.GetDocument(testDatabaseName, testCollectionName, newItem["id"].(string))
assert.Equal(t, newItem["id"], updatedDoc["id"])
assert.Equal(t, newItem["pk"], updatedDoc["pk"])
})
t.Run("Should execute UPSERT transactional batch", func(t *testing.T) {
context := context.TODO()
batch := collectionClient.NewTransactionalBatch(azcosmos.NewPartitionKeyString("pk"))
newItem := map[string]interface{}{
"id": "678901",
"pk": "666",
}
bytes, err := json.Marshal(newItem)
assert.Nil(t, err)
batch.UpsertItem(bytes, nil)
response, err := collectionClient.ExecuteTransactionalBatch(context, batch, &azcosmos.TransactionalBatchOptions{})
assert.Nil(t, err)
assert.True(t, response.Success)
assert.Equal(t, 1, len(response.OperationResults))
operationResponse := response.OperationResults[0]
assert.NotNil(t, operationResponse)
assert.NotNil(t, operationResponse.ResourceBody)
assert.Equal(t, int32(http.StatusCreated), operationResponse.StatusCode)
var itemResponseBody map[string]interface{}
json.Unmarshal(operationResponse.ResourceBody, &itemResponseBody)
assert.Equal(t, newItem["id"], itemResponseBody["id"])
assert.Equal(t, newItem["pk"], itemResponseBody["pk"])
updatedDoc, _ := ts.DataStore.GetDocument(testDatabaseName, testCollectionName, newItem["id"].(string))
assert.Equal(t, newItem["id"], updatedDoc["id"])
assert.Equal(t, newItem["pk"], updatedDoc["pk"])
})
t.Run("Should execute READ transactional batch", func(t *testing.T) {
context := context.TODO()
batch := collectionClient.NewTransactionalBatch(azcosmos.NewPartitionKeyString("pk"))
batch.ReadItem("67890", nil)
response, err := collectionClient.ExecuteTransactionalBatch(context, batch, &azcosmos.TransactionalBatchOptions{})
assert.Nil(t, err)
assert.True(t, response.Success)
assert.Equal(t, 1, len(response.OperationResults))
operationResponse := response.OperationResults[0]
assert.NotNil(t, operationResponse)
assert.NotNil(t, operationResponse.ResourceBody)
assert.Equal(t, int32(http.StatusOK), operationResponse.StatusCode)
var itemResponseBody map[string]interface{}
json.Unmarshal(operationResponse.ResourceBody, &itemResponseBody)
assert.Equal(t, "67890", itemResponseBody["id"])
})
})
runTestsWithPresets(t, "Test_Documents_With_Continuation_Token", presets, func(t *testing.T, ts *TestServer, client *azcosmos.Client) {
collectionClient := documents_InitializeDb(t, ts)
t.Run("Should query document with continuation token", func(t *testing.T) {
context := context.TODO()
pager := collectionClient.NewQueryItemsPager(
"SELECT c.id, c[\"pk\"] FROM c ORDER BY c.id",
azcosmos.PartitionKey{},
&azcosmos.QueryOptions{
PageSizeHint: 1,
})
assert.True(t, pager.More())
firstResponse, err := pager.NextPage(context)
assert.Nil(t, err)
assert.Equal(t, 1, len(firstResponse.Items))
var firstItem map[string]interface{}
err = json.Unmarshal(firstResponse.Items[0], &firstItem)
assert.Nil(t, err)
assert.Equal(t, "12345", firstItem["id"])
assert.Equal(t, "123", firstItem["pk"])
firstContinuationToken := continuationtoken.FromString(*firstResponse.ContinuationToken)
assert.Equal(t, 1, firstContinuationToken.Token.PageIndex)
assert.Equal(t, 1, firstContinuationToken.Token.TotalResults)
assert.True(t, pager.More())
secondResponse, err := pager.NextPage(context)
assert.Nil(t, err)
assert.Equal(t, 1, len(secondResponse.Items))
var secondItem map[string]interface{}
err = json.Unmarshal(secondResponse.Items[0], &secondItem)
assert.Nil(t, err)
assert.Equal(t, "67890", secondItem["id"])
assert.Equal(t, "456", secondItem["pk"])
assert.Nil(t, secondResponse.ContinuationToken)
assert.False(t, pager.More())
})
})
} }
+43
View File
@@ -0,0 +1,43 @@
package tests_test
import (
"fmt"
"net/http"
"net/url"
"testing"
"time"
"github.com/pikami/cosmium/api/config"
"github.com/pikami/cosmium/api/headers"
"github.com/pikami/cosmium/internal/authentication"
"github.com/stretchr/testify/assert"
)
// Request document with trailing slash like python cosmosdb client does.
func Test_Documents_Read_Trailing_Slash(t *testing.T) {
ts := runTestServer()
documents_InitializeDb(t, ts)
defer ts.Server.Close()
t.Run("Read doc with client that appends slash to path", func(t *testing.T) {
resourceIdTemplate := "dbs/%s/colls/%s/docs/%s"
path := fmt.Sprintf(resourceIdTemplate, testDatabaseName, testCollectionName, "12345")
testUrl := ts.URL + "/" + path + "/"
date := time.Now().Format(time.RFC1123)
signature := authentication.GenerateSignature("GET", "docs", path, date, config.DefaultAccountKey)
httpClient := &http.Client{}
req, _ := http.NewRequest("GET", testUrl, nil)
req.Header.Add(headers.XDate, date)
req.Header.Add(headers.Authorization, "sig="+url.QueryEscape(signature))
res, err := httpClient.Do(req)
assert.Nil(t, err)
if res != nil {
defer res.Body.Close()
assert.Equal(t, http.StatusOK, res.StatusCode, "Expected HTTP status 200 OK")
} else {
t.FailNow()
}
})
}
+63
View File
@@ -0,0 +1,63 @@
package main
import (
"encoding/hex"
"encoding/json"
"flag"
"fmt"
"github.com/pikami/cosmium/internal/rntbd"
)
func main() {
input := flag.String("input", "", "Input hex string")
isResponse := flag.Bool("response", false, "Is response")
flag.Parse()
data, err := hex.DecodeString(*input)
if err != nil {
fmt.Printf("Error decoding hex string: %v\n", err)
return
}
frame, err := rntbd.ParseFrame(data, *isResponse)
if err != nil {
fmt.Printf("Error parsing frame: %v\n", err)
return
}
fmt.Printf("Activity ID: %s\n", hex.EncodeToString(frame.ActivityId))
fmt.Printf("Resource Type: %s\n", frame.ResourceType.String())
fmt.Printf("Operation Type: %s\n", frame.OperationType.String())
if len(frame.RequestHeaders) > 0 {
fmt.Printf("=== Request Headers ===\n")
for header, value := range frame.RequestHeaders {
fmt.Printf("%s: %v\n", header.String(), value)
}
}
if len(frame.ResponseHeaders) > 0 {
fmt.Printf("=== Response Headers ===\n")
for header, value := range frame.ResponseHeaders {
fmt.Printf("%s: %v\n", header.String(), value)
}
}
if len(frame.ContextHeaders) > 0 {
fmt.Printf("=== Context Headers ===\n")
for header, value := range frame.ContextHeaders {
fmt.Printf("%s: %v\n", header.String(), value)
}
}
if len(frame.Payload) > 0 {
var jsonObj any
err := json.Unmarshal(frame.Payload, &jsonObj)
if err != nil {
fmt.Printf("Payload: %s\n", hex.EncodeToString(frame.Payload))
} else {
fmt.Printf("Payload: %+v\n", jsonObj)
}
}
}
+64
View File
@@ -0,0 +1,64 @@
package main
import (
"os"
"os/signal"
"syscall"
"github.com/pikami/cosmium/api"
"github.com/pikami/cosmium/api/config"
"github.com/pikami/cosmium/internal/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/rntbd"
)
func main() {
configuration := config.ParseFlags()
var dataStore datastore.DataStore
switch configuration.DataStore {
case config.DataStoreBadger:
dataStore = badgerdatastore.NewBadgerDataStore(badgerdatastore.BadgerDataStoreOptions{
InitialDataFilePath: configuration.InitialDataFilePath,
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)
err := server.Start()
if err != nil {
panic(err)
}
if configuration.EnableRntbd {
rntbdServer := rntbd.NewRntbdServer(configuration.RntbdPort, server)
err = rntbdServer.Start()
if err != nil {
panic(err)
}
defer rntbdServer.Stop()
}
waitForExit(server, dataStore)
}
func waitForExit(server *api.ApiServer, dataStore datastore.DataStore) {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
// Block until a exit signal is received
<-sigs
// Stop the server
server.Stop()
dataStore.Close()
}
+125
View File
@@ -0,0 +1,125 @@
# Contributor Covenant Code of Conduct
## Our Pledge
We as members, contributors, and leaders pledge to make participation in our
community a harassment-free experience for everyone, regardless of age, body
size, visible or invisible disability, ethnicity, sex characteristics, gender
identity and expression, level of experience, education, socio-economic status,
nationality, personal appearance, race, caste, color, religion, or sexual
identity and orientation.
We pledge to act and interact in ways that contribute to an open, welcoming,
diverse, inclusive, and healthy community.
## Our Standards
Examples of behavior that contributes to a positive environment for our
community include:
* Demonstrating empathy and kindness toward other people
* Being respectful of differing opinions, viewpoints, and experiences
* Giving and gracefully accepting constructive feedback
* Accepting responsibility and apologizing to those affected by our mistakes,
and learning from the experience
* Focusing on what is best not just for us as individuals, but for the overall
community
Examples of unacceptable behavior include:
* The use of sexualized language or imagery, and sexual attention or advances of
any kind
* Trolling, insulting or derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or email address,
without their explicit permission
* Other conduct which could reasonably be considered inappropriate in a
professional setting
## Enforcement Responsibilities
Community leaders are responsible for clarifying and enforcing our standards of
acceptable behavior and will take appropriate and fair corrective action in
response to any behavior that they deem inappropriate, threatening, offensive,
or harmful.
Community leaders have the right and responsibility to remove, edit, or reject
comments, commits, code, wiki edits, issues, and other contributions that are
not aligned to this Code of Conduct, and will communicate reasons for moderation
decisions when appropriate.
## Scope
This Code of Conduct applies within all community spaces, and also applies when
an individual is officially representing the community in public spaces.
Examples of representing our community include using an official email address,
posting via an official social media account, or acting as an appointed
representative at an online or offline event.
## Enforcement
Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported to the community leaders responsible for enforcement at
cosmium@pikami.org.
All complaints will be reviewed and investigated promptly and fairly.
All community leaders are obligated to respect the privacy and security of the
reporter of any incident.
## Enforcement Guidelines
Community leaders will follow these Community Impact Guidelines in determining
the consequences for any action they deem in violation of this Code of Conduct:
### 1. Correction
**Community Impact**: Use of inappropriate language or other behavior deemed
unprofessional or unwelcome in the community.
**Consequence**: A private, written warning from community leaders, providing
clarity around the nature of the violation and an explanation of why the
behavior was inappropriate. A public apology may be requested.
### 2. Warning
**Community Impact**: A violation through a single incident or series of
actions.
**Consequence**: A warning with consequences for continued behavior. No
interaction with the people involved, including unsolicited interaction with
those enforcing the Code of Conduct, for a specified period of time. This
includes avoiding interactions in community spaces as well as external channels
like social media. Violating these terms may lead to a temporary or permanent
ban.
### 3. Temporary Ban
**Community Impact**: A serious violation of community standards, including
sustained inappropriate behavior.
**Consequence**: A temporary ban from any sort of interaction or public
communication with the community for a specified period of time. No public or
private interaction with the people involved, including unsolicited interaction
with those enforcing the Code of Conduct, is allowed during this period.
Violating these terms may lead to a permanent ban.
### 4. Permanent Ban
**Community Impact**: Demonstrating a pattern of violation of community
standards, including sustained inappropriate behavior, harassment of an
individual, or aggression toward or disparagement of classes of individuals.
**Consequence**: A permanent ban from any sort of public interaction within the
community.
## Attribution
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
version 2.1, available at
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
Community Impact Guidelines were inspired by
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
For answers to common questions about this code of conduct, see the FAQ at
[https://www.contributor-covenant.org/faq][FAQ].
+230
View File
@@ -0,0 +1,230 @@
# Cosmium Compatibility with Cosmos DB
## Introduction
Cosmium is designed to emulate the functionality of Cosmos DB, providing developers with a local development environment that closely mimics the behavior of Cosmos DB. While Cosmium aims to be compatible with Cosmos DB, there are certain differences and limitations to be aware of. This document provides an overview of Cosmium's compatibility with Cosmos DB and highlights areas where deviations may occur.
## Supported Features
Cosmium strives to support the core features of Cosmos DB, including:
- REST API
- SQL-like query language
- Document-based data model
## Compatibility Matrix
### Features
| Feature | Implemented |
| ----------------------------- | ----------- |
| Subqueries | Yes |
| Joins | Yes |
| Computed properties | No |
| Coalesce operators | No |
| Bitwise operators | No |
| GeoJSON location data | No |
| Parameterized queries | Yes |
| Stored procedures | No |
| Triggers | No |
| User-defined functions (UDFs) | No |
### Clauses
| Clause | Implemented |
| ------------ | ----------- |
| SELECT | Yes |
| FROM | Yes |
| WHERE | Yes |
| ORDER BY | Yes |
| GROUP BY | Yes |
| OFFSET LIMIT | Yes |
### Keywords
| Keyword | Implemented |
| -------- | ----------- |
| BETWEEN | No |
| DISTINCT | Yes |
| LIKE | No |
| IN | Yes |
| TOP | Yes |
### Aggregate Functions
| Function | Implemented |
| -------- | ----------- |
| AVG | Yes |
| COUNT | Yes |
| MAX | Yes |
| MIN | Yes |
| SUM | Yes |
### Array Functions
| Function | Implemented |
| ------------------ | ----------- |
| ARRAY_CONCAT | Yes |
| ARRAY_CONTAINS | Yes |
| ARRAY_CONTAINS_ANY | Yes |
| ARRAY_CONTAINS_ALL | Yes |
| ARRAY_LENGTH | Yes |
| ARRAY_SLICE | Yes |
| CHOOSE | No |
| ObjectToArray | No |
| SetIntersect | Yes |
| SetUnion | Yes |
### Conditional Functions
| Function | Implemented |
| -------- | ----------- |
| IIF | Yes |
### Date and time Functions
| Function | Implemented |
| ------------------------- | ----------- |
| DateTimeAdd | No |
| DateTimeBin | No |
| DateTimeDiff | No |
| DateTimeFromParts | No |
| DateTimePart | No |
| DateTimeToTicks | No |
| DateTimeToTimestamp | No |
| GetCurrentDateTime | No |
| GetCurrentDateTimeStatic | No |
| GetCurrentTicks | No |
| GetCurrentTicksStatic | No |
| GetCurrentTimestamp | No |
| GetCurrentTimestampStatic | No |
| TicksToDateTime | No |
| TimestampToDateTime | No |
### Item Functions
| Function | Implemented |
| ---------- | ----------- |
| DocumentId | No |
### Mathematical Functions
| Function | Implemented |
| ---------------- | ----------- |
| ABS | Yes |
| ACOS | Yes |
| ASIN | Yes |
| ATAN | Yes |
| ATN2 | Yes |
| CEILING | Yes |
| COS | Yes |
| COT | Yes |
| DEGREES | Yes |
| EXP | Yes |
| FLOOR | Yes |
| IntAdd | Yes |
| IntBitAnd | Yes |
| IntBitLeftShift | Yes |
| IntBitNot | Yes |
| IntBitOr | Yes |
| IntBitRightShift | Yes |
| IntBitXor | Yes |
| IntDiv | Yes |
| IntMod | Yes |
| IntMul | Yes |
| IntSub | Yes |
| LOG | Yes |
| LOG10 | Yes |
| NumberBin | Yes |
| PI | Yes |
| POWER | Yes |
| RADIANS | Yes |
| RAND | Yes |
| ROUND | Yes |
| SIGN | Yes |
| SIN | Yes |
| SQRT | Yes |
| SQUARE | Yes |
| TAN | Yes |
| TRUNC | Yes |
### Spatial Functions
| Function | Implemented |
| ------------------ | ----------- |
| ST_AREA | No |
| ST_DISTANCE | No |
| ST_WITHIN | No |
| ST_INTERSECTS | No |
| ST_ISVALID | No |
| ST_ISVALIDDETAILED | No |
### String Functions
| Function | Implemented |
| --------------- | ----------- |
| CONCAT | Yes |
| CONTAINS | Yes |
| ENDSWITH | Yes |
| INDEX_OF | Yes |
| LEFT | Yes |
| LENGTH | Yes |
| LOWER | Yes |
| LTRIM | Yes |
| REGEXMATCH | No |
| REPLACE | Yes |
| REPLICATE | Yes |
| REVERSE | Yes |
| RIGHT | Yes |
| RTRIM | Yes |
| STARTSWITH | Yes |
| STRINGEQUALS | Yes |
| StringToArray | No |
| StringToBoolean | No |
| StringToNull | No |
| StringToNumber | No |
| StringToObject | No |
| SUBSTRING | Yes |
| ToString | Yes |
| TRIM | Yes |
| UPPER | Yes |
### Type checking Functions
| Function | Implemented |
| ---------------- | ----------- |
| IS_ARRAY | Yes |
| IS_BOOL | Yes |
| IS_DEFINED | Yes |
| IS_FINITE_NUMBER | Yes |
| IS_INTEGER | Yes |
| IS_NULL | Yes |
| IS_NUMBER | Yes |
| IS_OBJECT | Yes |
| IS_PRIMITIVE | Yes |
| IS_STRING | Yes |
### Transactional batch operations
Note: There's actually no transaction here. Think of this as a 'bulk operation' that can partially succeed.
| Operation | Implemented |
| --------- | ----------- |
| Create | Yes |
| Delete | Yes |
| Replace | Yes |
| Upsert | Yes |
| Read | Yes |
| Patch | No |
## Known Differences
While Cosmium aims to replicate the behavior of Cosmos DB as closely as possible, there are certain differences and limitations to be aware of:
1. **Performance**: Cosmium may exhibit different performance characteristics compared to Cosmos DB, especially under heavy load or large datasets.
2. **Consistency Levels**: The consistency model in Cosmium may differ slightly from Cosmos DB.
3. **Features**: Some advanced features or functionalities of Cosmos DB may not be fully supported or available in Cosmium.
## Future Development
Cosmium is actively developed and maintained, with ongoing efforts to improve compatibility with Cosmos DB and enhance its features and capabilities. Future updates may address known differences and limitations, as well as introduce new functionality to bring Cosmium closer to feature parity with Cosmos DB.
+37
View File
@@ -0,0 +1,37 @@
# Contributing to Cosmium
Thank you for considering contributing to Cosmium! We appreciate your interest in helping to improve our project.
Please note that by participating in this project, you agree to abide by our [Code of Conduct](/docs/CODE_OF_CONDUCT.md). We expect all contributors to uphold the principles of respect, inclusivity, and professionalism.
If you have any questions or need assistance with the contribution process, feel free to reach out to us by opening an issue or contacting the maintainers directly.
We look forward to your contributions! 🚀
## Finding ways to contribute
A great way to contribute is to scan the [Compatibility Matrix](/docs/compatibility.md) for unsupported features and improving compatibility with CosmosDB.
A part from that, the [Issues page](https://github.com/pikami/cosmium/issues) might contain issues registered by other users. Fixing reported issues is a great way to contribute.
## How to Contribute
1. **Create an Issue**: Before starting work on a new feature or bug fix, please create an issue or look for existing ones on the [Issues page](https://github.com/pikami/cosmium/issues) to discuss your proposed changes. This allows us to provide feedback and ensure that your contribution aligns with the project goals.
2. **Fork the Repository**: Once you have identified an issue to work on, fork the repository to your own GitHub account.
3. **Create a Branch**: Create a new branch for your changes using a descriptive name that reflects the issue you are addressing.
4. **Commit Changes**: Commit your changes with clear and descriptive commit messages. Reference the issue number in the commit message. **Please write unit tests for your implemented feature!**
5. **Create a Pull Request**: Once your changes are ready, create a pull request from your forked repository to the main repository. Be sure to include a detailed description of your changes and reference the relevant issue.
6. **Review and Collaborate**: Participate in the code review process by addressing any feedback or comments from maintainers. Collaboration and constructive feedback help ensure the quality of contributions.
## Example Commits
To get an idea of how to implement new query functions, you can review the following example commits:
* [Implement IN function](https://github.com/pikami/cosmium/commit/f37c664c1aef39ee820106eaec1a3708ee7a93c8)
* [Implement ToString function](https://github.com/pikami/cosmium/commit/16f41a547956f54481605f0ce035eee978a5e74b)
* [Implement ARRAY_CONCAT, ARRAY_LENGTH, ARRAY_SLICE, SetIntersect, SetUnion functions](https://github.com/pikami/cosmium/commit/1c5e5ce85d70ed91e4b9be9e8f76d59e6eafc1b5)
+46 -25
View File
@@ -1,43 +1,64 @@
module github.com/pikami/cosmium module github.com/pikami/cosmium
go 1.21.6 go 1.26.3
require ( require (
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.9.2 github.com/Azure/azure-sdk-for-go/sdk/azcore v1.22.0
github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos v0.3.6 github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos v1.4.2
github.com/gin-gonic/gin v1.9.1 github.com/cosmiumdev/json-patch/v5 v5.9.11
github.com/google/uuid v1.1.1 github.com/dgraph-io/badger/v4 v4.9.2
github.com/stretchr/testify v1.8.4 github.com/gin-gonic/gin v1.12.0
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 github.com/google/uuid v1.6.0
github.com/stretchr/testify v1.11.1
github.com/vmihailenco/msgpack/v5 v5.4.1
golang.org/x/exp v0.0.0-20260603202125-055de637280b
) )
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.5.2 // indirect github.com/Azure/azure-sdk-for-go/sdk/internal v1.12.0 // indirect
github.com/bytedance/sonic v1.9.1 // indirect github.com/bytedance/gopkg v0.1.4 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect github.com/bytedance/sonic v1.15.2 // indirect
github.com/bytedance/sonic/loader v0.5.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/cloudwego/base64x v0.1.7 // 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.2 // indirect github.com/dgraph-io/ristretto/v2 v2.4.0 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect github.com/dustin/go-humanize v1.0.1 // indirect
github.com/gabriel-vasile/mimetype v1.4.13 // indirect
github.com/gin-contrib/sse v1.1.1 // 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.14.0 // indirect github.com/go-playground/validator/v10 v10.30.3 // indirect
github.com/goccy/go-json v0.10.2 // indirect github.com/goccy/go-json v0.10.6 // indirect
github.com/goccy/go-yaml v1.19.2 // indirect
github.com/google/flatbuffers v25.12.19+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.4 // indirect github.com/klauspost/compress v1.18.6 // indirect
github.com/leodido/go-urn v1.2.4 // indirect github.com/klauspost/cpuid/v2 v2.3.0 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-isatty v0.0.22 // 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.0.8 // indirect github.com/pelletier/go-toml/v2 v2.3.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.6.0 // indirect
github.com/quic-go/quic-go v0.60.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.11 // indirect github.com/ugorji/go/codec v1.3.1 // indirect
golang.org/x/arch v0.3.0 // indirect github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
golang.org/x/crypto v0.18.0 // indirect go.mongodb.org/mongo-driver/v2 v2.6.0 // indirect
golang.org/x/net v0.20.0 // indirect go.opentelemetry.io/auto/sdk v1.2.1 // indirect
golang.org/x/sys v0.16.0 // indirect go.opentelemetry.io/otel v1.44.0 // indirect
golang.org/x/text v0.14.0 // indirect go.opentelemetry.io/otel/metric v1.44.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect go.opentelemetry.io/otel/trace v1.44.0 // indirect
golang.org/x/arch v0.28.0 // indirect
golang.org/x/crypto v0.53.0 // indirect
golang.org/x/net v0.56.0 // indirect
golang.org/x/sys v0.46.0 // indirect
golang.org/x/text v0.38.0 // indirect
google.golang.org/protobuf v1.36.11 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect
) )
+117 -73
View File
@@ -1,109 +1,153 @@
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.9.2 h1:c4k2FIYIh4xtwqrQwV0Ct1v5+ehlNXj5NI/MWVsiTkQ= github.com/Azure/azure-sdk-for-go/sdk/azcore v1.22.0 h1:aokoqcHvaGjiM3VpjKDfMMnF/8epJ+Q1HLJ7CudztqE=
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.9.2/go.mod h1:5FDJtLEO/GxwNgUxbwrY3LP0pEoThTQJtk2oysdXHxM= github.com/Azure/azure-sdk-for-go/sdk/azcore v1.22.0/go.mod h1:/WYEx9pcM9Y+Dd/APJaNlSvVSvzl54rrMdZT5+Oi2LM=
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.0.0 h1:Yoicul8bnVdQrhDMTHxdEckRGX01XvwXDHUT9zYZ3k0= github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.13.1 h1:Hk5QBxZQC1jb2Fwj6mpzme37xbCDdNTxU7O9eb5+LB4=
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.0.0/go.mod h1:+6sju8gk8FRmSajX3Oz4G5Gm7P+mbqE9FVaXXFYTkCM= github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.13.1/go.mod h1:IYus9qsFobWIc2YVwe/WPjcnyCkPKtnHAqUYeebc8z0=
github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos v0.3.6 h1:oBqQLSI1pZwGOdXJAoJJSzmff9tlfD4KroVfjQQmd0g= github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos v1.4.2 h1:zqxnp53f5Jn5PFU5Av4mvyWEbZ7whg72AoOCEzlXFKc=
github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos v0.3.6/go.mod h1:Beh5cHIXJ0oWEDWk9lNFtuklCojLLQ5hl+LqSNTTs0I= github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos v1.4.2/go.mod h1:Krtog/7tz27z75TwM5cIS8bxEH4dcBUezcq+kGVeZEo=
github.com/Azure/azure-sdk-for-go/sdk/internal v1.5.2 h1:LqbJ/WzJUwBf8UiaSzgX7aMclParm9/5Vgp+TY51uBQ= github.com/Azure/azure-sdk-for-go/sdk/internal v1.12.0 h1:fhqpLE3UEXi9lPaBRpQ6XuRW0nU7hgg4zlmZZa+a9q4=
github.com/Azure/azure-sdk-for-go/sdk/internal v1.5.2/go.mod h1:yInRyqWXAuaPrgI7p70+lDDgh3mlBohis29jGMISnmc= github.com/Azure/azure-sdk-for-go/sdk/internal v1.12.0/go.mod h1:7dCRMLwisfRH3dBupKeNCioWYUZ4SS09Z14H+7i8ZoY=
github.com/AzureAD/microsoft-authentication-library-for-go v0.4.0 h1:WVsrXCnHlDDX8ls+tootqRE87/hL9S/g4ewig9RsD/c= github.com/AzureAD/microsoft-authentication-library-for-go v1.6.0 h1:XRzhVemXdgvJqCH0sFfrBUTnUJSBrBf7++ypk+twtRs=
github.com/AzureAD/microsoft-authentication-library-for-go v0.4.0/go.mod h1:Vt9sXTKwMyGcOxSmLDMnGPgqsUg7m8pe215qMLrDXw4= github.com/AzureAD/microsoft-authentication-library-for-go v1.6.0/go.mod h1:HKpQxkWaGLJ+D/5H8QRpyQXA1eKjxkFlOMwck5+33Jk=
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= github.com/bytedance/gopkg v0.1.4 h1:oZnQwnX82KAIWb7033bEwtxvTqXcYMxDBaQxo5JJHWM=
github.com/bytedance/sonic v1.9.1 h1:6iJ6NqdoxCDr6mbY8h18oSO+cShGSMRGCEo7F2h0x8s= github.com/bytedance/gopkg v0.1.4/go.mod h1:v1zWfPm21Fb+OsyXN2VAHdL6TBb2L88anLQgdyje6R4=
github.com/bytedance/sonic v1.9.1/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U= github.com/bytedance/sonic v1.15.2 h1:90H+rcF/FwLXwfB1cudOLq/je83n683Utf4Cbp0xHCo=
github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY= github.com/bytedance/sonic v1.15.2/go.mod h1:mT2NbXunuaEbnZ+mRIX/vYqKISmgEuHFDI4UzmKx2SA=
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 h1:qSGYFH7+jGhDF8vLC+iwCD4WpbV1EBDSzWkJODFLams= github.com/bytedance/sonic/loader v0.5.1 h1:Ygpfa9zwRCCKSlrp5bBP/b/Xzc3VxsAW+5NIYXrOOpI=
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk= github.com/bytedance/sonic/loader v0.5.1/go.mod h1:AR4NYCk5DdzZizZ5djGqQ92eEhCCcdf5x77udYiSJRo=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cloudwego/base64x v0.1.7 h1:NppS+Fgzg5ovhn4NkUXaDT3x9jldgH5ToMCqzBSi2zI=
github.com/cloudwego/base64x v0.1.7/go.mod h1:Cu1PV9zfrSf7ET2tIbWbbEy7jO7HHJ13q4X2SQ8aWYg=
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.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU= github.com/dgraph-io/badger/v4 v4.9.2 h1:Wb5qw8gElqwV1a8msHTeQKova9b1V10heFKMIiPd80E=
github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA= github.com/dgraph-io/badger/v4 v4.9.2/go.mod h1:nJjaJTUOSsQEBhsq209FmwCvMJzEA3e74RjZw6V2pQI=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= github.com/dgraph-io/ristretto/v2 v2.4.0 h1:I/w09yLjhdcVD2QV192UJcq8dPBaAJb9pOuMyNy0XlU=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/dgraph-io/ristretto/v2 v2.4.0/go.mod h1:0KsrXtXvnv0EqnzyowllbVJB8yBonswa2lTCK2gGo9E=
github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg= github.com/dgryski/go-farm v0.0.0-20240924180020-3414d57e47da h1:aIftn67I1fkbMa512G+w+Pxci9hJPB8oMnkcP3iZF38=
github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU= 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.13 h1:46nXokslUBsAJE/wMsp5gtO500a4F3Nkz9Ufpk2AcUM=
github.com/gabriel-vasile/mimetype v1.4.13/go.mod h1:d+9Oxyo1wTzWdyVUPMmXFvp4F9tea18J8ufA774AB3s=
github.com/gin-contrib/sse v1.1.1 h1:uGYpNwTacv5R68bSGMapo62iLTRa9l5zxGCps4hK6ko=
github.com/gin-contrib/sse v1.1.1/go.mod h1:QXzuVkA0YO7o/gun03UI1Q+FTI8ZV/n5t03kIQAI89s=
github.com/gin-gonic/gin v1.12.0 h1:b3YAbrZtnf8N//yjKeU2+MQsh2mY5htkZidOM7O0wG8=
github.com/gin-gonic/gin v1.12.0/go.mod h1:VxccKfsSllpKshkBWgVgRniFFAzFb9csfngsqANjnLc=
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.14.0 h1:vgvQWe3XCz3gIeFDm/HnTIbj6UGmg/+t63MyGU2n5js= github.com/go-playground/validator/v10 v10.30.3 h1:4MU6YkEwx7GbcPJOZxrtbu+QfF3pJLJuaYTeAH0DYy8=
github.com/go-playground/validator/v10 v10.14.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= github.com/go-playground/validator/v10 v10.30.3/go.mod h1:4Axh7oCNGcoGkqLoE4YWt6n20mcEIsPRlB7vPk3lpyc=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= github.com/goccy/go-json v0.10.6 h1:p8HrPJzOakx/mn/bQtjgNjdTcN+/S6FcG2CTtQOrHVU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/goccy/go-json v0.10.6/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
github.com/golang-jwt/jwt v3.2.1+incompatible h1:73Z+4BJcrTC+KczS6WvTPvRGOp1WmfEP4Q1lOd9Z/+c= github.com/goccy/go-yaml v1.19.2 h1:PmFC1S6h8ljIz6gMRBopkjP1TVT7xuwrButHID66PoM=
github.com/golang-jwt/jwt v3.2.1+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I= github.com/goccy/go-yaml v1.19.2/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/golang-jwt/jwt/v5 v5.3.0 h1:pv4AsKCKKZuqlgs5sUmn4x8UlGa0kEVt/puTpKx9vvo=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/golang-jwt/jwt/v5 v5.3.0/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= github.com/google/flatbuffers v25.12.19+incompatible h1:haMV2JRRJCe1998HeW/p0X9UaMTK6SDo0ffLn2+DbLs=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/flatbuffers v25.12.19+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.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.1.1/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.6 h1:2jupLlAwFm95+YDR+NwD2MEfFO9d4z4Prjl1XXDjuao=
github.com/klauspost/cpuid/v2 v2.2.4 h1:acbojRNwl3o09bUq+yDCtZFc1aiwaAAxtcn8YkZXnvk= github.com/klauspost/compress v1.18.6/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ=
github.com/klauspost/cpuid/v2 v2.2.4/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY= github.com/klauspost/cpuid/v2 v2.3.0 h1:S4CRMLnYUhGeDFDqkGriYKdfoFlDnMtqTiI/sFzhA9Y=
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/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q= github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4= github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= github.com/mattn/go-isatty v0.0.22 h1:j8l17JJ9i6VGPUFUYoTUKPSgKe/83EYU2zBC7YNKMw4=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-isatty v0.0.22/go.mod h1:ZXfXG4SQHsB/w3ZeOYbR0PrPwLy+n6xiMrJlRFqopa4=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
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.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZR9tGQ= github.com/pelletier/go-toml/v2 v2.3.1 h1:MYEvvGnQjeNkRF1qUuGolNtNExTDwct51yp7olPtrEc=
github.com/pelletier/go-toml/v2 v2.0.8/go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNcZljzZR9VXg+4= github.com/pelletier/go-toml/v2 v2.3.1/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
github.com/pkg/browser v0.0.0-20210115035449-ce105d075bb4 h1:Qj1ukM4GlMWXNdMBuXcXfz/Kw9s1qm0CLY32QxuSImI= github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c h1:+mdjkGKdHQG3305AYmdv1U2eRNDiU2ErMBj1gwrq8eQ=
github.com/pkg/browser v0.0.0-20210115035449-ce105d075bb4/go.mod h1:N6UoU20jOqggOuDwUaBQpluzLNDqif3kq9z2wpdYEfQ= github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjLxUqIJNnCWiEdr3bn6IUYi15bNlnbCCU=
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/quic-go/go-ossfuzz-seeds v0.1.0 h1:APacT+iIaNF6fd8AGEiN3bT/Jtkd2jz4v4TzM7MFjy0=
github.com/quic-go/go-ossfuzz-seeds v0.1.0/go.mod h1:3IOHRbJIc+L6YKMwfDtJAM9Vj9k0YY4muhuyUYk5tbk=
github.com/quic-go/qpack v0.6.0 h1:g7W+BMYynC1LbYLSqRt8PBg5Tgwxn214ZZR34VIOjz8=
github.com/quic-go/qpack v0.6.0/go.mod h1:lUpLKChi8njB4ty2bFLX2x4gzDqXwUpaO1DP9qMDZII=
github.com/quic-go/quic-go v0.60.0 h1:xcQioE8OM66UQLeUMHltK1CCcOu3JbVB4JAQdDQSB+0=
github.com/quic-go/quic-go v0.60.0/go.mod h1:wpKpjmPpftl30sL6pFh7REVpjbcCVy4zt2vDyK1TuJk=
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.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
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.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU= github.com/ugorji/go/codec v1.3.1 h1:waO7eEiFDwidsBN6agj1vJQ4AG7lh2yqXyOXqhgQuyY=
github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= github.com/ugorji/go/codec v1.3.1/go.mod h1:pRBVtBSKl77K30Bv8R2P+cLSGaTtex6fsA2Wjqmfxj4=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= github.com/vmihailenco/msgpack/v5 v5.4.1 h1:cQriyiUvjTwOHg8QZaPihLWeRAAVoCpE00IUPn0Bjt8=
golang.org/x/arch v0.3.0 h1:02VY4/ZcO/gBOH6PUaoiptASxtXU10jazRCP865E97k= github.com/vmihailenco/msgpack/v5 v5.4.1/go.mod h1:GaZTsDaehaPpQVyxrf5mtQlH+pc21PIudVV/E3rRQok=
golang.org/x/arch v0.3.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc= github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg= go.mongodb.org/mongo-driver/v2 v2.6.0 h1:b9sJOYrkmt4l8bY43ZenFBcPlhYIjaOfYHLtbB/5qi8=
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 h1:LfspQV/FYTatPTr/3HzIcmiUFH7PGP+OQ6mgDYo3yuQ= go.mongodb.org/mongo-driver/v2 v2.6.0/go.mod h1:yOI9kBsufol30iFsl1slpdq1I0eHPzybRWdyYUs8K/0=
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225/go.mod h1:CxmFvTBINI24O/j8iY7H1xHzx2i4OsyguNBmN/uPtqc= go.opentelemetry.io/auto/sdk v1.2.1 h1:jXsnJ4Lmnqd11kwkBV2LgLoFMZKizbCi5fNZ/ipaZ64=
golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo= go.opentelemetry.io/auto/sdk v1.2.1/go.mod h1:KRTj+aOaElaLi+wW1kO/DZRXwkF4C5xPbEe3ZiIhN7Y=
golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY= go.opentelemetry.io/otel v1.44.0 h1:JjwHmHpA4iZ3wBxluu2fbbE7j4kqlE8jXyAyPXH7HqU=
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= go.opentelemetry.io/otel v1.44.0/go.mod h1:BMgjTHL9WPRlRjL2oZCBTL4whCGtXch2H4BhOPIAyYc=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= go.opentelemetry.io/otel/metric v1.44.0 h1:1w0gILTcHdr3YI+ixLyjemwrVnsMURbTZFrSYCdDdmc=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU= go.opentelemetry.io/otel/metric v1.44.0/go.mod h1:8O7hanEPBNgEMmybD3s2VBKcgWOCsA6tzHBPODAiquo=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= go.opentelemetry.io/otel/trace v1.44.0 h1:jxF5CsGYCe74MCRx2X4g7WsY/VBKRqqpNvXlX/6gtIk=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= go.opentelemetry.io/otel/trace v1.44.0/go.mod h1:oLl1jrMQAVo6v3GAggN+1VH9VIz9iUSvW53sW1Q8PIE=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= go.uber.org/mock v0.6.0 h1:hyF9dfmbgIX5EfOdasqLsWD6xqpNZlXblLB/Dbnwv3Y=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= go.uber.org/mock v0.6.0/go.mod h1:KiVJ4BqZJaMj4svdfmHM0AUx4NJYO8ZNpPnZn1Z+BBU=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= golang.org/x/arch v0.28.0 h1:wVwVdqsTuUbJvhYVCspQYwZXHNYeLSoZnmHD+ggddpQ=
google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng= golang.org/x/arch v0.28.0/go.mod h1:0X+GdSIP+kL5wPmpK7sdkEVTt2XoYP0cSjQSbZBwOi8=
google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= golang.org/x/crypto v0.53.0 h1:QZ4Muo8THX6CizN2vPPd5fBGHyogrdK9fG4wLPFUsto=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= golang.org/x/crypto v0.53.0/go.mod h1:DNLU434OwVakk9PzuwV8w62mAJpRJL3vsgcfp4Qnsio=
golang.org/x/exp v0.0.0-20260603202125-055de637280b h1:v1uXiEBHo8QA0LiGCo7UgHMzHT4Kdfpl2zmtH5vaP1Q=
golang.org/x/exp v0.0.0-20260603202125-055de637280b/go.mod h1:d2fgXJLVs4dYDHUk5lwMIfzRzSrWCfGZb0ZqeLa/Vcw=
golang.org/x/net v0.56.0 h1:Rw8j/hFzGvJUZwNBXnAtf5sVDVt+65SK2C7IxCxZt5o=
golang.org/x/net v0.56.0/go.mod h1:D3Ku6r+V6JROoZK144D2XfMHFcMq/0zSfLelVTCFKec=
golang.org/x/sys v0.46.0 h1:noSf2Fq6F8DBgS+LysIkx7rIExoNHJsxOAtPp4rthXw=
golang.org/x/sys v0.46.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
golang.org/x/text v0.38.0 h1:sXmwo9DwP3OK9EZ7PqAdaooSGozfl/3a6/xJcbzPRhE=
golang.org/x/text v0.38.0/go.mod h1:YXZt3QhHUKYT53r2lLKFIVi6Ao1jdzrTR/KQ09qyxF4=
google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE=
google.golang.org/protobuf v1.36.11/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/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=
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
@@ -10,6 +10,11 @@ import (
// https://learn.microsoft.com/en-us/rest/api/cosmos-db/access-control-on-cosmosdb-resources // https://learn.microsoft.com/en-us/rest/api/cosmos-db/access-control-on-cosmosdb-resources
func GenerateSignature(verb string, resourceType string, resourceId string, date string, masterKey string) string { func GenerateSignature(verb string, resourceType string, resourceId string, date string, masterKey string) string {
isNameBased := resourceId != "" && ((len(resourceId) > 4 && resourceId[3] == '/') || strings.HasPrefix(strings.ToLower(resourceId), "interopusers"))
if !isNameBased {
resourceId = strings.ToLower(resourceId)
}
payload := fmt.Sprintf( payload := fmt.Sprintf(
"%s\n%s\n%s\n%s\n%s\n", "%s\n%s\n%s\n%s\n%s\n",
strings.ToLower(verb), strings.ToLower(verb),
@@ -27,4 +27,14 @@ func Test_GenerateSignature(t *testing.T) {
signature := authentication.GenerateSignature("DELETE", "dbs", "dbs/Test Database", testDate, config.DefaultAccountKey) signature := authentication.GenerateSignature("DELETE", "dbs", "dbs/Test Database", testDate, config.DefaultAccountKey)
assert.Equal(t, "LcuXXg0TcXxZG0kUCj9tZIWRy2yCzim3oiqGiHpRqGs=", signature) assert.Equal(t, "LcuXXg0TcXxZG0kUCj9tZIWRy2yCzim3oiqGiHpRqGs=", signature)
}) })
t.Run("Should generate PKRANGES signature", func(t *testing.T) {
signature := authentication.GenerateSignature("GET", "pkranges", "m4d+xG08uVM=", testDate, config.DefaultAccountKey)
assert.Equal(t, "6S5ceZsl2EXWB3Jo5bJcK7zv8NxXnsxWPWD9TH3nNMo=", signature)
})
t.Run("Should generate PATCH signature", func(t *testing.T) {
signature := authentication.GenerateSignature("PATCH", "docs", "dbs/test-db/colls/test-coll/docs/67890", testDate, config.DefaultAccountKey)
assert.Equal(t, "VR1ddfxKBXnoaT+b3WkhyYVc9JmGNpTnaRmyDM44398=", signature)
})
} }
+9 -34
View File
@@ -1,43 +1,9 @@
package constants package constants
import ( import (
"fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/pikami/cosmium/api/config"
) )
var ServerInfoResponse = gin.H{
"_self": "",
"id": config.Config.DatabaseAccount,
"_rid": fmt.Sprintf("%s.%s", config.Config.DatabaseAccount, config.Config.DatabaseDomain),
"media": "//media/",
"addresses": "//addresses/",
"_dbs": "//dbs/",
"writableLocations": []map[string]interface{}{
{
"name": "South Central US",
"databaseAccountEndpoint": config.Config.DatabaseEndpoint,
},
},
"readableLocations": []map[string]interface{}{
{
"name": "South Central US",
"databaseAccountEndpoint": config.Config.DatabaseEndpoint,
},
},
"enableMultipleWriteLocations": false,
"userReplicationPolicy": map[string]interface{}{
"asyncReplication": false,
"minReplicaSetSize": 1,
"maxReplicasetSize": 4,
},
"userConsistencyPolicy": map[string]interface{}{"defaultConsistencyLevel": "Session"},
"systemReplicationPolicy": map[string]interface{}{"minReplicaSetSize": 1, "maxReplicasetSize": 4},
"readPolicy": map[string]interface{}{"primaryReadCoefficient": 1, "secondaryReadCoefficient": 1},
"queryEngineConfiguration": "{\"allowNewKeywords\":true,\"maxJoinsPerSqlQuery\":10,\"maxQueryRequestTimeoutFraction\":0.9,\"maxSqlQueryInputLength\":524288,\"maxUdfRefPerSqlQuery\":10,\"queryMaxInMemorySortDocumentCount\":-1000,\"spatialMaxGeometryPointCount\":256,\"sqlAllowNonFiniteNumbers\":false,\"sqlDisableOptimizationFlags\":0,\"enableSpatialIndexing\":true,\"maxInExpressionItemsCount\":2147483647,\"maxLogicalAndPerSqlQuery\":2147483647,\"maxLogicalOrPerSqlQuery\":2147483647,\"maxSpatialQueryCells\":2147483647,\"sqlAllowAggregateFunctions\":true,\"sqlAllowGroupByClause\":true,\"sqlAllowLike\":true,\"sqlAllowSubQuery\":true,\"sqlAllowScalarSubQuery\":true,\"sqlAllowTop\":true}",
}
var QueryPlanResponse = gin.H{ var QueryPlanResponse = gin.H{
"partitionedQueryExecutionInfoVersion": 2, "partitionedQueryExecutionInfoVersion": 2,
"queryInfo": map[string]interface{}{ "queryInfo": map[string]interface{}{
@@ -64,3 +30,12 @@ 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"}
var PreconditionFailedResponse = gin.H{
"code": "PreconditionFailed",
"message": "Operation cannot be performed because one of the specified precondition is not met.",
}
@@ -0,0 +1,145 @@
package continuationtoken
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"github.com/pikami/cosmium/internal/logger"
)
type ContinuationTokenExternal struct {
Token string `json:"token"`
Range struct {
Min string `json:"min"`
Max string `json:"max"`
} `json:"range"`
}
type ContinuationToken struct {
Token struct {
ResourceId string // RID
PageIndex int // RT
TotalResults int // TRC
ISV int // ISV
IEO int // IEO
QCF int // QCF
LR int // LR
}
Range struct {
Min string
Max string
}
}
func Generate(resourceid string, pageIndex int, totalResults int) ContinuationToken {
ct := ContinuationToken{}
ct.Token.ResourceId = resourceid
ct.Token.PageIndex = pageIndex
ct.Token.TotalResults = totalResults
ct.Token.ISV = 2
ct.Token.IEO = 65567
ct.Token.QCF = 8
ct.Token.LR = 1
ct.Range.Min = ""
ct.Range.Max = "FF"
return ct
}
func GenerateDefault(resourceid string) ContinuationToken {
return Generate(resourceid, 0, 0)
}
func (ct *ContinuationToken) ToString() string {
token := fmt.Sprintf(
"-RID:~%s#RT:%d#TRC:%d#ISV:%d#IEO:%d#QCF:%d#LR:%d",
ct.Token.ResourceId,
ct.Token.PageIndex,
ct.Token.TotalResults,
ct.Token.ISV,
ct.Token.IEO,
ct.Token.QCF,
ct.Token.LR,
)
ect := ContinuationTokenExternal{}
ect.Token = token
ect.Range.Min = ct.Range.Min
ect.Range.Max = ct.Range.Max
json, err := json.Marshal(ect)
if err != nil {
logger.Error(err, "failed to marshal continuation token")
return ""
}
return string(json)
}
func FromString(token string) ContinuationToken {
ect := ContinuationTokenExternal{}
err := json.Unmarshal([]byte(token), &ect)
if err != nil {
logger.Error(err, "failed to unmarshal continuation token")
return ContinuationToken{}
}
ct, err := parseContinuationToken(ect.Token, ect.Range.Min, ect.Range.Max)
if err != nil {
logger.Error(err, "failed to parse continuation token")
return ContinuationToken{}
}
return *ct
}
func parseContinuationToken(token string, minRange string, maxRange string) (*ContinuationToken, error) {
const prefix = "-RID:~"
if !strings.HasPrefix(token, prefix) {
return nil, fmt.Errorf("invalid token prefix")
}
parts := strings.Split(token[len(prefix):], "#")
if len(parts) != 7 {
return nil, fmt.Errorf("invalid token format: expected 7 fields, got %d", len(parts))
}
ct := &ContinuationToken{}
ct.Token.ResourceId = parts[0]
parseIntField := func(part, key string) (int, error) {
if !strings.HasPrefix(part, key+":") {
return 0, fmt.Errorf("expected %s field", key)
}
return strconv.Atoi(strings.TrimPrefix(part, key+":"))
}
var err error
if ct.Token.PageIndex, err = parseIntField(parts[1], "RT"); err != nil {
return nil, err
}
if ct.Token.TotalResults, err = parseIntField(parts[2], "TRC"); err != nil {
return nil, err
}
if ct.Token.ISV, err = parseIntField(parts[3], "ISV"); err != nil {
return nil, err
}
if ct.Token.IEO, err = parseIntField(parts[4], "IEO"); err != nil {
return nil, err
}
if ct.Token.QCF, err = parseIntField(parts[5], "QCF"); err != nil {
return nil, err
}
if ct.Token.LR, err = parseIntField(parts[6], "LR"); err != nil {
return nil, err
}
ct.Range.Min = minRange
ct.Range.Max = maxRange
return ct, nil
}
@@ -0,0 +1,35 @@
package continuationtoken
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Test_Generate(t *testing.T) {
token := Generate("test-resource-id", 1, 100)
assert.Equal(t, "test-resource-id", token.Token.ResourceId)
assert.Equal(t, 1, token.Token.PageIndex)
assert.Equal(t, 100, token.Token.TotalResults)
}
func Test_FromString(t *testing.T) {
token := FromString("{\"token\":\"-RID:~test-resource-id#RT:1#TRC:100#ISV:2#IEO:65567#QCF:8#LR:1\",\"range\":{\"min\":\"\",\"max\":\"FF\"}}")
assert.Equal(t, "test-resource-id", token.Token.ResourceId)
assert.Equal(t, 1, token.Token.PageIndex)
assert.Equal(t, 100, token.Token.TotalResults)
}
func Test_ToString(t *testing.T) {
token := Generate("test-resource-id", 1, 100)
assert.Equal(t, "{\"token\":\"-RID:~test-resource-id#RT:1#TRC:100#ISV:2#IEO:65567#QCF:8#LR:1\",\"range\":{\"min\":\"\",\"max\":\"FF\"}}", token.ToString())
}
func Test_GenerateDefault(t *testing.T) {
token := GenerateDefault("test-resource-id")
assert.Equal(t, "test-resource-id", token.Token.ResourceId)
assert.Equal(t, 0, token.Token.PageIndex)
assert.Equal(t, 0, token.Token.TotalResults)
}
@@ -0,0 +1,20 @@
package converters
import (
"github.com/pikami/cosmium/internal/datastore"
memoryexecutor "github.com/pikami/cosmium/query_executors/memory_executor"
)
type DocumentToRowTypeIterator struct {
documents datastore.DocumentIterator
}
func NewDocumentToRowTypeIterator(documents datastore.DocumentIterator) *DocumentToRowTypeIterator {
return &DocumentToRowTypeIterator{
documents: documents,
}
}
func (di *DocumentToRowTypeIterator) Next() (memoryexecutor.RowType, datastore.DataStoreStatus) {
return di.documents.Next()
}
@@ -0,0 +1,136 @@
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
gcDone chan struct{}
gcStopped chan struct{}
}
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,
gcDone: make(chan struct{}),
gcStopped: make(chan struct{}),
}
ds.initializeDataStore(options.InitialDataFilePath)
go ds.runGarbageCollector()
return ds
}
func (r *BadgerDataStore) Close() {
if r.gcTicker != nil {
r.gcTicker.Stop()
close(r.gcDone)
<-r.gcStopped
}
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() {
defer close(r.gcStopped)
for {
select {
case <-r.gcTicker.C:
for {
err := r.db.RunValueLogGC(0.7)
if err != nil {
break
}
}
case <-r.gcDone:
return
}
}
}
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
}
+44
View File
@@ -0,0 +1,44 @@
package datastore
type DataStore interface {
GetAllDatabases() ([]Database, DataStoreStatus)
GetDatabase(databaseId string) (Database, DataStoreStatus)
DeleteDatabase(databaseId string) DataStoreStatus
CreateDatabase(newDatabase Database) (Database, DataStoreStatus)
GetAllCollections(databaseId string) ([]Collection, DataStoreStatus)
GetCollection(databaseId string, collectionId string) (Collection, DataStoreStatus)
DeleteCollection(databaseId string, collectionId string) DataStoreStatus
CreateCollection(databaseId string, newCollection Collection) (Collection, DataStoreStatus)
GetAllDocuments(databaseId string, collectionId string) ([]Document, DataStoreStatus)
GetDocumentIterator(databaseId string, collectionId string) (DocumentIterator, DataStoreStatus)
GetDocument(databaseId string, collectionId string, documentId string) (Document, DataStoreStatus)
DeleteDocument(databaseId string, collectionId string, documentId string) DataStoreStatus
CreateDocument(databaseId string, collectionId string, document map[string]interface{}) (Document, DataStoreStatus)
GetAllTriggers(databaseId string, collectionId string) ([]Trigger, DataStoreStatus)
GetTrigger(databaseId string, collectionId string, triggerId string) (Trigger, DataStoreStatus)
DeleteTrigger(databaseId string, collectionId string, triggerId string) DataStoreStatus
CreateTrigger(databaseId string, collectionId string, trigger Trigger) (Trigger, DataStoreStatus)
GetAllStoredProcedures(databaseId string, collectionId string) ([]StoredProcedure, DataStoreStatus)
GetStoredProcedure(databaseId string, collectionId string, storedProcedureId string) (StoredProcedure, DataStoreStatus)
DeleteStoredProcedure(databaseId string, collectionId string, storedProcedureId string) DataStoreStatus
CreateStoredProcedure(databaseId string, collectionId string, storedProcedure StoredProcedure) (StoredProcedure, DataStoreStatus)
GetAllUserDefinedFunctions(databaseId string, collectionId string) ([]UserDefinedFunction, DataStoreStatus)
GetUserDefinedFunction(databaseId string, collectionId string, udfId string) (UserDefinedFunction, DataStoreStatus)
DeleteUserDefinedFunction(databaseId string, collectionId string, udfId string) DataStoreStatus
CreateUserDefinedFunction(databaseId string, collectionId string, udf UserDefinedFunction) (UserDefinedFunction, DataStoreStatus)
GetPartitionKeyRanges(databaseId string, collectionId string) ([]PartitionKeyRange, DataStoreStatus)
Close()
DumpToJson() (string, error)
}
type DocumentIterator interface {
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"`
}
@@ -0,0 +1,21 @@
package jsondatastore
import "github.com/pikami/cosmium/internal/datastore"
type ArrayDocumentIterator struct {
documents []datastore.Document
index int
}
func (i *ArrayDocumentIterator) Next() (datastore.Document, datastore.DataStoreStatus) {
i.index++
if i.index >= len(i.documents) {
return datastore.Document{}, datastore.StatusNotFound
}
return i.documents[i.index], datastore.StatusOk
}
func (i *ArrayDocumentIterator) Close() {
i.documents = []datastore.Document{}
}
@@ -0,0 +1,89 @@
package jsondatastore
import (
"fmt"
"time"
"github.com/google/uuid"
"github.com/pikami/cosmium/internal/datastore"
"github.com/pikami/cosmium/internal/resourceid"
structhidrators "github.com/pikami/cosmium/internal/struct_hidrators"
"golang.org/x/exp/maps"
)
func (r *JsonDataStore) GetAllCollections(databaseId string) ([]datastore.Collection, datastore.DataStoreStatus) {
r.storeState.RLock()
defer r.storeState.RUnlock()
if _, ok := r.storeState.Databases[databaseId]; !ok {
return make([]datastore.Collection, 0), datastore.StatusNotFound
}
return maps.Values(r.storeState.Collections[databaseId]), datastore.StatusOk
}
func (r *JsonDataStore) GetCollection(databaseId string, collectionId string) (datastore.Collection, datastore.DataStoreStatus) {
r.storeState.RLock()
defer r.storeState.RUnlock()
if _, ok := r.storeState.Databases[databaseId]; !ok {
return datastore.Collection{}, datastore.StatusNotFound
}
if _, ok := r.storeState.Collections[databaseId][collectionId]; !ok {
return datastore.Collection{}, datastore.StatusNotFound
}
return r.storeState.Collections[databaseId][collectionId], datastore.StatusOk
}
func (r *JsonDataStore) DeleteCollection(databaseId string, collectionId string) datastore.DataStoreStatus {
r.storeState.Lock()
defer r.storeState.Unlock()
if _, ok := r.storeState.Databases[databaseId]; !ok {
return datastore.StatusNotFound
}
if _, ok := r.storeState.Collections[databaseId][collectionId]; !ok {
return datastore.StatusNotFound
}
delete(r.storeState.Collections[databaseId], collectionId)
delete(r.storeState.Documents[databaseId], collectionId)
delete(r.storeState.Triggers[databaseId], collectionId)
delete(r.storeState.StoredProcedures[databaseId], collectionId)
delete(r.storeState.UserDefinedFunctions[databaseId], collectionId)
return datastore.StatusOk
}
func (r *JsonDataStore) CreateCollection(databaseId string, newCollection datastore.Collection) (datastore.Collection, datastore.DataStoreStatus) {
r.storeState.Lock()
defer r.storeState.Unlock()
var ok bool
var database datastore.Database
if database, ok = r.storeState.Databases[databaseId]; !ok {
return datastore.Collection{}, datastore.StatusNotFound
}
if _, ok = r.storeState.Collections[databaseId][newCollection.ID]; ok {
return datastore.Collection{}, datastore.Conflict
}
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)
r.storeState.Collections[databaseId][newCollection.ID] = newCollection
r.storeState.Documents[databaseId][newCollection.ID] = make(map[string]datastore.Document)
r.storeState.Triggers[databaseId][newCollection.ID] = make(map[string]datastore.Trigger)
r.storeState.StoredProcedures[databaseId][newCollection.ID] = make(map[string]datastore.StoredProcedure)
r.storeState.UserDefinedFunctions[databaseId][newCollection.ID] = make(map[string]datastore.UserDefinedFunction)
return newCollection, datastore.StatusOk
}
@@ -0,0 +1,70 @@
package jsondatastore
import (
"fmt"
"time"
"github.com/google/uuid"
"github.com/pikami/cosmium/internal/datastore"
"github.com/pikami/cosmium/internal/resourceid"
"golang.org/x/exp/maps"
)
func (r *JsonDataStore) GetAllDatabases() ([]datastore.Database, datastore.DataStoreStatus) {
r.storeState.RLock()
defer r.storeState.RUnlock()
return maps.Values(r.storeState.Databases), datastore.StatusOk
}
func (r *JsonDataStore) GetDatabase(id string) (datastore.Database, datastore.DataStoreStatus) {
r.storeState.RLock()
defer r.storeState.RUnlock()
if database, ok := r.storeState.Databases[id]; ok {
return database, datastore.StatusOk
}
return datastore.Database{}, datastore.StatusNotFound
}
func (r *JsonDataStore) DeleteDatabase(id string) datastore.DataStoreStatus {
r.storeState.Lock()
defer r.storeState.Unlock()
if _, ok := r.storeState.Databases[id]; !ok {
return datastore.StatusNotFound
}
delete(r.storeState.Databases, id)
delete(r.storeState.Collections, id)
delete(r.storeState.Documents, id)
delete(r.storeState.Triggers, id)
delete(r.storeState.StoredProcedures, id)
delete(r.storeState.UserDefinedFunctions, id)
return datastore.StatusOk
}
func (r *JsonDataStore) CreateDatabase(newDatabase datastore.Database) (datastore.Database, datastore.DataStoreStatus) {
r.storeState.Lock()
defer r.storeState.Unlock()
if _, ok := r.storeState.Databases[newDatabase.ID]; ok {
return datastore.Database{}, datastore.Conflict
}
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)
r.storeState.Databases[newDatabase.ID] = newDatabase
r.storeState.Collections[newDatabase.ID] = make(map[string]datastore.Collection)
r.storeState.Documents[newDatabase.ID] = make(map[string]map[string]datastore.Document)
r.storeState.Triggers[newDatabase.ID] = make(map[string]map[string]datastore.Trigger)
r.storeState.StoredProcedures[newDatabase.ID] = make(map[string]map[string]datastore.StoredProcedure)
r.storeState.UserDefinedFunctions[newDatabase.ID] = make(map[string]map[string]datastore.UserDefinedFunction)
return newDatabase, datastore.StatusOk
}
@@ -0,0 +1,113 @@
package jsondatastore
import (
"fmt"
"time"
"github.com/google/uuid"
"github.com/pikami/cosmium/internal/datastore"
"github.com/pikami/cosmium/internal/resourceid"
"golang.org/x/exp/maps"
)
func (r *JsonDataStore) GetAllDocuments(databaseId string, collectionId string) ([]datastore.Document, datastore.DataStoreStatus) {
r.storeState.RLock()
defer r.storeState.RUnlock()
if _, ok := r.storeState.Databases[databaseId]; !ok {
return make([]datastore.Document, 0), datastore.StatusNotFound
}
if _, ok := r.storeState.Collections[databaseId][collectionId]; !ok {
return make([]datastore.Document, 0), datastore.StatusNotFound
}
return maps.Values(r.storeState.Documents[databaseId][collectionId]), datastore.StatusOk
}
func (r *JsonDataStore) GetDocument(databaseId string, collectionId string, documentId string) (datastore.Document, datastore.DataStoreStatus) {
r.storeState.RLock()
defer r.storeState.RUnlock()
if _, ok := r.storeState.Databases[databaseId]; !ok {
return datastore.Document{}, datastore.StatusNotFound
}
if _, ok := r.storeState.Collections[databaseId][collectionId]; !ok {
return datastore.Document{}, datastore.StatusNotFound
}
if _, ok := r.storeState.Documents[databaseId][collectionId][documentId]; !ok {
return datastore.Document{}, datastore.StatusNotFound
}
return r.storeState.Documents[databaseId][collectionId][documentId], datastore.StatusOk
}
func (r *JsonDataStore) DeleteDocument(databaseId string, collectionId string, documentId string) datastore.DataStoreStatus {
r.storeState.Lock()
defer r.storeState.Unlock()
if _, ok := r.storeState.Databases[databaseId]; !ok {
return datastore.StatusNotFound
}
if _, ok := r.storeState.Collections[databaseId][collectionId]; !ok {
return datastore.StatusNotFound
}
if _, ok := r.storeState.Documents[databaseId][collectionId][documentId]; !ok {
return datastore.StatusNotFound
}
delete(r.storeState.Documents[databaseId][collectionId], documentId)
return datastore.StatusOk
}
func (r *JsonDataStore) CreateDocument(databaseId string, collectionId string, document map[string]interface{}) (datastore.Document, datastore.DataStoreStatus) {
r.storeState.Lock()
defer r.storeState.Unlock()
var ok bool
var documentId string
var database datastore.Database
var collection datastore.Collection
if documentId, ok = document["id"].(string); !ok || documentId == "" {
documentId = fmt.Sprint(uuid.New())
document["id"] = documentId
}
if database, ok = r.storeState.Databases[databaseId]; !ok {
return datastore.Document{}, datastore.StatusNotFound
}
if collection, ok = r.storeState.Collections[databaseId][collectionId]; !ok {
return datastore.Document{}, datastore.StatusNotFound
}
if _, ok := r.storeState.Documents[databaseId][collectionId][documentId]; ok {
return datastore.Document{}, datastore.Conflict
}
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"])
r.storeState.Documents[databaseId][collectionId][documentId] = document
return document, datastore.StatusOk
}
func (r *JsonDataStore) GetDocumentIterator(databaseId string, collectionId string) (datastore.DocumentIterator, datastore.DataStoreStatus) {
documents, status := r.GetAllDocuments(databaseId, collectionId)
if status != datastore.StatusOk {
return nil, status
}
return &ArrayDocumentIterator{
documents: documents,
index: -1,
}, datastore.StatusOk
}
@@ -0,0 +1,34 @@
package jsondatastore
import "github.com/pikami/cosmium/internal/datastore"
type JsonDataStore struct {
storeState State
initialDataFilePath string
persistDataFilePath string
}
type JsonDataStoreOptions struct {
InitialDataFilePath string
PersistDataFilePath string
}
func NewJsonDataStore(options JsonDataStoreOptions) *JsonDataStore {
dataStore := &JsonDataStore{
storeState: State{
Databases: make(map[string]datastore.Database),
Collections: make(map[string]map[string]datastore.Collection),
Documents: make(map[string]map[string]map[string]datastore.Document),
Triggers: make(map[string]map[string]map[string]datastore.Trigger),
StoredProcedures: make(map[string]map[string]map[string]datastore.StoredProcedure),
UserDefinedFunctions: make(map[string]map[string]map[string]datastore.UserDefinedFunction),
},
initialDataFilePath: options.InitialDataFilePath,
persistDataFilePath: options.PersistDataFilePath,
}
dataStore.InitializeDataStore()
return dataStore
}
@@ -0,0 +1,49 @@
package jsondatastore
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 *JsonDataStore) GetPartitionKeyRanges(databaseId string, collectionId string) ([]datastore.PartitionKeyRange, datastore.DataStoreStatus) {
r.storeState.RLock()
defer r.storeState.RUnlock()
databaseRid := databaseId
collectionRid := collectionId
var timestamp int64 = 0
if database, ok := r.storeState.Databases[databaseId]; !ok {
databaseRid = database.ResourceID
}
if collection, ok := r.storeState.Collections[databaseId][collectionId]; !ok {
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
}
+236
View File
@@ -0,0 +1,236 @@
package jsondatastore
import (
"encoding/json"
"log"
"os"
"reflect"
"sync"
"github.com/pikami/cosmium/internal/datastore"
"github.com/pikami/cosmium/internal/logger"
)
type State struct {
sync.RWMutex
// Map databaseId -> Database
Databases map[string]datastore.Database `json:"databases"`
// Map databaseId -> collectionId -> Collection
Collections map[string]map[string]datastore.Collection `json:"collections"`
// Map databaseId -> collectionId -> documentId -> Documents
Documents map[string]map[string]map[string]datastore.Document `json:"documents"`
// Map databaseId -> collectionId -> triggerId -> Trigger
Triggers map[string]map[string]map[string]datastore.Trigger `json:"triggers"`
// Map databaseId -> collectionId -> spId -> StoredProcedure
StoredProcedures map[string]map[string]map[string]datastore.StoredProcedure `json:"sprocs"`
// Map databaseId -> collectionId -> udfId -> UserDefinedFunction
UserDefinedFunctions map[string]map[string]map[string]datastore.UserDefinedFunction `json:"udfs"`
}
func (r *JsonDataStore) InitializeDataStore() {
if r.initialDataFilePath != "" {
r.LoadStateFS(r.initialDataFilePath)
return
}
if r.persistDataFilePath != "" {
stat, err := os.Stat(r.persistDataFilePath)
if err != nil {
return
}
if stat.IsDir() {
logger.ErrorLn("Argument '-Persist' must be a path to file, not a directory.")
os.Exit(1)
}
r.LoadStateFS(r.persistDataFilePath)
return
}
}
func (r *JsonDataStore) LoadStateFS(filePath string) {
data, err := os.ReadFile(filePath)
if err != nil {
log.Fatalf("Error reading state JSON file: %v", err)
return
}
err = r.LoadStateJSON(string(data))
if err != nil {
log.Fatalf("Error unmarshalling state JSON: %v", err)
}
}
func (r *JsonDataStore) LoadStateJSON(jsonData string) error {
r.storeState.Lock()
defer r.storeState.Unlock()
var state State
if err := json.Unmarshal([]byte(jsonData), &state); err != nil {
return err
}
r.storeState.Collections = state.Collections
r.storeState.Databases = state.Databases
r.storeState.Documents = state.Documents
r.ensureStoreStateNoNullReferences()
logger.InfoLn("Loaded state:")
logger.Infof("Databases: %d\n", getLength(r.storeState.Databases))
logger.Infof("Collections: %d\n", getLength(r.storeState.Collections))
logger.Infof("Documents: %d\n", getLength(r.storeState.Documents))
logger.Infof("Triggers: %d\n", getLength(r.storeState.Triggers))
logger.Infof("Stored procedures: %d\n", getLength(r.storeState.StoredProcedures))
logger.Infof("User defined functions: %d\n", getLength(r.storeState.UserDefinedFunctions))
return nil
}
func (r *JsonDataStore) SaveStateFS(filePath string) {
r.storeState.RLock()
defer r.storeState.RUnlock()
data, err := json.MarshalIndent(r.storeState, "", "\t")
if err != nil {
logger.Errorf("Failed to save state: %v\n", err)
return
}
os.WriteFile(filePath, data, os.ModePerm)
logger.InfoLn("Saved state:")
logger.Infof("Databases: %d\n", getLength(r.storeState.Databases))
logger.Infof("Collections: %d\n", getLength(r.storeState.Collections))
logger.Infof("Documents: %d\n", getLength(r.storeState.Documents))
logger.Infof("Triggers: %d\n", getLength(r.storeState.Triggers))
logger.Infof("Stored procedures: %d\n", getLength(r.storeState.StoredProcedures))
logger.Infof("User defined functions: %d\n", getLength(r.storeState.UserDefinedFunctions))
}
func (r *JsonDataStore) DumpToJson() (string, error) {
r.storeState.RLock()
defer r.storeState.RUnlock()
data, err := json.MarshalIndent(r.storeState, "", "\t")
if err != nil {
logger.Errorf("Failed to serialize state: %v\n", err)
return "", err
}
return string(data), nil
}
func (r *JsonDataStore) Close() {
if r.persistDataFilePath != "" {
r.SaveStateFS(r.persistDataFilePath)
}
}
func getLength(v interface{}) int {
switch v.(type) {
case datastore.Database,
datastore.Collection,
datastore.Document,
datastore.Trigger,
datastore.StoredProcedure,
datastore.UserDefinedFunction:
return 1
}
rv := reflect.ValueOf(v)
if rv.Kind() != reflect.Map {
return -1
}
count := 0
for _, key := range rv.MapKeys() {
if rv.MapIndex(key).Kind() == reflect.Map {
count += getLength(rv.MapIndex(key).Interface())
} else {
count++
}
}
return count
}
func (r *JsonDataStore) ensureStoreStateNoNullReferences() {
if r.storeState.Databases == nil {
r.storeState.Databases = make(map[string]datastore.Database)
}
if r.storeState.Collections == nil {
r.storeState.Collections = make(map[string]map[string]datastore.Collection)
}
if r.storeState.Documents == nil {
r.storeState.Documents = make(map[string]map[string]map[string]datastore.Document)
}
if r.storeState.Triggers == nil {
r.storeState.Triggers = make(map[string]map[string]map[string]datastore.Trigger)
}
if r.storeState.StoredProcedures == nil {
r.storeState.StoredProcedures = make(map[string]map[string]map[string]datastore.StoredProcedure)
}
if r.storeState.UserDefinedFunctions == nil {
r.storeState.UserDefinedFunctions = make(map[string]map[string]map[string]datastore.UserDefinedFunction)
}
for database := range r.storeState.Databases {
if r.storeState.Collections[database] == nil {
r.storeState.Collections[database] = make(map[string]datastore.Collection)
}
if r.storeState.Documents[database] == nil {
r.storeState.Documents[database] = make(map[string]map[string]datastore.Document)
}
if r.storeState.Triggers[database] == nil {
r.storeState.Triggers[database] = make(map[string]map[string]datastore.Trigger)
}
if r.storeState.StoredProcedures[database] == nil {
r.storeState.StoredProcedures[database] = make(map[string]map[string]datastore.StoredProcedure)
}
if r.storeState.UserDefinedFunctions[database] == nil {
r.storeState.UserDefinedFunctions[database] = make(map[string]map[string]datastore.UserDefinedFunction)
}
for collection := range r.storeState.Collections[database] {
if r.storeState.Documents[database][collection] == nil {
r.storeState.Documents[database][collection] = make(map[string]datastore.Document)
}
for document := range r.storeState.Documents[database][collection] {
if r.storeState.Documents[database][collection][document] == nil {
delete(r.storeState.Documents[database][collection], document)
}
}
if r.storeState.Triggers[database][collection] == nil {
r.storeState.Triggers[database][collection] = make(map[string]datastore.Trigger)
}
if r.storeState.StoredProcedures[database][collection] == nil {
r.storeState.StoredProcedures[database][collection] = make(map[string]datastore.StoredProcedure)
}
if r.storeState.UserDefinedFunctions[database][collection] == nil {
r.storeState.UserDefinedFunctions[database][collection] = make(map[string]datastore.UserDefinedFunction)
}
}
}
}
@@ -0,0 +1,91 @@
package jsondatastore
import (
"fmt"
"time"
"github.com/google/uuid"
"github.com/pikami/cosmium/internal/datastore"
"github.com/pikami/cosmium/internal/resourceid"
"golang.org/x/exp/maps"
)
func (r *JsonDataStore) GetAllStoredProcedures(databaseId string, collectionId string) ([]datastore.StoredProcedure, datastore.DataStoreStatus) {
r.storeState.RLock()
defer r.storeState.RUnlock()
return maps.Values(r.storeState.StoredProcedures[databaseId][collectionId]), datastore.StatusOk
}
func (r *JsonDataStore) GetStoredProcedure(databaseId string, collectionId string, spId string) (datastore.StoredProcedure, datastore.DataStoreStatus) {
r.storeState.RLock()
defer r.storeState.RUnlock()
if _, ok := r.storeState.Databases[databaseId]; !ok {
return datastore.StoredProcedure{}, datastore.StatusNotFound
}
if _, ok := r.storeState.Collections[databaseId][collectionId]; !ok {
return datastore.StoredProcedure{}, datastore.StatusNotFound
}
if sp, ok := r.storeState.StoredProcedures[databaseId][collectionId][spId]; ok {
return sp, datastore.StatusOk
}
return datastore.StoredProcedure{}, datastore.StatusNotFound
}
func (r *JsonDataStore) DeleteStoredProcedure(databaseId string, collectionId string, spId string) datastore.DataStoreStatus {
r.storeState.Lock()
defer r.storeState.Unlock()
if _, ok := r.storeState.Databases[databaseId]; !ok {
return datastore.StatusNotFound
}
if _, ok := r.storeState.Collections[databaseId][collectionId]; !ok {
return datastore.StatusNotFound
}
if _, ok := r.storeState.StoredProcedures[databaseId][collectionId][spId]; !ok {
return datastore.StatusNotFound
}
delete(r.storeState.StoredProcedures[databaseId][collectionId], spId)
return datastore.StatusOk
}
func (r *JsonDataStore) CreateStoredProcedure(databaseId string, collectionId string, sp datastore.StoredProcedure) (datastore.StoredProcedure, datastore.DataStoreStatus) {
r.storeState.Lock()
defer r.storeState.Unlock()
var ok bool
var database datastore.Database
var collection datastore.Collection
if sp.ID == "" {
return datastore.StoredProcedure{}, datastore.BadRequest
}
if database, ok = r.storeState.Databases[databaseId]; !ok {
return datastore.StoredProcedure{}, datastore.StatusNotFound
}
if collection, ok = r.storeState.Collections[databaseId][collectionId]; !ok {
return datastore.StoredProcedure{}, datastore.StatusNotFound
}
if _, ok = r.storeState.StoredProcedures[databaseId][collectionId][sp.ID]; ok {
return datastore.StoredProcedure{}, datastore.Conflict
}
sp.TimeStamp = time.Now().Unix()
sp.ResourceID = resourceid.NewCombined(collection.ResourceID, resourceid.New(resourceid.ResourceTypeStoredProcedure))
sp.ETag = fmt.Sprintf("\"%s\"", uuid.New())
sp.Self = fmt.Sprintf("dbs/%s/colls/%s/sprocs/%s/", database.ResourceID, collection.ResourceID, sp.ResourceID)
r.storeState.StoredProcedures[databaseId][collectionId][sp.ID] = sp
return sp, datastore.StatusOk
}
@@ -0,0 +1,91 @@
package jsondatastore
import (
"fmt"
"time"
"github.com/google/uuid"
"github.com/pikami/cosmium/internal/datastore"
"github.com/pikami/cosmium/internal/resourceid"
"golang.org/x/exp/maps"
)
func (r *JsonDataStore) GetAllTriggers(databaseId string, collectionId string) ([]datastore.Trigger, datastore.DataStoreStatus) {
r.storeState.RLock()
defer r.storeState.RUnlock()
return maps.Values(r.storeState.Triggers[databaseId][collectionId]), datastore.StatusOk
}
func (r *JsonDataStore) GetTrigger(databaseId string, collectionId string, triggerId string) (datastore.Trigger, datastore.DataStoreStatus) {
r.storeState.RLock()
defer r.storeState.RUnlock()
if _, ok := r.storeState.Databases[databaseId]; !ok {
return datastore.Trigger{}, datastore.StatusNotFound
}
if _, ok := r.storeState.Collections[databaseId][collectionId]; !ok {
return datastore.Trigger{}, datastore.StatusNotFound
}
if trigger, ok := r.storeState.Triggers[databaseId][collectionId][triggerId]; ok {
return trigger, datastore.StatusOk
}
return datastore.Trigger{}, datastore.StatusNotFound
}
func (r *JsonDataStore) DeleteTrigger(databaseId string, collectionId string, triggerId string) datastore.DataStoreStatus {
r.storeState.Lock()
defer r.storeState.Unlock()
if _, ok := r.storeState.Databases[databaseId]; !ok {
return datastore.StatusNotFound
}
if _, ok := r.storeState.Collections[databaseId][collectionId]; !ok {
return datastore.StatusNotFound
}
if _, ok := r.storeState.Triggers[databaseId][collectionId][triggerId]; !ok {
return datastore.StatusNotFound
}
delete(r.storeState.Triggers[databaseId][collectionId], triggerId)
return datastore.StatusOk
}
func (r *JsonDataStore) CreateTrigger(databaseId string, collectionId string, trigger datastore.Trigger) (datastore.Trigger, datastore.DataStoreStatus) {
r.storeState.Lock()
defer r.storeState.Unlock()
var ok bool
var database datastore.Database
var collection datastore.Collection
if trigger.ID == "" {
return datastore.Trigger{}, datastore.BadRequest
}
if database, ok = r.storeState.Databases[databaseId]; !ok {
return datastore.Trigger{}, datastore.StatusNotFound
}
if collection, ok = r.storeState.Collections[databaseId][collectionId]; !ok {
return datastore.Trigger{}, datastore.StatusNotFound
}
if _, ok = r.storeState.Triggers[databaseId][collectionId][trigger.ID]; ok {
return datastore.Trigger{}, datastore.Conflict
}
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)
r.storeState.Triggers[databaseId][collectionId][trigger.ID] = trigger
return trigger, datastore.StatusOk
}
@@ -0,0 +1,91 @@
package jsondatastore
import (
"fmt"
"time"
"github.com/google/uuid"
"github.com/pikami/cosmium/internal/datastore"
"github.com/pikami/cosmium/internal/resourceid"
"golang.org/x/exp/maps"
)
func (r *JsonDataStore) GetAllUserDefinedFunctions(databaseId string, collectionId string) ([]datastore.UserDefinedFunction, datastore.DataStoreStatus) {
r.storeState.RLock()
defer r.storeState.RUnlock()
return maps.Values(r.storeState.UserDefinedFunctions[databaseId][collectionId]), datastore.StatusOk
}
func (r *JsonDataStore) GetUserDefinedFunction(databaseId string, collectionId string, udfId string) (datastore.UserDefinedFunction, datastore.DataStoreStatus) {
r.storeState.RLock()
defer r.storeState.RUnlock()
if _, ok := r.storeState.Databases[databaseId]; !ok {
return datastore.UserDefinedFunction{}, datastore.StatusNotFound
}
if _, ok := r.storeState.Collections[databaseId][collectionId]; !ok {
return datastore.UserDefinedFunction{}, datastore.StatusNotFound
}
if udf, ok := r.storeState.UserDefinedFunctions[databaseId][collectionId][udfId]; ok {
return udf, datastore.StatusOk
}
return datastore.UserDefinedFunction{}, datastore.StatusNotFound
}
func (r *JsonDataStore) DeleteUserDefinedFunction(databaseId string, collectionId string, udfId string) datastore.DataStoreStatus {
r.storeState.Lock()
defer r.storeState.Unlock()
if _, ok := r.storeState.Databases[databaseId]; !ok {
return datastore.StatusNotFound
}
if _, ok := r.storeState.Collections[databaseId][collectionId]; !ok {
return datastore.StatusNotFound
}
if _, ok := r.storeState.UserDefinedFunctions[databaseId][collectionId][udfId]; !ok {
return datastore.StatusNotFound
}
delete(r.storeState.UserDefinedFunctions[databaseId][collectionId], udfId)
return datastore.StatusOk
}
func (r *JsonDataStore) CreateUserDefinedFunction(databaseId string, collectionId string, udf datastore.UserDefinedFunction) (datastore.UserDefinedFunction, datastore.DataStoreStatus) {
r.storeState.Lock()
defer r.storeState.Unlock()
var ok bool
var database datastore.Database
var collection datastore.Collection
if udf.ID == "" {
return datastore.UserDefinedFunction{}, datastore.BadRequest
}
if database, ok = r.storeState.Databases[databaseId]; !ok {
return datastore.UserDefinedFunction{}, datastore.StatusNotFound
}
if collection, ok = r.storeState.Collections[databaseId][collectionId]; !ok {
return datastore.UserDefinedFunction{}, datastore.StatusNotFound
}
if _, ok := r.storeState.UserDefinedFunctions[databaseId][collectionId][udf.ID]; ok {
return datastore.UserDefinedFunction{}, datastore.Conflict
}
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)
r.storeState.UserDefinedFunctions[databaseId][collectionId][udf.ID] = udf
return udf, datastore.StatusOk
}
@@ -1,26 +1,45 @@
package repositorymodels package datastore
type Database struct { type Database struct {
ID string `json:"id"` ID string `json:"id"`
TimeStamp int64 `json:"_ts"` TimeStamp int64 `json:"_ts"`
UniqueID string `json:"_rid"` ResourceID string `json:"_rid"`
ETag string `json:"_etag"` ETag string `json:"_etag"`
Self string `json:"_self"`
} }
type RepositoryStatus 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 DataStoreStatus = 5
Unknown DataStoreStatus = 6
)
type TriggerOperation string
const (
All TriggerOperation = "All"
Create TriggerOperation = "Create"
Delete TriggerOperation = "Delete"
Replace TriggerOperation = "Replace"
)
type TriggerType string
const (
Pre TriggerType = "Pre"
Post TriggerType = "Post"
) )
type Collection struct { type Collection struct {
ID string `json:"id"` ID string `json:"id"`
IndexingPolicy CollectionIndexingPolicy `json:"indexingPolicy"` IndexingPolicy CollectionIndexingPolicy `json:"indexingPolicy"`
PartitionKey CollectionPartitionKey `json:"partitionKey"` PartitionKey CollectionPartitionKey `json:"partitionKey"`
UniqueID string `json:"_rid"` ResourceID string `json:"_rid"`
TimeStamp int64 `json:"_ts"` TimeStamp int64 `json:"_ts"`
Self string `json:"_self"` Self string `json:"_self"`
ETag string `json:"_etag"` ETag string `json:"_etag"`
@@ -56,36 +75,36 @@ type CollectionPartitionKey struct {
type UserDefinedFunction struct { type UserDefinedFunction struct {
Body string `json:"body"` Body string `json:"body"`
ID string `json:"id"` ID string `json:"id"`
Rid string `json:"_rid"` ResourceID string `json:"_rid"`
Ts int `json:"_ts"` TimeStamp int64 `json:"_ts"`
Self string `json:"_self"` Self string `json:"_self"`
Etag string `json:"_etag"` ETag string `json:"_etag"`
} }
type StoredProcedure struct { type StoredProcedure struct {
Body string `json:"body"` Body string `json:"body"`
ID string `json:"id"` ID string `json:"id"`
Rid string `json:"_rid"` ResourceID string `json:"_rid"`
Ts int `json:"_ts"` TimeStamp int64 `json:"_ts"`
Self string `json:"_self"` Self string `json:"_self"`
Etag string `json:"_etag"` ETag string `json:"_etag"`
} }
type Trigger struct { type Trigger struct {
Body string `json:"body"` Body string `json:"body"`
ID string `json:"id"` ID string `json:"id"`
TriggerOperation string `json:"triggerOperation"` TriggerOperation TriggerOperation `json:"triggerOperation"`
TriggerType string `json:"triggerType"` TriggerType TriggerType `json:"triggerType"`
Rid string `json:"_rid"` ResourceID string `json:"_rid"`
Ts int `json:"_ts"` TimeStamp int64 `json:"_ts"`
Self string `json:"_self"` Self string `json:"_self"`
Etag string `json:"_etag"` ETag string `json:"_etag"`
} }
type Document map[string]interface{} type Document map[string]interface{}
type PartitionKeyRange struct { type PartitionKeyRange struct {
Rid string `json:"_rid"` ResourceID string `json:"_rid"`
ID string `json:"id"` ID string `json:"id"`
Etag string `json:"_etag"` Etag string `json:"_etag"`
MinInclusive string `json:"minInclusive"` MinInclusive string `json:"minInclusive"`
@@ -95,17 +114,6 @@ type PartitionKeyRange struct {
ThroughputFraction int `json:"throughputFraction"` ThroughputFraction int `json:"throughputFraction"`
Status string `json:"status"` Status string `json:"status"`
Parents []any `json:"parents"` Parents []any `json:"parents"`
Ts int `json:"_ts"` TimeStamp int64 `json:"_ts"`
Lsn int `json:"lsn"` Lsn int `json:"lsn"`
} }
type State 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"`
}
+140
View File
@@ -0,0 +1,140 @@
package logger
import (
"fmt"
"log"
"os"
"runtime"
"strings"
"sync"
)
type LogLevelType int
var (
LogLevelDebug LogLevelType = 0
LogLevelInfo LogLevelType = 1
LogLevelError LogLevelType = 2
LogLevelSilent LogLevelType = 10
)
type LogWriter struct {
WriterLevel LogLevelType
}
var logLevelMutex sync.RWMutex
var logLevel = LogLevelInfo
var DebugLogger = log.New(os.Stdout, "", log.Ldate|log.Ltime)
var InfoLogger = log.New(os.Stdout, "", log.Ldate|log.Ltime)
var ErrorLogger = log.New(os.Stderr, "", log.Ldate|log.Ltime)
func DebugLn(v ...any) {
if GetLogLevel() <= LogLevelDebug {
prefix := getCallerPrefix()
DebugLogger.Println(append([]interface{}{prefix}, v...)...)
}
}
func Debug(v ...any) {
if GetLogLevel() <= LogLevelDebug {
prefix := getCallerPrefix()
DebugLogger.Println(append([]interface{}{prefix}, v...)...)
}
}
func Debugf(format string, v ...any) {
if GetLogLevel() <= LogLevelDebug {
prefix := getCallerPrefix()
DebugLogger.Printf(prefix+format, v...)
}
}
func InfoLn(v ...any) {
if GetLogLevel() <= LogLevelInfo {
InfoLogger.Println(v...)
}
}
func Info(v ...any) {
if GetLogLevel() <= LogLevelInfo {
InfoLogger.Print(v...)
}
}
func Infof(format string, v ...any) {
if GetLogLevel() <= LogLevelInfo {
InfoLogger.Printf(format, v...)
}
}
func ErrorLn(v ...any) {
if GetLogLevel() <= LogLevelError {
prefix := getCallerPrefix()
ErrorLogger.Println(append([]interface{}{prefix}, v...)...)
}
}
func Error(v ...any) {
if GetLogLevel() <= LogLevelError {
prefix := getCallerPrefix()
ErrorLogger.Print(append([]interface{}{prefix}, v...)...)
}
}
func Errorf(format string, v ...any) {
if GetLogLevel() <= LogLevelError {
prefix := getCallerPrefix()
ErrorLogger.Printf(prefix+format, v...)
}
}
func (lw *LogWriter) Write(p []byte) (n int, err error) {
switch lw.WriterLevel {
case LogLevelDebug:
Debug(string(p))
case LogLevelInfo:
Info(string(p))
case LogLevelError:
Error(string(p))
}
return len(p), nil
}
func ErrorWriter() *LogWriter {
return &LogWriter{WriterLevel: LogLevelError}
}
func InfoWriter() *LogWriter {
return &LogWriter{WriterLevel: LogLevelInfo}
}
func DebugWriter() *LogWriter {
return &LogWriter{WriterLevel: LogLevelDebug}
}
func SetLogLevel(level LogLevelType) {
logLevelMutex.Lock()
defer logLevelMutex.Unlock()
logLevel = level
}
func GetLogLevel() LogLevelType {
logLevelMutex.RLock()
defer logLevelMutex.RUnlock()
return logLevel
}
func getCallerPrefix() string {
_, file, line, ok := runtime.Caller(2)
if ok {
parts := strings.Split(file, "/")
if len(parts) > 0 {
file = parts[len(parts)-1]
}
return fmt.Sprintf("%s:%d - ", file, line)
}
return ""
}
-66
View File
@@ -1,66 +0,0 @@
package repositories
import (
"fmt"
"time"
"github.com/google/uuid"
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
structhidrators "github.com/pikami/cosmium/internal/struct_hidrators"
"golang.org/x/exp/maps"
)
func GetAllCollections(databaseId string) ([]repositorymodels.Collection, repositorymodels.RepositoryStatus) {
if _, ok := storeState.Databases[databaseId]; !ok {
return make([]repositorymodels.Collection, 0), repositorymodels.StatusNotFound
}
return maps.Values(storeState.Collections[databaseId]), repositorymodels.StatusOk
}
func GetCollection(databaseId string, collectionId string) (repositorymodels.Collection, repositorymodels.RepositoryStatus) {
if _, ok := storeState.Databases[databaseId]; !ok {
return repositorymodels.Collection{}, repositorymodels.StatusNotFound
}
if _, ok := storeState.Collections[databaseId][collectionId]; !ok {
return repositorymodels.Collection{}, repositorymodels.StatusNotFound
}
return storeState.Collections[databaseId][collectionId], repositorymodels.StatusOk
}
func DeleteCollection(databaseId string, collectionId string) repositorymodels.RepositoryStatus {
if _, ok := storeState.Databases[databaseId]; !ok {
return repositorymodels.StatusNotFound
}
if _, ok := storeState.Collections[databaseId][collectionId]; !ok {
return repositorymodels.StatusNotFound
}
delete(storeState.Collections[databaseId], collectionId)
return repositorymodels.StatusOk
}
func CreateCollection(databaseId string, newCollection repositorymodels.Collection) (repositorymodels.Collection, repositorymodels.RepositoryStatus) {
if _, ok := storeState.Databases[databaseId]; !ok {
return repositorymodels.Collection{}, repositorymodels.StatusNotFound
}
if _, ok := storeState.Collections[databaseId][newCollection.ID]; ok {
return repositorymodels.Collection{}, repositorymodels.Conflict
}
newCollection = structhidrators.Hidrate(newCollection).(repositorymodels.Collection)
newCollection.TimeStamp = time.Now().Unix()
newCollection.UniqueID = uuid.New().String()
newCollection.ETag = fmt.Sprintf("\"%s\"", newCollection.UniqueID)
storeState.Collections[databaseId][newCollection.ID] = newCollection
storeState.Documents[databaseId][newCollection.ID] = make(map[string]repositorymodels.Document)
return newCollection, repositorymodels.StatusOk
}
-47
View File
@@ -1,47 +0,0 @@
package repositories
import (
"fmt"
"time"
"github.com/google/uuid"
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
"golang.org/x/exp/maps"
)
func GetAllDatabases() ([]repositorymodels.Database, repositorymodels.RepositoryStatus) {
return maps.Values(storeState.Databases), repositorymodels.StatusOk
}
func GetDatabase(id string) (repositorymodels.Database, repositorymodels.RepositoryStatus) {
if database, ok := storeState.Databases[id]; ok {
return database, repositorymodels.StatusOk
}
return repositorymodels.Database{}, repositorymodels.StatusNotFound
}
func DeleteDatabase(id string) repositorymodels.RepositoryStatus {
if _, ok := storeState.Databases[id]; !ok {
return repositorymodels.StatusNotFound
}
delete(storeState.Databases, id)
return repositorymodels.StatusOk
}
func CreateDatabase(newDatabase repositorymodels.Database) (repositorymodels.Database, repositorymodels.RepositoryStatus) {
if _, ok := storeState.Databases[newDatabase.ID]; ok {
return repositorymodels.Database{}, repositorymodels.Conflict
}
newDatabase.TimeStamp = time.Now().Unix()
newDatabase.UniqueID = uuid.New().String()
newDatabase.ETag = fmt.Sprintf("\"%s\"", newDatabase.UniqueID)
storeState.Databases[newDatabase.ID] = newDatabase
storeState.Collections[newDatabase.ID] = make(map[string]repositorymodels.Collection)
storeState.Documents[newDatabase.ID] = make(map[string]map[string]repositorymodels.Document)
return newDatabase, repositorymodels.StatusOk
}
-113
View File
@@ -1,113 +0,0 @@
package repositories
import (
"fmt"
"log"
"time"
"github.com/google/uuid"
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
"github.com/pikami/cosmium/parsers"
"github.com/pikami/cosmium/parsers/nosql"
memoryexecutor "github.com/pikami/cosmium/query_executors/memory_executor"
"golang.org/x/exp/maps"
)
func GetAllDocuments(databaseId string, collectionId string) ([]repositorymodels.Document, repositorymodels.RepositoryStatus) {
if _, ok := storeState.Databases[databaseId]; !ok {
return make([]repositorymodels.Document, 0), repositorymodels.StatusNotFound
}
if _, ok := storeState.Collections[databaseId][collectionId]; !ok {
return make([]repositorymodels.Document, 0), repositorymodels.StatusNotFound
}
return maps.Values(storeState.Documents[databaseId][collectionId]), repositorymodels.StatusOk
}
func GetDocument(databaseId string, collectionId string, documentId string) (repositorymodels.Document, repositorymodels.RepositoryStatus) {
if _, ok := storeState.Databases[databaseId]; !ok {
return repositorymodels.Document{}, repositorymodels.StatusNotFound
}
if _, ok := storeState.Collections[databaseId][collectionId]; !ok {
return repositorymodels.Document{}, repositorymodels.StatusNotFound
}
if _, ok := storeState.Documents[databaseId][collectionId][documentId]; !ok {
return repositorymodels.Document{}, repositorymodels.StatusNotFound
}
return storeState.Documents[databaseId][collectionId][documentId], repositorymodels.StatusOk
}
func DeleteDocument(databaseId string, collectionId string, documentId string) repositorymodels.RepositoryStatus {
if _, ok := storeState.Databases[databaseId]; !ok {
return repositorymodels.StatusNotFound
}
if _, ok := storeState.Collections[databaseId][collectionId]; !ok {
return repositorymodels.StatusNotFound
}
if _, ok := storeState.Documents[databaseId][collectionId][documentId]; !ok {
return repositorymodels.StatusNotFound
}
delete(storeState.Documents[databaseId][collectionId], documentId)
return repositorymodels.StatusOk
}
func CreateDocument(databaseId string, collectionId string, document map[string]interface{}) (repositorymodels.Document, repositorymodels.RepositoryStatus) {
var documentId string
var ok bool
if documentId, ok = document["id"].(string); !ok || documentId == "" {
return repositorymodels.Document{}, repositorymodels.BadRequest
}
if _, ok := storeState.Databases[databaseId]; !ok {
return repositorymodels.Document{}, repositorymodels.StatusNotFound
}
if _, ok = storeState.Collections[databaseId][collectionId]; !ok {
return repositorymodels.Document{}, repositorymodels.StatusNotFound
}
if _, ok := storeState.Documents[databaseId][collectionId][documentId]; ok {
return repositorymodels.Document{}, repositorymodels.Conflict
}
document["_ts"] = time.Now().Unix()
document["_rid"] = uuid.New().String()
document["_etag"] = fmt.Sprintf("\"%s\"", document["_rid"])
storeState.Documents[databaseId][collectionId][documentId] = document
return document, repositorymodels.StatusOk
}
func ExecuteQueryDocuments(databaseId string, collectionId string, query string, queryParameters map[string]interface{}) ([]memoryexecutor.RowType, repositorymodels.RepositoryStatus) {
parsedQuery, err := nosql.Parse("", []byte(query))
if err != nil {
log.Printf("Failed to parse query: %s\nerr: %v", query, err)
return nil, repositorymodels.BadRequest
}
collectionDocuments, status := GetAllDocuments(databaseId, collectionId)
if status != repositorymodels.StatusOk {
return nil, status
}
covDocs := make([]memoryexecutor.RowType, 0)
for _, doc := range collectionDocuments {
covDocs = append(covDocs, map[string]interface{}(doc))
}
if typedQuery, ok := parsedQuery.(parsers.SelectStmt); ok {
typedQuery.Parameters = queryParameters
return memoryexecutor.Execute(typedQuery, covDocs), repositorymodels.StatusOk
}
return nil, repositorymodels.BadRequest
}
@@ -1,23 +0,0 @@
package repositories
import repositorymodels "github.com/pikami/cosmium/internal/repository_models"
func GetPartitionKeyRanges(databaseId string, collectionId string) ([]repositorymodels.PartitionKeyRange, repositorymodels.RepositoryStatus) {
// I have no idea what this is tbh
return []repositorymodels.PartitionKeyRange{
{
Rid: "ZxlyAP7rKwACAAAAAAAAUA==",
ID: "0",
Etag: "\"00005504-0000-0100-0000-65c555490000\"",
MinInclusive: "",
MaxExclusive: "FF",
RidPrefix: 0,
Self: "dbs/ZxlyAA==/colls/ZxlyAP7rKwA=/pkranges/ZxlyAP7rKwACAAAAAAAAUA==/",
ThroughputFraction: 1,
Status: "online",
Parents: []interface{}{},
Ts: 1707431241,
Lsn: 17,
},
}, repositorymodels.StatusOk
}
-121
View File
@@ -1,121 +0,0 @@
package repositories
import (
"encoding/json"
"fmt"
"log"
"os"
"reflect"
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
)
var storedProcedures = []repositorymodels.StoredProcedure{}
var triggers = []repositorymodels.Trigger{}
var userDefinedFunctions = []repositorymodels.UserDefinedFunction{}
var storeState = repositorymodels.State{
Databases: make(map[string]repositorymodels.Database),
Collections: make(map[string]map[string]repositorymodels.Collection),
Documents: make(map[string]map[string]map[string]repositorymodels.Document),
}
func LoadStateFS(filePath string) {
data, err := os.ReadFile(filePath)
if err != nil {
log.Fatalf("Error reading state JSON file: %v", err)
}
var state repositorymodels.State
if err := json.Unmarshal(data, &state); err != nil {
log.Fatalf("Error unmarshalling state JSON: %v", err)
}
fmt.Println("Loaded state:")
fmt.Printf("Databases: %d\n", getLength(state.Databases))
fmt.Printf("Collections: %d\n", getLength(state.Collections))
fmt.Printf("Documents: %d\n", getLength(state.Documents))
storeState = state
ensureStoreStateNoNullReferences()
}
func SaveStateFS(filePath string) {
data, err := json.MarshalIndent(storeState, "", "\t")
if err != nil {
fmt.Printf("Failed to save state: %v\n", err)
return
}
os.WriteFile(filePath, data, os.ModePerm)
fmt.Println("Saved state:")
fmt.Printf("Databases: %d\n", getLength(storeState.Databases))
fmt.Printf("Collections: %d\n", getLength(storeState.Collections))
fmt.Printf("Documents: %d\n", getLength(storeState.Documents))
}
func GetState() repositorymodels.State {
return storeState
}
func getLength(v interface{}) int {
switch v.(type) {
case repositorymodels.Database,
repositorymodels.Collection,
repositorymodels.Document:
return 1
}
rv := reflect.ValueOf(v)
if rv.Kind() != reflect.Map {
return -1
}
count := 0
for _, key := range rv.MapKeys() {
if rv.MapIndex(key).Kind() == reflect.Map {
count += getLength(rv.MapIndex(key).Interface())
} else {
count++
}
}
return count
}
func ensureStoreStateNoNullReferences() {
if storeState.Databases == nil {
storeState.Databases = make(map[string]repositorymodels.Database)
}
if storeState.Collections == nil {
storeState.Collections = make(map[string]map[string]repositorymodels.Collection)
}
if storeState.Documents == nil {
storeState.Documents = make(map[string]map[string]map[string]repositorymodels.Document)
}
for database := range storeState.Databases {
if storeState.Collections[database] == nil {
storeState.Collections[database] = make(map[string]repositorymodels.Collection)
}
if storeState.Documents[database] == nil {
storeState.Documents[database] = make(map[string]map[string]repositorymodels.Document)
}
for collection := range storeState.Collections[database] {
if storeState.Documents[database][collection] == nil {
storeState.Documents[database][collection] = make(map[string]repositorymodels.Document)
}
for document := range storeState.Documents[database][collection] {
if storeState.Documents[database][collection][document] == nil {
delete(storeState.Documents[database][collection], document)
}
}
}
}
}
@@ -1,7 +0,0 @@
package repositories
import repositorymodels "github.com/pikami/cosmium/internal/repository_models"
func GetAllStoredProcedures(databaseId string, collectionId string) ([]repositorymodels.StoredProcedure, repositorymodels.RepositoryStatus) {
return storedProcedures, repositorymodels.StatusOk
}
-7
View File
@@ -1,7 +0,0 @@
package repositories
import repositorymodels "github.com/pikami/cosmium/internal/repository_models"
func GetAllTriggers(databaseId string, collectionId string) ([]repositorymodels.Trigger, repositorymodels.RepositoryStatus) {
return triggers, repositorymodels.StatusOk
}
@@ -1,7 +0,0 @@
package repositories
import repositorymodels "github.com/pikami/cosmium/internal/repository_models"
func GetAllUserDefinedFunctions(databaseId string, collectionId string) ([]repositorymodels.UserDefinedFunction, repositorymodels.RepositoryStatus) {
return userDefinedFunctions, repositorymodels.StatusOk
}
+95
View File
@@ -0,0 +1,95 @@
package resourceid
import (
"encoding/base64"
"math/rand"
"strings"
"github.com/google/uuid"
)
type ResourceType int
const (
ResourceTypeDatabase ResourceType = iota
ResourceTypeCollection
ResourceTypeDocument
ResourceTypeStoredProcedure
ResourceTypeTrigger
ResourceTypeUserDefinedFunction
ResourceTypeConflict
ResourceTypePartitionKeyRange
ResourceTypeSchema
)
func New(resourceType ResourceType) string {
var idBytes []byte
switch resourceType {
case ResourceTypeDatabase:
idBytes = randomBytes(4)
case ResourceTypeCollection:
idBytes = randomBytes(4)
// first byte should be bigger than 0x80 for collection ids
// clients classify this id as "user" otherwise
if (idBytes[0] & 0x80) <= 0 {
idBytes[0] = byte(rand.Intn(0x80) + 0x80)
}
case ResourceTypeDocument:
idBytes = randomBytes(8)
idBytes[7] = byte(rand.Intn(0x10)) // Upper 4 bits = 0
case ResourceTypeStoredProcedure:
idBytes = randomBytes(8)
idBytes[7] = byte(rand.Intn(0x10)) | 0x08 // Upper 4 bits = 0x08
case ResourceTypeTrigger:
idBytes = randomBytes(8)
idBytes[7] = byte(rand.Intn(0x10)) | 0x07 // Upper 4 bits = 0x07
case ResourceTypeUserDefinedFunction:
idBytes = randomBytes(8)
idBytes[7] = byte(rand.Intn(0x10)) | 0x06 // Upper 4 bits = 0x06
case ResourceTypeConflict:
idBytes = randomBytes(8)
idBytes[7] = byte(rand.Intn(0x10)) | 0x04 // Upper 4 bits = 0x04
case ResourceTypePartitionKeyRange:
// we don't do partitions yet, so just use a fixed id
idBytes = []byte{0x69, 0x69, 0x69, 0x69, 0x69, 0x69, 0x69, 0x50}
case ResourceTypeSchema:
idBytes = randomBytes(8)
idBytes[7] = byte(rand.Intn(0x10)) | 0x09 // Upper 4 bits = 0x09
default:
idBytes = randomBytes(4)
}
encoded := base64.StdEncoding.EncodeToString(idBytes)
return strings.ReplaceAll(encoded, "/", "-")
}
func NewCombined(ids ...string) string {
combinedIdBytes := make([]byte, 0)
for _, id := range ids {
idBytes, _ := base64.StdEncoding.DecodeString(strings.ReplaceAll(id, "-", "/"))
combinedIdBytes = append(combinedIdBytes, idBytes...)
}
encoded := base64.StdEncoding.EncodeToString(combinedIdBytes)
return strings.ReplaceAll(encoded, "/", "-")
}
func uintToBytes(id uint32) []byte {
buf := make([]byte, 4)
for i := 0; i < 4; i++ {
buf[i] = byte(id >> (i * 8))
}
return buf
}
func randomBytes(count int) []byte {
buf := make([]byte, count)
for i := 0; i < count; i += 4 {
id := uuid.New().ID()
idBytes := uintToBytes(id)
copy(buf[i:], idBytes)
}
return buf
}
+138
View File
@@ -0,0 +1,138 @@
package rntbd
import (
"bytes"
"encoding/binary"
)
type RntbdResponseFrame struct {
StatusCode uint16
ResourceType RntbdResourceType
ActivityId []byte
ResponseHeaders []RntbdResponseHeader
Payload []byte
}
type RntbdResponseHeader struct {
HeaderId uint16
TokenType RntbdTokenType
TokenValue any
}
type RntbdResponseFrameBuilder struct {
frame RntbdResponseFrame
}
func (b *RntbdResponseFrameBuilder) AddHeader(headerId uint16, tokenType RntbdTokenType, tokenValue any) {
b.frame.ResponseHeaders = append(b.frame.ResponseHeaders, RntbdResponseHeader{
HeaderId: headerId,
TokenType: tokenType,
TokenValue: tokenValue,
})
}
func (b *RntbdResponseFrameBuilder) AddPayload(payload []byte) {
b.frame.Payload = payload
}
func (b *RntbdResponseFrameBuilder) SetStatusCode(statusCode uint16) {
b.frame.StatusCode = statusCode
}
func (b *RntbdResponseFrameBuilder) SetResourceType(resourceType RntbdResourceType) {
b.frame.ResourceType = resourceType
}
func (b *RntbdResponseFrameBuilder) SetActivityId(activityId []byte) {
b.frame.ActivityId = activityId
}
func (b *RntbdResponseFrameBuilder) Build() *RntbdResponseFrame {
return &b.frame
}
func (f *RntbdResponseFrame) ToBytes() []byte {
var buffer bytes.Buffer
binary.Write(&buffer, binary.LittleEndian, f.StatusCode)
binary.Write(&buffer, binary.LittleEndian, uint16(f.ResourceType))
binary.Write(&buffer, binary.LittleEndian, f.ActivityId)
for _, header := range f.ResponseHeaders {
binary.Write(&buffer, binary.LittleEndian, header.HeaderId)
binary.Write(&buffer, binary.LittleEndian, uint8(header.TokenType))
switch header.TokenType {
case RntbdTokenTypeByte:
buffer.Write(header.TokenValue.([]byte))
case RntbdTokenTypeUShort:
binary.Write(&buffer, binary.LittleEndian, header.TokenValue.(uint16))
case RntbdTokenTypeULong:
binary.Write(&buffer, binary.LittleEndian, header.TokenValue.(uint32))
case RntbdTokenTypeLong:
binary.Write(&buffer, binary.LittleEndian, header.TokenValue.(int32))
case RntbdTokenTypeULongLong:
binary.Write(&buffer, binary.LittleEndian, header.TokenValue.(uint64))
case RntbdTokenTypeLongLong:
binary.Write(&buffer, binary.LittleEndian, header.TokenValue.(int64))
case RntbdTokenTypeGuid:
buffer.Write(header.TokenValue.([]byte))
case RntbdTokenTypeSmallString:
binary.Write(&buffer, binary.LittleEndian, uint8(len(header.TokenValue.(string))))
buffer.WriteString(header.TokenValue.(string))
case RntbdTokenTypeString:
binary.Write(&buffer, binary.LittleEndian, uint16(len(header.TokenValue.(string))))
buffer.WriteString(header.TokenValue.(string))
case RntbdTokenTypeULongString:
binary.Write(&buffer, binary.LittleEndian, uint32(len(header.TokenValue.(string))))
buffer.WriteString(header.TokenValue.(string))
case RntbdTokenTypeSmallBytes:
binary.Write(&buffer, binary.LittleEndian, uint8(len(header.TokenValue.([]byte))))
buffer.Write(header.TokenValue.([]byte))
case RntbdTokenTypeBytes:
binary.Write(&buffer, binary.LittleEndian, uint16(len(header.TokenValue.([]byte))))
buffer.Write(header.TokenValue.([]byte))
case RntbdTokenTypeULongBytes:
binary.Write(&buffer, binary.LittleEndian, uint32(len(header.TokenValue.([]byte))))
buffer.Write(header.TokenValue.([]byte))
case RntbdTokenTypeFloat:
binary.Write(&buffer, binary.LittleEndian, header.TokenValue.(float32))
case RntbdTokenTypeDouble:
binary.Write(&buffer, binary.LittleEndian, header.TokenValue.(float64))
case RntbdTokenTypeInvalid:
panic("invalid token type")
default:
panic("invalid token type")
}
}
payloadSize := uint32(0)
if len(f.Payload) > 0 {
payloadSize = uint32(len(f.Payload)) + 4
}
frameSize := uint32(buffer.Len()) + 4
result := make([]byte, frameSize+payloadSize)
binary.LittleEndian.PutUint32(result, frameSize)
copy(result[4:], buffer.Bytes())
if len(f.Payload) > 0 {
binary.LittleEndian.PutUint32(result[frameSize:], payloadSize-4)
copy(result[frameSize+4:], f.Payload)
}
return result
}
func buildContextFrame(requestFrame *RntbdFrame) []byte {
builder := RntbdResponseFrameBuilder{}
builder.SetStatusCode(200)
builder.SetResourceType(RntbdResourceTypeConnection)
builder.SetActivityId(requestFrame.ActivityId)
builder.AddHeader(uint16(RntbdContextHeaderServerAgent), RntbdTokenTypeSmallString, "DocumentDB Server")
builder.AddHeader(uint16(RntbdContextHeaderServerVersion), RntbdTokenTypeSmallString, " version=2.14.0.0")
builder.AddHeader(uint16(RntbdContextHeaderIdleTimeoutInSeconds), RntbdTokenTypeULong, uint32(120))
builder.AddHeader(uint16(RntbdContextHeaderUnauthenticatedTimeoutInSeconds), RntbdTokenTypeULong, uint32(25))
return builder.Build().ToBytes()
}
+746
View File
@@ -0,0 +1,746 @@
package rntbd
import (
"fmt"
"github.com/pikami/cosmium/api/headers"
)
type RntbdOperationType uint16
const (
RntbdOperationTypeConnection RntbdOperationType = 0x0000
RntbdOperationTypeCreate RntbdOperationType = 0x0001
RntbdOperationTypeUpdate RntbdOperationType = 0x0002
RntbdOperationTypeRead RntbdOperationType = 0x0003
RntbdOperationTypeReadFeed RntbdOperationType = 0x0004
RntbdOperationTypeDelete RntbdOperationType = 0x0005
RntbdOperationTypeReplace RntbdOperationType = 0x0006
RntbdOperationTypeExecuteJavaScript RntbdOperationType = 0x0008
RntbdOperationTypeSQLQuery RntbdOperationType = 0x0009
RntbdOperationTypePause RntbdOperationType = 0x000A
RntbdOperationTypeResume RntbdOperationType = 0x000B
RntbdOperationTypeStop RntbdOperationType = 0x000C
RntbdOperationTypeRecycle RntbdOperationType = 0x000D
RntbdOperationTypeCrash RntbdOperationType = 0x000E
RntbdOperationTypeQuery RntbdOperationType = 0x000F
RntbdOperationTypeForceConfigRefresh RntbdOperationType = 0x0010
RntbdOperationTypeHead RntbdOperationType = 0x0011
RntbdOperationTypeHeadFeed RntbdOperationType = 0x0012
RntbdOperationTypeUpsert RntbdOperationType = 0x0013
RntbdOperationTypeRecreate RntbdOperationType = 0x0014
RntbdOperationTypeThrottle RntbdOperationType = 0x0015
RntbdOperationTypeGetSplitPoint RntbdOperationType = 0x0016
RntbdOperationTypePreCreateValidation RntbdOperationType = 0x0017
RntbdOperationTypeBatchApply RntbdOperationType = 0x0018
RntbdOperationTypeAbortSplit RntbdOperationType = 0x0019
RntbdOperationTypeCompleteSplit RntbdOperationType = 0x001A
RntbdOperationTypeOfferUpdateOperation RntbdOperationType = 0x001B
RntbdOperationTypeOfferPreGrowValidation RntbdOperationType = 0x001C
RntbdOperationTypeBatchReportThroughputUtilization RntbdOperationType = 0x001D
RntbdOperationTypeCompletePartitionMigration RntbdOperationType = 0x001E
RntbdOperationTypeAbortPartitionMigration RntbdOperationType = 0x001F
RntbdOperationTypePreReplaceValidation RntbdOperationType = 0x0020
RntbdOperationTypeAddComputeGatewayRequestCharges RntbdOperationType = 0x0021
RntbdOperationTypeMigratePartition RntbdOperationType = 0x0022
)
type RntbdResourceType uint16
const (
RntbdResourceTypeConnection RntbdResourceType = 0x0000
RntbdResourceTypeDatabase RntbdResourceType = 0x0001
RntbdResourceTypeCollection RntbdResourceType = 0x0002
RntbdResourceTypeDocument RntbdResourceType = 0x0003
RntbdResourceTypeAttachment RntbdResourceType = 0x0004
RntbdResourceTypeUser RntbdResourceType = 0x0005
RntbdResourceTypePermission RntbdResourceType = 0x0006
RntbdResourceTypeStoredProcedure RntbdResourceType = 0x0007
RntbdResourceTypeConflict RntbdResourceType = 0x0008
RntbdResourceTypeTrigger RntbdResourceType = 0x0009
RntbdResourceTypeUserDefinedFunction RntbdResourceType = 0x000A
RntbdResourceTypeModule RntbdResourceType = 0x000B
RntbdResourceTypeReplica RntbdResourceType = 0x000C
RntbdResourceTypeModuleCommand RntbdResourceType = 0x000D
RntbdResourceTypeRecord RntbdResourceType = 0x000E
RntbdResourceTypeOffer RntbdResourceType = 0x000F
RntbdResourceTypePartitionSetInformation RntbdResourceType = 0x0010
RntbdResourceTypeXPReplicatorAddress RntbdResourceType = 0x0011
RntbdResourceTypeMasterPartition RntbdResourceType = 0x0012
RntbdResourceTypeServerPartition RntbdResourceType = 0x0013
RntbdResourceTypeDatabaseAccount RntbdResourceType = 0x0014
RntbdResourceTypeTopology RntbdResourceType = 0x0015
RntbdResourceTypePartitionKeyRange RntbdResourceType = 0x0016
RntbdResourceTypeSchema RntbdResourceType = 0x0018
RntbdResourceTypeBatchApply RntbdResourceType = 0x0019
RntbdResourceTypeRestoreMetadata RntbdResourceType = 0x001A
RntbdResourceTypeComputeGatewayCharges RntbdResourceType = 0x001B
RntbdResourceTypeRidRange RntbdResourceType = 0x001C
RntbdResourceTypeUserDefinedType RntbdResourceType = 0x001D
)
type RntbdRequestHeader uint16
const (
RntbdRequestHeaderResourceId RntbdRequestHeader = 0x0000 // RntbdTokenType.Bytes, required = false
RntbdRequestHeaderAuthorizationToken RntbdRequestHeader = 0x0001 // RntbdTokenType.String, required = false
RntbdRequestHeaderPayloadPresent RntbdRequestHeader = 0x0002 // RntbdTokenType.Byte, required = true
RntbdRequestHeaderDate RntbdRequestHeader = 0x0003 // RntbdTokenType.SmallString, required = false
RntbdRequestHeaderPageSize RntbdRequestHeader = 0x0004 // RntbdTokenType.ULong, required = false
RntbdRequestHeaderSessionToken RntbdRequestHeader = 0x0005 // RntbdTokenType.String, required = false
RntbdRequestHeaderContinuationToken RntbdRequestHeader = 0x0006 // RntbdTokenType.String, required = false
RntbdRequestHeaderIndexingDirective RntbdRequestHeader = 0x0007 // RntbdTokenType.Byte, required = false
RntbdRequestHeaderMatch RntbdRequestHeader = 0x0008 // RntbdTokenType.String, required = false
RntbdRequestHeaderPreTriggerInclude RntbdRequestHeader = 0x0009 // RntbdTokenType.String, required = false
RntbdRequestHeaderPostTriggerInclude RntbdRequestHeader = 0x000A // RntbdTokenType.String, required = false
RntbdRequestHeaderIsFanout RntbdRequestHeader = 0x000B // RntbdTokenType.Byte, required = false
RntbdRequestHeaderCollectionPartitionIndex RntbdRequestHeader = 0x000C // RntbdTokenType.ULong, required = false
RntbdRequestHeaderCollectionServiceIndex RntbdRequestHeader = 0x000D // RntbdTokenType.ULong, required = false
RntbdRequestHeaderPreTriggerExclude RntbdRequestHeader = 0x000E // RntbdTokenType.String, required = false
RntbdRequestHeaderPostTriggerExclude RntbdRequestHeader = 0x000F // RntbdTokenType.String, required = false
RntbdRequestHeaderConsistencyLevel RntbdRequestHeader = 0x0010 // RntbdTokenType.Byte, required = false
RntbdRequestHeaderEntityId RntbdRequestHeader = 0x0011 // RntbdTokenType.String, required = false
RntbdRequestHeaderResourceSchemaName RntbdRequestHeader = 0x0012 // RntbdTokenType.SmallString, required = false
RntbdRequestHeaderReplicaPath RntbdRequestHeader = 0x0013 // RntbdTokenType.String, required = true
RntbdRequestHeaderResourceTokenExpiry RntbdRequestHeader = 0x0014 // RntbdTokenType.ULong, required = false
RntbdRequestHeaderDatabaseName RntbdRequestHeader = 0x0015 // RntbdTokenType.String, required = false
RntbdRequestHeaderCollectionName RntbdRequestHeader = 0x0016 // RntbdTokenType.String, required = false
RntbdRequestHeaderDocumentName RntbdRequestHeader = 0x0017 // RntbdTokenType.String, required = false
RntbdRequestHeaderAttachmentName RntbdRequestHeader = 0x0018 // RntbdTokenType.String, required = false
RntbdRequestHeaderUserName RntbdRequestHeader = 0x0019 // RntbdTokenType.String, required = false
RntbdRequestHeaderPermissionName RntbdRequestHeader = 0x001A // RntbdTokenType.String, required = false
RntbdRequestHeaderStoredProcedureName RntbdRequestHeader = 0x001B // RntbdTokenType.String, required = false
RntbdRequestHeaderUserDefinedFunctionName RntbdRequestHeader = 0x001C // RntbdTokenType.String, required = false
RntbdRequestHeaderTriggerName RntbdRequestHeader = 0x001D // RntbdTokenType.String, required = false
RntbdRequestHeaderEnableScanInQuery RntbdRequestHeader = 0x001E // RntbdTokenType.Byte, required = false
RntbdRequestHeaderEmitVerboseTracesInQuery RntbdRequestHeader = 0x001F // RntbdTokenType.Byte, required = false
RntbdRequestHeaderConflictName RntbdRequestHeader = 0x0020 // RntbdTokenType.String, required = false
RntbdRequestHeaderBindReplicaDirective RntbdRequestHeader = 0x0021 // RntbdTokenType.String, required = false
RntbdRequestHeaderPrimaryMasterKey RntbdRequestHeader = 0x0022 // RntbdTokenType.String, required = false
RntbdRequestHeaderSecondaryMasterKey RntbdRequestHeader = 0x0023 // RntbdTokenType.String, required = false
RntbdRequestHeaderPrimaryReadonlyKey RntbdRequestHeader = 0x0024 // RntbdTokenType.String, required = false
RntbdRequestHeaderSecondaryReadonlyKey RntbdRequestHeader = 0x0025 // RntbdTokenType.String, required = false
RntbdRequestHeaderProfileRequest RntbdRequestHeader = 0x0026 // RntbdTokenType.Byte, required = false
RntbdRequestHeaderEnableLowPrecisionOrderBy RntbdRequestHeader = 0x0027 // RntbdTokenType.Byte, required = false
RntbdRequestHeaderClientVersion RntbdRequestHeader = 0x0028 // RntbdTokenType.SmallString, required = false
RntbdRequestHeaderCanCharge RntbdRequestHeader = 0x0029 // RntbdTokenType.Byte, required = false
RntbdRequestHeaderCanThrottle RntbdRequestHeader = 0x002A // RntbdTokenType.Byte, required = false
RntbdRequestHeaderPartitionKey RntbdRequestHeader = 0x002B // RntbdTokenType.String, required = false
RntbdRequestHeaderPartitionKeyRangeId RntbdRequestHeader = 0x002C // RntbdTokenType.String, required = false
RntbdRequestHeaderNotUsed2D RntbdRequestHeader = 0x002D // RntbdTokenType.Invalid, required = false
RntbdRequestHeaderNotUsed2E RntbdRequestHeader = 0x002E // RntbdTokenType.Invalid, required = false
RntbdRequestHeaderNotUsed2F RntbdRequestHeader = 0x002F // RntbdTokenType.Invalid, required = false
RntbdRequestHeaderMigrateCollectionDirective RntbdRequestHeader = 0x0031 // RntbdTokenType.Byte, required = false
RntbdRequestHeaderNotUsed32 RntbdRequestHeader = 0x0032 // RntbdTokenType.Invalid, required = false
RntbdRequestHeaderSupportSpatialLegacyCoordinates RntbdRequestHeader = 0x0033 // RntbdTokenType.Byte, required = false
RntbdRequestHeaderPartitionCount RntbdRequestHeader = 0x0034 // RntbdTokenType.ULong, required = false
RntbdRequestHeaderCollectionRid RntbdRequestHeader = 0x0035 // RntbdTokenType.String, required = false
RntbdRequestHeaderPartitionKeyRangeName RntbdRequestHeader = 0x0036 // RntbdTokenType.String, required = false
RntbdRequestHeaderSchemaName RntbdRequestHeader = 0x003A // RntbdTokenType.String, required = false
RntbdRequestHeaderFilterBySchemaRid RntbdRequestHeader = 0x003B // RntbdTokenType.String, required = false
RntbdRequestHeaderUsePolygonsSmallerThanAHemisphere RntbdRequestHeader = 0x003C // RntbdTokenType.Byte, required = false
RntbdRequestHeaderGatewaySignature RntbdRequestHeader = 0x003D // RntbdTokenType.String, required = false
RntbdRequestHeaderEnableLogging RntbdRequestHeader = 0x003E // RntbdTokenType.Byte, required = false
RntbdRequestHeaderAIM RntbdRequestHeader = 0x003F // RntbdTokenType.String, required = false
RntbdRequestHeaderPopulateQuotaInfo RntbdRequestHeader = 0x0040 // RntbdTokenType.Byte, required = false
RntbdRequestHeaderDisableRUPerMinuteUsage RntbdRequestHeader = 0x0041 // RntbdTokenType.Byte, required = false
RntbdRequestHeaderPopulateQueryMetrics RntbdRequestHeader = 0x0042 // RntbdTokenType.Byte, required = false
RntbdRequestHeaderResponseContinuationTokenLimitInKb RntbdRequestHeader = 0x0043 // RntbdTokenType.ULong, required = false
RntbdRequestHeaderPopulatePartitionStatistics RntbdRequestHeader = 0x0044 // RntbdTokenType.Byte, required = false
RntbdRequestHeaderRemoteStorageType RntbdRequestHeader = 0x0045 // RntbdTokenType.Byte, required = false
RntbdRequestHeaderCollectionRemoteStorageSecurityIdentifier RntbdRequestHeader = 0x0046 // RntbdTokenType.String, required = false
RntbdRequestHeaderIfModifiedSince RntbdRequestHeader = 0x0047 // RntbdTokenType.String, required = false
RntbdRequestHeaderPopulateCollectionThroughputInfo RntbdRequestHeader = 0x0048 // RntbdTokenType.Byte, required = false
RntbdRequestHeaderRemainingTimeInMsOnClientRequest RntbdRequestHeader = 0x0049 // RntbdTokenType.ULong, required = false
RntbdRequestHeaderClientRetryAttemptCount RntbdRequestHeader = 0x004A // RntbdTokenType.ULong, required = false
RntbdRequestHeaderTargetLsn RntbdRequestHeader = 0x004B // RntbdTokenType.LongLong, required = false
RntbdRequestHeaderTargetGlobalCommittedLsn RntbdRequestHeader = 0x004C // RntbdTokenType.LongLong, required = false
RntbdRequestHeaderTransportRequestID RntbdRequestHeader = 0x004D // RntbdTokenType.ULong, required = false
RntbdRequestHeaderRestoreMetadaFilter RntbdRequestHeader = 0x004E // RntbdTokenType.String, required = false
RntbdRequestHeaderRestoreParams RntbdRequestHeader = 0x004F // RntbdTokenType.String, required = false
RntbdRequestHeaderShareThroughput RntbdRequestHeader = 0x0050 // RntbdTokenType.Byte, required = false
RntbdRequestHeaderPartitionResourceFilter RntbdRequestHeader = 0x0051 // RntbdTokenType.String, required = false
RntbdRequestHeaderIsReadOnlyScript RntbdRequestHeader = 0x0052 // RntbdTokenType.Byte, required = false
RntbdRequestHeaderIsAutoScaleRequest RntbdRequestHeader = 0x0053 // RntbdTokenType.Byte, required = false
RntbdRequestHeaderForceQueryScan RntbdRequestHeader = 0x0054 // RntbdTokenType.Byte, required = false
RntbdRequestHeaderCanOfferReplaceComplete RntbdRequestHeader = 0x0056 // RntbdTokenType.Byte, required = false
RntbdRequestHeaderExcludeSystemProperties RntbdRequestHeader = 0x0057 // RntbdTokenType.Byte, required = false
RntbdRequestHeaderBinaryId RntbdRequestHeader = 0x0058 // RntbdTokenType.Bytes, required = false
RntbdRequestHeaderTimeToLiveInSeconds RntbdRequestHeader = 0x0059 // RntbdTokenType.Long, required = false
RntbdRequestHeaderEffectivePartitionKey RntbdRequestHeader = 0x005A // RntbdTokenType.Bytes, required = false
RntbdRequestHeaderBinaryPassthroughRequest RntbdRequestHeader = 0x005B // RntbdTokenType.Byte, required = false
RntbdRequestHeaderUserDefinedTypeName RntbdRequestHeader = 0x005C // RntbdTokenType.String, required = false
RntbdRequestHeaderEnableDynamicRidRangeAllocation RntbdRequestHeader = 0x005D // RntbdTokenType.Byte, required = false
RntbdRequestHeaderEnumerationDirection RntbdRequestHeader = 0x005E // RntbdTokenType.Byte, required = false
RntbdRequestHeaderStartId RntbdRequestHeader = 0x005F // RntbdTokenType.Bytes, required = false
RntbdRequestHeaderEndId RntbdRequestHeader = 0x0060 // RntbdTokenType.Bytes, required = false
RntbdRequestHeaderFanoutOperationState RntbdRequestHeader = 0x0061 // RntbdTokenType.Byte, required = false
RntbdRequestHeaderStartEpk RntbdRequestHeader = 0x0062 // RntbdTokenType.Bytes, required = false
RntbdRequestHeaderEndEpk RntbdRequestHeader = 0x0063 // RntbdTokenType.Bytes, required = false
RntbdRequestHeaderReadFeedKeyType RntbdRequestHeader = 0x0064 // RntbdTokenType.Byte, required = false
RntbdRequestHeaderContentSerializationFormat RntbdRequestHeader = 0x0065 // RntbdTokenType.Byte, required = false
RntbdRequestHeaderAllowTentativeWrites RntbdRequestHeader = 0x0066 // RntbdTokenType.Byte, required = false
RntbdRequestHeaderIsUserRequest RntbdRequestHeader = 0x0067 // RntbdTokenType.Byte, required = false
RntbdRequestHeaderSharedOfferThroughput RntbdRequestHeader = 0x0068 // RntbdTokenType.ULong, required = false
RntbdRequestHeaderSDKSupportedCapabilities RntbdRequestHeader = 0x00A2 // RntbdTokenType.ULong, required = ?
)
type RntbdResponseHeaderType uint16
const (
RntbdResponseHeaderPayloadPresent RntbdResponseHeaderType = 0x0000 // RntbdTokenType.Byte, required = true
RntbdResponseHeaderLastStateChangeDateTime RntbdResponseHeaderType = 0x0002 // RntbdTokenType.SmallString, required = false
RntbdResponseHeaderContinuationToken RntbdResponseHeaderType = 0x0003 // RntbdTokenType.String, required = false
RntbdResponseHeaderETag RntbdResponseHeaderType = 0x0004 // RntbdTokenType.String, required = false
RntbdResponseHeaderReadsPerformed RntbdResponseHeaderType = 0x0007 // RntbdTokenType.ULong, required = false
RntbdResponseHeaderWritesPerformed RntbdResponseHeaderType = 0x0008 // RntbdTokenType.ULong, required = false
RntbdResponseHeaderQueriesPerformed RntbdResponseHeaderType = 0x0009 // RntbdTokenType.ULong, required = false
RntbdResponseHeaderIndexTermsGenerated RntbdResponseHeaderType = 0x000A // RntbdTokenType.ULong, required = false
RntbdResponseHeaderScriptsExecuted RntbdResponseHeaderType = 0x000B // RntbdTokenType.ULong, required = false
RntbdResponseHeaderRetryAfterMilliseconds RntbdResponseHeaderType = 0x000C // RntbdTokenType.ULong, required = false
RntbdResponseHeaderIndexingDirective RntbdResponseHeaderType = 0x000D // RntbdTokenType.Byte, required = false
RntbdResponseHeaderStorageMaxResoureQuota RntbdResponseHeaderType = 0x000E // RntbdTokenType.String, required = false
RntbdResponseHeaderStorageResourceQuotaUsage RntbdResponseHeaderType = 0x000F // RntbdTokenType.String, required = false
RntbdResponseHeaderSchemaVersion RntbdResponseHeaderType = 0x0010 // RntbdTokenType.SmallString, required = false
RntbdResponseHeaderCollectionPartitionIndex RntbdResponseHeaderType = 0x0011 // RntbdTokenType.ULong, required = false
RntbdResponseHeaderCollectionServiceIndex RntbdResponseHeaderType = 0x0012 // RntbdTokenType.ULong, required = false
RntbdResponseHeaderLSN RntbdResponseHeaderType = 0x0013 // RntbdTokenType.LongLong, required = false
RntbdResponseHeaderItemCount RntbdResponseHeaderType = 0x0014 // RntbdTokenType.ULong, required = false
RntbdResponseHeaderRequestCharge RntbdResponseHeaderType = 0x0015 // RntbdTokenType.Double, required = false
RntbdResponseHeaderOwnerFullName RntbdResponseHeaderType = 0x0017 // RntbdTokenType.String, required = false
RntbdResponseHeaderOwnerId RntbdResponseHeaderType = 0x0018 // RntbdTokenType.String, required = false
RntbdResponseHeaderDatabaseAccountId RntbdResponseHeaderType = 0x0019 // RntbdTokenType.String, required = false
RntbdResponseHeaderQuorumAckedLSN RntbdResponseHeaderType = 0x001A // RntbdTokenType.LongLong, required = false
RntbdResponseHeaderRequestValidationFailure RntbdResponseHeaderType = 0x001B // RntbdTokenType.Byte, required = false
RntbdResponseHeaderSubStatus RntbdResponseHeaderType = 0x001C // RntbdTokenType.ULong, required = false
RntbdResponseHeaderCollectionUpdateProgress RntbdResponseHeaderType = 0x001D // RntbdTokenType.ULong, required = false
RntbdResponseHeaderCurrentWriteQuorum RntbdResponseHeaderType = 0x001E // RntbdTokenType.ULong, required = false
RntbdResponseHeaderCurrentReplicaSetSize RntbdResponseHeaderType = 0x001F // RntbdTokenType.ULong, required = false
RntbdResponseHeaderCollectionLazyIndexProgress RntbdResponseHeaderType = 0x0020 // RntbdTokenType.ULong, required = false
RntbdResponseHeaderPartitionKeyRangeId RntbdResponseHeaderType = 0x0021 // RntbdTokenType.String, required = false
RntbdResponseHeaderLogResults RntbdResponseHeaderType = 0x0025 // RntbdTokenType.String, required = false
RntbdResponseHeaderXPRole RntbdResponseHeaderType = 0x0026 // RntbdTokenType.ULong, required = false
RntbdResponseHeaderIsRUPerMinuteUsed RntbdResponseHeaderType = 0x0027 // RntbdTokenType.Byte, required = false
RntbdResponseHeaderQueryMetrics RntbdResponseHeaderType = 0x0028 // RntbdTokenType.String, required = false
RntbdResponseHeaderGlobalCommittedLSN RntbdResponseHeaderType = 0x0029 // RntbdTokenType.LongLong, required = false
RntbdResponseHeaderNumberOfReadRegions RntbdResponseHeaderType = 0x0030 // RntbdTokenType.ULong, required = false
RntbdResponseHeaderOfferReplacePending RntbdResponseHeaderType = 0x0031 // RntbdTokenType.Byte, required = false
RntbdResponseHeaderItemLSN RntbdResponseHeaderType = 0x0032 // RntbdTokenType.LongLong, required = false
RntbdResponseHeaderRestoreState RntbdResponseHeaderType = 0x0033 // RntbdTokenType.String, required = false
RntbdResponseHeaderCollectionSecurityIdentifier RntbdResponseHeaderType = 0x0034 // RntbdTokenType.String, required = false
RntbdResponseHeaderTransportRequestID RntbdResponseHeaderType = 0x0035 // RntbdTokenType.ULong, required = false
RntbdResponseHeaderShareThroughput RntbdResponseHeaderType = 0x0036 // RntbdTokenType.Byte, required = false
RntbdResponseHeaderDisableRntbdChannel RntbdResponseHeaderType = 0x0038 // RntbdTokenType.Byte, required = false
RntbdResponseHeaderServerDateTimeUtc RntbdResponseHeaderType = 0x0039 // RntbdTokenType.SmallString, required = false
RntbdResponseHeaderLocalLSN RntbdResponseHeaderType = 0x003A // RntbdTokenType.LongLong, required = false
RntbdResponseHeaderQuorumAckedLocalLSN RntbdResponseHeaderType = 0x003B // RntbdTokenType.LongLong, required = false
RntbdResponseHeaderItemLocalLSN RntbdResponseHeaderType = 0x003C // RntbdTokenType.LongLong, required = false
RntbdResponseHeaderHasTentativeWrites RntbdResponseHeaderType = 0x003D // RntbdTokenType.Byte, required = false
RntbdResponseHeaderSessionToken RntbdResponseHeaderType = 0x003E // RntbdTokenType.String, required = false
)
type RntbdContextHeader uint16
const (
RntbdContextHeaderProtocolVersion RntbdContextHeader = 0x0000 // RntbdTokenType.ULong, required = false
RntbdContextHeaderClientVersion RntbdContextHeader = 0x0001 // RntbdTokenType.SmallString, required = false
RntbdContextHeaderServerAgent RntbdContextHeader = 0x0002 // RntbdTokenType.SmallString, required = true
RntbdContextHeaderServerVersion RntbdContextHeader = 0x0003 // RntbdTokenType.SmallString, required = true
RntbdContextHeaderIdleTimeoutInSeconds RntbdContextHeader = 0x0004 // RntbdTokenType.ULong, required = false
RntbdContextHeaderUnauthenticatedTimeoutInSeconds RntbdContextHeader = 0x0005 // RntbdTokenType.ULong, required = false
)
type RntbdTokenType uint8
const (
RntbdTokenTypeByte RntbdTokenType = 0x00 // 8bit boolean
RntbdTokenTypeUShort RntbdTokenType = 0x01 // 16bit unsigned integer
RntbdTokenTypeULong RntbdTokenType = 0x02 // 32bit unsigned integer
RntbdTokenTypeLong RntbdTokenType = 0x03 // 32bit signed integer
RntbdTokenTypeULongLong RntbdTokenType = 0x04 // 64bit unsigned integer
RntbdTokenTypeLongLong RntbdTokenType = 0x05 // 64bit signed integer
RntbdTokenTypeGuid RntbdTokenType = 0x06 // 128bit GUID
RntbdTokenTypeSmallString RntbdTokenType = 0x07 // 8bit len + string
RntbdTokenTypeString RntbdTokenType = 0x08 // 16bit len + string
RntbdTokenTypeULongString RntbdTokenType = 0x09 // 32bit len + string
RntbdTokenTypeSmallBytes RntbdTokenType = 0x0A // 8bit len + bytes
RntbdTokenTypeBytes RntbdTokenType = 0x0B // 16bit len + bytes
RntbdTokenTypeULongBytes RntbdTokenType = 0x0C // 32bit len + bytes
RntbdTokenTypeFloat RntbdTokenType = 0x0D // 32bit float
RntbdTokenTypeDouble RntbdTokenType = 0x0E // 64bit double
RntbdTokenTypeInvalid RntbdTokenType = 0x0F // Invalid token type
)
func (h RntbdRequestHeader) String() string {
switch h {
case RntbdRequestHeaderResourceId:
return "RntbdRequestHeaderResourceId"
case RntbdRequestHeaderAuthorizationToken:
return headers.Authorization
case RntbdRequestHeaderPayloadPresent:
return "RntbdRequestHeaderPayloadPresent"
case RntbdRequestHeaderDate:
return headers.XDate
case RntbdRequestHeaderPageSize:
return "RntbdRequestHeaderPageSize"
case RntbdRequestHeaderSessionToken:
return "RntbdRequestHeaderSessionToken"
case RntbdRequestHeaderContinuationToken:
return "RntbdRequestHeaderContinuationToken"
case RntbdRequestHeaderIndexingDirective:
return "RntbdRequestHeaderIndexingDirective"
case RntbdRequestHeaderMatch:
return "RntbdRequestHeaderMatch"
case RntbdRequestHeaderPreTriggerInclude:
return "RntbdRequestHeaderPreTriggerInclude"
case RntbdRequestHeaderPostTriggerInclude:
return "RntbdRequestHeaderPostTriggerInclude"
case RntbdRequestHeaderIsFanout:
return "RntbdRequestHeaderIsFanout"
case RntbdRequestHeaderCollectionPartitionIndex:
return "RntbdRequestHeaderCollectionPartitionIndex"
case RntbdRequestHeaderCollectionServiceIndex:
return "RntbdRequestHeaderCollectionServiceIndex"
case RntbdRequestHeaderPreTriggerExclude:
return "RntbdRequestHeaderPreTriggerExclude"
case RntbdRequestHeaderPostTriggerExclude:
return "RntbdRequestHeaderPostTriggerExclude"
case RntbdRequestHeaderConsistencyLevel:
return headers.ConsistencyLevel
case RntbdRequestHeaderEntityId:
return "RntbdRequestHeaderEntityId"
case RntbdRequestHeaderResourceSchemaName:
return "RntbdRequestHeaderResourceSchemaName"
case RntbdRequestHeaderReplicaPath:
return "RntbdRequestHeaderReplicaPath"
case RntbdRequestHeaderResourceTokenExpiry:
return "RntbdRequestHeaderResourceTokenExpiry"
case RntbdRequestHeaderDatabaseName:
return "RntbdRequestHeaderDatabaseName"
case RntbdRequestHeaderCollectionName:
return "RntbdRequestHeaderCollectionName"
case RntbdRequestHeaderDocumentName:
return "RntbdRequestHeaderDocumentName"
case RntbdRequestHeaderAttachmentName:
return "RntbdRequestHeaderAttachmentName"
case RntbdRequestHeaderUserName:
return "RntbdRequestHeaderUserName"
case RntbdRequestHeaderPermissionName:
return "RntbdRequestHeaderPermissionName"
case RntbdRequestHeaderStoredProcedureName:
return "RntbdRequestHeaderStoredProcedureName"
case RntbdRequestHeaderUserDefinedFunctionName:
return "RntbdRequestHeaderUserDefinedFunctionName"
case RntbdRequestHeaderTriggerName:
return "RntbdRequestHeaderTriggerName"
case RntbdRequestHeaderEnableScanInQuery:
return "RntbdRequestHeaderEnableScanInQuery"
case RntbdRequestHeaderEmitVerboseTracesInQuery:
return "RntbdRequestHeaderEmitVerboseTracesInQuery"
case RntbdRequestHeaderConflictName:
return "RntbdRequestHeaderConflictName"
case RntbdRequestHeaderBindReplicaDirective:
return "RntbdRequestHeaderBindReplicaDirective"
case RntbdRequestHeaderPrimaryMasterKey:
return "RntbdRequestHeaderPrimaryMasterKey"
case RntbdRequestHeaderSecondaryMasterKey:
return "RntbdRequestHeaderSecondaryMasterKey"
case RntbdRequestHeaderPrimaryReadonlyKey:
return "RntbdRequestHeaderPrimaryReadonlyKey"
case RntbdRequestHeaderSecondaryReadonlyKey:
return "RntbdRequestHeaderSecondaryReadonlyKey"
case RntbdRequestHeaderProfileRequest:
return "RntbdRequestHeaderProfileRequest"
case RntbdRequestHeaderEnableLowPrecisionOrderBy:
return "RntbdRequestHeaderEnableLowPrecisionOrderBy"
case RntbdRequestHeaderClientVersion:
return "RntbdRequestHeaderClientVersion"
case RntbdRequestHeaderCanCharge:
return "RntbdRequestHeaderCanCharge"
case RntbdRequestHeaderCanThrottle:
return "RntbdRequestHeaderCanThrottle"
case RntbdRequestHeaderPartitionKey:
return "RntbdRequestHeaderPartitionKey"
case RntbdRequestHeaderPartitionKeyRangeId:
return "RntbdRequestHeaderPartitionKeyRangeId"
case RntbdRequestHeaderNotUsed2D:
return "RntbdRequestHeaderNotUsed2D"
case RntbdRequestHeaderNotUsed2E:
return "RntbdRequestHeaderNotUsed2E"
case RntbdRequestHeaderNotUsed2F:
return "RntbdRequestHeaderNotUsed2F"
case RntbdRequestHeaderMigrateCollectionDirective:
return "RntbdRequestHeaderMigrateCollectionDirective"
case RntbdRequestHeaderNotUsed32:
return "RntbdRequestHeaderNotUsed32"
case RntbdRequestHeaderSupportSpatialLegacyCoordinates:
return "RntbdRequestHeaderSupportSpatialLegacyCoordinates"
case RntbdRequestHeaderPartitionCount:
return "RntbdRequestHeaderPartitionCount"
case RntbdRequestHeaderCollectionRid:
return "RntbdRequestHeaderCollectionRid"
case RntbdRequestHeaderPartitionKeyRangeName:
return "RntbdRequestHeaderPartitionKeyRangeName"
case RntbdRequestHeaderSchemaName:
return "RntbdRequestHeaderSchemaName"
case RntbdRequestHeaderFilterBySchemaRid:
return "RntbdRequestHeaderFilterBySchemaRid"
case RntbdRequestHeaderUsePolygonsSmallerThanAHemisphere:
return "RntbdRequestHeaderUsePolygonsSmallerThanAHemisphere"
case RntbdRequestHeaderGatewaySignature:
return "RntbdRequestHeaderGatewaySignature"
case RntbdRequestHeaderEnableLogging:
return "RntbdRequestHeaderEnableLogging"
case RntbdRequestHeaderAIM:
return headers.AIM
case RntbdRequestHeaderPopulateQuotaInfo:
return "RntbdRequestHeaderPopulateQuotaInfo"
case RntbdRequestHeaderDisableRUPerMinuteUsage:
return "RntbdRequestHeaderDisableRUPerMinuteUsage"
case RntbdRequestHeaderPopulateQueryMetrics:
return "RntbdRequestHeaderPopulateQueryMetrics"
case RntbdRequestHeaderResponseContinuationTokenLimitInKb:
return "RntbdRequestHeaderResponseContinuationTokenLimitInKb"
case RntbdRequestHeaderPopulatePartitionStatistics:
return "RntbdRequestHeaderPopulatePartitionStatistics"
case RntbdRequestHeaderRemoteStorageType:
return "RntbdRequestHeaderRemoteStorageType"
case RntbdRequestHeaderCollectionRemoteStorageSecurityIdentifier:
return "RntbdRequestHeaderCollectionRemoteStorageSecurityIdentifier"
case RntbdRequestHeaderIfModifiedSince:
return "RntbdRequestHeaderIfModifiedSince"
case RntbdRequestHeaderPopulateCollectionThroughputInfo:
return "RntbdRequestHeaderPopulateCollectionThroughputInfo"
case RntbdRequestHeaderRemainingTimeInMsOnClientRequest:
return headers.RemainingTimeInMsOnClient
case RntbdRequestHeaderClientRetryAttemptCount:
return headers.ClientRetryAttemptCount
case RntbdRequestHeaderTargetLsn:
return "RntbdRequestHeaderTargetLsn"
case RntbdRequestHeaderTargetGlobalCommittedLsn:
return "RntbdRequestHeaderTargetGlobalCommittedLsn"
case RntbdRequestHeaderTransportRequestID:
return "RntbdRequestHeaderTransportRequestID"
case RntbdRequestHeaderRestoreMetadaFilter:
return "RntbdRequestHeaderRestoreMetadaFilter"
case RntbdRequestHeaderRestoreParams:
return "RntbdRequestHeaderRestoreParams"
case RntbdRequestHeaderShareThroughput:
return "RntbdRequestHeaderShareThroughput"
case RntbdRequestHeaderPartitionResourceFilter:
return "RntbdRequestHeaderPartitionResourceFilter"
case RntbdRequestHeaderIsReadOnlyScript:
return "RntbdRequestHeaderIsReadOnlyScript"
case RntbdRequestHeaderIsAutoScaleRequest:
return "RntbdRequestHeaderIsAutoScaleRequest"
case RntbdRequestHeaderForceQueryScan:
return "RntbdRequestHeaderForceQueryScan"
case RntbdRequestHeaderCanOfferReplaceComplete:
return "RntbdRequestHeaderCanOfferReplaceComplete"
case RntbdRequestHeaderExcludeSystemProperties:
return "RntbdRequestHeaderExcludeSystemProperties"
case RntbdRequestHeaderBinaryId:
return "RntbdRequestHeaderBinaryId"
case RntbdRequestHeaderTimeToLiveInSeconds:
return "RntbdRequestHeaderTimeToLiveInSeconds"
case RntbdRequestHeaderEffectivePartitionKey:
return "RntbdRequestHeaderEffectivePartitionKey"
case RntbdRequestHeaderBinaryPassthroughRequest:
return "RntbdRequestHeaderBinaryPassthroughRequest"
case RntbdRequestHeaderUserDefinedTypeName:
return "RntbdRequestHeaderUserDefinedTypeName"
case RntbdRequestHeaderEnableDynamicRidRangeAllocation:
return "RntbdRequestHeaderEnableDynamicRidRangeAllocation"
case RntbdRequestHeaderEnumerationDirection:
return "RntbdRequestHeaderEnumerationDirection"
case RntbdRequestHeaderStartId:
return "RntbdRequestHeaderStartId"
case RntbdRequestHeaderEndId:
return "RntbdRequestHeaderEndId"
case RntbdRequestHeaderFanoutOperationState:
return "RntbdRequestHeaderFanoutOperationState"
case RntbdRequestHeaderStartEpk:
return "RntbdRequestHeaderStartEpk"
case RntbdRequestHeaderEndEpk:
return "RntbdRequestHeaderEndEpk"
case RntbdRequestHeaderReadFeedKeyType:
return "RntbdRequestHeaderReadFeedKeyType"
case RntbdRequestHeaderContentSerializationFormat:
return "RntbdRequestHeaderContentSerializationFormat"
case RntbdRequestHeaderAllowTentativeWrites:
return "RntbdRequestHeaderAllowTentativeWrites"
case RntbdRequestHeaderIsUserRequest:
return "RntbdRequestHeaderIsUserRequest"
case RntbdRequestHeaderSharedOfferThroughput:
return "RntbdRequestHeaderSharedOfferThroughput"
case RntbdRequestHeaderSDKSupportedCapabilities:
return headers.SupportedCapabilities
}
return fmt.Sprintf("RntbdRequestHeader(%d)", h)
}
func (h RntbdContextHeader) String() string {
switch h {
case RntbdContextHeaderProtocolVersion:
return "RntbdContextHeaderProtocolVersion"
case RntbdContextHeaderClientVersion:
return "RntbdContextHeaderClientVersion"
case RntbdContextHeaderServerAgent:
return "RntbdContextHeaderServerAgent"
case RntbdContextHeaderServerVersion:
return "RntbdContextHeaderServerVersion"
case RntbdContextHeaderIdleTimeoutInSeconds:
return "RntbdContextHeaderIdleTimeoutInSeconds"
case RntbdContextHeaderUnauthenticatedTimeoutInSeconds:
return "RntbdContextHeaderUnauthenticatedTimeoutInSeconds"
}
return fmt.Sprintf("RntbdContextHeader(%d)", h)
}
func (h RntbdResponseHeaderType) String() string {
switch h {
case RntbdResponseHeaderPayloadPresent:
return "PayloadPresent"
case RntbdResponseHeaderLastStateChangeDateTime:
return "LastStateChangeDateTime"
case RntbdResponseHeaderContinuationToken:
return "ContinuationToken"
case RntbdResponseHeaderETag:
return "ETag"
case RntbdResponseHeaderReadsPerformed:
return "ReadsPerformed"
case RntbdResponseHeaderWritesPerformed:
return "WritesPerformed"
case RntbdResponseHeaderQueriesPerformed:
return "QueriesPerformed"
case RntbdResponseHeaderIndexTermsGenerated:
return "IndexTermsGenerated"
case RntbdResponseHeaderScriptsExecuted:
return "ScriptsExecuted"
case RntbdResponseHeaderRetryAfterMilliseconds:
return "RetryAfterMilliseconds"
case RntbdResponseHeaderIndexingDirective:
return "IndexingDirective"
case RntbdResponseHeaderStorageMaxResoureQuota:
return "StorageMaxResoureQuota"
case RntbdResponseHeaderStorageResourceQuotaUsage:
return "StorageResourceQuotaUsage"
case RntbdResponseHeaderSchemaVersion:
return "SchemaVersion"
case RntbdResponseHeaderCollectionPartitionIndex:
return "CollectionPartitionIndex"
case RntbdResponseHeaderCollectionServiceIndex:
return "CollectionServiceIndex"
case RntbdResponseHeaderLSN:
return "LSN"
case RntbdResponseHeaderItemCount:
return "ItemCount"
case RntbdResponseHeaderRequestCharge:
return "RequestCharge"
case RntbdResponseHeaderOwnerFullName:
return "OwnerFullName"
case RntbdResponseHeaderOwnerId:
return "OwnerId"
case RntbdResponseHeaderDatabaseAccountId:
return "DatabaseAccountId"
case RntbdResponseHeaderQuorumAckedLSN:
return "QuorumAckedLSN"
case RntbdResponseHeaderRequestValidationFailure:
return "RequestValidationFailure"
case RntbdResponseHeaderSubStatus:
return "SubStatus"
case RntbdResponseHeaderCollectionUpdateProgress:
return "CollectionUpdateProgress"
case RntbdResponseHeaderCurrentWriteQuorum:
return "CurrentWriteQuorum"
case RntbdResponseHeaderCurrentReplicaSetSize:
return "CurrentReplicaSetSize"
case RntbdResponseHeaderCollectionLazyIndexProgress:
return "CollectionLazyIndexProgress"
case RntbdResponseHeaderPartitionKeyRangeId:
return "PartitionKeyRangeId"
case RntbdResponseHeaderLogResults:
return "LogResults"
case RntbdResponseHeaderXPRole:
return "XPRole"
case RntbdResponseHeaderIsRUPerMinuteUsed:
return "IsRUPerMinuteUsed"
case RntbdResponseHeaderQueryMetrics:
return "QueryMetrics"
case RntbdResponseHeaderGlobalCommittedLSN:
return "GlobalCommittedLSN"
case RntbdResponseHeaderNumberOfReadRegions:
return "NumberOfReadRegions"
case RntbdResponseHeaderOfferReplacePending:
return "OfferReplacePending"
case RntbdResponseHeaderItemLSN:
return "ItemLSN"
case RntbdResponseHeaderRestoreState:
return "RestoreState"
case RntbdResponseHeaderCollectionSecurityIdentifier:
return "CollectionSecurityIdentifier"
case RntbdResponseHeaderTransportRequestID:
return "TransportRequestID"
case RntbdResponseHeaderShareThroughput:
return "ShareThroughput"
case RntbdResponseHeaderDisableRntbdChannel:
return "DisableRntbdChannel"
case RntbdResponseHeaderServerDateTimeUtc:
return "ServerDateTimeUtc"
case RntbdResponseHeaderLocalLSN:
return "LocalLSN"
case RntbdResponseHeaderQuorumAckedLocalLSN:
return "QuorumAckedLocalLSN"
case RntbdResponseHeaderItemLocalLSN:
return "ItemLocalLSN"
case RntbdResponseHeaderHasTentativeWrites:
return "HasTentativeWrites"
case RntbdResponseHeaderSessionToken:
return "SessionToken"
}
return fmt.Sprintf("RntbdResponseHeaderType(%d)", h)
}
func (r RntbdResourceType) String() string {
switch r {
case RntbdResourceTypeConnection:
return "Connection"
case RntbdResourceTypeDatabase:
return "Database"
case RntbdResourceTypeCollection:
return "Collection"
case RntbdResourceTypeDocument:
return "Document"
case RntbdResourceTypeAttachment:
return "Attachment"
case RntbdResourceTypeUser:
return "User"
case RntbdResourceTypePermission:
return "Permission"
case RntbdResourceTypeStoredProcedure:
return "StoredProcedure"
case RntbdResourceTypeConflict:
return "Conflict"
case RntbdResourceTypeTrigger:
return "Trigger"
case RntbdResourceTypeUserDefinedFunction:
return "UserDefinedFunction"
case RntbdResourceTypeModule:
return "Module"
case RntbdResourceTypeReplica:
return "Replica"
case RntbdResourceTypeModuleCommand:
return "ModuleCommand"
case RntbdResourceTypeRecord:
return "Record"
case RntbdResourceTypeOffer:
return "Offer"
case RntbdResourceTypePartitionSetInformation:
return "PartitionSetInformation"
case RntbdResourceTypeXPReplicatorAddress:
return "XPReplicatorAddress"
case RntbdResourceTypeMasterPartition:
return "MasterPartition"
case RntbdResourceTypeServerPartition:
return "ServerPartition"
case RntbdResourceTypeDatabaseAccount:
return "DatabaseAccount"
case RntbdResourceTypeTopology:
return "Topology"
case RntbdResourceTypePartitionKeyRange:
return "PartitionKeyRange"
case RntbdResourceTypeSchema:
return "Schema"
case RntbdResourceTypeBatchApply:
return "BatchApply"
case RntbdResourceTypeRestoreMetadata:
return "RestoreMetadata"
case RntbdResourceTypeComputeGatewayCharges:
return "ComputeGatewayCharges"
case RntbdResourceTypeRidRange:
return "RidRange"
case RntbdResourceTypeUserDefinedType:
return "UserDefinedType"
}
return fmt.Sprintf("RntbdResourceType(%d)", r)
}
func (o RntbdOperationType) String() string {
switch o {
case RntbdOperationTypeConnection:
return "Connection"
case RntbdOperationTypeCreate:
return "Create"
case RntbdOperationTypeUpdate:
return "Update"
case RntbdOperationTypeRead:
return "Read"
case RntbdOperationTypeReadFeed:
return "ReadFeed"
case RntbdOperationTypeDelete:
return "Delete"
case RntbdOperationTypeReplace:
return "Replace"
case RntbdOperationTypeExecuteJavaScript:
return "ExecuteJavaScript"
case RntbdOperationTypeSQLQuery:
return "SQLQuery"
case RntbdOperationTypePause:
return "Pause"
case RntbdOperationTypeResume:
return "Resume"
case RntbdOperationTypeStop:
return "Stop"
case RntbdOperationTypeRecycle:
return "Recycle"
case RntbdOperationTypeCrash:
return "Crash"
case RntbdOperationTypeQuery:
return "Query"
case RntbdOperationTypeForceConfigRefresh:
return "ForceConfigRefresh"
case RntbdOperationTypeHead:
return "Head"
case RntbdOperationTypeHeadFeed:
return "HeadFeed"
case RntbdOperationTypeUpsert:
return "Upsert"
case RntbdOperationTypeRecreate:
return "Recreate"
case RntbdOperationTypeThrottle:
return "Throttle"
case RntbdOperationTypeGetSplitPoint:
return "GetSplitPoint"
case RntbdOperationTypePreCreateValidation:
return "PreCreateValidation"
case RntbdOperationTypeBatchApply:
return "BatchApply"
case RntbdOperationTypeAbortSplit:
return "AbortSplit"
case RntbdOperationTypeCompleteSplit:
return "CompleteSplit"
case RntbdOperationTypeOfferUpdateOperation:
return "OfferUpdateOperation"
case RntbdOperationTypeOfferPreGrowValidation:
return "OfferPreGrowValidation"
case RntbdOperationTypeBatchReportThroughputUtilization:
return "BatchReportThroughputUtilization"
case RntbdOperationTypeCompletePartitionMigration:
return "CompletePartitionMigration"
case RntbdOperationTypeAbortPartitionMigration:
return "AbortPartitionMigration"
case RntbdOperationTypePreReplaceValidation:
return "PreReplaceValidation"
case RntbdOperationTypeAddComputeGatewayRequestCharges:
return "AddComputeGatewayRequestCharges"
case RntbdOperationTypeMigratePartition:
return "MigratePartition"
}
return fmt.Sprintf("RntbdOperationType(%d)", o)
}
+128
View File
@@ -0,0 +1,128 @@
package rntbd
import (
"bytes"
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/url"
"strconv"
"github.com/pikami/cosmium/api/headers"
)
func (f *RntbdFrame) ToHttpRequest() *http.Request {
req := &http.Request{
Method: operationTypeToHttpMethod(f.OperationType),
URL: &url.URL{Path: frameToPath(f)},
Body: io.NopCloser(bytes.NewReader(f.Payload)),
Header: http.Header{},
}
switch f.OperationType {
case RntbdOperationTypeQuery, RntbdOperationTypeSQLQuery:
req.Header.Set(headers.Query, "true")
case RntbdOperationTypeUpsert:
req.Header.Set(headers.IsUpsert, "true")
}
if ifMatch, ok := f.RequestHeaders[RntbdRequestHeaderMatch]; ok {
if ifMatchString, ok := ifMatch.(string); ok {
req.Header.Set(headers.IfMatch, ifMatchString)
}
}
if continuationToken, ok := f.RequestHeaders[RntbdRequestHeaderContinuationToken]; ok {
if continuationTokenString, ok := continuationToken.(string); ok {
req.Header.Set(headers.ContinuationToken, continuationTokenString)
}
}
if maxItemCount, ok := f.RequestHeaders[RntbdRequestHeaderPageSize]; ok {
if maxItemCountString, ok := maxItemCount.(uint64); ok {
req.Header.Set(headers.MaxItemCount, fmt.Sprintf("%d", maxItemCountString))
}
}
return req
}
func ToRntbdResponseFrame(responseWriter *httptest.ResponseRecorder) *RntbdResponseFrameBuilder {
builder := &RntbdResponseFrameBuilder{}
builder.SetStatusCode(uint16(responseWriter.Code))
if responseWriter.Header().Get(headers.ETag) != "" {
builder.AddHeader(uint16(RntbdResponseHeaderETag), RntbdTokenTypeString, responseWriter.Header().Get(headers.ETag))
}
if responseWriter.Header().Get(headers.ContinuationToken) != "" {
builder.AddHeader(uint16(RntbdResponseHeaderContinuationToken), RntbdTokenTypeString, responseWriter.Header().Get(headers.ContinuationToken))
}
if responseWriter.Header().Get(headers.ItemCount) != "" {
itemCount, err := strconv.ParseUint(responseWriter.Header().Get(headers.ItemCount), 10, 32)
if err != nil {
panic(err)
}
builder.AddHeader(uint16(RntbdResponseHeaderItemCount), RntbdTokenTypeULong, uint32(itemCount))
}
if responseWriter.Body.Len() > 0 {
builder.AddHeader(uint16(RntbdResponseHeaderPayloadPresent), RntbdTokenTypeByte, []byte{1})
builder.AddPayload(responseWriter.Body.Bytes())
} else {
builder.AddHeader(uint16(RntbdResponseHeaderPayloadPresent), RntbdTokenTypeByte, []byte{0})
}
return builder
}
func operationTypeToHttpMethod(operationType RntbdOperationType) string {
switch operationType {
case RntbdOperationTypeRead,
RntbdOperationTypeReadFeed:
return http.MethodGet
case RntbdOperationTypeCreate,
RntbdOperationTypeUpsert,
RntbdOperationTypeQuery,
RntbdOperationTypeSQLQuery:
return http.MethodPost
case RntbdOperationTypeUpdate,
RntbdOperationTypeReplace:
return http.MethodPut
case RntbdOperationTypeDelete:
return http.MethodDelete
}
panic(fmt.Sprintf("Unknown operation type: %d", operationType))
}
func frameToPath(frame *RntbdFrame) string {
databaseName, databaseOk := frame.RequestHeaders[RntbdRequestHeaderDatabaseName]
collectionName, collectionOk := frame.RequestHeaders[RntbdRequestHeaderCollectionName]
documentName, documentOk := frame.RequestHeaders[RntbdRequestHeaderDocumentName]
urlPath := ""
if databaseOk {
urlPath += fmt.Sprintf("/dbs/%s", databaseName)
} else if frame.ResourceType == RntbdResourceTypeDatabase {
urlPath += "/dbs"
}
if collectionOk {
urlPath += fmt.Sprintf("/colls/%s", collectionName)
} else if frame.ResourceType == RntbdResourceTypeCollection {
urlPath += "/colls"
}
if documentOk {
urlPath += fmt.Sprintf("/docs/%s", documentName)
} else if frame.ResourceType == RntbdResourceTypeDocument {
urlPath += "/docs"
}
return urlPath
}
+212
View File
@@ -0,0 +1,212 @@
package rntbd
import (
"bufio"
"bytes"
"encoding/binary"
"encoding/hex"
"fmt"
"io"
"os"
"github.com/pikami/cosmium/internal/logger"
)
type RntbdFrame struct {
ResourceType RntbdResourceType
OperationType RntbdOperationType
ActivityId []byte
RequestHeaders map[RntbdRequestHeader]any
ResponseHeaders map[RntbdResponseHeaderType]any
ContextHeaders map[RntbdContextHeader]any
Payload []byte
}
func ReadFrame(reader *bufio.Reader) (*RntbdFrame, error) {
sizeBytes := readBytes(reader, 4)
size := binary.LittleEndian.Uint32(sizeBytes)
payload := readBytes(reader, int(size)-4)
frame, err := parseFrame_Int(payload, false)
if err != nil {
return nil, err
}
if payloadPresent, ok := frame.RequestHeaders[RntbdRequestHeaderPayloadPresent]; ok && payloadPresent.([]byte)[0] == 1 {
payloadSize := binary.LittleEndian.Uint32(readBytes(reader, 4))
payload := readBytes(reader, int(payloadSize))
frame.Payload = payload
}
if payloadPresent, ok := frame.ResponseHeaders[RntbdResponseHeaderPayloadPresent]; ok && payloadPresent.([]byte)[0] == 1 {
payloadSize := binary.LittleEndian.Uint32(readBytes(reader, 4))
payload := readBytes(reader, int(payloadSize))
frame.Payload = payload
}
return frame, nil
}
func ParseFrame(data []byte, isResponse bool) (*RntbdFrame, error) {
if len(data) < 4 {
return nil, fmt.Errorf("data too short")
}
reader := bufio.NewReader(bytes.NewReader(data))
sizeBytes := readBytes(reader, 4)
size := binary.LittleEndian.Uint32(sizeBytes)
payload := readBytes(reader, int(size)-4)
frame, err := parseFrame_Int(payload, isResponse)
if err != nil {
return nil, err
}
if payloadPresent, ok := frame.RequestHeaders[RntbdRequestHeaderPayloadPresent]; ok && payloadPresent.([]byte)[0] == 1 {
payloadSize := binary.LittleEndian.Uint32(readBytes(reader, 4))
payload := readBytes(reader, int(payloadSize))
frame.Payload = payload
}
if payloadPresent, ok := frame.ResponseHeaders[RntbdResponseHeaderPayloadPresent]; ok && payloadPresent.([]byte)[0] == 1 {
payloadSize := binary.LittleEndian.Uint32(readBytes(reader, 4))
payload := readBytes(reader, int(payloadSize))
frame.Payload = payload
}
leftOverBytes, err := io.ReadAll(reader)
if err != nil {
logger.ErrorLn("Error reading leftOverBytes:", err)
}
if len(leftOverBytes) > 0 {
logger.ErrorLn("Left over bytes:", hex.EncodeToString(leftOverBytes))
}
return frame, nil
}
func parseFrame_Int(data []byte, isResponse bool) (*RntbdFrame, error) {
payloadReader := bufio.NewReader(bytes.NewReader(data))
resourceTypeBytes := readBytes(payloadReader, 2)
resourceType := binary.LittleEndian.Uint16(resourceTypeBytes)
operationTypeBytes := readBytes(payloadReader, 2)
operationType := RntbdOperationType(binary.LittleEndian.Uint16(operationTypeBytes))
activityIdBytes := readBytes(payloadReader, 16)
requestHeaders := make(map[RntbdRequestHeader]any)
responseHeaders := make(map[RntbdResponseHeaderType]any)
contextHeaders := make(map[RntbdContextHeader]any)
for {
if _, err := payloadReader.Peek(1); err != nil {
break
}
headerIdBytes := readBytes(payloadReader, 2)
headerId := binary.LittleEndian.Uint16(headerIdBytes)
token, err := parseRntbdToken(payloadReader)
if err != nil {
return nil, err
}
if resourceType == uint16(RntbdResourceTypeConnection) {
contextHeaders[RntbdContextHeader(headerId)] = token
} else if isResponse {
responseHeaders[RntbdResponseHeaderType(headerId)] = token
} else {
requestHeaders[RntbdRequestHeader(headerId)] = token
}
}
return &RntbdFrame{
ResourceType: RntbdResourceType(resourceType),
OperationType: RntbdOperationType(operationType),
ActivityId: activityIdBytes,
RequestHeaders: requestHeaders,
ResponseHeaders: responseHeaders,
ContextHeaders: contextHeaders,
}, nil
}
func parseRntbdToken(reader *bufio.Reader) (any, error) {
tokenTypeBytes := readBytes(reader, 1)
tokenType := RntbdTokenType(tokenTypeBytes[0])
switch tokenType {
case RntbdTokenTypeByte:
token := readBytes(reader, 1)
return token, nil
case RntbdTokenTypeUShort:
token := binary.LittleEndian.Uint16(readBytes(reader, 2))
return token, nil
case RntbdTokenTypeULong:
token := binary.LittleEndian.Uint32(readBytes(reader, 4))
return token, nil
case RntbdTokenTypeLong:
token := int32(binary.LittleEndian.Uint32(readBytes(reader, 4)))
return token, nil
case RntbdTokenTypeULongLong:
token := binary.LittleEndian.Uint64(readBytes(reader, 8))
return token, nil
case RntbdTokenTypeLongLong:
token := int64(binary.LittleEndian.Uint64(readBytes(reader, 8)))
return token, nil
case RntbdTokenTypeGuid:
token := readBytes(reader, 16)
return token, nil
case RntbdTokenTypeSmallString:
lengthBytes := readBytes(reader, 1)
length := uint8(lengthBytes[0])
token := readBytes(reader, int(length))
return string(token), nil
case RntbdTokenTypeString:
length := binary.LittleEndian.Uint16(readBytes(reader, 2))
token := readBytes(reader, int(length))
return string(token), nil
case RntbdTokenTypeULongString:
length := binary.LittleEndian.Uint32(readBytes(reader, 4))
token := readBytes(reader, int(length))
return string(token), nil
case RntbdTokenTypeSmallBytes:
lengthBytes := readBytes(reader, 1)
length := uint8(lengthBytes[0])
token := readBytes(reader, int(length))
return token, nil
case RntbdTokenTypeBytes:
length := binary.LittleEndian.Uint16(readBytes(reader, 2))
token := readBytes(reader, int(length))
return token, nil
case RntbdTokenTypeULongBytes:
length := binary.LittleEndian.Uint32(readBytes(reader, 4))
token := readBytes(reader, int(length))
return token, nil
case RntbdTokenTypeFloat:
// I can't be bothered to implement this, let's just return a byte array
token := readBytes(reader, 4)
return token, nil
case RntbdTokenTypeDouble:
// I can't be bothered to implement this, let's just return a byte array
token := readBytes(reader, 8)
return token, nil
case RntbdTokenTypeInvalid:
return nil, fmt.Errorf("invalid token type")
}
return nil, fmt.Errorf("invalid token type")
}
func readBytes(reader *bufio.Reader, n int) []byte {
bytes := make([]byte, n)
_, err := io.ReadFull(reader, bytes)
if err != nil {
logger.ErrorLn("Error reading bytes:", err)
os.Exit(0)
}
return bytes
}
+120
View File
@@ -0,0 +1,120 @@
package rntbd
import (
"bufio"
"crypto/tls"
"fmt"
"net"
"net/http/httptest"
"github.com/pikami/cosmium/api"
"github.com/pikami/cosmium/internal/logger"
tlsprovider "github.com/pikami/cosmium/internal/tls_provider"
)
type RntbdServer struct {
port int
listener net.Listener
apiServer *api.ApiServer
}
func NewRntbdServer(port int, apiServer *api.ApiServer) *RntbdServer {
return &RntbdServer{port: port, apiServer: apiServer}
}
func (s *RntbdServer) Start() error {
tlsConfig := tlsprovider.GetDefaultTlsConfig()
listener, err := tls.Listen("tcp", fmt.Sprintf(":%d", s.port), tlsConfig)
if err != nil {
return fmt.Errorf("failed to listen on port %d: %w", s.port, err)
}
s.listener = listener
go func() {
for {
conn, err := s.listener.Accept()
if err != nil {
logger.ErrorLn("Failed to accept connection:", err)
continue
}
go s.handleConnection(conn)
}
}()
return nil
}
func (s *RntbdServer) Stop() error {
return s.listener.Close()
}
func (s *RntbdServer) handleConnection(conn net.Conn) {
defer conn.Close()
reader := bufio.NewReader(conn)
writer := bufio.NewWriter(conn)
for {
_, err := reader.Peek(4)
if err != nil {
return
}
frame, err := ReadFrame(reader)
if err != nil {
logger.ErrorLn("Failed to read frame:", err)
continue
}
if frame.ResourceType == RntbdResourceTypeConnection {
responseFrame := buildContextFrame(frame)
_, err := writer.Write(responseFrame)
writer.Flush()
if err != nil {
logger.ErrorLn("Failed to write response frame:", err)
continue
}
continue
} else if frame.ResourceType == RntbdResourceTypeDatabase ||
frame.ResourceType == RntbdResourceTypeCollection ||
frame.ResourceType == RntbdResourceTypeDocument {
responseFrameBytes := s.passToApiServer(frame)
_, err := writer.Write(responseFrameBytes)
writer.Flush()
if err != nil {
logger.ErrorLn("Failed to write response frame:", err)
continue
}
continue
} else {
logger.Errorf("Received Unhandled RNTBD request from: %s with resource type: %s\n", conn.RemoteAddr(), frame.ResourceType.String())
}
}
}
func (s *RntbdServer) passToApiServer(frame *RntbdFrame) []byte {
req := frame.ToHttpRequest()
responseWriter := httptest.NewRecorder()
s.apiServer.GetRouter().ServeHTTP(responseWriter, req)
responseFrameBuilder := ToRntbdResponseFrame(responseWriter)
responseFrameBuilder.SetActivityId(frame.ActivityId)
if transportRequestId, ok := frame.RequestHeaders[RntbdRequestHeaderTransportRequestID]; ok {
responseFrameBuilder.AddHeader(uint16(RntbdResponseHeaderTransportRequestID), RntbdTokenTypeULong, transportRequestId)
}
responseFrameBuilder.AddHeader(uint16(RntbdResponseHeaderItemLSN), RntbdTokenTypeLongLong, int64(420))
responseFrameBuilder.AddHeader(uint16(RntbdResponseHeaderLocalLSN), RntbdTokenTypeLongLong, int64(420))
responseFrameBuilder.AddHeader(uint16(RntbdResponseHeaderGlobalCommittedLSN), RntbdTokenTypeLongLong, int64(420))
responseFrameBuilder.AddHeader(uint16(RntbdResponseHeaderItemLocalLSN), RntbdTokenTypeLongLong, int64(420))
responseFrameBuilder.AddHeader(uint16(RntbdResponseHeaderLSN), RntbdTokenTypeLongLong, int64(420))
responseFrameBuilder.AddHeader(uint16(RntbdResponseHeaderQuorumAckedLSN), RntbdTokenTypeLongLong, int64(420))
responseFrameBuilder.AddHeader(uint16(RntbdResponseHeaderQuorumAckedLocalLSN), RntbdTokenTypeLongLong, int64(420))
responseFrameBuilder.AddHeader(uint16(RntbdResponseHeaderCurrentReplicaSetSize), RntbdTokenTypeULong, uint32(1))
responseFrame := responseFrameBuilder.Build()
responseFrameBytes := responseFrame.ToBytes()
return responseFrameBytes
}
@@ -1,26 +1,24 @@
package structhidrators package structhidrators
import ( import "github.com/pikami/cosmium/internal/datastore"
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
)
var defaultCollection repositorymodels.Collection = repositorymodels.Collection{ var defaultCollection datastore.Collection = datastore.Collection{
IndexingPolicy: repositorymodels.CollectionIndexingPolicy{ IndexingPolicy: datastore.CollectionIndexingPolicy{
IndexingMode: "consistent", IndexingMode: "consistent",
Automatic: true, Automatic: true,
IncludedPaths: []repositorymodels.CollectionIndexingPolicyPath{ IncludedPaths: []datastore.CollectionIndexingPolicyPath{
{Path: "/*"}, {Path: "/*"},
}, },
ExcludedPaths: []repositorymodels.CollectionIndexingPolicyPath{ ExcludedPaths: []datastore.CollectionIndexingPolicyPath{
{Path: "/\"_etag\"/?"}, {Path: "/\"_etag\"/?"},
}, },
}, },
PartitionKey: repositorymodels.CollectionPartitionKey{ PartitionKey: datastore.CollectionPartitionKey{
Paths: []string{"/_partitionKey"}, Paths: []string{"/_partitionKey"},
Kind: "Hash", Kind: "Hash",
Version: 2, Version: 2,
}, },
UniqueID: "nFFFFFFFFFF=", ResourceID: "nFFFFFFFFFF=",
TimeStamp: 0, TimeStamp: 0,
Self: "", Self: "",
ETag: "\"00000000-0000-0000-0000-000000000000\"", ETag: "\"00000000-0000-0000-0000-000000000000\"",
+2 -2
View File
@@ -3,11 +3,11 @@ package structhidrators
import ( import (
"reflect" "reflect"
repositorymodels "github.com/pikami/cosmium/internal/repository_models" "github.com/pikami/cosmium/internal/datastore"
) )
func Hidrate(input interface{}) interface{} { func Hidrate(input interface{}) interface{} {
if reflect.TypeOf(input) == reflect.TypeOf(repositorymodels.Collection{}) { if reflect.TypeOf(input) == reflect.TypeOf(datastore.Collection{}) {
return hidrate(input, defaultCollection) return hidrate(input, defaultCollection)
} }
return input return input
+61
View File
@@ -0,0 +1,61 @@
package tlsprovider
const certificate = `
-----BEGIN CERTIFICATE-----
MIIEaDCCAlCgAwIBAgIUAY7ito1IQfbIi52C0evhqHWgEvQwDQYJKoZIhvcNAQEL
BQAwMzELMAkGA1UEBhMCTFQxEjAQBgNVBAgMCUxpdGh1YW5pYTEQMA4GA1UECgwH
Q29zbWl1bTAeFw0yNDAyMjcxOTE4NThaFw0zNDAyMjYxOTE4NThaMD8xCzAJBgNV
BAYTAkxUMRIwEAYDVQQIDAlMaXRodWFuaWExEDAOBgNVBAoMB0Nvc21pdW0xCjAI
BgNVBAMMASowggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCZxGz5clcf
fvE6wS9Q2xPsUjeKdwotRCfKRu9kT7o1cZOSRBp7DgdeLvZ7BzqU1tk5wiLLiZwB
gI6amQAd6z6EwUcUH0mHtFiWU0y/FROz0QUojbbYp0PMUhWjlPAxAGaiwgF/82z7
/lmgMjf5v32XsMfa4U+FaaNYs7gu7aCQBQTAHmOIPnEAeFk9xQ2VzntRUWwzDYOV
SimtPZk2O2X18V8KTgTLMQF1KErIyznIwEPB/BLi+ihLkh/8BaaxoIeOPIhRLNFr
ecZrc/8+S4dUSUQDfmV3JFYFFheG0XIPEwXIaXiDAphpkCGhMIC2pDL8r14sntvn
juHFZxmSP4V5AgMBAAGjaDBmMB8GA1UdIwQYMBaAFEbQ/7hV4FWrptdOk540R2lF
SB1BMAkGA1UdEwQCMAAwCwYDVR0PBAQDAgTwMAwGA1UdEQQFMAOCASowHQYDVR0O
BBYEFGv5XvoFFzrG54GQ+WMFm6UO36BJMA0GCSqGSIb3DQEBCwUAA4ICAQBZh/vZ
PBamebTEpiQz6cgf8+GcTi++ebYUGQ3YJj82pqVBdipOhYQOZJ0fOlT1qRGNglut
+m5zn0iuXsNucP/32xdf1aJBnsU/aGrlf5ohJpGNxYfNPsewxeqQI23Yj22ec1gy
WL2pFDYNyTZMM7Wgys7m3i9lb6TYOF2lNO3WbNuuuETsDAPa0rD0R8QsQOfYOSNJ
YuWE4qZu+ySvTWsMZwlcqs7QL3Sd91UjItIS/AgqbnLvgt4z5ckGCIvickUfAZuQ
6x592hTz4OZ+WIYDejtb5MMXRaKEXgfF6o1idrD7YgVutm+2+mYpN1v9aLbCs7QW
9RkJoTXFQRNGq6j/cO0ZrCKFkttduziMWRz5X9QWADME1NsL53DfDkaxp9Nh+CCu
0S9OF9nVLJVigdXe4O1cQ0Qh633O6k+F/xWYcmMyVt3V2bs7FPfygGUx60tfIbpi
cBK3BsuzUrId3ozvYPsmfxYlzmyspyS6G+f7zLFOakm3fuqDJpnFNXmRY2Ljd3Cp
punuMT6zSctHAxpgJm1g9R6PcaGr+b/n6zkbxyK9+SFzwN3Lb18WFj5OcslNM/g5
ERE5Ws+Vae6MleSmsxSytgH4qn0ormPWuouBLaW0Rv2ZHdkt3myq8kTqtqdw3LRR
ogcLQ3cL6I5FKGjm2TOF72DQHvOol8ck0uMz/w==
-----END CERTIFICATE-----
`
const certificateKey = `
-----BEGIN PRIVATE KEY-----
MIIEuwIBADANBgkqhkiG9w0BAQEFAASCBKUwggShAgEAAoIBAQCZxGz5clcffvE6
wS9Q2xPsUjeKdwotRCfKRu9kT7o1cZOSRBp7DgdeLvZ7BzqU1tk5wiLLiZwBgI6a
mQAd6z6EwUcUH0mHtFiWU0y/FROz0QUojbbYp0PMUhWjlPAxAGaiwgF/82z7/lmg
Mjf5v32XsMfa4U+FaaNYs7gu7aCQBQTAHmOIPnEAeFk9xQ2VzntRUWwzDYOVSimt
PZk2O2X18V8KTgTLMQF1KErIyznIwEPB/BLi+ihLkh/8BaaxoIeOPIhRLNFrecZr
c/8+S4dUSUQDfmV3JFYFFheG0XIPEwXIaXiDAphpkCGhMIC2pDL8r14sntvnjuHF
ZxmSP4V5AgMBAAECgf89wcgjpZnzoWoiM3Z6QDJnkiUdXQumHQracBnRFXnMy8p9
wCd4ecnu9ptd8OArXgVMiaILWZeGXlqtW872m6Lej6DrJkpOt3NG9CvscdaHdthW
9dzv8d7IEtuRN4/WWOm7Tke7eD7763ta9i9/niR2q7DazPVw8vYhkyoNe864qVrq
Vw6+MMetz3TDHZ68p17yJJ9FJ0z0vHj3KJFrxnJonMe+/LcQX490y4zZw+zeyCkh
y/bsgvFGhnUhJ+mOz+qv0KL7HyUR69p9/+mjQH+AQH+j24xgd1IL0Dror9Cy1kxY
uKmi8pN1y288GmjkWosGMb0p3Pse1OkOyYFIbxECgYEA2ED3PSPoHWLHfKhg2BFw
yMPtern06rjKuwMNlD+mKS66Z+OsQi2EBsqomGnr1HGvYgQik0jwMcx0+Sup9/Zp
az8ebH6S4Tdxmnlwn34lhTIAF1KJS19AYvbhOydV+M+hq7Y7QxTqYsJAgEYwsozQ
0XeAzRBIiRxdcMFHP40zZIkCgYEAtgdiwo5d5iyvXEqx/5+NdM4b/ImrbaFIAb0v
MqiPpOA/+7EKlx72gJKVKh2iv4jvEUfduNEUXt77Yqo66HhfiTBVYxYwThK8E0Mq
TSKKdJsdPSThLS3qjeARpzQpWLiBZH90GxbfFL3ogIOa/UcgwRrqPc5a/yq8adSs
KGrfvXECgYEAmSMAMbqgn1aY32y5D6jiDjm4jMTsa98qKN5TmlysRNODSxhNnptu
uASA+VVgnBNZV/aHqXboKMuZNe22shI7uqd62ueTCYtiljpTB46j8TtkFx/qe4Zb
KPmcq3ACkGwwF1G3i5xfEkputKd/yqCvKvYOLqjORNHiVXt5Acby0skCgYBYkZ9s
KvllVbi9n1qclnWtr9vONO5EmYT/051zeLDr+HEpditA/L/UL36Ez4awy2AHeIBZ
vOG8h6Kpj0q6cleJ2Qqy+8jlNBhvBu8+OOBFfHPtnFQ0N3M5NR1hze+QS7YpwBou
VCKXZRAL9/0h38oAK6huCkocfh7PH7vkrpvPAQKBgCFDDtk7aBJsNcOW+aq4IEvf
nZ5hhhdelNLeN29RrJ71GwJrCG3NbhopWlCDqZ/Dd6QoEUpebqvlMGvQJBuz/QKb
ilcZlmaCS9pqIXAFK9GQ89V/xa8OibOuJUiBgShnfSQqAwQrfX1vYjtKErnjoRFs
9+zaWugLCC47Hw6QlMDa
-----END PRIVATE KEY-----
`
+19
View File
@@ -0,0 +1,19 @@
package tlsprovider
import (
"crypto/tls"
"github.com/pikami/cosmium/internal/logger"
)
func GetDefaultTlsConfig() *tls.Config {
cert, err := tls.X509KeyPair([]byte(certificate), []byte(certificateKey))
if err != nil {
logger.ErrorLn("Failed to parse certificate and key:", err)
return &tls.Config{}
}
return &tls.Config{
Certificates: []tls.Certificate{cert},
}
}
-45
View File
@@ -1,45 +0,0 @@
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"github.com/pikami/cosmium/api"
"github.com/pikami/cosmium/api/config"
"github.com/pikami/cosmium/internal/repositories"
)
func main() {
config.ParseFlags()
if config.Config.InitialDataFilePath != "" {
repositories.LoadStateFS(config.Config.InitialDataFilePath)
}
router := api.CreateRouter()
if config.Config.TLS_CertificatePath == "" ||
config.Config.TLS_CertificateKey == "" {
go router.Run(fmt.Sprintf(":%d", config.Config.Port))
} else {
go router.RunTLS(
fmt.Sprintf(":%d", config.Config.Port),
config.Config.TLS_CertificatePath,
config.Config.TLS_CertificateKey)
}
waitForExit()
}
func waitForExit() {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
// Block until a exit signal is received
<-sigs
if config.Config.PersistDataFilePath != "" {
repositories.SaveStateFS(config.Config.PersistDataFilePath)
}
}
+79
View File
@@ -3,14 +3,26 @@ package parsers
type SelectStmt struct { type SelectStmt struct {
SelectItems []SelectItem SelectItems []SelectItem
Table Table Table Table
JoinItems []JoinItem
Filters interface{} Filters interface{}
Exists bool
Distinct bool
Count int Count int
Offset int
Parameters map[string]interface{} Parameters map[string]interface{}
OrderExpressions []OrderExpression OrderExpressions []OrderExpression
GroupBy []SelectItem
} }
type Table struct { type Table struct {
Value string Value string
SelectItem SelectItem
IsInSelect bool
}
type JoinItem struct {
Table Table
SelectItem SelectItem
} }
type SelectItemType int type SelectItemType int
@@ -21,6 +33,9 @@ const (
SelectItemTypeArray SelectItemTypeArray
SelectItemTypeConstant SelectItemTypeConstant
SelectItemTypeFunctionCall SelectItemTypeFunctionCall
SelectItemTypeSubQuery
SelectItemTypeExpression
SelectItemTypeBinaryExpression
) )
type SelectItem struct { type SelectItem struct {
@@ -29,6 +44,7 @@ type SelectItem struct {
SelectItems []SelectItem SelectItems []SelectItem
Type SelectItemType Type SelectItemType
Value interface{} Value interface{}
Invert bool
IsTopLevel bool IsTopLevel bool
} }
@@ -50,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 (
@@ -85,6 +107,7 @@ const (
FunctionCallContains FunctionCallType = "Contains" FunctionCallContains FunctionCallType = "Contains"
FunctionCallEndsWith FunctionCallType = "EndsWith" FunctionCallEndsWith FunctionCallType = "EndsWith"
FunctionCallStartsWith FunctionCallType = "StartsWith" FunctionCallStartsWith FunctionCallType = "StartsWith"
FunctionCallRegexMatch FunctionCallType = "RegexMatch"
FunctionCallIndexOf FunctionCallType = "IndexOf" FunctionCallIndexOf FunctionCallType = "IndexOf"
FunctionCallToString FunctionCallType = "ToString" FunctionCallToString FunctionCallType = "ToString"
FunctionCallUpper FunctionCallType = "Upper" FunctionCallUpper FunctionCallType = "Upper"
@@ -112,14 +135,70 @@ const (
FunctionCallIsString FunctionCallType = "IsString" FunctionCallIsString FunctionCallType = "IsString"
FunctionCallArrayConcat FunctionCallType = "ArrayConcat" FunctionCallArrayConcat FunctionCallType = "ArrayConcat"
FunctionCallArrayContains FunctionCallType = "ArrayContains"
FunctionCallArrayContainsAny FunctionCallType = "ArrayContainsAny"
FunctionCallArrayContainsAll FunctionCallType = "ArrayContainsAll"
FunctionCallArrayLength FunctionCallType = "ArrayLength" FunctionCallArrayLength FunctionCallType = "ArrayLength"
FunctionCallArraySlice FunctionCallType = "ArraySlice" FunctionCallArraySlice FunctionCallType = "ArraySlice"
FunctionCallSetIntersect FunctionCallType = "SetIntersect" FunctionCallSetIntersect FunctionCallType = "SetIntersect"
FunctionCallSetUnion FunctionCallType = "SetUnion" FunctionCallSetUnion FunctionCallType = "SetUnion"
FunctionCallIif FunctionCallType = "Iif"
FunctionCallMathAbs FunctionCallType = "MathAbs"
FunctionCallMathAcos FunctionCallType = "MathAcos"
FunctionCallMathAsin FunctionCallType = "MathAsin"
FunctionCallMathAtan FunctionCallType = "MathAtan"
FunctionCallMathAtn2 FunctionCallType = "MathAtn2"
FunctionCallMathCeiling FunctionCallType = "MathCeiling"
FunctionCallMathCos FunctionCallType = "MathCos"
FunctionCallMathCot FunctionCallType = "MathCot"
FunctionCallMathDegrees FunctionCallType = "MathDegrees"
FunctionCallMathExp FunctionCallType = "MathExp"
FunctionCallMathFloor FunctionCallType = "MathFloor"
FunctionCallMathIntAdd FunctionCallType = "MathIntAdd"
FunctionCallMathIntBitAnd FunctionCallType = "MathIntBitAnd"
FunctionCallMathIntBitLeftShift FunctionCallType = "MathIntBitLeftShift"
FunctionCallMathIntBitNot FunctionCallType = "MathIntBitNot"
FunctionCallMathIntBitOr FunctionCallType = "MathIntBitOr"
FunctionCallMathIntBitRightShift FunctionCallType = "MathIntBitRightShift"
FunctionCallMathIntBitXor FunctionCallType = "MathIntBitXor"
FunctionCallMathIntDiv FunctionCallType = "MathIntDiv"
FunctionCallMathIntMod FunctionCallType = "MathIntMod"
FunctionCallMathIntMul FunctionCallType = "MathIntMul"
FunctionCallMathIntSub FunctionCallType = "MathIntSub"
FunctionCallMathLog FunctionCallType = "MathLog"
FunctionCallMathLog10 FunctionCallType = "MathLog10"
FunctionCallMathNumberBin FunctionCallType = "MathNumberBin"
FunctionCallMathPi FunctionCallType = "MathPi"
FunctionCallMathPower FunctionCallType = "MathPower"
FunctionCallMathRadians FunctionCallType = "MathRadians"
FunctionCallMathRand FunctionCallType = "MathRand"
FunctionCallMathRound FunctionCallType = "MathRound"
FunctionCallMathSign FunctionCallType = "MathSign"
FunctionCallMathSin FunctionCallType = "MathSin"
FunctionCallMathSqrt FunctionCallType = "MathSqrt"
FunctionCallMathSquare FunctionCallType = "MathSquare"
FunctionCallMathTan FunctionCallType = "MathTan"
FunctionCallMathTrunc FunctionCallType = "MathTrunc"
FunctionCallAggregateAvg FunctionCallType = "AggregateAvg"
FunctionCallAggregateCount FunctionCallType = "AggregateCount"
FunctionCallAggregateMax FunctionCallType = "AggregateMax"
FunctionCallAggregateMin FunctionCallType = "AggregateMin"
FunctionCallAggregateSum FunctionCallType = "AggregateSum"
FunctionCallIn FunctionCallType = "In" FunctionCallIn FunctionCallType = "In"
) )
var AggregateFunctions = []FunctionCallType{
FunctionCallAggregateAvg,
FunctionCallAggregateCount,
FunctionCallAggregateMax,
FunctionCallAggregateMin,
FunctionCallAggregateSum,
}
type FunctionCall struct { type FunctionCall struct {
Arguments []interface{} Arguments []interface{}
Type FunctionCallType Type FunctionCallType
+131
View File
@@ -0,0 +1,131 @@
package nosql_test
import (
"testing"
"github.com/pikami/cosmium/parsers"
testutils "github.com/pikami/cosmium/test_utils"
)
func Test_Parse_AggregateFunctions(t *testing.T) {
t.Run("Should parse function AVG()", func(t *testing.T) {
testQueryParse(
t,
`SELECT AVG(c.a1) FROM c`,
parsers.SelectStmt{
SelectItems: []parsers.SelectItem{
{
Type: parsers.SelectItemTypeFunctionCall,
Value: parsers.FunctionCall{
Type: parsers.FunctionCallAggregateAvg,
Arguments: []interface{}{
parsers.SelectItem{
Path: []string{"c", "a1"},
Type: parsers.SelectItemTypeField,
},
},
},
},
},
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
},
)
})
t.Run("Should parse function COUNT()", func(t *testing.T) {
testQueryParse(
t,
`SELECT COUNT(c.a1) FROM c`,
parsers.SelectStmt{
SelectItems: []parsers.SelectItem{
{
Type: parsers.SelectItemTypeFunctionCall,
Value: parsers.FunctionCall{
Type: parsers.FunctionCallAggregateCount,
Arguments: []interface{}{
parsers.SelectItem{
Path: []string{"c", "a1"},
Type: parsers.SelectItemTypeField,
},
},
},
},
},
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
},
)
})
t.Run("Should parse function MAX()", func(t *testing.T) {
testQueryParse(
t,
`SELECT MAX(c.a1) FROM c`,
parsers.SelectStmt{
SelectItems: []parsers.SelectItem{
{
Type: parsers.SelectItemTypeFunctionCall,
Value: parsers.FunctionCall{
Type: parsers.FunctionCallAggregateMax,
Arguments: []interface{}{
parsers.SelectItem{
Path: []string{"c", "a1"},
Type: parsers.SelectItemTypeField,
},
},
},
},
},
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
},
)
})
t.Run("Should parse function MIN()", func(t *testing.T) {
testQueryParse(
t,
`SELECT MIN(c.a1) FROM c`,
parsers.SelectStmt{
SelectItems: []parsers.SelectItem{
{
Type: parsers.SelectItemTypeFunctionCall,
Value: parsers.FunctionCall{
Type: parsers.FunctionCallAggregateMin,
Arguments: []interface{}{
parsers.SelectItem{
Path: []string{"c", "a1"},
Type: parsers.SelectItemTypeField,
},
},
},
},
},
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
},
)
})
t.Run("Should parse function SUM()", func(t *testing.T) {
testQueryParse(
t,
`SELECT SUM(c.a1) FROM c`,
parsers.SelectStmt{
SelectItems: []parsers.SelectItem{
{
Type: parsers.SelectItemTypeFunctionCall,
Value: parsers.FunctionCall{
Type: parsers.FunctionCallAggregateSum,
Arguments: []interface{}{
parsers.SelectItem{
Path: []string{"c", "a1"},
Type: parsers.SelectItemTypeField,
},
},
},
},
},
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
},
)
})
}
+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),
},
},
)
})
}

Some files were not shown because too many files have changed in this diff Show More