Store profile usernames in lowercase for speedup

This commit is contained in:
Zed 2020-03-09 00:12:42 +01:00
parent a77c0f6a84
commit 5fc458638d
4 changed files with 19 additions and 12 deletions

View File

@ -17,6 +17,8 @@ withDb:
Video.title.safeAddColumn
Video.description.safeAddColumn
safeAddColumn Profile.lowername
var profileCacheTime = initDuration(minutes=10)
proc isOutdated*(profile: Profile): bool =
@ -25,7 +27,7 @@ proc isOutdated*(profile: Profile): bool =
proc cache*(profile: var Profile) =
withDb:
try:
let p = Profile.getOne("lower(username) = ?", toLower(profile.username))
let p = Profile.getOne("lowername = ?", profile.lowername)
profile.id = p.id
profile.update()
except KeyError:
@ -35,7 +37,7 @@ proc cache*(profile: var Profile) =
proc hasCachedProfile*(username: string): Option[Profile] =
withDb:
try:
let p = Profile.getOne("lower(username) = ?", toLower(username))
let p = Profile.getOne("lowername = ?", toLower(username))
doAssert not p.isOutdated
result = some p
except AssertionError, KeyError:
@ -45,7 +47,7 @@ proc getCachedProfile*(username, agent: string;
force=false): Future[Profile] {.async.} =
withDb:
try:
result.getOne("lower(username) = ?", toLower(username))
result.getOne("lowername = ?", toLower(username))
doAssert not result.isOutdated
except AssertionError, KeyError:
result = await getProfileFull(username, agent)

View File

@ -7,9 +7,11 @@ proc parseTimelineProfile*(node: XmlNode): Profile =
if profile == nil: return
let pre = ".ProfileHeaderCard-"
let username = profile.getUsername(pre & "screenname")
result = Profile(
fullname: profile.getName(pre & "nameLink"),
username: profile.getUsername(pre & "screenname"),
username: username,
lowername: toLower(username),
joinDate: profile.getDate(pre & "joinDateText"),
website: profile.selectAttr(pre & "urlText a", "title"),
bio: profile.getBio(pre & "bio"),
@ -27,9 +29,11 @@ proc parsePopupProfile*(node: XmlNode; selector=".profile-card"): Profile =
let profile = node.select(selector)
if profile == nil: return
let username = profile.getUsername(".username")
result = Profile(
fullname: profile.getName(".fullname"),
username: profile.getUsername(".username"),
username: username,
lowername: toLower(username),
bio: profile.getBio(".bio", fallback=".ProfileCard-bio"),
userpic: profile.getAvatar(".ProfileCard-avatarImage"),
verified: isVerified(profile),

View File

@ -15,10 +15,10 @@ withDb:
createTables()
except DbError:
discard
Prefs.theme.safeAddColumn
Prefs.hidePins.safeAddColumn
Prefs.hideReplies.safeAddColumn
Prefs.infiniteScroll.safeAddColumn
safeAddColumn Prefs.theme
safeAddColumn Prefs.hidePins
safeAddColumn Prefs.hideReplies
safeAddColumn Prefs.infiniteScroll
proc getDefaultPrefs(cfg: Config): Prefs =
result = genDefaultPrefs()

View File

@ -12,6 +12,7 @@ dbTypes:
Profile* = object
username*: string
fullname*: string
lowername*: string
location*: string
website*: string
bio*: string
@ -25,9 +26,9 @@ dbTypes:
verified*: bool
protected*: bool
joinDate* {.
dbType: "INTEGER"
parseIt: it.i.fromUnix()
formatIt: dbValue(it.toUnix())
dbType: "INTEGER"
parseIt: it.i.fromUnix()
formatIt: dbValue(it.toUnix())
.}: Time
updated* {.
dbType: "INTEGER"