MCPcopy Index your code
hub / github.com/dnote/dnote / incrementUserUSN

Function incrementUserUSN

pkg/server/app/helpers.go:28–53  ·  view source on GitHub ↗

incrementUserUSN increment the given user's max_usn by 1 and returns the new, incremented max_usn

(tx *gorm.DB, userID int)

Source from the content-addressed store, hash-verified

26// incrementUserUSN increment the given user's max_usn by 1
27// and returns the new, incremented max_usn
28func incrementUserUSN(tx *gorm.DB, userID int) (int, error) {
29 // First, get the current max_usn to detect transition from empty server
30 var user database.User
31 if err := tx.Select("max_usn, full_sync_before").Where("id = ?", userID).First(&user).Error; err != nil {
32 return 0, errors.Wrap(err, "getting current user state")
33 }
34
35 // If transitioning from empty server (MaxUSN=0) to non-empty (MaxUSN=1),
36 // set full_sync_before to current timestamp to force all other clients to full sync
37 if user.MaxUSN == 0 && user.FullSyncBefore == 0 {
38 currentTime := time.Now().Unix()
39 if err := tx.Table("users").Where("id = ?", userID).Update("full_sync_before", currentTime).Error; err != nil {
40 return 0, errors.Wrap(err, "setting full_sync_before on empty server transition")
41 }
42 }
43
44 if err := tx.Table("users").Where("id = ?", userID).Update("max_usn", gorm.Expr("max_usn + 1")).Error; err != nil {
45 return 0, errors.Wrap(err, "incrementing user max_usn")
46 }
47
48 if err := tx.Select("max_usn").Where("id = ?", userID).First(&user).Error; err != nil {
49 return 0, errors.Wrap(err, "getting the updated user max_usn")
50 }
51
52 return user.MaxUSN, nil
53}

Callers 7

CreateBookMethod · 0.85
DeleteBookMethod · 0.85
UpdateBookMethod · 0.85
TestIncremenetUserUSNFunction · 0.85
CreateNoteMethod · 0.85
UpdateNoteMethod · 0.85
DeleteNoteMethod · 0.85

Calls 2

NowMethod · 0.65
UpdateMethod · 0.45

Tested by 1

TestIncremenetUserUSNFunction · 0.68