Example #1
0
int
main()
{
	for (;;) {
		restore_db();
	}

	return 0;
}
Example #2
0
File: sync.c Project: atikinn/db165
static
struct column restore_col(char *record, struct table *tbl) {
    struct col_record *rec = (struct col_record *) record;
    bool clustered = tbl->clustered == tbl->col_count;
    struct column col = { .name = strdup(rec->name), .table = tbl, .index = NULL,
			  .clustered = clustered, .status = ONDISK, .data = { 0, 0, NULL } };
    cs165_log(stderr, "clustered = %d\n", col.clustered);
    enum index_type idx_type = rec->idx_type - '0';
    switch(idx_type) {
	case IDX_INVALID: break;
	case SORTED:
	case BTREE:
	    col.index = malloc(sizeof(struct column_index));
	    col.index->type = idx_type;
	    col.index->index = NULL;
	    break;
    }
    return col;
}

static
struct table restore_tbl(char *record, size_t *col_count) {
    struct tbl_record *rec = (struct tbl_record *) record;
    struct table tbl = { .name = strdup(rec->name), .length = rec->length,
		         .clustered = rec->clustered, .col_count = 0 };
    tbl.col = malloc(sizeof(struct column) * rec->col_count);
    *col_count = rec->col_count;
    return tbl;
}

static
struct db *restore_db(char *record) {
    struct db_record *rec = (struct db_record *) record;
    struct db *db = malloc(sizeof *db);
    assert (db);
    db->table_count = 0;
    db->capacity = rec->capacity;
    db->tables = malloc(sizeof(struct table) * db->capacity);
    assert(db->tables);
    db->name = strdup(rec->name);
    return db;
}

static
struct db *restore(char *meta, size_t sz) {
    enum meta_state state = DB_RECORD;
    struct db *db;
    struct table *tbl;
    struct column *col;
    char *rec;
    size_t col_count = 0, prev = 0;
    char varbuf[VARNAME_SIZE];
    for (size_t j = 0; j < sz; j++) {
	if (meta[j] == '|') {
	    meta[j] = '\0';
	    rec = &meta[prev];
	    prev = j + 1;

	    switch(state) {
		case DB_RECORD:
		    db = restore_db(rec);
		    //map_insert(db->name, db, ENTITY);
		    state = TBL_RECORD;
		    break;
		case TBL_RECORD:
		    db->tables[db->table_count] = restore_tbl(rec, &col_count);
		    tbl = &db->tables[db->table_count++];
		    snprintf(varbuf, sizeof varbuf, "%s.%s", db->name, tbl->name);
		    map_insert(strdup(varbuf), tbl, ENTITY);
		    state = COL_RECORD;
		    break;
		case COL_RECORD:
		    tbl->col[tbl->col_count] = restore_col(rec, tbl);
		    col = &tbl->col[tbl->col_count++];
		    snprintf(varbuf, sizeof varbuf, "%s.%s.%s", db->name, tbl->name, col->name);
		    map_insert(strdup(varbuf), col, ENTITY);
		    if (col_count == tbl->col_count) state = TBL_RECORD;
		    break;
	    }
	}
    }

    assert(state == TBL_RECORD);
    assert(db->table_count <= db->capacity);
    return db;
}

static
struct db *restore_meta(void) {
    char buf[PATHLEN];
    char *path = vbsnprintf(buf, sizeof buf, "%s/%s.bin", DBPATH, METAFILE);

    char *meta;
    off_t sz = filemap(path, &meta, PROT_READ | PROT_WRITE);
    struct db *db = restore(meta, sz);
    fileunmap(meta, sz);
    return db;
}

//////////////////////////////////////////////////////////////////////////////

static
void restore_sindex(struct column *col) {
    char *tname = col->table->name;
    char buf[PATHLEN];
    char *path = vbsnprintf(buf, sizeof buf, "%s/%s.%s.sorted.bin", DBPATH, tname, col->name);
    char *data;
    off_t sz = filemap(path, &data, PROT_READ);
    col->index->index = sindex_alloc(col->data.sz);
    memcpy(col->index->index, data, sz);
    assert(is_sorted(col->index->index, col->data.sz) == true);
    fileunmap(data, sz);
}
Example #3
0
void save_ns_dbase(void)
{
    dbFILE *f;
    int i;
    NickInfo *ni;
    char **access;
    static time_t lastwarn = 0;

    if (!(f = open_db(s_NickServ, NickDBName, "w")))
	return;
    for (ni = firstnick(); ni; ni = nextnick()) {
	SAFE(write_int8(1, f));
	SAFE(write_buffer(ni->nick, f));
	SAFE(write_buffer(ni->pass, f));
	SAFE(write_string(ni->url, f));
	SAFE(write_string(ni->email, f));
	SAFE(write_string(ni->last_usermask, f));
	SAFE(write_string(ni->last_realname, f));
	SAFE(write_string(ni->last_quit, f));
	SAFE(write_int32(ni->time_registered, f));
	SAFE(write_int32(ni->last_seen, f));
	SAFE(write_int16(ni->status, f));
	if (ni->link) {
	    SAFE(write_string(ni->link->nick, f));
	    SAFE(write_int16(ni->linkcount, f));
	    SAFE(write_int16(ni->channelcount, f));
	} else {
	    SAFE(write_string(NULL, f));
	    SAFE(write_int16(ni->linkcount, f));
	    SAFE(write_int32(ni->flags, f));
	    SAFE(write_ptr(ni->suspendinfo, f));
	    if (ni->suspendinfo) {
		SAFE(write_buffer(ni->suspendinfo->who, f));
		SAFE(write_string(ni->suspendinfo->reason, f));
		SAFE(write_int32(ni->suspendinfo->suspended, f));
		SAFE(write_int32(ni->suspendinfo->expires, f));
	    }
	    SAFE(write_int16(ni->accesscount, f));
	    for (i=0, access=ni->access; i<ni->accesscount; i++, access++)
		SAFE(write_string(*access, f));
	    SAFE(write_int16(ni->channelcount, f));
	    SAFE(write_int16(ni->channelmax, f));
	    SAFE(write_int16(ni->language, f));
	}
    } /* for (ni) */
    {
	/* This is an UGLY HACK but it simplifies loading.  It will go away
	 * in the next file version */
	static char buf[256];
	SAFE(write_buffer(buf, f));
    }
    close_db(f);
    return;

  fail:
    restore_db(f);
    log_perror("Write error on %s", NickDBName);
    if (time(NULL) - lastwarn > WarningTimeout) {
	wallops(NULL, "Write error on %s: %s", NickDBName,
		strerror(errno));
	lastwarn = time(NULL);
    }
}