summaryrefslogtreecommitdiff
path: root/db/pipeline/document_source_command_futures.cpp
blob: 61a257cf16f5b2eab3fa5be48f62c6cb694e293b (plain)
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
/**
 * Copyright 2011 (c) 10gen Inc.
 *
 * This program is free software: you can redistribute it and/or  modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "pch.h"

#include "db/pipeline/document_source.h"

namespace mongo {

    DocumentSourceCommandFutures::~DocumentSourceCommandFutures() {
    }

    bool DocumentSourceCommandFutures::eof() {
	/* if we haven't even started yet, do so */
	if (!pCurrent.get())
	    getNextDocument();

	return (pCurrent.get() == NULL);
    }

    bool DocumentSourceCommandFutures::advance() {
	if (eof())
	    return false;

	/* advance */
	getNextDocument();

	return (pCurrent.get() != NULL);
    }

    intrusive_ptr<Document> DocumentSourceCommandFutures::getCurrent() {
	assert(!eof());
	return pCurrent;
    }

    void DocumentSourceCommandFutures::setSource(
	const intrusive_ptr<DocumentSource> &pSource) {
	/* this doesn't take a source */
	assert(false);
    }

    void DocumentSourceCommandFutures::sourceToBson(
	BSONObjBuilder *pBuilder) const {
        /* this has no BSON equivalent */
	assert(false);
    }

    DocumentSourceCommandFutures::DocumentSourceCommandFutures(
	string &theErrmsg, FuturesList *pList):
        newSource(false),
        pBsonSource(),
        pCurrent(),
        iterator(pList->begin()),
        listEnd(pList->end()),
        errmsg(theErrmsg) {
    }

    intrusive_ptr<DocumentSourceCommandFutures>
    DocumentSourceCommandFutures::create(
	string &errmsg, FuturesList *pList) {
	intrusive_ptr<DocumentSourceCommandFutures> pSource(
	    new DocumentSourceCommandFutures(errmsg, pList));
	return pSource;
    }

    void DocumentSourceCommandFutures::getNextDocument() {
	while(true) {
	    if (!pBsonSource.get()) {
		/* if there aren't any more futures, we're done */
		if (iterator == listEnd) {
		    pCurrent.reset();
		    return;
		}

		/* grab the next command result */
		shared_ptr<Future::CommandResult> pResult(*iterator);
		++iterator;

		/* try to wait for it */
		if (!pResult->join()) {
		    error() << "sharded pipeline failed on shard: " <<
			pResult->getServer() << " error: " <<
			pResult->result() << endl;
		    errmsg += "-- mongod pipeline failed: ";
		    errmsg += pResult->result().toString();

		    /* move on to the next command future */
		    continue;
		}

		/* grab the result array out of the shard server's response */
		BSONObj shardResult(pResult->result());
		BSONObjIterator objIterator(shardResult);
		while(objIterator.more()) {
		    BSONElement element(objIterator.next());
		    const char *pFieldName = element.fieldName();

		    /* find the result array and quit this loop */
		    if (strcmp(pFieldName, "result") == 0) {
			pBsonSource = DocumentSourceBsonArray::create(&element);
			newSource = true;
			break;
		    }
		}
	    }

	    /* if we're done with this shard's results, try the next */
	    if (pBsonSource->eof() ||
		(!newSource && !pBsonSource->advance())) {
		pBsonSource.reset();
		continue;
	    }

	    pCurrent = pBsonSource->getCurrent();
	    newSource = false;
	    return;
	}
    }
}