summaryrefslogtreecommitdiff
path: root/src/mongo/db/pipeline/document.h
blob: f11a825151e1a94c39375878ddb9964ca2ce332d (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
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
/**
 * 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/>.
 */

#pragma once

#include "pch.h"

#include "util/intrusive_counter.h"

namespace mongo {
    class BSONObj;
    class FieldIterator;
    class Value;

    class Document :
        public IntrusiveCounterUnsigned {
    public:
        ~Document();

        /*
          Create a new Document from the given BSONObj.

          Document field values may be pointed to in the BSONObj, so it
          must live at least as long as the resulting Document.

          @returns shared pointer to the newly created Document
        */
        static intrusive_ptr<Document> createFromBsonObj(BSONObj *pBsonObj);

        /*
          Create a new empty Document.

          @param sizeHint a hint at what the number of fields will be; if
            known, this can be used to increase memory allocation efficiency
          @returns shared pointer to the newly created Document
        */
        static intrusive_ptr<Document> create(size_t sizeHint = 0);

        /*
          Clone a document.

          The new document shares all the fields' values with the original.

	  This is not a deep copy.  Only the fields on the top-level document
	  are cloned.

	  @returns the shallow clone of the document
        */
        intrusive_ptr<Document> clone();

        /*
          Add this document to the BSONObj under construction with the
          given BSONObjBuilder.
        */
        void toBson(BSONObjBuilder *pBsonObjBuilder);

        /*
          Create a new FieldIterator that can be used to examine the
          Document's fields.
        */
        FieldIterator *createFieldIterator();

        /*
          Get the value of the specified field.

          @param fieldName the name of the field
          @return point to the requested field
        */
        intrusive_ptr<const Value> getValue(const string &fieldName);

        /*
          Add the given field to the Document.

          BSON documents' fields are ordered; the new Field will be
          appened to the current list of fields.

          It is an error to add a field that has the same name as another
          field.
        */
        void addField(const string &fieldName,
		      const intrusive_ptr<const Value> &pValue);

        /*
          Set the given field to be at the specified position in the
          Document.  This will replace any field that is currently in that
          position.  The index must be within the current range of field
          indices.

	  pValue.get() may be NULL, in which case the field will be
	  removed.  fieldName is ignored in this case.

	  @param index the field index in the list of fields
	  @param fieldName the new field name
	  @param pValue the new Value
        */
        void setField(size_t index,
                      const string &fieldName,
		      const intrusive_ptr<const Value> &pValue);

	/*
	  Convenience type for dealing with fields.
	 */
	typedef pair<string, intrusive_ptr<const Value> > FieldPair;

	/*
	  Get the indicated field.

	  @param index the field index in the list of fields
	  @returns the field name and value of the field
	 */
	FieldPair getField(size_t index) const;

	/*
	  Get the number of fields in the Document.

	  @returns the number of fields in the Document
	 */
	size_t getFieldCount() const;

	/*
	  Get the index of the given field.

	  @param fieldName the name of the field
	  @returns the index of the field, or if it does not exist, the number
	    of fields (getFieldCount())
	*/
	size_t getFieldIndex(const string &fieldName) const;

	/*
	  Get a field by name.

	  @param fieldName the name of the field
	  @returns the value of the field
	*/
	intrusive_ptr<const Value> getField(const string &fieldName) const;

	/*
	  Get the approximate storage size of the document, in bytes.

	  Under the assumption that field name strings are shared, they are
	  not included in the total.

	  @returns the approximate storage
	*/
	size_t getApproximateSize() const;

        /*
          Compare two documents.

          BSON document field order is significant, so this just goes through
          the fields in order.  The comparison is done in roughly the same way
          as strings are compared, but comparing one field at a time instead
          of one character at a time.
        */
        static int compare(const intrusive_ptr<Document> &rL,
                           const intrusive_ptr<Document> &rR);

	static string idName; // shared "_id"

	/*
	  Calculate a hash value.

	  Meant to be used to create composite hashes suitable for
	  boost classes such as unordered_map<>.

	  @param seed value to augment with this' hash
	*/
	void hash_combine(size_t &seed) const;

    private:
        friend class FieldIterator;

        Document(size_t sizeHint);
        Document(BSONObj *pBsonObj);

        /* these two vectors parallel each other */
        vector<string> vFieldName;
        vector<intrusive_ptr<const Value> > vpValue;
    };


    class FieldIterator :
            boost::noncopyable {
    public:
        /*
          Ask if there are more fields to return.

          @return true if there are more fields, false otherwise
        */
        bool more() const;

        /*
          Move the iterator to point to the next field and return it.

          @return the next field's <name, Value>
        */
	Document::FieldPair next();

    private:
        friend class Document;

        /*
          Constructor.

          @param pDocument points to the document whose fields are being
              iterated
        */
        FieldIterator(const intrusive_ptr<Document> &pDocument);

        /*
          We'll hang on to the original document to ensure we keep the
          fieldPtr vector alive.
        */
	intrusive_ptr<Document> pDocument;
        size_t index; // current field in iteration
    };
}


/* ======================= INLINED IMPLEMENTATIONS ========================== */

namespace mongo {

    inline size_t Document::getFieldCount() const {
	return vFieldName.size();
    }
    
    inline Document::FieldPair Document::getField(size_t index) const {
        assert( index < vFieldName.size() );
        return FieldPair(vFieldName[index], vpValue[index]);
    }

}