├── NOTICE ├── .github ├── ISSUE_TEMPLATE │ ├── bug.md │ ├── feature.md │ ├── enhancement.md │ └── config.yml ├── CODEOWNERS ├── PULL_REQUEST_TEMPLATE.md ├── release.yml ├── PULL_REQUEST_TEMPLATE │ └── PULL_REQUEST_TEMPLATE_TEAM.md ├── dependabot.yml ├── RELEASE_CHECKLIST.md └── workflows │ └── integration.yml ├── internal ├── wire │ ├── testdata │ │ ├── handshake1_header.hex │ │ ├── handshake2_header.hex │ │ ├── handshake3_header.hex │ │ ├── handshake4_header.hex │ │ ├── handshake5_header.hex │ │ ├── handshake6_header.hex │ │ ├── msg_fuzz1.hex │ │ ├── handshake5_body.hex │ │ ├── dollar_dot.hex │ │ ├── handshake2_body.hex │ │ ├── handshake4_body.hex │ │ ├── import.hex │ │ ├── handshake1_body.hex │ │ └── handshake3_body.hex │ ├── wire.go │ ├── opmsgflagbit_string.go │ ├── opreplyflagbit_string.go │ ├── op_msg_flags_test.go │ ├── bits.go │ ├── opcode_string.go │ └── opqueryflagbit_string.go ├── bson │ ├── testdata │ │ ├── handshake3.hex │ │ ├── array_all.hex │ │ ├── array_fuzz1.hex │ │ ├── all.hex │ │ ├── handshake1.hex │ │ └── handshake2.hex │ ├── bool_test.go │ ├── null.go │ ├── cstring_test.go │ ├── object_id_test.go │ ├── regex_test.go │ ├── timestamp_test.go │ ├── tag_string.go │ ├── string_test.go │ ├── int32_test.go │ ├── tag.go │ ├── int64_test.go │ └── cstring.go ├── handlers │ ├── common │ │ ├── common.go │ │ ├── msg_debugerror.go │ │ ├── match.go │ │ ├── limit.go │ │ ├── parse_osrelease.go │ │ ├── msg_getfreemonitoringstatus.go │ │ ├── msg_getcmdlineopts.go │ │ ├── msg_connectionstatus.go │ │ ├── msg_setfreemonitoring.go │ │ └── typecode_test.go │ ├── dummy │ │ ├── cmd_query.go │ │ ├── msg_hostinfo.go │ │ ├── msg_buildinfo.go │ │ ├── msg_debugerror.go │ │ ├── msg_count.go │ │ ├── msg_drop.go │ │ ├── msg_find.go │ │ ├── msg_hello.go │ │ ├── msg_ping.go │ │ ├── msg_create.go │ │ ├── msg_delete.go │ │ ├── msg_getlog.go │ │ ├── msg_insert.go │ │ ├── msg_listcommands.go │ │ ├── msg_update.go │ │ ├── msg_datasize.go │ │ ├── msg_dbstats.go │ │ ├── msg_getcmdlineopts.go │ │ ├── msg_ismaster.go │ │ ├── msg_collstats.go │ │ ├── msg_whatsmyuri.go │ │ ├── msg_connectionstatus.go │ │ ├── msg_setfreemonitoring.go │ │ ├── msg_createindexes.go │ │ ├── msg_dropdatabase.go │ │ ├── msg_findandmodify.go │ │ ├── msg_getparameter.go │ │ ├── msg_listdatabases.go │ │ ├── msg_serverstatus.go │ │ ├── msg_listcollections.go │ │ ├── msg_getfreemonitoringstatus.go │ │ └── dummy.go │ ├── pg │ │ ├── pgdb │ │ │ └── placeholder.go │ │ ├── msg_hostinfo.go │ │ ├── msg_buildinfo.go │ │ ├── msg_debugerror.go │ │ ├── msg_listcommands.go │ │ ├── msg_getcmdlineopts.go │ │ ├── msg_connectionstatus.go │ │ ├── msg_setfreemonitoring.go │ │ ├── msg_getfreemonitoringstatus.go │ │ ├── msg_whatsmyuri.go │ │ ├── msg_ping.go │ │ ├── msg_createindexes.go │ │ ├── pg.go │ │ ├── msg_hello.go │ │ └── msg_ismaster.go │ └── tigris │ │ ├── msg_buildinfo.go │ │ ├── msg_hostinfo.go │ │ ├── msg_debugerror.go │ │ ├── msg_listcommands.go │ │ ├── msg_getcmdlineopts.go │ │ ├── msg_connectionstatus.go │ │ ├── msg_setfreemonitoring.go │ │ ├── msg_getfreemonitoringstatus.go │ │ ├── msg_count.go │ │ ├── msg_create.go │ │ ├── msg_dbstats.go │ │ ├── msg_datasize.go │ │ ├── msg_collstats.go │ │ ├── msg_whatsmyuri.go │ │ ├── msg_findandmodify.go │ │ ├── msg_ping.go │ │ ├── msg_createindexes.go │ │ ├── msg_hello.go │ │ └── msg_ismaster.go ├── types │ ├── sorttype_string.go │ ├── numberorderresult_string.go │ ├── compareresult_string.go │ ├── binarysubtype_string.go │ ├── comparetypeorderresult_string.go │ ├── types_test.go │ ├── timestamp.go │ └── binary.go ├── util │ ├── lazyerrors │ │ └── frame.go │ ├── testutil │ │ ├── testutil.go │ │ ├── dump.go │ │ ├── fuzz.go │ │ └── hexdump.go │ ├── must │ │ └── must.go │ ├── ctxutil │ │ └── ctxutil.go │ └── hex │ │ └── hex.go ├── fjson │ ├── bool_test.go │ ├── null.go │ ├── string_test.go │ ├── object_id_test.go │ ├── timestamp_test.go │ ├── regex_test.go │ ├── int32_test.go │ ├── int64_test.go │ ├── bool.go │ ├── int32.go │ ├── string.go │ └── datetime_test.go └── tjson │ ├── bool.go │ ├── int32.go │ ├── int64.go │ ├── string.go │ └── double.go ├── .markdownlint.yml ├── .gitignore ├── .codecov.yml ├── Dockerfile ├── tools ├── old.go └── check.go ├── cmd └── ferretdb │ ├── main_windows.go │ ├── main_unix.go │ ├── handler_dummy.go │ ├── main_test.go │ ├── handler_tigris.go │ └── handler_pg.go └── integration ├── shareddata └── shareddata.go └── delete_test.go /NOTICE: -------------------------------------------------------------------------------- 1 | FerretDB database system (https://www.ferretdb.io/). 2 | Copyright 2021 FerretDB Inc. 3 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug 3 | about: Some feature works incorrectly 4 | labels: code/bug 5 | --- 6 | -------------------------------------------------------------------------------- /internal/wire/testdata/handshake1_header.hex: -------------------------------------------------------------------------------- 1 | 00000000 74 01 00 00 01 00 00 00 00 00 00 00 d4 07 00 00 |t...............| 2 | -------------------------------------------------------------------------------- /internal/wire/testdata/handshake2_header.hex: -------------------------------------------------------------------------------- 1 | 00000000 3f 01 00 00 22 01 00 00 01 00 00 00 01 00 00 00 |?..."...........| 2 | -------------------------------------------------------------------------------- /internal/wire/testdata/handshake3_header.hex: -------------------------------------------------------------------------------- 1 | 00000000 74 01 00 00 02 00 00 00 00 00 00 00 d4 07 00 00 |t...............| 2 | -------------------------------------------------------------------------------- /internal/wire/testdata/handshake4_header.hex: -------------------------------------------------------------------------------- 1 | 00000000 3f 01 00 00 23 01 00 00 02 00 00 00 01 00 00 00 |?...#...........| 2 | -------------------------------------------------------------------------------- /internal/wire/testdata/handshake5_header.hex: -------------------------------------------------------------------------------- 1 | 00000000 5c 00 00 00 03 00 00 00 00 00 00 00 dd 07 00 00 |\...............| 2 | -------------------------------------------------------------------------------- /internal/wire/testdata/handshake6_header.hex: -------------------------------------------------------------------------------- 1 | 00000000 8b 07 00 00 24 01 00 00 03 00 00 00 dd 07 00 00 |....$...........| 2 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature 3 | about: Some feature is not implemented yet 4 | labels: code/feature 5 | --- 6 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/enhancement.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Enhancement 3 | about: Some feature could work better 4 | labels: code/enhancement 5 | --- 6 | -------------------------------------------------------------------------------- /internal/wire/testdata/msg_fuzz1.hex: -------------------------------------------------------------------------------- 1 | 00000000 30 00 00 00 30 30 30 30 30 30 30 30 dd 07 00 00 |0...00000000....| 2 | 00000010 30 30 30 30 01 30 30 30 ff 30 30 30 30 30 30 30 |0000.000.0000000| 3 | 00000020 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 |0000000000000000| 4 | -------------------------------------------------------------------------------- /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | # https://docs.github.com/en/repositories/managing-your-repositorys-settings-and-features/customizing-your-repository/about-code-owners 2 | # Order is important; the last matching pattern takes the most precedence. 3 | 4 | * @AlekSi 5 | /integration/ @AlekSi @rumyantseva 6 | /internal/ @AlekSi @rumyantseva 7 | -------------------------------------------------------------------------------- /internal/bson/testdata/handshake3.hex: -------------------------------------------------------------------------------- 1 | 00000000 47 00 00 00 10 62 75 69 6c 64 49 6e 66 6f 00 01 |G....buildInfo..| 2 | 00000010 00 00 00 03 6c 73 69 64 00 1e 00 00 00 05 69 64 |....lsid......id| 3 | 00000020 00 10 00 00 00 04 a3 19 f2 b4 a1 75 40 c7 b8 e7 |...........u@...| 4 | 00000030 a3 a3 2e c2 56 be 00 02 24 64 62 00 06 00 00 00 |....V...$db.....| 5 | 00000040 61 64 6d 69 6e 00 00 |admin..| 6 | -------------------------------------------------------------------------------- /internal/wire/testdata/handshake5_body.hex: -------------------------------------------------------------------------------- 1 | 00000000 00 00 00 00 00 47 00 00 00 10 62 75 69 6c 64 49 |.....G....buildI| 2 | 00000010 6e 66 6f 00 01 00 00 00 03 6c 73 69 64 00 1e 00 |nfo......lsid...| 3 | 00000020 00 00 05 69 64 00 10 00 00 00 04 a3 19 f2 b4 a1 |...id...........| 4 | 00000030 75 40 c7 b8 e7 a3 a3 2e c2 56 be 00 02 24 64 62 |u@.......V...$db| 5 | 00000040 00 06 00 00 00 61 64 6d 69 6e 00 00 |.....admin..| 6 | -------------------------------------------------------------------------------- /.markdownlint.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # https://github.com/DavidAnson/markdownlint 3 | # https://github.com/igorshubovych/markdownlint-cli 4 | # https://github.com/DavidAnson/markdownlint/blob/main/doc/Rules.md 5 | 6 | default: true 7 | 8 | MD002: false # deprecated 9 | MD003: {style: atx} 10 | MD004: {style: "asterisk"} 11 | MD006: false # deprecated 12 | MD013: false # we use https://sembr.org that could lead to long lines 13 | MD029: {style: ordered} 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Please use global .gitignore file for .vscode/, .idea/, etc. 2 | # See https://docs.github.com/en/get-started/getting-started-with-git/ignoring-files#configuring-ignored-files-for-all-repositories-on-your-computer 3 | 4 | bin/ 5 | vendor/ 6 | 7 | # test coverage results, generated files like commit.txt, etc. 8 | *.txt 9 | 10 | # test coverage results 11 | *.html 12 | 13 | # for now 14 | testdata/fuzz/ 15 | 16 | docker-compose.override.yml 17 | -------------------------------------------------------------------------------- /internal/bson/testdata/array_all.hex: -------------------------------------------------------------------------------- 1 | 00000000 58 00 00 00 04 30 00 05 00 00 00 00 05 31 00 01 |X....0.......1..| 2 | 00000010 00 00 00 80 42 08 32 00 01 09 33 00 2b e6 51 e7 |....B.2...3.+.Q.| 3 | 00000020 7a 01 00 00 03 34 00 05 00 00 00 00 01 35 00 71 |z....4.......5.q| 4 | 00000030 3d 0a d7 a3 10 45 40 10 36 00 2a 00 00 00 12 37 |=....E@.6.*....7| 5 | 00000040 00 2a 00 00 00 00 00 00 00 02 38 00 04 00 00 00 |.*........8.....| 6 | 00000050 66 6f 6f 00 0a 39 00 00 |foo..9..| 7 | -------------------------------------------------------------------------------- /internal/bson/testdata/array_fuzz1.hex: -------------------------------------------------------------------------------- 1 | 00000000 58 00 00 00 04 38 00 05 00 00 00 00 05 31 00 01 |X....8.......1..| 2 | 00000010 00 00 00 30 30 08 32 00 01 09 33 00 30 30 30 30 |...00.2...3.0000| 3 | 00000020 30 30 30 30 03 34 00 05 00 00 00 00 01 35 00 30 |0000.4.......5.0| 4 | 00000030 30 30 30 30 30 30 30 10 36 00 30 30 30 30 12 37 |0000000.6.0000.7| 5 | 00000040 00 30 30 30 30 30 30 30 30 02 30 00 04 00 00 00 |.00000000.0.....| 6 | 00000050 30 30 30 00 0a 39 00 00 |000..9..| 7 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/config.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # https://docs.github.com/en/communities/using-templates-to-encourage-useful-issues-and-pull-requests/configuring-issue-templates-for-your-repository 3 | blank_issues_enabled: false 4 | contact_links: 5 | - name: Quick question 6 | url: https://join.slack.com/t/ferretdb/shared_invite/zt-zqe9hj8g-ZcMG3~5Cs5u9uuOPnZB8~A 7 | about: Please join our Slack chat 8 | - name: General question or discussion 9 | url: https://github.com/FerretDB/FerretDB/discussions/categories/general 10 | about: Please use GitHub Discussions 11 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | # Description 2 | 3 | This PR closes #{issue_number}. 4 | 5 | 11 | 12 | ## Readiness checklist 13 | 14 | 18 | 19 | * [ ] I added tests for new functionality or bugfixes. 20 | * [ ] I ran `task all`, and it passed. 21 | -------------------------------------------------------------------------------- /internal/wire/testdata/dollar_dot.hex: -------------------------------------------------------------------------------- 1 | 0000 71 00 00 00 23 00 00 00 00 00 00 00 dd 07 00 00 q...#........... 2 | 0010 00 00 00 00 00 5c 00 00 00 02 69 6e 73 65 72 74 .....\....insert 3 | 0020 00 05 00 00 00 74 65 73 74 00 04 64 6f 63 75 6d .....test..docum 4 | 0030 65 6e 74 73 00 23 00 00 00 03 30 00 1b 00 00 00 ents.#....0..... 5 | 0040 08 24 2e 00 01 07 5f 69 64 00 61 af 7c 02 75 48 .$...._id.a.|.uH 6 | 0050 20 a5 92 3e a4 97 00 00 08 6f 72 64 65 72 65 64 ..>.....ordered 7 | 0060 00 01 02 24 64 62 00 05 00 00 00 74 65 73 74 00 ...$db.....test. 8 | 0070 00 . 9 | -------------------------------------------------------------------------------- /.codecov.yml: -------------------------------------------------------------------------------- 1 | --- 2 | coverage: 3 | range: "50...65" 4 | 5 | # https://docs.codecov.com/docs/commit-status 6 | status: 7 | project: 8 | default: 9 | target: auto 10 | threshold: 1% 11 | patch: 12 | default: 13 | target: 50% 14 | 15 | # https://docs.codecov.com/docs/ignoring-paths 16 | ignore: 17 | - cmd 18 | 19 | # https://docs.codecov.com/docs/pull-request-comments 20 | comment: 21 | layout: reach,diff,files,flags 22 | 23 | # https://docs.codecov.com/docs/github-checks 24 | # Annotations may be hidden by pressing the "a" key or by unselecting "Show annotations" in the top right of the file. 25 | github_checks: 26 | annotations: true 27 | -------------------------------------------------------------------------------- /.github/release.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # https://docs.github.com/en/repositories/releasing-projects-on-github/automatically-generated-release-notes 3 | 4 | changelog: 5 | categories: 6 | - title: New Features 🎉 7 | labels: 8 | - code/feature 9 | 10 | - title: Fixed Bugs 🐛 11 | labels: 12 | - code/bug 13 | - code/bug-regression 14 | 15 | - title: Enhancements 🛠 16 | labels: 17 | - code/enhancement 18 | 19 | - title: Documentation 📄 20 | labels: 21 | - documentation 22 | 23 | - title: Other Changes 🤖 24 | labels: 25 | - code/chore 26 | - deps 27 | 28 | - title: This section should be empty 29 | labels: 30 | - "*" 31 | -------------------------------------------------------------------------------- /internal/wire/wire.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // Package wire provides wire protocol implementation. 16 | package wire 17 | -------------------------------------------------------------------------------- /internal/handlers/common/common.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // Package common provides common code for all handlers. 16 | package common 17 | -------------------------------------------------------------------------------- /internal/types/sorttype_string.go: -------------------------------------------------------------------------------- 1 | // Code generated by "stringer -linecomment -type SortType"; DO NOT EDIT. 2 | 3 | package types 4 | 5 | import "strconv" 6 | 7 | func _() { 8 | // An "invalid array index" compiler error signifies that the constant values have changed. 9 | // Re-run the stringer command to generate them again. 10 | var x [1]struct{} 11 | _ = x[Ascending-1] 12 | _ = x[Descending - -1] 13 | } 14 | 15 | const ( 16 | _SortType_name_0 = "Descending" 17 | _SortType_name_1 = "Ascending" 18 | ) 19 | 20 | func (i SortType) String() string { 21 | switch { 22 | case i == -1: 23 | return _SortType_name_0 24 | case i == 1: 25 | return _SortType_name_1 26 | default: 27 | return "SortType(" + strconv.FormatInt(int64(i), 10) + ")" 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM scratch 2 | 3 | ARG VERSION 4 | ARG COMMIT 5 | ARG TARGETARCH 6 | 7 | ADD bin/ferretdb-${TARGETARCH} /ferretdb 8 | 9 | EXPOSE 27017 10 | 11 | ENTRYPOINT [ "/ferretdb" ] 12 | CMD [ "-listen-addr=:27017", "-postgresql-url=postgres://username:password@hostname:5432/ferretdb" ] 13 | 14 | # https://github.com/opencontainers/image-spec/blob/main/annotations.md 15 | LABEL org.opencontainers.image.description="A truly Open Source MongoDB alternative" 16 | LABEL org.opencontainers.image.licenses="Apache-2.0" 17 | LABEL org.opencontainers.image.revision="${COMMIT}" 18 | LABEL org.opencontainers.image.source="https://github.com/FerretDB/FerretDB" 19 | LABEL org.opencontainers.image.title="FerretDB" 20 | LABEL org.opencontainers.image.url="https://ferretdb.io/" 21 | LABEL org.opencontainers.image.vendor="FerretDB Inc." 22 | LABEL org.opencontainers.image.version="${VERSION}" 23 | -------------------------------------------------------------------------------- /tools/old.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | //go:build !go1.18 16 | // +build !go1.18 17 | 18 | package tools 19 | 20 | // The version of Go used in `go generate` command is old - we know that from the build tags above. 21 | // Let the user know. 22 | 23 | //go:generate go run check.go -old 24 | -------------------------------------------------------------------------------- /internal/types/numberorderresult_string.go: -------------------------------------------------------------------------------- 1 | // Code generated by "stringer -linecomment -type numberOrderResult"; DO NOT EDIT. 2 | 3 | package types 4 | 5 | import "strconv" 6 | 7 | func _() { 8 | // An "invalid array index" compiler error signifies that the constant values have changed. 9 | // Re-run the stringer command to generate them again. 10 | var x [1]struct{} 11 | _ = x[doubleNegativeZero-1] 12 | _ = x[doubleDT-2] 13 | _ = x[int32DT-3] 14 | _ = x[int64DT-4] 15 | } 16 | 17 | const _numberOrderResult_name = "doubleNegativeZerodoubleDTint32DTint64DT" 18 | 19 | var _numberOrderResult_index = [...]uint8{0, 18, 26, 33, 40} 20 | 21 | func (i numberOrderResult) String() string { 22 | i -= 1 23 | if i >= numberOrderResult(len(_numberOrderResult_index)-1) { 24 | return "numberOrderResult(" + strconv.FormatInt(int64(i+1), 10) + ")" 25 | } 26 | return _numberOrderResult_name[_numberOrderResult_index[i]:_numberOrderResult_index[i+1]] 27 | } 28 | -------------------------------------------------------------------------------- /internal/util/lazyerrors/frame.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package lazyerrors 16 | 17 | import "runtime" 18 | 19 | func pc() uintptr { 20 | pc := make([]uintptr, 1) 21 | runtime.Callers(3, pc) 22 | return pc[0] 23 | } 24 | 25 | func frame(pc uintptr) *runtime.Frame { 26 | f, _ := runtime.CallersFrames([]uintptr{pc}).Next() 27 | return &f 28 | } 29 | -------------------------------------------------------------------------------- /internal/util/testutil/testutil.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // Package testutil provides testing helpers. 16 | package testutil 17 | 18 | import ( 19 | "context" 20 | "testing" 21 | ) 22 | 23 | // Ctx returns test context. 24 | func Ctx(tb testing.TB) context.Context { 25 | tb.Helper() 26 | 27 | // TODO 28 | return context.Background() 29 | } 30 | -------------------------------------------------------------------------------- /cmd/ferretdb/main_windows.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package main 16 | 17 | import ( 18 | "context" 19 | "os" 20 | "os/signal" 21 | 22 | "golang.org/x/sys/windows" 23 | ) 24 | 25 | func notifyAppTermination(parent context.Context) (context.Context, context.CancelFunc) { 26 | return signal.NotifyContext(parent, windows.SIGTERM, windows.SIGINT, os.Interrupt) 27 | } 28 | -------------------------------------------------------------------------------- /internal/types/compareresult_string.go: -------------------------------------------------------------------------------- 1 | // Code generated by "stringer -linecomment -type CompareResult"; DO NOT EDIT. 2 | 3 | package types 4 | 5 | import "strconv" 6 | 7 | func _() { 8 | // An "invalid array index" compiler error signifies that the constant values have changed. 9 | // Re-run the stringer command to generate them again. 10 | var x [1]struct{} 11 | _ = x[Equal-0] 12 | _ = x[Less - -1] 13 | _ = x[Greater-1] 14 | _ = x[Incomparable-127] 15 | } 16 | 17 | const ( 18 | _CompareResult_name_0 = "<==>" 19 | _CompareResult_name_1 = "≹" 20 | ) 21 | 22 | var ( 23 | _CompareResult_index_0 = [...]uint8{0, 1, 3, 4} 24 | ) 25 | 26 | func (i CompareResult) String() string { 27 | switch { 28 | case -1 <= i && i <= 1: 29 | i -= -1 30 | return _CompareResult_name_0[_CompareResult_index_0[i]:_CompareResult_index_0[i+1]] 31 | case i == 127: 32 | return _CompareResult_name_1 33 | default: 34 | return "CompareResult(" + strconv.FormatInt(int64(i), 10) + ")" 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /internal/handlers/dummy/cmd_query.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package dummy 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/wire" 21 | ) 22 | 23 | // CmdQuery implements HandlerInterface. 24 | func (h *Handler) CmdQuery(ctx context.Context, query *wire.OpQuery) (*wire.OpReply, error) { 25 | return nil, notImplemented(query.Query.Command()) 26 | } 27 | -------------------------------------------------------------------------------- /cmd/ferretdb/main_unix.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | //go:build !windows 16 | // +build !windows 17 | 18 | package main 19 | 20 | import ( 21 | "context" 22 | "os/signal" 23 | 24 | "golang.org/x/sys/unix" 25 | ) 26 | 27 | func notifyAppTermination(parent context.Context) (context.Context, context.CancelFunc) { 28 | return signal.NotifyContext(parent, unix.SIGTERM, unix.SIGINT) 29 | } 30 | -------------------------------------------------------------------------------- /internal/wire/opmsgflagbit_string.go: -------------------------------------------------------------------------------- 1 | // Code generated by "stringer -linecomment -type OpMsgFlagBit"; DO NOT EDIT. 2 | 3 | package wire 4 | 5 | import "strconv" 6 | 7 | func _() { 8 | // An "invalid array index" compiler error signifies that the constant values have changed. 9 | // Re-run the stringer command to generate them again. 10 | var x [1]struct{} 11 | _ = x[OpMsgChecksumPresent-1] 12 | _ = x[OpMsgMoreToCome-2] 13 | _ = x[OpMsgExhaustAllowed-65536] 14 | } 15 | 16 | const ( 17 | _OpMsgFlagBit_name_0 = "checksumPresentmoreToCome" 18 | _OpMsgFlagBit_name_1 = "exhaustAllowed" 19 | ) 20 | 21 | var ( 22 | _OpMsgFlagBit_index_0 = [...]uint8{0, 15, 25} 23 | ) 24 | 25 | func (i OpMsgFlagBit) String() string { 26 | switch { 27 | case 1 <= i && i <= 2: 28 | i -= 1 29 | return _OpMsgFlagBit_name_0[_OpMsgFlagBit_index_0[i]:_OpMsgFlagBit_index_0[i+1]] 30 | case i == 65536: 31 | return _OpMsgFlagBit_name_1 32 | default: 33 | return "OpMsgFlagBit(" + strconv.FormatInt(int64(i), 10) + ")" 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /internal/handlers/pg/pgdb/placeholder.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package pgdb 16 | 17 | import "strconv" 18 | 19 | // Placeholder stores the number of the relevant placeholder of the query. 20 | type Placeholder int 21 | 22 | // Next increases the identifier value for the next variable in the PostgreSQL query. 23 | func (p *Placeholder) Next() string { 24 | *p++ 25 | return "$" + strconv.Itoa(int(*p)) 26 | } 27 | -------------------------------------------------------------------------------- /cmd/ferretdb/handler_dummy.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package main 16 | 17 | import ( 18 | "github.com/FerretDB/FerretDB/internal/handlers" 19 | "github.com/FerretDB/FerretDB/internal/handlers/dummy" 20 | ) 21 | 22 | // init registers `dummy` stub handler that is always enabled. 23 | func init() { 24 | registeredHandlers["dummy"] = func(*newHandlerOpts) (handlers.Interface, error) { 25 | return dummy.New() 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /internal/handlers/pg/msg_hostinfo.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package pg 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/handlers/common" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgHostInfo implements HandlerInterface. 25 | func (h *Handler) MsgHostInfo(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | return common.MsgHostInfo(ctx, msg) 27 | } 28 | -------------------------------------------------------------------------------- /internal/handlers/dummy/msg_hostinfo.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package dummy 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/handlers/common" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgHostInfo implements HandlerInterface. 25 | func (h *Handler) MsgHostInfo(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | return common.MsgHostInfo(ctx, msg) 27 | } 28 | -------------------------------------------------------------------------------- /internal/handlers/pg/msg_buildinfo.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package pg 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/handlers/common" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgBuildInfo implements HandlerInterface. 25 | func (h *Handler) MsgBuildInfo(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | return common.MsgBuildInfo(ctx, msg) 27 | } 28 | -------------------------------------------------------------------------------- /internal/handlers/dummy/msg_buildinfo.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package dummy 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/handlers/common" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgBuildInfo implements HandlerInterface. 25 | func (h *Handler) MsgBuildInfo(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | return common.MsgBuildInfo(ctx, msg) 27 | } 28 | -------------------------------------------------------------------------------- /internal/handlers/pg/msg_debugerror.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package pg 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/handlers/common" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgDebugError implements HandlerInterface. 25 | func (h *Handler) MsgDebugError(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | return common.MsgDebugError(ctx, msg) 27 | } 28 | -------------------------------------------------------------------------------- /internal/handlers/tigris/msg_buildinfo.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package tigris 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/handlers/common" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgBuildInfo implements HandlerInterface. 25 | func (h *Handler) MsgBuildInfo(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | return common.MsgBuildInfo(ctx, msg) 27 | } 28 | -------------------------------------------------------------------------------- /internal/handlers/tigris/msg_hostinfo.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package tigris 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/handlers/common" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgHostInfo implements HandlerInterface. 25 | func (h *Handler) MsgHostInfo(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | return common.MsgHostInfo(ctx, msg) 27 | } 28 | -------------------------------------------------------------------------------- /internal/handlers/dummy/msg_debugerror.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package dummy 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/handlers/common" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgDebugError implements HandlerInterface. 25 | func (h *Handler) MsgDebugError(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | return common.MsgDebugError(ctx, msg) 27 | } 28 | -------------------------------------------------------------------------------- /internal/handlers/tigris/msg_debugerror.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package tigris 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/handlers/common" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgDebugError implements HandlerInterface. 25 | func (h *Handler) MsgDebugError(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | return common.MsgDebugError(ctx, msg) 27 | } 28 | -------------------------------------------------------------------------------- /internal/handlers/dummy/msg_count.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package dummy 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/util/must" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgCount implements HandlerInterface. 25 | func (h *Handler) MsgCount(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | return nil, notImplemented(must.NotFail(msg.Document()).Command()) 27 | } 28 | -------------------------------------------------------------------------------- /internal/handlers/dummy/msg_drop.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package dummy 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/util/must" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgDrop implements HandlerInterface. 25 | func (h *Handler) MsgDrop(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | return nil, notImplemented(must.NotFail(msg.Document()).Command()) 27 | } 28 | -------------------------------------------------------------------------------- /internal/handlers/dummy/msg_find.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package dummy 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/util/must" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgFind implements HandlerInterface. 25 | func (h *Handler) MsgFind(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | return nil, notImplemented(must.NotFail(msg.Document()).Command()) 27 | } 28 | -------------------------------------------------------------------------------- /internal/handlers/dummy/msg_hello.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package dummy 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/util/must" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgHello implements HandlerInterface. 25 | func (h *Handler) MsgHello(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | return nil, notImplemented(must.NotFail(msg.Document()).Command()) 27 | } 28 | -------------------------------------------------------------------------------- /internal/handlers/dummy/msg_ping.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package dummy 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/util/must" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgPing implements HandlerInterface. 25 | func (h *Handler) MsgPing(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | return nil, notImplemented(must.NotFail(msg.Document()).Command()) 27 | } 28 | -------------------------------------------------------------------------------- /internal/handlers/pg/msg_listcommands.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package pg 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/handlers/common" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgListCommands implements handlers.Interface. 25 | func (h *Handler) MsgListCommands(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | return common.MsgListCommands(ctx, msg) 27 | } 28 | -------------------------------------------------------------------------------- /internal/handlers/dummy/msg_create.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package dummy 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/util/must" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgCreate implements HandlerInterface. 25 | func (h *Handler) MsgCreate(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | return nil, notImplemented(must.NotFail(msg.Document()).Command()) 27 | } 28 | -------------------------------------------------------------------------------- /internal/handlers/dummy/msg_delete.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package dummy 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/util/must" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgDelete implements HandlerInterface. 25 | func (h *Handler) MsgDelete(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | return nil, notImplemented(must.NotFail(msg.Document()).Command()) 27 | } 28 | -------------------------------------------------------------------------------- /internal/handlers/dummy/msg_getlog.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package dummy 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/util/must" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgGetLog implements HandlerInterface. 25 | func (h *Handler) MsgGetLog(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | return nil, notImplemented(must.NotFail(msg.Document()).Command()) 27 | } 28 | -------------------------------------------------------------------------------- /internal/handlers/dummy/msg_insert.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package dummy 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/util/must" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgInsert implements HandlerInterface. 25 | func (h *Handler) MsgInsert(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | return nil, notImplemented(must.NotFail(msg.Document()).Command()) 27 | } 28 | -------------------------------------------------------------------------------- /internal/handlers/dummy/msg_listcommands.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package dummy 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/handlers/common" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgListCommands implements handlers.Interface. 25 | func (h *Handler) MsgListCommands(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | return common.MsgListCommands(ctx, msg) 27 | } 28 | -------------------------------------------------------------------------------- /internal/handlers/dummy/msg_update.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package dummy 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/util/must" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgUpdate implements HandlerInterface. 25 | func (h *Handler) MsgUpdate(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | return nil, notImplemented(must.NotFail(msg.Document()).Command()) 27 | } 28 | -------------------------------------------------------------------------------- /internal/handlers/pg/msg_getcmdlineopts.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package pg 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/handlers/common" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgGetCmdLineOpts implements HandlerInterface. 25 | func (h *Handler) MsgGetCmdLineOpts(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | return common.MsgGetCmdLineOpts(ctx, msg) 27 | } 28 | -------------------------------------------------------------------------------- /internal/handlers/tigris/msg_listcommands.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package tigris 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/handlers/common" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgListCommands implements handlers.Interface. 25 | func (h *Handler) MsgListCommands(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | return common.MsgListCommands(ctx, msg) 27 | } 28 | -------------------------------------------------------------------------------- /internal/handlers/dummy/msg_datasize.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package dummy 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/util/must" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgDataSize implements HandlerInterface. 25 | func (h *Handler) MsgDataSize(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | return nil, notImplemented(must.NotFail(msg.Document()).Command()) 27 | } 28 | -------------------------------------------------------------------------------- /internal/handlers/dummy/msg_dbstats.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package dummy 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/util/must" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgDBStats implements HandlerInterface. 25 | func (h *Handler) MsgDBStats(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | return nil, notImplemented(must.NotFail(msg.Document()).Command()) 27 | } 28 | -------------------------------------------------------------------------------- /internal/handlers/dummy/msg_getcmdlineopts.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package dummy 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/handlers/common" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgGetCmdLineOpts implements HandlerInterface. 25 | func (h *Handler) MsgGetCmdLineOpts(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | return common.MsgGetCmdLineOpts(ctx, msg) 27 | } 28 | -------------------------------------------------------------------------------- /internal/handlers/dummy/msg_ismaster.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package dummy 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/util/must" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgIsMaster implements HandlerInterface. 25 | func (h *Handler) MsgIsMaster(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | return nil, notImplemented(must.NotFail(msg.Document()).Command()) 27 | } 28 | -------------------------------------------------------------------------------- /internal/handlers/pg/msg_connectionstatus.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package pg 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/handlers/common" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgConnectionStatus implements HandlerInterface. 25 | func (h *Handler) MsgConnectionStatus(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | return common.MsgConnectionStatus(ctx, msg) 27 | } 28 | -------------------------------------------------------------------------------- /internal/handlers/tigris/msg_getcmdlineopts.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package tigris 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/handlers/common" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgGetCmdLineOpts implements HandlerInterface. 25 | func (h *Handler) MsgGetCmdLineOpts(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | return common.MsgGetCmdLineOpts(ctx, msg) 27 | } 28 | -------------------------------------------------------------------------------- /internal/handlers/dummy/msg_collstats.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package dummy 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/util/must" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgCollStats implements HandlerInterface. 25 | func (h *Handler) MsgCollStats(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | return nil, notImplemented(must.NotFail(msg.Document()).Command()) 27 | } 28 | -------------------------------------------------------------------------------- /internal/handlers/dummy/msg_whatsmyuri.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package dummy 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/util/must" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgWhatsMyURI implements HandlerInterface. 25 | func (h *Handler) MsgWhatsMyURI(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | return nil, notImplemented(must.NotFail(msg.Document()).Command()) 27 | } 28 | -------------------------------------------------------------------------------- /internal/handlers/pg/msg_setfreemonitoring.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package pg 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/handlers/common" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgSetFreeMonitoring implements HandlerInterface. 25 | func (h *Handler) MsgSetFreeMonitoring(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | return common.MsgSetFreeMonitoring(ctx, msg) 27 | } 28 | -------------------------------------------------------------------------------- /internal/handlers/common/msg_debugerror.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package common 16 | 17 | import ( 18 | "context" 19 | "errors" 20 | 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgDebugError is a common implementation of the debugError command. 25 | func MsgDebugError(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | // TODO https://github.com/FerretDB/FerretDB/issues/640 27 | return nil, errors.New("debugError") 28 | } 29 | -------------------------------------------------------------------------------- /internal/handlers/dummy/msg_connectionstatus.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package dummy 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/handlers/common" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgConnectionStatus implements HandlerInterface. 25 | func (h *Handler) MsgConnectionStatus(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | return common.MsgConnectionStatus(ctx, msg) 27 | } 28 | -------------------------------------------------------------------------------- /internal/handlers/dummy/msg_setfreemonitoring.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package dummy 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/handlers/common" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgSetFreeMonitoring implements HandlerInterface. 25 | func (h *Handler) MsgSetFreeMonitoring(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | return common.MsgSetFreeMonitoring(ctx, msg) 27 | } 28 | -------------------------------------------------------------------------------- /internal/handlers/tigris/msg_connectionstatus.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package tigris 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/handlers/common" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgConnectionStatus implements HandlerInterface. 25 | func (h *Handler) MsgConnectionStatus(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | return common.MsgConnectionStatus(ctx, msg) 27 | } 28 | -------------------------------------------------------------------------------- /internal/handlers/dummy/msg_createindexes.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package dummy 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/util/must" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgCreateIndexes implements HandlerInterface. 25 | func (h *Handler) MsgCreateIndexes(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | return nil, notImplemented(must.NotFail(msg.Document()).Command()) 27 | } 28 | -------------------------------------------------------------------------------- /internal/handlers/dummy/msg_dropdatabase.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package dummy 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/util/must" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgDropDatabase implements HandlerInterface. 25 | func (h *Handler) MsgDropDatabase(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | return nil, notImplemented(must.NotFail(msg.Document()).Command()) 27 | } 28 | -------------------------------------------------------------------------------- /internal/handlers/dummy/msg_findandmodify.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package dummy 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/util/must" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgFindAndModify implements HandlerInterface. 25 | func (h *Handler) MsgFindAndModify(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | return nil, notImplemented(must.NotFail(msg.Document()).Command()) 27 | } 28 | -------------------------------------------------------------------------------- /internal/handlers/dummy/msg_getparameter.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package dummy 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/util/must" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgGetParameter implements HandlerInterface. 25 | func (h *Handler) MsgGetParameter(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | return nil, notImplemented(must.NotFail(msg.Document()).Command()) 27 | } 28 | -------------------------------------------------------------------------------- /internal/handlers/dummy/msg_listdatabases.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package dummy 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/util/must" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgListDatabases implements HandlerInterface. 25 | func (h *Handler) MsgListDatabases(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | return nil, notImplemented(must.NotFail(msg.Document()).Command()) 27 | } 28 | -------------------------------------------------------------------------------- /internal/handlers/dummy/msg_serverstatus.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package dummy 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/util/must" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgServerStatus implements HandlerInterface. 25 | func (h *Handler) MsgServerStatus(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | return nil, notImplemented(must.NotFail(msg.Document()).Command()) 27 | } 28 | -------------------------------------------------------------------------------- /internal/handlers/tigris/msg_setfreemonitoring.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package tigris 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/handlers/common" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgSetFreeMonitoring implements HandlerInterface. 25 | func (h *Handler) MsgSetFreeMonitoring(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | return common.MsgSetFreeMonitoring(ctx, msg) 27 | } 28 | -------------------------------------------------------------------------------- /internal/handlers/dummy/msg_listcollections.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package dummy 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/util/must" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgListCollections implements HandlerInterface. 25 | func (h *Handler) MsgListCollections(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | return nil, notImplemented(must.NotFail(msg.Document()).Command()) 27 | } 28 | -------------------------------------------------------------------------------- /internal/handlers/pg/msg_getfreemonitoringstatus.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package pg 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/handlers/common" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgGetFreeMonitoringStatus implements HandlerInterface. 25 | func (h *Handler) MsgGetFreeMonitoringStatus(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | return common.MsgGetFreeMonitoringStatus(ctx, msg) 27 | } 28 | -------------------------------------------------------------------------------- /internal/handlers/dummy/msg_getfreemonitoringstatus.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package dummy 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/handlers/common" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgGetFreeMonitoringStatus implements HandlerInterface. 25 | func (h *Handler) MsgGetFreeMonitoringStatus(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | return common.MsgGetFreeMonitoringStatus(ctx, msg) 27 | } 28 | -------------------------------------------------------------------------------- /internal/handlers/tigris/msg_getfreemonitoringstatus.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package tigris 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/handlers/common" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgGetFreeMonitoringStatus implements HandlerInterface. 25 | func (h *Handler) MsgGetFreeMonitoringStatus(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | return common.MsgGetFreeMonitoringStatus(ctx, msg) 27 | } 28 | -------------------------------------------------------------------------------- /cmd/ferretdb/main_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | //go:build testcover 16 | // +build testcover 17 | 18 | package main 19 | 20 | import ( 21 | "os" 22 | "testing" 23 | ) 24 | 25 | // TestCover allows us to run FerretDB with coverage enabled. 26 | func TestCover(t *testing.T) { 27 | main() 28 | } 29 | 30 | // TestMain ensures that command-line flags are initialized correctly when FerretDB is run with coverage enabled. 31 | func TestMain(m *testing.M) { 32 | initFlags() 33 | os.Exit(m.Run()) 34 | } 35 | -------------------------------------------------------------------------------- /internal/handlers/tigris/msg_count.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package tigris 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/util/must" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgCount implements HandlerInterface. 25 | func (h *Handler) MsgCount(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | // TODO https://github.com/FerretDB/FerretDB/issues/771 27 | return nil, notImplemented(must.NotFail(msg.Document()).Command()) 28 | } 29 | -------------------------------------------------------------------------------- /internal/handlers/tigris/msg_create.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package tigris 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/util/must" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgCreate implements HandlerInterface. 25 | func (h *Handler) MsgCreate(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | // TODO https://github.com/FerretDB/FerretDB/issues/772 27 | return nil, notImplemented(must.NotFail(msg.Document()).Command()) 28 | } 29 | -------------------------------------------------------------------------------- /internal/handlers/tigris/msg_dbstats.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package tigris 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/util/must" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgDBStats implements HandlerInterface. 25 | func (h *Handler) MsgDBStats(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | // TODO https://github.com/FerretDB/FerretDB/issues/774 27 | return nil, notImplemented(must.NotFail(msg.Document()).Command()) 28 | } 29 | -------------------------------------------------------------------------------- /internal/handlers/tigris/msg_datasize.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package tigris 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/util/must" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgDataSize implements HandlerInterface. 25 | func (h *Handler) MsgDataSize(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | // TODO https://github.com/FerretDB/FerretDB/issues/773 27 | return nil, notImplemented(must.NotFail(msg.Document()).Command()) 28 | } 29 | -------------------------------------------------------------------------------- /internal/handlers/tigris/msg_collstats.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package tigris 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/util/must" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgCollStats implements HandlerInterface. 25 | func (h *Handler) MsgCollStats(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | // TODO https://github.com/FerretDB/FerretDB/issues/770 27 | return nil, notImplemented(must.NotFail(msg.Document()).Command()) 28 | } 29 | -------------------------------------------------------------------------------- /internal/handlers/tigris/msg_whatsmyuri.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package tigris 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/util/must" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgWhatsMyURI implements HandlerInterface. 25 | func (h *Handler) MsgWhatsMyURI(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | // TODO https://github.com/FerretDB/FerretDB/issues/692 27 | return nil, notImplemented(must.NotFail(msg.Document()).Command()) 28 | } 29 | -------------------------------------------------------------------------------- /internal/handlers/tigris/msg_findandmodify.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package tigris 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/util/must" 21 | "github.com/FerretDB/FerretDB/internal/wire" 22 | ) 23 | 24 | // MsgFindAndModify implements HandlerInterface. 25 | func (h *Handler) MsgFindAndModify(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 26 | // TODO https://github.com/FerretDB/FerretDB/issues/775 27 | return nil, notImplemented(must.NotFail(msg.Document()).Command()) 28 | } 29 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE/PULL_REQUEST_TEMPLATE_TEAM.md: -------------------------------------------------------------------------------- 1 | 6 | 7 | # Description 8 | 9 | This PR closes #{issue_number}. 10 | 11 | 17 | 18 | ## Readiness checklist 19 | 20 | 24 | 25 | * [ ] I set assignee, reviewers, labels, project and sprint. 26 | * [ ] I added tests for new functionality or bugfixes. 27 | * [ ] I ran `task all`, and it passed. 28 | * [ ] I added/updated documentation for exported and unexported functions, variables, types, etc. 29 | * [ ] I checked complex documentation rendering with `bin/godoc -index -play -timestamps -http=127.0.0.1:6060`. 30 | -------------------------------------------------------------------------------- /internal/types/binarysubtype_string.go: -------------------------------------------------------------------------------- 1 | // Code generated by "stringer -linecomment -type BinarySubtype"; DO NOT EDIT. 2 | 3 | package types 4 | 5 | import "strconv" 6 | 7 | func _() { 8 | // An "invalid array index" compiler error signifies that the constant values have changed. 9 | // Re-run the stringer command to generate them again. 10 | var x [1]struct{} 11 | _ = x[BinaryGeneric-0] 12 | _ = x[BinaryFunction-1] 13 | _ = x[BinaryGenericOld-2] 14 | _ = x[BinaryUUIDOld-3] 15 | _ = x[BinaryUUID-4] 16 | _ = x[BinaryMD5-5] 17 | _ = x[BinaryEncrypted-6] 18 | _ = x[BinaryUser-128] 19 | } 20 | 21 | const ( 22 | _BinarySubtype_name_0 = "genericfunctiongeneric-olduuid-olduuidmd5encrypted" 23 | _BinarySubtype_name_1 = "user" 24 | ) 25 | 26 | var ( 27 | _BinarySubtype_index_0 = [...]uint8{0, 7, 15, 26, 34, 38, 41, 50} 28 | ) 29 | 30 | func (i BinarySubtype) String() string { 31 | switch { 32 | case i <= 6: 33 | return _BinarySubtype_name_0[_BinarySubtype_index_0[i]:_BinarySubtype_index_0[i+1]] 34 | case i == 128: 35 | return _BinarySubtype_name_1 36 | default: 37 | return "BinarySubtype(" + strconv.FormatInt(int64(i), 10) + ")" 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | --- 2 | version: 2 3 | updates: 4 | - package-ecosystem: "github-actions" 5 | directory: "/" 6 | labels: [deps] 7 | schedule: 8 | interval: "weekly" 9 | day: "thursday" 10 | time: "01:42" 11 | - package-ecosystem: "docker" 12 | directory: "/" 13 | labels: [deps] 14 | schedule: 15 | interval: "weekly" 16 | day: "thursday" 17 | time: "01:42" 18 | - package-ecosystem: "docker" 19 | directory: "/build/deps" 20 | labels: [deps] 21 | schedule: 22 | interval: "weekly" 23 | day: "thursday" 24 | time: "01:42" 25 | - package-ecosystem: "gomod" 26 | directory: "/" 27 | labels: [deps] 28 | schedule: 29 | interval: "weekly" 30 | day: "thursday" 31 | time: "01:42" 32 | - package-ecosystem: "gomod" 33 | directory: "/integration" 34 | labels: [deps] 35 | schedule: 36 | interval: "weekly" 37 | day: "thursday" 38 | time: "01:42" 39 | - package-ecosystem: "gomod" 40 | directory: "/tools" 41 | labels: [deps] 42 | schedule: 43 | interval: "weekly" 44 | day: "thursday" 45 | time: "01:42" 46 | -------------------------------------------------------------------------------- /internal/wire/opreplyflagbit_string.go: -------------------------------------------------------------------------------- 1 | // Code generated by "stringer -linecomment -type OpReplyFlagBit"; DO NOT EDIT. 2 | 3 | package wire 4 | 5 | import "strconv" 6 | 7 | func _() { 8 | // An "invalid array index" compiler error signifies that the constant values have changed. 9 | // Re-run the stringer command to generate them again. 10 | var x [1]struct{} 11 | _ = x[OpReplyCursorNotFound-1] 12 | _ = x[OpReplyQueryFailure-2] 13 | _ = x[OpReplyShardConfigStale-4] 14 | _ = x[OpReplyAwaitCapable-8] 15 | } 16 | 17 | const ( 18 | _OpReplyFlagBit_name_0 = "CursorNotFoundQueryFailure" 19 | _OpReplyFlagBit_name_1 = "ShardConfigStale" 20 | _OpReplyFlagBit_name_2 = "AwaitCapable" 21 | ) 22 | 23 | var ( 24 | _OpReplyFlagBit_index_0 = [...]uint8{0, 14, 26} 25 | ) 26 | 27 | func (i OpReplyFlagBit) String() string { 28 | switch { 29 | case 1 <= i && i <= 2: 30 | i -= 1 31 | return _OpReplyFlagBit_name_0[_OpReplyFlagBit_index_0[i]:_OpReplyFlagBit_index_0[i+1]] 32 | case i == 4: 33 | return _OpReplyFlagBit_name_1 34 | case i == 8: 35 | return _OpReplyFlagBit_name_2 36 | default: 37 | return "OpReplyFlagBit(" + strconv.FormatInt(int64(i), 10) + ")" 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /internal/wire/op_msg_flags_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package wire 16 | 17 | import ( 18 | "testing" 19 | 20 | "github.com/stretchr/testify/assert" 21 | ) 22 | 23 | func TestOpMsgFlagBitsString(t *testing.T) { 24 | t.Parallel() 25 | 26 | assert.Equal(t, "[]", OpMsgFlags(0).String()) 27 | assert.Equal(t, "[checksumPresent]", OpMsgFlags(OpMsgChecksumPresent).String()) 28 | assert.Equal(t, "[moreToCome]", OpMsgFlags(OpMsgMoreToCome).String()) 29 | assert.Equal(t, "[checksumPresent|exhaustAllowed]", OpMsgFlags(OpMsgChecksumPresent|OpMsgExhaustAllowed).String()) 30 | } 31 | -------------------------------------------------------------------------------- /internal/wire/bits.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package wire 16 | 17 | import "strings" 18 | 19 | type flagBit uint32 20 | 21 | type flags uint32 22 | 23 | func (flags flags) strings(bitStringer func(flagBit) string) []string { 24 | res := make([]string, 0, 2) 25 | for shift := 0; shift < 32; shift++ { 26 | bit := flags >> shift 27 | if bit&1 == 1 { 28 | res = append(res, bitStringer(1< 0: 25 | if int64(len(docs)) <= limit { 26 | return docs, nil 27 | } 28 | return docs[:limit], nil 29 | default: 30 | // TODO https://github.com/FerretDB/FerretDB/issues/79 31 | return nil, NewErrorMsg(ErrNotImplemented, "LimitDocuments: negative limit values are not supported") 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /internal/util/must/must.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // Package must provides Do helper function. 16 | package must 17 | 18 | // NotFail panics if the error is not nil, returns res otherwise. 19 | // 20 | // Use that function only for static initialization, test code, or code that "can't" fail. 21 | // When in doubt, don't. 22 | func NotFail[T any](res T, err error) T { 23 | if err != nil { 24 | panic(err) 25 | } 26 | return res 27 | } 28 | 29 | // NoError panics if the error is not nil. 30 | // 31 | // Use that function only for static initialization, test code, or code that "can't" fail. 32 | // When in doubt, don't. 33 | func NoError(err error) { 34 | if err != nil { 35 | panic(err) 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /internal/util/ctxutil/ctxutil.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package ctxutil 16 | 17 | import ( 18 | "context" 19 | "time" 20 | ) 21 | 22 | // WithDelay returns a context that is canceled after a given amount of time after done channel is closed. 23 | func WithDelay(done <-chan struct{}, delay time.Duration) (context.Context, context.CancelFunc) { 24 | ctx, cancel := context.WithCancel(context.Background()) 25 | 26 | go func() { 27 | select { 28 | case <-ctx.Done(): 29 | return 30 | 31 | case <-done: 32 | t := time.NewTimer(delay) 33 | defer t.Stop() 34 | 35 | select { 36 | case <-ctx.Done(): 37 | return 38 | case <-t.C: 39 | cancel() 40 | } 41 | } 42 | }() 43 | 44 | return ctx, cancel 45 | } 46 | -------------------------------------------------------------------------------- /internal/handlers/tigris/msg_ping.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package tigris 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/types" 21 | "github.com/FerretDB/FerretDB/internal/util/must" 22 | "github.com/FerretDB/FerretDB/internal/wire" 23 | ) 24 | 25 | // MsgPing implements HandlerInterface. 26 | func (h *Handler) MsgPing(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 27 | if _, err := h.driver.Info(ctx); err != nil { 28 | return nil, err 29 | } 30 | 31 | var reply wire.OpMsg 32 | must.NoError(reply.SetSections(wire.OpMsgSection{ 33 | Documents: []*types.Document{must.NotFail(types.NewDocument( 34 | "ok", float64(1), 35 | ))}, 36 | })) 37 | 38 | return &reply, nil 39 | } 40 | -------------------------------------------------------------------------------- /internal/fjson/bool_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package fjson 16 | 17 | import ( 18 | "testing" 19 | 20 | "github.com/AlekSi/pointer" 21 | ) 22 | 23 | var boolTestCases = []testCase{{ 24 | name: "false", 25 | v: pointer.To(boolType(false)), 26 | j: `false`, 27 | }, { 28 | name: "true", 29 | v: pointer.To(boolType(true)), 30 | j: `true`, 31 | }} 32 | 33 | func TestBool(t *testing.T) { 34 | t.Parallel() 35 | testJSON(t, boolTestCases, func() fjsontype { return new(boolType) }) 36 | } 37 | 38 | func FuzzBool(f *testing.F) { 39 | fuzzJSON(f, boolTestCases, func() fjsontype { return new(boolType) }) 40 | } 41 | 42 | func BenchmarkBool(b *testing.B) { 43 | benchmark(b, boolTestCases, func() fjsontype { return new(boolType) }) 44 | } 45 | -------------------------------------------------------------------------------- /internal/bson/bool_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package bson 16 | 17 | import ( 18 | "testing" 19 | 20 | "github.com/AlekSi/pointer" 21 | ) 22 | 23 | var boolTestCases = []testCase{{ 24 | name: "false", 25 | v: pointer.To(boolType(false)), 26 | b: []byte{0x00}, 27 | }, { 28 | name: "true", 29 | v: pointer.To(boolType(true)), 30 | b: []byte{0x01}, 31 | }} 32 | 33 | func TestBool(t *testing.T) { 34 | t.Parallel() 35 | testBinary(t, boolTestCases, func() bsontype { return new(boolType) }) 36 | } 37 | 38 | func FuzzBool(f *testing.F) { 39 | fuzzBinary(f, boolTestCases, func() bsontype { return new(boolType) }) 40 | } 41 | 42 | func BenchmarkBool(b *testing.B) { 43 | benchmark(b, boolTestCases, func() bsontype { return new(boolType) }) 44 | } 45 | -------------------------------------------------------------------------------- /internal/bson/null.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package bson 16 | 17 | import ( 18 | "bufio" 19 | 20 | "github.com/FerretDB/FerretDB/internal/types" 21 | ) 22 | 23 | // nullType represents BSON Null type. 24 | type nullType types.NullType 25 | 26 | func (*nullType) bsontype() {} 27 | 28 | // ReadFrom implements bsontype interface. 29 | func (*nullType) ReadFrom(r *bufio.Reader) error { 30 | return nil 31 | } 32 | 33 | // WriteTo implements bsontype interface. 34 | func (nullType) WriteTo(w *bufio.Writer) error { 35 | return nil 36 | } 37 | 38 | // MarshalBinary implements bsontype interface. 39 | func (nullType) MarshalBinary() ([]byte, error) { 40 | return nil, nil 41 | } 42 | 43 | // check interfaces 44 | var ( 45 | _ bsontype = (*nullType)(nil) 46 | ) 47 | -------------------------------------------------------------------------------- /internal/fjson/null.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package fjson 16 | 17 | import ( 18 | "fmt" 19 | 20 | "github.com/FerretDB/FerretDB/internal/types" 21 | ) 22 | 23 | // nullType represents BSON Null type. 24 | type nullType types.NullType 25 | 26 | // fjsontype implements fjsontype interface. 27 | func (*nullType) fjsontype() {} 28 | 29 | // UnmarshalJSON implements fjsontype interface. 30 | func (*nullType) UnmarshalJSON(data []byte) error { 31 | panic(fmt.Sprintf("must not be called, was called with %s", string(data))) 32 | } 33 | 34 | // MarshalJSON implements fjsontype interface. 35 | func (*nullType) MarshalJSON() ([]byte, error) { 36 | return []byte("null"), nil 37 | } 38 | 39 | // check interfaces 40 | var ( 41 | _ fjsontype = (*nullType)(nil) 42 | ) 43 | -------------------------------------------------------------------------------- /internal/handlers/common/parse_osrelease.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package common 16 | 17 | import ( 18 | "bufio" 19 | "io" 20 | "strings" 21 | 22 | "github.com/FerretDB/FerretDB/internal/util/lazyerrors" 23 | ) 24 | 25 | // parseOSRelease parses the /etc/os-release file. 26 | func parseOSRelease(reader io.Reader) (string, string, error) { 27 | scanner := bufio.NewScanner(reader) 28 | 29 | configParams := map[string]string{} 30 | for scanner.Scan() { 31 | str := strings.Split(scanner.Text(), "=") 32 | if len(str) == 1 { 33 | continue 34 | } 35 | configParams[str[0]] = str[1] 36 | } 37 | if err := scanner.Err(); err != nil { 38 | return "", "", lazyerrors.Error(err) 39 | } 40 | 41 | return configParams["NAME"], configParams["VERSION"], nil 42 | } 43 | -------------------------------------------------------------------------------- /cmd/ferretdb/handler_tigris.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | //go:build tigris 16 | // +build tigris 17 | 18 | package main 19 | 20 | import ( 21 | "flag" 22 | 23 | "github.com/FerretDB/FerretDB/internal/handlers" 24 | "github.com/FerretDB/FerretDB/internal/handlers/tigris" 25 | ) 26 | 27 | // `tigris` handler flags. 28 | var ( 29 | tigrisURLF = flag.String("tigris-url", "127.0.0.1:8081", "Tigris URL") 30 | ) 31 | 32 | // init registers `tigris` handler for Tigris that is enabled only when compiled with `tigris` build tag. 33 | func init() { 34 | registeredHandlers["tigris"] = func(opts *newHandlerOpts) (handlers.Interface, error) { 35 | handlerOpts := &tigris.NewOpts{ 36 | TigrisURL: *tigrisURLF, 37 | L: opts.logger, 38 | } 39 | return tigris.New(handlerOpts) 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /.github/RELEASE_CHECKLIST.md: -------------------------------------------------------------------------------- 1 | # Release Checklist 2 | 3 | 1. Check tests, linters. 4 | 2. Check issues and pull requests, update milestones and labels. 5 | 3. Create draft release on GitHub with autogenerated release notes. 6 | 4. Tweak generated release notes: 7 | * Ensure that "This section should be empty" section does not exist. 8 | * Add "All closed issues and pull requests" and "All commits", remove "Full Changelog". 9 | * Tweak spacing, headers levels. 10 | 5. Update CHANGELOG.md with the same release notes: 11 | * Check spacing, header levels. 12 | 6. Make a signed tag `vX.Y.Z` with the relevant section of the changelog using `--cleanup=verbatim`. 13 | 7. Push it! 14 | 8. Make [release](https://github.com/FerretDB/FerretDB/releases). 15 | 9. Refresh 16 | * `env GOPROXY=https://proxy.golang.org go install -v github.com/FerretDB/FerretDB/cmd/ferretdb@` 17 | * 18 | 10. `task docker-local` 19 | 11. `task docker-push` with four tags (`X.Y.Z` without leading `v` and `latest` for both ghcr.io and Docker Hub): 20 | * `task docker-push DOCKER_IMAGE=ferretdb/ferretdb:latest` 21 | * `task docker-push DOCKER_IMAGE=ferretdb/ferretdb:` 22 | * `task docker-push DOCKER_IMAGE=ghcr.io/ferretdb/ferretdb:latest` 23 | * `task docker-push DOCKER_IMAGE=ghcr.io/ferretdb/ferretdb:` 24 | 12. Close milestone in issues. 25 | 13. Announce it! 26 | -------------------------------------------------------------------------------- /internal/bson/cstring_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package bson 16 | 17 | import ( 18 | "testing" 19 | 20 | "github.com/AlekSi/pointer" 21 | ) 22 | 23 | var cstringTestCases = []testCase{{ 24 | name: "foo", 25 | v: pointer.To(CString("foo")), 26 | b: []byte{0x66, 0x6f, 0x6f, 0x00}, 27 | }, { 28 | name: "empty", 29 | v: pointer.To(CString("")), 30 | b: []byte{0x00}, 31 | }} 32 | 33 | func TestCString(t *testing.T) { 34 | t.Parallel() 35 | testBinary(t, cstringTestCases, func() bsontype { return new(CString) }) 36 | } 37 | 38 | func FuzzCString(f *testing.F) { 39 | fuzzBinary(f, cstringTestCases, func() bsontype { return new(CString) }) 40 | } 41 | 42 | func BenchmarkCString(b *testing.B) { 43 | benchmark(b, cstringTestCases, func() bsontype { return new(CString) }) 44 | } 45 | -------------------------------------------------------------------------------- /internal/types/comparetypeorderresult_string.go: -------------------------------------------------------------------------------- 1 | // Code generated by "stringer -linecomment -type compareTypeOrderResult"; DO NOT EDIT. 2 | 3 | package types 4 | 5 | import "strconv" 6 | 7 | func _() { 8 | // An "invalid array index" compiler error signifies that the constant values have changed. 9 | // Re-run the stringer command to generate them again. 10 | var x [1]struct{} 11 | _ = x[nullDataType-1] 12 | _ = x[nanDataType-2] 13 | _ = x[numbersDataType-3] 14 | _ = x[stringDataType-4] 15 | _ = x[documentDataType-5] 16 | _ = x[arrayDataType-6] 17 | _ = x[binDataType-7] 18 | _ = x[objectIDDataType-8] 19 | _ = x[booleanDataType-9] 20 | _ = x[dateDataType-10] 21 | _ = x[timestampDataType-11] 22 | _ = x[regexDataType-12] 23 | } 24 | 25 | const _compareTypeOrderResult_name = "nullDataTypenanDataTypenumbersDataTypestringDataTypedocumentDataTypearrayDataTypebinDataTypeobjectIDDataTypebooleanDataTypedateDataTypetimestampDataTyperegexDataType" 26 | 27 | var _compareTypeOrderResult_index = [...]uint8{0, 12, 23, 38, 52, 68, 81, 92, 108, 123, 135, 152, 165} 28 | 29 | func (i compareTypeOrderResult) String() string { 30 | i -= 1 31 | if i >= compareTypeOrderResult(len(_compareTypeOrderResult_index)-1) { 32 | return "compareTypeOrderResult(" + strconv.FormatInt(int64(i+1), 10) + ")" 33 | } 34 | return _compareTypeOrderResult_name[_compareTypeOrderResult_index[i]:_compareTypeOrderResult_index[i+1]] 35 | } 36 | -------------------------------------------------------------------------------- /internal/handlers/pg/msg_whatsmyuri.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package pg 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/types" 21 | "github.com/FerretDB/FerretDB/internal/util/lazyerrors" 22 | "github.com/FerretDB/FerretDB/internal/util/must" 23 | "github.com/FerretDB/FerretDB/internal/wire" 24 | ) 25 | 26 | // MsgWhatsMyURI implements HandlerInterface. 27 | func (h *Handler) MsgWhatsMyURI(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 28 | var reply wire.OpMsg 29 | err := reply.SetSections(wire.OpMsgSection{ 30 | Documents: []*types.Document{must.NotFail(types.NewDocument( 31 | "you", h.peerAddr, 32 | "ok", float64(1), 33 | ))}, 34 | }) 35 | if err != nil { 36 | return nil, lazyerrors.Error(err) 37 | } 38 | 39 | return &reply, nil 40 | } 41 | -------------------------------------------------------------------------------- /internal/types/types_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package types 16 | 17 | import ( 18 | "testing" 19 | 20 | "github.com/stretchr/testify/assert" 21 | ) 22 | 23 | func TestDeepCopy(t *testing.T) { 24 | t.Parallel() 25 | 26 | t.Run("Binary", func(t *testing.T) { 27 | t.Parallel() 28 | 29 | b1 := Binary{ 30 | Subtype: 0x01, 31 | B: []byte{0x01, 0x02, 0x03}, 32 | } 33 | b2 := deepCopy(b1) 34 | 35 | assert.Equal(t, b1, b2) 36 | assert.NotSame(t, b1, b2) 37 | 38 | b1.B[0] = 0 39 | assert.NotEqual(t, b1, b2) 40 | }) 41 | 42 | t.Run("ObjectID", func(t *testing.T) { 43 | t.Parallel() 44 | 45 | o1 := NewObjectID() 46 | o2 := deepCopy(o1) 47 | 48 | assert.Equal(t, o1, o2) 49 | assert.NotSame(t, o1, o2) 50 | 51 | o1[0] = 0 52 | assert.NotEqual(t, o1, o2) 53 | }) 54 | } 55 | -------------------------------------------------------------------------------- /internal/wire/opcode_string.go: -------------------------------------------------------------------------------- 1 | // Code generated by "stringer -linecomment -type OpCode"; DO NOT EDIT. 2 | 3 | package wire 4 | 5 | import "strconv" 6 | 7 | func _() { 8 | // An "invalid array index" compiler error signifies that the constant values have changed. 9 | // Re-run the stringer command to generate them again. 10 | var x [1]struct{} 11 | _ = x[OpCodeReply-1] 12 | _ = x[OpCodeUpdate-2001] 13 | _ = x[OpCodeInsert-2002] 14 | _ = x[OpCodeGetByOID-2003] 15 | _ = x[OpCodeQuery-2004] 16 | _ = x[OpCodeGetMore-2005] 17 | _ = x[OpCodeDelete-2006] 18 | _ = x[OpCodeKillCursors-2007] 19 | _ = x[OpCodeCompressed-2012] 20 | _ = x[OpCodeMsg-2013] 21 | } 22 | 23 | const ( 24 | _OpCode_name_0 = "OP_REPLY" 25 | _OpCode_name_1 = "OP_UPDATEOP_INSERTOP_GET_BY_OIDOP_QUERYOP_GET_MOREOP_DELETEOP_KILL_CURSORS" 26 | _OpCode_name_2 = "OP_COMPRESSEDOP_MSG" 27 | ) 28 | 29 | var ( 30 | _OpCode_index_1 = [...]uint8{0, 9, 18, 31, 39, 50, 59, 74} 31 | _OpCode_index_2 = [...]uint8{0, 13, 19} 32 | ) 33 | 34 | func (i OpCode) String() string { 35 | switch { 36 | case i == 1: 37 | return _OpCode_name_0 38 | case 2001 <= i && i <= 2007: 39 | i -= 2001 40 | return _OpCode_name_1[_OpCode_index_1[i]:_OpCode_index_1[i+1]] 41 | case 2012 <= i && i <= 2013: 42 | i -= 2012 43 | return _OpCode_name_2[_OpCode_index_2[i]:_OpCode_index_2[i+1]] 44 | default: 45 | return "OpCode(" + strconv.FormatInt(int64(i), 10) + ")" 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /internal/handlers/pg/msg_ping.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package pg 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/types" 21 | "github.com/FerretDB/FerretDB/internal/util/lazyerrors" 22 | "github.com/FerretDB/FerretDB/internal/util/must" 23 | "github.com/FerretDB/FerretDB/internal/wire" 24 | ) 25 | 26 | // MsgPing implements HandlerInterface. 27 | func (h *Handler) MsgPing(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 28 | if err := h.pgPool.Ping(ctx); err != nil { 29 | return nil, err 30 | } 31 | 32 | var reply wire.OpMsg 33 | err := reply.SetSections(wire.OpMsgSection{ 34 | Documents: []*types.Document{must.NotFail(types.NewDocument( 35 | "ok", float64(1), 36 | ))}, 37 | }) 38 | if err != nil { 39 | return nil, lazyerrors.Error(err) 40 | } 41 | 42 | return &reply, nil 43 | } 44 | -------------------------------------------------------------------------------- /internal/wire/opqueryflagbit_string.go: -------------------------------------------------------------------------------- 1 | // Code generated by "stringer -linecomment -type OpQueryFlagBit"; DO NOT EDIT. 2 | 3 | package wire 4 | 5 | import "strconv" 6 | 7 | func _() { 8 | // An "invalid array index" compiler error signifies that the constant values have changed. 9 | // Re-run the stringer command to generate them again. 10 | var x [1]struct{} 11 | _ = x[OpQueryTailableCursor-2] 12 | _ = x[OpQuerySlaveOk-4] 13 | _ = x[OpQueryOplogReplay-8] 14 | _ = x[OpQueryNoCursorTimeout-16] 15 | _ = x[OpQueryAwaitData-32] 16 | _ = x[OpQueryExhaust-64] 17 | _ = x[OpQueryPartial-128] 18 | } 19 | 20 | const ( 21 | _OpQueryFlagBit_name_0 = "TailableCursor" 22 | _OpQueryFlagBit_name_1 = "SlaveOk" 23 | _OpQueryFlagBit_name_2 = "OplogReplay" 24 | _OpQueryFlagBit_name_3 = "NoCursorTimeout" 25 | _OpQueryFlagBit_name_4 = "AwaitData" 26 | _OpQueryFlagBit_name_5 = "Exhaust" 27 | _OpQueryFlagBit_name_6 = "Partial" 28 | ) 29 | 30 | func (i OpQueryFlagBit) String() string { 31 | switch { 32 | case i == 2: 33 | return _OpQueryFlagBit_name_0 34 | case i == 4: 35 | return _OpQueryFlagBit_name_1 36 | case i == 8: 37 | return _OpQueryFlagBit_name_2 38 | case i == 16: 39 | return _OpQueryFlagBit_name_3 40 | case i == 32: 41 | return _OpQueryFlagBit_name_4 42 | case i == 64: 43 | return _OpQueryFlagBit_name_5 44 | case i == 128: 45 | return _OpQueryFlagBit_name_6 46 | default: 47 | return "OpQueryFlagBit(" + strconv.FormatInt(int64(i), 10) + ")" 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /internal/fjson/string_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package fjson 16 | 17 | import ( 18 | "testing" 19 | 20 | "github.com/AlekSi/pointer" 21 | ) 22 | 23 | var stringTestCases = []testCase{{ 24 | name: "foo", 25 | v: pointer.To(stringType("foo")), 26 | j: `"foo"`, 27 | }, { 28 | name: "empty", 29 | v: pointer.To(stringType("")), 30 | j: `""`, 31 | }, { 32 | name: "zero", 33 | v: pointer.To(stringType("\x00")), 34 | j: `"\u0000"`, 35 | }} 36 | 37 | func TestString(t *testing.T) { 38 | t.Parallel() 39 | testJSON(t, stringTestCases, func() fjsontype { return new(stringType) }) 40 | } 41 | 42 | func FuzzString(f *testing.F) { 43 | fuzzJSON(f, stringTestCases, func() fjsontype { return new(stringType) }) 44 | } 45 | 46 | func BenchmarkString(b *testing.B) { 47 | benchmark(b, stringTestCases, func() fjsontype { return new(stringType) }) 48 | } 49 | -------------------------------------------------------------------------------- /internal/fjson/object_id_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package fjson 16 | 17 | import ( 18 | "testing" 19 | 20 | "github.com/AlekSi/pointer" 21 | ) 22 | 23 | var objectIDTestCases = []testCase{{ 24 | name: "normal", 25 | v: pointer.To(objectIDType{0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}), 26 | j: `{"$o":"010101010101010101010101"}`, 27 | }, { 28 | name: "EOF", 29 | j: `{`, 30 | jErr: `unexpected EOF`, 31 | }} 32 | 33 | func TestObjectID(t *testing.T) { 34 | t.Parallel() 35 | testJSON(t, objectIDTestCases, func() fjsontype { return new(objectIDType) }) 36 | } 37 | 38 | func FuzzObjectID(f *testing.F) { 39 | fuzzJSON(f, objectIDTestCases, func() fjsontype { return new(objectIDType) }) 40 | } 41 | 42 | func BenchmarkObjectID(b *testing.B) { 43 | benchmark(b, objectIDTestCases, func() fjsontype { return new(objectIDType) }) 44 | } 45 | -------------------------------------------------------------------------------- /internal/fjson/timestamp_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package fjson 16 | 17 | import ( 18 | "testing" 19 | 20 | "github.com/AlekSi/pointer" 21 | ) 22 | 23 | var timestampTestCases = []testCase{{ 24 | name: "one", 25 | v: pointer.To(timestampType(1)), 26 | j: `{"$t":"1"}`, 27 | }, { 28 | name: "zero", 29 | v: pointer.To(timestampType(0)), 30 | j: `{"$t":"0"}`, 31 | }, { 32 | name: "EOF", 33 | j: `{`, 34 | jErr: `unexpected EOF`, 35 | }} 36 | 37 | func TestTimestamp(t *testing.T) { 38 | t.Parallel() 39 | testJSON(t, timestampTestCases, func() fjsontype { return new(timestampType) }) 40 | } 41 | 42 | func FuzzTimestamp(f *testing.F) { 43 | fuzzJSON(f, timestampTestCases, func() fjsontype { return new(timestampType) }) 44 | } 45 | 46 | func BenchmarkTimestamp(b *testing.B) { 47 | benchmark(b, timestampTestCases, func() fjsontype { return new(timestampType) }) 48 | } 49 | -------------------------------------------------------------------------------- /internal/fjson/regex_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package fjson 16 | 17 | import ( 18 | "testing" 19 | 20 | "github.com/AlekSi/pointer" 21 | ) 22 | 23 | var regexTestCases = []testCase{{ 24 | name: "normal", 25 | v: pointer.To(regexType{Pattern: "hoffman", Options: "i"}), 26 | j: `{"$r":"hoffman","o":"i"}`, 27 | }, { 28 | name: "empty", 29 | v: pointer.To(regexType{Pattern: "", Options: ""}), 30 | j: `{"$r":"","o":""}`, 31 | }, { 32 | name: "EOF", 33 | j: `{`, 34 | jErr: `unexpected EOF`, 35 | }} 36 | 37 | func TestRegex(t *testing.T) { 38 | t.Parallel() 39 | testJSON(t, regexTestCases, func() fjsontype { return new(regexType) }) 40 | } 41 | 42 | func FuzzRegex(f *testing.F) { 43 | fuzzJSON(f, regexTestCases, func() fjsontype { return new(regexType) }) 44 | } 45 | 46 | func BenchmarkRegex(b *testing.B) { 47 | benchmark(b, regexTestCases, func() fjsontype { return new(regexType) }) 48 | } 49 | -------------------------------------------------------------------------------- /internal/handlers/common/msg_getfreemonitoringstatus.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package common 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/types" 21 | "github.com/FerretDB/FerretDB/internal/util/lazyerrors" 22 | "github.com/FerretDB/FerretDB/internal/util/must" 23 | "github.com/FerretDB/FerretDB/internal/wire" 24 | ) 25 | 26 | // MsgGetFreeMonitoringStatus is a common implementation of the getFreeMonitoringStatus command. 27 | func MsgGetFreeMonitoringStatus(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 28 | var reply wire.OpMsg 29 | err := reply.SetSections(wire.OpMsgSection{ 30 | Documents: []*types.Document{must.NotFail(types.NewDocument( 31 | "state", "disabled", 32 | "message", "monitoring is not enabled", 33 | "ok", float64(1), 34 | ))}, 35 | }) 36 | if err != nil { 37 | return nil, lazyerrors.Error(err) 38 | } 39 | return &reply, nil 40 | } 41 | -------------------------------------------------------------------------------- /internal/util/testutil/dump.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package testutil 16 | 17 | import ( 18 | "bytes" 19 | "encoding/json" 20 | "testing" 21 | 22 | "github.com/stretchr/testify/require" 23 | 24 | "github.com/FerretDB/FerretDB/internal/fjson" 25 | "github.com/FerretDB/FerretDB/internal/types" 26 | ) 27 | 28 | // Dump returns string representation for debugging. 29 | func Dump[T types.Type](tb testing.TB, o T) string { 30 | tb.Helper() 31 | 32 | // We might switch to go-spew or something else later. 33 | b, err := fjson.Marshal(o) 34 | require.NoError(tb, err) 35 | 36 | return string(IndentJSON(tb, b)) 37 | } 38 | 39 | // IndentJSON returns an indented form of the JSON input. 40 | func IndentJSON(tb testing.TB, b []byte) []byte { 41 | tb.Helper() 42 | 43 | dst := bytes.NewBuffer(make([]byte, 0, len(b))) 44 | err := json.Indent(dst, b, "", " ") 45 | require.NoError(tb, err) 46 | return dst.Bytes() 47 | } 48 | -------------------------------------------------------------------------------- /internal/bson/object_id_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package bson 16 | 17 | import ( 18 | "testing" 19 | 20 | "github.com/AlekSi/pointer" 21 | ) 22 | 23 | var objectIDTestCases = []testCase{{ 24 | name: "normal", 25 | v: pointer.To(objectIDType{0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}), 26 | b: []byte{0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 27 | }, { 28 | name: "EOF", 29 | b: []byte{0x00}, 30 | bErr: `unexpected EOF`, 31 | }} 32 | 33 | func TestObjectID(t *testing.T) { 34 | t.Parallel() 35 | testBinary(t, objectIDTestCases, func() bsontype { return new(objectIDType) }) 36 | } 37 | 38 | func FuzzObjectID(f *testing.F) { 39 | fuzzBinary(f, objectIDTestCases, func() bsontype { return new(objectIDType) }) 40 | } 41 | 42 | func BenchmarkObjectID(b *testing.B) { 43 | benchmark(b, objectIDTestCases, func() bsontype { return new(objectIDType) }) 44 | } 45 | -------------------------------------------------------------------------------- /internal/handlers/common/msg_getcmdlineopts.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package common 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/types" 21 | "github.com/FerretDB/FerretDB/internal/util/lazyerrors" 22 | "github.com/FerretDB/FerretDB/internal/util/must" 23 | "github.com/FerretDB/FerretDB/internal/wire" 24 | ) 25 | 26 | // MsgGetCmdLineOpts is a common implementation of the getCmdLineOpts command. 27 | func MsgGetCmdLineOpts(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 28 | var reply wire.OpMsg 29 | err := reply.SetSections(wire.OpMsgSection{ 30 | Documents: []*types.Document{must.NotFail(types.NewDocument( 31 | "argv", must.NotFail(types.NewArray("ferretdb")), 32 | "parsed", must.NotFail(types.NewDocument()), 33 | "ok", float64(1), 34 | ))}, 35 | }) 36 | if err != nil { 37 | return nil, lazyerrors.Error(err) 38 | } 39 | 40 | return &reply, nil 41 | } 42 | -------------------------------------------------------------------------------- /internal/types/timestamp.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package types 16 | 17 | import ( 18 | "sync/atomic" 19 | "time" 20 | ) 21 | 22 | type ( 23 | // Timestamp represents BSON type Timestamp. 24 | Timestamp int64 25 | ) 26 | 27 | // timestampCounter is an ordinal number for timestamps in the system. 28 | var timestampCounter uint32 29 | 30 | // NewTimestamp returns a timestamp from time and an increment. 31 | func NewTimestamp(t time.Time, c uint32) Timestamp { 32 | sec := t.Unix() 33 | sec <<= 32 34 | sec |= int64(c) 35 | return Timestamp(sec) 36 | } 37 | 38 | // NextTimestamp returns a timestamp from time and an internal ops counter. 39 | func NextTimestamp(t time.Time) Timestamp { 40 | c := atomic.AddUint32(×tampCounter, 1) 41 | return NewTimestamp(t, c) 42 | } 43 | 44 | // Time returns time.Time ignoring increment. 45 | func (t Timestamp) Time() time.Time { 46 | t >>= 32 47 | return time.Unix(int64(t), 0) 48 | } 49 | -------------------------------------------------------------------------------- /internal/util/testutil/fuzz.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package testutil 16 | 17 | import ( 18 | "bytes" 19 | "crypto/sha256" 20 | "fmt" 21 | "os" 22 | "path/filepath" 23 | "testing" 24 | 25 | "github.com/stretchr/testify/require" 26 | ) 27 | 28 | // WriteSeedCorpusFile adds given data to the fuzzing seed corpus for given fuzz function. 29 | // 30 | // It can be an alternative to using f.Add. 31 | func WriteSeedCorpusFile(tb testing.TB, funcName string, b []byte) { 32 | tb.Helper() 33 | 34 | var buf bytes.Buffer 35 | buf.WriteString("go test fuzz v1\n") 36 | _, err := fmt.Fprintf(&buf, "[]byte(%q)\n", b) 37 | require.NoError(tb, err) 38 | 39 | dir := filepath.Join("testdata", "fuzz", funcName) 40 | err = os.MkdirAll(dir, 0o777) 41 | require.NoError(tb, err) 42 | 43 | filename := filepath.Join(dir, fmt.Sprintf("test-%x", sha256.Sum256(b))) 44 | err = os.WriteFile(filename, buf.Bytes(), 0o666) 45 | require.NoError(tb, err) 46 | } 47 | -------------------------------------------------------------------------------- /tools/check.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | //go:build ignore 16 | // +build ignore 17 | 18 | // This file is invoked from tools.go or old.go and should work with any version of Go. 19 | 20 | package main 21 | 22 | import ( 23 | "flag" 24 | "log" 25 | "regexp" 26 | "runtime" 27 | "strconv" 28 | ) 29 | 30 | func main() { 31 | log.SetFlags(0) 32 | 33 | oldF := flag.Bool("old", false, "") 34 | flag.Parse() 35 | 36 | if *oldF { 37 | log.Fatalf("FerretDB requires Go 1.18 or later.") 38 | } 39 | 40 | v := runtime.Version() 41 | re := regexp.MustCompile(`go1\.(\d+)`) 42 | m := re.FindStringSubmatch(v) 43 | if len(m) != 2 { 44 | log.Fatalf("Unexpected version %q.", v) 45 | } 46 | 47 | minor, err := strconv.Atoi(m[1]) 48 | if err != nil { 49 | log.Fatalf("Unexpected version %q: %s.", v, err) 50 | } 51 | 52 | if minor < 18 { 53 | log.Fatalf("FerretDB requires Go 1.18 or later. The version of `go` binary in $PATH is %q.", v) 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /internal/bson/regex_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package bson 16 | 17 | import ( 18 | "testing" 19 | 20 | "github.com/AlekSi/pointer" 21 | ) 22 | 23 | var regexTestCases = []testCase{{ 24 | name: "normal", 25 | v: pointer.To(regexType{Pattern: "hoffman", Options: "i"}), 26 | b: []byte{0x68, 0x6f, 0x66, 0x66, 0x6d, 0x61, 0x6e, 0x00, 0x69, 0x00}, 27 | }, { 28 | name: "empty", 29 | v: pointer.To(regexType{Pattern: "", Options: ""}), 30 | b: []byte{0x00, 0x00}, 31 | }, { 32 | name: "EOF", 33 | b: []byte{0x00}, 34 | bErr: `EOF`, 35 | }} 36 | 37 | func TestRegex(t *testing.T) { 38 | t.Parallel() 39 | testBinary(t, regexTestCases, func() bsontype { return new(regexType) }) 40 | } 41 | 42 | func FuzzRegex(f *testing.F) { 43 | fuzzBinary(f, regexTestCases, func() bsontype { return new(regexType) }) 44 | } 45 | 46 | func BenchmarkRegex(b *testing.B) { 47 | benchmark(b, regexTestCases, func() bsontype { return new(regexType) }) 48 | } 49 | -------------------------------------------------------------------------------- /cmd/ferretdb/handler_pg.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package main 16 | 17 | import ( 18 | "flag" 19 | "time" 20 | 21 | "github.com/FerretDB/FerretDB/internal/handlers" 22 | "github.com/FerretDB/FerretDB/internal/handlers/pg" 23 | "github.com/FerretDB/FerretDB/internal/handlers/pg/pgdb" 24 | ) 25 | 26 | // `pg` handler flags. 27 | var ( 28 | postgresqlURLF = flag.String("postgresql-url", "postgres://postgres@127.0.0.1:5432/ferretdb", "PostgreSQL URL") 29 | ) 30 | 31 | // init registers `pg` handler for PostgreSQL that is always enabled. 32 | func init() { 33 | registeredHandlers["pg"] = func(opts *newHandlerOpts) (handlers.Interface, error) { 34 | pgPool, err := pgdb.NewPool(opts.ctx, *postgresqlURLF, opts.logger, false) 35 | if err != nil { 36 | return nil, err 37 | } 38 | 39 | handlerOpts := &pg.NewOpts{ 40 | PgPool: pgPool, 41 | L: opts.logger, 42 | StartTime: time.Now(), 43 | } 44 | return pg.New(handlerOpts) 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /internal/bson/testdata/all.hex: -------------------------------------------------------------------------------- 1 | 00000000 2d 01 00 00 04 62 69 6e 61 72 79 00 16 00 00 00 |-....binary.....| 2 | 00000010 05 30 00 01 00 00 00 80 42 05 31 00 00 00 00 00 |.0......B.1.....| 3 | 00000020 00 00 04 62 6f 6f 6c 00 0d 00 00 00 08 30 00 01 |...bool......0..| 4 | 00000030 08 31 00 00 00 04 64 61 74 65 74 69 6d 65 00 1b |.1....datetime..| 5 | 00000040 00 00 00 09 30 00 2b e6 51 e7 7a 01 00 00 09 31 |....0.+.Q.z....1| 6 | 00000050 00 00 28 d3 ed 7c c7 ff ff 00 04 64 6f 75 62 6c |..(..|.....doubl| 7 | 00000060 65 00 1b 00 00 00 01 30 00 71 3d 0a d7 a3 10 45 |e......0.q=....E| 8 | 00000070 40 01 31 00 00 00 00 00 00 00 00 00 00 04 69 6e |@.1...........in| 9 | 00000080 74 33 32 00 13 00 00 00 10 30 00 2a 00 00 00 10 |t32......0.*....| 10 | 00000090 31 00 00 00 00 00 00 04 69 6e 74 36 34 00 1b 00 |1.......int64...| 11 | 000000a0 00 00 12 30 00 2a 00 00 00 00 00 00 00 12 31 00 |...0.*........1.| 12 | 000000b0 00 00 00 00 00 00 00 00 00 04 6f 62 6a 65 63 74 |..........object| 13 | 000000c0 49 44 00 23 00 00 00 07 30 00 42 00 00 00 00 00 |ID.#....0.B.....| 14 | 000000d0 00 00 00 00 00 00 07 31 00 00 00 00 00 00 00 00 |.......1........| 15 | 000000e0 00 00 00 00 00 00 04 73 74 72 69 6e 67 00 18 00 |.......string...| 16 | 000000f0 00 00 02 30 00 04 00 00 00 66 6f 6f 00 02 31 00 |...0.....foo..1.| 17 | 00000100 01 00 00 00 00 00 04 74 69 6d 65 73 74 61 6d 70 |.......timestamp| 18 | 00000110 00 1b 00 00 00 11 30 00 2a 00 00 00 00 00 00 00 |......0.*.......| 19 | 00000120 11 31 00 00 00 00 00 00 00 00 00 00 00 |.1...........| 20 | -------------------------------------------------------------------------------- /internal/wire/testdata/handshake2_body.hex: -------------------------------------------------------------------------------- 1 | 00000000 08 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 2 | 00000010 01 00 00 00 1b 01 00 00 08 69 73 6d 61 73 74 65 |.........ismaste| 3 | 00000020 72 00 01 03 74 6f 70 6f 6c 6f 67 79 56 65 72 73 |r...topologyVers| 4 | 00000030 69 6f 6e 00 2d 00 00 00 07 70 72 6f 63 65 73 73 |ion.-....process| 5 | 00000040 49 64 00 60 fb ed 53 71 fe 1b ae 70 33 95 05 12 |Id....Sq...p3...| 6 | 00000050 63 6f 75 6e 74 65 72 00 00 00 00 00 00 00 00 00 |counter.........| 7 | 00000060 00 10 6d 61 78 42 73 6f 6e 4f 62 6a 65 63 74 53 |..maxBsonObjectS| 8 | 00000070 69 7a 65 00 00 00 00 01 10 6d 61 78 4d 65 73 73 |ize......maxMess| 9 | 00000080 61 67 65 53 69 7a 65 42 79 74 65 73 00 00 6c dc |ageSizeBytes..l.| 10 | 00000090 02 10 6d 61 78 57 72 69 74 65 42 61 74 63 68 53 |..maxWriteBatchS| 11 | 000000a0 69 7a 65 00 a0 86 01 00 09 6c 6f 63 61 6c 54 69 |ize......localTi| 12 | 000000b0 6d 65 00 a3 00 95 d8 7a 01 00 00 10 6c 6f 67 69 |me.....z....logi| 13 | 000000c0 63 61 6c 53 65 73 73 69 6f 6e 54 69 6d 65 6f 75 |calSessionTimeou| 14 | 000000d0 74 4d 69 6e 75 74 65 73 00 1e 00 00 00 10 63 6f |tMinutes......co| 15 | 000000e0 6e 6e 65 63 74 69 6f 6e 49 64 00 1c 00 00 00 10 |nnectionId......| 16 | 000000f0 6d 69 6e 57 69 72 65 56 65 72 73 69 6f 6e 00 00 |minWireVersion..| 17 | 00000100 00 00 00 10 6d 61 78 57 69 72 65 56 65 72 73 69 |....maxWireVersi| 18 | 00000110 6f 6e 00 0d 00 00 00 08 72 65 61 64 4f 6e 6c 79 |on......readOnly| 19 | 00000120 00 00 01 6f 6b 00 00 00 00 00 00 00 f0 3f 00 |...ok........?.| 20 | -------------------------------------------------------------------------------- /internal/wire/testdata/handshake4_body.hex: -------------------------------------------------------------------------------- 1 | 00000000 08 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 2 | 00000010 01 00 00 00 1b 01 00 00 08 69 73 6d 61 73 74 65 |.........ismaste| 3 | 00000020 72 00 01 03 74 6f 70 6f 6c 6f 67 79 56 65 72 73 |r...topologyVers| 4 | 00000030 69 6f 6e 00 2d 00 00 00 07 70 72 6f 63 65 73 73 |ion.-....process| 5 | 00000040 49 64 00 60 fb ed 53 71 fe 1b ae 70 33 95 05 12 |Id....Sq...p3...| 6 | 00000050 63 6f 75 6e 74 65 72 00 00 00 00 00 00 00 00 00 |counter.........| 7 | 00000060 00 10 6d 61 78 42 73 6f 6e 4f 62 6a 65 63 74 53 |..maxBsonObjectS| 8 | 00000070 69 7a 65 00 00 00 00 01 10 6d 61 78 4d 65 73 73 |ize......maxMess| 9 | 00000080 61 67 65 53 69 7a 65 42 79 74 65 73 00 00 6c dc |ageSizeBytes..l.| 10 | 00000090 02 10 6d 61 78 57 72 69 74 65 42 61 74 63 68 53 |..maxWriteBatchS| 11 | 000000a0 69 7a 65 00 a0 86 01 00 09 6c 6f 63 61 6c 54 69 |ize......localTi| 12 | 000000b0 6d 65 00 b8 00 95 d8 7a 01 00 00 10 6c 6f 67 69 |me.....z....logi| 13 | 000000c0 63 61 6c 53 65 73 73 69 6f 6e 54 69 6d 65 6f 75 |calSessionTimeou| 14 | 000000d0 74 4d 69 6e 75 74 65 73 00 1e 00 00 00 10 63 6f |tMinutes......co| 15 | 000000e0 6e 6e 65 63 74 69 6f 6e 49 64 00 1d 00 00 00 10 |nnectionId......| 16 | 000000f0 6d 69 6e 57 69 72 65 56 65 72 73 69 6f 6e 00 00 |minWireVersion..| 17 | 00000100 00 00 00 10 6d 61 78 57 69 72 65 56 65 72 73 69 |....maxWireVersi| 18 | 00000110 6f 6e 00 0d 00 00 00 08 72 65 61 64 4f 6e 6c 79 |on......readOnly| 19 | 00000120 00 00 01 6f 6b 00 00 00 00 00 00 00 f0 3f 00 |...ok........?.| 20 | -------------------------------------------------------------------------------- /internal/bson/timestamp_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package bson 16 | 17 | import ( 18 | "testing" 19 | 20 | "github.com/AlekSi/pointer" 21 | ) 22 | 23 | var timestampTestCases = []testCase{{ 24 | name: "one", 25 | v: pointer.To(timestampType(1)), 26 | b: []byte{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 27 | }, { 28 | name: "zero", 29 | v: pointer.To(timestampType(0)), 30 | b: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 31 | }, { 32 | name: "EOF", 33 | b: []byte{0x00}, 34 | bErr: `unexpected EOF`, 35 | }} 36 | 37 | func TestTimestamp(t *testing.T) { 38 | t.Parallel() 39 | testBinary(t, timestampTestCases, func() bsontype { return new(timestampType) }) 40 | } 41 | 42 | func FuzzTimestamp(f *testing.F) { 43 | fuzzBinary(f, timestampTestCases, func() bsontype { return new(timestampType) }) 44 | } 45 | 46 | func BenchmarkTimestamp(b *testing.B) { 47 | benchmark(b, timestampTestCases, func() bsontype { return new(timestampType) }) 48 | } 49 | -------------------------------------------------------------------------------- /internal/fjson/int32_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package fjson 16 | 17 | import ( 18 | "math" 19 | "testing" 20 | 21 | "github.com/AlekSi/pointer" 22 | ) 23 | 24 | var int32TestCases = []testCase{{ 25 | name: "42", 26 | v: pointer.To(int32Type(42)), 27 | j: `42`, 28 | }, { 29 | name: "zero", 30 | v: pointer.To(int32Type(0)), 31 | j: `0`, 32 | }, { 33 | name: "max int32", 34 | v: pointer.To(int32Type(math.MaxInt32)), 35 | j: `2147483647`, 36 | }, { 37 | name: "min int32", 38 | v: pointer.To(int32Type(math.MinInt32)), 39 | j: `-2147483648`, 40 | }} 41 | 42 | func TestInt32(t *testing.T) { 43 | t.Parallel() 44 | testJSON(t, int32TestCases, func() fjsontype { return new(int32Type) }) 45 | } 46 | 47 | func FuzzInt32(f *testing.F) { 48 | fuzzJSON(f, int32TestCases, func() fjsontype { return new(int32Type) }) 49 | } 50 | 51 | func BenchmarkInt32(b *testing.B) { 52 | benchmark(b, int32TestCases, func() fjsontype { return new(int32Type) }) 53 | } 54 | -------------------------------------------------------------------------------- /internal/handlers/tigris/msg_createindexes.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package tigris 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/handlers/common" 21 | "github.com/FerretDB/FerretDB/internal/types" 22 | "github.com/FerretDB/FerretDB/internal/util/must" 23 | "github.com/FerretDB/FerretDB/internal/wire" 24 | ) 25 | 26 | // MsgCreateIndexes implements HandlerInterface. 27 | func (h *Handler) MsgCreateIndexes(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 28 | // TODO https://github.com/FerretDB/FerretDB/issues/78 29 | 30 | document, err := msg.Document() 31 | if err != nil { 32 | return nil, err 33 | } 34 | 35 | common.Ignored(document, h.L, "writeConcern", "commitQuorum", "comment") 36 | 37 | var reply wire.OpMsg 38 | must.NoError(reply.SetSections(wire.OpMsgSection{ 39 | Documents: []*types.Document{must.NotFail(types.NewDocument( 40 | "ok", float64(1), 41 | ))}, 42 | })) 43 | 44 | return &reply, nil 45 | } 46 | -------------------------------------------------------------------------------- /internal/bson/tag_string.go: -------------------------------------------------------------------------------- 1 | // Code generated by "stringer -linecomment -type tag"; DO NOT EDIT. 2 | 3 | package bson 4 | 5 | import "strconv" 6 | 7 | func _() { 8 | // An "invalid array index" compiler error signifies that the constant values have changed. 9 | // Re-run the stringer command to generate them again. 10 | var x [1]struct{} 11 | _ = x[tagDouble-1] 12 | _ = x[tagString-2] 13 | _ = x[tagDocument-3] 14 | _ = x[tagArray-4] 15 | _ = x[tagBinary-5] 16 | _ = x[tagUndefined-6] 17 | _ = x[tagObjectID-7] 18 | _ = x[tagBool-8] 19 | _ = x[tagDateTime-9] 20 | _ = x[tagNull-10] 21 | _ = x[tagRegex-11] 22 | _ = x[tagDBPointer-12] 23 | _ = x[tagJavaScript-13] 24 | _ = x[tagSymbol-14] 25 | _ = x[tagJavaScriptScope-15] 26 | _ = x[tagInt32-16] 27 | _ = x[tagTimestamp-17] 28 | _ = x[tagInt64-18] 29 | _ = x[tagDecimal-19] 30 | _ = x[tagMinKey-255] 31 | _ = x[tagMaxKey-127] 32 | } 33 | 34 | const ( 35 | _tag_name_0 = "DoubleStringDocumentArrayBinaryUndefinedObjectIDBoolDateTimeNullRegexDBPointerJavaScriptSymbolJavaScriptScopeInt32TimestampInt64Decimal" 36 | _tag_name_1 = "MaxKey" 37 | _tag_name_2 = "MinKey" 38 | ) 39 | 40 | var ( 41 | _tag_index_0 = [...]uint8{0, 6, 12, 20, 25, 31, 40, 48, 52, 60, 64, 69, 78, 88, 94, 109, 114, 123, 128, 135} 42 | ) 43 | 44 | func (i tag) String() string { 45 | switch { 46 | case 1 <= i && i <= 19: 47 | i -= 1 48 | return _tag_name_0[_tag_index_0[i]:_tag_index_0[i+1]] 49 | case i == 127: 50 | return _tag_name_1 51 | case i == 255: 52 | return _tag_name_2 53 | default: 54 | return "tag(" + strconv.FormatInt(int64(i), 10) + ")" 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /internal/wire/testdata/import.hex: -------------------------------------------------------------------------------- 1 | 0000 47 01 00 00 07 00 00 00 00 00 00 00 dd 07 00 00 G............... 2 | 0010 00 00 00 00 00 54 00 00 00 02 69 6e 73 65 72 74 .....T....insert 3 | 0020 00 06 00 00 00 61 63 74 6f 72 00 08 6f 72 64 65 .....actor..orde 4 | 0030 72 65 64 00 01 03 77 72 69 74 65 43 6f 6e 63 65 red...writeConce 5 | 0040 72 6e 00 15 00 00 00 02 77 00 09 00 00 00 6d 61 rn......w.....ma 6 | 0050 6a 6f 72 69 74 79 00 00 02 24 64 62 00 07 00 00 jority...$db.... 7 | 0060 00 6d 6f 6e 69 6c 61 00 00 01 dd 00 00 00 64 6f .monila.......do 8 | 0070 63 75 6d 65 6e 74 73 00 69 00 00 00 07 5f 69 64 cuments.i...._id 9 | 0080 00 61 2e c2 80 00 00 00 01 00 00 00 01 10 61 63 .a............ac 10 | 0090 74 6f 72 5f 69 64 00 01 00 00 00 02 66 69 72 73 tor_id......firs 11 | 00a0 74 5f 6e 61 6d 65 00 09 00 00 00 50 45 4e 45 4c t_name.....PENEL 12 | 00b0 4f 50 45 00 02 6c 61 73 74 5f 6e 61 6d 65 00 08 OPE..last_name.. 13 | 00c0 00 00 00 47 55 49 4e 45 53 53 00 09 6c 61 73 74 ...GUINESS..last 14 | 00d0 5f 75 70 64 61 74 65 00 28 18 33 48 70 01 00 00 _update.(.3Hp... 15 | 00e0 00 66 00 00 00 07 5f 69 64 00 61 2e c2 80 00 00 .f...._id.a..... 16 | 00f0 00 02 00 00 00 02 10 61 63 74 6f 72 5f 69 64 00 .......actor_id. 17 | 0100 02 00 00 00 02 66 69 72 73 74 5f 6e 61 6d 65 00 .....first_name. 18 | 0110 05 00 00 00 4e 49 43 4b 00 02 6c 61 73 74 5f 6e ....NICK..last_n 19 | 0120 61 6d 65 00 09 00 00 00 57 41 48 4c 42 45 52 47 ame.....WAHLBERG 20 | 0130 00 09 6c 61 73 74 5f 75 70 64 61 74 65 00 28 18 ..last_update.(. 21 | 0140 33 48 70 01 00 00 00 3Hp.... 22 | -------------------------------------------------------------------------------- /internal/util/testutil/hexdump.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package testutil 16 | 17 | import ( 18 | "os" 19 | "path/filepath" 20 | "testing" 21 | 22 | "github.com/stretchr/testify/require" 23 | 24 | "github.com/FerretDB/FerretDB/internal/util/hex" 25 | "github.com/FerretDB/FerretDB/internal/util/must" 26 | ) 27 | 28 | // ParseDump parses string to bytes, in tests. 29 | func ParseDump(tb testing.TB, s string) []byte { 30 | tb.Helper() 31 | 32 | b, err := hex.ParseDump(s) 33 | require.NoError(tb, err) 34 | return b 35 | } 36 | 37 | // ParseDumpFile parses file input to bytes, in tests. 38 | func ParseDumpFile(tb testing.TB, path ...string) []byte { 39 | tb.Helper() 40 | 41 | b, err := os.ReadFile(filepath.Join(path...)) 42 | require.NoError(tb, err) 43 | return ParseDump(tb, string(b)) 44 | } 45 | 46 | // MustParseDumpFile panics if fails to parse file input to byte array. 47 | func MustParseDumpFile(path ...string) []byte { 48 | b := must.NotFail(os.ReadFile(filepath.Join(path...))) 49 | return must.NotFail(hex.ParseDump(string(b))) 50 | } 51 | -------------------------------------------------------------------------------- /internal/bson/string_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package bson 16 | 17 | import ( 18 | "testing" 19 | 20 | "github.com/AlekSi/pointer" 21 | ) 22 | 23 | var stringTestCases = []testCase{{ 24 | name: "foo", 25 | v: pointer.To(stringType("foo")), 26 | b: []byte{0x04, 0x00, 0x00, 0x00, 0x66, 0x6f, 0x6f, 0x00}, 27 | }, { 28 | name: "empty", 29 | v: pointer.To(stringType("")), 30 | b: []byte{0x01, 0x00, 0x00, 0x00, 0x00}, 31 | }, { 32 | name: "zero", 33 | v: pointer.To(stringType("\x00")), 34 | b: []byte{0x02, 0x00, 0x00, 0x00, 0x00, 0x00}, 35 | }, { 36 | name: "EOF", 37 | b: []byte{0x00}, 38 | bErr: `unexpected EOF`, 39 | }} 40 | 41 | func TestString(t *testing.T) { 42 | t.Parallel() 43 | testBinary(t, stringTestCases, func() bsontype { return new(stringType) }) 44 | } 45 | 46 | func FuzzString(f *testing.F) { 47 | fuzzBinary(f, stringTestCases, func() bsontype { return new(stringType) }) 48 | } 49 | 50 | func BenchmarkString(b *testing.B) { 51 | benchmark(b, stringTestCases, func() bsontype { return new(stringType) }) 52 | } 53 | -------------------------------------------------------------------------------- /integration/shareddata/shareddata.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package shareddata 16 | 17 | import ( 18 | "go.mongodb.org/mongo-driver/bson" 19 | "golang.org/x/exp/constraints" 20 | "golang.org/x/exp/maps" 21 | "golang.org/x/exp/slices" 22 | ) 23 | 24 | // Provider is implemented by shared data sets that provide documents. 25 | type Provider interface { 26 | // Docs returns shared data documents. All calls should return the same data. 27 | Docs() []bson.D 28 | } 29 | 30 | // Values stores shared data documents as {"_id": key, "value": value} documents. 31 | type Values[idType constraints.Ordered] struct { 32 | data map[idType]any 33 | } 34 | 35 | // Docs implement Provider interface. 36 | func (values *Values[idType]) Docs() []bson.D { 37 | ids := maps.Keys(values.data) 38 | slices.Sort(ids) 39 | 40 | res := make([]bson.D, 0, len(values.data)) 41 | for _, id := range ids { 42 | res = append(res, bson.D{{"_id", id}, {"value", values.data[id]}}) 43 | } 44 | 45 | return res 46 | } 47 | 48 | // check interfaces 49 | var ( 50 | _ Provider = (*Values[string])(nil) 51 | ) 52 | -------------------------------------------------------------------------------- /internal/handlers/common/msg_connectionstatus.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package common 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/types" 21 | "github.com/FerretDB/FerretDB/internal/util/lazyerrors" 22 | "github.com/FerretDB/FerretDB/internal/util/must" 23 | "github.com/FerretDB/FerretDB/internal/wire" 24 | ) 25 | 26 | // MsgConnectionStatus is a common implementation of the connectionStatus command. 27 | func MsgConnectionStatus(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 28 | var reply wire.OpMsg 29 | err := reply.SetSections(wire.OpMsgSection{ 30 | Documents: []*types.Document{must.NotFail(types.NewDocument( 31 | "authInfo", must.NotFail(types.NewDocument( 32 | "authenticatedUsers", must.NotFail(types.NewArray()), 33 | "authenticatedUserRoles", must.NotFail(types.NewArray()), 34 | "authenticatedUserPrivileges", must.NotFail(types.NewArray()), 35 | )), 36 | "ok", float64(1), 37 | ))}, 38 | }) 39 | if err != nil { 40 | return nil, lazyerrors.Error(err) 41 | } 42 | 43 | return &reply, nil 44 | } 45 | -------------------------------------------------------------------------------- /internal/handlers/pg/msg_createindexes.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package pg 16 | 17 | import ( 18 | "context" 19 | 20 | "github.com/FerretDB/FerretDB/internal/handlers/common" 21 | "github.com/FerretDB/FerretDB/internal/types" 22 | "github.com/FerretDB/FerretDB/internal/util/lazyerrors" 23 | "github.com/FerretDB/FerretDB/internal/util/must" 24 | "github.com/FerretDB/FerretDB/internal/wire" 25 | ) 26 | 27 | // MsgCreateIndexes implements HandlerInterface. 28 | func (h *Handler) MsgCreateIndexes(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 29 | // TODO https://github.com/FerretDB/FerretDB/issues/78 30 | 31 | document, err := msg.Document() 32 | if err != nil { 33 | return nil, err 34 | } 35 | 36 | common.Ignored(document, h.l, "writeConcern", "commitQuorum", "comment") 37 | 38 | var reply wire.OpMsg 39 | err = reply.SetSections(wire.OpMsgSection{ 40 | Documents: []*types.Document{must.NotFail(types.NewDocument( 41 | "ok", float64(1), 42 | ))}, 43 | }) 44 | if err != nil { 45 | return nil, lazyerrors.Error(err) 46 | } 47 | 48 | return &reply, nil 49 | } 50 | -------------------------------------------------------------------------------- /internal/fjson/int64_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package fjson 16 | 17 | import ( 18 | "math" 19 | "testing" 20 | 21 | "github.com/AlekSi/pointer" 22 | ) 23 | 24 | var int64TestCases = []testCase{{ 25 | name: "42", 26 | v: pointer.To(int64Type(42)), 27 | j: `{"$l":"42"}`, 28 | }, { 29 | name: "zero", 30 | v: pointer.To(int64Type(0)), 31 | j: `{"$l":"0"}`, 32 | }, { 33 | name: "max int64", 34 | v: pointer.To(int64Type(math.MaxInt64)), 35 | j: `{"$l":"9223372036854775807"}`, 36 | }, { 37 | name: "min int64", 38 | v: pointer.To(int64Type(math.MinInt64)), 39 | j: `{"$l":"-9223372036854775808"}`, 40 | }, { 41 | name: "EOF", 42 | j: `{`, 43 | jErr: `unexpected EOF`, 44 | }} 45 | 46 | func TestInt64(t *testing.T) { 47 | t.Parallel() 48 | testJSON(t, int64TestCases, func() fjsontype { return new(int64Type) }) 49 | } 50 | 51 | func FuzzInt64(f *testing.F) { 52 | fuzzJSON(f, int64TestCases, func() fjsontype { return new(int64Type) }) 53 | } 54 | 55 | func BenchmarkInt64(b *testing.B) { 56 | benchmark(b, int64TestCases, func() fjsontype { return new(int64Type) }) 57 | } 58 | -------------------------------------------------------------------------------- /internal/bson/testdata/handshake1.hex: -------------------------------------------------------------------------------- 1 | 00000000 4d 01 00 00 08 69 73 6d 61 73 74 65 72 00 01 03 |M....ismaster...| 2 | 00000010 63 6c 69 65 6e 74 00 08 01 00 00 03 64 72 69 76 |client......driv| 3 | 00000020 65 72 00 30 00 00 00 02 6e 61 6d 65 00 07 00 00 |er.0....name....| 4 | 00000030 00 6e 6f 64 65 6a 73 00 02 76 65 72 73 69 6f 6e |.nodejs..version| 5 | 00000040 00 0d 00 00 00 34 2e 30 2e 30 2d 62 65 74 61 2e |.....4.0.0-beta.| 6 | 00000050 36 00 00 03 6f 73 00 51 00 00 00 02 74 79 70 65 |6...os.Q....type| 7 | 00000060 00 07 00 00 00 44 61 72 77 69 6e 00 02 6e 61 6d |.....Darwin..nam| 8 | 00000070 65 00 07 00 00 00 64 61 72 77 69 6e 00 02 61 72 |e.....darwin..ar| 9 | 00000080 63 68 69 74 65 63 74 75 72 65 00 04 00 00 00 78 |chitecture.....x| 10 | 00000090 36 34 00 02 76 65 72 73 69 6f 6e 00 07 00 00 00 |64..version.....| 11 | 000000a0 32 30 2e 36 2e 30 00 00 02 70 6c 61 74 66 6f 72 |20.6.0...platfor| 12 | 000000b0 6d 00 3e 00 00 00 4e 6f 64 65 2e 6a 73 20 76 31 |m.>...Node.js v1| 13 | 000000c0 34 2e 31 37 2e 33 2c 20 4c 45 20 28 75 6e 69 66 |4.17.3, LE (unif| 14 | 000000d0 69 65 64 29 7c 4e 6f 64 65 2e 6a 73 20 76 31 34 |ied)|Node.js v14| 15 | 000000e0 2e 31 37 2e 33 2c 20 4c 45 20 28 75 6e 69 66 69 |.17.3, LE (unifi| 16 | 000000f0 65 64 29 00 03 61 70 70 6c 69 63 61 74 69 6f 6e |ed)..application| 17 | 00000100 00 1d 00 00 00 02 6e 61 6d 65 00 0e 00 00 00 6d |......name.....m| 18 | 00000110 6f 6e 67 6f 73 68 20 31 2e 30 2e 31 00 00 00 04 |ongosh 1.0.1....| 19 | 00000120 63 6f 6d 70 72 65 73 73 69 6f 6e 00 11 00 00 00 |compression.....| 20 | 00000130 02 30 00 05 00 00 00 6e 6f 6e 65 00 00 08 6c 6f |.0.....none...lo| 21 | 00000140 61 64 42 61 6c 61 6e 63 65 64 00 00 00 |adBalanced...| 22 | -------------------------------------------------------------------------------- /internal/bson/testdata/handshake2.hex: -------------------------------------------------------------------------------- 1 | 00000000 4d 01 00 00 08 69 73 6d 61 73 74 65 72 00 01 03 |M....ismaster...| 2 | 00000010 63 6c 69 65 6e 74 00 08 01 00 00 03 64 72 69 76 |client......driv| 3 | 00000020 65 72 00 30 00 00 00 02 6e 61 6d 65 00 07 00 00 |er.0....name....| 4 | 00000030 00 6e 6f 64 65 6a 73 00 02 76 65 72 73 69 6f 6e |.nodejs..version| 5 | 00000040 00 0d 00 00 00 34 2e 30 2e 30 2d 62 65 74 61 2e |.....4.0.0-beta.| 6 | 00000050 36 00 00 03 6f 73 00 51 00 00 00 02 74 79 70 65 |6...os.Q....type| 7 | 00000060 00 07 00 00 00 44 61 72 77 69 6e 00 02 6e 61 6d |.....Darwin..nam| 8 | 00000070 65 00 07 00 00 00 64 61 72 77 69 6e 00 02 61 72 |e.....darwin..ar| 9 | 00000080 63 68 69 74 65 63 74 75 72 65 00 04 00 00 00 78 |chitecture.....x| 10 | 00000090 36 34 00 02 76 65 72 73 69 6f 6e 00 07 00 00 00 |64..version.....| 11 | 000000a0 32 30 2e 36 2e 30 00 00 02 70 6c 61 74 66 6f 72 |20.6.0...platfor| 12 | 000000b0 6d 00 3e 00 00 00 4e 6f 64 65 2e 6a 73 20 76 31 |m.>...Node.js v1| 13 | 000000c0 34 2e 31 37 2e 33 2c 20 4c 45 20 28 75 6e 69 66 |4.17.3, LE (unif| 14 | 000000d0 69 65 64 29 7c 4e 6f 64 65 2e 6a 73 20 76 31 34 |ied)|Node.js v14| 15 | 000000e0 2e 31 37 2e 33 2c 20 4c 45 20 28 75 6e 69 66 69 |.17.3, LE (unifi| 16 | 000000f0 65 64 29 00 03 61 70 70 6c 69 63 61 74 69 6f 6e |ed)..application| 17 | 00000100 00 1d 00 00 00 02 6e 61 6d 65 00 0e 00 00 00 6d |......name.....m| 18 | 00000110 6f 6e 67 6f 73 68 20 31 2e 30 2e 31 00 00 00 04 |ongosh 1.0.1....| 19 | 00000120 63 6f 6d 70 72 65 73 73 69 6f 6e 00 11 00 00 00 |compression.....| 20 | 00000130 02 30 00 05 00 00 00 6e 6f 6e 65 00 00 08 6c 6f |.0.....none...lo| 21 | 00000140 61 64 42 61 6c 61 6e 63 65 64 00 00 00 |adBalanced...| 22 | -------------------------------------------------------------------------------- /integration/delete_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package integration 16 | 17 | import ( 18 | "testing" 19 | 20 | "github.com/stretchr/testify/assert" 21 | "github.com/stretchr/testify/require" 22 | "go.mongodb.org/mongo-driver/bson" 23 | 24 | "github.com/FerretDB/FerretDB/integration/shareddata" 25 | ) 26 | 27 | // TestDeleteSimple checks simple cases of doc deletion. 28 | func TestDeleteSimple(t *testing.T) { 29 | t.Parallel() 30 | ctx, collection := setup(t, shareddata.Scalars, shareddata.Composites) 31 | 32 | for name, tc := range map[string]struct { 33 | collection string 34 | expectedCount int64 35 | }{ 36 | "DeleteOne": { 37 | collection: collection.Name(), 38 | expectedCount: 1, 39 | }, 40 | "DeleteFromNonExistingCollection": { 41 | collection: "doesnotexist", 42 | expectedCount: 0, 43 | }, 44 | } { 45 | name, tc := name, tc 46 | t.Run(name, func(t *testing.T) { 47 | t.Parallel() 48 | 49 | cursor, err := collection.Database().Collection(tc.collection).DeleteOne(ctx, bson.D{}) 50 | require.NoError(t, err) 51 | assert.Equal(t, tc.expectedCount, cursor.DeletedCount) 52 | }) 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /internal/bson/int32_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package bson 16 | 17 | import ( 18 | "math" 19 | "testing" 20 | 21 | "github.com/AlekSi/pointer" 22 | ) 23 | 24 | var int32TestCases = []testCase{{ 25 | name: "42", 26 | v: pointer.To(int32Type(42)), 27 | b: []byte{0x2a, 0x00, 0x00, 0x00}, 28 | }, { 29 | name: "zero", 30 | v: pointer.To(int32Type(0)), 31 | b: []byte{0x00, 0x00, 0x00, 0x00}, 32 | }, { 33 | name: "max int32", 34 | v: pointer.To(int32Type(math.MaxInt32)), 35 | b: []byte{0xff, 0xff, 0xff, 0x7f}, 36 | }, { 37 | name: "min int32", 38 | v: pointer.To(int32Type(math.MinInt32)), 39 | b: []byte{0x00, 0x00, 0x00, 0x80}, 40 | }, { 41 | name: "EOF", 42 | b: []byte{0x00}, 43 | bErr: `unexpected EOF`, 44 | }} 45 | 46 | func TestInt32(t *testing.T) { 47 | t.Parallel() 48 | testBinary(t, int32TestCases, func() bsontype { return new(int32Type) }) 49 | } 50 | 51 | func FuzzInt32(f *testing.F) { 52 | fuzzBinary(f, int32TestCases, func() bsontype { return new(int32Type) }) 53 | } 54 | 55 | func BenchmarkInt32(b *testing.B) { 56 | benchmark(b, int32TestCases, func() bsontype { return new(int32Type) }) 57 | } 58 | -------------------------------------------------------------------------------- /internal/bson/tag.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package bson 16 | 17 | //go:generate ../../bin/stringer -linecomment -type tag 18 | 19 | type tag byte 20 | 21 | const ( 22 | tagDouble = tag(0x01) // Double 23 | tagString = tag(0x02) // String 24 | tagDocument = tag(0x03) // Document 25 | tagArray = tag(0x04) // Array 26 | tagBinary = tag(0x05) // Binary 27 | tagUndefined = tag(0x06) // Undefined 28 | tagObjectID = tag(0x07) // ObjectID 29 | tagBool = tag(0x08) // Bool 30 | tagDateTime = tag(0x09) // DateTime 31 | tagNull = tag(0x0a) // Null 32 | tagRegex = tag(0x0b) // Regex 33 | tagDBPointer = tag(0x0c) // DBPointer 34 | tagJavaScript = tag(0x0d) // JavaScript 35 | tagSymbol = tag(0x0e) // Symbol 36 | tagJavaScriptScope = tag(0x0f) // JavaScriptScope 37 | tagInt32 = tag(0x10) // Int32 38 | tagTimestamp = tag(0x11) // Timestamp 39 | tagInt64 = tag(0x12) // Int64 40 | tagDecimal = tag(0x13) // Decimal 41 | tagMinKey = tag(0xff) // MinKey 42 | tagMaxKey = tag(0x7f) // MaxKey 43 | ) 44 | -------------------------------------------------------------------------------- /internal/handlers/tigris/msg_hello.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package tigris 16 | 17 | import ( 18 | "context" 19 | "time" 20 | 21 | "github.com/FerretDB/FerretDB/internal/types" 22 | "github.com/FerretDB/FerretDB/internal/util/must" 23 | "github.com/FerretDB/FerretDB/internal/wire" 24 | ) 25 | 26 | // MsgHello implements HandlerInterface. 27 | func (h *Handler) MsgHello(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 28 | if _, err := h.driver.Info(ctx); err != nil { 29 | return nil, err 30 | } 31 | 32 | var reply wire.OpMsg 33 | must.NoError(reply.SetSections(wire.OpMsgSection{ 34 | Documents: []*types.Document{must.NotFail(types.NewDocument( 35 | "isWritablePrimary", true, 36 | // topologyVersion 37 | "maxBsonObjectSize", int32(types.MaxDocumentLen), 38 | "maxMessageSizeBytes", int32(wire.MaxMsgLen), 39 | "maxWriteBatchSize", int32(100000), 40 | "localTime", time.Now(), 41 | // logicalSessionTimeoutMinutes 42 | // connectionId 43 | "minWireVersion", int32(13), 44 | "maxWireVersion", int32(13), 45 | "readOnly", false, 46 | "ok", float64(1), 47 | ))}, 48 | })) 49 | 50 | return &reply, nil 51 | } 52 | -------------------------------------------------------------------------------- /internal/handlers/common/msg_setfreemonitoring.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package common 16 | 17 | import ( 18 | "context" 19 | "fmt" 20 | 21 | "github.com/FerretDB/FerretDB/internal/util/lazyerrors" 22 | "github.com/FerretDB/FerretDB/internal/wire" 23 | ) 24 | 25 | // MsgSetFreeMonitoring is a common implementation of the setFreeMonitoring command. 26 | func MsgSetFreeMonitoring(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 27 | document, err := msg.Document() 28 | if err != nil { 29 | return nil, lazyerrors.Error(err) 30 | } 31 | 32 | command := document.Command() 33 | var action string 34 | if action, err = GetRequiredParam[string](document, "action"); err != nil { 35 | return nil, err 36 | } 37 | 38 | switch action { 39 | case "enable", "disable": 40 | return nil, NewErrorMsg( 41 | ErrFreeMonitoringDisabled, 42 | "Free Monitoring has been disabled via the command-line and/or config file", 43 | ) 44 | default: 45 | return nil, NewErrorMsg( 46 | ErrBadValue, 47 | fmt.Sprintf( 48 | "Enumeration value '%s' for field '%s' is not a valid value.", 49 | action, 50 | command+".action", 51 | ), 52 | ) 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /internal/handlers/dummy/dummy.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // Package dummy provides a basic handler implementation. 16 | // 17 | // The whole package can be copied to start a new handler implementation. 18 | package dummy 19 | 20 | import ( 21 | "github.com/FerretDB/FerretDB/internal/handlers" 22 | "github.com/FerretDB/FerretDB/internal/handlers/common" 23 | ) 24 | 25 | // notImplemented returns error for stub command handlers. 26 | func notImplemented(command string) error { 27 | return common.NewErrorMsg(common.ErrNotImplemented, "I'm a stub, not a real handler for "+command) 28 | } 29 | 30 | // Handler implements handlers.Interface by stubbing all methods except the following handler-independent commands: 31 | // 32 | // * buildInfo; 33 | // * connectionStatus; 34 | // * debugError; 35 | // * getCmdLineOpts; 36 | // * hostInfo; 37 | // * listCommands. 38 | type Handler struct{} 39 | 40 | // New returns a new handler. 41 | func New() (handlers.Interface, error) { 42 | return new(Handler), nil 43 | } 44 | 45 | // Close implements handlers.Interface. 46 | func (h *Handler) Close() {} 47 | 48 | // check interfaces 49 | var ( 50 | _ handlers.Interface = (*Handler)(nil) 51 | ) 52 | -------------------------------------------------------------------------------- /internal/fjson/bool.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package fjson 16 | 17 | import ( 18 | "bytes" 19 | "encoding/json" 20 | 21 | "github.com/FerretDB/FerretDB/internal/util/lazyerrors" 22 | ) 23 | 24 | // boolType represents BSON Boolean type. 25 | type boolType bool 26 | 27 | // fjsontype implements fjsontype interface. 28 | func (b *boolType) fjsontype() {} 29 | 30 | // UnmarshalJSON implements fjsontype interface. 31 | func (b *boolType) UnmarshalJSON(data []byte) error { 32 | if bytes.Equal(data, []byte("null")) { 33 | panic("null data") 34 | } 35 | 36 | r := bytes.NewReader(data) 37 | dec := json.NewDecoder(r) 38 | 39 | var o bool 40 | if err := dec.Decode(&o); err != nil { 41 | return lazyerrors.Error(err) 42 | } 43 | if err := checkConsumed(dec, r); err != nil { 44 | return lazyerrors.Error(err) 45 | } 46 | 47 | *b = boolType(o) 48 | return nil 49 | } 50 | 51 | // MarshalJSON implements fjsontype interface. 52 | func (b *boolType) MarshalJSON() ([]byte, error) { 53 | res, err := json.Marshal(bool(*b)) 54 | if err != nil { 55 | return nil, lazyerrors.Error(err) 56 | } 57 | return res, nil 58 | } 59 | 60 | // check interfaces 61 | var ( 62 | _ fjsontype = (*boolType)(nil) 63 | ) 64 | -------------------------------------------------------------------------------- /internal/tjson/bool.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package tjson 16 | 17 | import ( 18 | "bytes" 19 | "encoding/json" 20 | 21 | "github.com/FerretDB/FerretDB/internal/util/lazyerrors" 22 | ) 23 | 24 | // boolType represents BSON Boolean type. 25 | type boolType bool 26 | 27 | // tjsontype implements tjsontype interface. 28 | func (b *boolType) tjsontype() {} 29 | 30 | // UnmarshalJSON implements the json.Unmarshaler interface. 31 | func (b *boolType) UnmarshalJSON(data []byte) error { 32 | if bytes.Equal(data, []byte("null")) { 33 | panic("null data") 34 | } 35 | 36 | r := bytes.NewReader(data) 37 | dec := json.NewDecoder(r) 38 | 39 | var o bool 40 | if err := dec.Decode(&o); err != nil { 41 | return lazyerrors.Error(err) 42 | } 43 | if err := checkConsumed(dec, r); err != nil { 44 | return lazyerrors.Error(err) 45 | } 46 | 47 | *b = boolType(o) 48 | return nil 49 | } 50 | 51 | // MarshalJSON implements tjsontype interface. 52 | func (b *boolType) MarshalJSON() ([]byte, error) { 53 | res, err := json.Marshal(bool(*b)) 54 | if err != nil { 55 | return nil, lazyerrors.Error(err) 56 | } 57 | return res, nil 58 | } 59 | 60 | // check interfaces 61 | var ( 62 | _ tjsontype = (*boolType)(nil) 63 | ) 64 | -------------------------------------------------------------------------------- /internal/fjson/int32.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package fjson 16 | 17 | import ( 18 | "bytes" 19 | "encoding/json" 20 | 21 | "github.com/FerretDB/FerretDB/internal/util/lazyerrors" 22 | ) 23 | 24 | // int32Type represents BSON 32-bit integer type. 25 | type int32Type int32 26 | 27 | // fjsontype implements fjsontype interface. 28 | func (i *int32Type) fjsontype() {} 29 | 30 | // UnmarshalJSON implements fjsontype interface. 31 | func (i *int32Type) UnmarshalJSON(data []byte) error { 32 | if bytes.Equal(data, []byte("null")) { 33 | panic("null data") 34 | } 35 | 36 | r := bytes.NewReader(data) 37 | dec := json.NewDecoder(r) 38 | 39 | var o int32 40 | if err := dec.Decode(&o); err != nil { 41 | return lazyerrors.Error(err) 42 | } 43 | if err := checkConsumed(dec, r); err != nil { 44 | return lazyerrors.Error(err) 45 | } 46 | 47 | *i = int32Type(o) 48 | return nil 49 | } 50 | 51 | // MarshalJSON implements fjsontype interface. 52 | func (i *int32Type) MarshalJSON() ([]byte, error) { 53 | res, err := json.Marshal(int32(*i)) 54 | if err != nil { 55 | return nil, lazyerrors.Error(err) 56 | } 57 | return res, nil 58 | } 59 | 60 | // check interfaces 61 | var ( 62 | _ fjsontype = (*int32Type)(nil) 63 | ) 64 | -------------------------------------------------------------------------------- /internal/handlers/pg/pg.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // Package pg provides PostgreSQL handler. 16 | package pg 17 | 18 | import ( 19 | "time" 20 | 21 | "go.uber.org/zap" 22 | 23 | "github.com/FerretDB/FerretDB/internal/handlers" 24 | "github.com/FerretDB/FerretDB/internal/handlers/pg/pgdb" 25 | ) 26 | 27 | // Handler implements handlers.Interface on top of PostgreSQL. 28 | type Handler struct { 29 | // TODO replace those fields with 30 | // opts *NewOpts 31 | pgPool *pgdb.Pool 32 | l *zap.Logger 33 | peerAddr string 34 | startTime time.Time 35 | } 36 | 37 | // NewOpts represents handler configuration. 38 | type NewOpts struct { 39 | PgPool *pgdb.Pool 40 | L *zap.Logger 41 | PeerAddr string 42 | StartTime time.Time 43 | } 44 | 45 | // New returns a new handler. 46 | func New(opts *NewOpts) (handlers.Interface, error) { 47 | h := &Handler{ 48 | pgPool: opts.PgPool, 49 | l: opts.L, 50 | peerAddr: opts.PeerAddr, 51 | startTime: opts.StartTime, 52 | } 53 | return h, nil 54 | } 55 | 56 | // Close implements HandlerInterface. 57 | func (h *Handler) Close() { 58 | h.pgPool.Close() 59 | } 60 | 61 | // check interfaces 62 | var ( 63 | _ handlers.Interface = (*Handler)(nil) 64 | ) 65 | -------------------------------------------------------------------------------- /internal/tjson/int32.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package tjson 16 | 17 | import ( 18 | "bytes" 19 | "encoding/json" 20 | 21 | "github.com/FerretDB/FerretDB/internal/util/lazyerrors" 22 | ) 23 | 24 | // int32Type represents BSON 32-bit integer type. 25 | type int32Type int32 26 | 27 | // tjsontype implements tjsontype interface. 28 | func (i *int32Type) tjsontype() {} 29 | 30 | // UnmarshalJSON implements the json.Unmarshaler interface. 31 | func (i *int32Type) UnmarshalJSON(data []byte) error { 32 | if bytes.Equal(data, []byte("null")) { 33 | panic("null data") 34 | } 35 | 36 | r := bytes.NewReader(data) 37 | dec := json.NewDecoder(r) 38 | 39 | var o int32 40 | if err := dec.Decode(&o); err != nil { 41 | return lazyerrors.Error(err) 42 | } 43 | if err := checkConsumed(dec, r); err != nil { 44 | return lazyerrors.Error(err) 45 | } 46 | 47 | *i = int32Type(o) 48 | return nil 49 | } 50 | 51 | // MarshalJSON implements tjsontype interface. 52 | func (i *int32Type) MarshalJSON() ([]byte, error) { 53 | res, err := json.Marshal(int32(*i)) 54 | if err != nil { 55 | return nil, lazyerrors.Error(err) 56 | } 57 | return res, nil 58 | } 59 | 60 | // check interfaces 61 | var ( 62 | _ tjsontype = (*int32Type)(nil) 63 | ) 64 | -------------------------------------------------------------------------------- /internal/tjson/int64.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package tjson 16 | 17 | import ( 18 | "bytes" 19 | "encoding/json" 20 | 21 | "github.com/FerretDB/FerretDB/internal/util/lazyerrors" 22 | ) 23 | 24 | // int64Type represents BSON 64-bit integer type. 25 | type int64Type int64 26 | 27 | // tjsontype implements tjsontype interface. 28 | func (i *int64Type) tjsontype() {} 29 | 30 | // UnmarshalJSON implements the json.Unmarshaler interface. 31 | func (i *int64Type) UnmarshalJSON(data []byte) error { 32 | if bytes.Equal(data, []byte("null")) { 33 | panic("null data") 34 | } 35 | 36 | r := bytes.NewReader(data) 37 | dec := json.NewDecoder(r) 38 | 39 | var o int64 40 | if err := dec.Decode(&o); err != nil { 41 | return lazyerrors.Error(err) 42 | } 43 | if err := checkConsumed(dec, r); err != nil { 44 | return lazyerrors.Error(err) 45 | } 46 | 47 | *i = int64Type(o) 48 | return nil 49 | } 50 | 51 | // MarshalJSON implements tjsontype interface. 52 | func (i *int64Type) MarshalJSON() ([]byte, error) { 53 | res, err := json.Marshal(int64(*i)) 54 | if err != nil { 55 | return nil, lazyerrors.Error(err) 56 | } 57 | return res, nil 58 | } 59 | 60 | // check interfaces 61 | var ( 62 | _ tjsontype = (*int64Type)(nil) 63 | ) 64 | -------------------------------------------------------------------------------- /internal/types/binary.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package types 16 | 17 | //go:generate ../../bin/stringer -linecomment -type BinarySubtype 18 | 19 | // BinarySubtype represents BSON Binary's subtype. 20 | type BinarySubtype byte 21 | 22 | const ( 23 | // BinaryGeneric represents a BSON generic binary subtype. 24 | BinaryGeneric = BinarySubtype(0x00) // generic 25 | 26 | // BinaryFunction represents a BSON function. 27 | BinaryFunction = BinarySubtype(0x01) // function 28 | 29 | // BinaryGenericOld represents a BSON generic-old. 30 | BinaryGenericOld = BinarySubtype(0x02) // generic-old 31 | 32 | // BinaryUUIDOld represents a BSON UUID old. 33 | BinaryUUIDOld = BinarySubtype(0x03) // uuid-old 34 | 35 | // BinaryUUID represents a BSON UUID. 36 | BinaryUUID = BinarySubtype(0x04) // uuid 37 | 38 | // BinaryMD5 represents a BSON md5. 39 | BinaryMD5 = BinarySubtype(0x05) // md5 40 | 41 | // BinaryEncrypted represents a Encrypted BSON value. 42 | BinaryEncrypted = BinarySubtype(0x06) // encrypted 43 | 44 | // BinaryUser represents a User defined. 45 | BinaryUser = BinarySubtype(0x80) // user 46 | ) 47 | 48 | // Binary represents BSON type Binary. 49 | type Binary struct { 50 | Subtype BinarySubtype 51 | B []byte 52 | } 53 | -------------------------------------------------------------------------------- /internal/bson/int64_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package bson 16 | 17 | import ( 18 | "math" 19 | "testing" 20 | 21 | "github.com/AlekSi/pointer" 22 | ) 23 | 24 | var int64TestCases = []testCase{{ 25 | name: "42", 26 | v: pointer.To(int64Type(42)), 27 | b: []byte{0x2a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 28 | }, { 29 | name: "zero", 30 | v: pointer.To(int64Type(0)), 31 | b: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 32 | }, { 33 | name: "max int64", 34 | v: pointer.To(int64Type(math.MaxInt64)), 35 | b: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, 36 | }, { 37 | name: "min int64", 38 | v: pointer.To(int64Type(math.MinInt64)), 39 | b: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80}, 40 | }, { 41 | name: "EOF", 42 | b: []byte{0x00}, 43 | bErr: `unexpected EOF`, 44 | }} 45 | 46 | func TestInt64(t *testing.T) { 47 | t.Parallel() 48 | testBinary(t, int64TestCases, func() bsontype { return new(int64Type) }) 49 | } 50 | 51 | func FuzzInt64(f *testing.F) { 52 | fuzzBinary(f, int64TestCases, func() bsontype { return new(int64Type) }) 53 | } 54 | 55 | func BenchmarkInt64(b *testing.B) { 56 | benchmark(b, int64TestCases, func() bsontype { return new(int64Type) }) 57 | } 58 | -------------------------------------------------------------------------------- /internal/fjson/string.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package fjson 16 | 17 | import ( 18 | "bytes" 19 | "encoding/json" 20 | 21 | "github.com/FerretDB/FerretDB/internal/util/lazyerrors" 22 | ) 23 | 24 | // stringType represents BSON UTF-8 string type. 25 | type stringType string 26 | 27 | // fjsontype implements fjsontype interface. 28 | func (str *stringType) fjsontype() {} 29 | 30 | // UnmarshalJSON implements fjsontype interface. 31 | func (str *stringType) UnmarshalJSON(data []byte) error { 32 | if bytes.Equal(data, []byte("null")) { 33 | panic("null data") 34 | } 35 | 36 | r := bytes.NewReader(data) 37 | dec := json.NewDecoder(r) 38 | 39 | var o string 40 | if err := dec.Decode(&o); err != nil { 41 | return lazyerrors.Error(err) 42 | } 43 | if err := checkConsumed(dec, r); err != nil { 44 | return lazyerrors.Error(err) 45 | } 46 | 47 | *str = stringType(o) 48 | return nil 49 | } 50 | 51 | // MarshalJSON implements fjsontype interface. 52 | func (str *stringType) MarshalJSON() ([]byte, error) { 53 | res, err := json.Marshal(string(*str)) 54 | if err != nil { 55 | return nil, lazyerrors.Error(err) 56 | } 57 | return res, nil 58 | } 59 | 60 | // check interfaces 61 | var ( 62 | _ fjsontype = (*stringType)(nil) 63 | ) 64 | -------------------------------------------------------------------------------- /internal/util/hex/hex.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package hex 16 | 17 | import ( 18 | "bufio" 19 | "encoding/hex" 20 | "strings" 21 | 22 | "github.com/FerretDB/FerretDB/internal/util/lazyerrors" 23 | ) 24 | 25 | // Dump makes a hex dump of byte array. 26 | func Dump(b []byte) string { 27 | return hex.Dump(b) 28 | } 29 | 30 | // ParseDump decodes from hex dump to the byte array. 31 | func ParseDump(s string) ([]byte, error) { 32 | var res []byte 33 | 34 | scanner := bufio.NewScanner(strings.NewReader(strings.TrimSpace(s))) 35 | for scanner.Scan() { 36 | line := strings.TrimSpace(scanner.Text()) 37 | if line == "" { 38 | continue 39 | } 40 | 41 | if line[len(line)-1] == '|' { 42 | // go dump 43 | line = strings.TrimSpace(line[8:60]) 44 | line = strings.Join(strings.Split(line, " "), "") 45 | } else { 46 | // wireshark dump 47 | line = strings.TrimSpace(line[7:54]) 48 | line = strings.Join(strings.Split(line, " "), "") 49 | } 50 | 51 | b, err := hex.DecodeString(line) 52 | if err != nil { 53 | return nil, lazyerrors.Error(err) 54 | } 55 | res = append(res, b...) 56 | } 57 | 58 | if err := scanner.Err(); err != nil { 59 | return nil, lazyerrors.Error(err) 60 | } 61 | 62 | return res, nil 63 | } 64 | -------------------------------------------------------------------------------- /internal/handlers/tigris/msg_ismaster.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package tigris 16 | 17 | import ( 18 | "context" 19 | "time" 20 | 21 | "github.com/FerretDB/FerretDB/internal/types" 22 | "github.com/FerretDB/FerretDB/internal/util/lazyerrors" 23 | "github.com/FerretDB/FerretDB/internal/util/must" 24 | "github.com/FerretDB/FerretDB/internal/wire" 25 | ) 26 | 27 | // MsgIsMaster implements HandlerInterface. 28 | func (h *Handler) MsgIsMaster(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 29 | if _, err := h.driver.Info(ctx); err != nil { 30 | return nil, lazyerrors.Error(err) 31 | } 32 | 33 | var reply wire.OpMsg 34 | must.NoError(reply.SetSections(wire.OpMsgSection{ 35 | Documents: []*types.Document{must.NotFail(types.NewDocument( 36 | "ismaster", true, // only lowercase 37 | // topologyVersion 38 | "maxBsonObjectSize", int32(types.MaxDocumentLen), 39 | "maxMessageSizeBytes", int32(wire.MaxMsgLen), 40 | "maxWriteBatchSize", int32(100000), 41 | "localTime", time.Now(), 42 | // logicalSessionTimeoutMinutes 43 | // connectionId 44 | "minWireVersion", int32(13), 45 | "maxWireVersion", int32(13), 46 | "readOnly", false, 47 | "ok", float64(1), 48 | ))}, 49 | })) 50 | 51 | return &reply, nil 52 | } 53 | -------------------------------------------------------------------------------- /internal/tjson/string.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package tjson 16 | 17 | import ( 18 | "bytes" 19 | "encoding/json" 20 | 21 | "github.com/FerretDB/FerretDB/internal/util/lazyerrors" 22 | ) 23 | 24 | // stringType represents BSON UTF-8 string type. 25 | type stringType string 26 | 27 | // tjsontype implements tjsontype interface. 28 | func (str *stringType) tjsontype() {} 29 | 30 | // UnmarshalJSON implements the json.Unmarshaler interface. 31 | func (str *stringType) UnmarshalJSON(data []byte) error { 32 | if bytes.Equal(data, []byte("null")) { 33 | panic("null data") 34 | } 35 | 36 | r := bytes.NewReader(data) 37 | dec := json.NewDecoder(r) 38 | 39 | var o string 40 | if err := dec.Decode(&o); err != nil { 41 | return lazyerrors.Error(err) 42 | } 43 | if err := checkConsumed(dec, r); err != nil { 44 | return lazyerrors.Error(err) 45 | } 46 | 47 | *str = stringType(o) 48 | return nil 49 | } 50 | 51 | // MarshalJSON implements tjsontype interface. 52 | func (str *stringType) MarshalJSON() ([]byte, error) { 53 | res, err := json.Marshal(string(*str)) 54 | if err != nil { 55 | return nil, lazyerrors.Error(err) 56 | } 57 | return res, nil 58 | } 59 | 60 | // check interfaces 61 | var ( 62 | _ tjsontype = (*stringType)(nil) 63 | ) 64 | -------------------------------------------------------------------------------- /internal/tjson/double.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package tjson 16 | 17 | import ( 18 | "bytes" 19 | "encoding/json" 20 | 21 | "github.com/FerretDB/FerretDB/internal/util/lazyerrors" 22 | ) 23 | 24 | // doubleType represents BSON 64-bit binary floating point type. 25 | type doubleType float64 26 | 27 | // tjsontype implements tjsontype interface. 28 | func (d *doubleType) tjsontype() {} 29 | 30 | // UnmarshalJSON implements the json.Unmarshaler interface. 31 | func (d *doubleType) UnmarshalJSON(data []byte) error { 32 | if bytes.Equal(data, []byte("null")) { 33 | panic("null data") 34 | } 35 | 36 | r := bytes.NewReader(data) 37 | dec := json.NewDecoder(r) 38 | 39 | var o float64 40 | if err := dec.Decode(&o); err != nil { 41 | return lazyerrors.Error(err) 42 | } 43 | if err := checkConsumed(dec, r); err != nil { 44 | return lazyerrors.Error(err) 45 | } 46 | 47 | *d = doubleType(o) 48 | return nil 49 | } 50 | 51 | // MarshalJSON implements tjsontype interface. 52 | func (d *doubleType) MarshalJSON() ([]byte, error) { 53 | res, err := json.Marshal(float64(*d)) 54 | if err != nil { 55 | return nil, lazyerrors.Error(err) 56 | } 57 | return res, nil 58 | } 59 | 60 | // check interfaces 61 | var ( 62 | _ tjsontype = (*doubleType)(nil) 63 | ) 64 | -------------------------------------------------------------------------------- /internal/handlers/pg/msg_hello.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package pg 16 | 17 | import ( 18 | "context" 19 | "time" 20 | 21 | "github.com/FerretDB/FerretDB/internal/types" 22 | "github.com/FerretDB/FerretDB/internal/util/lazyerrors" 23 | "github.com/FerretDB/FerretDB/internal/util/must" 24 | "github.com/FerretDB/FerretDB/internal/wire" 25 | ) 26 | 27 | // MsgHello implements HandlerInterface. 28 | func (h *Handler) MsgHello(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 29 | if err := h.pgPool.Ping(ctx); err != nil { 30 | return nil, err 31 | } 32 | 33 | var reply wire.OpMsg 34 | err := reply.SetSections(wire.OpMsgSection{ 35 | Documents: []*types.Document{must.NotFail(types.NewDocument( 36 | "isWritablePrimary", true, 37 | // topologyVersion 38 | "maxBsonObjectSize", int32(types.MaxDocumentLen), 39 | "maxMessageSizeBytes", int32(wire.MaxMsgLen), 40 | "maxWriteBatchSize", int32(100000), 41 | "localTime", time.Now(), 42 | // logicalSessionTimeoutMinutes 43 | // connectionId 44 | "minWireVersion", int32(13), 45 | "maxWireVersion", int32(13), 46 | "readOnly", false, 47 | "ok", float64(1), 48 | ))}, 49 | }) 50 | if err != nil { 51 | return nil, lazyerrors.Error(err) 52 | } 53 | 54 | return &reply, nil 55 | } 56 | -------------------------------------------------------------------------------- /internal/bson/cstring.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package bson 16 | 17 | import ( 18 | "bufio" 19 | 20 | "github.com/FerretDB/FerretDB/internal/util/lazyerrors" 21 | ) 22 | 23 | // CString represents BSON zero-terminated UTF-8 string type. 24 | type CString string 25 | 26 | func (cstr *CString) bsontype() {} 27 | 28 | // ReadFrom implements bsontype interface. 29 | func (cstr *CString) ReadFrom(r *bufio.Reader) error { 30 | b, err := r.ReadBytes(0) 31 | if err != nil { 32 | return lazyerrors.Errorf("bson.CString.ReadFrom: %w", err) 33 | } 34 | 35 | *cstr = CString(b[:len(b)-1]) 36 | return nil 37 | } 38 | 39 | // WriteTo implements bsontype interface. 40 | func (cstr CString) WriteTo(w *bufio.Writer) error { 41 | v, err := cstr.MarshalBinary() 42 | if err != nil { 43 | return lazyerrors.Errorf("bson.CString.WriteTo: %w", err) 44 | } 45 | 46 | _, err = w.Write(v) 47 | if err != nil { 48 | return lazyerrors.Errorf("bson.CString.WriteTo: %w", err) 49 | } 50 | 51 | return nil 52 | } 53 | 54 | // MarshalBinary implements bsontype interface. 55 | func (cstr CString) MarshalBinary() ([]byte, error) { 56 | b := make([]byte, len(cstr)+1) 57 | copy(b, cstr) 58 | return b, nil 59 | } 60 | 61 | // check interfaces 62 | var ( 63 | _ bsontype = (*CString)(nil) 64 | ) 65 | -------------------------------------------------------------------------------- /internal/handlers/pg/msg_ismaster.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package pg 16 | 17 | import ( 18 | "context" 19 | "time" 20 | 21 | "github.com/FerretDB/FerretDB/internal/types" 22 | "github.com/FerretDB/FerretDB/internal/util/lazyerrors" 23 | "github.com/FerretDB/FerretDB/internal/util/must" 24 | "github.com/FerretDB/FerretDB/internal/wire" 25 | ) 26 | 27 | // MsgIsMaster implements HandlerInterface. 28 | func (h *Handler) MsgIsMaster(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { 29 | if err := h.pgPool.Ping(ctx); err != nil { 30 | return nil, err 31 | } 32 | 33 | var reply wire.OpMsg 34 | err := reply.SetSections(wire.OpMsgSection{ 35 | Documents: []*types.Document{must.NotFail(types.NewDocument( 36 | "ismaster", true, // only lowercase 37 | // topologyVersion 38 | "maxBsonObjectSize", int32(types.MaxDocumentLen), 39 | "maxMessageSizeBytes", int32(wire.MaxMsgLen), 40 | "maxWriteBatchSize", int32(100000), 41 | "localTime", time.Now(), 42 | // logicalSessionTimeoutMinutes 43 | // connectionId 44 | "minWireVersion", int32(13), 45 | "maxWireVersion", int32(13), 46 | "readOnly", false, 47 | "ok", float64(1), 48 | ))}, 49 | }) 50 | if err != nil { 51 | return nil, lazyerrors.Error(err) 52 | } 53 | 54 | return &reply, nil 55 | } 56 | -------------------------------------------------------------------------------- /internal/wire/testdata/handshake1_body.hex: -------------------------------------------------------------------------------- 1 | 00000000 00 00 00 00 61 64 6d 69 6e 2e 24 63 6d 64 00 00 |....admin.$cmd..| 2 | 00000010 00 00 00 ff ff ff ff 4d 01 00 00 08 69 73 6d 61 |.......M....isma| 3 | 00000020 73 74 65 72 00 01 03 63 6c 69 65 6e 74 00 08 01 |ster...client...| 4 | 00000030 00 00 03 64 72 69 76 65 72 00 30 00 00 00 02 6e |...driver.0....n| 5 | 00000040 61 6d 65 00 07 00 00 00 6e 6f 64 65 6a 73 00 02 |ame.....nodejs..| 6 | 00000050 76 65 72 73 69 6f 6e 00 0d 00 00 00 34 2e 30 2e |version.....4.0.| 7 | 00000060 30 2d 62 65 74 61 2e 36 00 00 03 6f 73 00 51 00 |0-beta.6...os.Q.| 8 | 00000070 00 00 02 74 79 70 65 00 07 00 00 00 44 61 72 77 |...type.....Darw| 9 | 00000080 69 6e 00 02 6e 61 6d 65 00 07 00 00 00 64 61 72 |in..name.....dar| 10 | 00000090 77 69 6e 00 02 61 72 63 68 69 74 65 63 74 75 72 |win..architectur| 11 | 000000a0 65 00 04 00 00 00 78 36 34 00 02 76 65 72 73 69 |e.....x64..versi| 12 | 000000b0 6f 6e 00 07 00 00 00 32 30 2e 36 2e 30 00 00 02 |on.....20.6.0...| 13 | 000000c0 70 6c 61 74 66 6f 72 6d 00 3e 00 00 00 4e 6f 64 |platform.>...Nod| 14 | 000000d0 65 2e 6a 73 20 76 31 34 2e 31 37 2e 33 2c 20 4c |e.js v14.17.3, L| 15 | 000000e0 45 20 28 75 6e 69 66 69 65 64 29 7c 4e 6f 64 65 |E (unified)|Node| 16 | 000000f0 2e 6a 73 20 76 31 34 2e 31 37 2e 33 2c 20 4c 45 |.js v14.17.3, LE| 17 | 00000100 20 28 75 6e 69 66 69 65 64 29 00 03 61 70 70 6c | (unified)..appl| 18 | 00000110 69 63 61 74 69 6f 6e 00 1d 00 00 00 02 6e 61 6d |ication......nam| 19 | 00000120 65 00 0e 00 00 00 6d 6f 6e 67 6f 73 68 20 31 2e |e.....mongosh 1.| 20 | 00000130 30 2e 31 00 00 00 04 63 6f 6d 70 72 65 73 73 69 |0.1....compressi| 21 | 00000140 6f 6e 00 11 00 00 00 02 30 00 05 00 00 00 6e 6f |on......0.....no| 22 | 00000150 6e 65 00 00 08 6c 6f 61 64 42 61 6c 61 6e 63 65 |ne...loadBalance| 23 | 00000160 64 00 00 00 |d...| 24 | -------------------------------------------------------------------------------- /internal/wire/testdata/handshake3_body.hex: -------------------------------------------------------------------------------- 1 | 00000000 00 00 00 00 61 64 6d 69 6e 2e 24 63 6d 64 00 00 |....admin.$cmd..| 2 | 00000010 00 00 00 ff ff ff ff 4d 01 00 00 08 69 73 6d 61 |.......M....isma| 3 | 00000020 73 74 65 72 00 01 03 63 6c 69 65 6e 74 00 08 01 |ster...client...| 4 | 00000030 00 00 03 64 72 69 76 65 72 00 30 00 00 00 02 6e |...driver.0....n| 5 | 00000040 61 6d 65 00 07 00 00 00 6e 6f 64 65 6a 73 00 02 |ame.....nodejs..| 6 | 00000050 76 65 72 73 69 6f 6e 00 0d 00 00 00 34 2e 30 2e |version.....4.0.| 7 | 00000060 30 2d 62 65 74 61 2e 36 00 00 03 6f 73 00 51 00 |0-beta.6...os.Q.| 8 | 00000070 00 00 02 74 79 70 65 00 07 00 00 00 44 61 72 77 |...type.....Darw| 9 | 00000080 69 6e 00 02 6e 61 6d 65 00 07 00 00 00 64 61 72 |in..name.....dar| 10 | 00000090 77 69 6e 00 02 61 72 63 68 69 74 65 63 74 75 72 |win..architectur| 11 | 000000a0 65 00 04 00 00 00 78 36 34 00 02 76 65 72 73 69 |e.....x64..versi| 12 | 000000b0 6f 6e 00 07 00 00 00 32 30 2e 36 2e 30 00 00 02 |on.....20.6.0...| 13 | 000000c0 70 6c 61 74 66 6f 72 6d 00 3e 00 00 00 4e 6f 64 |platform.>...Nod| 14 | 000000d0 65 2e 6a 73 20 76 31 34 2e 31 37 2e 33 2c 20 4c |e.js v14.17.3, L| 15 | 000000e0 45 20 28 75 6e 69 66 69 65 64 29 7c 4e 6f 64 65 |E (unified)|Node| 16 | 000000f0 2e 6a 73 20 76 31 34 2e 31 37 2e 33 2c 20 4c 45 |.js v14.17.3, LE| 17 | 00000100 20 28 75 6e 69 66 69 65 64 29 00 03 61 70 70 6c | (unified)..appl| 18 | 00000110 69 63 61 74 69 6f 6e 00 1d 00 00 00 02 6e 61 6d |ication......nam| 19 | 00000120 65 00 0e 00 00 00 6d 6f 6e 67 6f 73 68 20 31 2e |e.....mongosh 1.| 20 | 00000130 30 2e 31 00 00 00 04 63 6f 6d 70 72 65 73 73 69 |0.1....compressi| 21 | 00000140 6f 6e 00 11 00 00 00 02 30 00 05 00 00 00 6e 6f |on......0.....no| 22 | 00000150 6e 65 00 00 08 6c 6f 61 64 42 61 6c 61 6e 63 65 |ne...loadBalance| 23 | 00000160 64 00 00 00 |d...| 24 | -------------------------------------------------------------------------------- /internal/fjson/datetime_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package fjson 16 | 17 | import ( 18 | "testing" 19 | "time" 20 | 21 | "github.com/AlekSi/pointer" 22 | ) 23 | 24 | var dateTimeTestCases = []testCase{{ 25 | name: "2021", 26 | v: pointer.To(dateTimeType(time.Date(2021, 11, 1, 10, 18, 42, 123000000, time.UTC).Local())), 27 | j: `{"$d":1635761922123}`, 28 | }, { 29 | name: "unix_zero", 30 | v: pointer.To(dateTimeType(time.Unix(0, 0))), 31 | j: `{"$d":0}`, 32 | }, { 33 | name: "0", 34 | v: pointer.To(dateTimeType(time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC).Local())), 35 | j: `{"$d":-62167219200000}`, 36 | }, { 37 | name: "9999", 38 | v: pointer.To(dateTimeType(time.Date(9999, 12, 31, 23, 59, 59, 999000000, time.UTC).Local())), 39 | j: `{"$d":253402300799999}`, 40 | }, { 41 | name: "EOF", 42 | j: `{`, 43 | jErr: `unexpected EOF`, 44 | }} 45 | 46 | func TestDateTime(t *testing.T) { 47 | t.Parallel() 48 | testJSON(t, dateTimeTestCases, func() fjsontype { return new(dateTimeType) }) 49 | } 50 | 51 | func FuzzDateTime(f *testing.F) { 52 | fuzzJSON(f, dateTimeTestCases, func() fjsontype { return new(dateTimeType) }) 53 | } 54 | 55 | func BenchmarkDateTime(b *testing.B) { 56 | benchmark(b, dateTimeTestCases, func() fjsontype { return new(dateTimeType) }) 57 | } 58 | -------------------------------------------------------------------------------- /.github/workflows/integration.yml: -------------------------------------------------------------------------------- 1 | --- 2 | name: Integration 3 | on: 4 | pull_request: 5 | types: 6 | - opened 7 | - reopened 8 | - synchronize 9 | push: 10 | branches: 11 | - main 12 | schedule: 13 | - cron: "42 3 * * *" 14 | 15 | env: 16 | GOPATH: /home/runner/go 17 | GOCACHE: /home/runner/go/cache 18 | GOLANGCI_LINT_CACHE: /home/runner/go/cache/lint 19 | GOMODCACHE: /home/runner/go/mod 20 | GOPROXY: https://proxy.golang.org 21 | 22 | jobs: 23 | test: 24 | name: Test 25 | runs-on: ubuntu-20.04 26 | 27 | strategy: 28 | fail-fast: false 29 | matrix: 30 | db: 31 | - FerretDB 32 | - MongoDB 33 | 34 | steps: 35 | - name: Checkout code 36 | uses: actions/checkout@v3 37 | with: 38 | fetch-depth: 0 39 | 40 | - name: Setup Go 41 | uses: FerretDB/github-actions/setup-go@main 42 | with: 43 | cache-key: test 44 | 45 | - name: Install Task 46 | run: go generate -x 47 | working-directory: tools 48 | 49 | - name: Start environment 50 | run: bin/task env-up-detach 51 | 52 | - name: Run init 53 | run: bin/task init 54 | 55 | - name: Wait for and setup environment 56 | run: bin/task env-setup 57 | 58 | - name: Run tests 59 | run: bin/task test-integration-db DB=${{ matrix.db }} 60 | 61 | - name: Upload coverage information 62 | uses: codecov/codecov-action@v3 63 | with: 64 | files: ./integration/integration-${{ matrix.db }}.txt 65 | flags: integration,${{ matrix.db }} 66 | 67 | # we don't want them on CI 68 | - name: Clean test and fuzz caches 69 | if: always() 70 | run: | 71 | go clean -testcache -fuzzcache 72 | 73 | - name: Check dirty 74 | run: | 75 | git status 76 | git diff --exit-code 77 | -------------------------------------------------------------------------------- /internal/handlers/common/typecode_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 FerretDB Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package common 16 | 17 | import ( 18 | "testing" 19 | 20 | "github.com/stretchr/testify/assert" 21 | 22 | "github.com/FerretDB/FerretDB/internal/types" 23 | "github.com/FerretDB/FerretDB/internal/util/must" 24 | ) 25 | 26 | func TestHasSameTypeElements(t *testing.T) { 27 | t.Parallel() 28 | 29 | for name, tc := range map[string]struct { //nolint:paralleltest // false positive 30 | array *types.Array 31 | same bool 32 | }{ 33 | "ArrayInt": { 34 | array: must.NotFail(types.NewArray(must.NotFail(types.NewArray(int32(1), int32(2))), int32(3))), 35 | same: false, 36 | }, 37 | "IntSame": { 38 | array: must.NotFail(types.NewArray(int32(1), int32(2))), 39 | same: true, 40 | }, 41 | "IntDouble": { 42 | array: must.NotFail(types.NewArray(int32(1), 42.3)), 43 | same: false, 44 | }, 45 | "IntDoubleWhole": { 46 | array: must.NotFail(types.NewArray(int32(1), 42.0)), 47 | same: true, 48 | }, 49 | "IntLong": { 50 | array: must.NotFail(types.NewArray(int32(1), int64(42))), 51 | same: true, 52 | }, 53 | } { 54 | tc, name := tc, name 55 | t.Run(name, func(t *testing.T) { 56 | t.Parallel() 57 | 58 | result := hasSameTypeElements(tc.array) 59 | assert.Equal(t, tc.same, result) 60 | }) 61 | } 62 | } 63 | --------------------------------------------------------------------------------