Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new PostgreSQL backend stub #3319

Merged
merged 53 commits into from
Sep 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
2cbcb3b
Add dummy decorator
AlekSi Sep 4, 2023
a16d4ca
Improve dummy
AlekSi Sep 4, 2023
7855c79
Tweak timestamp
AlekSi Sep 4, 2023
67c174f
Add basic oplog decorator
AlekSi Sep 4, 2023
4f2add7
Clean-up `*types.Timestamp` a bit
AlekSi Sep 4, 2023
75fdd96
Merge branch 'timestamp' into decorators
AlekSi Sep 4, 2023
d433561
Make it kinda work
AlekSi Sep 4, 2023
3e8dd1f
Fix
AlekSi Sep 5, 2023
757874e
Merge branch 'timestamp' into decorators
AlekSi Sep 5, 2023
5532b29
Merge branch 'main' into decorators
AlekSi Sep 5, 2023
47bcc04
Enable MongoDB OpLog
AlekSi Sep 5, 2023
ceac70d
Enable OpLog in MongoDB
AlekSi Sep 5, 2023
d14b858
Merge branch 'oplog' into decorators
AlekSi Sep 5, 2023
9c5bdd5
Make repl to work
AlekSi Sep 5, 2023
38b34d2
Add TODOs
AlekSi Sep 5, 2023
788fa5f
Update labels
AlekSi Sep 5, 2023
a2b2939
Improve test
AlekSi Sep 5, 2023
c965516
Lint
AlekSi Sep 5, 2023
396d386
Merge branch 'oplog' into decorators
AlekSi Sep 5, 2023
24a97d8
Merge branch 'main' into decorators
AlekSi Sep 6, 2023
fd865d4
Pass all options
AlekSi Sep 6, 2023
add67f0
Merge branch 'main' into decorators
AlekSi Sep 6, 2023
6749a40
Add `saslStart`
AlekSi Sep 6, 2023
09fede8
re-lint
AlekSi Sep 6, 2023
4ad6000
Merge branch 'main' into decorators
AlekSi Sep 6, 2023
8a85d16
Add missing methods
AlekSi Sep 6, 2023
a0b3b66
Merge branch 'decorators' into new-pg
AlekSi Sep 6, 2023
f52c8ef
Merge branch 'main' into new-pg
AlekSi Sep 7, 2023
37d431a
Use Ping method
AlekSi Sep 7, 2023
dcb1dcd
Implement ping
AlekSi Sep 7, 2023
89d4eaa
WIP
AlekSi Sep 7, 2023
98deddf
Merge branch 'main' into new-pg
AlekSi Sep 7, 2023
d06229e
Merge branch 'main' into new-pg
AlekSi Sep 7, 2023
88ba789
Remove `Clode`
AlekSi Sep 7, 2023
d808d41
Tweak
AlekSi Sep 7, 2023
6eb8442
Remove `Database.Close`, add `Database.Ping`
AlekSi Sep 7, 2023
f4c3cee
Relax toolchain
AlekSi Sep 7, 2023
0a76b0b
Merge branch 'ping-close' into new-pg
AlekSi Sep 7, 2023
879e8f5
Do not use Ping just yet
AlekSi Sep 7, 2023
bca9caa
Remove ping
AlekSi Sep 7, 2023
948d239
Update comments
AlekSi Sep 7, 2023
a232c4f
Remove conninfo reference
AlekSi Sep 7, 2023
2eedebd
Remove commented out code
AlekSi Sep 7, 2023
949135e
Merge branch 'ping-close' into new-pg
AlekSi Sep 7, 2023
78be87e
Unskip test
AlekSi Sep 8, 2023
8b11dd8
Merge branch 'ping-close' into new-pg
AlekSi Sep 8, 2023
91d5772
Merge branch 'main' into new-pg
AlekSi Sep 8, 2023
5d08d85
Merge branch 'main' into new-pg
AlekSi Sep 8, 2023
fea87fc
Start pool
AlekSi Sep 8, 2023
f4b9db8
Rework
AlekSi Sep 10, 2023
a2c9b56
Tweaks
AlekSi Sep 10, 2023
31809de
Tweak flags
AlekSi Sep 10, 2023
ea4d7db
Tweak flags
AlekSi Sep 10, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Clean-up *types.Timestamp a bit
Needed for the future PR.
  • Loading branch information
