1 /*  This file is part of Jellyfish.
2 
3     Jellyfish is free software: you can redistribute it and/or modify
4     it under the terms of the GNU General Public License as published by
5     the Free Software Foundation, either version 3 of the License, or
6     (at your option) any later version.
7 
8     Jellyfish is distributed in the hope that it will be useful,
9     but WITHOUT ANY WARRANTY; without even the implied warranty of
10     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11     GNU General Public License for more details.
12 
13     You should have received a copy of the GNU General Public License
14     along with Jellyfish.  If not, see <http://www.gnu.org/licenses/>.
15 */
16 
17 #ifndef __JELLYFISH_TEXT_DUMPER_HPP__
18 #define __JELLYFISH_TEXT_DUMPER_HPP__
19 
20 #include <jellyfish/sorted_dumper.hpp>
21 
22 namespace jellyfish {
23 template<typename Key, typename Val>
24 class text_writer {
25 public:
write(std::ostream & out,const Key & key,const Val val)26   void write(std::ostream& out, const Key& key, const Val val) {
27    out << key << " " << val << "\n";
28   }
29 };
30 
31 template<typename storage_t>
32 class text_dumper : public sorted_dumper<text_dumper<storage_t>, storage_t> {
33   typedef sorted_dumper<text_dumper<storage_t>, storage_t> super;
34   text_writer<typename super::key_type, uint64_t> writer;
35 
36 public:
37   static const char* format;
38 
text_dumper(int nb_threads,const char * file_prefix,file_header * header=0)39   text_dumper(int nb_threads, const char* file_prefix, file_header* header = 0) :
40     super(nb_threads, file_prefix, header)
41   { }
42 
_dump(storage_t * ary)43   virtual void _dump(storage_t* ary) {
44     if(super::header_) {
45       super::header_->update_from_ary(*ary);
46       super::header_->format(format);
47     }
48     super::_dump(ary);
49   }
50 
write_key_value_pair(std::ostream & out,typename super::heap_item item)51   void write_key_value_pair(std::ostream& out, typename super::heap_item item) {
52     writer.write(out, item->key_, item->val_);
53   }
54 };
55 template<typename storage_t>
56 const char* jellyfish::text_dumper<storage_t>::format = "text/sorted";
57 
58 template<typename Key, typename Val>
59 class text_reader {
60   std::istream& is_;
61   char* buffer_;
62   Key key_;
63   Val val_;
64   const RectangularBinaryMatrix m_;
65   const size_t                  size_mask_;
66 
67 public:
text_reader(std::istream & is,file_header * header)68   text_reader(std::istream& is,
69               file_header* header) :
70     is_(is),
71     buffer_(new char[header->key_len() / 2 + 1]),
72     key_(header->key_len() / 2),
73     m_(header->matrix()),
74     size_mask_(header->size() - 1)
75   { }
76 
key() const77   const Key& key() const { return key_; }
val() const78   const Val& val() const { return val_; }
pos() const79   size_t pos() const { return m_.times(key()) & size_mask_; }
80 
next()81   bool next() {
82     is_ >> key_ >> val_;
83     return is_.good();
84   }
85 };
86 }
87 
88 #endif /* __JELLYFISH_TEXT_DUMPER_HPP__ */
89