Logo
ApraPipes 1.0
Loading...
Searching...
No Matches
OrderedCacheOfFiles.h
1#pragma once
2
3#include <boost/filesystem.hpp>
4#include <boost/multi_index_container.hpp>
5#include <boost/multi_index/ordered_index.hpp>
6#include <boost/multi_index/identity.hpp>
7#include <boost/multi_index/member.hpp>
8
9#include <string>
10#define batch_size 1440
11
12namespace fs = boost::filesystem;
13namespace bmi = boost::multi_index;
14
15
17{
18public:
19 OrderedCacheOfFiles(std::string& video_folder, uint32_t initial_batch_size = 1440, uint32_t _lowerWaterMark = 1441, uint32_t _upperWaterMark = 2880);
21 {
23 {
24 mThread->join();
25 }
26 }
27 void updateBatchSize(uint32_t _batchSize)
28 {
29 batchSize = _batchSize;
30 }
32 {
34 }
35 void deleteLostEntry(std::string& filePath);
36 uint64_t getFileDuration(std::string& filename);
37 // Note - getFileAt() is an unreliable method. Use ONLY if you know what you are doing.
38 std::string getFileAt(uint64_t timestamp, bool direction);
39 bool isTimeStampInFile(std::string& filename, uint64_t timestamp);
40 std::string getNextFileAfter(std::string& currentFile, bool direction);
41 std::vector<boost::filesystem::path> parseAndSortDateDir(const std::string& rootDir);
42 std::vector<boost::filesystem::path> parseAndSortHourDir(const std::string& rootDir);
43 std::vector<boost::filesystem::path> parseAndSortMp4Files(const std::string& rootDir);
44 bool parseFiles(uint64_t start_ts, bool direction, bool includeFloorFile = false, bool disableBatchSizeCheck = false, uint64_t skipTS = 0);
45 bool getRandomSeekFile(uint64_t ts, bool direction, uint64_t& skipMsecs, std::string& fileName);
46 bool getFileFromCache(uint64_t timestamp, bool direction, std::string& fileName);
47 size_t getCacheSize()
48 {
49 return videoCache.size();
50 }
51 bool fetchAndUpdateFromDisk(std::string videoFile, uint64_t& start_ts, uint64_t& end_ts);
52 bool fetchFromCache(std::string& videoFile, uint64_t& start_ts, uint64_t& end_ts);
53 void readVideoStartEnd(std::string& filePath, uint64_t& start_ts, uint64_t& end_ts);
54 void clearCache();
55 bool refreshCache();
56 std::string getLastVideoInCache() { return videoCache.rbegin()->path; }
57 void updateCache(std::string& filePath, uint64_t& start_ts, uint64_t& end_ts); // allow updates from playback
58 std::map<std::string, std::pair<uint64_t, uint64_t>> getSnapShot(); // too costly, use for debugging only
59 bool probe(boost::filesystem::path dirPath, std::string& videoName);
60 bool getPreviousAndNextFile(std::string videoPath, std::string& previousFile, std::string& nextFile);
61private:
62 bool lastKnownPlaybackDir = true; // sync with mp4 playback
63 boost::mutex m_mutex;
64 boost::shared_ptr<boost::thread> mThread = nullptr;
65 int cacheSize = 1440;
66 int batchSize = 1440;
67 std::string rootDir = "";
71 /* Util methods */
72 bool filePatternCheck(const fs::path& path);
73 bool datePatternCheck(const boost::filesystem::path& path);
74 bool hourPatternCheck(const boost::filesystem::path& path);
75 //bool openFileUpdateCacheByIter(iter);
76 //bool openFileUpdateCacheByFilename(filePath);
77
78 /* ---------Cache Stuff ------------*/
80 {
81 std::string END_ITER = "END_ITERATOR";
83
84 // cache entry format
85 struct Video
86 {
87 uint64_t start_ts, end_ts;
88 std::string path;
89
90 Video(std::string& _path, uint64_t _start_ts) : path(_path), start_ts(_start_ts)
91 {
92 end_ts = 0;
93 }
94
95 Video(const std::string _path, uint64_t _start_ts) : path(_path), start_ts(_start_ts)
96 {
97 end_ts = 0;
98 }
99
100 Video(std::string _path, uint64_t _start_ts, uint64_t _end_ts) : path(_path), start_ts(_start_ts), end_ts(_end_ts) {}
101
102 void updateEndTS(uint64_t ts)
103 {
104 end_ts = ts;
105 }
106 };
107
108 // cache index tags
109 struct videoPath {};
110 struct videoTS {};
111
112 // VideoCache data type
113 typedef boost::multi_index_container<
114 Video,
115 bmi::indexed_by<
116 // sort by less<string> on path
117 bmi::ordered_unique<bmi::tag<videoPath>, bmi::member<Video, std::string, &Video::path> >,
118
119 // sort by less<int> on videoTS
120 bmi::ordered_non_unique<bmi::tag<videoTS>, bmi::member<Video, uint64_t, &Video::start_ts> >
121 >
123
124 // Cache object and index
126 VideoCache::index<videoTS>::type& videoCacheStartTSIndex = videoCache.get<videoTS>();
127
128 // Cache methods
129 void insertInVideoCache(Video vid);
130
131 // Cache cleanup strategy
132 void retireOldFiles(uint64_t startTSofRelevantFile);
133 void dropFarthestFromTS(uint64_t startTS);
134
135};
Definition OrderedCacheOfFiles.h:17
uint32_t upperWaterMark
Definition OrderedCacheOfFiles.h:69
bool hourPatternCheck(const boost::filesystem::path &path)
Definition OrderedCacheOfFiles.cpp:900
std::vector< boost::filesystem::path > parseAndSortHourDir(const std::string &rootDir)
Definition OrderedCacheOfFiles.cpp:618
bool refreshCache()
Definition OrderedCacheOfFiles.cpp:871
void cleanCacheOnSeperateThread(bool flag)
Definition OrderedCacheOfFiles.h:31
std::string getNextFileAfter(std::string &currentFile, bool direction)
Definition OrderedCacheOfFiles.cpp:258
size_t getCacheSize()
Definition OrderedCacheOfFiles.h:47
void readVideoStartEnd(std::string &filePath, uint64_t &start_ts, uint64_t &end_ts)
Definition OrderedCacheOfFiles.cpp:337
OrderedCacheOfFiles(std::string &video_folder, uint32_t initial_batch_size=1440, uint32_t _lowerWaterMark=1441, uint32_t _upperWaterMark=2880)
Definition OrderedCacheOfFiles.cpp:8
bool getFileFromCache(uint64_t timestamp, bool direction, std::string &fileName)
Definition OrderedCacheOfFiles.cpp:525
VideoCache videoCache
Definition OrderedCacheOfFiles.h:125
bool filePatternCheck(const fs::path &path)
Definition OrderedCacheOfFiles.cpp:879
uint64_t getFileDuration(std::string &filename)
Definition OrderedCacheOfFiles.cpp:17
bool lastKnownPlaybackDir
Definition OrderedCacheOfFiles.h:62
int cacheSize
Definition OrderedCacheOfFiles.h:65
bool fetchAndUpdateFromDisk(std::string videoFile, uint64_t &start_ts, uint64_t &end_ts)
Definition OrderedCacheOfFiles.cpp:64
void deleteLostEntry(std::string &filePath)
Definition OrderedCacheOfFiles.cpp:848
bool cleanCacheOnMainThread
Definition OrderedCacheOfFiles.h:70
void clearCache()
Definition OrderedCacheOfFiles.cpp:862
bool datePatternCheck(const boost::filesystem::path &path)
Definition OrderedCacheOfFiles.cpp:889
boost::multi_index_container< Video, bmi::indexed_by< bmi::ordered_unique< bmi::tag< videoPath >, bmi::member< Video, std::string, &Video::path > >, bmi::ordered_non_unique< bmi::tag< videoTS >, bmi::member< Video, uint64_t, &Video::start_ts > > > > VideoCache
Definition OrderedCacheOfFiles.h:122
void updateBatchSize(uint32_t _batchSize)
Definition OrderedCacheOfFiles.h:27
bool parseFiles(uint64_t start_ts, bool direction, bool includeFloorFile=false, bool disableBatchSizeCheck=false, uint64_t skipTS=0)
Definition OrderedCacheOfFiles.cpp:661
void dropFarthestFromTS(uint64_t startTS)
Definition OrderedCacheOfFiles.cpp:792
VideoCache::index< videoTS >::type & videoCacheStartTSIndex
Definition OrderedCacheOfFiles.h:126
int batchSize
Definition OrderedCacheOfFiles.h:66
uint32_t lowerWaterMark
Definition OrderedCacheOfFiles.h:68
bool getPreviousAndNextFile(std::string videoPath, std::string &previousFile, std::string &nextFile)
Definition OrderedCacheOfFiles.cpp:155
std::map< std::string, std::pair< uint64_t, uint64_t > > getSnapShot()
Definition OrderedCacheOfFiles.cpp:105
bool probe(boost::filesystem::path dirPath, std::string &videoName)
Definition OrderedCacheOfFiles.cpp:119
~OrderedCacheOfFiles()
Definition OrderedCacheOfFiles.h:20
void retireOldFiles(uint64_t startTSofRelevantFile)
Definition OrderedCacheOfFiles.cpp:776
std::string getFileAt(uint64_t timestamp, bool direction)
Definition OrderedCacheOfFiles.cpp:195
boost::mutex m_mutex
Definition OrderedCacheOfFiles.h:63
bool fetchFromCache(std::string &videoFile, uint64_t &start_ts, uint64_t &end_ts)
Definition OrderedCacheOfFiles.cpp:32
void insertInVideoCache(Video vid)
Definition OrderedCacheOfFiles.cpp:572
std::vector< boost::filesystem::path > parseAndSortDateDir(const std::string &rootDir)
Definition OrderedCacheOfFiles.cpp:589
bool isTimeStampInFile(std::string &filename, uint64_t timestamp)
Definition OrderedCacheOfFiles.cpp:309
void updateCache(std::string &filePath, uint64_t &start_ts, uint64_t &end_ts)
Definition OrderedCacheOfFiles.cpp:391
boost::shared_ptr< boost::thread > mThread
Definition OrderedCacheOfFiles.h:64
std::vector< boost::filesystem::path > parseAndSortMp4Files(const std::string &rootDir)
Definition OrderedCacheOfFiles.cpp:642
bool getRandomSeekFile(uint64_t ts, bool direction, uint64_t &skipMsecs, std::string &fileName)
Definition OrderedCacheOfFiles.cpp:409
std::string rootDir
Definition OrderedCacheOfFiles.h:67
struct OrderedCacheOfFiles::CacheIteratorState cacheIteratorState
std::string getLastVideoInCache()
Definition OrderedCacheOfFiles.h:56
Definition OrderedCacheOfFiles.h:80
std::string END_ITER
Definition OrderedCacheOfFiles.h:81
Definition OrderedCacheOfFiles.h:86
Video(std::string _path, uint64_t _start_ts, uint64_t _end_ts)
Definition OrderedCacheOfFiles.h:100
std::string path
Definition OrderedCacheOfFiles.h:88
Video(std::string &_path, uint64_t _start_ts)
Definition OrderedCacheOfFiles.h:90
uint64_t end_ts
Definition OrderedCacheOfFiles.h:87
Video(const std::string _path, uint64_t _start_ts)
Definition OrderedCacheOfFiles.h:95
void updateEndTS(uint64_t ts)
Definition OrderedCacheOfFiles.h:102
uint64_t start_ts
Definition OrderedCacheOfFiles.h:87
Definition OrderedCacheOfFiles.h:109
Definition OrderedCacheOfFiles.h:110