Commit d35cdcfb authored by ritapravo's avatar ritapravo

added

parent 7c7ebadd
File added
Team Members
Ashwin Patidar 213050063
Debajyoti Saha 21Q050001
Ritapravo Sarkar 213059003
Prerequisite:--
Installation of grpc in the system.
1. To build Server and Client:
Put "assignment4" directory (available in submission) in grpc/examples/cpp/ folder and run this commands
$ cd assignment4
$ mkdir -p cmake/build
$ pushd cmake/build
$ cmake -DCMAKE_PREFIX_PATH=$MY_INSTALL_DIR ../..
$ make
now to run server and client you have to be in assignmnet4/cmake/build directory and run this commands their
1. for server in one terminal
$./server
2. for client in other terminal
$./client
References :
1. for LFU cache :- https://www.youtube.com/watch?v=rSwmpWaJPG0&ab_channel=TechLifeWithShivank
\ No newline at end of file
# Default ignored files
/shelf/
/workspace.xml
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# Editor-based HTTP Client requests
/httpRequests/
KeyValueStore
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module classpath="CMake" type="CPP_MODULE" version="4" />
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/assignment4.iml" filepath="$PROJECT_DIR$/.idea/assignment4.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/../../.." vcs="Git" />
</component>
</project>
\ No newline at end of file
# Copyright 2020 the gRPC authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
licenses(["notice"]) # 3-clause BSD
cc_binary(
name = "keyvaluestore_client",
srcs = [
"caching_interceptor.h",
"client.cc",
],
defines = ["BAZEL_BUILD"],
deps = [
"//:grpc++",
"//examples/protos:keyvaluestore",
],
)
cc_binary(
name = "keyvaluestore_server",
srcs = ["server.cc"],
defines = ["BAZEL_BUILD"],
deps = [
"//:grpc++",
"//examples/protos:keyvaluestore",
],
)
# Copyright 2021 the gRPC authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# cmake build file for C++ keyvaluestore example.
# Assumes protobuf and gRPC have been installed using cmake.
# See cmake_externalproject/CMakeLists.txt for all-in-one cmake build
# that automatically builds all the dependencies before building keyvaluestore.
cmake_minimum_required(VERSION 3.5.1)
project(KeyValueStore C CXX)
include(../cmake/common.cmake)
# Proto file
get_filename_component(kvs_proto "keyvaluestore.proto" ABSOLUTE)
get_filename_component(kvs_proto_path "${kvs_proto}" PATH)
# Generated sources
set(kvs_proto_srcs "${CMAKE_CURRENT_BINARY_DIR}/keyvaluestore.pb.cc")
set(kvs_proto_hdrs "${CMAKE_CURRENT_BINARY_DIR}/keyvaluestore.pb.h")
set(kvs_grpc_srcs "${CMAKE_CURRENT_BINARY_DIR}/keyvaluestore.grpc.pb.cc")
set(kvs_grpc_hdrs "${CMAKE_CURRENT_BINARY_DIR}/keyvaluestore.grpc.pb.h")
add_custom_command(
OUTPUT "${kvs_proto_srcs}" "${kvs_proto_hdrs}" "${kvs_grpc_srcs}" "${kvs_grpc_hdrs}"
COMMAND ${_PROTOBUF_PROTOC}
ARGS --grpc_out "${CMAKE_CURRENT_BINARY_DIR}"
--cpp_out "${CMAKE_CURRENT_BINARY_DIR}"
-I "${kvs_proto_path}"
--plugin=protoc-gen-grpc="${_GRPC_CPP_PLUGIN_EXECUTABLE}"
"${kvs_proto}"
DEPENDS "${kvs_proto}")
# Include generated *.pb.h files
include_directories("${CMAKE_CURRENT_BINARY_DIR}")
# kvs_grpc_proto
add_library(kvs_grpc_proto
${kvs_grpc_srcs}
${kvs_grpc_hdrs}
${kvs_proto_srcs}
${kvs_proto_hdrs})
target_link_libraries(kvs_grpc_proto
${_REFLECTION}
${_GRPC_GRPCPP}
${_PROTOBUF_LIBPROTOBUF})
# client
add_executable(client "keyvalueclient.cc")
target_link_libraries(client
kvs_grpc_proto
${_REFLECTION}
${_GRPC_GRPCPP}
${_PROTOBUF_LIBPROTOBUF})
# server
add_executable(server "keyvalueserver.cc")
target_link_libraries(server
kvs_grpc_proto
${_REFLECTION}
${_GRPC_GRPCPP}
${_PROTOBUF_LIBPROTOBUF})
#include<bits/stdc++.h>
#include "KVFiles.cpp"
#include "pthread.h"
using namespace std;
void remove(string t, queue<string>& q)
{
queue<string> ref;
int s = q.size();
int cnt = 0;
while (q.front() != t and !q.empty()) {
ref.push(q.front());
q.pop();
cnt++;
}
if (q.empty()) {
cout << "Element not found in Cache!!" << endl;
while (!ref.empty()) {
q.push(ref.front());
ref.pop();
}
}
else {
q.pop();
while (!ref.empty()) {
q.push(ref.front());
ref.pop();
}
int k = s - cnt - 1;
while (k--) {
string p = q.front();
q.pop();
q.push(p);
}
}
}
pthread_mutex_t l;
class LRUCache {
private:
unordered_map<string,string> mp;
queue<string> q;
int len;
public:
LRUCache(int capacity) {
len=capacity;
pthread_mutex_init(&l,NULL);
}
string get(string key) {
pthread_mutex_lock(&l);
auto it=mp.find(key);
if(it==mp.end())
{
string v = getFromFile(key);
if(!v.compare("0")) {
pthread_mutex_unlock(&l);
return "key not found";
}
else{
add(key,v);
pthread_mutex_unlock(&l);
return v;
}
}
remove(key, q);
q.push(key);
pthread_mutex_unlock(&l);
return it->second;
}
void add(string key,string value){
auto it=mp.find(key);
if(it!=mp.end()) {
it->second=value;
remove(key, q);
q.push(key);
return;
}
else
{
if(mp.size()<len) {
mp[key]=value;
q.push(key);
return;
}
else
{
string cur=q.front();
q.pop();
mp.erase(cur);
mp[key]=value;
q.push(key);
return;
}
}
}
void put(string key, string value) {
pthread_mutex_lock(&l);
auto it=mp.find(key);
if(it!=mp.end()) {
it->second=value;
putInFile(key,value);
remove(key, q);
q.push(key);
pthread_mutex_unlock(&l);
return;
}
else
{
if(mp.size()<len) {
mp[key]=value;
q.push(key);
putInFile(key,value);
pthread_mutex_unlock(&l);
return;
}
else
{
string cur=q.front();
q.pop();
mp.erase(cur);
mp[key]=value;
q.push(key);
putInFile(key,value);
pthread_mutex_unlock(&l);
return;
}
}
}
int del(string key) {
pthread_mutex_lock(&l);
mp.erase(key);
remove(key, q);
int i = delfromFile(key);
pthread_mutex_unlock(&l);
return i;
}
};
class LFUCache {
public:
// m tracks the minimum frequency at an instance of time
// s is the current s of the cache
// c is the maximum capacity of the cache
int m, s=0, c=0;
// The Cache(hashmap) where key and <values,frequency> are stored
unordered_map<string, pair<string, int>> Cache;
// Hashmap mapped from frequency to keylist, ie, each frequency is mapped to a list of keys
unordered_map<int, list<string>> temp1;
// Hashmap that maps the key to its position in the list freq_keylist[freq]
unordered_map<string, list<string>::iterator> temp2;
//constructor for LFU cache
LFUCache(int capacity) {
c = capacity;
pthread_mutex_init(&l,NULL);
}
string get(string key) {
pthread_mutex_lock(&l);
if(Cache.find(key)==Cache.end()) {
string v = getFromFile(key);
if(!v.compare("0")) {
pthread_mutex_unlock(&l);
return "key not found";
}
else{
add(key,v);
pthread_mutex_unlock(&l);
return v;
}
}
int freq = Cache[key].second;
temp1[freq].erase(temp2[key]);
Cache[key].second++;
temp1[Cache[key].second].push_back(key);
temp2[key] = --temp1[Cache[key].second].end();
int z = temp1[m].size();
if(z==0) m++;
pthread_mutex_unlock(&l);
return Cache[key].first;
}
void add(string key,string value){
if(c<=0) return;
// when the key in found in the cache
if(Cache.find(key)!=Cache.end()){
string abcde = get(key);
Cache[key].first = value;
return;
}
// when key is not found in the cache
if(s==c){
//remove the element from cache with min freq
string remove_key = temp1[m].front();
temp2.erase(remove_key);
Cache.erase(remove_key);
temp1[m].pop_front();
}
Cache[key] = {value, 1};
temp1[1].push_back(key);
temp2[key] = --temp1[1].end();
m=1;
if(s < c) s++;
}
void put(string key, string value) {
pthread_mutex_lock(&l);
if(c<=0){
pthread_mutex_unlock(&l);
return;
}
// when the key in found in the cache
if(Cache.find(key)!=Cache.end()){
//string abcde = get(key);
Cache[key].first = value;
putInFile(key,value);
int freq = Cache[key].second;
temp1[freq].erase(temp2[key]);
Cache[key].second++;
temp1[Cache[key].second].push_back(key);
temp2[key] = --temp1[Cache[key].second].end();
int z = temp1[m].size();
if(z==0) m++;
pthread_mutex_unlock(&l);
return;
}
// when key is not found in the cache
putInFile(key,value);
if(s==c){
//remove the element from cache with min freq
string remove_key = temp1[m].front();
temp2.erase(remove_key);
Cache.erase(remove_key);
temp1[m].pop_front();
}
Cache[key] = {value, 1};
temp1[1].push_back(key);
temp2[key] = --temp1[1].end();
m=1;
if(s < c) s++;
pthread_mutex_unlock(&l);
}
int del(string key) {
pthread_mutex_lock(&l);
int i = delfromFile(key);
if(Cache.find(key)==Cache.end()){
cout << "key" << " : " << key << "->" <<"Not present in cache" << " ";
}
else {
int f = Cache[key].second;
temp2.erase(key);
Cache.erase(key);
temp1[f].remove(key);
s--;
}
pthread_mutex_unlock(&l);
return i;
}
};
\ No newline at end of file
[ZoneTransfer]
ZoneId=3
HostUrl=https://web.whatsapp.com/
#include <iostream>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <fstream>
#include <bits/stdc++.h>
using namespace std;
string getFromFile(string key)
{
fstream file;
file.open("../../storage/a.txt",ios::in);
if(file.is_open())
{
string line,k,v;
v = "0";
while (getline(file, line))
{
stringstream stream(line);
getline(stream, k,' ');
if(k == key)
{
getline(stream,v);
break;
}
}
file.close();
return v;
}
return "0";
}
void putInFile(string key, string value)
{
string line,k,v,add;;
ifstream in("../../storage/a.txt");
if( !in.is_open())
{
in.close();
ofstream out("../../storage/a.txt");
add = key+' '+value;
out << add << "\n";
out.close();
return;
}
ofstream out("../../storage/o.txt");
while( getline(in,line) )
{
stringstream stream(line);
getline(stream, k,' ');
if(k != key)
{
getline(stream, v);
add = k+' '+v;
out << add << "\n";
}
}
add = key+' '+value;
out << add << "\n";
in.close();
out.close();
remove("../../storage/a.txt");
rename("../../storage/o.txt","../../storage/a.txt");
return;
}
int delfromFile(string key)
{
string line,k,v,add;
int flag=0;
ifstream in("../../storage/a.txt");
if( !in.is_open())
{
return -1;
}
ofstream out("../../storage/o.txt");
while( getline(in,line) )
{
stringstream stream(line);
getline(stream, k,' ');
if(k != key)
{
getline(stream, v);
add = k+' '+v;
out << add << "\n";
}
else
{
flag=1;
}
}
in.close();
out.close();
remove("../../storage/a.txt");
rename("../../storage/o.txt","../../storage/a.txt");
if (flag==0)
{
return -2;
}
return 0;
}
\ No newline at end of file
PUT key value
PUT key1 value1
PUT key2 value2
GET key
PUT key3 value3
DEL key1
DEL key3
GET key3
\ No newline at end of file
LISTENING_PORT=8081
CACHE_REPLACEMENT_TYPE=LRU
CACHE_SIZE=3
THREAD_POOL_SIZE=5
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
// Copyright 2018 gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
syntax = "proto3";
package kvstore;
// A simple key-value storage service
service KeyValueStore {
// Provides a value for each key request
rpc GetValues (Request) returns (Response) {}
rpc StorePair (Request) returns (Response) {}
rpc DeletePair (Request) returns (Response) {}
}
// The request message containing the key
message Request {
string key = 1;
string value = 2;
int32 type = 3;
}
// The response message containing the value associated with the key
message Response {
string value = 1;
}
sagar sagar
shiva shiva
ghana hello
key2 value2
key value
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