/*
* Copyright (c) 2008-2014 MongoDB, 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 com.mongodb.util.Util;
import org.bson.BSONObject;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* A thread-safe client view of a logical database in a MongoDB cluster. A DB instance can be achieved from a {@link MongoClient} instance
* using code like:
* <pre>
* {@code
* MongoClient mongoClient = new MongoClient();
* DB db = mongoClient.getDB("<db name>");
* }</pre>
*
* @mongodb.driver.manual reference/glossary/#term-database Database
* @see MongoClient
*/
public abstract class DB {
private static final Set<String> _obedientCommands = new HashSet<String>();
static {
_obedientCommands.add("group");
_obedientCommands.add("aggregate");
_obedientCommands.add("collstats");
_obedientCommands.add("dbstats");
_obedientCommands.add("count");
_obedientCommands.add("distinct");
_obedientCommands.add("geonear");
_obedientCommands.add("geosearch");
_obedientCommands.add("geowalk");
_obedientCommands.add("text");
_obedientCommands.add("parallelcollectionscan");
_obedientCommands.add("listindexes");
_obedientCommands.add("listcollections");
}
/**
* Constructs a new instance of the {@code DB}.
*
* @param mongo the mongo instance
* @param name the database name - must not be empty and cannot contain spaces
*/
public DB(final Mongo mongo, final String name) {
if(!isValidName(name))
throw new IllegalArgumentException("Invalid database name format. Database name is either empty or it contains spaces.");
_mongo = mongo;
_name = name;
_options = new Bytes.OptionHolder( _mongo._netOptions );
}
/**
* Determines the read preference that should be used for the given command.
*
* @param command the {@link DBObject} representing the command
* @param requestedPreference the preference requested by the client.
* @return the read preference to use for the given command. It will never return {@code null}.
* @see com.mongodb.ReadPreference
*/
ReadPreference getCommandReadPreference(DBObject command, ReadPreference requestedPreference){
if (_mongo.getReplicaSetStatus() == null) {
return requestedPreference;
}
String comString = command.keySet().iterator().next();
if (comString.equals("getnonce") || comString.equals("authenticate")) {
return ReadPreference.primaryPreferred();
}
boolean primaryRequired;
// explicitly check mapreduce commands are inline
if(comString.equals("mapreduce")) {
Object out = command.get("out");
if (out instanceof BSONObject ){
BSONObject outMap = (BSONObject) out;
primaryRequired = outMap.get("inline") == null;
} else {
primaryRequired = true;
}
} else if(comString.equals("aggregate")) {
@SuppressWarnings("unchecked")
List<DBObject> pipeline = (List<DBObject>) command.get("pipeline");
primaryRequired = pipeline.get(pipeline.size()-1).get("$out") != null;
} else {
primaryRequired = !_obedientCommands.contains(comString.toLowerCase());
}
if (primaryRequired) {
return ReadPreference.primary();
} else if (requestedPreference == null) {
return ReadPreference.primary();
} else {
return requestedPreference;
}
}
/**
* <p>Starts a new 'consistent request'</p>.
*
* <p>Following this call and until {@link com.mongodb.DB#requestDone()} is called, all db operations will use the same underlying
* connection.</p>
*
* @deprecated The main use case for this method is to ensure that applications can read their own unacknowledged writes,
* but this is no longer so prevalent since the driver started defaulting to acknowledged writes. The other main use case is to
* ensure that related read operations are all routed to the same server when using a non-primary read preference. But this is
* dangerous because mongos does not provide this guarantee. For these reasons, this method is now deprecated and will be
* removed in the next major release.
*/
@Deprecated
public abstract void requestStart();
/**
* Ends the current 'consistent request'.
* @deprecated The main use case for this method is to ensure that applications can read their own unacknowledged writes,
* but this is no longer so prevalent since the driver started defaulting to acknowledged writes. The other main use case is to
* ensure that related read operations are all routed to the same server when using a non-primary read preference. But this is
* dangerous because mongos does not provide this guarantee. For these reasons, this method is now deprecated and will be
* removed in the next major release.
*/
@Deprecated
public abstract void requestDone();
/**
* Ensure that a connection is assigned to the current "consistent request" (from primary pool, if connected to a replica set).
*
* @deprecated The main use case for this method is to ensure that applications can read their own unacknowledged writes,
* but this is no longer so prevalent since the driver started defaulting to acknowledged writes. The other main use case is to
* ensure that related read operations are all routed to the same server when using a non-primary read preference. But this is
* dangerous because mongos does not provide this guarantee. For these reasons, this method is now deprecated and will be
* removed in the next major release.
*/
@Deprecated
public abstract void requestEnsureConnection();
/**
* <p>Gets a collection with a given name. If the collection does not exist, a new collection is created.</p>
*
* <p>This class is NOT part of the public API. Be prepared for non-binary compatible changes in minor releases.</p>
*
* @param name the name of the collection
* @return the collection
*/
protected abstract DBCollection doGetCollection( String name );
/**
* Gets a collection with a given name.
*
* @param name the name of the collection to return
* @return the collection
*/
public DBCollection getCollection( String name ){
DBCollection c = doGetCollection( name );
return c;
}
/**
* <p>Creates a collection with a given name and options. If the collection already exists, this throws a
* {@code CommandFailureException}.</p>
*
* <p>Possible options:</p>
* <ul>
* <li> <b>capped</b> ({@code boolean}) - Enables a collection cap. False by default. If enabled,
* you must specify a size parameter. </li>
* <li> <b>size</b> ({@code int}) - If capped is true, size specifies a maximum size in bytes for the capped collection. When
* capped is false, you may use size to preallocate space. </li>
* <li> <b>max</b> ({@code int}) - Optional. Specifies a maximum "cap" in number of documents for capped collections. You m
没有合适的资源?快使用搜索试试~ 我知道了~
jar包改造过程.v1.0.20180517.zip
共5个文件
class:2个
java:2个
docx:1个
5星 · 超过95%的资源 需积分: 9 15 下载量 56 浏览量
2018-05-17
15:08:47
上传
评论
收藏 239KB ZIP 举报
温馨提示
该zip包里面存放了改造jar包的过程,以及改造完成之后的class文件
资源推荐
资源详情
资源评论
收起资源包目录
jar包改造过程.v1.0.20180517.zip (5个子文件)
DBTCPConnector.class 19KB
DBTCPConnector.java 23KB
DB.class 16KB
jar包改造过程.v1.0.20180517.docx 222KB
DB.java 40KB
共 5 条
- 1
资源评论
- u0111212902019-03-18再下载一遍,照着改改,感谢作者
拱拱大白菜
- 粉丝: 6
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功