Commit a9e2787b authored by Nilesh Jagdish's avatar Nilesh Jagdish

Added client and delete function in cache

parent 5ffd666b
...@@ -57,17 +57,8 @@ void lru(string key, string value) { ...@@ -57,17 +57,8 @@ void lru(string key, string value) {
// Write back to persistent storage // Write back to persistent storage
} }
else { else {
for (auto p : hashTable) {
cout << p.first << " : ";
cout << p.second->key << " " << p.second->value << " " << p.second->dirtyBit << endl ;
}
int num = hashTable.erase(it->key); int num = hashTable.erase(it->key);
cache.pop_back(); cache.pop_back();
for (auto p : hashTable) {
cout << p.first << " : ";
cout << p.second->key << " " << p.second->value << " " << p.second->dirtyBit << endl;
}
printf("-----------------------------\n");
cacheNode cnode; cacheNode cnode;
cnode.key = key; cnode.key = key;
cnode.value = value; cnode.value = value;
...@@ -95,6 +86,16 @@ void insert(string key, string value) { ...@@ -95,6 +86,16 @@ void insert(string key, string value) {
} }
} }
/*
* Delete from cache
*/
void del(string key) {
unordered_map<string, list <cacheNode> :: iterator> :: iterator it = hashTable.find(key);
list <cacheNode> :: iterator cnode_it = it->second;
cache.erase(cnode_it);
hashTable.erase(key);
}
/* /*
* Testing code * Testing code
*/ */
...@@ -129,7 +130,6 @@ int main() { ...@@ -129,7 +130,6 @@ int main() {
cout << key3 << " " << search(key3) << endl; cout << key3 << " " << search(key3) << endl;
cout << key4 << " " << search(key4) << endl; cout << key4 << " " << search(key4) << endl;
printf("-----------------------------\n"); printf("-----------------------------\n");
for (auto p : hashTable) { for (auto p : hashTable) {
cout << p.first << " : "; cout << p.first << " : ";
cout << p.second->key << " " << p.second->value << " " << p.second->dirtyBit << endl; cout << p.second->key << " " << p.second->value << " " << p.second->dirtyBit << endl;
...@@ -140,4 +140,10 @@ int main() { ...@@ -140,4 +140,10 @@ int main() {
cout << p.first << " : "; cout << p.first << " : ";
cout << p.second->key << " " << p.second->value << " " << p.second->dirtyBit << endl; cout << p.second->key << " " << p.second->value << " " << p.second->dirtyBit << endl;
} }
printf("-----------------------------\n");
del(key5);
for (auto p : hashTable) {
cout << p.first << " : ";
cout << p.second->key << " " << p.second->value << " " << p.second->dirtyBit << endl;
}
} }
#include <stdio.h>
#include "client.h"
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#include <unistd.h>
void padding(char* str, char* key, int padTo) {
int n = strlen(str);
for(int i = n + 1; i < padTo; i++) {
key[i] = ' ';
}
}
int connectToServer(char *addr, char *portNo) {
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
struct addrinfo hints, *result;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
// getaddrinfo return getaddr objects by searching using the provided hints, name, service
int s = getaddrinfo(addr, portNo, &hints, &result);
if(s != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
return -1;
}
connect(sockfd, result->ai_addr, result->ai_addrlen);
cout << GET("ABC", sockfd) << endl;
return 0;
}
int GET(char* key, int sockfd) {
int padTo = 513;
char *buffer = (char *)malloc(padTo + 1);
strncpy(&buffer[1], key, strlen(key));
padding(key, buffer, padTo);
buffer[padTo] = '\0';
buffer[0] = '1';
write(sockfd, buffer, strlen(buffer));
char recvBuffer[padTo];
int len = read(sockfd, recvBuffer, padTo);
if(recvBuffer[0] == 200) {
printf("Value : %s\n", recvBuffer[1]);
return 1;
}
else
return 0;
}
int PUT(char* key, char* value, int sockfd) {
int padTo = 257;
char *buffer = (char *)malloc(2 * padTo - 1);
strncpy(&buffer[1], key, strlen(key));
padding(key, buffer, padTo);
strncpy(&buffer[padTo], value, strlen(value));
padding(value, &buffer[padTo], padTo);
buffer[0] = '2';
write(sockfd, buffer, strlen(buffer));
char recvBuffer[padTo];
int len = read(sockfd, recvBuffer, padTo);
if(recvBuffer[0] == 200) {
printf("Success\n");
return 1;
}
else {
printf("Failure\n");
return 0;
}
}
int DEL(char* key, int sockfd) {
int padTo = 513;
char *buffer = (char *)malloc(padTo + 1);
strncpy(&buffer[1], key, strlen(key));
padding(key, buffer, padTo);
buffer[padTo] = '\0';
buffer[0] = '3';
write(sockfd, buffer, strlen(buffer));
char recvBuffer[padTo];
int len = read(sockfd, recvBuffer, padTo);
if(recvBuffer[0] == 200) {
printf("Value : %s\n", recvBuffer[1]);
return 1;
}
else
return 0;
}
int main(int argc, char **argv) {
printf("IP = %s, Port = %s\n", argv[1], argv[2]);
connectToServer(argv[1], argv[2]);
return 0;
}
// 1. connection to server // 1. connection to server
// 2. KV communication functions // 2. KV communication functions
#include <iostream>
#include <list>
#include <iterator>
#include <string>
#include <unordered_map>
using namespace std;
int connectToServer(char *addr, char *portNo); int connectToServer(char *addr, char *portNo);
void sendToServer(int sockfd);
int GET(char* key, char *request); int GET(char* key, int sockfd);
int PUT(char* key, char* value, char *request); int PUT(char* key, char* value, int sockfd);
int DELETE(char* key, char *request); int DEL(char* key, int sockfd);
void terminateConnection(); void terminateConnection();
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