MADARA  3.4.1
FileStreamer.h
Go to the documentation of this file.
1 #ifndef _MADARA_KNOWLEDGE_FILESTREAMER_H_
2 #define _MADARA_KNOWLEDGE_FILESTREAMER_H_
3 
4 #include <string>
5 #include <fstream>
6 #include <vector>
13 
14 namespace madara
15 {
16 namespace knowledge
17 {
24 {
25 public:
30 
38  const std::string& key, const std::string& filename, KnowledgeBase kb)
39  {
40  init(key, filename, kb);
41  }
42 
49  inline void init(
50  const std::string& key, const std::string& filename, KnowledgeBase kb)
51  {
52  // setup containers
53  file_space.set_name(key, kb);
58 
59  file_crc.set_name(file_space["crc"].get_name(), kb);
60  file_size.set_name(file_space["size"].get_name(), kb);
61 
62  // setup file information
63  filename_ = filename;
66 
67  stream_.open(filename, std::ios::in | std::ios::binary);
68  }
69 
73  inline size_t load(size_t index, size_t frag_size = 60000)
74  {
75  size_t bytes_read = 0;
76 
77  if (stream_)
78  {
79  // seek to the index position in the file
80  stream_.seekg((std::streampos)index * frag_size, std::ios::beg);
81 
82  // read the fragment into a local buffer
83  std::vector<unsigned char> frag(frag_size);
84  // char * frag = new char [frag_size];
85  stream_.read((char*)frag.data(), frag_size);
86  bytes_read = stream_.gcount();
87  frag.resize(bytes_read);
88 
89  KnowledgeRecord record;
90  record.emplace_file(std::move(frag));
91 
92  // set the file fragment into the KB
93  file_fragments.set(std::to_string((unsigned long long)index), record);
94  // file_fragments.emplace_file ()
95  }
96 
97  return bytes_read;
98  }
99 
104  inline uint32_t get_crc(void)
105  {
106  return (uint32_t)file_crc.to_integer();
107  }
108 
113  inline size_t get_size(void)
114  {
115  return (size_t)file_size.to_integer();
116  }
117 
123  {
124  return filename_;
125  }
126 
130  inline void clear_fragments(void)
131  {
133  }
134 
138  inline void modify(void)
139  {
140  file_size.modify();
141  file_crc.modify();
142  }
143 
146 
149 
152 
155 
156 private:
159 
161  std::ifstream stream_;
162 };
163 }
164 }
165 
166 #endif // _MADARA_KNOWLEDGE_FILESTREAMER_H_
Splits files into fragments that can be saved to and loaded from a knowledge base.
Definition: FileStreamer.h:24
containers::Map file_fragments
the crc of the file
Definition: FileStreamer.h:154
containers::FlexMap file_space
records that contain the file fragments
Definition: FileStreamer.h:145
FileStreamer(const std::string &key, const std::string &filename, KnowledgeBase kb)
Constructor.
Definition: FileStreamer.h:37
std::ifstream stream_
the stream to read from
Definition: FileStreamer.h:161
void init(const std::string &key, const std::string &filename, KnowledgeBase kb)
Initializes the file stream and all containers.
Definition: FileStreamer.h:49
std::string filename_
the name of the file
Definition: FileStreamer.h:158
uint32_t get_crc(void)
Returns the crc of the file.
Definition: FileStreamer.h:104
size_t load(size_t index, size_t frag_size=60000)
Loads a fragment into the knowledge base.
Definition: FileStreamer.h:73
containers::Integer file_crc
the crc of the file
Definition: FileStreamer.h:151
void modify(void)
Remodifies the file size and crc.
Definition: FileStreamer.h:138
containers::Integer file_size
the size of the file contents
Definition: FileStreamer.h:148
std::string get_filename(void)
Returns the name of the file.
Definition: FileStreamer.h:122
void clear_fragments(void)
Clears the fragment list.
Definition: FileStreamer.h:130
size_t get_size(void)
Returns the size of the file.
Definition: FileStreamer.h:113
This class provides a distributed knowledge base to users.
Definition: KnowledgeBase.h:45
This class encapsulates an entry in a KnowledgeBase.
void emplace_file(Args &&... args)
Construct a file (vector of unsigned char) within this KnowledgeRecord.
This class stores a flexible map of strings and ints to KaRL variables FlexMap differs from Map in th...
Definition: FlexMap.h:54
void set_name(const std::string &var_name, KnowledgeBase &knowledge)
Sets the variable name that this refers to.
Definition: FlexMap.cpp:573
void to_container(BufferVector &target) const
Fills a BufferVector container with all subkeys.
Definition: FlexMap.cpp:280
This class stores an integer within a variable context.
Definition: Integer.h:32
knowledge::KnowledgeRecord::Integer to_integer(void) const
Returns the value as an integer (same as *)
Definition: Integer.inl:282
void set_name(const std::string &var_name, KnowledgeBase &knowledge)
Sets the variable name that this refers to.
Definition: Integer.inl:54
void modify(void)
Mark the value as modified.
Definition: Integer.inl:7
This class stores a map of strings to KaRL variables.
Definition: Map.h:33
int set(const std::string &key, madara::knowledge::KnowledgeRecord::Integer value=madara::knowledge::KnowledgeRecord::MODIFIED)
Sets a location within the map to the specified value.
Definition: Map.cpp:582
void clear(bool clear_knowledge=true)
Clears the map.
Definition: Map.cpp:355
constexpr string_t string
constexpr binary_t binary
Provides functions and classes for the distributed knowledge base.
MADARA_EXPORT void frag(const char *source, uint64_t total_size, const char *originator, const char *domain, uint64_t clock, uint64_t timestamp, uint32_t quality, unsigned char ttl, uint64_t fragment_size, FragmentMap &map)
Breaks a large packet into smaller packets.
unsigned int file_size(const std::string &filename)
Returns the size of a file.
Definition: Utility.inl:395
uint32_t file_crc(const std::string &filename, size_t max_block=1000000)
Returns the crc of a file.
Definition: Utility.inl:423
Copyright(c) 2020 Galois.