void nosql_find(char* collection, char* find, char* projection) { FILE * fp; char * line = NULL; size_t len = 0; ssize_t read; char* filePath = nosql_getPath(collection); fp = fopen(filePath, "r"); //Open file free(filePath); if (fp == NULL) return; t_hashmap* findMap = JSON_parse(find); t_hashmap* projectionMap = JSON_parse(projection); t_hashmap* current = NULL; while ((read = getline(&line, &len, fp)) != -1) { //Read line current = JSON_parse(line); //Convert to hashmap if (nosql_mapPassCondition(current, findMap) == 1) { //If hashmap pass the find condition nosql_print(current, projectionMap); //Output this projection } free(current); } //You'r free free(projectionMap); free(findMap); fclose(fp); if (line) free(line); }
void nosql_set(char* collection, char* set, char* where) { char* filePath = nosql_getPath(collection); char* fileTmpPath = nosql_getTmpPath(collection); FILE* fp = fopen(filePath, "r"); //Open original file FILE* fpTmp = fopen(fileTmpPath, "w+"); //Open temp file if (fp == NULL) return; if (fpTmp == NULL) { fclose(fp); return; } char * line = NULL; size_t len = 0; ssize_t read; t_hashmap* setMap = JSON_parse(set); t_hashmap* whereMap = JSON_parse(where); t_hashmap* current; while ((read = getline(&line, &len, fp)) != -1) { //Read line from the original file current = JSON_parse(line); //Convert to hashmap if (nosql_mapPassCondition(current, whereMap) == 1) { //If hashmap pass the where condition for (int slot = 0; slot < setMap->slots; slot++) { //Loop on Set data t_hashmap_entry* entries = setMap->entries[slot]; while (entries != NULL) { hashmap_put(current, entries->key, entries->value); //Update Hashmap entries = entries->next; } } } fprintf(fpTmp, "%s\n", JSON_stringify(current));//Insert into temp file free(current); } //You'r free free(setMap); free(whereMap); if (line) free(line); fclose(fpTmp); fclose(fp); remove(filePath); rename(fileTmpPath, filePath); free(fileTmpPath); free(filePath); }
void nosql_remove(char* collection, char* remove) { char* filePath = nosql_getPath(collection); char* fileTmpPath = nosql_getTmpPath(collection); FILE* file = fopen(filePath, "r"); //Open original file FILE* fileTmp = fopen(fileTmpPath, "w+"); //Open temp file if (file == NULL) return; if (fileTmp == NULL) { fclose(file); return; } char * line = NULL; size_t len = 0; ssize_t read; t_hashmap* removedMap = JSON_parse(remove); t_hashmap* current; while ((read = getline(&line, &len, file)) != -1) { //Read line from the original file current = JSON_parse(line); //Convert to hashmap if (nosql_mapPassCondition(current, removedMap) != 1) { fprintf(fileTmp, "%s\n", JSON_stringify(current)); } free(current); } //You'r free free(removedMap); if (line) free(line); fclose(fileTmp); fclose(file); rename(fileTmpPath, filePath); free(fileTmpPath); free(filePath); }
hashmap_t* hashmap_load(char * db) { hashmap_t* hashmap=hashmap_create(); FILE* fp; size_t len = 0; ssize_t read; char* line=NULL; fp = fopen(db, "r"); if (fp == NULL) return NULL; while ((read = getline(&line, &len, fp)) != -1) { if (line[read - 1] == '\n') line[read - 1] = '\0'; list_value_t* addlist=JSON_parse(line); hashmap_add_list(hashmap,addlist); } fclose(fp); if (line) free(line); return hashmap; }
hashmap_node_t* hashmap_find(hashmap_t* hashmap,char *string) { char* key; list_value_t* find=JSON_parse(string); key=find->key; int create=1; hashmap_node_t* result=NULL; if(strlen(key)==0) { int i; for(i=0; i<BUCKET_NUMBER; i++) { hashmap_node_t* tmp=hashmap->map[i]; while (tmp!=NULL) { if(create) { result=hashmap_node_create(0, tmp->lt); create=0; } else hashmap_node_append_node(result, hashmap_node_create(0, tmp->lt)); tmp=tmp->next; } } return result; } else { uint32_t hash=hashmap_hash(key); hashmap_node_t* bucket = *hashmap_get_bucket(hashmap, hash); if(bucket==NULL) return bucket; else { while(bucket!=NULL) { if(bucket->hash==hash && strcmp(key,bucket->lt->key)==0 && value_compare(find, bucket->lt)==0) { if(create) { result=hashmap_node_create(hash, bucket->lt); create=0; } else hashmap_node_append_node(result, hashmap_node_create(hash, bucket->lt)); } bucket=bucket->next; } } return result; } }
void hashmap_where(hashmap_node_t** result, char *string) { int found=0; hashmap_node_t* prev=NULL; hashmap_node_t* current=*result; list_value_t* tmprec; list_value_t* wheres=JSON_parse(string); list_value_t* tmpw; while (current!=NULL) { tmpw=wheres; while(tmpw!=NULL) { tmprec=current->lt->record; while (tmprec!=NULL) { if(strcmp(tmpw->key,tmprec->key)==0 && value_compare(tmpw, tmprec) == 0) { found=1; } tmprec=tmprec->next; } tmpw=tmpw->next; } if(!found) { if (prev) { prev->next=current->next; free(current); current=prev->next; } else { *result=current->next; free(current); current=*result; } } else { prev=current; current=current->next; } found=0; } }
int build_help_entries(char *file_name) { struct JSONObject *root = JSON_parse(file_name); if(!root) { printf("Unable to parse file %s\n", file_name); return 1; } struct JSONObject *entries = JSON_get_object_item(root, "entries"); if(!entries) { printf("EXCEPTION", "No entries found in file"); JSON_delete_object(root); return 1; } nof_help_entries = JSON_get_array_size(entries); help_entries = (help_entry_t *)malloc(nof_help_entries * sizeof(help_entry_t)); if(!help_entries) { printf("EXCEPTION", "Unable to allocate memory for help entries"); JSON_delete_object(root); return 1; } int i,j; for(i=0; i<nof_help_entries; i++) { struct JSONObject *entry = JSON_get_array_item(entries, i); unsigned int type = JSON_get_object_item(entry, "type")->ivalue; if(type != FUNCTION && type != MACRO && type != SPECIAL_OPERATOR) { printf("EXCEPTION", "Invalid help entry type"); JSON_delete_object(root); free(help_entries); return 1; } help_entries[i].type = type; help_entries[i].name = strdup(JSON_get_object_item(entry, "name")->strvalue); help_entries[i].syntax = strdup(JSON_get_object_item(entry, "syntax")->strvalue); help_entries[i].args = strdup(JSON_get_object_item(entry, "args")->strvalue); help_entries[i].desc = strdup(JSON_get_object_item(entry, "desc")->strvalue); help_entries[i].exceptions = strdup(JSON_get_object_item(entry, "exceptions")->strvalue); help_entries[i].examples_count = JSON_get_array_size(JSON_get_object_item(entry, "examples")); if(help_entries[i].examples_count > 0) { help_entries[i].examples = (char **)malloc(help_entries[i].examples_count * sizeof(char *)); if(!help_entries[i].examples) { printf("EXCEPTION", "Unable to allocate memory for examples"); JSON_delete_object(root); free(help_entries); return 1; } } for(j=0; j<help_entries[i].examples_count; j++) help_entries[i].examples[j] = strdup(JSON_get_array_item(JSON_get_object_item(entry, "examples"), j)->strvalue); help_entries[i].see_also_count = JSON_get_array_size(JSON_get_object_item(entry, "see-also")); if(help_entries[i].see_also_count > 0) { help_entries[i].see_also = (char **)malloc(help_entries[i].see_also_count * sizeof(char *)); if(!help_entries[i].see_also) { printf("EXCEPTION", "Unable to allocate memory for see-also-count"); JSON_delete_object(root); free(help_entries); return 1; } } for(j=0; j<help_entries[i].see_also_count; j++) help_entries[i].see_also[j] = strdup(JSON_get_array_item(JSON_get_object_item(entry, "see-also"), j)->strvalue); } JSON_delete_object(root); return 0; }
int main() { json* jsonObject = NULL; char* buff = (char*) malloc(2048); JSON_parse("{" \ "_id: \"55f4e924dc0280cf398b4567\"," \ "sequenceID: \"EW-00000002\"," \ "\"users\": [" \ " \"3948590903446507\"," \ " \"980111083745926\"" \ " ]," \ "\"started\": 1440205510328," \ "embed: { inner : \"inner_val\", testfloat: 12345678.9432 }" \ "}", &jsonObject); memset(buff, 0, 2048); if (JSON_Stringify(jsonObject, buff, 2048) == 0) printf("Parsed: %s\n", buff); jsonObject = JSON_New("root"); json* item = JSON_New("_id"); JSON_SetValue(item, "55f4e924dc0280cf398b4567"); JSON_AddObject(jsonObject, item); item = JSON_New("sequenceID"); JSON_SetValue(item, "EW-00000002"); JSON_AddObject(jsonObject, item); array* a = array_create(); array_add(a, "744566090321683857"); array_add(a, "487411096483838"); item = JSON_New("users"); JSON_SetArrayValue(item, a); JSON_AddObject(jsonObject, item); item = JSON_New("started"); JSON_SetValue(item, "1440205510328"); JSON_AddObject(jsonObject, item); json* embed = JSON_New("embed"); JSON_AddObject(jsonObject, embed); item = JSON_New("inner"); JSON_SetValue(item, "inner_val"); JSON_AddObject(embed, item); item = JSON_New("testfloat"); JSON_SetValue(item, "12345678.9432"); JSON_AddObject(embed, item); jsonType type; item = JSON_GetObjectItem(jsonObject, "sequenceID", &type); JSON_PrintItemValue(item, type); item = JSON_GetObjectItem(jsonObject, "embed.inner", &type); JSON_PrintItemValue(item, type); item = JSON_GetObjectItem(jsonObject, "embed.testfloat", &type); JSON_PrintItemValue(item, type); item = JSON_GetObjectItem(jsonObject, "users", & type); JSON_PrintItemValue(item, type); memset(buff, 0, 2048); if (JSON_Stringify(jsonObject, buff, 2048) == 0) printf("Built: %s\n", buff); }