From e63583dc38e35d2c791e301f056617fb4f577701 Mon Sep 17 00:00:00 2001 From: jypelle Date: Thu, 24 Oct 2019 16:25:02 +0200 Subject: [PATCH] Sync bug fix + Composite Id Rework to avoid Storm Bug with string composite id --- restApiV1/artist.go | 8 ++++---- srv/entity/artist.go | 11 ++++------- srv/entity/favoritePlaylist.go | 31 +++++++++++++++---------------- srv/entity/playlist.go | 31 ++++++++++++------------------- srv/svc/favoritePlaylist.go | 12 ++++++------ srv/svc/playlist.go | 6 +++--- srv/svc/sync.go | 4 ++-- version/version.go | 2 +- 8 files changed, 47 insertions(+), 58 deletions(-) diff --git a/restApiV1/artist.go b/restApiV1/artist.go index 93badf2..3fa6a17 100644 --- a/restApiV1/artist.go +++ b/restApiV1/artist.go @@ -3,12 +3,12 @@ package restApiV1 // Artist type Artist struct { - Id string `json:"id" storm:"id"` + Id string `json:"id"` CreationTs int64 `json:"creationTs"` - UpdateTs int64 `json:"updateTs" storm:"index"` - ArtistMeta `storm:"inline"` + UpdateTs int64 `json:"updateTs"` + ArtistMeta } type ArtistMeta struct { - Name string `json:"name" storm:"index"` + Name string `json:"name"` } diff --git a/srv/entity/artist.go b/srv/entity/artist.go index 0913a3c..822a006 100644 --- a/srv/entity/artist.go +++ b/srv/entity/artist.go @@ -35,17 +35,14 @@ type ArtistSongId struct { } type ArtistSongEntity struct { - Id ArtistSongId `storm:"id"` - ArtistId string `storm:"index"` - SongId string `storm:"index"` + Id string `storm:"id"` + ArtistId string `storm:"index"` + SongId string `storm:"index"` } func NewArtistSongEntity(artistId string, songId string) *ArtistSongEntity { return &ArtistSongEntity{ - Id: ArtistSongId{ - ArtistId: artistId, - SongId: songId, - }, + Id: artistId + ":" + songId, ArtistId: artistId, SongId: songId, } diff --git a/srv/entity/favoritePlaylist.go b/srv/entity/favoritePlaylist.go index 46e080e..2ff52ef 100644 --- a/srv/entity/favoritePlaylist.go +++ b/srv/entity/favoritePlaylist.go @@ -6,20 +6,17 @@ import ( ) type FavoritePlaylistEntity struct { - Id restApiV1.FavoritePlaylistId `storm:"id"` - UpdateTs int64 `storm:"index"` - UserId string `storm:"index"` - PlaylistId string `storm:"index"` + Id string `storm:"id"` + UpdateTs int64 `storm:"index"` + UserId string `storm:"index"` + PlaylistId string `storm:"index"` } func NewFavoritePlaylistEntity(userId string, playlistId string) *FavoritePlaylistEntity { now := time.Now().UnixNano() return &FavoritePlaylistEntity{ - Id: restApiV1.FavoritePlaylistId{ - UserId: userId, - PlaylistId: playlistId, - }, + Id: userId + ":" + playlistId, UpdateTs: now, UserId: userId, PlaylistId: playlistId, @@ -27,30 +24,32 @@ func NewFavoritePlaylistEntity(userId string, playlistId string) *FavoritePlayli } func (e *FavoritePlaylistEntity) Fill(s *restApiV1.FavoritePlaylist) { - s.Id = e.Id + s.Id = restApiV1.FavoritePlaylistId{UserId: e.UserId, PlaylistId: e.PlaylistId} s.UpdateTs = e.UpdateTs } func (e *FavoritePlaylistEntity) LoadMeta(s *restApiV1.FavoritePlaylistMeta) { if s != nil { - e.Id = s.Id + e.Id = s.Id.UserId + ":" + s.Id.PlaylistId e.UserId = s.Id.UserId e.PlaylistId = s.Id.PlaylistId } } type DeletedFavoritePlaylistEntity struct { - Id restApiV1.FavoritePlaylistId `storm:"id"` - DeleteTs int64 `storm:"index"` - UserId string `storm:"index"` + Id string `storm:"id"` + DeleteTs int64 `storm:"index"` + UserId string `storm:"index"` + PlaylistId string `storm:"index"` } func NewDeletedFavoritePlaylistEntity(favoritePlaylistId restApiV1.FavoritePlaylistId) *DeletedFavoritePlaylistEntity { now := time.Now().UnixNano() return &DeletedFavoritePlaylistEntity{ - Id: favoritePlaylistId, - DeleteTs: now, - UserId: favoritePlaylistId.UserId, + Id: favoritePlaylistId.UserId + ":" + favoritePlaylistId.PlaylistId, + DeleteTs: now, + UserId: favoritePlaylistId.UserId, + PlaylistId: favoritePlaylistId.PlaylistId, } } diff --git a/srv/entity/playlist.go b/srv/entity/playlist.go index c7e4ade..771d364 100644 --- a/srv/entity/playlist.go +++ b/srv/entity/playlist.go @@ -45,36 +45,29 @@ type PlaylistSongId struct { } type PlaylistSongEntity struct { - Id PlaylistSongId `storm:"id"` - SongId string `storm:"index"` + Id string `storm:"id"` + PlaylistId string `storm:"index"` + SongId string `storm:"index"` } func NewPlaylistSongEntity(playlistId string, songId string) *PlaylistSongEntity { return &PlaylistSongEntity{ - Id: PlaylistSongId{ - PlaylistId: playlistId, - SongId: songId, - }, - SongId: songId, + Id: playlistId + ":" + songId, + PlaylistId: playlistId, + SongId: songId, } } -type OwnedUserPlaylistId struct { - UserId string - PlaylistId string -} - type OwnedUserPlaylistEntity struct { - Id OwnedUserPlaylistId `storm:"id"` - UserId string `storm:"index"` + Id string `storm:"id"` + UserId string `storm:"index"` + PlaylistId string `storm:"index"` } func NewOwnedUserPlaylistEntity(userId string, playlistId string) *OwnedUserPlaylistEntity { return &OwnedUserPlaylistEntity{ - Id: OwnedUserPlaylistId{ - UserId: userId, - PlaylistId: playlistId, - }, - UserId: userId, + Id: userId + playlistId, + UserId: userId, + PlaylistId: playlistId, } } diff --git a/srv/svc/favoritePlaylist.go b/srv/svc/favoritePlaylist.go index 1db267d..1ace61b 100644 --- a/srv/svc/favoritePlaylist.go +++ b/srv/svc/favoritePlaylist.go @@ -96,7 +96,7 @@ func (s *Service) CreateFavoritePlaylist(externalTrn storm.Node, favoritePlaylis var favoritePlaylistEntity entity.FavoritePlaylistEntity - e = txn.One("Id", favoritePlaylistMeta.Id, &favoritePlaylistEntity) + e = txn.One("Id", favoritePlaylistMeta.Id.UserId+":"+favoritePlaylistMeta.Id.PlaylistId, &favoritePlaylistEntity) if e != nil && e != storm.ErrNotFound { return nil, e } @@ -116,7 +116,7 @@ func (s *Service) CreateFavoritePlaylist(externalTrn storm.Node, favoritePlaylis // if previously deletedFavoritePlaylist exists var deletedFavoritePlaylistEntity entity.DeletedFavoritePlaylistEntity - e = txn.One("Id", favoritePlaylistMeta.Id, &deletedFavoritePlaylistEntity) + e = txn.One("Id", favoritePlaylistMeta.Id.UserId+":"+favoritePlaylistMeta.Id.PlaylistId, &deletedFavoritePlaylistEntity) if e != nil && e != storm.ErrNotFound { return nil, e } @@ -155,7 +155,7 @@ func (s *Service) DeleteFavoritePlaylist(externalTrn storm.Node, favoritePlaylis } var favoritePlaylistEntity entity.FavoritePlaylistEntity - e = txn.One("Id", favoritePlaylistId, &favoritePlaylistEntity) + e = txn.One("Id", favoritePlaylistId.UserId+":"+favoritePlaylistId.PlaylistId, &favoritePlaylistEntity) if e != nil { return nil, e } @@ -167,7 +167,7 @@ func (s *Service) DeleteFavoritePlaylist(externalTrn storm.Node, favoritePlaylis } // Archive favoritePlaylistId - e = txn.Save(entity.NewDeletedFavoritePlaylistEntity(favoritePlaylistEntity.Id)) + e = txn.Save(entity.NewDeletedFavoritePlaylistEntity(favoritePlaylistId)) if e != nil { return nil, e } @@ -207,7 +207,7 @@ func (s *Service) GetDeletedFavoritePlaylistIds(externalTrn storm.Node, fromTs i } for _, deletedFavoritePlaylistEntity := range deletedFavoritePlaylistEntities { - favoritePlaylistIds = append(favoritePlaylistIds, deletedFavoritePlaylistEntity.Id) + favoritePlaylistIds = append(favoritePlaylistIds, restApiV1.FavoritePlaylistId{UserId: deletedFavoritePlaylistEntity.UserId, PlaylistId: deletedFavoritePlaylistEntity.PlaylistId}) } return favoritePlaylistIds, nil @@ -237,7 +237,7 @@ func (s *Service) GetDeletedUserFavoritePlaylistIds(externalTrn storm.Node, from } for _, deletedFavoritePlaylistEntity := range deletedFavoritePlaylistEntities { - playlistIds = append(playlistIds, deletedFavoritePlaylistEntity.Id.PlaylistId) + playlistIds = append(playlistIds, deletedFavoritePlaylistEntity.PlaylistId) } return playlistIds, nil diff --git a/srv/svc/playlist.go b/srv/svc/playlist.go index 0950d4a..466c2d0 100644 --- a/srv/svc/playlist.go +++ b/srv/svc/playlist.go @@ -398,7 +398,7 @@ func (s *Service) DeletePlaylist(externalTrn storm.Node, playlistId string) (*re return nil, e } for _, favoritePlaylistEntity := range favoritePlaylistEntities { - s.DeleteFavoritePlaylist(txn, favoritePlaylistEntity.Id) + s.DeleteFavoritePlaylist(txn, restApiV1.FavoritePlaylistId{UserId: favoritePlaylistEntity.UserId, PlaylistId: favoritePlaylistEntity.PlaylistId}) } // Delete ower link @@ -494,7 +494,7 @@ func (s *Service) GetPlaylistIdsFromSongId(externalTrn storm.Node, songId string } for _, playlistSongEntity := range playlistSongEntities { - playlistIds = append(playlistIds, playlistSongEntity.Id.PlaylistId) + playlistIds = append(playlistIds, playlistSongEntity.PlaylistId) } return playlistIds, nil @@ -525,7 +525,7 @@ func (s *Service) GetPlaylistIdsFromOwnerUserId(externalTrn storm.Node, ownerUse } for _, ownedUserPlaylistEntity := range ownedUserPlaylistEntities { - playlistIds = append(playlistIds, ownedUserPlaylistEntity.Id.PlaylistId) + playlistIds = append(playlistIds, ownedUserPlaylistEntity.PlaylistId) } return playlistIds, nil diff --git a/srv/svc/sync.go b/srv/svc/sync.go index 06cf885..4c69486 100644 --- a/srv/svc/sync.go +++ b/srv/svc/sync.go @@ -14,7 +14,7 @@ func (s *Service) ReadSyncReport(fromTs int64) (*restApiV1.SyncReport, error) { var err error var txn storm.Node - txn, err = s.Db.Begin(false) + txn, err = s.Db.Begin(true) if err != nil { return nil, err } @@ -92,7 +92,7 @@ func (s *Service) ReadFileSyncReport(fromTs int64, userId string) (*restApiV1.Fi var err error var txn storm.Node - txn, err = s.Db.Begin(false) + txn, err = s.Db.Begin(true) if err != nil { return nil, err } diff --git a/version/version.go b/version/version.go index e91adce..6422fea 100644 --- a/version/version.go +++ b/version/version.go @@ -5,7 +5,7 @@ import "strconv" var ( MajorVersionNumber int = 0 MinorVersionNumber int = 1 - PatchVersionNumber int = 0 + PatchVersionNumber int = 1 ) func Version() string {