Skip to content

Commit

Permalink
Split tweet editing methods into short and long edits
Browse files Browse the repository at this point in the history
The tweet editing methods, along with their respective prefixes, have been split into short and long versions to handle different requirements. This change impacts many parts of the codebase including the key builder and the tweets repo. The splitting of these methods will provide more flexibility and adaptability in handling edit scenarios based on tweet length, leading to optimized performance.
  • Loading branch information
lueurxax committed Dec 29, 2023
1 parent a83f026 commit 7ac6c0d
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 14 deletions.
67 changes: 60 additions & 7 deletions internal/repo/editing_tweets.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (

type editingTweetsRepo interface {
SaveTweetForEdit(ctx context.Context, tweet *common.Tweet) error
GetTweetForEdit(ctx context.Context) ([]common.Tweet, error)
DeleteEditedTweets(ctx context.Context, ids []string) error
GetTweetForShortEdit(ctx context.Context) ([]common.Tweet, error)
DeleteShortEditedTweets(ctx context.Context, ids []string) error
}

func (d *db) SaveTweetForEdit(ctx context.Context, tweet *common.Tweet) error {
Expand All @@ -26,18 +26,19 @@ func (d *db) SaveTweetForEdit(ctx context.Context, tweet *common.Tweet) error {
return err
}

tx.Set(d.keyBuilder.EditingTweet(tweet.ID), data)
tx.Set(d.keyBuilder.EditingTweetShort(tweet.ID), data)
tx.Set(d.keyBuilder.EditingTweetLong(tweet.ID), data)

return tx.Commit()
}

func (d *db) GetTweetForEdit(ctx context.Context) ([]common.Tweet, error) {
func (d *db) GetTweetForShortEdit(ctx context.Context) ([]common.Tweet, error) {
tx, err := d.db.NewTransaction(ctx)
if err != nil {
return nil, err
}

pr, err := fdb.PrefixRange(d.keyBuilder.EditingTweets())
pr, err := fdb.PrefixRange(d.keyBuilder.EditingTweetsShort())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -70,14 +71,66 @@ func (d *db) GetTweetForEdit(ctx context.Context) ([]common.Tweet, error) {
return res, nil
}

func (d *db) DeleteEditedTweets(ctx context.Context, ids []string) error {
func (d *db) DeleteShortEditedTweets(ctx context.Context, ids []string) error {
tx, err := d.db.NewTransaction(ctx)
if err != nil {
return err
}

for _, id := range ids {
tx.Clear(d.keyBuilder.EditingTweet(id))
tx.Clear(d.keyBuilder.EditingTweetShort(id))
}

return tx.Commit()
}

func (d *db) GetTweetForLongEdit(ctx context.Context) ([]common.Tweet, error) {
tx, err := d.db.NewTransaction(ctx)
if err != nil {
return nil, err
}

pr, err := fdb.PrefixRange(d.keyBuilder.EditingTweetsLong())
if err != nil {
return nil, err
}

kvs, err := tx.GetRange(pr)
if err != nil {
return nil, err
}

if len(kvs) == 0 {
return nil, ErrTweetsNotFound
}

res := make([]common.Tweet, 0, len(kvs))

for _, kv := range kvs {
var tweet common.Tweet

if err = jsoniter.Unmarshal(kv.Value, &tweet); err != nil {
return nil, err
}

res = append(res, tweet)
}

if err = tx.Commit(); err != nil {
return nil, err
}

return res, nil
}

func (d *db) DeleteLongEditedTweets(ctx context.Context, ids []string) error {
tx, err := d.db.NewTransaction(ctx)
if err != nil {
return err
}

for _, id := range ids {
tx.Clear(d.keyBuilder.EditingTweetLong(id))
}

return tx.Commit()
Expand Down
22 changes: 16 additions & 6 deletions internal/repo/keys/key_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ type Builder interface {
TweetUsernameRatingKey(username string) []byte
TweetRatings() []byte
SentTweet(link string) []byte
EditingTweet(id string) []byte
EditingTweets() []byte
EditingTweetShort(id string) []byte
EditingTweetLong(id string) []byte
EditingTweetsShort() []byte
EditingTweetsLong() []byte
TelegramSessionStorage() []byte
TwitterAccount(login string) []byte
TwitterAccounts() []byte
Expand All @@ -46,12 +48,20 @@ func (b builder) TwitterAccount(login string) []byte {
return append(twitterAccountsPrefix[:], []byte(login)...)
}

func (b builder) EditingTweet(id string) []byte {
return append(editingTweetPrefix[:], []byte(id)...)
func (b builder) EditingTweetShort(id string) []byte {
return append(editingTweetShortPrefix[:], []byte(id)...)
}

func (b builder) EditingTweets() []byte {
return editingTweetPrefix[:]
func (b builder) EditingTweetsShort() []byte {
return editingTweetShortPrefix[:]
}

func (b builder) EditingTweetLong(id string) []byte {
return append(editingTweetLongPrefix[:], []byte(id)...)
}

func (b builder) EditingTweetsLong() []byte {
return editingTweetLongPrefix[:]
}

func (b builder) TelegramSessionStorage() []byte {
Expand Down
3 changes: 2 additions & 1 deletion internal/repo/keys/prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ var (
tweetPrefix Prefix = [2]byte{0x00, 0x01}
sentTweetPrefix Prefix = [2]byte{0x00, 0x03}
tweetRatingPrefix Prefix = [2]byte{0x00, 0x04}
editingTweetPrefix Prefix = [2]byte{0x00, 0x05}
editingTweetShortPrefix Prefix = [2]byte{0x00, 0x05}
editingTweetLongPrefix Prefix = [2]byte{0x00, 0x09}
telegramSessionStoragePrefix Prefix = [2]byte{0x00, 0x06}
twitterAccountsPrefix Prefix = [2]byte{0x00, 0x07}
twitterAccountsCookiePrefix Prefix = [2]byte{0x00, 0x08}
Expand Down

0 comments on commit 7ac6c0d

Please sign in to comment.