Commit 348a1527 authored by Sharron4me's avatar Sharron4me

Initial Commit

parent 8066bddd
Get 1
Put 1 1000
Get 1
Put 1 20
Get 1
Put X8KRO X64QA
Put 65LOI ZLVRM
Put ZJWEP 9OTFS
Put QJVEZ N37IZ
Put SC0PE GDYBA
Put L63CP OB7OE
Put E2NEH API3J
Put CKS2R B3RQ3
Put Z2RB7 RDU1V
Put GCJP4 UEQAP
Put D7OGA YEABF
Put FWJKN EZKG6
Put 0A6HR 9850V
Put IU8LR A5TKB
Put 772PP HFWJL
Put B0L8Y B4T4V
Put PQCLS U9BS0
Put GOSEM 0Q5HJ
Put YLGO7 AT3KV
Put ZXZRI QX7N2
Put QB0G5 MSPSY
Put 7ZAGN DB12Z
Put UZFG5 Y4XZW
Put 7X53V ZXT19
Put Q4OUU LVZ7Q
Put LX9PB 0NK6I
Put RMH40 8QUZ2
Put INU85 ITQT6
Put B2UEI 6KBCF
Put MU59H W1OYL
Put H6CDN PHGAF
Put K8JIS CN3EY
Put 4XWB2 J5Q82
Put LXKNN SXPRH
Put 36KH2 O52RA
Put SKE59 0M70E
Put UV6TK EISSY
Put 7MT7O B16I3
Put 3Y8NL 25FBM
Put ZW5Q3 U5KY6
Put J9N6X 50FMF
Put CIBYA EIG46
Put 919XQ DP5WB
Put ZD8WW X8ZNB
Put T3P4H SUZ18
Put EBY2M GXZDD
Put YHEA1 AXUCA
Put 3L51S FQ2RA
Put GFEYR J81GZ
Put FF1GI B8YBQ
Put Q6038 T2J1L
Put TR22L 4PNBH
Put H87WW ZVRTN
Put E89LS 8YUX9
Put ODTFK 69R0Q
Put 10YHW UCCE9
Put 4GES5 KLY7Q
Put J9RMU ALE5W
Put F3KHJ F5E55
Put 5V5W4 XXIJ9
Put ZSNLX IJBZE
Put B3XPO 1RGZA
Put TNHMW B68X4
Put UV27P P2CCJ
Put 62Q7U HJ9U1
Put G9EMC 3QZ97
Put 3MTD7 JQ35G
Put 83XGG DVRUB
Put HORE0 F7TVN
Put TD7D2 PJ6V4
Put BJOSD AFS8A
Put 7L0PV ILAXZ
Put 1BHB4 D49S5
Put 93ESL JN9HS
Put 1JR7R JW58P
Put LHH8G 8LT3F
Put IEOH7 QP2MH
Put RWYMC MW9M5
Put QUZK0 HKTJR
Put JZXTX M584B
Put 8Z1U9 O4XSU
Put NDUK6 I4VXG
Put AELPD PW8YH
Put 9SIZT H9RS5
Put JC01M NNVVN
Put N5GUF SZOBQ
Put LO0QC MSQTB
Put ZVXK4 K11YE
Put RSGNB OAMZX
Put SR7OC KGY76
Put ZKGLU 508QD
Put VKU1F 9E85E
Put 3E4X7 21EJN
Put UTCZF C83W5
Put CFZP6 05ZWA
Put LW01E QRHKF
Put E62AZ 2SWRB
Put L9Z9R 1W237
Put KVTWG L98UC
Put ZZ9UH 8D8S2
Put PHCYU UIBBJ
Put B84CO C3IOR
Put 2TJWF KZA8H
Put D5V4H 423PD
Put N0717 15N8F
Put GMUOE FTPWF
Put Z2MPU TBFTU
Put I5D3V WWGX4
Put 6KX3M 1TCRP
Put RR5AD CEYCI
Put 6D5NK G3XLO
Put MJ7M1 WUXVS
Put P6CKI ZKGGA
Put TIWU7 SBX6G
Put DTJR5 XQT1H
Put XBM3T UDNXJ
Put WW8RU MF4FE
Put VZSAO MAETS
Put 9M788 XQO5Z
Put FRXLT ZSUM5
Put V1VGK L1D0R
Put RWSD8 7VLFY
Put BBUQT TZ4LS
Put 627JL NL9OS
Put VJ186 1XKX1
Put X7KSP 9HC8K
Put HCFP1 7K2J8
Put OXNUH Q2PMP
Put 6CUCD ABUHK
Put TN3P4 ZPK7U
Put S9TK3 7X2EZ
Put 8QCU9 TXPCV
Put 8WGCT 92TKC
Put 64OEM 7QMCV
Put NL9IH L946H
Put CUBLU 8KEX5
Put P1U12 AYA0M
Put 6QRUD R300X
Put C9CUE PF2VD
Put O2TNL AMSAW
Put LZMSY 93FSH
Put U60OY S101M
Put 170V0 HWKU1
Put LS62Q 3L3ZS
Put 3AG6C HOHK2
Put 4XI8O H3SNP
Put 8J8ZI OHCNZ
Put 1UVW1 6W2FD
Put WYZ3Q S2T7R
Put VELOP AZ5W5
Put 1BTWC K1P0A
Put K5I9W NBQO8
Put Z5E21 84AVD
Put O1QBH 122QW
Put O5D8D 2INXL
Put GH6FF PZJ2I
Put PMCVF 7UHHD
Put FP7D6 5J0U9
Put X8WA4 CKRBZ
Put 3GTXX TAKIC
Put JCZCG D9X0D
Put BK7TX EMB1X
Put NECSQ MPTWH
Put T4PO2 J91I0
Put SJ6S9 5TJAF
Put RDCF0 TAA89
Put 8GCC4 NKFJL
Put RBDT0 78XZ5
Put L10N8 NZOWY
Put HSXTO RCNO2
Put TZFBB J08QO
Put X248S PTISV
Put G44X4 1APW9
Put H7R9G DQ2XL
Put JYIFR QJMVH
Put 2HAB7 38888
Put X7ET3 ZHLE3
Put 46MXZ O3WU0
Put C68OO ZLBAK
Put 8N24C R7K1R
Put WGZSH DRYHU
Put YUKDM 3F2L3
Put FMPCT GZ7YP
Put K6TRY P4H2V
Put KH5S5 ZV6J9
Put ACY3Y BJPWV
Put 2WMYO TURHN
Put XZFJJ NQ6ID
Put CFZQH XLCME
Put JH47N WEDTG
Put 2OAV4 4ET5U
Put 83VTQ QKYXK
Put S1IE4 XZC69
Put 77EWA 2SNNG
Put SL68U ZO7VU
Put SRXFI 7POMC
Put PY27U SM21Y
Put 1C49Y 082YH
Put 1V7I6 VHIZ9
Put 5243R XI1PW
Put ZZRKO L2724
Put U3G4B YVL09
......@@ -5,105 +5,170 @@ using namespace std;
class cache
{
public:
struct entry{
string key;
string value;
bool valid;
bool modified;
class entry{
public:
string key="";
string value="";
bool valid=1;
bool modified=0;
int last_used=-1;
int frequency=0;
pthread_mutex_t lock;
};
int max_capacity;
const int KEY_SIZE= 256;
const int VAL_SIZE =256;
map<string,entry*>cache;
entry (cache)[10024];
long long int current_capacity;
long long unsigned int counter;
pthread_mutex_t mutex;
// bool delete_key(string);
// void find_new_slot();
string get_entry(string key){
if(cache.find(key)!=cache.end()){
return cache[key]->value;
pthread_mutex_lock(&mutex);
//cout<<"GETTING "<<key<<endl;
for(int i=0;i<max_capacity;i++){
if(cache[i].last_used!=-1 && cache[i].key==key){
cache[i].last_used=++counter;
cache[i].frequency++;
show_all_elements();
pthread_mutex_unlock(&mutex);
return cache[i].value;
}
}
char val[256];
int return_ =file_get(toCharArray(key),val);
cout<<return_<<endl;
if(return_){
pthread_mutex_unlock(&mutex);
put_into_cache(key,toString(val),1);
show_all_elements();
return toString(val);
}
show_all_elements();
pthread_mutex_unlock(&mutex);
return "KEY NOT EXIST";
}
void initialize_cache(int size_){
storage_init();
//cout<<"Size oF Cache is "<<size_<<endl;
current_capacity=size_;
max_capacity=size_;
// cache=(entry**)malloc(sizeof(entry *)*size_);
//cout<<"Initialized\n";
pthread_mutex_init(&mutex, NULL);
counter=0;
}
bool put_into_cache(string key1,string value2,int code){
if(key1.length()>256 || value2.length()>256 || key1.length()==0 ||value2.length()==0 ){
void show_all_elements(){
cout<<"===================================================\n";
for(int i=0;i<max_capacity;i++){
cout<<cache[i].key<<" --- "<<cache[i].value<<" --- "<<cache[i].last_used<<endl;
}
cout<<"===================================================\n";
}
bool put_into_cache(string key1,string value2,int code){
cout<<"Put Called\n";
pthread_mutex_lock(&mutex);
cout<<"Putting11 "<<key1<<" - "<<value2<<endl;
int loc=-1;
for(int i=0;i<max_capacity;i++){
if(cache[i].key==key1){
//cout<<"Putting ";
loc=i;
break;
}
}
if(loc!=-1){
cache[loc].value=value2;
cache[loc].last_used=counter++;
show_all_elements();
pthread_mutex_unlock(&mutex);
return 1;
}
////cout<<"Putting "<<key1<<" - "<<value2<<endl;
//cout<<key1.compare(value2)<<endl;
if(key1.length()>256 || value2.length()>256 || key1.length()==0 ||value2.length()==0 || !key1.compare(value2)){
show_all_elements();
pthread_mutex_unlock(&mutex);
return 0;
}
struct entry * temp=(struct entry*)malloc(sizeof(struct entry));
int slot;
//cout<<"Debug7"<<endl;
slot=find_new_slot(code);
int k;
pthread_mutex_lock(&(temp->lock));
temp->key=key1;
temp->value =value2;
temp->valid=1;
temp->modified=0;
if(code=1)
temp->last_used=counter++;
else
temp->frequency++;
if(current_capacity<=0){
find_new_slot(code);
current_capacity++;
}
cache[key1]=temp;
current_capacity--;
counter++;
//cout<<"Debug1"<<endl;
cache[slot].key=key1;
//cout<<"Debug2"<<endl;
cache[slot].value =value2;
//cout<<"Debug3"<<endl;
cache[slot].valid=1;
//cout<<"Debug4"<<endl;
cache[slot].modified=0;
//cout<<"Debug5"<<endl;
cache[slot].last_used=counter++;
cache[slot].frequency++;
//cout<<"Debug6"<<endl;
//cout<<"Added Entry:"<<key1<<" - "<<cache[slot].value<<endl;
show_all_elements();
file_put(toCharArray(key1),toCharArray(value2));
pthread_mutex_unlock(&(temp->lock));
pthread_mutex_unlock(&mutex);
return 1;
}
void find_new_slot(int code){
cout<<"Problem here"<<endl;
string lowest;
int find_new_slot(int code){
int lowest;
int last_mod=INT_MAX;
for(map<string,entry*>::const_iterator it=cache.begin();it!=cache.end();++it){
for(int i=0;i<max_capacity;i++){
if(code==1){
if(it->second->last_used<last_mod){
lowest=it->first;
last_mod=it->second->last_used;
if(cache[i].last_used==-1)
return i;
if(cache[i].last_used<last_mod){
lowest=i;
last_mod=cache[i].last_used;
}
}
else{
if(it->second->frequency<last_mod){
lowest=it->first;
last_mod=it->second->frequency;
if(cache[i].last_used==-1)
return i;
if(cache[i].frequency<last_mod){
lowest=i;
last_mod=cache[i].frequency;
}
}
}
entry* temp = cache[lowest];
cout<<"DELETEING ENTRY KEY : "<<lowest<<endl;
file_del(toCharArray(lowest));
cache.erase(lowest);
// free(temp);
}
void print_cache(){
for(map<string,entry*>::const_iterator it=cache.begin();it!=cache.end();++it)
cout<<it->first<<" "<<it->second->value<<endl;
return lowest;
}
bool delete_key(string key){
// cout<<"DELETEING ENTRY KEY : "<<key<<endl;
auto it = cache.find(key);
if(it==cache.end())
//cout<<"DELETEING ENTRY KEY : "<<key<<endl;
pthread_mutex_lock(&mutex);
int loc=-1;
for(int i=0;i<max_capacity;i++){
if(cache[i].key==key){
loc=i;
break;
}
}
char val[256];
if(loc==-1){
int return_ =file_get(toCharArray(key),val);
if(return_){
file_del(toCharArray(key));
pthread_mutex_unlock(&mutex);
return true;
}
pthread_mutex_unlock(&mutex);
return false;
pthread_mutex_lock(&(it->second->lock));
file_del(toCharArray(it->first));
cache.erase(it->first);
pthread_mutex_unlock(&(it->second->lock));
// free(it->second);
}
file_del(toCharArray(cache[loc].key));
cache[loc].key="";
cache[loc].value="";
cache[loc].last_used=-1;
show_all_elements();
pthread_mutex_unlock(&mutex);
return true;
}
};
......@@ -15,7 +15,9 @@
#include <thread>
#include "kv.grpc.pb.h"
#include <chrono>
using namespace std::chrono;
using grpc::Channel;
using grpc::ChannelArguments;
......@@ -107,7 +109,7 @@ public:
GPR_ASSERT(ok);
if(status.ok())
{
std::cout << "Message received: " <<reply.message() <<"Status:"<<reply.status()<< std::endl;
std::cout << "Message received: " <<reply.message_() <<"Status:"<<reply.status()<< std::endl;
// time(&end);
}
......@@ -138,7 +140,7 @@ public:
GPR_ASSERT(ok);
if(status.ok())
{
std::cout << "Message received: " <<reply.message()<<"Status:"<<reply.status() << std::endl;
std::cout << "Message received: " <<reply.message_()<<"Status:"<<reply.status() << std::endl;
// time(&end);
}
......@@ -231,8 +233,11 @@ int main(int argc, char* argv[])
cin>>mode;
if(mode==1)
{
double totalB=0;
fstream newfile;
newfile.open("batch.txt",ios::in); //open a file to perform read operation using file object
int numB=0;
if (newfile.is_open())
{ //checking whether the file is open
string tp;
......@@ -243,44 +248,70 @@ int main(int argc, char* argv[])
std::vector<std::string> v = split (tp, delimiter);
string func=v.front();
if(!func.compare("Get"))
{
numB++;
v.erase(v.begin());
string key=v.front();
time(&start);
string key=v.front();
// unsync the I/O of C and C++.
ios_base::sync_with_stdio(false);
client.GetValue(key);
ios_base::sync_with_stdio(false);
auto start = high_resolution_clock::now();
client.GetValue(key);
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
totalB+=duration.count();
cout<<"Get Time:"<<duration.count()<< " microseconds"<<endl;
}
else if(!func.compare("Put"))
{
numB++;
v.erase(v.begin());
string key=v.front();
v.erase(v.begin());
string value=v.front();
time(&start);
// unsync the I/O of C and C++.
ios_base::sync_with_stdio(false);
ios_base::sync_with_stdio(false);
auto start = high_resolution_clock::now();
client.PutKValue(key,value);
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
totalB+=duration.count();
cout<<"Put Time:"<<duration.count()<< " microseconds"<<endl;
}
else if(!func.compare("Del"))
{
numB++;
v.erase(v.begin());
string key=v.front();
time(&start);
// unsync the I/O of C and C++.
ios_base::sync_with_stdio(false);
client.DelKValue(key);
auto start = high_resolution_clock::now();
client.DelKValue(key);
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
totalB+=duration.count();
cout<<"Delete Time:"<<duration.count()<< " microseconds"<<endl;
}
}
newfile.close(); //close the file object.
cout<<"Requests Batch:"<<numB<<endl;
cout<<"Total Time Batch:"<<totalB<<endl;
cout<<"Average Time Batch:"<<double(totalB/numB)<<endl;
}
}
else if(mode==2)
{
int numI=0;
double totalI=0;
std::cout<<"1.Get Key 2.Put Key Value 3.Del Key"<<std::endl;
std::cout << "Press control-c to quit" << std::endl << std::endl;
while(1)
......@@ -292,37 +323,65 @@ std::string delimiter = " ";
string func=v.front();
if(!func.compare("Get"))
{
numI++;
v.erase(v.begin());
string key=v.front();
time(&start);
string key=v.front();
// unsync the I/O of C and C++.
ios_base::sync_with_stdio(false);
auto start = high_resolution_clock::now();
client.GetValue(key);
}
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
totalI+=duration.count();
cout<<"Get Time:"<<duration.count()<< " microseconds"<<endl;
}
else if(!func.compare("Put"))
{
numI++;
v.erase(v.begin());
string key=v.front();
v.erase(v.begin());
string value=v.front();
time(&start);
// unsync the I/O of C and C++.
ios_base::sync_with_stdio(false);
auto start = high_resolution_clock::now();
client.PutKValue(key,value);
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
totalI+=duration.count();
cout<<"Put Time:"<<duration.count()<< " microseconds"<<endl;
}
else if(!func.compare("Del"))
{
numI++;
v.erase(v.begin());
string key=v.front();
time(&start);
// unsync the I/O of C and C++.
ios_base::sync_with_stdio(false);
client.DelKValue(key);
} }
auto start = high_resolution_clock::now();
client.DelKValue(key);
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
totalI+=duration.count();
cout<<"Delete Time:"<<duration.count()<< " microseconds"<<endl;
}
else if(!func.compare("Exit"))
{
cout<<"Requests Interactive:"<<numI<<endl;
cout<<"Total Time Interactive:"<<totalI<<endl;
cout<<"Average Time Interactive:"<<double(totalI/numI)<<endl;
break;
}
}
}
std::cout << "Press control-c to quit" << std::endl << std::endl;
thread_.join();
}
......
......@@ -31,35 +31,35 @@ service KeyValueStore {
// The request message containing the key.
message GetRequest {
string key = 1;
bytes key = 1;
}
// The response message containing the value of corresponding key
message GetReply {
string value = 1;
bytes value = 1;
int32 status=2;
}
// The request message containing the key and value.
message PutRequest {
string key = 1;
string value = 2;
bytes key = 1;
bytes value = 2;
}
// The response message containing message after adding key-value pair
message PutReply {
string message = 1;
bytes message_ = 1;
int32 status=2;
}
// The request message containing the key and value.
message DelRequest {
string key = 1;
bytes key = 1;
}
// The response message containing message after removing key-value pair
message DelReply {
string message = 1;
bytes message_ = 1;
int32 status=2;
}
......
......@@ -42,8 +42,8 @@ using namespace std;
pthread_mutex_t lock1;
int tech=0;
int num_threads;
string port="2021";
ofstream MyFile("server.logs");
struct configuration
{
int cacheSize, threadPSize,listeningPort;
......@@ -53,46 +53,57 @@ struct configuration
cache c;
std::string getFromMap(std::string key)
{
ofstream MyFile("server.logs");
// Key is not present
MyFile <<"Get "<<key;
if(!(c.get_entry(key).empty()))
{
string ans= c.get_entry(key);
MyFile<<" Succesfull"<<endl;
MyFile.close();
return ans;
}
else{
char val[256];
char *temp=toCharArray(key);
if(file_get(temp,val)){
free(temp);
//free(temp);
MyFile<<" Succesfull"<<endl;
MyFile.close();
return toString(val);
}
MyFile<<" Failed"<<endl;
MyFile.close();
return "KEY NOT EXIST";
}
}
std::string putIntoMap(std::string key ,std::string value){
ofstream MyFile("server.logs");
MyFile <<"Put "<<key;
if(c.put_into_cache(key,value,tech)){
MyFile<<" Succesfull"<<endl;
MyFile.close();
return "Key: "+key+" Added Successfully";
}
MyFile<<" Failed"<<endl;
MyFile.close();
return "Error Occured While Adding Key : "+key;
}
std::string deleteFromMap(std::string key)
{
ofstream MyFile("server.logs");
MyFile <<"Delete"<<key;
if(c.delete_key(key)){
MyFile<<" Succesfull"<<endl;
MyFile.close();
return "Deleted Key : "+key;
}
MyFile<<" Failed"<<endl;
MyFile.close();
return "KEY NOT EXIST";
}
......@@ -150,8 +161,9 @@ class CallData: public CommonCallData
{
new CallData(service_, cq_);
// std::cout << "key = " << request_.key() << std::endl;
reply_.set_value(getFromMap(request_.key()));
if(!getFromMap(request_.key()).compare("KEY NOT EXIST"))
string reply_from_server=getFromMap(request_.key());
reply_.set_value(reply_from_server);
if(!(reply_from_server).compare("KEY NOT EXIST"))
{
reply_.set_status(400);
}
......@@ -193,8 +205,9 @@ public:
{
new CallData1M(service_, cq_);
// std::cout << "Key:" << request_.key()<<"Value: "<<request_.value() << std::endl;
reply_.set_message(putIntoMap(request_.key(),request_.value()));
if(!putIntoMap(request_.key(),request_.value()).compare("Error Occured While Adding Key"))
std::string reply_from_server=putIntoMap(request_.key(),request_.value());
reply_.set_message_(reply_from_server);