Exemple #1
0
// Set the status of the specified global commit. Return 'true' on success,
// 'false' otherwise.
bool clog_write(clog_t clog, xid_t xid, int status) {
	clogfile_t *file = clog_xid_to_file(clog, xid);
	if (!file) {
		debug("xid %u out of range, creating the file\n", xid);
		clogfile_t newfile;
		if (!clogfile_open_by_id(&newfile, clog->datadir, XID_TO_FILEID(xid), true)) {
			shout(
				"failed to create new clogfile "
				"while saving transaction status\n"
			);
			return false;
		}

		clogfile_chain_t *lastfile = new_clogfile_chain(&newfile);
		lastfile->prev = clog->lastfile;
		clog->lastfile = lastfile;
	}
	file = clog_xid_to_file(clog, xid);
	if (!file) {
		shout("the file is absent despite our efforts\n");
		return false;
	}
	bool ok = clogfile_set_status(file, xid, status);
	return ok;
}
int main() {
    clogfile_t a, b;
    if (!clogfile_open_by_id(&a, "/tmp", 0, false)) {
        if (!clogfile_open_by_id(&a, "/tmp", 0, true)) {
            return EXIT_FAILURE;
        }
    }
    if (!clogfile_open_by_id(&b, "/tmp", 1, false)) {
        if (!clogfile_open_by_id(&b, "/tmp", 1, true)) {
            return EXIT_FAILURE;
        }
    }

    uint64_t xid;
    for (xid = 0; xid < 32; xid++) {
        int status;
        if (xid < 16) {
            status = clogfile_get_status(&a, xid);
        } else {
            status = clogfile_get_status(&b, xid);
        }
        printf("before: %lu status %d\n", xid, status);
    }

    if (!clogfile_set_status(&a, 0, XSTATUS_INPROGRESS)) return EXIT_FAILURE;
    if (!clogfile_set_status(&a, 1, XSTATUS_COMMITTED)) return EXIT_FAILURE;
    if (!clogfile_set_status(&a, 2, XSTATUS_ABORTED)) return EXIT_FAILURE;
    if (!clogfile_set_status(&b, 29, XSTATUS_INPROGRESS)) return EXIT_FAILURE;
    if (!clogfile_set_status(&b, 30, XSTATUS_COMMITTED)) return EXIT_FAILURE;
    if (!clogfile_set_status(&b, 31, XSTATUS_ABORTED)) return EXIT_FAILURE;

    for (xid = 0; xid < 32; xid++) {
        int status;
        if (xid < 16) {
            status = clogfile_get_status(&a, xid);
        } else {
            status = clogfile_get_status(&b, xid);
        }
        printf(" after: %lu status %d\n", xid, status);
    }

    if (!clogfile_close(&a)) return EXIT_FAILURE;
    if (!clogfile_close(&b)) return EXIT_FAILURE;
    return EXIT_SUCCESS;
}
Exemple #3
0
static clogfile_chain_t *load_clogfile_chain(char *datadir) {
	clogfile_t file;
	int fileid = get_latest_fileid(datadir);
	if (!clogfile_open_by_id(&file, datadir, fileid, false)) {
		// This may be the first launch, so try to create the file.
		if (!clogfile_open_by_id(&file, datadir, fileid, true)) {
			return NULL;
		}
	}
	clogfile_chain_t *head = new_clogfile_chain(&file);

	clogfile_chain_t *tail = head;
	while (fileid-- > 0) {
		if (!clogfile_open_by_id(&file, datadir, fileid, false)) {
			break;
		}
		tail->prev = new_clogfile_chain(&file);
		tail = tail->prev;
	}

	return head;
}