Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
synapse
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Monitor
Service Desk
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
Maunium
synapse
Commits
a2866e2e
Commit
a2866e2e
authored
9 years ago
by
Mark Haines
Browse files
Options
Downloads
Patches
Plain Diff
Rename direction to step, apply checks consistently
parent
e36bfbab
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
synapse/storage/__init__.py
+1
-1
1 addition, 1 deletion
synapse/storage/__init__.py
synapse/storage/util/id_generators.py
+15
-15
15 additions, 15 deletions
synapse/storage/util/id_generators.py
with
16 additions
and
16 deletions
synapse/storage/__init__.py
+
1
−
1
View file @
a2866e2e
...
@@ -97,7 +97,7 @@ class DataStore(RoomMemberStore, RoomStore,
...
@@ -97,7 +97,7 @@ class DataStore(RoomMemberStore, RoomStore,
db_conn
,
"
events
"
,
"
stream_ordering
"
db_conn
,
"
events
"
,
"
stream_ordering
"
)
)
self
.
_backfill_id_gen
=
StreamIdGenerator
(
self
.
_backfill_id_gen
=
StreamIdGenerator
(
db_conn
,
"
events
"
,
"
stream_ordering
"
,
direction
=-
1
db_conn
,
"
events
"
,
"
stream_ordering
"
,
step
=-
1
)
)
self
.
_receipts_id_gen
=
StreamIdGenerator
(
self
.
_receipts_id_gen
=
StreamIdGenerator
(
db_conn
,
"
receipts_linearized
"
,
"
stream_id
"
db_conn
,
"
receipts_linearized
"
,
"
stream_id
"
...
...
This diff is collapsed.
Click to expand it.
synapse/storage/util/id_generators.py
+
15
−
15
View file @
a2866e2e
...
@@ -29,16 +29,16 @@ class IdGenerator(object):
...
@@ -29,16 +29,16 @@ class IdGenerator(object):
return
self
.
_next_id
return
self
.
_next_id
def
_load_current_id
(
db_conn
,
table
,
column
,
direction
=
1
):
def
_load_current_id
(
db_conn
,
table
,
column
,
step
=
1
):
cur
=
db_conn
.
cursor
()
cur
=
db_conn
.
cursor
()
if
direction
==
1
:
if
step
==
1
:
cur
.
execute
(
"
SELECT MAX(%s) FROM %s
"
%
(
column
,
table
,))
cur
.
execute
(
"
SELECT MAX(%s) FROM %s
"
%
(
column
,
table
,))
else
:
else
:
cur
.
execute
(
"
SELECT MIN(%s) FROM %s
"
%
(
column
,
table
,))
cur
.
execute
(
"
SELECT MIN(%s) FROM %s
"
%
(
column
,
table
,))
val
,
=
cur
.
fetchone
()
val
,
=
cur
.
fetchone
()
cur
.
close
()
cur
.
close
()
current_id
=
int
(
val
)
if
val
else
direction
current_id
=
int
(
val
)
if
val
else
step
return
(
max
if
direction
==
1
else
min
)(
current_id
,
direction
)
return
(
max
if
step
>
0
else
min
)(
current_id
,
step
)
class
StreamIdGenerator
(
object
):
class
StreamIdGenerator
(
object
):
...
@@ -58,21 +58,21 @@ class StreamIdGenerator(object):
...
@@ -58,21 +58,21 @@ class StreamIdGenerator(object):
:param list extra_tables: List of pairs of database tables and columns to
:param list extra_tables: List of pairs of database tables and columns to
use to source the initial value of the generator from. The value with
use to source the initial value of the generator from. The value with
the largest magnitude is used.
the largest magnitude is used.
:param int
direction
: which direction the stream ids grow in. +1 to grow
:param int
step
: which direction the stream ids grow in. +1 to grow
upwards, -1 to grow downwards.
upwards, -1 to grow downwards.
Usage:
Usage:
with stream_id_gen.get_next() as stream_id:
with stream_id_gen.get_next() as stream_id:
# ... persist event ...
# ... persist event ...
"""
"""
def
__init__
(
self
,
db_conn
,
table
,
column
,
extra_tables
=
[],
direction
=
1
):
def
__init__
(
self
,
db_conn
,
table
,
column
,
extra_tables
=
[],
step
=
1
):
self
.
_lock
=
threading
.
Lock
()
self
.
_lock
=
threading
.
Lock
()
self
.
_
direction
=
direction
self
.
_
step
=
step
self
.
_current
=
_load_current_id
(
db_conn
,
table
,
column
,
direction
)
self
.
_current
=
_load_current_id
(
db_conn
,
table
,
column
,
step
)
for
table
,
column
in
extra_tables
:
for
table
,
column
in
extra_tables
:
self
.
_current
=
(
max
if
direction
>
0
else
min
)(
self
.
_current
=
(
max
if
step
>
0
else
min
)(
self
.
_current
,
self
.
_current
,
_load_current_id
(
db_conn
,
table
,
column
,
direction
)
_load_current_id
(
db_conn
,
table
,
column
,
step
)
)
)
self
.
_unfinished_ids
=
deque
()
self
.
_unfinished_ids
=
deque
()
...
@@ -83,7 +83,7 @@ class StreamIdGenerator(object):
...
@@ -83,7 +83,7 @@ class StreamIdGenerator(object):
# ... persist event ...
# ... persist event ...
"""
"""
with
self
.
_lock
:
with
self
.
_lock
:
self
.
_current
+=
self
.
_
direction
self
.
_current
+=
self
.
_
step
next_id
=
self
.
_current
next_id
=
self
.
_current
self
.
_unfinished_ids
.
append
(
next_id
)
self
.
_unfinished_ids
.
append
(
next_id
)
...
@@ -106,9 +106,9 @@ class StreamIdGenerator(object):
...
@@ -106,9 +106,9 @@ class StreamIdGenerator(object):
"""
"""
with
self
.
_lock
:
with
self
.
_lock
:
next_ids
=
range
(
next_ids
=
range
(
self
.
_current
+
self
.
_
direction
,
self
.
_current
+
self
.
_
step
,
self
.
_current
+
self
.
_
direction
*
(
n
+
1
),
self
.
_current
+
self
.
_
step
*
(
n
+
1
),
self
.
_
direction
self
.
_
step
)
)
self
.
_current
+=
n
self
.
_current
+=
n
...
@@ -132,7 +132,7 @@ class StreamIdGenerator(object):
...
@@ -132,7 +132,7 @@ class StreamIdGenerator(object):
"""
"""
with
self
.
_lock
:
with
self
.
_lock
:
if
self
.
_unfinished_ids
:
if
self
.
_unfinished_ids
:
return
self
.
_unfinished_ids
[
0
]
-
self
.
_
direction
return
self
.
_unfinished_ids
[
0
]
-
self
.
_
step
return
self
.
_current
return
self
.
_current
...
...
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