forked from mongodb/mongo-java-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOutMessage.java
More file actions
167 lines (128 loc) · 4.68 KB
/
OutMessage.java
File metadata and controls
167 lines (128 loc) · 4.68 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
// OutMessage.java
/**
* Copyright (C) 2008 10gen Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mongodb;
import java.io.IOException;
import java.io.OutputStream;
import java.util.concurrent.atomic.AtomicInteger;
import org.bson.BSONObject;
import org.bson.BasicBSONEncoder;
import org.bson.io.PoolOutputBuffer;
class OutMessage extends BasicBSONEncoder {
static AtomicInteger ID = new AtomicInteger(1);
static OutMessage query( Mongo m , int options , String ns , int numToSkip , int batchSize , DBObject query , DBObject fields ){
return query( m, options, ns, numToSkip, batchSize, query, fields, ReadPreference.PRIMARY );
}
static OutMessage query( Mongo m , int options , String ns , int numToSkip , int batchSize , DBObject query , DBObject fields, ReadPreference readPref ){
return query( m, options, ns, numToSkip, batchSize, query, fields, readPref, DefaultDBEncoder.FACTORY.create());
}
static OutMessage query( Mongo m , int options , String ns , int numToSkip , int batchSize , DBObject query , DBObject fields, ReadPreference readPref, DBEncoder enc ){
OutMessage out = new OutMessage( m , 2004, enc );
out._appendQuery( options , ns , numToSkip , batchSize , query , fields, readPref);
return out;
}
OutMessage( Mongo m ){
this( m , DefaultDBEncoder.FACTORY.create() );
}
OutMessage( Mongo m , int op ){
this( m );
reset( op );
}
OutMessage( Mongo m , DBEncoder encoder ) {
_encoder = encoder;
_mongo = m;
_buffer = _mongo == null ? new PoolOutputBuffer() : _mongo._bufferPool.get();
_buffer.reset();
set( _buffer );
}
OutMessage( Mongo m , int op , DBEncoder enc ) {
this( m , enc );
reset( op );
}
private void _appendQuery( int options , String ns , int numToSkip , int batchSize , DBObject query , DBObject fields, ReadPreference readPref){
_queryOptions = options;
_readPref = readPref;
//If the readPrefs are non-null and non-primary, set slaveOk query option
if (_readPref != null && !(_readPref instanceof ReadPreference.PrimaryReadPreference))
_queryOptions |= Bytes.QUERYOPTION_SLAVEOK;
writeInt( _queryOptions );
writeCString( ns );
writeInt( numToSkip );
writeInt( batchSize );
putObject( query );
if ( fields != null )
putObject( fields );
}
private void reset( int op ){
done();
_buffer.reset();
set( _buffer );
_id = ID.getAndIncrement();
writeInt( 0 ); // length: will set this later
writeInt( _id );
writeInt( 0 ); // response to
writeInt( op );
}
void prepare(){
_buffer.writeInt( 0 , _buffer.size() );
}
void pipe( OutputStream out )
throws IOException {
_buffer.pipe( out );
}
int size(){
return _buffer.size();
}
byte[] toByteArray(){
return _buffer.toByteArray();
}
void doneWithMessage(){
if ( _buffer != null && _mongo != null ) {
_buffer.reset();
_mongo._bufferPool.done( _buffer );
}
_buffer = null;
_mongo = null;
}
boolean hasOption( int option ){
return ( _queryOptions & option ) != 0;
}
int getId(){
return _id;
}
@Override
public int putObject(BSONObject o) {
// check max size
int sz = _encoder.writeObject(_buf, o);
if (_mongo != null) {
int maxsize = _mongo.getConnector().getMaxBsonObjectSize();
maxsize = Math.max(maxsize, Bytes.MAX_OBJECT_SIZE);
if (sz > maxsize) {
throw new MongoInternalException("DBObject of size " + sz + " is over Max BSON size " + _mongo.getMaxBsonObjectSize());
}
}
return sz;
}
public ReadPreference getReadPreference(){
return _readPref;
}
private Mongo _mongo;
private PoolOutputBuffer _buffer;
private int _id;
private int _queryOptions = 0;
private ReadPreference _readPref = ReadPreference.PRIMARY;
private DBEncoder _encoder;
}