You need to sign in or sign up before continuing.
Commit 36cd6757 authored by Mayank Manoj's avatar Mayank Manoj

Merge branch 'mayank' into 'master'

Mayank

See merge request !3
parents e94cec16 2cf6ddb2
......@@ -3,36 +3,46 @@
#include "LFU.h"
using namespace std;
class memoryManagement {
class memoryManagement
{
public:
virtual string get(int *a, string s) {
virtual string get(int *a, string s)
{
return "This will never run";
}
virtual void put(string a, string b) {
virtual void put(string a, string b)
{
return;
}
virtual void del(int *a, string s) {
virtual void del(int *a, string s)
{
return;
}
virtual void traverse() {
virtual void traverse()
{
return;
}
virtual void pushAll() {
virtual void pushAll()
{
return;
}
virtual string getKeyValuePairs(int id) {
virtual string getKeyValuePairs(int id)
{
return "This will never run";
}
};
class storageLRU : public memoryManagement {
class storageLRU : public memoryManagement
{
public:
LRUcache mycache;
storageLRU(int capacity) {
storageLRU(int capacity)
{
mycache.setCap(capacity);
}
string get(int *status, string key) {
string get(int *status, string key)
{
string result = "";
int status2 = 200;
......@@ -42,37 +52,45 @@ public:
return result;
}
void put(string key, string val) {
void put(string key, string val)
{
mycache.put(key, val);
}
void del(int *status, string key) {
void del(int *status, string key)
{
int status2 = 200;
mycache.del(key, &status2);
*status = status2;
}
void traverse() {
void traverse()
{
mycache.traverse();
}
void pushAll() {
void pushAll()
{
mycache.pushAll();
}
string getKeyValuePairs(int id) {
return "keyvaluepairs";
string getKeyValuePairs(int id)
{
return mycache.getKeyValuePairs(id);
}
};
class storageLFU : public memoryManagement {
class storageLFU : public memoryManagement
{
public:
LFUCache mycache;
storageLFU(int capacity) {
storageLFU(int capacity)
{
mycache.setCap(capacity);
}
string get(int *status, string key) {
string get(int *status, string key)
{
string result = "";
int status2 = 200;
......@@ -82,26 +100,32 @@ public:
return result;
}
void put(string key, string val) {
void put(string key, string val)
{
mycache.PUT(key, val);
}
void del(int *status, string key) {
void del(int *status, string key)
{
int status2 = 200;
mycache.DEL(key, &status2);
*status = status2;
}
void traverse() {
void traverse()
{
mycache.traverse();
}
void pushAll() {
void pushAll()
{
mycache.pushAll();
}
string getKeyValuePairs(int id) {
return "keyvaluepairs";
// key1;key2;key3;;value1;value2;value3;
string getKeyValuePairs(int id)
{
return mycache.getKeyValuePairs(id);
}
};
\ No newline at end of file
This diff is collapsed.
#include <bits/stdc++.h>
#include "LRUNode.h"
#include <dirent.h>
#include <sys/stat.h>
#define NOT_EXIST "KEY NOT EXIST"
using namespace std;
inline bool fileExists(const std::string &name) {
inline bool fileExists(const std::string &name)
{
struct stat buffer;
return (stat(name.c_str(), &buffer) == 0);
}
string getFilename(string key) {
string getFilename(string key)
{
string filename = ".";
int length = key.size();
if (length == 1) {
if (length == 1)
{
return "_.kvm";
}
filename += char(length - 1);
......@@ -24,7 +28,8 @@ string getFilename(string key) {
return filename;
}
class LRUcache {
class LRUcache
{
private:
int capacity;
string deleted;
......@@ -34,7 +39,8 @@ private:
unordered_map<string, Node *> cache;
public:
LRUcache() {
LRUcache()
{
head = new Node("HEAD", "HEAD");
tail = new Node("TAIL", "TAIL");
head->next = tail;
......@@ -43,11 +49,13 @@ public:
deleted += char(0);
}
void setCap(int capacity) {
void setCap(int capacity)
{
this->capacity = capacity;
}
LRUcache(int capacity) {
LRUcache(int capacity)
{
this->capacity = capacity;
head = new Node("HEAD", "HEAD");
tail = new Node("TAIL", "TAIL");
......@@ -57,7 +65,8 @@ public:
deleted += char(0);
}
Node *getUtil(string key) {
Node *getUtil(string key)
{
if (cache.find(key) == cache.end())
return NULL;
Node *x = cache[key];
......@@ -73,11 +82,14 @@ public:
return x;
}
string get(string key, int *status) {
string get(string key, int *status)
{
//std::lock_guard<std::mutex> guard(mtx);
Node *x = getUtil(key);
if (x) {
if (x->payload == deleted) {
if (x)
{
if (x->payload == deleted)
{
*status = 400;
return NOT_EXIST;
}
......@@ -86,7 +98,8 @@ public:
string fileName = getFilename(key);
if (!fileExists(fileName)) {
if (!fileExists(fileName))
{
*status = 400;
return NOT_EXIST;
}
......@@ -97,7 +110,8 @@ public:
string _key, val;
unordered_map<string, string> flush;
do {
do
{
getline(fin, _key);
if (_key.size() == 0)
break;
......@@ -113,12 +127,14 @@ public:
fin.close();
unordered_map<string, string>::iterator itr;
for (itr = flush.begin(); itr != flush.end() && cache.size() < capacity - 1; itr++) {
for (itr = flush.begin(); itr != flush.end() && cache.size() < capacity - 1; itr++)
{
this->put(itr->first, itr->second);
flush[itr->first] = deleted;
}
if (flush.find(key) != flush.end()) {
if (flush.find(key) != flush.end())
{
this->put(key, flush[key]);
flush[key] = deleted;
}
......@@ -128,7 +144,8 @@ public:
ofstream fout;
fout.open(fileName);
for (itr = flush.begin(); itr != flush.end(); itr++) {
for (itr = flush.begin(); itr != flush.end(); itr++)
{
if (itr->second == deleted)
continue;
fout << itr->first << "\n";
......@@ -136,7 +153,8 @@ public:
}
fout.close();
x = getUtil(key);
if (x) {
if (x)
{
return x->payload;
}
*status = 400;
......@@ -144,12 +162,14 @@ public:
return NOT_EXIST;
}
void del(string key, int *status) {
void del(string key, int *status)
{
int status2 = 200;
string result = get(key, &status2);
// mtx.lock();
if (status2 == 400) {
if (status2 == 400)
{
*status = 400;
// mtx.unlock();
return;
......@@ -159,9 +179,11 @@ public:
// mtx.unlock();
}
void pushAll() {
void pushAll()
{
unordered_map<string, Node *>::iterator itr;
for (itr = cache.begin(); itr != cache.end(); itr++) {
for (itr = cache.begin(); itr != cache.end(); itr++)
{
string fileName = getFilename(itr->first);
ofstream fout;
......@@ -177,12 +199,15 @@ public:
tail->prev = head;
}
void put(string key, string payload) {
void put(string key, string payload)
{
//mtx.lock();
if (getUtil(key) != NULL)
cache[key]->payload = payload;
else {
if (cache.size() == capacity) {
else
{
if (cache.size() == capacity)
{
Node *x = tail->prev;
string keyToBeFlushed = x->key;
cache.erase(keyToBeFlushed);
......@@ -206,7 +231,9 @@ public:
head->next = x;
x->prev = head;
} else {
}
else
{
Node *x = new Node(key, payload);
cache[key] = x;
x->next = head->next;
......@@ -219,12 +246,90 @@ public:
//mtx.unlock();
}
void traverse() {
void traverse()
{
Node *temp = head->next;
while (temp->next) {
while (temp->next)
{
cout << temp->key << "\n";
temp = temp->next;
}
cout << "\n";
}
int hash(string s)
{
return (((int)s.at(0)) << 8) + ((int)s.at(1));
}
// key1;key2;key3;;value1;value2;value3;
string getKeyValuePairs(int id)
{
unordered_map<string, string> flush;
string keyValPairs = "";
DIR *dirFile = opendir(".");
if (dirFile)
{
struct dirent *hFile;
while ((hFile = readdir(dirFile)) != NULL)
{
string fName(hFile->d_name);
string fileName(hFile->d_name);
int n;
if ((n = fileName.size()) < 8)
continue;
if (fileName[n - 3] == 'k' && fileName[n - 2] == 'v' && fileName[n - 1] == 'm')
{
fileName.erase(n - 4);
fileName = fileName.substr(2);
if (hash(fileName) <= id)
{
ifstream fin;
fin.open(fName);
string _key, val;
do
{
getline(fin, _key);
if (_key.size() == 0)
break;
getline(fin, val);
if (val != deleted)
flush[_key] = val;
else
flush.erase(_key);
if (fin.eof())
break;
} while (fin);
fin.close();
const char *c = fName.c_str();
remove(c);
}
}
}
closedir(dirFile);
}
Node *temp = head->next;
while (temp->next)
{
if (hash(temp->key) <= id)
{
flush[temp->key] = temp->payload;
}
temp = temp->next;
}
unordered_map<string, string>::iterator itr;
for (itr = flush.begin(); itr != flush.end(); itr++)
keyValPairs += itr->first + ";";
keyValPairs += ";";
for (itr = flush.begin(); itr != flush.end(); itr++)
keyValPairs += itr->second + ";";
return keyValPairs;
}
};
......@@ -160,6 +160,7 @@ void RunClient() {
Info info;
ClientContext context;
Status status=stub->GETADDRESS(&context,null,&info);
std::cout<<"Server: "<<info.address()<<std::endl;
std::string target_address(info.address());
// Instantiates the client
KeyValueServicesClient client(
......
LISTENING_PORT=50051
LISTENING_PORT=50055
CACHE_REPLACEMENT_TYPE=LRU
CACHE_SIZE=256
NUM_SERVER_THREADS=4
......@@ -186,7 +186,13 @@ private:
RequestType reqType;
};
void signalHandler(int signum) {
remove(SERVERS);
exit(0);
}
int main(int argc,char **argv) {
signal(SIGINT, signalHandler);
srand(time(0));
string server_address("0.0.0.0:1234");
builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment