// START FUNC DECL int del_all_lmt_flds( char *list ) // STOP FUNC DECL { int status = 0; int num_in_list,list_id, tbl_id, xlist_id; // OLD char *new_fld = NULL; int new_fld_id; long long new_fld_cnt; status = get_list_id(list, &list_id, &tbl_id, &xlist_id); cBYE(status); status = get_num_in_list(list_id, &num_in_list); cBYE(status); for ( int pos = num_in_list; pos > 0; pos-- ) { status = del_aux_fld_if_exists(tbl_id, list_id, pos, "lmt_"); cBYE(status); /* OLD status = del_aux_fld_if_exists(tbl_id, list_id, pos, "new_", "xcl_"); cBYE(status); status = get_fld_name(list_id, pos, "new_", &new_fld); cBYE(status); status = get_fld_id (list_id, pos, "new_", &new_fld_id); cBYE(status); status = get_fld_cnt (list_id, pos, "new_", &new_fld_cnt); cBYE(status); status = set_fld_id_cnt(list_id, pos, "xcl_", new_fld_id, new_fld, new_fld_cnt); cBYE(status); status = set_fld_id_cnt(list_id, pos, "lmt_", new_fld_id, new_fld, new_fld_cnt); cBYE(status); free_if_non_null(new_fld); */ } BYE: // OLD free_if_non_null(new_fld); return(status); }
// START FUNC DECL int ylc_del( char *list ) // STOP FUNC DECL { int status = 0; bool b_is_list; int rc, list_id = -1, tbl_id = -1, xlist_id = -1; int num_in_list; char *zErrMsg = NULL; char *cum_fld = NULL; int cum_fld_id; char command[1024]; if ( ( list == NULL ) || ( *list == '\0' ) ) { go_BYE(-1); } status = is_list(list, &b_is_list); if ( b_is_list == false ) { fprintf(stderr, "List [%s] does not exist \n", list); return(status); } status = get_list_id(list, &list_id, &tbl_id, &xlist_id); cBYE(status); if ( list_id <= 0 ) { go_BYE(-1); } // Delete xlist if any if ( xlist_id > 0 ) { status = get_xlist_info(xlist_id, &cum_fld, &cum_fld_id); cBYE(status); status = del_fld(NULL, tbl_id, NULL, cum_fld_id, true); cBYE(status); zero_string(command, 1024); sprintf(command, "delete from xlist where id = %d \n", xlist_id); rc = sqlite3_exec(g_db, command, NULL, 0, &zErrMsg); if( rc != SQLITE_OK ){ handle_sqlite_error(); } // delete entries corresponding to this xlist zero_string(command, 1024); sprintf(command, "delete from flds_x_xlist where xlist_id = %d \n", xlist_id); rc = sqlite3_exec(g_db, command, NULL, 0, &zErrMsg); if( rc != SQLITE_OK ){ handle_sqlite_error(); } } status = get_num_in_list(list_id, &num_in_list); cBYE(status); for ( int position = num_in_list; position > 0 ; position-- ) { status = del_aux_fld_if_exists(tbl_id, list_id, position, "lmt_"); cBYE(status); status = del_aux_fld_if_exists(tbl_id, list_id, position, "xcl_"); cBYE(status); status = del_aux_fld_if_exists(tbl_id, list_id, position, "new_"); cBYE(status); } zero_string(command, 1024); sprintf(command, "delete from flds_x_list where list_id = %d \n", list_id); rc = sqlite3_exec(g_db, command, NULL, 0, &zErrMsg); if( rc != SQLITE_OK ){ handle_sqlite_error(); } zero_string(command, 1024); sprintf(command, "delete from list where name = '%s' \n", list); rc = sqlite3_exec(g_db, command, NULL, 0, &zErrMsg); if( rc != SQLITE_OK ){ handle_sqlite_error(); } BYE: // TODO P3: free_if_non_null(zErrMsg); free_if_non_null(cum_fld); return(status); }
// START FUNC DECL int and_not_excl( char *list ) // STOP FUNC DECL { int status = 0; char xcl_fld[MAX_LEN_FLD_NAME+1]; char *new_fld = NULL, *cum_xcl_fld = NULL; int num_in_list; int tbl_id, list_id, xlist_id, cum_xcl_fld_id; TBL_REC_TYPE tbl_rec; status = get_list_id(list, &list_id, &tbl_id, &xlist_id); cBYE(status); if ( xlist_id <= 0 ) { go_BYE(-1); } status = get_num_in_list(list_id, &num_in_list); cBYE(status); status = get_tbl_meta(tbl_id, &tbl_rec); cBYE(status); char *tbl = tbl_rec.name; if ( num_in_list == 0 ) { return(status); } // Nothing to do status = get_xlist_info(xlist_id, &cum_xcl_fld, &cum_xcl_fld_id); cBYE(status); for ( int pos = 1; pos <= num_in_list; pos++ ) { int xcl_fld_id = -1; long long xcl_fld_cnt = -1 ; int new_fld_id = -1; long long new_fld_cnt = -1 ; status = get_fld_name(list_id, pos, "new_", &new_fld); cBYE(status); status = fld_meta(tbl, new_fld, "", &new_fld_id, false); cBYE(status); status = del_aux_fld_if_exists(tbl_id, list_id, pos, "xcl_"); cBYE(status); status = mk_name_new_fld(list, tbl, new_fld, "xcl_", xcl_fld); cBYE(status); status = f1f2opf3(tbl, new_fld, cum_xcl_fld, "&&!", xcl_fld); cBYE(status); status = count_ones(tbl, xcl_fld, &xcl_fld_cnt); cBYE(status); status = fld_meta(tbl, xcl_fld, "", &xcl_fld_id, false); cBYE(status); status = count_ones(tbl, new_fld, &new_fld_cnt); cBYE(status); // update meta data if ( new_fld_cnt == xcl_fld_cnt ) { status = set_fld_id_cnt(list_id, pos, "xcl_", new_fld_id, new_fld, new_fld_cnt); cBYE(status); status = set_fld_id_cnt(list_id, pos, "lmt_", new_fld_id, new_fld, new_fld_cnt); cBYE(status); status = del_fld(NULL, tbl_id, NULL, xcl_fld_id, true); cBYE(status); } else { status = set_fld_id_cnt(list_id, pos, "xcl_", xcl_fld_id, xcl_fld, xcl_fld_cnt); cBYE(status); status = set_fld_id_cnt(list_id, pos, "lmt_", xcl_fld_id, xcl_fld, xcl_fld_cnt); cBYE(status); } free_if_non_null(new_fld); } BYE: free_if_non_null(new_fld); return(status); }
static ERL_NIF_TERM cqueue_withdraw(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) { ERL_NIF_TERM result; cqueue_handle* handle = NULL; int list_id = get_list_id(enif_thread_self()); if (list_id == -1) return enif_make_badarg(env); if (enif_get_resource(env, argv[0], cqueue_RESOURCE, (void**)&handle) == 0) { return enif_make_badarg(env); } result = handle->queue->Withdraw(env, list_id); return result; }
static ERL_NIF_TERM cqueue_deposit(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) { cqueue_handle* handle = NULL; int list_id = get_list_id(enif_thread_self()); ErlNifPid pid; if (list_id == -1) return enif_make_badarg(env); if (enif_get_resource(env, argv[0], cqueue_RESOURCE, (void**)&handle) == 0) { return enif_make_badarg(env); } if (enif_get_local_pid(env, argv[1], &pid) == 0) { return enif_make_badarg(env); } handle->queue->Deposit(pid, list_id); return enif_make_atom(env, "ok"); }
// START FUNC DECL int add_to_xlist( char *list, int list_id, char *cum_fld, int cum_fld_id ) // STOP FUNC DECL { int status = 0; char command[1024]; char *zErrMsg = NULL; zero_string(command, 1024); if ( list_id <= 0 ) { int tbl_id, xfld_id; status = get_list_id(list, &list_id, &tbl_id, &xfld_id); cBYE(status); } sprintf(command, "insert into xlist (cum_fld, cum_fld_id, list, list_id) values ('%s', %d, '%s', %d)", cum_fld, cum_fld_id, list, list_id); int rc = sqlite3_exec(g_db, command, NULL, 0, &zErrMsg); if ( rc ) { handle_sqlite_error(); } BYE: // TODO P3: free_if_non_null(zErrMsg); return(status); }
int main(int argc, const char * argv[]) { FILE *fp = NULL; //For writing the lists. FILE *fp1 = NULL; //For writing the list ids. int i=0; int count=0; int index=0; int data=0; int list_number=0; int count_lists=0; long list_length=0; long list_id_length=0; int list_count=0; int result=0; int arr_list_id[MAX_LISTS]; char ch; node *head=NULL; node *head_mem[MAX_LISTS]; count_lists=0; if (argc == 1) { printf("\n Enter at least one argument \n"); exit(1); } //line_count = number_of_lines_count_in_file("list.out"); list_number = atoi(argv[2]); list_id_length = get_list_id(&fp1, arr_list_id, &list_count); if (strcmp(argv[1], "create") == 0) { printf ("\n creating a list \n"); if (!check_line_number(arr_list_id, list_number, list_id_length)) { data = atoi(argv[3]); head = create(list_number, data); write_to_file(head, &fp); write_list_number_file(&fp1, list_number, list_length); } } else if (strcmp(argv[1], "insert") == 0) { printf ("\n inserting into list \n"); // Check to see if the list_number is in arr_list_id if (!check_line_number(arr_list_id, list_number, list_count)) { while (1) { printf("\n List doesn't exist. \n Do you want to create it? \n Type 'y' for YES or 'n' for NO \n"); scanf("%c",&ch); if (ch == 'y' || ch == 'Y') { printf("\n Creating... \n"); data = atoi(argv[3]); head = create(list_number, data); write_list_number_file(&fp1, list_number, list_length); if (argc == 4) write_to_file(head, &fp); else { for (i = 4; i < argc; i++ ) { data = atoi(argv[i]); insert(&head, list_number, data); } write_to_file(head, &fp); } break; } else if (ch == 'n' || ch == 'Y') { exit(1); } else { printf ("\n Type 'y' for YES or 'n' for NO \n"); } } } else { count = load_in_memory(&fp, head_mem); if (count == 1) { index = 0; } else if (count == 0) { while (1) { printf("\n List doesn't exist. \n Do you want to create it? \n Type 'y' for YES or 'n' for NO \n"); scanf("%c",&ch); if (ch == 'y' || ch == 'Y') { printf("\n Creating... \n"); data = atoi(argv[3]); head = create(list_number, data); write_list_number_file(&fp1, list_number, list_length); if (argc == 4) write_to_file(head, &fp); else { for (i = 4; i < argc; i++ ) { data = atoi(argv[i]); insert(&head, list_number, data); } write_to_file(head, &fp); } break; } else if (ch == 'n' || ch == 'n') { exit(1); } else { printf ("\n Type 'y' for YES or 'n' for NO \n"); } } } for (i = 0; i < count; i++) { if (head_mem[i]->list_id == list_number) { index = i; break; } } for (i = 3; i < argc; i++) { head = head_mem[index]; data = atoi(argv[i]); insert(&head, list_number, data); head_mem[index] = head; } rewrite_to_file(head_mem, &fp, list_number, count); } } else if (strcmp(argv[1], "display") == 0) { printf ("\n displaying a list \n"); if (check_line_number(arr_list_id, list_number, list_count)) { head = read_from_file_and_construct_list(&fp, list_number, &list_length); display(head); } else { printf("\n List does not exist \n"); } } else if (strcmp(argv[1], "remove_node") == 0) { if (check_line_number(arr_list_id, list_number, list_count)) { printf ("\n Removing a node \n"); data = atoi(argv[3]); count = load_in_memory(&fp, head_mem); if (count == 1) { index = 0; } else { for (i = 0; i < count; i++) { if (head_mem[i]->list_id == list_number) { index = i; break; } } } if (check_node_present(head_mem[index], data)) { for (i = 3; i < argc; i++) { head = head_mem[index]; data = atoi(argv[i]); result = remove_node(&head, data); head_mem[index] = head; } if (head_mem[index] == NULL) { remove_from_listid_file(&fp1, list_number); } rewrite_to_file(head_mem, &fp, list_number, count); } else { printf ("\n Node not found \n"); } } else { printf("\n List does not exist\n"); } } else if (strcmp(argv[1], "remove_list") == 0) { printf ("\n Removing a list \n"); if (check_line_number(arr_list_id, list_number, list_count)) { count = load_in_memory(&fp, head_mem); if (count == 1) { index = 0; } else if (count == 0) { printf("file is empty"); exit(1); } else { for (i = 0; i < count; i++) { if (head_mem[i]->list_id == list_number) { index = i; break; } } } remove_list(head_mem[index]); remove_from_listid_file(&fp1, list_number); head_mem[index] = NULL; rewrite_to_file(head_mem, &fp, list_number, count); } else { printf("\n List does not exist\n"); } } else { printf ("\n Invalid option \n"); } return 0; }
// START FUNC DECL int ylc_check( ) // STOP FUNC DECL { int status = 0; char *mode = NULL; int num_lists = 0; char **lists = NULL; FLD_REC_TYPE fld_rec; int fld_id; FLD_REC_TYPE nn_fld_rec; int nn_fld_id; #define NUM_MODES 4 long long mode_cnt[NUM_MODES]; int mode_fld_id[NUM_MODES]; mode = malloc(16); /* Make sure that all flds referenced are Q fields */ status = fetch_rows(g_db, "list", "name", "", "order by id", (void **)&lists, &num_lists); cBYE(status); if ( num_lists == 0 ) { fprintf(stderr, "Nothing to check\n"); goto BYE; } for ( int i = 0; i < num_lists; i++ ) { int tbl_id, xlist_id, list_id, num_in_list; status = get_list_id(lists[i], &list_id, &tbl_id, &xlist_id); cBYE(status); char *list_name = lists[i]; status = get_num_in_list(list_id, &num_in_list); cBYE(status); if ( num_in_list == 0 ) { continue; } for ( int position = 1; position <= num_in_list; position++ ) { for ( int mode_idx = 0; mode_idx < NUM_MODES; mode_idx++ ) { switch ( mode_idx ) { case 0 : strcpy(mode, ""); break; case 1 : strcpy(mode, "new_"); break; case 2 : strcpy(mode, "xcl_"); break; case 3 : strcpy(mode, "lmt_"); break; default : go_BYE(-1); break; } char *fld = NULL; long long cnt; int fld_id; status = get_fld_name(list_id, position, mode, &fld); cBYE(status); if ( fld == NULL ) { go_BYE(-1); } status = get_fld_cnt(list_id, position, mode, &cnt); cBYE(status); mode_cnt[mode_idx] = cnt; status = get_fld_id(list_id, position, mode, &fld_id); cBYE(status); mode_fld_id[mode_idx] = fld_id; status = is_fld(NULL, tbl_id, fld, &fld_id, &fld_rec, &nn_fld_id, &nn_fld_rec); cBYE(status); if ( fld_id < 0 ) { fprintf(stderr, "MISSING! List %s, Pos = %d, Mode = %s, Fld [%s]\n", lists[i], position, mode, fld); continue; } if ( fld_rec.fldtype != B ) { go_BYE(-1); } free_if_non_null(fld); } for ( int i = 1; i < NUM_MODES; i++ ) { if ( ( mode_cnt[i] > 0 ) && ( mode_cnt[i-1] > 0 ) ) { if ( mode_cnt[i] == mode_cnt[i-1] ) { if ( mode_fld_id[i] != mode_fld_id[i-1] ) { fprintf(stderr, "Error for List %s Position %d Mode %d \n", list_name, position, i); fprintf(stderr, "cnt[%d] = %lld, cnt[%d] = %lld \n", i, mode_cnt[i], i-1, mode_cnt[i-1]); fprintf(stderr, "fld_id[%d] = %d, fld_id[%d] = %d \n", i, mode_fld_id[i], i-1, mode_fld_id[i-1]); } } else if ( mode_cnt[i] > mode_cnt[i-1] ) { fprintf(stderr, "Error for List %s Position %d Mode %d \n", list_name, position, i); fprintf(stderr, "cnt[%d] = %lld, cnt[%d] = %lld \n", i, mode_cnt[i], i-1, mode_cnt[i-1]); fprintf(stderr, "fld_id[%d] = %d, fld_id[%d] = %d \n", i, mode_fld_id[i], i-1, mode_fld_id[i-1]); go_BYE(-1); } } } } } fprintf(stderr, "All checks passed\n"); BYE: if ( num_lists > 0 ) { for ( int i = 0; i < num_lists; i++ ) { if ( lists != NULL ) { free_if_non_null(lists[i]); } } } free_if_non_null(lists); free_if_non_null(mode); return(status); }
int ylc_limit( char *list, char *str_limits ) { int status = 0; TBL_REC_TYPE tbl_rec; int tbl_id = -1, xlist_id = -1; bool b_is_list; int list_id = -1, num_in_list = -1; char **Y = NULL; int nY = 0; long long limit[MAX_NUM_IN_LIST]; char *fld = NULL, *xcl_fld = NULL; char lmt_fld[MAX_LEN_FLD_NAME+1]; /*-----------------------------------------------------------*/ zero_string(lmt_fld, (MAX_LEN_FLD_NAME+1)); status = is_list(list, &b_is_list); if ( b_is_list == false ) { fprintf(stderr, "List [%s] does not exist \n", list); go_BYE(-1); } status = get_list_id(list, &list_id, &tbl_id, &xlist_id); cBYE(status); if ( list_id < 0 ) { go_BYE(-1); } status = get_num_in_list(list_id, &num_in_list); cBYE(status); if ( num_in_list <= 0 ) { go_BYE(-1); } /*-----------------------------------------------------------*/ status = get_tbl_meta(tbl_id, &tbl_rec); cBYE(status); char *tbl = tbl_rec.name; /*-----------------------------------------------------------*/ status = break_str(str_limits, ":", &Y, &nY); cBYE(status); if ( nY != num_in_list ) { go_BYE(-1); } for ( int i = 0; i < num_in_list; i++ ) { char *endptr; limit[i] = strtoll(Y[i], &endptr, 10); if ( *endptr != '\0' ) { go_BYE(-1); } free_if_non_null(Y[i]); if ( ( limit[i] <= 0 ) && ( limit[i] != -1 ) ) { go_BYE(-1); } } free_if_non_null(Y); /*-----------------------------------------------------------*/ for ( int i = 0; i < num_in_list; i++ ) { int pos = i + 1; if ( limit[i] <= 0 ) { /* No limit */ status = del_aux_fld_if_exists(tbl_id, list_id, pos, "lmt_"); cBYE(status); } else { char str_limit_spec[64]; zero_string(str_limit_spec, 64); int lmt_fld_id = -1; long long lmt_fld_cnt = -1; status = get_fld_cnt(list_id, pos, "lmt_", &lmt_fld_cnt); cBYE(status); if ( lmt_fld_cnt <= limit[i] ) { /* Nothing to do */ continue; } // Delete current xcl field status = del_aux_fld_if_exists(tbl_id, list_id, pos, "lmt_"); #ifdef TODO if ( xcl_cnt < limit[i] ) { /* xcl field can work as lmt field */ // TODO: P2 We can do some optimization here } #endif status = get_fld_name(list_id, pos, "xcl_", &xcl_fld); cBYE(status); status = get_fld_name(list_id, pos, "", &fld ); cBYE(status); status = mk_name_new_fld( list, tbl, fld, "l_", lmt_fld); cBYE(status); status = dup_fld(tbl, xcl_fld, lmt_fld); cBYE(status); sprintf(str_limit_spec, "op=[zero_after_n]:limit=[%lld]", limit[i]); status = fop(tbl, lmt_fld, str_limit_spec); cBYE(status); status = fld_meta(tbl, lmt_fld, "", &lmt_fld_id, false); cBYE(status); status = count_ones(tbl, lmt_fld, &lmt_fld_cnt); cBYE(status); status = set_fld_id_cnt(list_id, pos, "lmt_", lmt_fld_id, lmt_fld, lmt_fld_cnt); cBYE(status); free_if_non_null(xcl_fld); free_if_non_null(fld); } } BYE: free_if_non_null(xcl_fld); free_if_non_null(fld); return(status); }