Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 3 additions & 2 deletions nodestorage/indexstorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const (
spaceCollName = "space"
settingsCollName = "settings"
newHashKey = "nh"
// oldHashKey is the legacy diff hash key: never written, only cleared
// oldHashKey is the legacy diff hash key: never written, deleted on update
oldHashKey = "oh"
statusKey = "s"
lastAccessKey = "la"
Expand Down Expand Up @@ -104,6 +104,7 @@ func (d *indexStorage) UpdateHash(ctx context.Context, updates ...SpaceUpdate) (
update.Updated = time.Now()
}
v.Set(newHashKey, a.NewString(update.NewHash))
v.Del(oldHashKey)
v.Set(lastAccessKey, a.NewNumberFloat64(float64(update.Updated.Unix())))
if v.Get(statusKey) == nil {
v.Set(statusKey, a.NewNumberInt(int(SpaceStatusOk)))
Expand Down Expand Up @@ -216,7 +217,7 @@ func (d *indexStorage) SetSpaceStatus(ctx context.Context, spaceId string, statu
v.Set(statusKey, a.NewNumberInt(int(status)))
v.Set(lastAccessKey, a.NewNumberInt(int(time.Now().Unix())))
if status == SpaceStatusRemove {
v.Set(oldHashKey, a.NewNull())
v.Del(oldHashKey)
v.Set(newHashKey, a.NewNull())
}
return v, true, nil
Expand Down
27 changes: 27 additions & 0 deletions nodestorage/indexstorage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"time"

anystore "github.com/anyproto/any-store"
"github.com/anyproto/any-store/anyenc"
"github.com/anyproto/any-store/query"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -100,6 +102,31 @@ func Test_migrateToSingleCollection(t *testing.T) {
assert.Contains(t, collNames, settingsCollName)
}

func TestIndexStorage_NoLegacyOldHash(t *testing.T) {
tempDir := t.TempDir()
fx, err := createTestIndexStorage(ctx, tempDir)
require.NoError(t, err)
defer fx.Close()
coll := fx.(*indexStorage).spaceColl

// legacy doc carries "oh" written by an older version
_, err = coll.UpsertId(ctx, "legacy", query.ModifyFunc(func(a *anyenc.Arena, v *anyenc.Value) (*anyenc.Value, bool, error) {
v.Set(oldHashKey, a.NewString("old"))
return v, true, nil
}))
require.NoError(t, err)

require.NoError(t, fx.UpdateHash(ctx, SpaceUpdate{SpaceId: "legacy", NewHash: "new"}))
require.NoError(t, fx.UpdateHash(ctx, SpaceUpdate{SpaceId: "fresh", NewHash: "new"}))
require.NoError(t, fx.SetSpaceStatus(ctx, "removed", SpaceStatusRemove, ""))

for _, id := range []string{"legacy", "fresh", "removed"} {
doc, err := coll.FindId(ctx, id)
require.NoError(t, err)
assert.Nil(t, doc.Value().Get(oldHashKey), id)
}
}

func TestIndexStorage_MarkArchived(t *testing.T) {
tempDir := t.TempDir()
fx, err := createTestIndexStorage(ctx, tempDir)
Expand Down
Loading