account_data.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 OpenMarket Ltd
  3. # Copyright 2018 New Vector Ltd
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. from synapse.replication.slave.storage._base import BaseSlavedStore
  17. from synapse.replication.slave.storage._slaved_id_tracker import SlavedIdTracker
  18. from synapse.storage.account_data import AccountDataWorkerStore
  19. from synapse.storage.tags import TagsWorkerStore
  20. class SlavedAccountDataStore(TagsWorkerStore, AccountDataWorkerStore, BaseSlavedStore):
  21. def __init__(self, db_conn, hs):
  22. self._account_data_id_gen = SlavedIdTracker(
  23. db_conn, "account_data_max_stream_id", "stream_id",
  24. )
  25. super(SlavedAccountDataStore, self).__init__(db_conn, hs)
  26. def get_max_account_data_stream_id(self):
  27. return self._account_data_id_gen.get_current_token()
  28. def stream_positions(self):
  29. result = super(SlavedAccountDataStore, self).stream_positions()
  30. position = self._account_data_id_gen.get_current_token()
  31. result["user_account_data"] = position
  32. result["room_account_data"] = position
  33. result["tag_account_data"] = position
  34. return result
  35. def process_replication_rows(self, stream_name, token, rows):
  36. if stream_name == "tag_account_data":
  37. self._account_data_id_gen.advance(token)
  38. for row in rows:
  39. self.get_tags_for_user.invalidate((row.user_id,))
  40. self._account_data_stream_cache.entity_has_changed(
  41. row.user_id, token
  42. )
  43. elif stream_name == "account_data":
  44. self._account_data_id_gen.advance(token)
  45. for row in rows:
  46. if not row.room_id:
  47. self.get_global_account_data_by_type_for_user.invalidate(
  48. (row.data_type, row.user_id,)
  49. )
  50. self.get_account_data_for_user.invalidate((row.user_id,))
  51. self.get_account_data_for_room.invalidate((row.user_id, row.room_id,))
  52. self.get_account_data_for_room_and_type.invalidate(
  53. (row.user_id, row.room_id, row.data_type,),
  54. )
  55. self._account_data_stream_cache.entity_has_changed(
  56. row.user_id, token
  57. )
  58. return super(SlavedAccountDataStore, self).process_replication_rows(
  59. stream_name, token, rows
  60. )