state_deltas.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 Vector Creations Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import logging
  16. from synapse.storage._base import SQLBaseStore
  17. logger = logging.getLogger(__name__)
  18. class StateDeltasStore(SQLBaseStore):
  19. def get_current_state_deltas(self, prev_stream_id):
  20. """Fetch a list of room state changes since the given stream id
  21. Each entry in the result contains the following fields:
  22. - stream_id (int)
  23. - room_id (str)
  24. - type (str): event type
  25. - state_key (str):
  26. - event_id (str|None): new event_id for this state key. None if the
  27. state has been deleted.
  28. - prev_event_id (str|None): previous event_id for this state key. None
  29. if it's new state.
  30. Args:
  31. prev_stream_id (int): point to get changes since (exclusive)
  32. Returns:
  33. Deferred[list[dict]]: results
  34. """
  35. prev_stream_id = int(prev_stream_id)
  36. if not self._curr_state_delta_stream_cache.has_any_entity_changed(
  37. prev_stream_id
  38. ):
  39. return []
  40. def get_current_state_deltas_txn(txn):
  41. # First we calculate the max stream id that will give us less than
  42. # N results.
  43. # We arbitarily limit to 100 stream_id entries to ensure we don't
  44. # select toooo many.
  45. sql = """
  46. SELECT stream_id, count(*)
  47. FROM current_state_delta_stream
  48. WHERE stream_id > ?
  49. GROUP BY stream_id
  50. ORDER BY stream_id ASC
  51. LIMIT 100
  52. """
  53. txn.execute(sql, (prev_stream_id,))
  54. total = 0
  55. max_stream_id = prev_stream_id
  56. for max_stream_id, count in txn:
  57. total += count
  58. if total > 100:
  59. # We arbitarily limit to 100 entries to ensure we don't
  60. # select toooo many.
  61. break
  62. # Now actually get the deltas
  63. sql = """
  64. SELECT stream_id, room_id, type, state_key, event_id, prev_event_id
  65. FROM current_state_delta_stream
  66. WHERE ? < stream_id AND stream_id <= ?
  67. ORDER BY stream_id ASC
  68. """
  69. txn.execute(sql, (prev_stream_id, max_stream_id))
  70. return self.cursor_to_dict(txn)
  71. return self.runInteraction(
  72. "get_current_state_deltas", get_current_state_deltas_txn
  73. )
  74. def _get_max_stream_id_in_current_state_deltas_txn(self, txn):
  75. return self._simple_select_one_onecol_txn(
  76. txn,
  77. table="current_state_delta_stream",
  78. keyvalues={},
  79. retcol="COALESCE(MAX(stream_id), -1)",
  80. )
  81. def get_max_stream_id_in_current_state_deltas(self):
  82. return self.runInteraction(
  83. "get_max_stream_id_in_current_state_deltas",
  84. self._get_max_stream_id_in_current_state_deltas_txn,
  85. )