From 5fc458638dd150504da91ab29953d8d349a619f7 Mon Sep 17 00:00:00 2001 From: Zed Date: Mon, 9 Mar 2020 00:12:42 +0100 Subject: [PATCH] Store profile usernames in lowercase for speedup --- src/cache.nim | 8 +++++--- src/parser.nim | 8 ++++++-- src/prefs.nim | 8 ++++---- src/types.nim | 7 ++++--- 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/cache.nim b/src/cache.nim index 2adee32..f6da5be 100644 --- a/src/cache.nim +++ b/src/cache.nim @@ -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) diff --git a/src/parser.nim b/src/parser.nim index a5e5171..11763cc 100644 --- a/src/parser.nim +++ b/src/parser.nim @@ -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), diff --git a/src/prefs.nim b/src/prefs.nim index 4461ca5..f388a89 100644 --- a/src/prefs.nim +++ b/src/prefs.nim @@ -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() diff --git a/src/types.nim b/src/types.nim index 4deba0a..93991f3 100644 --- a/src/types.nim +++ b/src/types.nim @@ -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"