Exemplo n.º 1
0
Arquivo: db.c Projeto: SanketDG/db_c
int main(int argc, char *argv[])
{

    int fd;
    person_record rec;

    /* fd = open_record("data1"); */

    if(argc > 1)
    {
        // insert data

        if(argc > 5 && !strcmp(argv[1], "insert"))
        {
            fd = update_record("data1", 1);
            rec.key = atoi(argv[2]);
            strcpy(rec.fname, argv[3]);
            strcpy(rec.lname, argv[4]);
            rec.age = atoi(argv[5]);

            insert_record(fd, &rec);
        }

        /* delete data */

        if(argc > 2 && !strcmp(argv[1], "delete"))
        {
            fd = update_record("data1", 0);
            delete_record(fd, atoi(argv[2]));
        }

        /* print data */

        if(argc > 2 && !strcmp(argv[1], "print"))
        {
            fd = update_record("data1", 1);

            get_record(fd, &rec, atoi(argv[2]));

            printf("key = %d\n", rec.key);
            printf("First = %s\n", rec.fname);
            printf("Last = %s\n", rec.lname);
            printf("Age = %d\n", rec.age);
        }
    }

    close_record(fd);
    return 0;
}
Exemplo n.º 2
0
static DBusMessage *update_xml_record(DBusConnection *conn,
				DBusMessage *msg,
				struct service_adapter *serv_adapter)
{
	struct record_data *user_record;
	sdp_record_t *sdp_record;
	const char *record;
	dbus_uint32_t handle;
	int len;

	if (dbus_message_get_args(msg, NULL,
				DBUS_TYPE_UINT32, &handle,
				DBUS_TYPE_STRING, &record,
				DBUS_TYPE_INVALID) == FALSE)
		return NULL;

	len = (record ? strlen(record) : 0);
	if (len == 0)
		return btd_error_invalid_args(msg);

	user_record = find_record(serv_adapter, handle,
				dbus_message_get_sender(msg));
	if (!user_record)
		return btd_error_not_available(msg);

	sdp_record = sdp_xml_parse_record(record, len);
	if (!sdp_record) {
		error("Parsing of XML service record failed");
		return btd_error_failed(msg,
					"Parsing of XML service record failed");
	}

