/* The child part of the multiprocess demonstration. * Attempts to add 10 entries of {process_x, x} where x is pid, then remove 9 of them. * Uses file locking; the add or remove command will not happen until the lockfile is created successfully. * * argv: argument list passed into this program. */ void run_worker(const char **argv) { char lockfile[250]; strcpy(lockfile, argv[2]); strcat(lockfile, ".lock"); int fd; int pid = getpid(); for (int i = 0; i < 10; i++) { while ((fd = open(lockfile, O_CREAT | O_EXCL)) < 3) { } Person to_add; to_add.id = pid; sprintf(to_add.name, "process_%d", to_add.id); db_add(argv[2], &to_add); close(fd); unlink(lockfile); } for (int i = 0; i < 9; i++) { while ((fd = open(lockfile, O_CREAT | O_EXCL)) < 3) { } char name[30]; sprintf(name, "process_%d", pid); db_remove(argv[2], name); close(fd); unlink(lockfile); } }
/* Button "Delete" from Register Page */ static void delete_clicked (void *data, Evas_Object *obj, const char *emission, const char *source) { Db_Entry *contact; contact = elm_list_item_data_get(App.selection); elm_list_item_del(App.selection); db_remove(App._db, contact); elm_pager_content_promote(App.info.pager, App.info.pgMain); }
/* Interactive function to remove a person. Prompts user for name, then invokes DB's remove function. * * filepath: the path of the db file. DB will open it. */ void cmd_remove(char *filepath) { char buffer[100]; printf("Enter the name to remove\n> "); fgets(buffer, 100, stdin); unsigned long end = strlen(buffer) - 1; if (buffer[end] == '\n') buffer[end] = '\0'; db_remove(filepath, buffer); }
/***************************************************************** Function Name: main Description: This is a main function Parameter: @argc: argument count @argv: argument vector Author: SA12226114(Xin Zhu), SA12226139(Dan Shu) Date: 2012-11-27 ******************************************************************/ int main(int argc, char *argv[]) { void *db = NULL; char cmdline[CMDLINE_LEN], cmd[CMD_LEN], key[KEY_LEN], value[VALUE_LEN]; /* display menu */ util_menu(); while(1) { /* format command line */ util_format(cmdline, cmd, key, value); if(strncmp(cmd, "open", sizeof("open")) == 0) /* create database */ { db_create(&db, key); } else if(strncmp(cmd, "set", sizeof("set")) == 0) /* store records */ { db_write(db, key, value); } else if(strncmp(cmd, "get", sizeof("get")) == 0) /* retrieve records */ { db_read(db, key); } else if(strncmp(cmd, "remove", sizeof("remove")) == 0) /* remove a record */ { db_remove(db, key); } else if(strncmp(cmd, "ls", sizeof("ls")) == 0) /* dump all records in database */ { db_queryallrecords(db); } else if(strncmp(cmd, "close", sizeof("close")) == 0) /* close the database */ { db_close(&db); } else if(strncmp(cmd, "delete", sizeof("delete")) == 0) /* delete the object */ { db_delete(&db); } else if(strncmp(cmd, "help", sizeof("help")) == 0) /* display menu info */ { util_menu(); } else if(strncmp(cmd, "quit", sizeof("quit")) == 0) /* quit system */ { return; } } return 0; }
int auth_db_cleanup_sub(DBKey key,void *data,va_list ap) { struct auth_node *node=(struct auth_node*)data; if(DIFF_TICK(gettick(),node->node_created)>30000) { ShowNotice("Character (aid: %d) not authed within 30 seconds of character select!\n", node->account_id); if (node->char_dat) aFree(node->char_dat); db_remove(auth_db, key); return 1; } return 0; }
static int db_nessdb_delete(struct nb_db *db, struct nb_key *key) { struct db_nessdb *t = db->priv; assert (key->size < UINT_MAX); struct slice nkey; nkey.len = key->size; nkey.data = key->data; db_remove(t->instance->db, &nkey); ++t->sent; return 0; }
JNIEXPORT jint JNICALL Java_org_nessdb_DB_remove(JNIEnv *jenv, jobject clazz, jlong ptr, jbyteArray jkey, jint jklen) { (void)clazz; char *key, *ktmp; struct slice sk; struct nessdb *_db = (struct nessdb *)ptr; if (!_db) { __ERROR("remove-->db is null, pls open first\n"); return 0; } if (jklen >= NESSDB_MAX_KEY_SIZE) { __ERROR("key length too long...%d", jklen); return (-1); } if (jklen == 0) return 0; memset(&sk, 0, sizeof(struct slice)); key = (char*)(*jenv)->GetByteArrayElements(jenv, jkey, 0); if (key == NULL) return 0; ktmp = malloc(jklen + 1); memset(ktmp, 0, jklen + 1); memcpy(ktmp, key, jklen); sk.data = ktmp; sk.len = jklen; db_remove(_db, &sk); /* release */ if (key) { (*jenv)->ReleaseByteArrayElements(jenv, jkey, (jbyte*)key, 0); free(ktmp); } return 1; }
/* The worker method for the multithreaded demonstration. Almost identical to the multiprocess worker above. * Each thread will attempt to acquire the lock, add a person with name "thread_x" and id x where x is the threadid assigned by me, then relinquish the lock. * It will do this 10 times, then attempt to remove 9 of those entries (again, reacquiring the mutex per operation, and relinquishing when done with that one operation.) * * arg: void pointer to a single argument passed in from pthread_create(). * * Returns: NULL in all cases. Return type only included for conformance with pthread_create(). */ void* run_threaded_worker(void *arg) { // Unpack the info struct thread_info ti = *(thread_info*)arg; // Each time, acquire the lock, create a person with name and id using threadid from ti, add it, then unlock. for (int i = 0; i < 10; i++) { pthread_mutex_lock(ti.lock); //- CRITICAL SECTION -// Person to_add; to_add.id = ti.threadid; sprintf(to_add.name, "thread_%d", ti.threadid); db_add(ti.path, &to_add); //- END CRITICAL SECTION -// pthread_mutex_unlock(ti.lock); } // Each time, acquire the lock, create the name to look for from threadid in ti, remove it, then unlock. for (int i = 0; i < 9; i++) { pthread_mutex_lock(ti.lock); //- CRITICAL SECTION -// char name[30]; sprintf(name, "thread_%d", ti.threadid); db_remove(ti.path, name); //- END CRITICAL SECTION -// pthread_mutex_unlock(ti.lock); } // Cause we gotta. return NULL; }
void _process_cmd(int fd, struct request *req) { char sent_buf[BUF_SIZE]; struct response *resp; memset(sent_buf, 0, BUF_SIZE); switch(req->cmd){ case CMD_PING:{ resp = response_new(0,OK_PONG); response_detch(resp,sent_buf); write(fd,sent_buf,strlen(sent_buf)); request_free_value(req); response_free(resp); break; } case CMD_SET:{ struct slice sk, sv; if(req->argc == 3) { sk.len = req->argv[1]->len; sk.data = req->argv[1]->data; sv.len = req->argv[2]->len; sv.data = req->argv[2]->data; db_add(_svr.db, &sk, &sv); resp = response_new(0,OK); response_detch(resp,sent_buf); write(fd,sent_buf,strlen(sent_buf)); request_free_value(req); response_free(resp); break; } goto __default; } case CMD_MSET:{ int i; int c = req->argc; for (i = 1; i < c; i += 2) { struct slice sk, sv; sk.len = req->argv[i]->len; sk.data = req->argv[i]->data; sv.len = req->argv[i+1]->len; sv.data = req->argv[i+1]->data; db_add(_svr.db, &sk, &sv); } resp = response_new(0, OK); response_detch(resp, sent_buf); write(fd,sent_buf, strlen(sent_buf)); request_free_value(req); response_free(resp); break; } case CMD_GET:{ int ret; struct slice sk; struct slice sv; if (req->argc == 2) { sk.len = req->argv[1]->len; sk.data = req->argv[1]->data; ret = db_get(_svr.db, &sk, &sv); if (ret == 1) { resp = response_new(1,OK_200); resp->argv[0] = sv.data; } else { resp = response_new(0,OK_404); resp->argv[0] = NULL; } response_detch(resp, sent_buf); write(fd,sent_buf,strlen(sent_buf)); request_free_value(req); response_free(resp); if (ret == 1) free(sv.data); break; } goto __default; } case CMD_MGET:{ int i; int ret; int c=req->argc; int sub_c=c-1; char **vals = calloc(c, sizeof(char*)); resp=response_new(sub_c, OK_200); for (i = 1; i < c; i++){ struct slice sk; struct slice sv; sk.len = req->argv[i]->len; sk.data = req->argv[i]->data; ret = db_get(_svr.db, &sk, &sv); if (ret == 1) vals[i-1] = sv.data; else vals[i-1] = NULL; resp->argv[i-1] = vals[i-1]; } response_detch(resp, sent_buf); write(fd, sent_buf, strlen(sent_buf)); request_free_value(req); response_free(resp); for (i = 0; i < sub_c; i++){ if (vals[i]) free(vals[i]); } free(vals); break; } case CMD_INFO:{ char *infos; infos = db_info(_svr.db); resp = response_new(1, OK_200); resp->argv[0] = infos; response_detch(resp, sent_buf); write(fd,sent_buf, strlen(sent_buf)); request_free_value(req); response_free(resp); break; } case CMD_DEL:{ int i; for (i = 1; i < req->argc; i++){ struct slice sk; sk.len = req->argv[i]->len; sk.data = req->argv[i]->data; db_remove(_svr.db, &sk); } resp = response_new(0, OK); response_detch(resp, sent_buf); write(fd, sent_buf, strlen(sent_buf)); request_free_value(req); response_free(resp); break; } case CMD_EXISTS:{ struct slice sk; sk.len = req->argv[1]->len; sk.data = req->argv[1]->data; int ret= db_exists(_svr.db, &sk); if(ret) write(fd,":1\r\n",4); else write(fd,":-1\r\n",5); } break; case CMD_SHUTDOWN: __ERROR("siloserver shutdown..."); db_close(_svr.db); exit(2); break; __default: default:{ resp = response_new(0, ERR); response_detch(resp, sent_buf); write(fd, sent_buf, strlen(sent_buf)); request_free_value(req); response_free(resp); break; } } }