struct map* mapCreate(void) { struct map *m=(struct map*)memoryAlloc(sizeof(struct map)); m->size=2; m->freenode=0; m->buffer=(struct node*)memoryAlloc(sizeof(struct node)*m->size); init_table(m); return m; }
static struct node * expand_search(struct map *m,struct key *key) { int i; int s=m->size; struct node *old=m->buffer; struct node *buffer=(struct node*)memoryAlloc(m->size*2*sizeof(struct node)); if (buffer==0) { return 0; } m->size*=2; m->freenode=0; m->buffer=buffer; init_table(m); for (i=0;i<s;i++) { if (old[i].value!=0) { map_search(m,old[i].key)->value = old[i].value; } } memoryFree(old); return map_search(m,key); }
/* * connectionListenerCreate * Creates the connectionListener state instance (OOP) */ ConnectionListener connectionListenerCreate(DistributionManager distributionManager,Properties properties) { ConnectionListenerInternal*m =(ConnectionListenerInternal*)memoryAlloc(sizeof(ConnectionListenerInternal)); m->distributionManager=distributionManager; m->properties=properties; m->active=TRUE; return ; }
CommandExecuter commandExecuterCreate(Registry registry,Core core,SessionMetaData sessionMetaData){ CommandExecuterInternal* m=(CommandExecuterInternal*)memoryAlloc(sizeof(CommandExecuterInternal)); m->registry=registry; m->core=core; m->sessionMetaData=sessionMetaData; return m; }
FileHelper fileHelperCreate(char *path) { FileHelperInternal *m = (FileHelperInternal *) memoryAlloc(sizeof(FileHelperInternal)); m->filePath = path; m->registryMap = hashmapCreate(SMALL_CONTAINER_SIZE); m->managers = hashmapCreate(SMALL_CONTAINER_SIZE); return m; }
CommandContainer commandContainerCreate(){ CommandContainerInternal* m=(CommandContainerInternal*)memoryAlloc(sizeof(CommandContainerInternal)); m->versionMap=hashmapCreate(SMALL_CONTAINER_SIZE); m->versions=linkedListCreate(); m->atomicCommands=linkedListCreate(); m->lock=lockCreate(); return m; }
Lock lockCreate(){ LockInternal * m=memoryAlloc(sizeof(LockInternal)); int result=pthread_mutex_init(&m->mutex,NULL); if(result !=0){ Throw(Fatal,1,"Fail to init mutex"); } return m; }
ValidationManager validationManagerCreate(Registry registry){ ValidationManagerStruct*m=(ValidationManagerStruct*)memoryAlloc(sizeof(ValidationManagerStruct)); m->registry=registry; m->entitiesMap=hashmapCreate(SMALL_CONTAINER_SIZE); m->parentChildrenRelationEntities=hashmapCreate(SMALL_CONTAINER_SIZE); m->childrenParentRelationEntities=hashmapCreate(SMALL_CONTAINER_SIZE); m->deletedEntitiesMap=hashmapCreate(SMALL_CONTAINER_SIZE); return m; }
int registryCreateChildrenBufferElemrnts(ArrayList list,Object data,char* key,int keyLenght){ ArcStruct * arc=(ArcStruct*)data; void* buffer=memoryAlloc(+SIZE_OF_INT*2+arc->arcLength); NodeInternal* node=(NodeInternal*)arc->child; memcpy(buffer+SIZE_OF_INT*0,&(node->id),SIZE_OF_INT); memcpy(buffer+SIZE_OF_INT*1,&arc->arcLength,SIZE_OF_INT); memcpy(buffer+SIZE_OF_INT*2,arc->arcName,arc->arcLength); arrayListAddLast(list,buffer); return VALID; }
void tcpConnectionListenerRunThread(TcpConnection tcpConnection){ TcpConnectionInternal* internal=(TcpConnectionInternal*)tcpConnection; //Declaring proc ess variables. int size ; void* buffer; void *commandBuffer; int commandSize; int currentSize; int tempSize=0; int result; while(internal->active==TRUE){ size=0; while(size<SIZE_OF_INT){ tempSize=read(internal->sockfd,&commandSize+size, SIZE_OF_INT-size); if(tempSize<=0){ internal->active=FALSE; distributionManagerHandleTcpConnectionListenerFailure(internal->distributionManager,internal,internal->coreManagerId); return; } size +=tempSize; } commandSize=htonl(commandSize); buffer=memoryAlloc(commandSize-SIZE_OF_INT); currentSize=0; while(currentSize<commandSize-SIZE_OF_INT){ currentSize+=read(internal->sockfd,buffer+currentSize, MIN(MAX_TCP_MESSAGE_SIZE,commandSize-currentSize-SIZE_OF_INT)); if(currentSize<=0){ internal->active=FALSE; distributionManagerHandleTcpConnectionListenerFailure(internal->distributionManager,internal,internal->coreManagerId); memoryFree(buffer); return; } } Command command=commandDeSerialize(buffer,commandSize-SIZE_OF_INT); commandPush(command,IP,STRING,internal->remoteIp,internal->remoteIpLenght); commandPush(command,PORT,INTEGER,&internal->remotePort); memoryFree(buffer); // handle message distributionManagerConsumeTcp(internal->distributionManager,command); // Create the result message and send it to client commandSize=commandSerialize(command,&commandBuffer); result=writeToSocket(internal->sockfd,commandBuffer,commandSize); memoryFree(commandBuffer); if(result<0){ internal->active=FALSE; distributionManagerHandleTcpConnectionListenerFailure(internal->distributionManager,internal,internal->coreManagerId); return; } // Delete all none model or none atomic commands if(commandIsDeleteOnExit(command)==TRUE){ commandFree(command); } } internal->readyForCleanup+=LISTENER_READY_FOR_CLEANUP; }
Properties propertiesCreate(char *filePath) { PropertiesInternal *m = (PropertiesInternal *) memoryAlloc(sizeof(PropertiesInternal)); m->map = hashmapCreate(SMALL_CONTAINER_SIZE); FILE *fp; long len; char *buf; fp = fopen(filePath, "rt"); if (fp == NULL) { printf("couldn't open the properties file"); }; fseek(fp, 0, SEEK_END); //go to end len = ftell(fp); //get position at end (length) fseek(fp, 0, SEEK_SET); //go to beg. buf = (char *) memoryAlloc(len + 1); //malloc buffer long result = fread(buf, (size_t) (len + 1), 1, fp); //read into buffer if (result < 0) { printf("Fail to read properties file ."); } fclose(fp); buf[len] = 0; char *key; char *value; const char newLineDelimiter[] = "\n"; const char spaseDelimiter[] = "="; char *token; token = strtok(buf, newLineDelimiter); LinkedList linkedList = linkedListCreate(); while (token != NULL) { linkedListAddFirst(linkedList, token); token = strtok(NULL, newLineDelimiter); } while (linkedListSize(linkedList) > 0) { char *token = (char *) linkedListRemoveFirst(linkedList); key = strtok(token, spaseDelimiter); value = strtok(NULL, spaseDelimiter); unsigned int keyId = stringTransformToInt(key, strlen(key)); hashmapPut(m->map, keyId, value); } linkedListFree(linkedList); return m; }
int attributeSerializeLocal(Attribute attribute,void** buffer){ int length=SIZE_OF_INT*4+attribute->nameLength+attribute->dataLength; *buffer=memoryAlloc(length); memcpy((*buffer)+SIZE_OF_INT*0,&length,SIZE_OF_INT); memcpy((*buffer)+SIZE_OF_INT*1,&attribute->nameLength,SIZE_OF_INT); memcpy((*buffer)+SIZE_OF_INT*2,&attribute->dataType,SIZE_OF_INT); memcpy((*buffer)+SIZE_OF_INT*3,&attribute->dataLength,SIZE_OF_INT); memcpy((*buffer)+SIZE_OF_INT*4,attribute->name,attribute->nameLength); memcpy((*buffer)+SIZE_OF_INT*4+attribute->nameLength,attribute->data,attribute->dataLength); return length; }
/** * AtomicOnlineSchema open;<p> * create new instance of commandsList which will contain all the transactional commands.<p> * create new instance of transactionEntities which will contain a list of the loked entities. */ Transaction transactionCreate(Core core,CommandExecuter commandExecutor,Registry registry,SchemaAdapter schemaAdapter,SessionMetaData sessionMetaData,long lockTimeout){ TransactionInternal*m=(TransactionInternal*)memoryAlloc(sizeof(TransactionInternal)); m->core=core; m->commandExecutor=commandExecutor; m->registry=registry; m->scheamAdapter=schemaAdapter; m->sessionMetaData=sessionMetaData; m->transactionEntities=hashmapCreate(SMALL_CONTAINER_SIZE); m->dirtySet=hashmapCreate(SMALL_CONTAINER_SIZE); m->commandList=arrayListCreate(); m->transaction=NULL; m->lockTimeout=lockTimeout; }
/* * serialize Node; */ int nodeSerialize( Node node,char ** result){ NodeInternal* internal=(NodeInternal*)node; Array attributes; stringHashmapToArray(node->attributes,&attributes); void* attributeBuffer; int attributeLength; int size=SIZE_OF_INT*4; int numberOfAttributes=arrayGetLength(attributes); int i; Attribute attribute; for(i=0;i<numberOfAttributes;i++){ attribute=arrayGetObject(attributes,i); size+=attributeSerializeLocal(attribute,&attributeBuffer); memoryFree(attributeBuffer); } stringHashmapIterate(internal->children,registryUpdateNodeBufferSize,(Object)&size); char * buffer=memoryAlloc(size); *result=buffer; memcpy(buffer+SIZE_OF_INT*0,&(internal->schemaId),SIZE_OF_INT); memcpy(buffer+SIZE_OF_INT*1,&(internal->id),SIZE_OF_INT); memcpy(buffer+SIZE_OF_INT*2,&numberOfAttributes,SIZE_OF_INT); buffer+=SIZE_OF_INT*3; for(i=0;i<numberOfAttributes;i++){ attribute=arrayGetObject(attributes,i); attributeLength=attributeSerializeLocal(attribute,&attributeBuffer); memcpy(buffer,attributeBuffer,attributeLength); buffer+=attributeLength; memoryFree(attributeBuffer); } arrayFree(attributes); ArrayList list=arrayListCreate(); stringHashmapIterate(internal->children,registryCreateChildrenBufferElemrnts,list); int numberOfChildren=arrayListSize(list); memcpy(buffer,&numberOfChildren,SIZE_OF_INT); buffer+=SIZE_OF_INT; int j; for(j=0;j<numberOfChildren;j++){ void* bufferElement=arrayListGetValueAt(list,j); int bufferSize; memcpy(&bufferSize,bufferElement+SIZE_OF_INT,SIZE_OF_INT); memcpy(buffer,bufferElement,SIZE_OF_INT*2+bufferSize); buffer+=SIZE_OF_INT*2+bufferSize; memoryFree(bufferElement); } arrayListFree(list); return size; }
TcpConnection tcpConnectionCreate(DistributionManager distributionManager,int sockfd,int coreManagerId,char* remoteIp,int remotePort){ TcpConnectionInternal* m=(TcpConnectionInternal*)memoryAlloc(sizeof(TcpConnectionInternal)); m->coreManagerId=coreManagerId; m->sockfd=sockfd; m->remoteIp=strdup(remoteIp); m->remoteIpLenght=strlen(remoteIp); m->remotePort=remotePort; m->distributionManager=distributionManager; m->active=TRUE; m->commands=arrayListCreate(); m->readyForCleanup=0; int set = 1; setsockopt(sockfd, SOL_SOCKET, MSG_NOSIGNAL, (void *)&set, sizeof(int)); return m; }
int attributeSerialize(Attribute attribute,void** buffer){ int length=SIZE_OF_INT*4+attribute->nameLength+attribute->dataLength; *buffer=memoryAlloc(length); int netLength=ntohl(length); int netNameLength=ntohl(attribute->nameLength); int netDataLength=ntohl(attribute->dataLength); int netDataType=ntohl(attribute->dataType); memcpy((*buffer)+SIZE_OF_INT*0,&netLength,SIZE_OF_INT); memcpy((*buffer)+SIZE_OF_INT*1,&netNameLength,SIZE_OF_INT); memcpy((*buffer)+SIZE_OF_INT*2,&netDataType,SIZE_OF_INT); memcpy((*buffer)+SIZE_OF_INT*3,&netDataLength,SIZE_OF_INT); memcpy((*buffer)+SIZE_OF_INT*4,attribute->name,attribute->nameLength); memcpy((*buffer)+SIZE_OF_INT*4+attribute->nameLength,attribute->data,attribute->dataLength); return length; }
TcpConnection tcpConnectionCreate(ConnectionPool connectionPool,DistributionManager distributionManager,int coreManagerId,Properties properties){ TcpConnectionInternal* m=(TcpConnectionInternal*)memoryAlloc(sizeof(TcpConnectionInternal)); m->connectionPool=connectionPool; char* remoteIp=propertiesGet(properties,"ip"); int remotePort=atoi(propertiesGet(properties,"port")); memset(&m->address, 0, sizeof(m->address)); /* Zero out structure */ m->address.sin_family = AF_INET; /* Internet address family */ m->address.sin_addr.s_addr = inet_addr(remoteIp); /* Server IP address */ m->address.sin_port = htons(remotePort); /* Server port */ /* Establish the connection to the echo server */ if((m->sockfd=socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0){ printf("Fail to establish tcp connection with the server - IP%s port %i ",remoteIp,remotePort); return NULL; } if (connect(m->sockfd, (struct sockaddr *) &m->address, sizeof(m->address)) < 0){ printf("Fail to establish tcp connection with the server - IP%s port %i ",remoteIp,remotePort); return NULL; } m->coreManagerId=coreManagerId; m->remoteIp=strdup(remoteIp); m->remoteIpLenght=strlen(remoteIp); m->remotePort=remotePort; m->distributionManager=distributionManager; m->active=TRUE; int set = 1; setsockopt(m->sockfd, SOL_SOCKET, MSG_NOSIGNAL, (void *)&set, sizeof(int)); char* userName=propertiesGet(properties,"userName"); char* password=propertiesGet(properties,"password"); char* ip=propertiesGet(properties,"ip"); int port=atoi(propertiesGet(properties,"port")); int userNameLenght=strlen(userName); int passwordLenght=strlen(password); int ipLenght=strlen(ip); Command command= commandBuilderBuildTcpConnectionCommand(userName, userNameLenght,password,passwordLenght,coreManagerId,ip,ipLenght,port); tcpConnectionSend(m,command); int status = commandGetInt(command,COMMAND_STATUS); if( status!=VALID){ printf("Fail to establish tcp connection with the server : cause is authentication failure."); return NULL; } return m; }
int tcpConnectionSend(TcpConnection tcpConnection,Command command){ TcpConnectionInternal* internal=(TcpConnectionInternal*)tcpConnection; int size ; void* buffer; void *commandBuffer; int commandSize; int currentSize; int tempSize=0; int result; if(internal->active==FALSE){ return; } commandSize=commandSerialize(command,&commandBuffer); result = tcpConnectionWriteToSocket(internal->sockfd,commandBuffer,commandSize); memoryFree(commandBuffer); if(result!=0){ } size=0; while(size<SIZE_OF_INT){ tempSize=read(internal->sockfd,&commandSize+size, SIZE_OF_INT-size); if(tempSize<0){ internal->active=FALSE; distributionManagerHandleTcpConnectionListenerFailure(internal->distributionManager,internal,internal->coreManagerId); return; } size +=tempSize; } commandSize=htonl(commandSize); buffer=memoryAlloc(commandSize-SIZE_OF_INT); currentSize=0; while(currentSize<commandSize-SIZE_OF_INT){ currentSize+=read(internal->sockfd,buffer+currentSize, MIN(MAX_TCP_MESSAGE_SIZE,commandSize-currentSize-SIZE_OF_INT)); if(currentSize<0){ internal->active=FALSE; distributionManagerHandleTcpConnectionListenerFailure(internal->distributionManager,internal,internal->coreManagerId); memoryFree(buffer); return; } } Command resultCommand=commandDeSerialize(buffer,commandSize-SIZE_OF_INT); commandFill(resultCommand,command); return VALID; }
/* * Create node. */ int nodeCreate(Registry registry,int nodeId,int schemaId,int lock,Node * result){ // Create the new node and update data RegistryInternal* internal=(RegistryInternal*)registry; NodeInternal* node = (NodeInternal*) memoryAlloc(sizeof(NodeInternal)); node->id=nodeId; node->phantom=FALSE; node->locked=FALSE; node->children=stringHashmapCreate(SMALL_CONTAINER_SIZE); node->schemaId=schemaId; node->phantom=FALSE; node->lockOwnerSessionId=lock; node->attributes=stringHashmapCreate(SMALL_CONTAINER_SIZE); hashmapPut(internal->nodes,nodeId,node); // Update result only if it doesn't refer to NULL; if(result!=NULL){ *result =(Node)node; } return VALID; }
void commandContainerPushCommand(CommandContainer commandContainer,Command command){ CommandContainerInternal* internal=(CommandContainerInternal*)commandContainer; lockLock(internal->lock); time_t currentTime; // Stores seconds elapsed since 01-01-1970 time(¤tTime); int type=commandGetInt(command,COMMAND_TYPE); if(type==ATOMIC_MODEL_COMMAND){ commandPush(command,SERVER_HANDLING_TIME,INTEGER,¤tTime); linkedListAddLast(internal->atomicCommands,command); }else{ int version=commandGetInt(command,VERSION); int* intBuffer=memoryAlloc(SIZE_OF_INT); memcpy(intBuffer,&version,SIZE_OF_INT); commandPush(command,SERVER_HANDLING_TIME,INTEGER,¤tTime); // Update version Map; hashmapPut(internal->versionMap,version,command); // Update version list linkedListAddLast(internal->versions,intBuffer); } lockUnlock(internal->lock); }
/* * connectionListenerRunThread * 1. Listen and wait for connections request * 2. convert the request into command * 3. pass the request command to the DistributionMAnager */ void connectionListenerRunThread(ConnectionListener connectionListener) { // Declaring process variables. ConnectionListenerInternal* internal=(ConnectionListenerInternal*)connectionListener; int server_sockfd, client_sockfd; int server_len ; int size ; unsigned client_len; struct sockaddr_in server_address; struct sockaddr_in client_address; void* readBuffer; void* writeBuffer; int commandSize; int currentSize; const int optVal = 1; const socklen_t optLen = sizeof(optVal); // Remove any old socket and create an unnamed socket for the server. server_sockfd = socket(AF_INET, SOCK_STREAM, 0); int rtn = setsockopt(server_sockfd, SOL_SOCKET, SO_REUSEADDR, (void*) &optVal, optLen); server_address.sin_family = AF_INET; server_address.sin_addr.s_addr = INADDR_ANY;// htons(ipConverterToLong(internal->ip)); server_address.sin_port = htons(internal->port); server_len = sizeof(server_address); int rc = bind(server_sockfd, (struct sockaddr *) &server_address, server_len); if(rc<0) { Throw (Fatal,1,"Failt to bind to listen port \n"); } printf("RC from bind = %d\n", rc ); //Create a connection queue and wait for clients rc = listen(server_sockfd, 5); printf("RC from listen = %d\n", rc ) ; client_len = sizeof(client_address); while(internal->active==TRUE) { client_sockfd = accept(server_sockfd, (struct sockaddr *) &client_address, &client_len); char* ip=inet_ntoa(client_address.sin_addr); size=0; size += read(client_sockfd,&commandSize+size, SIZE_OF_INT); if(size<SIZE_OF_INT) { continue; } commandSize=htonl(commandSize); readBuffer=memoryAlloc(commandSize-SIZE_OF_INT); currentSize=0; currentSize+=read(client_sockfd,readBuffer+currentSize, commandSize-SIZE_OF_INT); if(currentSize<commandSize-SIZE_OF_INT) { memoryFree(readBuffer); continue; } Command command=commandDeSerialize(readBuffer,commandSize-SIZE_OF_INT); memoryFree(readBuffer); // handle message handleMessage(client_sockfd,internal,command,ip); // Create the result message and send it to client commandSize=commandSerialize(command,&writeBuffer); int result = write(client_sockfd,writeBuffer,commandSize); if(result<0) { printf("ConnectionListener Fail to write to socket"); } memoryFree(writeBuffer); //commandDestroy(command); } printf("Server connection listener stoped\n"); close(client_sockfd); }
void fileHelperLoad(FileHelper fileHelper) { FileHelperInternal *m = (FileHelperInternal *) fileHelper; fileHelperCheckCreate(m->filePath); int sizeOfInt = sizeof(int); struct direct **schemasIds = {0}; struct direct *schenaId; int size = scandir(m->filePath, &schemasIds, 0, alphasort); // loop all schemas int i; for (i = 0; i < size; i++) { schenaId = (struct direct *) schemasIds[i]; char *schemaIdString = schenaId->d_name; int schemaId = atoi(schemaIdString); if (schemaId != 0) { printf("schema %s\n", schenaId->d_name); struct direct **pagesIds = {0}; struct direct *pageStruct; char endOdString = '\0'; int pageDirLength = strlen(m->filePath); int schemaIdStringLength = strlen(schemaIdString); char slesh = '/'; char *pageDir = (char *) memoryAlloc(pageDirLength + 1 + schemaIdStringLength + 1); memcpy(pageDir, m->filePath, pageDirLength); memcpy(pageDir + pageDirLength, &slesh, 1); memcpy(pageDir + pageDirLength + 1, schemaIdString, schemaIdStringLength); memcpy(pageDir + pageDirLength + 1 + schemaIdStringLength, &endOdString, 1); // cleanBuffer(pageDir); // strcat(pageDir,m->filePath); // strcat(pageDir,"/"); // strcat(pageDir,schemaIdString); HashMap childrenRelations = hashmapCreate(SMALL_CONTAINER_SIZE); Registry registry = registryCreate(); hashmapPut(m->registryMap, schemaId, registry); PageManager pageManager = pageManagerCreate(schemaId, pageDir); hashmapPut(m->managers, schemaId, pageManager); int size = scandir(pageDir, &pagesIds, 0, alphasort); int h; //loop pages for (h = 0; h < size; h++) { //scandir (pageDir, &pagesIds, 0, alphasort); pageStruct = (struct direct *) pagesIds[h]; char *pageString = pageStruct->d_name; int pageId = atoi(pageString); if (pageId != 0) { printf("page %s\n", pageStruct->d_name); // create file path. //int i=0; int pageDirLength = strlen(pageDir); int pageStringLength = strlen(pageString); char slesh = '/'; char *filePath = (char *) memoryAlloc(pageDirLength + 1 + pageStringLength + 1); memcpy(filePath, pageDir, pageDirLength); memcpy(filePath + pageDirLength, &slesh, 1); memcpy(filePath + pageDirLength + 1, pageString, pageStringLength); memcpy(filePath + pageDirLength + 1 + pageStringLength, &endOdString, 1); FILE *fp; long len; // Create pointer to file. fp = fopen(filePath, "rb"); if (fp == NULL) { printf("couldn't open the properties file"); } fseek(fp, 0, SEEK_END); // Go to end len = ftell(fp); // Get position at end (length) // Read data to buffer char *buffer = memoryAlloc(sizeof(char) * len); // Create buffer char *pointerToFreeBuffer = buffer; fseek(fp, 0, SEEK_SET); // Go to the beginning. int result = fread(buffer, len, 1, fp); // Read into buffer if (result < 0) { printf("Fail to read file : %s", buffer); } fclose(fp); int order = 0; long index = 0; long step = 0; while (index < len) { char *start = buffer; int id; int schemaId; int numberOfAttributes; int childrenSize; int attributeSize; memcpy(&schemaId, buffer + SIZE_OF_INT * 0, SIZE_OF_INT); memcpy(&id, buffer + SIZE_OF_INT * 1, SIZE_OF_INT); memcpy(&numberOfAttributes, buffer + SIZE_OF_INT * 2, SIZE_OF_INT); buffer += SIZE_OF_INT * 3; Node node; nodeCreate(registry, id, schemaId, FALSE, &node); int h; for (h = 0; h < numberOfAttributes; h++) { memcpy(&attributeSize, buffer, SIZE_OF_INT); Attribute attribute = attributeDeserializeLocal(buffer); buffer += attributeSize; stringHashmapPut(node->attributes, attribute->name, attribute->nameLength, attribute); } memcpy(&childrenSize, buffer, SIZE_OF_INT); Array children = arrayCreate(childrenSize); hashmapPut(childrenRelations, id, children); int i; buffer += SIZE_OF_INT; int childrenIdBuffer; for (i = 0; i < childrenSize; i++) { memcpy(&childrenIdBuffer, buffer + SIZE_OF_INT, SIZE_OF_INT); void *bufferElement = memoryAlloc(childrenIdBuffer + SIZE_OF_INT * 2); memcpy(bufferElement, buffer, SIZE_OF_INT * 2 + childrenIdBuffer); arrayInsertObject(children, bufferElement); buffer += SIZE_OF_INT * 2 + childrenIdBuffer; } step = buffer - start; index += step; pageManagerRegister(pageManager, pageId, id, index - step, index, order, filePath); order++; } // Free the file buffer memoryFree(pointerToFreeBuffer); } } if (size > 0) { while (size) { size = size - 1; free(pagesIds[size]); } free(pagesIds); } hashmapIterate(childrenRelations, &updateChildren, registry); hashmapFree(childrenRelations); } } if (size > 0) { while (size) { size = size - 1; free(schemasIds[size]); } free(schemasIds); } }
/* * Create the registry structure. */ Registry registryCreate(){ RegistryInternal* m = (RegistryInternal*) memoryAlloc(sizeof(RegistryInternal)); m->nodes=hashmapCreate(SMALL_CONTAINER_SIZE); return m; }
void fileHelperConsume(FileHelper fileHelper, Command command) { FileHelperInternal *m = (FileHelperInternal *) fileHelper; PageManager pageManager = NULL; int schemaId = commandGetInt(command, SCHEMA_ID); int entityId = commandGetInt(command, ENTITY_ID); hashmapGet(m->managers, schemaId, &pageManager); char schemaIdString[10]; if (pageManager == NULL) { stringConvertInt(schemaIdString, schemaId); char *pageDir = (char *) memoryAlloc(strlen(m->filePath) + strlen(schemaIdString) + 1); strcpy(pageDir, m->filePath); strcat(pageDir, "/"); strcat(pageDir, schemaIdString); mkdir(pageDir, S_IRWXU | S_IRGRP | S_IXGRP); pageManager = pageManagerCreate(schemaId, pageDir); hashmapPut(m->managers, schemaId, pageManager); } Registry registry; hashmapGet(m->registryMap, schemaId, ®istry); if (registry == NULL) { Throw(Fatal, 1, "FileHelper unsupported state failed to find registry for schemaId "); } char *buf = NULL; int type = commandGetInt(command, COMMAND_TYPE); switch (type) { case CREATE_NEW_ENTITY: { int lenght = commandGetByteArray(command, SERIALIZED_NODE, &buf); pageManagerAdd(pageManager, entityId, buf, lenght); return; } case DELETE_ENTITY: { pageManagerDelete(pageManager, entityId); return; } case ADD_CHILD_ENTITY_TO_PARENT_ENTITY: { int lenght = commandGetByteArray(command, SERIALIZED_NODE, &buf); pageManagerUpdate(pageManager, entityId, buf, lenght); return; } case REMOVE_CHILD_ENTITY_FROM_PARENT_ENTITY: { int lenght = commandGetByteArray(command, SERIALIZED_NODE, &buf); pageManagerUpdate(pageManager, entityId, buf, lenght); return; } case UPDATE_ENTITY: { int lenght = commandGetByteArray(command, SERIALIZED_NODE, &buf); pageManagerUpdate(pageManager, entityId, buf, lenght); return; } case ADD_ATTRIBUTE: { int lenght = commandGetByteArray(command, SERIALIZED_NODE, &buf); pageManagerUpdate(pageManager, entityId, buf, lenght); return; } case REMOVE_ATTRIBUTE: { int lenght = commandGetByteArray(command, SERIALIZED_NODE, &buf); pageManagerUpdate(pageManager, entityId, buf, lenght); return; } } }