-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfindcommand.cpp
148 lines (121 loc) · 4.3 KB
/
findcommand.cpp
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
// *********************************************************************************************************************
// file:
// author: Juan Pablo Crossley ([email protected])
// created:
// updated:
// license:
//
// This file is part of the djondb project, for license information please refer to the LICENSE file,
// the application and libraries are provided as-is and free of use under the terms explained in the file LICENSE
// Its authors create this application in order to make the world a better place to live, but you should use it on
// your own risks.
//
// Also, be adviced that, the GPL license force the committers to ensure this application will be free of use, thus
// if you do any modification you will be required to provide it for free unless you use it for personal use (you may
// charge yourself if you want), bare in mind that you will be required to provide a copy of the license terms that ensures
// this program will be open sourced and all its derivated work will be too.
// *********************************************************************************************************************
#include "findcommand.h"
#include "bsonoutputstream.h"
#include "bsoninputstream.h"
#include "dbcontroller.h"
#include "outputstream.h"
#include "util.h"
FindCommand::FindCommand()
: Command(FIND)
{
//ctor
_findresult = NULL;
_select = NULL;
_filter = NULL;
_namespace = NULL;
_db = NULL;
}
FindCommand::~FindCommand()
{
if (_select != NULL) delete _select;
if (_filter != NULL) delete _filter;
if (_namespace != NULL) delete _namespace;
if (_db != NULL) delete _db;
if (_findresult != NULL) {
for (std::vector<BSONObj*>::const_iterator i = _findresult->begin(); i != _findresult->end(); i++) {
BSONObj* obj = *i;
delete obj;
}
delete _findresult;
}
}
FindCommand::FindCommand(const FindCommand& other) :Command(FIND)
{
this->_select = new std::string(*other._select);
this->_filter = new std::string(*other._filter);
this->_namespace = new std::string(*other._namespace);
this->_db = new std::string(*other._db);
if (other._findresult != NULL) {
this->_findresult = new std::vector<BSONObj*>();
for (std::vector<BSONObj*>::const_iterator i = other._findresult->begin(); i != other._findresult->end(); i++) {
BSONObj* obj = new BSONObj(**i);
this->_findresult->push_back(obj);
}
}
}
void FindCommand::execute() {
Logger* log = getLogger(NULL);
if (log->isDebug()) log->debug("executing find command on %s", nameSpace()->c_str());
_findresult = dbController()->find(const_cast<char*>(DB()->c_str()), const_cast<char*>(nameSpace()->c_str()), select()->c_str(), filter()->c_str());
delete log;
}
void* FindCommand::result() {
std::vector<BSONObj*>* result = new std::vector<BSONObj*>();
for (std::vector<BSONObj*>::const_iterator i = _findresult->begin(); i != _findresult->end(); i++) {
BSONObj* obj = new BSONObj(**i);
result->push_back(obj);
}
return result;
}
void FindCommand::writeCommand(OutputStream* out) const {
out->writeString(*_db);
out->writeString(*_namespace);
out->writeString(*_filter);
out->writeString(*_select);
}
void FindCommand::readResult(InputStream* is) {
Logger* log = getLogger(NULL);
if (log->isDebug()) log->debug("writing result of find command on %s", nameSpace()->c_str());
BSONInputStream* bsonin = new BSONInputStream(is);
std::vector<BSONObj*>* result = bsonin->readBSONArray();
_findresult = result;
delete bsonin;
}
void FindCommand::writeResult(OutputStream* out) const {
Logger* log = getLogger(NULL);
if (log->isDebug()) log->debug("writing result of find command on %s", nameSpace()->c_str());
BSONOutputStream* bsonout = new BSONOutputStream(out);
bsonout->writeBSONArray(*_findresult);
delete bsonout;
delete log;
}
void FindCommand::setNameSpace(const std::string& ns) {
_namespace = new std::string(ns);
}
std::string* FindCommand::nameSpace() const {
return _namespace;
}
void FindCommand::setSelect(const std::string& select) {
_select = new std::string(select);
}
std::string* FindCommand::select() const {
return _select;
}
void FindCommand::setFilter(const std::string& filter) {
_filter = new std::string(filter);
}
std::string* FindCommand::filter() const {
return _filter;
}
void FindCommand::setDB(const std::string& db) {
_db = new std::string(db);
}
const std::string* FindCommand::DB() const {
return _db;
}