-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* * Added madara::utility::file_crc * Added madara::utility::file_size for file streams * Added internals of FragmentsToFilesFilter to look for MFS file fragments * Added tests for Fragments filter and new utility functions * [skip ci] * * [skip ci] * FragmentsToFilesFilter now takes fragments and saves them to disk in the format {filename}.{frag}.{crc}.frag * Still need to piece everything together when the fragments are done * Updated test_filters.cpp to test changes to FragmentsToFilesFilter * Added filename_has_redirects to madara::utility for checking for .., ~, etc. * * Added file_from_fragments and the ability to merge fragments and delete them through the utility function * Added calls to new utility function from FragmentsToFilesFilter.cpp * * Added FragmentsToFilesFilter implementation for piecing together files from MFS transfers * Updated python port documentation * Added read_thread_hertz to Python TransportSettings * Added AggregateFilter to Python port * Added FragmentsToFilesFilter to Python port * Added utility functions to Python port * Added read thread hertz setting capability to karl and mfs * Updated test_filters to properly test for MFS file transfer * Added FileReceiver python test for MFS transfer * * Moved python test into same folder that David put the test_any.py into * * Using same naming convention as other test * * correcting issue with the file_receiver test being saved to wrong dir * * Removed ALWAYS logging in FragmentsToFilesFilter * Removed boost::filesystem usage in karl * Removed boost::filesystem usage in Utility * Updated test_filters to test for SUCCESS/FAIL with FragmentsToFilesFilter * * Cleaned up tools/karl segfault and style issues * * Implementing equivalent of path.filename from boost::filesystem in karl.cpp * * Added FileStreamer * Updated MFS to use FileStreamer * Added reset to containers::Map * Added clear by VariableReference to ThreadSafeContent * Segfault occurs if I try to clear map entries in any way * * Fixed memory corruption issue with containers::Map and the clear/reset methods * Fixed heap related issues for FileStreamer. FileStreamer should fix the issue with instability with large files that was being experienced by FileFragmenter in MFS
- Loading branch information
1 parent
1cc4d47
commit 16d02f7
Showing
8 changed files
with
317 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
#ifndef _MADARA_KNOWLEDGE_FILESTREAMER_H_ | ||
#define _MADARA_KNOWLEDGE_FILESTREAMER_H_ | ||
|
||
#include <string> | ||
#include <fstream> | ||
#include <vector> | ||
#include "madara/knowledge/KnowledgeUpdateSettings.h" | ||
#include "madara/knowledge/KnowledgeRecord.h" | ||
#include "madara/utility/Utility.h" | ||
#include "madara/utility/ScopedArray.h" | ||
#include "madara/knowledge/containers/FlexMap.h" | ||
#include "madara/logger/GlobalLogger.h" | ||
|
||
namespace madara | ||
{ | ||
namespace knowledge | ||
{ | ||
/** | ||
* @class FileStreamer | ||
* @brief Splits files into fragments that can be saved to and loaded from | ||
* a knowledge base | ||
*/ | ||
class FileStreamer | ||
{ | ||
public: | ||
/** | ||
* Constructor | ||
**/ | ||
FileStreamer () | ||
{ | ||
} | ||
|
||
/** | ||
* Constructor | ||
* @param key the location in the knowledge base to save to | ||
* @param filename the file to open and read from | ||
* @param kb the knowledge base to stream to | ||
**/ | ||
FileStreamer (const std::string & key, const std::string & filename, | ||
KnowledgeBase kb) | ||
{ | ||
init (key, filename, kb); | ||
} | ||
|
||
/** | ||
* Initializes the file stream and all containers | ||
* @param key the location in the knowledge base to save to | ||
* @param filename the file to open and read from | ||
* @param kb the knowledge base to stream to | ||
**/ | ||
inline void init ( | ||
const std::string & key, const std::string & filename, | ||
KnowledgeBase kb) | ||
{ | ||
// setup containers | ||
file_space.set_name (key, kb); | ||
file_space["size"].to_container (file_size); | ||
file_space["crc"].to_container (file_crc); | ||
file_space["contents"].to_container (file_fragments); | ||
file_fragments.clear (); | ||
|
||
file_crc.set_name (file_space["crc"].get_name (), kb); | ||
file_size.set_name (file_space["size"].get_name (), kb); | ||
|
||
// setup file information | ||
filename_ = filename; | ||
file_crc = (KnowledgeRecord::Integer) utility::file_crc (filename); | ||
file_size = (KnowledgeRecord::Integer) utility::file_size (filename); | ||
|
||
stream_.open (filename, std::ios::in | std::ios::binary); | ||
} | ||
|
||
/** | ||
* Loads a fragment into the knowledge base | ||
**/ | ||
inline size_t load (size_t index, size_t frag_size = 60000) | ||
{ | ||
size_t bytes_read = 0; | ||
|
||
if (stream_) | ||
{ | ||
// seek to the index position in the file | ||
stream_.seekg ((std::streampos)index * frag_size, std::ios::beg); | ||
|
||
// read the fragment into a local buffer | ||
std::vector<unsigned char> frag (frag_size); | ||
// char * frag = new char [frag_size]; | ||
stream_.read ((char *)frag.data (), frag_size); | ||
bytes_read = stream_.gcount (); | ||
frag.resize (bytes_read); | ||
|
||
KnowledgeRecord record; | ||
record.emplace_file (std::move (frag)); | ||
|
||
// set the file fragment into the KB | ||
file_fragments.set ( | ||
std::to_string ((unsigned long long)index), record); | ||
// file_fragments.emplace_file () | ||
} | ||
|
||
return bytes_read; | ||
} | ||
|
||
/** | ||
* Returns the crc of the file | ||
* @return the crc of the file | ||
**/ | ||
inline uint32_t get_crc (void) | ||
{ | ||
return (uint32_t) file_crc.to_integer (); | ||
} | ||
|
||
/** | ||
* Returns the size of the file | ||
* @return the size of the file | ||
**/ | ||
inline size_t get_size (void) | ||
{ | ||
return (size_t) file_size.to_integer (); | ||
} | ||
|
||
/** | ||
* Returns the name of the file | ||
* @return the name of the file being streamed | ||
**/ | ||
inline std::string get_filename (void) | ||
{ | ||
return filename_; | ||
} | ||
|
||
/** | ||
* Clears the fragment list | ||
**/ | ||
inline void clear_fragments (void) | ||
{ | ||
// option 1. seg fault | ||
file_fragments.clear (); | ||
|
||
// option 2. seg fault | ||
// knowledge::KnowledgeRules keys; | ||
// file_fragments.keys (keys); | ||
|
||
// for (auto key : keys) | ||
// { | ||
// file_fragments.erase (key); | ||
// } | ||
|
||
// option 3. seg fault | ||
//file_fragments.reset (); | ||
|
||
// option 4. nothing doesn't segfault | ||
} | ||
|
||
/** | ||
* Remodifies the file size and crc | ||
**/ | ||
inline void modify (void) | ||
{ | ||
file_size.modify (); | ||
file_crc.modify (); | ||
} | ||
|
||
/// records that contain the file fragments | ||
containers::FlexMap file_space; | ||
|
||
/// the size of the file contents | ||
containers::Integer file_size; | ||
|
||
/// the crc of the file | ||
containers::Integer file_crc; | ||
|
||
/// the crc of the file | ||
containers::Map file_fragments; | ||
|
||
private: | ||
|
||
/// the name of the file | ||
std::string filename_; | ||
|
||
/// the stream to read from | ||
std::ifstream stream_; | ||
}; | ||
} | ||
} | ||
|
||
#endif // _MADARA_KNOWLEDGE_FILESTREAMER_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.