Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
gajim
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
eta
gajim
Commits
0aa07522
Commit
0aa07522
authored
7 years ago
by
Philipp Hörist
Browse files
Options
Downloads
Patches
Plain Diff
Logger: Refactor get_jid_id()
- Cache jid_id so we save on DB querys
parent
9e39287d
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
gajim/common/logger.py
+46
-39
46 additions, 39 deletions
gajim/common/logger.py
with
46 additions
and
39 deletions
gajim/common/logger.py
+
46
−
39
View file @
0aa07522
...
...
@@ -108,6 +108,7 @@ class SubscriptionConstant(IntEnum):
class
Logger
:
def
__init__
(
self
):
self
.
jids_already_in
=
[]
# holds jids that we already have in DB
self
.
_jid_ids
=
{}
self
.
con
=
None
self
.
commit_timout_id
=
None
...
...
@@ -189,6 +190,7 @@ class Logger:
def
init_vars
(
self
):
self
.
open_db
()
self
.
get_jids_already_in_db
()
self
.
get_jid_ids_from_db
()
@staticmethod
def
_get_timeout
():
...
...
@@ -226,6 +228,15 @@ class Logger:
self
.
cur
.
execute
(
sql_to_commit
)
self
.
_timeout_commit
()
def
get_jid_ids_from_db
(
self
):
"""
Load all jid/jid_id tuples into a dict for faster access
"""
rows
=
self
.
con
.
execute
(
'
SELECT jid_id, jid, type FROM jids
'
).
fetchall
()
for
row
in
rows
:
self
.
_jid_ids
[
row
.
jid
]
=
row
def
get_jids_already_in_db
(
self
):
try
:
self
.
cur
.
execute
(
'
SELECT jid FROM jids
'
)
...
...
@@ -288,42 +299,38 @@ class Logger:
return
[
user
[
'
jid
'
]
for
user
in
family
]
return
[
jid
]
def
get_jid_id
(
self
,
jid
,
typestr
=
None
):
"""
jids table has jid and jid_id logs table has log_id, jid_id,
contact_name, time, kind, show, message so to ask logs we need jid_id
that matches our jid in jids table this method wants jid and returns the
jid_id for later sql-ing on logs typestr can be
'
ROOM
'
or anything else
depending on the type of JID and is only needed to be specified when the
JID is new in DB
"""
if
jid
.
find
(
'
/
'
)
!=
-
1
:
# if it has a /
jid_is_from_pm
=
self
.
jid_is_from_pm
(
jid
)
if
not
jid_is_from_pm
:
# it's normal jid with resource
jid
=
jid
.
split
(
'
/
'
,
1
)[
0
]
# remove the resource
if
jid
in
self
.
jids_already_in
:
# we already have jids in DB
self
.
cur
.
execute
(
'
SELECT jid_id FROM jids WHERE jid=?
'
,
[
jid
])
row
=
self
.
cur
.
fetchone
()
if
row
:
return
row
.
jid_id
# oh! a new jid :), we add it now
if
typestr
==
'
ROOM
'
:
typ
=
JIDConstant
.
ROOM_TYPE
else
:
typ
=
JIDConstant
.
NORMAL_TYPE
try
:
self
.
cur
.
execute
(
'
INSERT INTO jids (jid, type) VALUES (?, ?)
'
,
(
jid
,
typ
))
self
.
con
.
commit
()
except
sqlite
.
IntegrityError
:
# Jid already in DB, maybe added by another instance. re-read DB
self
.
get_jids_already_in_db
()
return
self
.
get_jid_id
(
jid
,
typestr
)
except
sqlite
.
OperationalError
as
e
:
raise
exceptions
.
PysqliteOperationalError
(
str
(
e
))
jid_id
=
self
.
cur
.
lastrowid
self
.
jids_already_in
.
append
(
jid
)
return
jid_id
def
get_jid_id
(
self
,
jid
,
type_
=
None
):
"""
Get the jid id from a jid.
In case the jid id is not found create a new one.
:param jid: The JID
:param type_: The JIDConstant type
return the jid id
"""
result
=
self
.
_jid_ids
.
get
(
jid
,
None
)
if
result
is
not
None
:
return
result
.
jid_id
sql
=
'
SELECT jid_id, jid, type FROM jids WHERE jid = ?
'
row
=
self
.
con
.
execute
(
sql
,
[
jid
]).
fetchone
()
if
row
is
not
None
:
self
.
_jid_ids
[
jid
]
=
row
return
row
.
jid_id
if
type_
is
None
:
raise
ValueError
(
'
Unable to insert new JID because type is missing
'
)
sql
=
'
INSERT INTO jids (jid, type) VALUES (?, ?)
'
lastrowid
=
self
.
con
.
execute
(
sql
,
(
jid
,
type_
)).
lastrowid
Row
=
namedtuple
(
'
Row
'
,
'
jid_id jid type
'
)
self
.
_jid_ids
[
jid
]
=
Row
(
lastrowid
,
jid
,
type_
)
self
.
_timeout_commit
()
return
lastrowid
def
convert_kind_values_to_db_api_values
(
self
,
kind
):
"""
...
...
@@ -961,7 +968,7 @@ class Logger:
try
:
account_jid_id
=
self
.
get_jid_id
(
account_jid
)
jid_id
=
self
.
get_jid_id
(
jid
)
jid_id
=
self
.
get_jid_id
(
jid
,
type_
=
JIDConstant
.
NORMAL_TYPE
)
except
exceptions
.
PysqliteOperationalError
as
e
:
raise
exceptions
.
PysqliteOperationalError
(
str
(
e
))
...
...
@@ -993,7 +1000,7 @@ class Logger:
Return the accound_jid roster in NonBlockingRoster format
"""
data
=
{}
account_jid_id
=
self
.
get_jid_id
(
account_jid
)
account_jid_id
=
self
.
get_jid_id
(
account_jid
,
type_
=
JIDConstant
.
NORMAL_TYPE
)
# First we fill data with roster_entry informations
self
.
cur
.
execute
(
'''
...
...
@@ -1149,7 +1156,7 @@ class Logger:
"""
account_jid_id
=
self
.
get_jid_id
(
account_jid
)
jid_id
=
self
.
get_jid_id
(
jid
)
jid_id
=
self
.
get_jid_id
(
jid
,
type_
=
JIDConstant
.
NORMAL_TYPE
)
sql
=
'''
UPDATE roster_entry SET avatar_sha = ?
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment