1 /*  This file is part of Jellyfish.
2 
3     This work is dual-licensed under 3-Clause BSD License or GPL 3.0.
4     You can choose between one of them if you use this work.
5 
6 `SPDX-License-Identifier: BSD-3-Clause OR  GPL-3.0`
7 */
8 
9 #ifndef __JELLYFISH_TEXT_DUMPER_HPP__
10 #define __JELLYFISH_TEXT_DUMPER_HPP__
11 
12 #include <jellyfish/sorted_dumper.hpp>
13 
14 namespace jellyfish {
15 template<typename Key, typename Val>
16 class text_writer {
17 public:
write(std::ostream & out,const Key & key,const Val val)18   void write(std::ostream& out, const Key& key, const Val val) {
19    out << key << " " << val << "\n";
20   }
21 };
22 
23 template<typename storage_t>
24 class text_dumper : public sorted_dumper<text_dumper<storage_t>, storage_t> {
25   typedef sorted_dumper<text_dumper<storage_t>, storage_t> super;
26   text_writer<typename super::key_type, uint64_t> writer;
27 
28 public:
29   static const char* format;
30 
text_dumper(int nb_threads,const char * file_prefix,file_header * header=0)31   text_dumper(int nb_threads, const char* file_prefix, file_header* header = 0) :
32     super(nb_threads, file_prefix, header)
33   { }
34 
_dump(storage_t * ary)35   virtual void _dump(storage_t* ary) {
36     if(super::header_) {
37       super::header_->update_from_ary(*ary);
38       super::header_->format(format);
39     }
40     super::_dump(ary);
41   }
42 
write_key_value_pair(std::ostream & out,typename super::heap_item item)43   void write_key_value_pair(std::ostream& out, typename super::heap_item item) {
44     writer.write(out, item->key_, item->val_);
45   }
46 };
47 template<typename storage_t>
48 const char* jellyfish::text_dumper<storage_t>::format = "text/sorted";
49 
50 template<typename Key, typename Val>
51 class text_reader {
52   std::istream& is_;
53   char* buffer_;
54   Key key_;
55   Val val_;
56   const RectangularBinaryMatrix m_;
57   const size_t                  size_mask_;
58 
59 public:
text_reader(std::istream & is,file_header * header)60   text_reader(std::istream& is,
61               file_header* header) :
62     is_(is),
63     buffer_(new char[header->key_len() / 2 + 1]),
64     key_(header->key_len() / 2),
65     m_(header->matrix()),
66     size_mask_(header->size() - 1)
67   { }
68 
key() const69   const Key& key() const { return key_; }
val() const70   const Val& val() const { return val_; }
pos() const71   size_t pos() const { return m_.times(key()) & size_mask_; }
72 
next()73   bool next() {
74     is_ >> key_ >> val_;
75     return is_.good();
76   }
77 };
78 }
79 
80 #endif /* __JELLYFISH_TEXT_DUMPER_HPP__ */
81