FileManager v1.7.0
Manage access to files with async model
Loading...
Searching...
No Matches
WriteFileHandle.cpp
1#include "WriteFileHandle.h"
2
3#include "FileManager.h"
4
5using namespace std;
6
7namespace file_manager
8{
9 bool WriteFileHandle::CachingBuffer::increaseCacheData()
10 {
11 if (!isCachingAvailable)
12 {
13 return false;
14 }
15
16 string_view availableCacheData(pbase(), pptr());
17
18 if (availableCacheData.size() + cache.getCurrentCacheSize() > cache.getCacheSize())
19 {
20 isCachingAvailable = false;
21
22 _utility::changeCurrentCacheSize<minus>(cacheData.size());
23
24 cacheData.clear();
25
26 return false;
27 }
28
29 _utility::changeCurrentCacheSize<plus>(availableCacheData.size());
30
31 cacheData += availableCacheData;
32
33 return true;
34 }
35
36 WriteFileHandle::CachingBuffer::CachingBuffer(Cache& cache, const filesystem::path& filePath, ios_base::openmode mode) :
37 cache(cache),
38 filePath(filePath),
39 isCachingAvailable(cache.getCacheSize())
40 {
41 open(filePath, mode);
42 }
43
44 int WriteFileHandle::CachingBuffer::sync()
45 {
46 this->increaseCacheData();
47
48 return filebuf::sync();
49 }
50
51 WriteFileHandle::CachingBuffer::~CachingBuffer()
52 {
53 if (!this->increaseCacheData())
54 {
55 return;
56 }
57
58 _utility::addCache(move(filePath), move(cacheData));
59 }
60
61 WriteFileHandle::WriteFileHandle(const filesystem::path& filePath, ios_base::openmode mode) :
62 FileHandle(filePath, mode | ios_base::out)
63 {
64
65 }
66
67 void WriteFileHandle::write(const string& data)
68 {
69 file.write(data.data(), data.size()).flush();
70 }
71
72 ostream& WriteFileHandle::getStream()
73 {
74 return file.write(nullptr, 0);
75 }
76
77 WriteFileHandle::~WriteFileHandle()
78 {
79 if (isNotifyOnDestruction)
80 {
81 FileManager::getInstance().completeWriteRequest(filePath);
82 }
83 }
84}
uint64_t getCacheSize() const
Get global cache size.
Definition Cache.cpp:182
uint64_t getCurrentCacheSize() const
Used cache size.
Definition Cache.cpp:187