-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuildGraph.cpp
More file actions
184 lines (171 loc) · 4.84 KB
/
buildGraph.cpp
File metadata and controls
184 lines (171 loc) · 4.84 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "buildGraph.h"
BuildGraph* BuildGraph::_pInstance = new BuildGraph();
BuildGraph::BuildGraph()
{
Init();
}
BuildGraph* BuildGraph::GetInstance()
{
if(_pInstance == NULL)
_pInstance = new BuildGraph();
return _pInstance;
}
int BuildGraph::InsertUserGraph(const long &userId,const long &fuserId){
map<long int,UserLink>::iterator iter;
UserLink header;
iter = userGraphMap.find(userId);
if(iter != userGraphMap.end()){
header = iter->second;
struct UserNode *p;
p = header;
while(p!=NULL){
if(p->uid == fuserId)
{
p->connect++;
break;
}
p = p->next;
}
if(p == NULL)
{
p = (struct UserNode *)malloc(sizeof(struct UserNode));
p->uid = fuserId;
p->connect = 1;
p->next = header->next;
header->next = p;
CacheUtil::GetInstance()->InsertUserMap(userId,1,0);
}
}
else{
UserLink header;
UserNode *p;
header = new UserNode;
p = new UserNode;
header->connect = 1;
header->next = p;
header->uid = userId;
p->connect =1;
p->uid = fuserId;
p->next = NULL;
userGraphMap[userId] = header;
CacheUtil::GetInstance()->InsertUserMap(userId,1,0);
}
return 0;
}
bool BuildGraph::Init()
{
LOG4CPLUS_INFO(LogUtil::GetInstance()->GetLogger()
,"BuildGraph Init");
vector<Status> statusV;
string startTime = "2009-08-12 00:00:00";
string query = "select sid,userid from comment where time < '";
query += startTime + "'";
if(DbUtil::GetInstance()->DbQueryStatus(query,statusV))
{
LOG4CPLUS_ERROR(LogUtil::GetInstance()->GetLogger()
,"DbQueryStatus fail");
return false;
}
for(unsigned int i=0;i<statusV.size();i++){
long userId;
userId = CacheUtil::GetInstance()->FindUserIdBySid(statusV[i].sid);
if(userId != -1)
{
if(InsertUserGraph(userId,statusV[i].userid))
{
LOG4CPLUS_ERROR(LogUtil::GetInstance()->GetLogger()
,"InsertUserGraph fail");
return false;
}
}
}
UpdateGraphWeight();
return true;
}
bool BuildGraph::UpdateGraphWeight()
{
map<long int,UserLink>::iterator iter;
for(iter = userGraphMap.begin();iter != userGraphMap.end();iter++)
{
UserLink head = iter->second;
UserNode *p = head;
while(p!=NULL)
{
p->weight = CacheUtil::GetInstance()->FindWeightByUid(p->uid);
p = p->next;
}
}
return true;
}
bool BuildGraph::AddGraph(const string &startTime,const string &endTime){
vector<Status> statusV;
string query = "select sid,userid from comment where time >= '";
query += startTime + "' and time < '";
query += endTime + "'";
//将endTime 赋值给 startTime 作为新的startTime
if(DbUtil::GetInstance()->DbQueryStatus(query,statusV))
{
LOG4CPLUS_ERROR(LogUtil::GetInstance()->GetLogger()
,"DbQueryStatus fail");
return false;
}
for(unsigned int i=0;i<statusV.size();i++){
long userId;
int weight;
userId = CacheUtil::GetInstance()->FindUserIdBySid(statusV[i].sid);
if(userId != -1)
{
if(InsertUserGraph(userId,statusV[i].userid))
{
LOG4CPLUS_ERROR(LogUtil::GetInstance()->GetLogger()
,"InsertUserGraph fail");
return false;
}
}
}
UpdateGraphWeight();
return true;
}
int BuildGraph::Show()
{
map<long int,UserLink>::iterator iter;
struct UserNode *p;
for(iter = userGraphMap.begin();iter != userGraphMap.end();iter++){
cout<<"userid:"<<iter->first<<" Link ";
p = iter->second;
while(p!=NULL){
cout<<p->uid<<" "<<p->connect<<" "<<p->weight<<" ";
p = p->next;
}
cout<<endl;
}
}
bool BuildGraph::Clear(){
map<long int,UserLink>::iterator iter;
struct UserNode *p;
struct UserNode *q;
for(iter = userGraphMap.begin();iter != userGraphMap.end();iter++){
p = iter->second;
q = p->next;
while(q!=NULL){
delete p;
p = q;
q = p->next;
}
delete p;
}
userGraphMap.clear();
return true;
}
map<long int,UserLink>* BuildGraph::GetUserGraphMap()
{
return &userGraphMap;
}
void BuildGraph::Test(){
}
BuildGraph::~BuildGraph()
{
Clear();
if(BuildGraph::_pInstance)
delete BuildGraph::_pInstance;
}