Commit fbf175f3 authored by Saikumar's avatar Saikumar

Delete persistentStorage.cpp

parent 07fdf497
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <string.h>
// #include <cstdlib>
#include<fcntl.h>
#include "storage.h"
#include <iostream>
#include<semaphore.h>
using namespace std;
pthread_mutex_t m;
sem_t w;
int readercout=0;
// struct n1
// {
// char key[257];
// int lineno;
// char filename[5];
// struct n1 *p1;
// struct n1 *p2;
// int h;
// };
struct n2
{
char filename[5];
int nooffreeline;
struct n2 *pointer;
};
struct n1 *h1=NULL;
struct n2 *h2=NULL;
unsigned int nooffiles=0;
void initializeStorageVariables()
{
pthread_mutex_init(&m, NULL);
sem_init(&w, 0,1);
}
struct n2 * check(struct n2 *h2){
while (1)
{
if(h2==NULL){
return NULL;
}
else{
if((h2->nooffreeline)>0){
return h2;}
else{
h2=h2->pointer;
}
}
}
}
int max(int a,int b){
if(a>b){
return a;
}
return b;
}
int hei(struct n1*r){
if(r==NULL){
return 0;
}
return (1+max(hei(r->p1),hei(r->p2)));
}
struct n1* ll(struct n1*r){
struct n1*a=r->p1;
r->p1=r->p1->p2;
a->p2=r;
r->h=max(hei(r->p1),hei(r->p2));
a->h=max(hei(a->p1),hei(a->p2));
return a;
}
struct n1* rr(struct n1*r ){
struct n1*a=r->p2;
r->p2=a->p1;
a->p1=r;
a->h=max(hei(a->p1),hei(a->p2));
r->h=max(hei(r->p1),hei(r->p2));
return a;
}
struct n1 * add;
char lineno=100;
struct n1 * insert(struct n1 *r,char * key){
int tt=0;
if(r==NULL){
r=(struct n1 *)malloc(sizeof(struct n1));
strcpy(r->key,key);
r->p1=NULL;
r->p2=NULL;
r->h=0;
add=r;
struct n2 *tt2=check(h2);
if(tt2==NULL){
tt2=(struct n2 *)malloc(sizeof(struct n2));
char filename2[10];
sprintf(filename2,"%u",(nooffiles=nooffiles+1));
strcpy(tt2->filename,filename2);
// printf(" for the first time storing the file name in n2 is %s\n",tt2->filename);
strcpy(r->filename,filename2);
// printf(" for the first time storing the file name in n1 is %s\n",r->filename);
tt2->nooffreeline=19;
tt2->pointer=h2;
h2=tt2;
FILE *fptr=fopen(strcat(filename2,".txt"),"w");
fprintf(fptr,"aaaaaaaaaaaaaaaaaaaa############################################################################################################################################################################################################################################\n");
fclose(fptr);
// printf(" for the first time storing the file name in n2 is %s\n",tt2->filename);
// printf(" for the first time storing the file name in n1 is %s\n",r->filename);
}
else{
tt2->nooffreeline-=1;
char dum[38];
// sprintf(dum,"the no of free lines left in file %02d",tt2->nooffreeline);
// write(STDOUT_FILENO,dum,36);
// write(STDOUT_FILENO,"\n",1);
strcpy(r->filename,tt2->filename);
}
return r;
}
else{
if((tt=strcmp(key,r->key))>0){//the key i want to add is alphabeticaly greter than the one that is added strcmp(myelement,pre element)
r->p2=insert(r->p2,key);
r->h=max(hei(r->p1),hei(r->p2));
int r2;
if(r->p1==NULL){
r2=1+r->p2->h;
}else{
r2=r->p2->h-r->p1->h;
}
if(r2==1||r2==-1||r2==0){
return r;
}
else{
if(r2<0){
int r3;
if(r->p1->p1==NULL){
r3=1+r->p1->p2->h;
}
else if(r->p1->p2==NULL){
r3=-1-r->p1->p1->h;
}else{
r3=r->p1->p2->h-r->p1->p1->h;
}
if(r3<0){
//left -left
return ll(r);
}
else{
//left-right
r->p1=rr(r->p1);
return ll(r);
}
}
else{
int r3;
if(r->p2->p1==NULL){
r3=1+r->p2->p2->h;
}
else if(r->p2->p2==NULL){
r3=-1-r->p2->p1->h;
}else{
r3=r->p2->p2->h-r->p2->p1->h;
}
if(r3<0){
//right-left
r->p2=ll(r->p2);
return rr(r);
}
else{
//right-right
return rr(r);
}
}
}
}
else if(tt<0){
r->p1=insert(r->p1,key);
r->h=max(hei(r->p1),hei(r->p2));
int r2;
if(r->p2==NULL){
r2=-1-(r->p1->h);
}
else{
r2=r->p2->h-(r->p1->h);
}
if(r2==1||r2==-1||r2==0){
return r;
}
else{
if(r2<0){
int r3;
if(r->p1->p1==NULL){
r3=1+r->p1->p2->h;
}
else if(r->p1->p2==NULL){
r3=-1-r->p1->p1->h;
}else{
r3=r->p1->p2->h-r->p1->p1->h;
}
if(r3<0){
//left -left
return ll(r);
}
else{
//left-right
r->p1=rr(r->p1);
return ll(r);
}
}
else{
int r3;
if(r->p2->p1==NULL){
r3=1+r->p2->p2->h;
}
else if(r->p2->p2==NULL){
r3=-1-r->p2->p1->h;
}else{
r3=r->p2->p2->h-r->p2->p1->h;
}
if(r3<0){
//right-left
r->p2=ll(r->p2);
return rr(r);
}
else{
//right-right
return rr(r);
}
}
}
}
else{
add=r;
lineno=r->lineno;
return r;
}
}
}
struct n1* finding=NULL;
struct n1* pre;
struct n1* fpre(struct n1 *r){
if(r->p2==NULL){
pre=r;
return r->p1;
}
r->p2=fpre(r->p2);
r->h=max(hei(r->p1),hei(r->p2));
int r2;
if(r->p1==NULL){
if(r->p2==NULL){
return r;
}
else{
r2=1+r->p2->h;
}
}
else{
if(r->p2==NULL){
r2=-1-r->p1->h;
}
else{
r2=r->p2->h-r->p1->h;
}
}
if(r2==1||r2==-1||r2==0){
return r;
}
else{
if(r2<0){
int r3;
if(r->p1->p1==NULL){
r3=1+r->p1->p2->h;
}
else if(r->p1->p2==NULL){
r3=-1-r->p1->p1->h;
}else{
r3=r->p1->p2->h-r->p1->p1->h;
}
if(r3<0){
//left -left
return ll(r);
}
else{
//left-right
r->p1=rr(r->p1);
return ll(r);
}
}
else{
int r3;
if(r->p2->p1==NULL){
r3=1+r->p2->p2->h;
}
else if(r->p2->p2==NULL){
r3=-1-r->p2->p1->h;
}else{
r3=r->p2->p2->h-r->p2->p1->h;
}
if(r3<0){
//right-left
r->p2=ll(r->p2);
return rr(r);
}
else{
//right-right
return rr(r);
}
}
}
return r;
}
struct n1* del(struct n1*r,char *key){
int tt;
if(r==NULL){
return NULL;
}
if((tt=strcmp(key,r->key))==0){//both are equal
if(r->p1==NULL&&r->p2==NULL){
finding=r;
return NULL;
}
if(r->p1==NULL){
finding=r;
return r->p2;
}
if(r->p2==NULL){
finding=r;
return r->p1;
}
finding=r;
fpre(r->p1);
pre->p1=r->p1;
pre->p2=r->p2;
return pre;
}
if(tt>0){//the key i want to add is alphabeticaly greter than the one that is added strcmp(myelement,pre element)
r->p2=del(r->p2,key);
r->h=max(hei(r->p1),hei(r->p2));
int r2;
if(r->p1==NULL){
if(r->p2==NULL){
return r;
}
else{
r2=1+r->p2->h;
}
}
else{
if(r->p2==NULL){
r2=-1-r->p1->h;
}
else{
r2=r->p2->h-r->p1->h;
}
}
if(r2==1||r2==-1||r2==0){
return r;
}
else{
if(r2<0){
int r3;
if(r->p1->p1==NULL){
r3=1+r->p1->p2->h;
}
else if(r->p1->p2==NULL){
r3=-1-r->p1->p1->h;
}else{
r3=r->p1->p2->h-r->p1->p1->h;
}
if(r3<0){
//left -left
return ll(r);
}
else{
//left-right
r->p1=rr(r->p1);
return ll(r);
}
}
else{
int r3;
if(r->p2->p1==NULL){
r3=1+r->p2->p2->h;
}
else if(r->p2->p2==NULL){
r3=-1-r->p2->p1->h;
}else{
r3=r->p2->p2->h-r->p2->p1->h;
}
if(r3<0){
//right-left
r->p2=ll(r->p2);
return rr(r);
}
else{
//right-right
return rr(r);
}
}
}
}
else{
r->p1=del(r->p1,key);
r->h=max(hei(r->p1),hei(r->p2));
int r2;
if(r->p1==NULL){
if(r->p2==NULL){
return r;
}
else{
r2=1+r->p2->h;
}
}
else{
if(r->p2==NULL){
r2=-1-r->p1->h;
}
else{
r2=r->p2->h-r->p1->h;
}
}
if(r2==1||r2==-1||r2==0){
return r;
}
else{
if(r2<0){
int r3;
if(r->p1->p1==NULL){
r3=1+r->p1->p2->h;
}
else if(r->p1->p2==NULL){
r3=-1-r->p1->p1->h;
}else{
r3=r->p1->p2->h-r->p1->p1->h;
}
if(r3<0){
//left -left
return ll(r);
}
else{
//left-right
r->p1=rr(r->p1);
return ll(r);
}
}
else{
int r3;
if(r->p2->p1==NULL){
r3=1+r->p2->p2->h;
}
else if(r->p2->p2==NULL){
r3=-1-r->p2->p1->h;
}else{
r3=r->p2->p2->h-r->p2->p1->h;
}
if(r3<0){
//right-left
r->p2=ll(r->p2);
return rr(r);
}
else{
//right-right
return rr(r);
}
}
}
}
}
struct n1 * GETT(char * key){
pthread_mutex_lock(&m);
readercout+=1;
if(readercout==1){
sem_wait(&w);
}
pthread_mutex_unlock(&m);
int tt;
struct n1 *dh=h1;
while(1)
{
if(dh==NULL)
{
string s(1, (char)240);
string result = s + "Get key not found";
printf("The key is not present\n");
// record[0]=240;
// record[1]='\0';
// char *pointer=record;
pthread_mutex_lock(&m);
readercout-=1;
if(readercout==0){
sem_post(&w);
}
pthread_mutex_unlock(&m);
return dh;
// return result;
}
else
{
printf("%s = %s \n",key ,dh->key);
if((tt=strcmp(key,dh->key))==0)
{
char filename2[10];
strcpy(filename2,dh->filename);
FILE *f=fopen(strcat(filename2,".txt"),"r");
char record[257];
fseek(f,dh->lineno*257,SEEK_SET);
fgets(record,256,f);
fclose(f);
int sp=0;
while (record[sp]!='#')
{
sp=sp+1;
}
record[sp]='\0';
string result=record;
// char *recor=record;
// return recor;
pthread_mutex_lock(&m);
readercout-=1;
if(readercout==0){
sem_post(&w);
}
pthread_mutex_unlock(&m);
return dh;
// return result;
}
else if(tt>0)
{
dh=dh->p2;
}
else{
dh=dh->p1;
}
}
}
}
void PUTT(char *key,char *value){
sem_wait(&w);
printf("key = %s\nvalue=%s",key, value);
h1=insert(h1,key);
char filename2[10];
strcpy(filename2,add->filename);
FILE *f=fopen(strcat(filename2,".txt"),"r");
char s[21];
int n=0;
fgets(s,21,f);
fclose(f);
// printf("%s\n",s);
if(lineno==100){
while(s[n]!='a'){
n=n+1;
}
int mm=n;
add->lineno=n+1;
char dum[24];
// sprintf(dum,"the free line no is %02d",mm+1);
// write(STDOUT_FILENO,dum,22);
// write(STDOUT_FILENO,"\n",1);
n=strlen(value);
char ts[257];
strcpy(ts,value);
while(n!=256){
ts[n]='#';
n+=1;
}
ts[256]='\n';
n=open(filename2,O_WRONLY);
lseek(n,mm,SEEK_SET);
write(n,"n",1);
lseek(n,(add->lineno)*257,SEEK_SET);
write(n,ts,257);
close(n);
}
else{
n=strlen(value);
char ts[257];
strcpy(ts,value);
while(n!=256){
ts[n]='#';
n+=1;
}
ts[256]='\n';
n=open(strcat(add->filename,".txt"),O_WRONLY);
lseek(n,(add->lineno)*257,SEEK_SET);
lineno=100;
write(n,ts,257);
close(n);
}
printf("PUTT completed\n");
sem_post(&w);
}
struct n2 * checkfile(struct n2 *h2,char *s){
while (1)
{
if(h2==NULL){
// printf("return \n");
return NULL;
}
else{
// printf("%s = %s \n",h2->filename,s);
if(strcmp(h2->filename,s)==0){
// printf("if \n");
return h2;}
else{
h2=h2->pointer;
// printf("else \n");
}
}
}
}
string DELL(char *key){
sem_wait(&w);
h1=del(h1,key);
// write(STDOUT_FILENO,"came out of del \n",17);
// printf("key = %s\n", key);
if(finding==NULL){
printf("could not delete as key does not exist \n");
string s(1, (char)240);
string response = s + "Delete key not found";
sem_post(&w);
return response;
}
// write(STDOUT_FILENO,"the finding is not null \n",22);
char filename2[10];
strcpy(filename2,finding->filename);
int n=open(strcat(filename2,".txt"),O_WRONLY);
// write(STDOUT_FILENO,"test \n",6);
lseek(n,finding->lineno-1,SEEK_SET);
// write(STDOUT_FILENO,"test \n",6);
write(n,"a",1);
// write(STDOUT_FILENO,"test \n",6);
close(n);
// write(STDOUT_FILENO,"test \n",6);
// write(STDOUT_FILENO,"the file name i shoud be delted",31);
// write(STDOUT_FILENO,finding->filename,5);
// write(STDOUT_FILENO,"\n",1);
struct n2 *tp=checkfile(h2,finding->filename);
// write(STDOUT_FILENO,"the file name i shoud be delted",31);
// write(STDOUT_FILENO,finding->filename,1);
// write(STDOUT_FILENO,"\n",1);
if(tp==NULL){
printf("the checkfile has given NULL");
string s(1, (char)240);
string response = s + "Delete Failed";
sem_post(&w);
return response;
}
// write(STDOUT_FILENO,"test \n",6);
tp->nooffreeline=tp->nooffreeline-1;
// write(STDOUT_FILENO,"test \n",6);
free(finding);
finding=NULL;
// printf("del function completed\n");
string s(1, (char)200);
string response = s + "Delete Successful";
sem_post(&w);
return response;
}
void pp(struct n1*r){
if(r==NULL){
return;
}
pp(r->p1);
printf("%s\n",r->key);
pp(r->p2);
}
void pp2(struct n1*r){
if(r==NULL){
return;
}
printf("%s\n",r->key);
pp2(r->p1);
pp2(r->p2);
}
// int main()
// {
// PUTT("1",);
// return 0;
// }
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