-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTableCacheCallbacks.inc.cpp
More file actions
176 lines (145 loc) · 4.5 KB
/
Copy pathTableCacheCallbacks.inc.cpp
File metadata and controls
176 lines (145 loc) · 4.5 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
#ifndef Table_Cache_Callbacks_h
#define Table_Cache_Callbacks_h
#include <set>
#include <string>
#include <vector>
#include "TableRow.h"
#include "IQuestProcessor.h"
#include "TableCacheErrorInfo.h"
#define FPNN_MAX_ERROR_CODE 29999
inline FPAnswerPtr dumpErrorAnswer(IAsyncAnswerPtr async, FPAnswerPtr answer)
{
FPAReader ar(answer);
int64_t code = ar.wantInt("code");
std::string ex = ar.wantString("ex");
std::string raiser = ar.wantString("raiser");
return FPAWriter::errorAnswer(async->getQuest(), code, ex, raiser);
}
template <typename TYPE>
class FetchRowCallback: public AnswerCallback
{
private:
int _retryTimes;
TABLEPtr _scheme;
FPQuestPtr _dbQuest;
IAsyncAnswerPtr _async;
TableCacheProcessorPtr _processor;
std::vector<uint16_t> _requiredIndex;
std::map<TYPE, std::vector<std::string>> _cachedResult;
void addRowDataToResult(const std::string& key, int64_t&, const std::vector<std::string>& row)
{
_cachedResult[(int64_t)atoll(key.c_str())] = row;
}
void addRowDataToResult(const std::string& key, const std::string&, const std::vector<std::string>& row)
{
_cachedResult[key] = row;
}
public:
FetchRowCallback(IAsyncAnswerPtr async, TableCacheProcessorPtr processor, FPQuestPtr dbQuest,
TABLEPtr scheme, std::vector<uint16_t>& requiredIndex,
std::map<TYPE, std::vector<std::string>>& cachedResult):
_retryTimes(0), _scheme(scheme), _dbQuest(dbQuest), _async(async), _processor(processor)
{
_requiredIndex.swap(requiredIndex);
_cachedResult.swap(cachedResult);
}
virtual void onAnswer(FPAnswerPtr answer)
{
TYPE kindSign;
std::string keyCloumn = _scheme->get_key_name();
std::vector<uint16_t> index = _scheme->get_fields_index(std::vector<std::string>{keyCloumn});
FPAReader ar(answer);
std::vector<std::vector<std::string>> rows = ar.want("rows", std::vector<std::vector<std::string>>());
for (const auto& rowData: rows)
{
std::vector<std::string> result;
result.reserve(_requiredIndex.size());
for (size_t i = 0; i < _requiredIndex.size(); i++)
result.push_back(rowData[_requiredIndex[i]]);
const std::string& hintValue = rowData[index[0]];
addRowDataToResult(hintValue, kindSign, result);
}
FPAWriter aw(1, _async->getQuest());
aw.param("data", _cachedResult);
answer = aw.take();
_async->sendAnswer(answer);
_processor->addRows(_scheme, rows);
}
virtual void onException(FPAnswerPtr answer, int errorCode)
{
if (_retryTimes == 0)
{
if (errorCode <= FPNN_MAX_ERROR_CODE)
{
FetchRowCallback* callback = new FetchRowCallback(
_async, _processor, _dbQuest, _scheme, _requiredIndex, _cachedResult);
callback->_retryTimes = 1;
if (_processor->_dbproxy->sendQuest(_dbQuest, callback))
return;
delete callback;
answer = nullptr;
}
}
if (!answer)
answer = ErrorInfo::queryDBProxyFailedAnswer(_async->getQuest());
else
//answer->setSeqNum(_async->getQuest()->seqNum());
answer = dumpErrorAnswer(_async, answer);
_async->sendAnswer(answer);
}
};
class WriteCallback: public AnswerCallback
{
private:
int _retryTimes;
FPQuestPtr _dbQuest;
TCPClientPtr _dbproxy;
IAsyncAnswerPtr _async;
int64_t _hintId;
std::string _tableName;
TableCacheProcessorPtr _processor;
void cleanCache();
public:
WriteCallback(IAsyncAnswerPtr async, TCPClientPtr dbproxy, FPQuestPtr dbQuest):
_retryTimes(0), _dbQuest(dbQuest), _dbproxy(dbproxy), _async(async), _processor(nullptr) {}
virtual void onAnswer(FPAnswerPtr answer);
virtual void onException(FPAnswerPtr answer, int errorCode);
void cleanCacheAfterGotResponse(int64_t hintId, const std::string& tableName, TableCacheProcessorPtr processor)
{
_hintId = hintId; _tableName = tableName; _processor = processor;
}
};
void WriteCallback::onAnswer(FPAnswerPtr)
{
cleanCache();
FPAnswerPtr answer = FPAWriter::emptyAnswer(_async->getQuest());
_async->sendAnswer(answer);
}
void WriteCallback::onException(FPAnswerPtr answer, int errorCode)
{
if (_retryTimes == 0)
{
if (errorCode <= FPNN_MAX_ERROR_CODE)
{
WriteCallback* callback = new WriteCallback(_async, _dbproxy, _dbQuest);
callback->_retryTimes = 1;
if (_dbproxy->sendQuest(_dbQuest, callback))
return;
delete callback;
answer = nullptr;
}
}
if (!answer)
answer = ErrorInfo::queryDBProxyFailedAnswer(_async->getQuest());
else
//answer->setSeqNum(_async->getQuest()->seqNum());
answer = dumpErrorAnswer(_async, answer);
_async->sendAnswer(answer);
cleanCache();
}
void WriteCallback::cleanCache()
{
if (_processor)
_processor->cleanCache(_tableName, _hintId);
}
#endif