You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

97 lines
3.0 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014 matrix.org
  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. from synapse.api.errors import SynapseError
  16. class PaginationConfig(object):
  17. """A configuration object which stores pagination parameters."""
  18. def __init__(self, from_tok=None, to_tok=None, limit=0):
  19. self.from_tok = from_tok
  20. self.to_tok = to_tok
  21. self.limit = limit
  22. @classmethod
  23. def from_request(cls, request, raise_invalid_params=True):
  24. params = {
  25. "from_tok": PaginationStream.TOK_START,
  26. "to_tok": PaginationStream.TOK_END,
  27. "limit": 0
  28. }
  29. query_param_mappings = [ # 3-tuple of qp_key, attribute, rules
  30. ("from", "from_tok", lambda x: type(x) == str),
  31. ("to", "to_tok", lambda x: type(x) == str),
  32. ("limit", "limit", lambda x: x.isdigit())
  33. ]
  34. for qp, attr, is_valid in query_param_mappings:
  35. if qp in request.args:
  36. if is_valid(request.args[qp][0]):
  37. params[attr] = request.args[qp][0]
  38. elif raise_invalid_params:
  39. raise SynapseError(400, "%s parameter is invalid." % qp)
  40. return PaginationConfig(**params)
  41. class PaginationStream(object):
  42. """ An interface for streaming data as chunks. """
  43. TOK_START = "START"
  44. TOK_END = "END"
  45. def get_chunk(self, config=None):
  46. """ Return the next chunk in the stream.
  47. Args:
  48. config (PaginationConfig): The config to aid which chunk to get.
  49. Returns:
  50. A dict containing the new start token "start", the new end token
  51. "end" and the data "chunk" as a list.
  52. """
  53. raise NotImplementedError()
  54. class StreamData(object):
  55. """ An interface for obtaining streaming data from a table. """
  56. def __init__(self, hs):
  57. self.hs = hs
  58. self.store = hs.get_datastore()
  59. def get_rows(self, user_id, from_pkey, to_pkey, limit):
  60. """ Get event stream data between the specified pkeys.
  61. Args:
  62. user_id : The user's ID
  63. from_pkey : The starting pkey.
  64. to_pkey : The end pkey. May be -1 to mean "latest".
  65. limit: The max number of results to return.
  66. Returns:
  67. A tuple containing the list of event stream data and the last pkey.
  68. """
  69. raise NotImplementedError()
  70. def max_token(self):
  71. """ Get the latest currently-valid token.
  72. Returns:
  73. The latest token."""
  74. raise NotImplementedError()