-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathddd.c
74 lines (67 loc) · 1.95 KB
/
ddd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "postgres.h"
#include "access/clog.h"
#include "storage/lwlock.h"
#include "utils/hsearch.h"
#include "ddd.h"
void MtmGraphInit(MtmGraph* graph)
{
memset(graph->hashtable, 0, sizeof(graph->hashtable));
}
static inline MtmVertex* findVertex(MtmGraph* graph, GlobalTransactionId* gtid)
{
uint32 h = gtid->xid % MAX_TRANSACTIONS;
MtmVertex* v;
for (v = graph->hashtable[h]; v != NULL; v = v->collision) {
if (EQUAL_GTID(v->gtid, *gtid)) {
return v;
}
}
v = (MtmVertex*)palloc(sizeof(MtmVertex));
v->gtid = *gtid;
v->outgoingEdges = NULL;
v->collision = graph->hashtable[h];
graph->hashtable[h] = v;
return v;
}
void MtmGraphAdd(MtmGraph* graph, GlobalTransactionId* gtid, int size)
{
GlobalTransactionId* last = gtid + size;
while (gtid != last) {
MtmVertex* src = findVertex(graph, gtid++);
while (gtid->node != 0) {
MtmVertex* dst = findVertex(graph, gtid++);
MtmEdge* e = (MtmEdge*)palloc(sizeof(MtmEdge));
e->dst = dst;
e->src = src;
e->next = src->outgoingEdges;
src->outgoingEdges = e;
}
gtid += 1;
}
}
static bool recursiveTraverseGraph(MtmVertex* root, MtmVertex* v)
{
MtmEdge* e;
v->gtid.node = VISITED_NODE_MARK;
for (e = v->outgoingEdges; e != NULL; e = e->next) {
if (e->dst == root) {
return true;
} else if (e->dst->gtid.node != VISITED_NODE_MARK && recursiveTraverseGraph(root, e->dst)) { /* loop */
return true;
}
}
return false;
}
bool MtmGraphFindLoop(MtmGraph* graph, GlobalTransactionId* root)
{
MtmVertex* v;
for (v = graph->hashtable[root->xid % MAX_TRANSACTIONS]; v != NULL; v = v->collision) {
if (EQUAL_GTID(v->gtid, *root)) {
if (recursiveTraverseGraph(v, v)) {
return true;
}
break;
}
}
return false;
}