-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapiNote.cpp
More file actions
247 lines (225 loc) · 7.19 KB
/
apiNote.cpp
File metadata and controls
247 lines (225 loc) · 7.19 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include "apiNote.h"
///\fn apiNote
///\arg std::string filespec
///
///< creates a store to save the notes into
///
apiNote::apiNote() : srvlbClient<apiNote>()
{
//ctor
}
///\fn openStore
///\arg filespec
///\return none
///<
void apiNote::openStore(std::string &filespec){
m_fname = (filespec.empty()) ? "apiNote.json" : filespec;
ms_js.reset(new jsonStore(m_fname));
}
///\fn ~apiNote
///\arg none
///\return none
///< deletes the jsonStore pointer
apiNote::~apiNote()
{
//delete m_js;
ms_js =nullptr;
}
///\fn writeNote
///\arg int length
///\arg char * data
///\return none
///< takes the raw data from a post puts it in a stringstream
///< adds a json {id:NNN} to the data and then
///< then sends boost::property_tree::ptree to the jsonStore for presistant storage
///<
void apiNote::writeNote(int length, char * data)
{
std::string b;
std::stringstream jsonStream;
b.append(data,length);
jsonStream << b;
std::string sid = ms_js->getId();
try {
pt::ptree pbody;
pt::read_json(jsonStream,pbody);
m_note.put("id",sid);
for (pt::ptree::iterator ibody = pbody.begin(); ibody != pbody.end(); ibody++){
m_note.put( ibody->first,ibody->second.get_value<std::string>() );
}
ms_js->writeJ(sid,m_note);
}catch (std::exception &e){
std::cerr << "apiNote::writeNote caught exception" << e.what() << std::endl;
}
}
///\fn virtual writeStuff for srvlbClient interface
///\arg int length
///\arg char * data last character is zero
///\ return none
void apiNote::writeStuff(int length, char * data){
writeNote(length,data);
}
///\fn response
///\arg std::stringstream & jsonResponse
///\return fills in the stringStream with the ptree
///\pre writeNote called before
///\warning relies on the fact that writeNote was called previously
///\warning returns values from last call to writeNote
///\warning NOT THREAD SAFE
void apiNote::response(std::stringstream & jsonResponse)
{
pt::write_json(jsonResponse,m_note);
}
///\fn virtual getResponse for srvlbClient interface
///\arg string & returnV
///\return none
///< passed in string returned full
void apiNote::getResponse(std::string & returnV){
std::stringstream jsonResponse;
pt::write_json(jsonResponse,m_note);
returnV.append(jsonResponse.str());
}
///\fn retrive note
///\arg std::string not id to fetch
///\arg std::stringstream & jsonRespone
///\return fills in the note associated with passed in request id
///\warning NOT THREAD SAFE
std::shared_ptr<apiNote::t_apiNoteInfo> apiNote::retriveNote(std::string sid, std::stringstream & jsonResponse)
{
pt::ptree jsonData;
std::shared_ptr<jsonIndex::t_jsonIndex> sji = ms_js->getJ(sid,jsonData);
if (! jsonData.empty()){
pt::write_json(jsonResponse,jsonData);
}
return (sji);
}
///\fn virtual getSpecific for srvlbClient interface
///\arg string & returnV
///\arg string & query api note
///\return none
///< passed in string returned full
void apiNote::retriveSpecific(std::string &returnV,std::string & queryV)
{
std::stringstream jsonResponse;
jsonResponse.str("");
retriveNote(queryV,jsonResponse);
returnV.append(jsonResponse.str());
}
///\fn fetchAllNotes
///\arg stringstream &
///\return fills stringstream with all notes in an array of json elements
///\warning works with a small number of notes
///\todo change to send in starting token and a max size so json records can be chunked
///\warning NOT THREAD SAFE
void apiNote::fetchAllNotes(std::stringstream &allnotes,std::string &token)
{
bool getNotes = true;
std::string sid;
long id =0;
while (getNotes) {
sid = std::to_string(id);
id++;
std::stringstream ssrnote;
ssrnote.str(""); // clear string in case complier is optimizing too much
std::shared_ptr<jsonIndex::t_jsonIndex> spji = retriveNote(sid,ssrnote);
if (!token.empty() && spji.get() != nullptr) {
if (! Contains(token,ssrnote))
continue;
}
if (spji.get() !=nullptr){
allnotes << ssrnote.str();
}else {
getNotes = false;
}
}
return;
}
///\fn virtual retriveItemsMatching for srvlbClient interface
///\arg string & returnV
///\arg string & query search token
///\return none
///< passed in string returned full could be multiple notes
void apiNote::retriveItemsMatching(std::string &returnV, std::string &queryV)
{
std::stringstream multipleNotes;
fetchAllNotes(multipleNotes,queryV);
returnV.append (multipleNotes.str() );
}
///\fn virtual retriveAll for srvlbClient interface
///\arg string & returnV
///\return none
///< passed in string returned full with all notes
///
void apiNote::retriveAll(std::string& returnV){
std::stringstream allNotes;
buildJsonArray(allNotes);
returnV.append(allNotes.str());
}
///\fn buildJsonArray
///\arg std::stringstream &
///\return filled stringstream
///\warning large notes will consume as much memory as needed
///< build a jsonArray from all notes in the system
///\todo possibly write to separate jsonStorage so
///< you can chunk the answer back to the response
void apiNote::buildJsonArray(std::stringstream &ssrnote)
{
std::stringstream buildx;
bool getNotes = true;
std::string sid;
long id =0;
pt::ptree jArray;
pt::ptree jAElement;
while (getNotes) {
sid = std::to_string(id);
id++;
//std::stringstream ssrnote;
std::shared_ptr<jsonIndex::t_jsonIndex> spji = retriveNote(sid,buildx);
if (spji.get() !=nullptr){
try {
pt::ptree child;
pt::read_json(buildx,child);
for (pt::ptree::iterator pit = child.begin(); pit != child.end(); ){
pt::ptree field;
for (int i=0 ; i < 2 ; i++ ) {
field.put(pit->first,pit->second.get_value<std::string>() );
/**
* debug
*std::cout << pit->first << ":" << pit->second.get_value<std::string>() << std::endl;
*/
pit++;
}
jAElement.push_back(std::make_pair("",field));
}
}catch (std::exception &e){
std::cerr << e.what() << " from apiNote::BuildJsonArray" << std::endl;
}
}else {
getNotes = false;
}
}
try{
std::stringstream elm;
pt::write_json(elm,jAElement);
jArray.add_child("all notes",jAElement);
pt::write_json(ssrnote,jArray);
/**
* debug
* std::cout << ssrnote.str() << std::endl;
*/
}catch (std::exception & e){
std::cerr << e.what() << " from apiNote::BuildJsonArray" << std::endl;
}
}
///\fn Contains
///\arg std::string token [in]
///\arg std::stringstream data [in]
///\return true
///\brief true if the token is in the body or the key of the json
bool apiNote::Contains(std::string & token, std::stringstream & data)
{
std::string sdata = data.str();
boost::smatch found;
boost::regex expr(token.c_str(),boost::regex::perl|boost::match_partial);
return (boost::regex_search(sdata,found,expr )) ;
}