	return update_record(conn, msg, serv_adapter, handle, sdp_record);
}
Exemplo n.º 3
0
void det_page_done(void *data, Evas_Object *obj, void *event_info)
{
	char te_data[255], *task_entry;
	_Task *tsk = (_Task *)data;
	
	sprintf(te_data, "%s", (const char *)elm_entry_entry_get(entry));
	task_entry = strtok(te_data, "<");
	if(strcmp(task_entry, tsk->text)!=0) {
		sprintf(tsk->text, "%s", task_entry);
		WRITE = 1;
	}

	sprintf(te_data, "%s", (const char *)elm_entry_entry_get(note_entry));
	task_entry = strtok(te_data, "<");
	if(strcmp(task_entry, "Note") == 0) task_entry = "";
	if(strcmp(task_entry, tsk->note)!=0) {
		sprintf(tsk->note, "%s", task_entry);
		WRITE = 1;
	}
	
	if(WRITE) {
		WRITE = 0;
		update_record (tsk->no);
		Elm_Object_Item *item;
		item = (Elm_Object_Item *)elm_genlist_selected_item_get(list);
		if(item) elm_genlist_item_update(task_list[tsk->no]);
		last_rec = -1;
		item = elm_genlist_first_item_get(list);
		if(item) elm_genlist_item_selected_set(item ,1);
	}
	cat_win_del(det_page, NULL, NULL);
}
Exemplo n.º 4
0
static void handle_accept_ack(consensus_component* comp,void* data){
    accept_ack* msg = data;
    // if currently the node is not the leader, then it should ignore all the
    // accept ack, because that can must be the msg from previous view
    SYS_LOG(comp,"Node %d Handle Accept Ack From Node %u.\n",
            comp->node_id,msg->node_id);
    if(comp->my_role!=LEADER){
        goto handle_accept_ack_exit;
    }
    // the request has reached quorum
    if(view_stamp_comp(&msg->msg_vs,comp->highest_committed_vs)<=0){
        goto handle_accept_ack_exit;
    }
    db_key_type record_no = vstol(&msg->msg_vs);
    request_record* record_data = NULL;
    size_t data_size;
    retrieve_record(comp->db_ptr,sizeof(record_no),&record_no,&data_size,(void**)&record_data);
    if(record_data==NULL){
        SYS_LOG(comp,"Received Ack To Non-Exist Record %lu.\n",
                record_no);
        goto handle_accept_ack_exit;
    }
    update_record(record_data,msg->node_id);
    // we do not care about whether the update is successful, otherwise this can
    // be treated as a message loss
    store_record(comp->db_ptr,sizeof(record_no),&record_no,REQ_RECORD_SIZE(record_data),record_data);
handle_accept_ack_exit:
    try_to_execute(comp);
    return;
};
Exemplo n.º 5
0
static gboolean on_quit(GtkWidget *widget, GdkEvent *event, Editor *e)
{
    update_record(e);
    library_save(e->library, e->path, 0);
    library_free(e->library);
    gtk_main_quit();
    return FALSE;
}
Exemplo n.º 6
0
Arquivo: sim.c Projeto: morphis/ofono
static void ril_sim_update_cyclic(struct ofono_sim *sim, int fileid,
					int length, const unsigned char *value,
					const unsigned char *path,
					unsigned int path_len,
					ofono_sim_write_cb_t cb, void *data)
{
	/* Only mode valid for cyclic files is PREVIOUS */
	update_record(sim, fileid, GRIL_REC_ACCESS_MODE_PREVIOUS, 0,
			length, value, path, path_len, cb, data);
}
Exemplo n.º 7
0
Arquivo: sim.c Projeto: morphis/ofono
static void ril_sim_update_record(struct ofono_sim *sim, int fileid,
					int record, int length,
					const unsigned char *value,
					const unsigned char *path,
					unsigned int path_len,
					ofono_sim_write_cb_t cb, void *data)
{
	update_record(sim, fileid, GRIL_REC_ACCESS_MODE_ABSOLUTE, record,
			length, value, path, path_len, cb, data);
}
Exemplo n.º 8
0
Arquivo: sim.c Projeto: endocode/ofono
static void ril_sim_update_record(struct ofono_sim *sim, int fileid,
					int record, int length,
					const unsigned char *value,
					const unsigned char *path,
					unsigned int path_len,
					ofono_sim_write_cb_t cb, void *data)
{
	DBG("");

	update_record(sim, fileid, 4, record, length, value,
			path, path_len, cb, data);
}
Exemplo n.º 9
0
void save_button_clicked(void *data, Evas_Object *obj, void *event_info)
{
	char te_data[255], *task_entry;
	
	//get task no
	Elm_Object_Item *item = (Elm_Object_Item *)elm_genlist_selected_item_get(list);
	if (item) {
		_Task *tsk = (_Task *)elm_object_item_data_get(item);
		
		//get data from entry
		sprintf(te_data, "%s", (const char *)elm_entry_entry_get(tk));
		task_entry = strtok(te_data, "<");
		sprintf(tsk->text, "%s", task_entry);
		
		//save data to database
		update_record(tsk->no);
		elm_genlist_item_update(task_list[tsk->no]);
	}
	else { //add a new record with this data
		time_t curtime;
		struct tm *loctime;
		char dt[6], te_data[255], *task_entry;
		int i = total_tasks;

		total_tasks ++;
		
		//get the time
		curtime = time (NULL);
		loctime = localtime (&curtime);
		strftime(dt, 6, "%d-%m", loctime);

		Task[i].no = i;
		Task[i].cb = 0;
		if (dummy_pr) Task[i].pr = dummy_pr;
		else Task[i].pr = 1;
		//get entry data
		sprintf(te_data, "%s", (const char *)elm_entry_entry_get(tk));
		task_entry = strtok(te_data, "<");
		if (strcmp(task_entry, "") !=0) strcpy(Task[i].text, task_entry);
		else strcpy(Task[i].text, "Task");
		//set current date
		if (strcmp(dummy_date, "") != 0) strcpy(Task[i].date, dummy_date);
		else strcpy(Task[i].date, dt);
		if(strcmp(sel_category, " All Tasks ")==0) strcpy(Task[i].cat, "Personal");
		else strcpy(Task[i].cat, sel_category);
		task_list[i] = elm_genlist_item_append(list, &itc1, &Task[i], NULL, ELM_GENLIST_ITEM_NONE,
							 	 	NULL, NULL);
		last_rec = -1;
		//insert record
		insert_record(i);
		elm_genlist_item_selected_set(task_list[i], 1);
	}
}
Exemplo n.º 10
0
void task_cb_changed(void *data, Evas_Object *obj, void *event_info)
{
	_Task *tsk = (_Task *)data;
	
	if(tsk->cb) tsk->cb = 0;
	else tsk->cb =1;

	//printf("no %d key %d\n", tsk->no, tsk->key);
	//TODO - strike through the task
	//remove this task from the list
	elm_object_item_del(task_list[tsk->no]);
	//update the db
	update_record (tsk->no);
	last_rec = -1;
}
Exemplo n.º 11
0
static void on_prev(GtkButton *button, Editor *e)
{
    update_record(e);
    if (e->record > 0)
    {
        e->record--;
        update_view(e);
        return;
    }

    e->shelf--;
    if (e->shelf == -1)
        e->shelf = library_shelves_count(e->library) - 1;
    e->record = current_shelf(e)->count - 1;
    update_view(e);
}
Exemplo n.º 12
0
static void on_next(GtkButton *button, Editor *e)
{
    Shelf *s = current_shelf(e);
    update_record(e);
    if (e->record < s->count - 1)
    {
        e->record++;
        update_view(e);
        return;
    }

    e->record = 0;
    e->shelf++;
    if (e->shelf >= library_shelves_count(e->library))
        e->shelf = 0;
    update_view(e);
}
Exemplo n.º 13
0
GuiPlayer::GuiPlayer( VlcInstance &inst ) : QWidget( NULL ), inst2( inst ), player( inst ), ui( new Ui_player ){
	ui->setupUi( this );
	
	libvlc_event_attach(
			libvlc_media_player_event_manager( player )
		,	libvlc_MediaPlayerTimeChanged
		,	&event_time_changed
		,	this
		);
	
	render = NULL;
	set_render( new Render( (QWidget*)NULL ) );
	
	connect( ui->btn_play, SIGNAL( clicked() ), &player, SLOT( play() ) );
	connect( ui->btn_pause, SIGNAL( clicked() ), &player, SLOT( pause() ) );
	connect( ui->btn_stop, SIGNAL( clicked() ), &player, SLOT( set_stop() ) );
	connect( ui->btn_next, SIGNAL( clicked() ), &player, SLOT( next_chapter() ) );
	connect( ui->btn_prev, SIGNAL( clicked() ), &player, SLOT( previous_chapter() ) );
	connect( ui->btn_record, SIGNAL( clicked() ), this, SLOT( update_record() ) );
	connect( ui->sld_time, SIGNAL( valueChanged(int) ), this, SLOT( set_time(int) ) );
	
	setAcceptDrops( true );
}
Exemplo n.º 14
0
 void task_select(void *data, Evas_Object *obj, void *event_info)
{
	char tystr[2];
	char te_data[255], *task_entry;
	Elm_Object_Item *item;
	_Task *tsk=NULL;

	//for entries, get entry data and check agianst previous task text, date
	//if changed - write
	if (last_rec != -1) { //cater for initial null data
		sprintf(te_data, "%s", (const char *)elm_entry_entry_get(tk));
		task_entry = strtok(te_data, "<");
		tsk = (_Task *)elm_object_item_data_get(task_list[last_rec]);
		if (strcmp(task_entry, tsk->text) != 0) {
			sprintf(tsk->text, "%s", task_entry);
			WRITE = 1;
		}
	}
	
	if (WRITE == 1) {
		elm_genlist_item_update(task_list[last_rec]);
		update_record(tsk->no);
		WRITE = 0;
	}
	
	Evas_Object *li = data;
	item = (Elm_Object_Item *)elm_genlist_selected_item_get(li);
	tsk = (_Task *)elm_object_item_data_get(item);
	last_rec = tsk->no;
	elm_object_text_set(cat_bt, tsk->cat);
	sprintf(tystr, "%d", tsk->pr);
	elm_object_text_set(pr_bt, tystr);
	elm_object_text_set(date_bt, tsk->date);
	elm_entry_entry_set(tk, tsk->text);
	if (strcmp(tsk->text, "Task") == 0) elm_entry_select_all(tk);
}
Exemplo n.º 15
0
int 
run_test(const char *filename)
{
	MI_INFO        *file;
	int		i         , j, error, deleted, rec_length, uniques = 0;
	ha_rows		found , row_count;
	my_off_t	pos;
	char		record    [MAX_REC_LENGTH], key[MAX_REC_LENGTH], read_record[MAX_REC_LENGTH];
	MI_UNIQUEDEF	uniquedef;
	MI_CREATE_INFO	create_info;

	bzero((char *)recinfo, sizeof(recinfo));

	/* First define 2 columns */
	recinfo[0].type = FIELD_NORMAL;
	recinfo[0].length = 1;	/* For NULL bits */
	recinfo[1].type = key_field;
	recinfo[1].length = (key_field == FIELD_BLOB ? 4 + mi_portable_sizeof_char_ptr :
			     key_length);
	if (key_field == FIELD_VARCHAR)
		recinfo[1].length += 2;
	recinfo[2].type = extra_field;
	recinfo[2].length = (extra_field == FIELD_BLOB ? 4 + mi_portable_sizeof_char_ptr : 24);
	if (extra_field == FIELD_VARCHAR)
		recinfo[2].length += 2;
	if (opt_unique) {
		recinfo[3].type = FIELD_CHECK;
		recinfo[3].length = MI_UNIQUE_HASH_LENGTH;
	}
	rec_length = recinfo[0].length + recinfo[1].length + recinfo[2].length +
		recinfo[3].length;


	/* Define a key over the first column */
	keyinfo[0].seg = keyseg;
	keyinfo[0].keysegs = 1;
	keyinfo[0].seg[0].type = key_type;
	keyinfo[0].seg[0].flag = pack_seg;
	keyinfo[0].seg[0].start = 1;
	keyinfo[0].seg[0].length = key_length;
	keyinfo[0].seg[0].null_bit = null_fields ? 2 : 0;
	keyinfo[0].seg[0].null_pos = 0;
	keyinfo[0].seg[0].language = MY_CHARSET_CURRENT;
	if (pack_seg & HA_BLOB_PART) {
		keyinfo[0].seg[0].bit_start = 4;	/* Length of blob length */
	}
	keyinfo[0].flag = (uint8) (pack_keys | unique_key);

	bzero((byte *) flags, sizeof(flags));
	if (opt_unique) {
		uint		start;
		uniques = 1;
		bzero((char *)&uniquedef, sizeof(uniquedef));
		bzero((char *)uniqueseg, sizeof(uniqueseg));
		uniquedef.seg = uniqueseg;
		uniquedef.keysegs = 2;

		/* Make a unique over all columns (except first NULL fields) */
		for (i = 0, start = 1; i < 2; i++) {
			uniqueseg[i].start = start;
			start += recinfo[i + 1].length;
			uniqueseg[i].length = recinfo[i + 1].length;
			uniqueseg[i].language = MY_CHARSET_CURRENT;
		}
		uniqueseg[0].type = key_type;
		uniqueseg[0].null_bit = null_fields ? 2 : 0;
		uniqueseg[1].type = HA_KEYTYPE_TEXT;
		if (extra_field == FIELD_BLOB) {
			uniqueseg[1].length = 0;	/* The whole blob */
			uniqueseg[1].bit_start = 4;	/* long blob */
			uniqueseg[1].flag |= HA_BLOB_PART;
		} else if (extra_field == FIELD_VARCHAR)
			uniqueseg[1].flag |= HA_VAR_LENGTH;
	} else
		uniques = 0;

	if (!silent)
		printf("- Creating isam-file\n");
	bzero((char *)&create_info, sizeof(create_info));
	create_info.max_rows = (ulong) (rec_pointer_size ?
					(1L << (rec_pointer_size * 8)) / 40 :
					0);
	if (mi_create(filename, 1, keyinfo, 3 + opt_unique, recinfo,
		      uniques, &uniquedef, &create_info,
		      create_flag))
		goto err;
	if (!(file = mi_open(filename, 2, HA_OPEN_ABORT_IF_LOCKED)))
		goto err;
	if (!silent)
		printf("- Writing key:s\n");

	my_errno = 0;
	row_count = deleted = 0;
	for (i = 49; i >= 1; i -= 2) {
		if (insert_count-- == 0) {
			VOID(mi_close(file));
			exit(0);
		}
		j = i % 25 + 1;
		create_record(record, j);
		error = mi_write(file, record);
		if (!error)
			row_count++;
		flags[j] = 1;
		if (verbose || error)
			printf("J= %2d  mi_write: %d  errno: %d\n", j, error, my_errno);
	}

	/* Insert 2 rows with null values */
	if (null_fields) {
		create_record(record, 0);
		error = mi_write(file, record);
		if (!error)
			row_count++;
		if (verbose || error)
			printf("J= NULL  mi_write: %d  errno: %d\n", error, my_errno);
		error = mi_write(file, record);
		if (!error)
			row_count++;
		if (verbose || error)
			printf("J= NULL  mi_write: %d  errno: %d\n", error, my_errno);
		flags[0] = 2;
	}
	if (!skip_update) {
		if (opt_unique) {
			if (!silent)
				printf("- Checking unique constraint\n");
			create_record(record, j);
			if (!mi_write(file, record) || my_errno != HA_ERR_FOUND_DUPP_UNIQUE) {
				printf("unique check failed\n");
			}
		}
		if (!silent)
			printf("- Updating rows\n");

		/* Update first last row to force extend of file */
		if (mi_rsame(file, read_record, -1)) {
			printf("Can't find last row with mi_rsame\n");
		} else {
			memcpy(record, read_record, rec_length);
			update_record(record);
			if (mi_update(file, read_record, record)) {
				printf("Can't update last row: %.*s\n",
				 keyinfo[0].seg[0].length, read_record + 1);
			}
		}

		/* Read through all rows and update them */
		pos = (my_off_t) 0;
		found = 0;
		while ((error = mi_rrnd(file, read_record, pos)) == 0) {
			if (update_count-- == 0) {
				VOID(mi_close(file));
				exit(0);
			}
			memcpy(record, read_record, rec_length);
			update_record(record);
			if (mi_update(file, read_record, record)) {
				printf("Can't update row: %.*s, error: %d\n",
				       keyinfo[0].seg[0].length, record + 1, my_errno);
			}
			found++;
			pos = HA_OFFSET_ERROR;
		}
		if (found != row_count)
			printf("Found %ld of %ld rows\n", found, row_count);
	}
	if (!silent)
		printf("- Reopening file\n");
	if (mi_close(file))
		goto err;
	if (!(file = mi_open(filename, 2, HA_OPEN_ABORT_IF_LOCKED)))
		goto err;
	if (!skip_update) {
		if (!silent)
			printf("- Removing keys\n");

		for (i = 0; i <= 10; i++) {
			/* testing */
			if (remove_count-- == 0) {
				VOID(mi_close(file));
				exit(0);
			}
			j = i * 2;
			if (!flags[j])
				continue;
			create_key(key, j);
			my_errno = 0;
			if ((error = mi_rkey(file, read_record, 0, key, 0, HA_READ_KEY_EXACT))) {
				if (verbose || (flags[j] >= 1 ||
				(error && my_errno != HA_ERR_KEY_NOT_FOUND)))
					printf("key: '%.*s'  mi_rkey:  %3d  errno: %3d\n",
					       (int)key_length, key + test(null_fields), error, my_errno);
			} else {
				error = mi_delete(file, read_record);
				if (verbose || error)
					printf("key: '%.*s'  mi_delete: %3d  errno: %3d\n",
					       (int)key_length, key + test(null_fields), error, my_errno);
				if (!error) {
					deleted++;
					flags[j]--;
				}
			}
		}
	}
	if (!silent)
		printf("- Reading rows with key\n");
	for (i = 0; i <= 25; i++) {
		create_key(key, i);
		my_errno = 0;
		error = mi_rkey(file, read_record, 0, key, 0, HA_READ_KEY_EXACT);
		if (verbose ||
		    (error == 0 && flags[i] == 0 && unique_key) ||
		    (error && (flags[i] != 0 || my_errno != HA_ERR_KEY_NOT_FOUND))) {
			printf("key: '%.*s'  mi_rkey: %3d  errno: %3d  record: %s\n",
			       (int)key_length, key + test(null_fields), error, my_errno, record + 1);
		}
	}

	if (!silent)
		printf("- Reading rows with position\n");
	for (i = 1, found = 0; i <= 30; i++) {
		my_errno = 0;
		if ((error = mi_rrnd(file, read_record, i == 1 ? 0L : HA_OFFSET_ERROR)) == -1) {
			if (found != row_count - deleted)
				printf("Found only %ld of %ld rows\n", found, row_count - deleted);
			break;
		}
		if (!error)
			found++;
		if (verbose || (error != 0 && error != HA_ERR_RECORD_DELETED &&
				error != HA_ERR_END_OF_FILE)) {
			printf("pos: %2d  mi_rrnd: %3d  errno: %3d  record: %s\n",
			       i - 1, error, my_errno, read_record + 1);
		}
	}
	if (mi_close(file))
		goto err;
	my_end(MY_CHECK_ERROR);

	return (0);
err:
	printf("got error: %3d when using myisam-database\n", my_errno);
	return 1;		/* skipp warning */
}
Exemplo n.º 16
0
int rsm_op(struct consensus_component_t* comp, void* data, size_t data_size){
    int ret = 1;
    pthread_mutex_lock(&comp->mutex);
    view_stamp next = get_next_view_stamp(comp);

    /* record the data persistently */
    db_key_type record_no = vstol(next);
    request_record* record_data = (request_record*)malloc(data_size + sizeof(request_record));
    gettimeofday(&record_data->created_time, NULL);
    record_data->bit_map = (1<<comp->node_id);
    record_data->data_size = data_size;
    memcpy(record_data->data, data, data_size);
    if(store_record(comp->db_ptr, sizeof(record_no), &record_no, REQ_RECORD_SIZE(record_data), record_data))
    {
        goto handle_submit_req_exit;
    }
    ret = 0;

    comp->highest_seen_vs.req_id = comp->highest_seen_vs.req_id + 1;
    uint64_t offset = shared_memory.tail;
    log_entry* new_entry = (log_entry*)((char*)shared_memory.shm[comp->node_id] + shared_memory.tail);
    new_entry->data_size = data_size;
    shared_memory.tail = shared_memory.tail + log_entry_len(new_entry);
    pthread_mutex_unlock(&comp->mutex);
    new_entry->node_id = comp->node_id;
    new_entry->req_canbe_exed.view_id = comp->committed.view_id;
    new_entry->req_canbe_exed.req_id = comp->committed.req_id;
    new_entry->msg_vs = next;
    memcpy(new_entry->data, data, data_size);

    if(comp->group_size > 1){
        for (int i = 0; i < comp->group_size; i++) {
            //TODO RDMA write

            if (i == comp->node_id)
                continue;
            memcpy((void*)((char*)shared_memory.shm[i] + offset), new_entry, log_entry_len(new_entry));
        }

recheck:
        for (int i = 0; i < MAX_SERVER_COUNT; i++)
        {
            if (i == comp->node_id)
                continue;
            if (new_entry->ack[i].msg_vs.view_id == next.view_id && new_entry->ack[i].msg_vs.req_id == next.req_id)
            {
                update_record(record_data, new_entry->ack[i].node_id);
                store_record(comp->db_ptr, sizeof(record_no), &record_no, REQ_RECORD_SIZE(record_data), record_data);
            }
        }
        if (reached_quorum(record_data, comp->group_size))
        {
            goto handle_submit_req_exit;
        }else{
            goto recheck;
        }
    }else{
        CON_LOG(comp, "group_size <= 1, execute by myself.\n");
    }
handle_submit_req_exit: 
    if(record_data != NULL){
        free(record_data);
    }
    //TODO: do we need the lock here?
    while (new_entry->msg_vs.req_id > comp->committed.req_id + 1);
    comp->committed.req_id = comp->committed.req_id + 1;
    return ret;
}
Exemplo n.º 17
0
/**
 * @brief Recovery system based on log file
 */
