Beispiel #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;
}