YPC  0.2.0
sealed_file.h
1 #pragma once
2 #include "ypc/common/limits.h"
3 #include "ypc/core/blockfile.h"
4 #include "ypc/core/byte.h"
5 #include "ypc/core/memref.h"
6 #include "ypc/core/ntobject_file.h"
7 #include <atomic>
8 #include <condition_variable>
9 #include <memory>
10 #include <mutex>
11 #include <queue>
12 #include <thread>
13 
14 namespace ypc {
15 
16 namespace internal {
18 public:
19  using blockfile_t = blockfile<0x4788d13e7fefe21f, 1024 * 1024,
20  256 * ::ypc::utc::max_item_size>;
21 
22  sealed_file_base(const std::string &file_path, bool read);
23 
24  virtual ~sealed_file_base() = default;
25  virtual void write_item(const bytes &data);
26 
27  virtual void reset_read() = 0;
28 
29  virtual bool next_item(memref &s) = 0;
30 
31 public:
32  sealed_file_base(const sealed_file_base &) = delete;
34  sealed_file_base &operator=(sealed_file_base &&) = delete;
35  sealed_file_base &operator=(const sealed_file_base &) = delete;
36 
37 protected:
38  blockfile_t m_file;
39 };
40 } // namespace internal
41 
43 public:
44  simple_sealed_file(const std::string &file_path, bool read);
45  virtual void reset_read();
46  virtual bool next_item(memref &s);
47 };
48 
49 // We use cache to improve read performance
51 public:
52  sealed_file_with_cache_opt(const std::string &file_path, bool read);
53 
54  virtual ~sealed_file_with_cache_opt();
55 
56  virtual void reset_read();
57 
58  virtual bool next_item(memref &s);
59 
64  operator=(const sealed_file_with_cache_opt &) = delete;
65 
66 protected:
67  std::unique_ptr<std::thread> m_io_thread;
68  std::queue<memref> m_cached;
69  bool m_reach_end;
70  std::mutex m_mutex;
71  std::condition_variable m_cond_full;
72  std::condition_variable m_cond_empty;
73  uint32_t m_max_queue_size;
74  std::atomic_bool m_to_close;
75 };
76 } // namespace ypc
77 
78 define_nt(sfm_path, std::string);
79 // define_nt(sfm_hash, std::string);
80 // define_nt(sfm_path, ypc::bytes);
81 define_nt(sfm_hash, ypc::bytes);
82 define_nt(sfm_index, uint16_t);
83 define_nt(sfm_num, uint16_t);
84 using sfm_item_t = ::ff::net::ntpackage<1, sfm_path, sfm_index, sfm_hash>;
85 define_nt(sfm_items, std::vector<sfm_item_t>);
86 
87 using sfm_t = ::ff::net::ntpackage<2, sfm_items, sfm_num, sfm_hash>;
ypc::memref
Definition: memref.h:6
byte.h
ypc::sealed_file_with_cache_opt
Definition: sealed_file.h:50
ypc::internal::sealed_file_base
Definition: sealed_file.h:17
ypc::blockfile< 0x4788d13e7fefe21f, 1024 *1024, 256 *::ypc::utc::max_item_size >
ypc::utc::bytes< byte_t, ::ypc::utc::byte_encode::raw_bytes >
ypc::simple_sealed_file
Definition: sealed_file.h:42