AlekSi committed Sep 4, 2023
commit 4f2add7afde6509402893c4ea00a2f2196b9c780
6 changes: 3 additions & 3 deletions internal/handlers/sjson/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func TestSchemaMarshalUnmarshal(t *testing.T) {
{"t": "object", "$s": {
"p": {
"arr": {"t": "array", "i": [
{"t": "int"},
{"t": "int"},
{"t": "timestamp"}
]},
"bar": {"t": "null"},
Expand Down Expand Up @@ -164,13 +164,13 @@ func TestSchemaMarshalUnmarshal(t *testing.T) {
json: `{
"p": {
"obj": {"t": "object", "$s": {
"p": {
"p": {
"arr": {"t": "array", "i": [
{"t": "object", "$s": {}}, {"t": "object", "$s": {
"p": {"foo": {"t": "array", "i":[]}}, "$k": ["foo"]
}}
]},
"empty-arr": {"t": "array", "i":[]}
"empty-arr": {"t": "array", "i":[]}
},
"$k": ["arr", "empty-arr"]
}}
Expand Down
12 changes: 6 additions & 6 deletions internal/types/object_id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,35 +24,35 @@ import (
//nolint:paralleltest // we modify the global objectIDProcess
func TestNewObjectID(t *testing.T) {
objectIDProcess = [5]byte{0x0b, 0xad, 0xc0, 0xff, 0xee}
ts := time.Date(2022, time.April, 13, 12, 44, 42, 0, time.UTC)
d := time.Date(2022, time.April, 13, 12, 44, 42, 0, time.UTC)

objectIDCounter.Store(0)
assert.Equal(
t,
ObjectID{0x62, 0x56, 0xc5, 0xba, 0x0b, 0xad, 0xc0, 0xff, 0xee, 0x00, 0x00, 0x01},
newObjectIDTime(ts),
newObjectIDTime(d),
)
assert.Equal(
t,
ObjectID{0x62, 0x56, 0xc5, 0xba, 0x0b, 0xad, 0xc0, 0xff, 0xee, 0x00, 0x00, 0x02},
newObjectIDTime(ts),
newObjectIDTime(d),
)

// test wraparound
objectIDCounter.Store(1<<24 - 2)
assert.Equal(
t,
ObjectID{0x62, 0x56, 0xc5, 0xba, 0x0b, 0xad, 0xc0, 0xff, 0xee, 0xff, 0xff, 0xff},
newObjectIDTime(ts),
newObjectIDTime(d),
)
assert.Equal(
t,
ObjectID{0x62, 0x56, 0xc5, 0xba, 0x0b, 0xad, 0xc0, 0xff, 0xee, 0x00, 0x00, 0x00},
newObjectIDTime(ts),
newObjectIDTime(d),
)
assert.Equal(
t,
ObjectID{0x62, 0x56, 0xc5, 0xba, 0x0b, 0xad, 0xc0, 0xff, 0xee, 0x00, 0x00, 0x01},
newObjectIDTime(ts),
newObjectIDTime(d),
)
}
36 changes: 15 additions & 21 deletions internal/types/timestamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,24 @@ import (
"time"
)

type (
// Timestamp represents BSON type Timestamp.
Timestamp int64
)

// timestampCounter is an ordinal number for timestamps in the system.
var timestampCounter uint32
// Timestamp represents BSON type Timestamp.
type Timestamp uint64

// NewTimestamp returns a timestamp from time and an increment.
func NewTimestamp(t time.Time, c uint32) Timestamp {
sec := t.Unix()
sec <<= 32
sec |= int64(c)
return Timestamp(sec)
}
// timestampCounter is a process-wide timestamp counter.
var timestampCounter atomic.Uint32

// NextTimestamp returns a timestamp from time and an internal ops counter.
// NextTimestamp returns the next timestamp for the given time value.
func NextTimestamp(t time.Time) Timestamp {
c := atomic.AddUint32(&timestampCounter, 1)
return NewTimestamp(t, c)
// Technically, that should be a counter within a second, not a process-wide,
// but that's good enough for us.
c := uint64(timestampCounter.Add(1))

sec := uint64(t.Unix())
return Timestamp((sec << 32) | c)
}

// Time returns time.Time ignoring increment.
func (t Timestamp) Time() time.Time {
t >>= 32
return time.Unix(int64(t), 0)
// Time returns timestamp's time component.
func (ts Timestamp) Time() time.Time {
sec := int64(ts >> 32)
return time.Unix(sec, 0).UTC()
}
45 changes: 45 additions & 0 deletions internal/types/timestamp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2021 FerretDB Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package types

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
)

//nolint:paralleltest // we modify the global timestampCounter
func TestNextTimestamp(t *testing.T) {
t.Run("UnixZero", func(t *testing.T) {
d := time.Unix(0, 0).UTC()

timestampCounter.Store(0)
assert.Equal(t, Timestamp(1), NextTimestamp(d))
assert.Equal(t, Timestamp(2), NextTimestamp(d))

assert.Equal(t, d, NextTimestamp(d).Time())
})

t.Run("Normal", func(t *testing.T) {
d := time.Date(2023, time.September, 12, 59, 44, 42, 0, time.UTC)

timestampCounter.Store(0)
assert.Equal(t, Timestamp(7278646209986691073), NextTimestamp(d))
assert.Equal(t, Timestamp(7278646209986691074), NextTimestamp(d))

assert.Equal(t, d, NextTimestamp(d).Time())
})
}