void WriteBehindFrontendLogger::DoRecovery() {
  // Set log file size
  log_file_size = GetLogFileSize(log_file_fd);

  // Go over the log size if needed
  if (log_file_size > 0) {
    bool reached_end_of_file = false;

    // check whether first item is LOGRECORD_TYPE_TRANSACTION_COMMIT
    // if not, no need to do recovery.
    // if yes, need to replay all log records before we hit
    // LOGRECORD_TYPE_TRANSACTION_DONE
    bool need_recovery = NeedRecovery();
    if (need_recovery == true) {
      TransactionRecord dummy_transaction_record(LOGRECORD_TYPE_INVALID);
      cid_t current_commit_id = INVALID_CID;

      // Go over each log record in the log file
      while (reached_end_of_file == false) {
        // Read the first byte to identify log record type
        // If that is not possible, then wrap up recovery
        LogRecordType log_type = GetNextLogRecordType(log_file, log_file_size);

        switch (log_type) {
          case LOGRECORD_TYPE_TRANSACTION_DONE:
          case LOGRECORD_TYPE_TRANSACTION_COMMIT: {
            // read but do nothing
            ReadTransactionRecordHeader(dummy_transaction_record, log_file,
                                        log_file_size);
          } break;

          case LOGRECORD_TYPE_WBL_TUPLE_INSERT: {
            TupleRecord insert_record(LOGRECORD_TYPE_WBL_TUPLE_INSERT);
            ReadTupleRecordHeader(insert_record, log_file, log_file_size);

            auto insert_location = insert_record.GetInsertLocation();
            auto info = SetInsertCommitMark(insert_location);
            current_commit_id = info.first;
          } break;

          case LOGRECORD_TYPE_WBL_TUPLE_DELETE: {
            TupleRecord delete_record(LOGRECORD_TYPE_WBL_TUPLE_DELETE);
            ReadTupleRecordHeader(delete_record, log_file, log_file_size);

            auto delete_location = delete_record.GetDeleteLocation();
            auto info = SetDeleteCommitMark(delete_location);
            current_commit_id = info.first;
          } break;

          case LOGRECORD_TYPE_WBL_TUPLE_UPDATE: {
            TupleRecord update_record(LOGRECORD_TYPE_WBL_TUPLE_UPDATE);
            ReadTupleRecordHeader(update_record, log_file, log_file_size);

            auto delete_location = update_record.GetDeleteLocation();
            SetDeleteCommitMark(delete_location);

            auto insert_location = update_record.GetInsertLocation();
            auto info = SetInsertCommitMark(insert_location);
            current_commit_id = info.first;
          } break;

          default:
            reached_end_of_file = true;
            break;
        }
      }

      // Update latest commit id
      if (latest_commit_id < current_commit_id) {
        latest_commit_id = current_commit_id;
      }

      // write out a trasaction done log record to file
      // to avoid redo next time during recovery
      WriteTransactionLogRecord(
          TransactionRecord(LOGRECORD_TYPE_TRANSACTION_DONE));
    }

    // After finishing recovery, set the next oid with maximum oid
    // observed during the recovery
    auto &manager = catalog::Manager::GetInstance();
    manager.SetNextOid(max_oid);
  }
}
Exemplo n.º 18
0
static int run_test(const char *filename)
{
  MARIA_HA *file;
  int i,j= 0,error,deleted,rec_length,uniques=0;
  uint offset_to_key;
  ha_rows found,row_count;
  uchar record[MAX_REC_LENGTH],key[MAX_REC_LENGTH],read_record[MAX_REC_LENGTH];
  MARIA_UNIQUEDEF uniquedef;
  MARIA_CREATE_INFO create_info;

  if (die_in_middle_of_transaction)
    null_fields= 1;

  bzero((char*) recinfo,sizeof(recinfo));
  bzero((char*) &create_info,sizeof(create_info));

  /* First define 2 columns */
  create_info.null_bytes= 1;
  recinfo[0].type= key_field;
  recinfo[0].length= (key_field == FIELD_BLOB ? 4+portable_sizeof_char_ptr :
		      key_length);
  if (key_field == FIELD_VARCHAR)
    recinfo[0].length+= HA_VARCHAR_PACKLENGTH(key_length);
  recinfo[1].type=extra_field;
  recinfo[1].length= (extra_field == FIELD_BLOB ? 4 + portable_sizeof_char_ptr : 24);
  if (extra_field == FIELD_VARCHAR)
    recinfo[1].length+= HA_VARCHAR_PACKLENGTH(recinfo[1].length);
  recinfo[1].null_bit= null_fields ? 2 : 0;

  if (opt_unique)
  {
    recinfo[2].type=FIELD_CHECK;
    recinfo[2].length=MARIA_UNIQUE_HASH_LENGTH;
  }
  rec_length= recinfo[0].length + recinfo[1].length + recinfo[2].length +
    create_info.null_bytes;

  if (key_type == HA_KEYTYPE_VARTEXT1 &&
      key_length > 255)
    key_type= HA_KEYTYPE_VARTEXT2;

  /* Define a key over the first column */
  keyinfo[0].seg=keyseg;
  keyinfo[0].keysegs=1;
  keyinfo[0].block_length= 0;                   /* Default block length */
  keyinfo[0].key_alg=HA_KEY_ALG_BTREE;
  keyinfo[0].seg[0].type= key_type;
  keyinfo[0].seg[0].flag= pack_seg;
  keyinfo[0].seg[0].start=1;
  keyinfo[0].seg[0].length=key_length;
  keyinfo[0].seg[0].null_bit= null_fields ? 2 : 0;
  keyinfo[0].seg[0].null_pos=0;
  keyinfo[0].seg[0].language= default_charset_info->number;
  if (pack_seg & HA_BLOB_PART)
  {
    keyinfo[0].seg[0].bit_start=4;		/* Length of blob length */
  }
  keyinfo[0].flag = (uint8) (pack_keys | unique_key);

  bzero((uchar*) flags,sizeof(flags));
  if (opt_unique)
  {
    uint start;
    uniques=1;
    bzero((char*) &uniquedef,sizeof(uniquedef));
    bzero((char*) uniqueseg,sizeof(uniqueseg));
    uniquedef.seg=uniqueseg;
    uniquedef.keysegs=2;

    /* Make a unique over all columns (except first NULL fields) */
    for (i=0, start=1 ; i < 2 ; i++)
    {
      uniqueseg[i].start=start;
      start+=recinfo[i].length;
      uniqueseg[i].length=recinfo[i].length;
      uniqueseg[i].language= default_charset_info->number;
    }
    uniqueseg[0].type= key_type;
    uniqueseg[0].null_bit= null_fields ? 2 : 0;
    uniqueseg[1].type= HA_KEYTYPE_TEXT;
    if (extra_field == FIELD_BLOB)
    {
      uniqueseg[1].length=0;			/* The whole blob */
      uniqueseg[1].bit_start=4;			/* long blob */
      uniqueseg[1].flag|= HA_BLOB_PART;
    }
    else if (extra_field == FIELD_VARCHAR)
    {
      uniqueseg[1].flag|= HA_VAR_LENGTH_PART;
      uniqueseg[1].type= (HA_VARCHAR_PACKLENGTH(recinfo[1].length-1) == 1 ?
                          HA_KEYTYPE_VARTEXT1 : HA_KEYTYPE_VARTEXT2);
    }
  }
  else
    uniques=0;

  offset_to_key= MY_TEST(null_fields);
  if (key_field == FIELD_BLOB || key_field == FIELD_VARCHAR)
    offset_to_key+= 2;

  if (!silent)
    printf("- Creating maria file\n");
  create_info.max_rows=(ulong) (rec_pointer_size ?
				(1L << (rec_pointer_size*8))/40 :
				0);
  create_info.transactional= transactional;
  if (maria_create(filename, record_type, 1, keyinfo,2+opt_unique,recinfo,
		uniques, &uniquedef, &create_info,
		create_flag))
    goto err;
  if (!(file=maria_open(filename,2,HA_OPEN_ABORT_IF_LOCKED)))
    goto err;
  if (!silent)
    printf("- Writing key:s\n");

  if (maria_begin(file))
    goto err;
  if (opt_versioning)
    maria_versioning(file, 1);
  my_errno=0;
  row_count=deleted=0;
  for (i=49 ; i>=1 ; i-=2 )
  {
    if (insert_count-- == 0)
    {
      if (testflag)
        break;
      maria_close(file);
      exit(0);
    }
    j=i%25 +1;
    create_record(record,j);
    error=maria_write(file,record);
    if (!error)
      row_count++;
    flags[j]=1;
    if (verbose || error)
      printf("J= %2d  maria_write: %d  errno: %d\n", j,error,my_errno);
  }

  if (maria_commit(file) || maria_begin(file))
    goto err;

  if (checkpoint == 1 && ma_checkpoint_execute(CHECKPOINT_MEDIUM, FALSE))
    goto err;

  if (testflag == 1)
    goto end;

  /* Insert 2 rows with null values */
  if (null_fields)
  {
    create_record(record,0);
    error=maria_write(file,record);
    if (!error)
      row_count++;
    if (verbose || error)
      printf("J= NULL  maria_write: %d  errno: %d\n", error,my_errno);
    error=maria_write(file,record);
    if (!error)
      row_count++;
    if (verbose || error)
      printf("J= NULL  maria_write: %d  errno: %d\n", error,my_errno);
    flags[0]=2;
  }

  if (checkpoint == 2 && ma_checkpoint_execute(CHECKPOINT_MEDIUM, FALSE))
    goto err;

  if (testflag == 2)
  {
    printf("Terminating after inserts\n");
    goto end;
  }

  if (maria_commit(file) || maria_begin(file))
    goto err;

  if (!skip_update)
  {
    if (opt_unique)
    {
      if (!silent)
	printf("- Checking unique constraint\n");
      create_record(record,j);                  /* Check last created row */
      if (!maria_write(file,record) || my_errno != HA_ERR_FOUND_DUPP_UNIQUE)
      {
	printf("unique check failed\n");
      }
    }
    if (!silent)
      printf("- Updating rows\n");

    /* Update first last row to force extend of file */
    if (maria_rsame(file,read_record,-1))
    {
      printf("Can't find last row with maria_rsame\n");
    }
    else
    {
      memcpy(record,read_record,rec_length);
      update_record(record);
      if (maria_update(file,read_record,record))
      {
	printf("Can't update last row: %.*s\n",
	       keyinfo[0].seg[0].length,read_record+1);
      }
    }

    /* Read through all rows and update them */
    maria_scan_init(file);

    found=0;
    while ((error= maria_scan(file,read_record)) == 0)
    {
      if (--update_count == 0) { maria_close(file); exit(0) ; }
      memcpy(record,read_record,rec_length);
      update_record(record);
      if (maria_update(file,read_record,record))
      {
	printf("Can't update row: %.*s, error: %d\n",
	       keyinfo[0].seg[0].length,record+1,my_errno);
      }
      found++;
    }
    if (found != row_count)
      printf("Found %ld of %ld rows\n", (ulong) found, (ulong) row_count);
    maria_scan_end(file);
  }

  if (checkpoint == 3 && ma_checkpoint_execute(CHECKPOINT_MEDIUM, FALSE))
    goto err;

  if (testflag == 3)
  {
    printf("Terminating after updates\n");
    goto end;
  }
  if (!silent)
    printf("- Reopening file\n");
  if (maria_commit(file))
    goto err;
  if (maria_close(file))
    goto err;
  if (!(file=maria_open(filename,2,HA_OPEN_ABORT_IF_LOCKED)))
    goto err;
  if (maria_begin(file))
    goto err;
  if (opt_versioning)
    maria_versioning(file, 1);
  if (!skip_delete)
  {
    if (!silent)
      printf("- Removing keys\n");

    for (i=0 ; i <= 10 ; i++)
    {
      /*
        If you want to debug the problem in ma_test_recovery with BLOBs
        (see @todo there), you can break out of the loop after just one
        delete, it is enough, like this:
        if (i==1) break;
      */
      /* testing */
      if (remove_count-- == 0)
      {
        fprintf(stderr,
                "delete-rows number of rows deleted; Going down hard!\n");
        goto end;
      }
      j=i*2;
      if (!flags[j])
	continue;
      create_key(key,j);
      my_errno=0;
      if ((error = maria_rkey(file, read_record, 0, key,
                              HA_WHOLE_KEY, HA_READ_KEY_EXACT)))
      {
	if (verbose || (flags[j] >= 1 ||
			(error && my_errno != HA_ERR_KEY_NOT_FOUND)))
	  printf("key: '%.*s'  maria_rkey:  %3d  errno: %3d\n",
		 (int) key_length,key+offset_to_key,error,my_errno);
      }
      else
      {
	error=maria_delete(file,read_record);
	if (verbose || error)
	  printf("key: '%.*s'  maria_delete: %3d  errno: %3d\n",
		 (int) key_length, key+offset_to_key, error, my_errno);
	if (! error)
	{
	  deleted++;
	  flags[j]--;
	}
      }
    }
  }

  if (checkpoint == 4 && ma_checkpoint_execute(CHECKPOINT_MEDIUM, FALSE))
    goto err;

  if (testflag == 4)
  {
    printf("Terminating after deletes\n");
    goto end;
  }

  if (!silent)
    printf("- Reading rows with key\n");
  record[1]= 0;                                 /* For nicer printf */

  if (record_type == NO_RECORD)
    maria_extra(file, HA_EXTRA_KEYREAD, 0);

  for (i=0 ; i <= 25 ; i++)
  {
    create_key(key,i);
    my_errno=0;
    error=maria_rkey(file,read_record,0,key,HA_WHOLE_KEY,HA_READ_KEY_EXACT);
    if (verbose ||
	(error == 0 && flags[i] == 0 && unique_key) ||
	(error && (flags[i] != 0 || my_errno != HA_ERR_KEY_NOT_FOUND)))
    {
      printf("key: '%.*s'  maria_rkey: %3d  errno: %3d  record: %s\n",
	     (int) key_length,key+offset_to_key,error,my_errno,record+1);
    }
  }
  if (record_type == NO_RECORD)
  {
    maria_extra(file, HA_EXTRA_NO_KEYREAD, 0);
    goto end;
  }

  if (!silent)
    printf("- Reading rows with position\n");

  if (maria_scan_init(file))
  {
    fprintf(stderr, "maria_scan_init failed\n");
    goto err;
  }

  for (i=1,found=0 ; i <= 30 ; i++)
  {
    my_errno=0;
    if ((error= maria_scan(file, read_record)) == HA_ERR_END_OF_FILE)
    {
      if (found != row_count-deleted)
	printf("Found only %ld of %ld rows\n", (ulong) found,
	       (ulong) (row_count - deleted));
      break;
    }
    if (!error)
      found++;
    if (verbose || (error != 0 && error != HA_ERR_RECORD_DELETED &&
		    error != HA_ERR_END_OF_FILE))
    {
      printf("pos: %2d  maria_rrnd: %3d  errno: %3d  record: %s\n",
	     i-1,error,my_errno,read_record+1);
    }
  }
  maria_scan_end(file);

end:
  if (die_in_middle_of_transaction)
  {
    /* As commit record is not done, UNDO entries needs to be rolled back */
    switch (die_in_middle_of_transaction) {
    case 1:
      /*
        Flush changed pages go to disk. That will also flush log. Recovery
        will skip REDOs and apply UNDOs.
      */
      _ma_flush_table_files(file, MARIA_FLUSH_DATA | MARIA_FLUSH_INDEX,
                            FLUSH_RELEASE, FLUSH_RELEASE);
      break;
    case 2:
      /*
        Just flush log. Pages are likely to not be on disk. Recovery will
        then execute REDOs and UNDOs.
      */
      if (translog_flush(file->trn->undo_lsn))
        goto err;
      break;
    case 3:
      /*
        Flush nothing. Pages and log are likely to not be on disk. Recovery
        will then do nothing.
      */
      break;
    case 4:
      /*
        Flush changed data pages go to disk. Changed index pages are not
        flushed. Recovery will skip some REDOs and apply UNDOs.
      */
      _ma_flush_table_files(file, MARIA_FLUSH_DATA, FLUSH_RELEASE,
                            FLUSH_RELEASE);
      /*
        We have to flush log separately as the redo for the last key page
        may not be flushed
      */
      if (translog_flush(file->trn->undo_lsn))
        goto err;
      break;
    }
    printf("Dying on request without maria_commit()/maria_close()\n");
    sf_leaking_memory= 1;
    exit(0);
  }

  if (maria_commit(file))
    goto err;
  if (maria_close(file))
    goto err;
  maria_end();
  my_uuid_end();
  my_end(MY_CHECK_ERROR);

  return (0);
err:
  printf("got error: %3d when using maria-database\n",my_errno);
  return 1;			/* skip warning */
}
Exemplo n.º 19
0
bool setupTVs(bool ismaster, bool &error)
{
    error = false;
    QString localhostname = gCoreContext->GetHostName();

    MSqlQuery query(MSqlQuery::InitCon());

    if (ismaster)
    {
        // Hack to make sure recorded.basename gets set if the user
        // downgrades to a prior version and creates new entries
        // without it.
        if (!query.exec("UPDATE recorded SET basename = CONCAT(chanid, '_', "
                        "DATE_FORMAT(starttime, '%Y%m%d%H%i00'), '_', "
                        "DATE_FORMAT(endtime, '%Y%m%d%H%i00'), '.nuv') "
                        "WHERE basename = '';"))
            MythDB::DBError("Updating record basename",
                                 query.lastQuery());

        // Hack to make sure record.station gets set if the user
        // downgrades to a prior version and creates new entries
        // without it.
        if (!query.exec("UPDATE channel SET callsign=chanid "
                        "WHERE callsign IS NULL OR callsign='';"))
            MythDB::DBError("Updating channel callsign", query.lastQuery());

        if (query.exec("SELECT MIN(chanid) FROM channel;"))
        {
            query.first();
            int min_chanid = query.value(0).toInt();
            if (!query.exec(QString("UPDATE record SET chanid = %1 "
                                    "WHERE chanid IS NULL;").arg(min_chanid)))
                MythDB::DBError("Updating record chanid", query.lastQuery());
        }
        else
            MythDB::DBError("Querying minimum chanid", query.lastQuery());

        MSqlQuery records_without_station(MSqlQuery::InitCon());
        records_without_station.prepare("SELECT record.chanid,"
                " channel.callsign FROM record LEFT JOIN channel"
                " ON record.chanid = channel.chanid WHERE record.station='';");
        if (records_without_station.exec() && records_without_station.next())
        {
            MSqlQuery update_record(MSqlQuery::InitCon());
            update_record.prepare("UPDATE record SET station = :CALLSIGN"
                    " WHERE chanid = :CHANID;");
            do
            {
                update_record.bindValue(":CALLSIGN",
                        records_without_station.value(1));
                update_record.bindValue(":CHANID",
                        records_without_station.value(0));
                if (!update_record.exec())
                {
                    MythDB::DBError("Updating record station",
                            update_record.lastQuery());
                }
            } while (records_without_station.next());
        }
    }

    if (!query.exec(
            "SELECT cardid, hostname "
            "FROM capturecard "
            "ORDER BY cardid"))
    {
        MythDB::DBError("Querying Recorders", query);
        return false;
    }

    vector<uint>    cardids;
    vector<QString> hosts;
    while (query.next())
    {
        uint    cardid = query.value(0).toUInt();
        QString host   = query.value(1).toString();
        QString cidmsg = QString("Card %1").arg(cardid);

        if (host.isEmpty())
        {
            QString msg = cidmsg + " does not have a hostname defined.\n"
                "Please run setup and confirm all of the capture cards.\n";

            VERBOSE(VB_IMPORTANT, msg);
            gCoreContext->LogEntry("mythbackend", LP_CRITICAL,
                               "Problem with capture cards", msg);
            continue;
        }

        cardids.push_back(cardid);
        hosts.push_back(host);
    }

    for (uint i = 0; i < cardids.size(); i++)
    {
        if (hosts[i] == localhostname)
            new TVRec(cardids[i]);
    }

    for (uint i = 0; i < cardids.size(); i++)
    {
        uint    cardid = cardids[i];
        QString host   = hosts[i];
        QString cidmsg = QString("Card %1").arg(cardid);

        if (!ismaster)
        {
            if (host == localhostname)
            {
                TVRec *tv = TVRec::GetTVRec(cardid);
                if (tv && tv->Init())
                {
                    EncoderLink *enc = new EncoderLink(cardid, tv);
                    tvList[cardid] = enc;
                }
                else
                {
                    gCoreContext->LogEntry("mythbackend", LP_CRITICAL,
                                       "Problem with capture cards",
                                       cidmsg + " failed init");
                    delete tv;
                    // The master assumes card comes up so we need to
                    // set error and exit if a non-master card fails.
                    error = true;
                }
            }
        }
        else
        {
            if (host == localhostname)
            {
                TVRec *tv = TVRec::GetTVRec(cardid);
                if (tv && tv->Init())
                {
                    EncoderLink *enc = new EncoderLink(cardid, tv);
                    tvList[cardid] = enc;
                }
                else
                {
                    gCoreContext->LogEntry("mythbackend", LP_CRITICAL,
                                       "Problem with capture cards",
                                       cidmsg + "failed init");
                    delete tv;
                }
            }
            else
            {
                EncoderLink *enc = new EncoderLink(cardid, NULL, host);
                tvList[cardid] = enc;
            }
        }
    }

    if (tvList.empty())
    {
        VERBOSE(VB_IMPORTANT, LOC_WARN +
                "No valid capture cards are defined in the database.");

        gCoreContext->LogEntry("mythbackend", LP_WARNING,
                           "No capture cards are defined",
                           "This backend will not be used for recording.");
    }

    return true;
}