Flex  0.17.9
column.h
Go to the documentation of this file.
1 
16 #ifndef GRAPHSCOPE_PROPERTY_COLUMN_H_
17 #define GRAPHSCOPE_PROPERTY_COLUMN_H_
18 
19 #include <string>
20 #include <string_view>
21 #include "grape/utils/concurrent_queue.h"
22 
23 #include "flex/utils/mmap_array.h"
25 #include "grape/serialization/out_archive.h"
26 
27 namespace gs {
28 
29 class ColumnBase {
30  public:
31  virtual ~ColumnBase() {}
32 
33  virtual void open(const std::string& name, const std::string& snapshot_dir,
34  const std::string& work_dir) = 0;
35 
36  virtual void open_in_memory(const std::string& name) = 0;
37 
38  virtual void open_with_hugepages(const std::string& name, bool force) = 0;
39 
40  virtual void close() = 0;
41 
42  virtual void touch(const std::string& filename) = 0;
43 
44  virtual void dump(const std::string& filename) = 0;
45 
46  virtual size_t size() const = 0;
47 
48  virtual void copy_to_tmp(const std::string& cur_path,
49  const std::string& tmp_path) = 0;
50  virtual void resize(size_t size) = 0;
51 
52  virtual PropertyType type() const = 0;
53 
54  virtual void set_any(size_t index, const Any& value) = 0;
55 
56  virtual Any get(size_t index) const = 0;
57 
58  virtual void ingest(uint32_t index, grape::OutArchive& arc) = 0;
59 
60  virtual StorageStrategy storage_strategy() const = 0;
61 };
62 
63 template <typename T>
64 class TypedColumn : public ColumnBase {
65  public:
66  TypedColumn(StorageStrategy strategy) : strategy_(strategy) {}
68 
69  void open(const std::string& name, const std::string& snapshot_dir,
70  const std::string& work_dir) override {
71  std::string basic_path = snapshot_dir + "/" + name;
72  if (std::filesystem::exists(basic_path)) {
73  basic_buffer_.open(basic_path, false);
74  basic_size_ = basic_buffer_.size();
75  } else {
76  basic_size_ = 0;
77  }
78  if (work_dir == "") {
79  extra_size_ = 0;
80  } else {
81  extra_buffer_.open(work_dir + "/" + name, true);
82  extra_size_ = extra_buffer_.size();
83  }
84  }
85 
86  void open_in_memory(const std::string& name) override {
87  if (!name.empty() && std::filesystem::exists(name)) {
88  basic_buffer_.open(name, false);
89  basic_size_ = basic_buffer_.size();
90  } else {
91  basic_buffer_.reset();
92  basic_size_ = 0;
93  }
94  extra_buffer_.reset();
95  extra_size_ = 0;
96  }
97 
98  void open_with_hugepages(const std::string& name, bool force) override {
99  if (strategy_ == StorageStrategy::kMem || force) {
100  if (!name.empty() && std::filesystem::exists(name)) {
101  basic_buffer_.open_with_hugepages(name);
102  basic_size_ = basic_buffer_.size();
103  } else {
104  basic_buffer_.reset();
105  basic_buffer_.set_hugepage_prefered(true);
106  basic_size_ = 0;
107  }
108  extra_buffer_.reset();
109  extra_buffer_.set_hugepage_prefered(true);
110  extra_size_ = 0;
111  } else if (strategy_ == StorageStrategy::kDisk) {
112  LOG(INFO) << "Open " << name << " with normal mmap pages";
113  open_in_memory(name);
114  }
115  }
116 
117  void touch(const std::string& filename) override {
118  mmap_array<T> tmp;
119  tmp.open(filename, true);
121  for (size_t k = 0; k < basic_size_; ++k) {
122  tmp.set(k, basic_buffer_.get(k));
123  }
124  for (size_t k = 0; k < extra_size_; ++k) {
125  tmp.set(k + basic_size_, extra_buffer_.get(k));
126  }
127  basic_size_ = 0;
128  basic_buffer_.reset();
129  extra_size_ = tmp.size();
130  extra_buffer_.swap(tmp);
131  tmp.reset();
132  }
133 
134  void close() override {
135  basic_buffer_.reset();
136  extra_buffer_.reset();
137  }
138 
139  void copy_to_tmp(const std::string& cur_path,
140  const std::string& tmp_path) override {
141  mmap_array<T> tmp;
142  if (!std::filesystem::exists(cur_path)) {
143  return;
144  }
145  copy_file(cur_path, tmp_path);
147  basic_size_ = 0;
148  tmp.open(tmp_path, true);
149  basic_buffer_.reset();
150  extra_buffer_.swap(tmp);
151  tmp.reset();
152  }
153 
154  void dump(const std::string& filename) override {
155  if (basic_size_ != 0 && extra_size_ == 0) {
156  basic_buffer_.dump(filename);
157  } else if (basic_size_ == 0 && extra_size_ != 0) {
158  extra_buffer_.dump(filename);
159  } else {
160  mmap_array<T> tmp;
161  tmp.open(filename, true);
162  for (size_t k = 0; k < basic_size_; ++k) {
163  tmp.set(k, basic_buffer_.get(k));
164  }
165  for (size_t k = 0; k < extra_size_; ++k) {
166  tmp.set(k + basic_size_, extra_buffer_.get(k));
167  }
168  }
169  }
170 
171  size_t size() const override { return basic_size_ + extra_size_; }
172 
173  void resize(size_t size) override {
174  if (size < basic_buffer_.size()) {
175  basic_size_ = size;
176  extra_size_ = 0;
177  } else {
178  basic_size_ = basic_buffer_.size();
180  extra_buffer_.resize(extra_size_);
181  }
182  }
183 
184  PropertyType type() const override { return AnyConverter<T>::type(); }
185 
186  void set_value(size_t index, const T& val) {
187  if (index >= basic_size_ && index < basic_size_ + extra_size_) {
188  extra_buffer_.set(index - basic_size_, val);
189  } else if (index < basic_size_) {
190  basic_buffer_.set(index, val);
191  } else {
192  throw std::runtime_error("Index out of range");
193  }
194  }
195 
196  void set_any(size_t index, const Any& value) override {
197  set_value(index, AnyConverter<T>::from_any(value));
198  }
199 
200  T get_view(size_t index) const {
201  return index < basic_size_ ? basic_buffer_.get(index)
202  : extra_buffer_.get(index - basic_size_);
203  }
204 
205  Any get(size_t index) const override {
206  return AnyConverter<T>::to_any(get_view(index));
207  }
208 
209  void ingest(uint32_t index, grape::OutArchive& arc) override {
210  T val;
211  arc >> val;
212  set_value(index, val);
213  }
214 
215  StorageStrategy storage_strategy() const override { return strategy_; }
216 
217  const mmap_array<T>& basic_buffer() const { return basic_buffer_; }
218  size_t basic_buffer_size() const { return basic_size_; }
219  const mmap_array<T>& extra_buffer() const { return extra_buffer_; }
220  size_t extra_buffer_size() const { return extra_size_; }
221 
222  private:
224  size_t basic_size_;
226  size_t extra_size_;
228 };
229 
230 template <>
232  public:
233  TypedColumn(const std::vector<PropertyType>& types) : types_(types) {
234  if (types.size() == 0) {
235  LOG(FATAL) << "RecordView column must have sub types.";
236  }
237  }
238 
240 
241  void open(const std::string& name, const std::string& snapshot_dir,
242  const std::string& work_dir) override {
243  LOG(FATAL) << "RecordView column does not support open.";
244  }
245 
246  void open_in_memory(const std::string& name) override;
247 
248  void open_with_hugepages(const std::string& name, bool force) override {
249  LOG(FATAL) << "RecordView column does not support open with hugepages.";
250  }
251 
252  void touch(const std::string& filename) override {
253  LOG(FATAL) << "RecordView column does not support touch.";
254  }
255 
256  void dump(const std::string& filename) override {
257  LOG(FATAL) << "RecordView column does not support dump.";
258  }
259 
260  void copy_to_tmp(const std::string& cur_path,
261  const std::string& tmp_path) override {
262  LOG(FATAL) << "RecordView column does not support copy_to_tmp.";
263  }
264  void close() override;
265 
266  size_t size() const override;
267  void resize(size_t size) override;
268 
269  PropertyType type() const override { return PropertyType::kRecordView; }
270 
271  void set_any(size_t index, const Any& value) override;
272 
273  void set_value(size_t index, const RecordView& val);
274 
275  RecordView get_view(size_t index) const;
276 
277  Any get(size_t index) const override;
278 
279  void ingest(uint32_t index, grape::OutArchive& arc) override {
280  LOG(FATAL) << "RecordView column does not support ingest.";
281  }
282 
284  LOG(ERROR) << "RecordView column does not have storage strategy.";
285  return StorageStrategy::kMem;
286  }
287 
288  std::vector<PropertyType> sub_types() const { return types_; }
289 
290  private:
291  std::vector<PropertyType> types_;
292  std::shared_ptr<Table> table_;
293 };
294 
305 
306 template <>
307 class TypedColumn<grape::EmptyType> : public ColumnBase {
308  public:
309  TypedColumn(StorageStrategy strategy) : strategy_(strategy) {}
311 
312  void open(const std::string& name, const std::string& snapshot_dir,
313  const std::string& work_dir) override {}
314  void open_in_memory(const std::string& name) override {}
315  void open_with_hugepages(const std::string& name, bool force) override {}
316  void touch(const std::string& filename) override {}
317  void dump(const std::string& filename) override {}
318  void copy_to_tmp(const std::string& cur_path,
319  const std::string& tmp_path) override {}
320  void close() override {}
321  size_t size() const override { return 0; }
322  void resize(size_t size) override {}
323 
324  PropertyType type() const override { return PropertyType::kEmpty; }
325 
326  void set_any(size_t index, const Any& value) override {}
327 
328  Any get(size_t index) const override { return Any(); }
329 
330  void ingest(uint32_t index, grape::OutArchive& arc) override {}
331 
332  StorageStrategy storage_strategy() const override { return strategy_; }
333 
334  private:
336 };
337 template <>
338 class TypedColumn<std::string_view> : public ColumnBase {
339  public:
342  : strategy_(strategy), width_(width) {}
344 
345  void open(const std::string& name, const std::string& snapshot_dir,
346  const std::string& work_dir) override {
347  std::string basic_path = snapshot_dir + "/" + name;
348  if (std::filesystem::exists(basic_path + ".items")) {
349  basic_buffer_.open(basic_path, false);
350  basic_size_ = basic_buffer_.size();
351  basic_pos_ = basic_buffer_.data_size();
352  } else {
353  basic_size_ = 0;
354  basic_pos_ = 0;
355  }
356  if (work_dir == "") {
357  extra_size_ = 0;
358  pos_.store(0);
359  } else {
360  extra_buffer_.open(work_dir + "/" + name, true);
361  extra_size_ = extra_buffer_.size();
362  pos_.store(extra_buffer_.data_size());
363  }
364  }
365 
366  void open_in_memory(const std::string& prefix) override {
367  basic_buffer_.open(prefix, false);
368  basic_size_ = basic_buffer_.size();
369  basic_pos_ = basic_buffer_.data_size();
370 
371  extra_buffer_.reset();
372  extra_size_ = 0;
373  pos_.store(0);
374  }
375 
376  void open_with_hugepages(const std::string& prefix, bool force) override {
377  if (strategy_ == StorageStrategy::kMem || force) {
378  basic_buffer_.open_with_hugepages(prefix);
379  basic_size_ = basic_buffer_.size();
380  basic_pos_ = basic_buffer_.data_size();
381 
382  extra_buffer_.reset();
383  extra_buffer_.set_hugepage_prefered(true);
384  extra_size_ = 0;
385  pos_.store(0);
386  } else if (strategy_ == StorageStrategy::kDisk) {
387  LOG(INFO) << "Open " << prefix << " with normal mmap pages";
388  open_in_memory(prefix);
389  }
390  }
391 
392  void touch(const std::string& filename) override {
394  tmp.open(filename, true);
396  size_t offset = 0;
397  for (size_t k = 0; k < basic_size_; ++k) {
398  std::string_view val = basic_buffer_.get(k);
399  tmp.set(k, offset, val);
400  offset += val.size();
401  }
402  for (size_t k = 0; k < extra_size_; ++k) {
403  std::string_view val = extra_buffer_.get(k);
404  tmp.set(k + basic_size_, offset, val);
405  offset += val.size();
406  }
407 
408  basic_size_ = 0;
409  basic_pos_ = 0;
410  basic_buffer_.reset();
411  extra_size_ = tmp.size();
412  extra_buffer_.swap(tmp);
413  tmp.reset();
414 
415  pos_.store(offset);
416  }
417 
418  void close() override {
419  basic_buffer_.reset();
420  extra_buffer_.reset();
421  }
422 
423  void copy_to_tmp(const std::string& cur_path,
424  const std::string& tmp_path) override {
426  if (!std::filesystem::exists(cur_path + ".data")) {
427  return;
428  }
429  copy_file(cur_path + ".data", tmp_path + ".data");
430  copy_file(cur_path + ".items", tmp_path + ".items");
431 
433  basic_size_ = 0;
434  basic_pos_ = 0;
435  basic_buffer_.reset();
436  tmp.open(tmp_path, true);
437  extra_buffer_.swap(tmp);
438  tmp.reset();
439  pos_.store(extra_buffer_.data_size());
440  }
441 
442  void dump(const std::string& filename) override {
443  if (basic_size_ != 0 && extra_size_ == 0) {
444  basic_buffer_.resize(basic_size_, basic_pos_.load());
445  basic_buffer_.dump(filename);
446  } else if (basic_size_ == 0 && extra_size_ != 0) {
447  extra_buffer_.resize(extra_size_, pos_.load());
448  extra_buffer_.dump(filename);
449  } else {
451  tmp.open(filename, true);
453  (basic_size_ + extra_size_) * width_);
454  size_t offset = 0;
455  for (size_t k = 0; k < basic_size_; ++k) {
456  std::string_view val = basic_buffer_.get(k);
457  tmp.set(k, offset, val);
458  offset += val.size();
459  }
460  for (size_t k = 0; k < extra_size_; ++k) {
461  std::string_view val = extra_buffer_.get(k);
462  tmp.set(k + basic_size_, offset, extra_buffer_.get(k));
463  offset += val.size();
464  }
465  tmp.resize(basic_size_ + extra_size_, offset);
466  tmp.reset();
467  }
468  }
469 
470  size_t size() const override { return basic_size_ + extra_size_; }
471 
472  void resize(size_t size) override {
473  if (size < basic_buffer_.size()) {
474  basic_size_ = size;
475  extra_size_ = 0;
476  } else {
477  basic_size_ = basic_buffer_.size();
479  if (basic_buffer_.size() != 0) {
480  size_t basic_avg_width =
481  (basic_buffer_.data_size() + basic_buffer_.size() - 1) /
482  basic_buffer_.size();
483  // extra_size_ * basic_avg_width may be smaller than pos_.load()
484  extra_buffer_.resize(
485  extra_size_, std::max(extra_size_ * basic_avg_width, pos_.load()));
486  } else {
487  extra_buffer_.resize(extra_size_,
488  std::max(extra_size_ * width_, pos_.load()));
489  }
490  }
491  // resize `data` of basic_buffer
492  {
493  size_t pos = basic_pos_.load();
494  pos = pos + (pos + 4) / 5;
495  basic_buffer_.resize(basic_size_, pos);
496  }
497  }
498 
499  PropertyType type() const override { return PropertyType::Varchar(width_); }
500 
501  void set_value(size_t idx, const std::string_view& val) {
502  if (idx >= basic_size_ && idx < basic_size_ + extra_size_) {
503  size_t offset = pos_.fetch_add(val.size());
504  extra_buffer_.set(idx - basic_size_, offset, val);
505  } else if (idx < basic_size_) {
506  size_t offset = basic_pos_.fetch_add(val.size());
507  basic_buffer_.set(idx, offset, val);
508  } else {
509  LOG(FATAL) << "Index out of range";
510  }
511  }
512 
513  void set_any(size_t idx, const Any& value) override {
514  set_value(idx, value.AsStringView());
515  }
516 
517  // make sure there is enough space for the value
518  void set_value_with_check(size_t idx, const std::string_view& value) {
519  if (idx >= basic_size_ && idx < basic_size_ + extra_size_) {
520  size_t offset = pos_.fetch_add(value.size());
521  if (pos_.load() > extra_buffer_.data_size()) {
522  extra_buffer_.resize(extra_buffer_.size(), pos_.load());
523  }
524  extra_buffer_.set(idx - basic_size_, offset, value);
525  } else if (idx < basic_size_) {
526  size_t offset = basic_pos_.fetch_add(value.size());
527  if (basic_pos_.load() > basic_buffer_.data_size()) {
528  basic_buffer_.resize(basic_buffer_.size(), basic_pos_.load());
529  }
530  basic_buffer_.set(idx, offset, value);
531  } else {
532  LOG(FATAL) << "Index out of range";
533  }
534  }
535 
536  std::string_view get_view(size_t idx) const {
537  return idx < basic_size_ ? basic_buffer_.get(idx)
538  : extra_buffer_.get(idx - basic_size_);
539  }
540 
541  Any get(size_t idx) const override {
543  }
544 
545  void ingest(uint32_t index, grape::OutArchive& arc) override {
546  std::string_view val;
547  arc >> val;
548  set_value(index, val);
549  }
550 
552  return basic_buffer_;
553  }
554 
555  StorageStrategy storage_strategy() const override { return strategy_; }
556 
557  size_t basic_buffer_size() const { return basic_size_; }
558 
560  return extra_buffer_;
561  }
562 
563  size_t extra_buffer_size() const { return extra_size_; }
564 
565  private:
567  size_t basic_size_;
569  size_t extra_size_;
570  std::atomic<size_t> pos_;
571  std::atomic<size_t> basic_pos_;
573  uint16_t width_;
574 };
575 
577 template <typename INDEX_T>
578 class LFIndexer;
579 
580 template <typename INDEX_T>
581 class StringMapColumn : public ColumnBase {
582  public:
584  : index_col_(strategy), meta_map_(nullptr) {
586  meta_map_->init(
588  }
589 
591  if (meta_map_) {
592  meta_map_->close();
593  delete meta_map_;
594  }
595  index_col_.close();
596  }
597 
598  void copy_to_tmp(const std::string& cur_path,
599  const std::string& tmp_path) override {
600  meta_map_->copy_to_tmp(cur_path + ".map_meta", tmp_path + ".map_meta");
601  index_col_.copy_to_tmp(cur_path, tmp_path);
602  }
603  void open(const std::string& name, const std::string& snapshot_dir,
604  const std::string& work_dir) override;
605  void open_in_memory(const std::string& name) override;
606  void open_with_hugepages(const std::string& name, bool force) override;
607  void dump(const std::string& filename) override;
608 
609  void touch(const std::string& filename) override {
610  index_col_.touch(filename);
611  }
612 
613  void close() override {
614  if (meta_map_ != nullptr) {
615  meta_map_->close();
616  }
617  index_col_.close();
618  }
619 
620  size_t size() const override { return index_col_.size(); }
621  void resize(size_t size) override { index_col_.resize(size); }
622 
623  PropertyType type() const override { return PropertyType::kStringMap; }
624 
625  void set_value(size_t idx, const std::string_view& val);
626 
627  void set_any(size_t idx, const Any& value) override {
628  set_value(idx, value.AsStringView());
629  }
630 
631  std::string_view get_view(size_t idx) const;
632 
633  Any get(size_t idx) const override {
635  }
636 
637  void ingest(uint32_t index, grape::OutArchive& arc) override {
638  std::string_view val;
639  arc >> val;
640  set_value(index, val);
641  }
642 
644  return index_col_.storage_strategy();
645  }
646 
647  const TypedColumn<INDEX_T>& get_index_col() const { return index_col_; }
648  const LFIndexer<INDEX_T>& get_meta_map() const { return *meta_map_; }
649 
650  private:
653  grape::SpinLock lock_;
654 };
655 
656 template <typename INDEX_T>
657 void StringMapColumn<INDEX_T>::open(const std::string& name,
658  const std::string& snapshot_dir,
659  const std::string& work_dir) {
660  index_col_.open(name, snapshot_dir, work_dir);
661  meta_map_->open(name + ".map_meta", snapshot_dir, work_dir);
662  meta_map_->reserve(std::numeric_limits<INDEX_T>::max());
663 }
664 
665 template <typename INDEX_T>
666 void StringMapColumn<INDEX_T>::open_in_memory(const std::string& name) {
667  index_col_.open_in_memory(name);
668  meta_map_->open_in_memory(name + ".map_meta");
669  meta_map_->reserve(std::numeric_limits<INDEX_T>::max());
670 }
671 
672 template <typename INDEX_T>
674  bool force) {
675  index_col_.open_with_hugepages(name, force);
676  meta_map_->open_with_hugepages(name + ".map_meta", true);
677  meta_map_->reserve(std::numeric_limits<INDEX_T>::max());
678 }
679 
680 template <typename INDEX_T>
681 void StringMapColumn<INDEX_T>::dump(const std::string& filename) {
682  index_col_.dump(filename);
683  meta_map_->dump(filename + ".map_meta", "");
684 }
685 
686 template <typename INDEX_T>
687 std::string_view StringMapColumn<INDEX_T>::get_view(size_t idx) const {
688  INDEX_T ind = index_col_.get_view(idx);
689  return meta_map_->get_key(ind).AsStringView();
690 }
691 
692 template <typename INDEX_T>
694  const std::string_view& val) {
695  INDEX_T lid;
696  if (!meta_map_->get_index(val, lid)) {
697  lock_.lock();
698  if (!meta_map_->get_index(val, lid)) {
699  lid = meta_map_->insert(val);
700  }
701  lock_.unlock();
702  }
703  index_col_.set_value(idx, lid);
704 }
705 
707 
708 std::shared_ptr<ColumnBase> CreateColumn(
710  const std::vector<PropertyType>& sub_types = {});
711 
712 #ifdef USE_PTHASH
713 template <typename EDATA_T>
714 class ConcatColumn : public ColumnBase {
715  public:
716  ~ConcatColumn() {}
717 
718  ConcatColumn(const TypedColumn<EDATA_T>& basic_column,
719  const TypedColumn<EDATA_T>& extra_column)
720  : basic_column_(basic_column),
721  extra_column_(extra_column),
722  basic_size_(basic_column.size()) {}
723 
724  void open(const std::string& name, const std::string& snapshot_dir,
725  const std::string& work_dir) {
726  LOG(FATAL) << "not implemented";
727  }
728 
729  void open_in_memory(const std::string& name) {
730  LOG(FATAL) << "not implemented";
731  }
732 
733  void open_with_hugepages(const std::string& name, bool force) {
734  LOG(FATAL) << "not implemented";
735  }
736 
737  void close() { LOG(FATAL) << "not implemented"; }
738 
739  EDATA_T get_view(size_t index) const {
740  return index < basic_size_ ? basic_column_.get(index)
741  : extra_column_.get(index - basic_size_);
742  }
743 
744  void touch(const std::string& filename) { LOG(FATAL) << "not implemented"; }
745 
746  virtual void dump(const std::string& filename) {
747  LOG(FATAL) << "not implemented";
748  }
749 
750  size_t size() const { return basic_size_ + extra_column_.size(); }
751 
752  void copy_to_tmp(const std::string& cur_path, const std::string& tmp_path) {
753  LOG(FATAL) << "not implemented";
754  }
755  void resize(size_t size) { LOG(FATAL) << "not implemented"; }
756 
757  PropertyType type() const { return AnyConverter<EDATA_T>::type(); }
758 
759  void set_any(size_t index, const Any& value) {
760  LOG(FATAL) << "not implemented";
761  }
762 
763  Any get(size_t index) const {
764  if (index < basic_size_) {
765  return basic_column_.get(index);
766  } else {
767  return extra_column_.get(index - basic_size_);
768  }
769  }
770 
771  void ingest(uint32_t index, grape::OutArchive& arc) {
772  LOG(FATAL) << "not implemented";
773  }
774 
775  StorageStrategy storage_strategy() const {
776  return basic_column_.storage_strategy();
777  }
778 
779  private:
780  const TypedColumn<EDATA_T>& basic_column_;
781  const TypedColumn<EDATA_T>& extra_column_;
782  size_t basic_size_;
783 };
784 #endif
785 
788  public:
789  virtual ~RefColumnBase() {}
790  virtual Any get(size_t index) const = 0;
791 };
792 
793 // Different from TypedColumn, RefColumn is a wrapper of mmap_array
794 template <typename T>
796  public:
797  using value_type = T;
798 
800  : basic_buffer(buffer),
801  basic_size(0),
802  extra_buffer(buffer),
803  extra_size(buffer.size()),
804  strategy_(strategy) {}
806  : basic_buffer(column.basic_buffer()),
807  basic_size(column.basic_buffer_size()),
808  extra_buffer(column.extra_buffer()),
809  extra_size(column.extra_buffer_size()),
810  strategy_(column.storage_strategy()) {}
812 
813  inline T get_view(size_t index) const {
814  return index < basic_size ? basic_buffer.get(index)
815  : extra_buffer.get(index - basic_size);
816  }
817 
818  size_t size() const { return basic_size + extra_size; }
819 
820  Any get(size_t index) const override {
821  return AnyConverter<T>::to_any(get_view(index));
822  }
823 
824  private:
826  size_t basic_size;
828  size_t extra_size;
829 
831 };
832 
833 template <>
835  public:
836  TypedRefColumn(LabelKey label_key) : label_key_(label_key) {}
837 
839 
840  inline LabelKey get_view(size_t index) const { return label_key_; }
841 
842  Any get(size_t index) const override {
843  LOG(ERROR) << "LabelKeyColumn does not support get() to Any";
844  return Any();
845  }
846 
847  private:
849 };
850 
851 template <>
853  public:
855  TypedRefColumn(label_t label_key) : label_key_(label_key) {}
856 
858 
859  inline GlobalId get_view(size_t index) const {
860  return GlobalId(label_key_, index);
861  }
862 
863  Any get(size_t index) const override {
864  LOG(ERROR) << "GlobalId Column does not support get() to Any";
865  return Any();
866  }
867 
868  private:
870 };
871 
872 // Create a reference column from a ColumnBase that contains a const reference
873 // to the actual column storage, offering a column-based store interface for
874 // vertex properties.
875 std::shared_ptr<RefColumnBase> CreateRefColumn(
876  std::shared_ptr<ColumnBase> column);
877 
878 } // namespace gs
879 
880 #endif // GRAPHSCOPE_PROPERTY_COLUMN_H_
gs::TypedColumn< std::string_view >::extra_buffer
const mmap_array< std::string_view > & extra_buffer() const
Definition: column.h:559
gs::TypedColumn< std::string_view >::extra_buffer_size
size_t extra_buffer_size() const
Definition: column.h:563
grape
Definition: types.h:33
gs::TypedColumn::touch
void touch(const std::string &filename) override
Definition: column.h:117
gs::TypedColumn::resize
void resize(size_t size) override
Definition: column.h:173
gs::TypedRefColumn::get_view
T get_view(size_t index) const
Definition: column.h:813
gs::TypedRefColumn::size
size_t size() const
Definition: column.h:818
gs::TypedColumn< std::string_view >::basic_buffer_size
size_t basic_buffer_size() const
Definition: column.h:557
gs::TypedRefColumn< LabelKey >::get
Any get(size_t index) const override
Definition: column.h:842
gs::TypedColumn::open
void open(const std::string &name, const std::string &snapshot_dir, const std::string &work_dir) override
Definition: column.h:69
gs::mmap_array< std::string_view >::resize
void resize(size_t size, size_t data_size)
Definition: mmap_array.h:483
gs::mmap_array< std::string_view >::size
size_t size() const
Definition: mmap_array.h:498
gs::StringMapColumn::get_meta_map
const LFIndexer< INDEX_T > & get_meta_map() const
Definition: column.h:648
gs::StringMapColumn::dump
void dump(const std::string &filename) override
Definition: column.h:681
gs::Any
Definition: types.h:383
gs::TypedColumn< std::string_view >::type
PropertyType type() const override
Definition: column.h:499
gs::StringMapColumn::get_view
std::string_view get_view(size_t idx) const
Definition: column.h:687
gs::ColumnBase::open
virtual void open(const std::string &name, const std::string &snapshot_dir, const std::string &work_dir)=0
gs::TypedColumn< RecordView >::types_
std::vector< PropertyType > types_
Definition: column.h:291
gs::TypedColumn< std::string_view >::resize
void resize(size_t size) override
Definition: column.h:472
gs::TypedColumn::TypedColumn
TypedColumn(StorageStrategy strategy)
Definition: column.h:66
gs::TypedColumn< std::string_view >::dump
void dump(const std::string &filename) override
Definition: column.h:442
gs::TypedColumn::set_value
void set_value(size_t index, const T &val)
Definition: column.h:186
gs::TypedColumn< RecordView >::touch
void touch(const std::string &filename) override
Definition: column.h:252
gs::ColumnBase::type
virtual PropertyType type() const =0
types.h
gs::TypedColumn< RecordView >::type
PropertyType type() const override
Definition: column.h:269
gs::mmap_array< std::string_view >::open
void open(const std::string &filename, bool sync_to_file)
Definition: mmap_array.h:463
gs::StringMapColumn::lock_
grape::SpinLock lock_
Definition: column.h:653
gs::TypedColumn< std::string_view >::basic_buffer
const mmap_array< std::string_view > & basic_buffer() const
Definition: column.h:551
gs::TypedColumn< RecordView >::table_
std::shared_ptr< Table > table_
Definition: column.h:292
gs::TypedColumn< std::string_view >::width_
uint16_t width_
Definition: column.h:573
gs::TypedRefColumn< GlobalId >::~TypedRefColumn
~TypedRefColumn()
Definition: column.h:857
gs::TypedRefColumn< LabelKey >::label_key_
LabelKey label_key_
Definition: column.h:848
gs::TypedColumn< grape::EmptyType >::open_with_hugepages
void open_with_hugepages(const std::string &name, bool force) override
Definition: column.h:315
gs::TypedColumn::basic_buffer_
mmap_array< T > basic_buffer_
Definition: column.h:223
gs::TypedColumn< grape::EmptyType >::set_any
void set_any(size_t index, const Any &value) override
Definition: column.h:326
gs::RefColumnBase
Create RefColumn for ease of usage for hqps.
Definition: column.h:787
gs::TypedColumn< RecordView >::storage_strategy
StorageStrategy storage_strategy() const override
Definition: column.h:283
gs::TypedColumn::close
void close() override
Definition: column.h:134
gs::StringMapColumn::get_index_col
const TypedColumn< INDEX_T > & get_index_col() const
Definition: column.h:647
gs::TypedColumn::size
size_t size() const override
Definition: column.h:171
gs::StringMapColumn::ingest
void ingest(uint32_t index, grape::OutArchive &arc) override
Definition: column.h:637
gs::TypedRefColumn::extra_buffer
const mmap_array< T > & extra_buffer
Definition: column.h:827
gs::TypedColumn< std::string_view >::size
size_t size() const override
Definition: column.h:470
gs::StringMapColumn::set_any
void set_any(size_t idx, const Any &value) override
Definition: column.h:627
gs::StringMapColumn::StringMapColumn
StringMapColumn(StorageStrategy strategy)
Definition: column.h:583
gs::TypedColumn< RecordView >::open
void open(const std::string &name, const std::string &snapshot_dir, const std::string &work_dir) override
Definition: column.h:241
gs::TypedColumn< grape::EmptyType >::storage_strategy
StorageStrategy storage_strategy() const override
Definition: column.h:332
gs::StringMapColumn::close
void close() override
Definition: column.h:613
gs::PropertyType::Varchar
static PropertyType Varchar(uint16_t max_length)
Definition: types.cc:323
gs::TypedColumn< std::string_view >::~TypedColumn
~TypedColumn()
Definition: column.h:343
gs::TypedColumn::open_with_hugepages
void open_with_hugepages(const std::string &name, bool force) override
Definition: column.h:98
gs::mmap_array::resize
void resize(size_t size)
Definition: mmap_array.h:319
gs::StringMapColumn::open_with_hugepages
void open_with_hugepages(const std::string &name, bool force) override
Definition: column.h:673
gs::GlobalId
Definition: types.h:163
gs::StorageStrategy::kDisk
@ kDisk
gs::TypedColumn::type
PropertyType type() const override
Definition: column.h:184
gs::StringMapColumn::size
size_t size() const override
Definition: column.h:620
gs::LabelKey::label_data_type
uint8_t label_data_type
Definition: types.h:277
gs::TypedColumn< std::string_view >::open_in_memory
void open_in_memory(const std::string &prefix) override
Definition: column.h:366
gs
Definition: adj_list.h:23
gs::TypedColumn
Definition: column.h:64
gs::StringMapColumn::~StringMapColumn
~StringMapColumn()
Definition: column.h:590
gs::TypedColumn< RecordView >::dump
void dump(const std::string &filename) override
Definition: column.h:256
gs::TypedColumn< grape::EmptyType >::get
Any get(size_t index) const override
Definition: column.h:328
gs::PropertyType::kEmpty
static const PropertyType kEmpty
Definition: types.h:133
gs::StorageStrategy
StorageStrategy
Definition: types.h:58
gs::StringMapColumn::index_col_
TypedColumn< INDEX_T > index_col_
Definition: column.h:651
gs::TypedColumn< std::string_view >::close
void close() override
Definition: column.h:418
gs::TypedRefColumn::get
Any get(size_t index) const override
Definition: column.h:820
gs::CreateRefColumn
std::shared_ptr< RefColumnBase > CreateRefColumn(std::shared_ptr< ColumnBase > column)
Definition: column.cc:187
gs::TypedColumn::ingest
void ingest(uint32_t index, grape::OutArchive &arc) override
Definition: column.h:209
gs::TypedColumn::get
Any get(size_t index) const override
Definition: column.h:205
gs::mmap_array< std::string_view >::reset
void reset()
Definition: mmap_array.h:453
gs::TypedRefColumn< LabelKey >::get_view
LabelKey get_view(size_t index) const
Definition: column.h:840
gs::TypedColumn::storage_strategy
StorageStrategy storage_strategy() const override
Definition: column.h:215
gs::TypedColumn< std::string_view >::extra_buffer_
mmap_array< std::string_view > extra_buffer_
Definition: column.h:568
gs::mmap_array::open
void open(const std::string &filename, bool sync_to_file=false)
Definition: mmap_array.h:129
gs::StringMapColumn::resize
void resize(size_t size) override
Definition: column.h:621
gs::TypedRefColumn::basic_size
size_t basic_size
Definition: column.h:826
gs::TypedColumn::extra_buffer
const mmap_array< T > & extra_buffer() const
Definition: column.h:219
gs::StringMapColumn::open_in_memory
void open_in_memory(const std::string &name) override
Definition: column.h:666
gs::TypedColumn< grape::EmptyType >::strategy_
StorageStrategy strategy_
Definition: column.h:335
gs::TypedRefColumn::extra_size
size_t extra_size
Definition: column.h:828
gs::ColumnBase::storage_strategy
virtual StorageStrategy storage_strategy() const =0
gs::TypedColumn< grape::EmptyType >::TypedColumn
TypedColumn(StorageStrategy strategy)
Definition: column.h:309
gs::PropertyType::STRING_DEFAULT_MAX_LENGTH
static constexpr const uint16_t STRING_DEFAULT_MAX_LENGTH
Definition: types.h:96
gs::TypedRefColumn< GlobalId >::get
Any get(size_t index) const override
Definition: column.h:863
gs::TypedColumn< std::string_view >::strategy_
StorageStrategy strategy_
Definition: column.h:572
gs::TypedColumn::get_view
T get_view(size_t index) const
Definition: column.h:200
gs::TypedRefColumn::TypedRefColumn
TypedRefColumn(const TypedColumn< T > &column)
Definition: column.h:805
gs::CreateColumn
std::shared_ptr< ColumnBase > CreateColumn(PropertyType type, StorageStrategy strategy, const std::vector< PropertyType > &sub_types)
Definition: column.cc:114
gs::TypedColumn< std::string_view >::pos_
std::atomic< size_t > pos_
Definition: column.h:570
gs::TypedColumn< grape::EmptyType >::ingest
void ingest(uint32_t index, grape::OutArchive &arc) override
Definition: column.h:330
gs::TypedColumn< std::string_view >::set_value
void set_value(size_t idx, const std::string_view &val)
Definition: column.h:501
gs::TypedColumn::basic_buffer_size
size_t basic_buffer_size() const
Definition: column.h:218
gs::TypedRefColumn::TypedRefColumn
TypedRefColumn(const mmap_array< T > &buffer, StorageStrategy strategy)
Definition: column.h:799
gs::TypedColumn< std::string_view >::storage_strategy
StorageStrategy storage_strategy() const override
Definition: column.h:555
gs::TypedColumn< std::string_view >
Definition: column.h:338
gs::TypedColumn< std::string_view >::TypedColumn
TypedColumn(StorageStrategy strategy, uint16_t width=PropertyType::STRING_DEFAULT_MAX_LENGTH)
Definition: column.h:340
gs::TypedColumn< std::string_view >::basic_pos_
std::atomic< size_t > basic_pos_
Definition: column.h:571
gs::TypedColumn< RecordView >::~TypedColumn
~TypedColumn()
Definition: column.h:239
gs::TypedColumn< std::string_view >::basic_size_
size_t basic_size_
Definition: column.h:567
gs::TypedRefColumn< LabelKey >::~TypedRefColumn
~TypedRefColumn()
Definition: column.h:838
gs::TypedColumn::open_in_memory
void open_in_memory(const std::string &name) override
Definition: column.h:86
gs::TypedColumn< std::string_view >::open_with_hugepages
void open_with_hugepages(const std::string &prefix, bool force) override
Definition: column.h:376
gs::mmap_array::size
size_t size() const
Definition: mmap_array.h:415
gs::TypedColumn< RecordView >::TypedColumn
TypedColumn(const std::vector< PropertyType > &types)
Definition: column.h:233
gs::ColumnBase::copy_to_tmp
virtual void copy_to_tmp(const std::string &cur_path, const std::string &tmp_path)=0
gs::StringMapColumn::open
void open(const std::string &name, const std::string &snapshot_dir, const std::string &work_dir) override
Definition: column.h:657
gs::PropertyType::kRecordView
static const PropertyType kRecordView
Definition: types.h:149
gs::ColumnBase::set_any
virtual void set_any(size_t index, const Any &value)=0
gs::StringMapColumn::touch
void touch(const std::string &filename) override
Definition: column.h:609
gs::mmap_array< std::string_view >
Definition: mmap_array.h:447
gs::TypedColumn::strategy_
StorageStrategy strategy_
Definition: column.h:227
gs::TypedColumn< std::string_view >::extra_size_
size_t extra_size_
Definition: column.h:569
gs::RefColumnBase::~RefColumnBase
virtual ~RefColumnBase()
Definition: column.h:789
gs::LabelKey
Definition: types.h:276
gs::TypedColumn< RecordView >::ingest
void ingest(uint32_t index, grape::OutArchive &arc) override
Definition: column.h:279
gs::TypedColumn< std::string_view >::touch
void touch(const std::string &filename) override
Definition: column.h:392
gs::TypedColumn::~TypedColumn
~TypedColumn()
Definition: column.h:67
gs::TypedColumn< grape::EmptyType >::size
size_t size() const override
Definition: column.h:321
gs::TypedRefColumn< GlobalId >::TypedRefColumn
TypedRefColumn(label_t label_key)
Definition: column.h:855
gs::ColumnBase::dump
virtual void dump(const std::string &filename)=0
gs::TypedColumn::dump
void dump(const std::string &filename) override
Definition: column.h:154
gs::TypedColumn::extra_buffer_
mmap_array< T > extra_buffer_
Definition: column.h:225
gs::TypedColumn< grape::EmptyType >::~TypedColumn
~TypedColumn()
Definition: column.h:310
gs::TypedRefColumn::value_type
T value_type
Definition: column.h:797
gs::TypedColumn< grape::EmptyType >::open
void open(const std::string &name, const std::string &snapshot_dir, const std::string &work_dir) override
Definition: column.h:312
mmap_array.h
gs::ColumnBase::get
virtual Any get(size_t index) const =0
gs::StringMapColumn
Definition: column.h:581
gs::ColumnBase::~ColumnBase
virtual ~ColumnBase()
Definition: column.h:31
gs::ColumnBase::size
virtual size_t size() const =0
gs::StringMapColumn::type
PropertyType type() const override
Definition: column.h:623
gs::ColumnBase::open_in_memory
virtual void open_in_memory(const std::string &name)=0
gs::TypedColumn< std::string_view >::ingest
void ingest(uint32_t index, grape::OutArchive &arc) override
Definition: column.h:545
gs::copy_file
void copy_file(const std::string &src, const std::string &dst)
Definition: file_names.h:80
gs::TypedRefColumn< GlobalId >::get_view
GlobalId get_view(size_t index) const
Definition: column.h:859
gs::TypedColumn::extra_size_
size_t extra_size_
Definition: column.h:226
gs::TypedColumn::set_any
void set_any(size_t index, const Any &value) override
Definition: column.h:196
gs::TypedRefColumn
Definition: column.h:795
gs::ColumnBase::close
virtual void close()=0
gs::TypedColumn< grape::EmptyType >::touch
void touch(const std::string &filename) override
Definition: column.h:316
gs::mmap_array
Definition: mmap_array.h:65
gs::StringMapColumn::get
Any get(size_t idx) const override
Definition: column.h:633
gs::StringMapColumn::storage_strategy
StorageStrategy storage_strategy() const override
Definition: column.h:643
gs::snapshot_dir
std::string snapshot_dir(const std::string &work_dir, uint32_t version)
Definition: file_names.h:192
std
Definition: loading_config.h:232
gs::TypedRefColumn::strategy_
StorageStrategy strategy_
Definition: column.h:830
gs::ColumnBase::open_with_hugepages
virtual void open_with_hugepages(const std::string &name, bool force)=0
gs::TypedColumn< RecordView >::copy_to_tmp
void copy_to_tmp(const std::string &cur_path, const std::string &tmp_path) override
Definition: column.h:260
gs::TypedColumn< std::string_view >::open
void open(const std::string &name, const std::string &snapshot_dir, const std::string &work_dir) override
Definition: column.h:345
gs::TypedColumn< grape::EmptyType >::close
void close() override
Definition: column.h:320
gs::TypedColumn< std::string_view >::get_view
std::string_view get_view(size_t idx) const
Definition: column.h:536
gs::StringMapColumn::copy_to_tmp
void copy_to_tmp(const std::string &cur_path, const std::string &tmp_path) override
Definition: column.h:598
gs::RefColumnBase::get
virtual Any get(size_t index) const =0
gs::TypedColumn< RecordView >::sub_types
std::vector< PropertyType > sub_types() const
Definition: column.h:288
gs::AnyConverter
Definition: types.h:381
gs::TypedRefColumn::basic_buffer
const mmap_array< T > & basic_buffer
Definition: column.h:825
gs::ColumnBase::ingest
virtual void ingest(uint32_t index, grape::OutArchive &arc)=0
gs::TypedColumn< grape::EmptyType >::dump
void dump(const std::string &filename) override
Definition: column.h:317
gs::TypedColumn< std::string_view >::get
Any get(size_t idx) const override
Definition: column.h:541
gs::mmap_array< std::string_view >::set
void set(size_t idx, size_t offset, const std::string_view &val)
Definition: mmap_array.h:488
gs::TypedColumn::extra_buffer_size
size_t extra_buffer_size() const
Definition: column.h:220
gs::TypedRefColumn::~TypedRefColumn
~TypedRefColumn()
Definition: column.h:811
gs::TypedRefColumn< GlobalId >::label_key_
label_t label_key_
Definition: column.h:869
gs::TypedColumn< grape::EmptyType >::resize
void resize(size_t size) override
Definition: column.h:322
gs::TypedColumn< grape::EmptyType >::open_in_memory
void open_in_memory(const std::string &name) override
Definition: column.h:314
gs::TypedColumn< std::string_view >::copy_to_tmp
void copy_to_tmp(const std::string &cur_path, const std::string &tmp_path) override
Definition: column.h:423
gs::StringMapColumn::meta_map_
LFIndexer< INDEX_T > * meta_map_
Definition: column.h:652
gs::PropertyType
Definition: types.h:95
gs::RecordView
Definition: types.h:285
gs::TypedColumn< RecordView >
Definition: column.h:231
gs::TypedRefColumn< LabelKey >::TypedRefColumn
TypedRefColumn(LabelKey label_key)
Definition: column.h:836
gs::StringMapColumn::set_value
void set_value(size_t idx, const std::string_view &val)
Definition: column.h:693
gs::TypedColumn< grape::EmptyType >::type
PropertyType type() const override
Definition: column.h:324
gs::ColumnBase::resize
virtual void resize(size_t size)=0
gs::mmap_array::reset
void reset()
Definition: mmap_array.h:84
gs::mmap_array::set
void set(size_t idx, const T &val)
Definition: mmap_array.h:408
gs::ColumnBase::touch
virtual void touch(const std::string &filename)=0
gs::TypedColumn::copy_to_tmp
void copy_to_tmp(const std::string &cur_path, const std::string &tmp_path) override
Definition: column.h:139
gs::TypedColumn::basic_buffer
const mmap_array< T > & basic_buffer() const
Definition: column.h:217
gs::LFIndexer
Definition: id_indexer.h:184
gs::PropertyType::kStringMap
static const PropertyType kStringMap
Definition: types.h:146
gs::TypedColumn< std::string_view >::set_any
void set_any(size_t idx, const Any &value) override
Definition: column.h:513
gs::TypedRefColumn< GlobalId >::label_t
typename LabelKey::label_data_type label_t
Definition: column.h:854
gs::ColumnBase
Definition: column.h:29
gs::StorageStrategy::kMem
@ kMem
gs::Any::AsStringView
std::string_view AsStringView() const
Definition: types.h:641
gs::TypedColumn::basic_size_
size_t basic_size_
Definition: column.h:224
gs::TypedColumn< std::string_view >::basic_buffer_
mmap_array< std::string_view > basic_buffer_
Definition: column.h:566
gs::TypedColumn< std::string_view >::set_value_with_check
void set_value_with_check(size_t idx, const std::string_view &value)
Definition: column.h:518
gs::TypedColumn< RecordView >::open_with_hugepages
void open_with_hugepages(const std::string &name, bool force) override
Definition: column.h:248
gs::TypedColumn< grape::EmptyType >::copy_to_tmp
void copy_to_tmp(const std::string &cur_path, const std::string &tmp_path) override
Definition: column.h:318