1
- #include "transaction.h"
1
+ #include <stddef.h>
2
+ #include <stdlib.h>
3
+ #include <string.h>
4
+ #include "ddd.h"
2
5
3
- typedef struct Instance {
4
- struct Edge * edges ;/* local subgraph */
5
- }Instance ;
6
-
7
- typedef struct Edge {
8
- L2List node ;/* node of list of outgoing eedges */
9
- struct Edge * next ;/* list of edges of local subgraph */
10
- struct Vertex * dst ;
11
- struct Vertex * src ;
12
- }Edge ;
13
-
14
- typedef struct Vertex
15
- {
16
- L2List outgoingEdges ;
17
- xid_t xid ;
18
- int nIncomingEdges ;
19
- bool visited ;
20
- }Vertex ;
21
-
22
- typedef struct Graph
23
- {
24
- Vertex * hashtable [MAX_TRANSACTIONS ];
25
- Edge * freeEdges ;
26
- Vertex * freeVertexes ;
27
- }Graph ;
6
+ static bool recursiveTraverseGraph (Vertex * root ,Vertex * v ,int marker );
28
7
29
8
void initGraph (Graph * graph )
30
9
{
31
- memset (graph -> hashtable ,0. sizeof (graph -> hashtable ));
10
+ memset (graph -> hashtable ,0 , sizeof (graph -> hashtable ));
32
11
graph -> freeEdges = NULL ;
33
12
graph -> freeVertexes = NULL ;
13
+ graph -> marker = 0 ;
34
14
}
35
15
36
- Edge * newEdge (Graph * graph )
16
+ static inline Edge * newEdge (Graph * graph )
37
17
{
38
18
Edge * edge = graph -> freeEdges ;
39
19
if (edge == NULL ) {
@@ -44,44 +24,51 @@ Edge* newEdge(Graph* graph)
44
24
return edge ;
45
25
}
46
26
47
- void freeVertex (Graph * graph ,Vertex * vertex )
27
+ static inline void freeVertex (Graph * graph ,Vertex * vertex )
48
28
{
49
- vertex -> node .next = (L2List * )graph -> freeVertexes ;
29
+ int h = vertex -> xid %MAX_TRANSACTIONS ;
30
+ Vertex * * vpp = & graph -> hashtable [h ];
31
+ while (* vpp != vertex ) {
32
+ vpp = & (* vpp )-> next ;
33
+ }
34
+ * vpp = vertex -> next ;
35
+ vertex -> next = graph -> freeVertexes ;
50
36
graph -> freeVertexes = vertex ;
37
+
51
38
}
52
39
53
- void freeEdge (Graph * graph ,Edge * edge )
40
+ static inline void freeEdge (Graph * graph ,Edge * edge )
54
41
{
55
42
edge -> next = graph -> freeEdges ;
56
43
graph -> freeEdges = edge ;
57
44
}
58
45
59
- Vertex * newVertex (Graph * graph )
46
+ static inline Vertex * newVertex (Graph * graph )
60
47
{
61
48
Vertex * v = graph -> freeVertexes ;
62
49
if (v == NULL ) {
63
50
v = (Vertex * )malloc (sizeof (Vertex ));
64
51
}else {
65
- graph -> freeVertexes = ( Vertex * ) v . node . next ;
52
+ graph -> freeVertexes = v -> next ;
66
53
}
67
54
return v ;
68
55
}
69
56
70
- Vertex * findVertex (Graph * graph ,xid_t xid )
57
+ static inline Vertex * findVertex (Graph * graph ,xid_t xid )
71
58
{
72
- xid_t h = xid ;
59
+ xid_t h = xid % MAX_TRANSACTIONS ;
73
60
Vertex * v ;
74
- while (( v = graph -> hashtable [h % MAX_TRANSACTIONS ]) != NULL ) {
61
+ for ( v = graph -> hashtable [h ]; v != NULL ; v = v -> next ) {
75
62
if (v -> xid == xid ) {
76
63
return v ;
77
64
}
78
- h += 1 ;
79
65
}
80
66
v = newVertex (graph );
81
- l2_list_init (v -> outgoingEdges );
67
+ l2_list_init (& v -> outgoingEdges );
82
68
v -> xid = xid ;
83
69
v -> nIncomingEdges = 0 ;
84
- graph -> hashtable [h %MAX_TRANSACTIONS ]= v ;
70
+ v -> next = graph -> hashtable [h ];
71
+ graph -> hashtable [h ]= v ;
85
72
return v ;
86
73
}
87
74
@@ -107,15 +94,38 @@ void addSubgraph(Instance* instance, Graph* graph, xid_t* xids, int n_xids)
107
94
next = e -> next ;
108
95
l2_list_unlink (& e -> node );
109
96
if (-- e -> dst -> nIncomingEdges == 0 && l2_list_is_empty (& e -> dst -> outgoingEdges )) {
110
- freeVertex (e -> dst );
97
+ freeVertex (graph , e -> dst );
111
98
}
112
99
if (e -> src -> nIncomingEdges == 0 && l2_list_is_empty (& e -> src -> outgoingEdges )) {
113
- freeVertex (e -> src );
100
+ freeVertex (graph , e -> src );
114
101
}
115
- freeEdge (e );
102
+ freeEdge (graph , e );
116
103
}
117
104
}
118
105
119
- bool findLoop ( Graph * graph )
106
+ static bool recursiveTraverseGraph ( Vertex * root , Vertex * v , int marker )
120
107
{
108
+ L2List * l ;
109
+ Edge * e ;
110
+ v -> visited = marker ;
111
+ for (l = v -> outgoingEdges .next ;l != & v -> outgoingEdges ;l = e -> node .next ) {
112
+ e = (Edge * )l ;
113
+ if (e -> dst == root ) {
114
+ return true;
115
+ }else if (e -> dst -> visited != marker && recursiveTraverseGraph (root ,e -> dst ,marker )) {/* loop */
116
+ return true;
117
+ }
118
+ }
119
+ return false;
120
+ }
121
+
122
+ bool findLoop (Graph * graph ,xid_t root )
123
+ {
124
+ Vertex * v ;
125
+ for (v = graph -> hashtable [root %MAX_TRANSACTIONS ];v != NULL ;v = v -> next ) {
126
+ if (v -> xid == root ) {
127
+ return recursiveTraverseGraph (v ,v ,++ graph -> marker );
128
+ }
129
+ }
130
+ return false;
121
131
}