_table.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #
  2. # Copyright 2019 The FATE Authors. All Rights Reserved.
  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. #
  16. from typing import Iterable
  17. from fate_arch._standalone import Session
  18. from fate_arch.storage import StorageEngine, StandaloneStoreType
  19. from fate_arch.storage import StorageTableBase
  20. class StorageTable(StorageTableBase):
  21. def __init__(
  22. self,
  23. session: Session,
  24. address=None,
  25. name: str = None,
  26. namespace: str = None,
  27. partitions: int = 1,
  28. store_type: StandaloneStoreType = StandaloneStoreType.ROLLPAIR_LMDB,
  29. options=None,
  30. ):
  31. super(StorageTable, self).__init__(
  32. name=name,
  33. namespace=namespace,
  34. address=address,
  35. partitions=partitions,
  36. options=options,
  37. engine=StorageEngine.STANDALONE,
  38. store_type=store_type,
  39. )
  40. self._session = session
  41. self._table = self._session.create_table(
  42. namespace=self._namespace,
  43. name=self._name,
  44. partitions=partitions,
  45. need_cleanup=self._store_type == StandaloneStoreType.ROLLPAIR_IN_MEMORY,
  46. error_if_exist=False,
  47. )
  48. def _put_all(self, kv_list: Iterable, **kwargs):
  49. return self._table.put_all(kv_list)
  50. def _collect(self, **kwargs):
  51. return self._table.collect(**kwargs)
  52. def _count(self):
  53. return self._table.count()
  54. def _destroy(self):
  55. return self._table.destroy()
  56. def _save_as(self, address, name, namespace, partitions=None, **kwargs):
  57. self._table.save_as(name=name, namespace=namespace)
  58. table = StorageTable(
  59. session=self._session,
  60. address=address,
  61. partitions=partitions,
  62. name=name,
  63. namespace=namespace,
  64. **kwargs,
  65. )
  66. return table