temp.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #pragma once
  2. #include <yt/cpp/mapreduce/interface/client.h>
  3. namespace NYT {
  4. ////////////////////////////////////////////////////////////////////////////////
  5. ///
  6. /// @brief RAII class for working with temporary tables. Constructor of this class creates temporary table and destructor removes it.
  7. ///
  8. /// CAVEAT: when using with transactions destructor of TTempTable should be called before commiting transaction.
  9. class TTempTable
  10. {
  11. public:
  12. ///
  13. /// @brief Constructor creates temporary table
  14. ///
  15. /// @param client -- YT client or transaction object.
  16. /// @param prefix -- table name prefix
  17. /// @param directory -- path to directory where temporary table will be created.
  18. /// @param options -- options to be passed for table creation (might be useful if we want to set table attributes)
  19. explicit TTempTable(
  20. IClientBasePtr client,
  21. const TString& prefix = {},
  22. const TYPath& directory = {},
  23. const TCreateOptions& options = {});
  24. TTempTable(const TTempTable&) = delete;
  25. TTempTable& operator=(const TTempTable&) = delete;
  26. TTempTable(TTempTable&&);
  27. TTempTable& operator=(TTempTable&&);
  28. ~TTempTable();
  29. ///
  30. /// @brief Create table with given path that will be autoremoved in RAII manner.
  31. ///
  32. /// In contrast to TTempTable constructor this function uses provided path to table and don't modify it in any manner.
  33. /// It's user responsibility to provide unique path.
  34. static TTempTable CreateAutoremovingTable(IClientBasePtr client, TYPath path, const TCreateOptions& options);
  35. /// Return full path to the table.
  36. TString Name() const &;
  37. TString Name() && = delete;
  38. /// Release table and return its path. Table will not be deleted by TTempTable destructor after this call.
  39. TString Release();
  40. private:
  41. struct TPrivateConstuctorTag
  42. { };
  43. TTempTable(TPrivateConstuctorTag, IClientBasePtr client, TYPath path, const TCreateOptions& options);
  44. private:
  45. IClientBasePtr Client_;
  46. TYPath Name_;
  47. bool Owns_ = true;
  48. private:
  49. void RemoveTable();
  50. };
  51. ///
  52. /// @brief Create empty table with unique name in given directory and return its path.
  53. ///
  54. /// @param client -- YT client or transaction object.
  55. /// @param prefix -- table name prefix
  56. /// @param directory -- path to directory where temporary table will be created.
  57. /// @param options -- options to be passed for table creation (might be useful if we want to set table attributes)
  58. TYPath CreateTempTable(const IClientBasePtr& client, const TString& prefix, const TYPath& directory, const TCreateOptions& options);
  59. ////////////////////////////////////////////////////////////////////////////////
  60. } // namespace NYT