int cdb_read_header(cdb_t *cdb) { struct stat st; /* If the header has already been read from backing store do not read again */ if (cdb->synced == true) { return CDB_SUCCESS; } if (cdb_open(cdb) != 0) { return cdb_error(); } if (pread(cdb->fd, cdb->header, HEADER_SIZE, 0) != HEADER_SIZE) { return cdb_error(); } if (strncmp(cdb->header->token, CDB_TOKEN, sizeof(CDB_TOKEN)) != 0) { return CDB_EBADTOK; } if (strncmp(cdb->header->version, CDB_VERSION, sizeof(CDB_VERSION)) != 0) { return CDB_EBADVER; } cdb->synced = true; /* Calculate the number of records */ if (fstat(cdb->fd, &st) == 0) { cdb->header->num_records = (st.st_size - HEADER_SIZE) / RECORD_SIZE; } else { cdb->header->num_records = 0; } return CDB_SUCCESS; }
static PyObject* cdb_py_open_cdb(PyObject* self) { cdb_t *cdb = ((StorageObject*)self)->cdb; if (cdb_open(cdb) != CDB_SUCCESS) { PyErr_SetFromErrno(PyExc_IOError); } return self; }
int cdb_write_header(cdb_t *cdb) { if (cdb->synced) { return CDB_SUCCESS; } if (_cdb_is_writable(cdb) == false) { return CDB_ERDONLY; } cdb->synced = false; if (cdb_open(cdb) > 0) { return cdb_error(); } if (pwrite(cdb->fd, cdb->header, HEADER_SIZE, 0) != HEADER_SIZE) { return cdb_error(); } /* This really slows down moneypenny - we're ok with trusting the VFS * right now. */ /* OS X doesn't have fdatasync() */ #if 0 #ifdef HAVE_FDATASYNC if (fdatasync(cdb->fd) != 0) return cdb_error(); #else #ifdef HAVE_FSYNC if (fsync(cdb->fd) != 0) return cdb_error(); #endif #endif #endif cdb->synced = true; return CDB_SUCCESS; }
int main(int argc, char *argv[]) { int thread_num = 2; int record_num = 10000000; char *db_path = NULL; printf("Usage: %s db_path [record_num] [thread_num]\n", argv[0]); if (argc >= 2) db_path = argv[1]; else return -1; if (argc >= 3) record_num = atoi(argv[2]); if (argc >= 4) thread_num = atoi(argv[3]); record_num = record_num < 100? 100: record_num; thread_num = thread_num < 1? 1: thread_num; srand(time(NULL)); db = cdb_new(); cdb_option(db, record_num / 100, 0, 1024000); if (cdb_open(db, db_path, CDB_CREAT | CDB_TRUNC) < 0) { printf("DB Open err\n"); return -1; } optable = prob_table1; pthread_t threads[thread_num]; for(int i = 0; i < thread_num; i++) { pthread_create(&threads[i], NULL, test_thread, &record_num); } int clear_interval = 0; while(1) { CDBSTAT st; cdb_stat(db, &st); printf("rnum: %lu, rcnum: %lu, pnum: %lu, pcnum %lu, rlatcy: %u wlatcy: %u" " rh/m: %lu/%lu ph/m: %lu/%lu\n", st.rnum, st.rcnum, st.pnum, st.pcnum, st.rlatcy, st.wlatcy, st.rchit, st.rcmiss, st.pchit, st.pcmiss); if (++clear_interval % 20 == 0) cdb_stat(db, NULL); if (st.rnum > 0.7 * record_num) optable = prob_table2; if (st.rnum > 0.9 * record_num) optable = prob_table3; if (st.rnum < 0.8 * record_num) optable = prob_table2; if (st.rnum < 0.6 * record_num) optable = prob_table1; fflush(stdout); sleep(1); } return 0; }
static int Storage_init(PyObject *self, PyObject *args, PyObject *kwdict) { static char *kwlist[] = { "filename", "flags", "mode", "name", "desc", "max_records", "type", "units", "min_value", "max_value", NULL, }; char *filename; int flags = -1; int mode = -1; char *name = NULL; char *desc = ""; char *type = (char*)_string_from_cdb_type(CDB_DEFAULT_DATA_TYPE); char *units = CDB_DEFAULT_DATA_UNIT; uint64_t max_records = 0; double min_value = 0; double max_value = 0; if (!PyArg_ParseTupleAndKeywords(args, kwdict, "s|iiszlzzlli:new", kwlist, &filename, &flags, &mode, &name, &desc, &max_records, &type, &units, &min_value, &max_value)) { return -1; } PyObject *header = ((StorageObject*)self)->header; cdb_t *cdb = ((StorageObject*)self)->cdb; cdb->filename = filename; cdb->flags = flags; cdb->mode = mode; // Try the open if (cdb_open(cdb) != CDB_SUCCESS) { PyErr_SetFromErrno(PyExc_IOError); return -1; } // Try and read a header, if it doesn't exist, create one if (name == NULL) { if (cdb_read_header(cdb) != CDB_SUCCESS) { PyErr_SetFromErrno(PyExc_IOError); return -1; } } else { cdb_generate_header(cdb, name, desc, max_records, _cdb_type_from_string(type), units, min_value, max_value); if (cdb_write_header(cdb) != CDB_SUCCESS) { PyErr_SetFromErrno(PyExc_IOError); return -1; } } PyDict_SetItemString(header, "filename", PyString_FromString(filename)); PyDict_SetItemString(header, "name", PyString_FromString(cdb->header->name)); PyDict_SetItemString(header, "desc", PyString_FromString(cdb->header->desc)); PyDict_SetItemString(header, "type", PyString_FromString(_string_from_cdb_type(cdb->header->type))); PyDict_SetItemString(header, "units", PyString_FromString(cdb->header->units)); PyDict_SetItemString(header, "num_records", PyInt_FromLong(cdb->header->num_records)); PyDict_SetItemString(header, "max_records", PyInt_FromLong(cdb->header->max_records)); PyDict_SetItemString(header, "min_value", PyInt_FromLong(cdb->header->min_value)); PyDict_SetItemString(header, "max_value", PyInt_FromLong(cdb->header->max_value)); return 0; }