_table.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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.storage import StorageTableBase, StorageEngine, EggRollStoreType
  18. class StorageTable(StorageTableBase):
  19. def __init__(
  20. self,
  21. context,
  22. name,
  23. namespace,
  24. address,
  25. partitions: int = 1,
  26. store_type: EggRollStoreType = EggRollStoreType.ROLLPAIR_LMDB,
  27. options=None,
  28. ):
  29. super(StorageTable, self).__init__(
  30. name=name,
  31. namespace=namespace,
  32. address=address,
  33. partitions=partitions,
  34. options=options,
  35. engine=StorageEngine.EGGROLL,
  36. store_type=store_type,
  37. )
  38. self._context = context
  39. self._options["store_type"] = self._store_type
  40. self._options["total_partitions"] = partitions
  41. self._options["create_if_missing"] = True
  42. self._table = self._context.load(
  43. namespace=self._namespace, name=self._name, options=self._options
  44. )
  45. def _save_as(self, address, name, namespace, partitions=None, **kwargs):
  46. self._table.save_as(name=name, namespace=namespace)
  47. table = StorageTable(
  48. context=self._context,
  49. address=address,
  50. partitions=partitions,
  51. name=name,
  52. namespace=namespace
  53. )
  54. return table
  55. def _put_all(self, kv_list: Iterable, **kwargs):
  56. return self._table.put_all(kv_list)
  57. def _collect(self, **kwargs) -> list:
  58. return self._table.get_all(**kwargs)
  59. def _destroy(self):
  60. return self._table.destroy()
  61. def _count(self, **kwargs):
  62. return self._table.count()