Add concurrency handling implementation with ticket management and file locking
This commit is contained in:
67
cpp/src/Public/sweepstore/sweepstore.h
Normal file
67
cpp/src/Public/sweepstore/sweepstore.h
Normal file
@@ -0,0 +1,67 @@
|
||||
#pragma once
|
||||
|
||||
#include <iosfwd>
|
||||
|
||||
#include "concurrency.h"
|
||||
#include "header.h"
|
||||
#include "utils/helpers.h"
|
||||
#include "utils/file_handle.h"
|
||||
|
||||
class Sweepstore {
|
||||
|
||||
private:
|
||||
std::string filePath;
|
||||
SweepstoreFileHandle file;
|
||||
SweepstoreHeader* header;
|
||||
SweepstoreConcurrencyHeader* concurrencyHeader;
|
||||
|
||||
public:
|
||||
|
||||
Sweepstore(const std::string& filePath) : filePath(filePath), file(filePath, std::ios::binary | std::ios::in | std::ios::out | std::ios::trunc) {
|
||||
header = new SweepstoreHeader(file);
|
||||
concurrencyHeader = new SweepstoreConcurrencyHeader(file);
|
||||
}
|
||||
|
||||
~Sweepstore() {
|
||||
delete header;
|
||||
delete concurrencyHeader;
|
||||
file.close();
|
||||
}
|
||||
|
||||
void initialise(int concurrentWorkers = 4);
|
||||
|
||||
SweepstoreConcurrencyHeader* getConcurrencyHeader() {
|
||||
return concurrencyHeader;
|
||||
}
|
||||
|
||||
class Proxy {
|
||||
Sweepstore* sweepstore;
|
||||
std::string key;
|
||||
public:
|
||||
Proxy(Sweepstore* sweepstoreIn, const std::string& keyIn)
|
||||
: sweepstore(sweepstoreIn), key(keyIn) {}
|
||||
|
||||
template <typename T>
|
||||
operator T() {
|
||||
// Get value from sweepstore
|
||||
throw std::runtime_error("Not implemented: Reading values from Sweepstore by key is not implemented.");
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void operator=(const T& value) {
|
||||
SweepstoreConcurrency::spawnTicket(sweepstore->filePath,
|
||||
SweepstoreTicketOperation::WRITE,
|
||||
bt_hash(key),
|
||||
sizeof(T),
|
||||
[this, key = this->key, &value]() {
|
||||
|
||||
}
|
||||
);
|
||||
};
|
||||
};
|
||||
|
||||
Proxy operator[](const std::string& key) {
|
||||
return Proxy(this, key);
|
||||
}
|
||||
|
||||
};
|
||||
Reference in New Issue
Block a user