Skip to content

Commit

Permalink
make share key shorter to type and less likely to conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
XANi committed Dec 28, 2024
1 parent 1f62d5d commit 39cd099
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
5 changes: 3 additions & 2 deletions messdb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func New(cfg Config) (*MessDB, error) {
return nil, err
}
if len(cfg.SharedPrefix) == 0 {
cfg.SharedPrefix = "shared::"
cfg.SharedPrefix = "@"
}
mdb := &MessDB{
db: db,
Expand Down Expand Up @@ -74,9 +74,10 @@ func (m *MessDB) startSync() error {
}
}()
go func() {
shared_q := fmt.Sprintf("synced_at < ? AND key LIKE '%s%%'", m.sharedPrefix)
for {
records := []KV{}
m.db.Limit(100).Find(&records, "synced_at < ? AND key LIKE 'shared::%'", time.Now().Add(time.Hour*-4))
m.db.Limit(100).Find(&records, shared_q, time.Now().Add(time.Hour*-4))
if len(records) == 0 {
time.Sleep(time.Minute * 120)
continue
Expand Down
18 changes: 9 additions & 9 deletions messdb/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,39 +96,39 @@ func TestMessDBCluster(t *testing.T) {
l.LogLevel = logger.Info
db2.db.Logger = l

err = db1.Set("shared::test1", []byte("1234"))
err = db1.Set("@test1", []byte("1234"))
require.NoError(t, err)
err = db2.Set("shared::test2", []byte("5678"))
err = db2.Set("@test2", []byte("5678"))
require.NoError(t, err)
time.Sleep(time.Second)

value, found, err := db1.Get("shared::test1")
value, found, err := db1.Get("@test1")
require.NoError(t, err)
assert.Equal(t, "1234", string(value))
assert.True(t, found)

value, found, err = db1.Get("shared::test2")
value, found, err = db1.Get("@test2")
require.NoError(t, err)
assert.Equal(t, "5678", string(value))
assert.True(t, found)

value, found, err = db2.Get("shared::test1")
value, found, err = db2.Get("@test1")
require.NoError(t, err)
assert.Equal(t, "1234", string(value))
assert.True(t, found)

value, found, err = db2.Get("shared::test2")
value, found, err = db2.Get("@test2")
require.NoError(t, err)
assert.Equal(t, "5678", string(value))
assert.True(t, found)

err = db1.Set("shared::test1", []byte("6666"))
err = db1.Set("@test1", []byte("6666"))
time.Sleep(time.Second)
require.NoError(t, err)
err = db2.Set("shared::test1", []byte("6667"))
err = db2.Set("@test1", []byte("6667"))
// fail coz the key belongs to db1
require.Error(t, err)
value, found, err = db2.Get("shared::test1")
value, found, err = db2.Get("@test1")
require.NoError(t, err)
assert.Equal(t, "6666", string(value))
assert.True(t, found)
Expand Down

0 comments on commit 39cd099

Please sign in to comment.