diff options
author | Jason Rassi <rassi@10gen.com> | 2014-05-21 19:45:10 -0400 |
---|---|---|
committer | Jason Rassi <rassi@10gen.com> | 2014-05-21 19:47:49 -0400 |
commit | eb8b368edb239a7a698d734dedcf4f6950bf64ec (patch) | |
tree | 80b2bb99da4958e4a318e824f3f1059f36a310d4 /src/mongo/db | |
parent | a7526c96e9a584c646294d4414acbad1b452a6f4 (diff) | |
download | mongo-eb8b368edb239a7a698d734dedcf4f6950bf64ec.tar.gz |
SERVER-13737 Pull CollectionOptions out into libcollection_options.a
Diffstat (limited to 'src/mongo/db')
-rw-r--r-- | src/mongo/db/catalog/SConscript | 8 | ||||
-rw-r--r-- | src/mongo/db/catalog/collection_options.cpp | 147 | ||||
-rw-r--r-- | src/mongo/db/catalog/collection_options.h | 77 | ||||
-rw-r--r-- | src/mongo/db/catalog/collection_options_test.cpp | 58 | ||||
-rw-r--r-- | src/mongo/db/catalog/database.cpp | 86 | ||||
-rw-r--r-- | src/mongo/db/catalog/database.h | 46 | ||||
-rw-r--r-- | src/mongo/db/structure/catalog/namespace_details.cpp | 17 | ||||
-rw-r--r-- | src/mongo/db/structure/catalog/namespace_details.h | 6 |
8 files changed, 294 insertions, 151 deletions
diff --git a/src/mongo/db/catalog/SConscript b/src/mongo/db/catalog/SConscript new file mode 100644 index 00000000000..f610c8aa5d4 --- /dev/null +++ b/src/mongo/db/catalog/SConscript @@ -0,0 +1,8 @@ +# -*- mode: python; -*- + +Import("env") + +env.Library('collection_options', ['collection_options.cpp'], LIBDEPS=['$BUILD_DIR/mongo/bson']) + +env.CppUnitTest('collection_options_test', ['collection_options_test.cpp'], + LIBDEPS=['collection_options']) diff --git a/src/mongo/db/catalog/collection_options.cpp b/src/mongo/db/catalog/collection_options.cpp new file mode 100644 index 00000000000..f9371bc7e2e --- /dev/null +++ b/src/mongo/db/catalog/collection_options.cpp @@ -0,0 +1,147 @@ +/** + * Copyright (C) 2014 MongoDB 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/>. + * + * As a special exception, the copyright holders give permission to link the + * code of portions of this program with the OpenSSL library under certain + * conditions as described in each individual source file and distribute + * linked combinations including the program with the OpenSSL library. You + * must comply with the GNU Affero General Public License in all respects for + * all of the code used other than as permitted herein. If you modify file(s) + * with this exception, you may extend this exception to your version of the + * file(s), but you are not obligated to do so. If you do not wish to do so, + * delete this exception statement from your version. If you delete this + * exception statement from all source files in the program, then also delete + * it in the license file. + */ + +#include "mongo/platform/basic.h" + +#include "mongo/db/catalog/collection_options.h" + +namespace mongo { + + // static + bool CollectionOptions::validMaxCappedDocs( long long* max ) { + if ( *max <= 0 || + *max == std::numeric_limits<long long>::max() ) { + *max = 0x7fffffff; + return true; + } + + if ( *max < ( 0x1LL << 31 ) ) { + return true; + } + + return false; + } + + void CollectionOptions::reset() { + capped = false; + cappedSize = 0; + cappedMaxDocs = 0; + initialNumExtents = 0; + initialExtentSizes.clear(); + autoIndexId = DEFAULT; + flags = 0; + flagsSet = false; + temp = false; + } + + Status CollectionOptions::parse( const BSONObj& options ) { + reset(); + + BSONObjIterator i( options ); + while ( i.more() ) { + BSONElement e = i.next(); + StringData fieldName = e.fieldName(); + + if ( fieldName == "capped" ) { + capped = e.trueValue(); + } + else if ( fieldName == "size" ) { + if ( !e.isNumber() ) + return Status( ErrorCodes::BadValue, "size has to be a number" ); + cappedSize = e.numberLong(); + if ( cappedSize < 0 ) + return Status( ErrorCodes::BadValue, "size has to be >= 0" ); + cappedSize += 0xff; + cappedSize &= 0xffffffffffffff00LL; + } + else if ( fieldName == "max" ) { + if ( !e.isNumber() ) + return Status( ErrorCodes::BadValue, "max has to be a number" ); + cappedMaxDocs = e.numberLong(); + if ( !validMaxCappedDocs( &cappedMaxDocs ) ) + return Status( ErrorCodes::BadValue, + "max in a capped collection has to be < 2^31 or not set" ); + } + else if ( fieldName == "$nExtents" ) { + if ( e.type() == Array ) { + BSONObjIterator j( e.Obj() ); + while ( j.more() ) { + BSONElement inner = j.next(); + initialExtentSizes.push_back( inner.numberInt() ); + } + } + else { + initialNumExtents = e.numberLong(); + } + } + else if ( fieldName == "autoIndexId" ) { + if ( e.trueValue() ) + autoIndexId = YES; + else + autoIndexId = NO; + } + else if ( fieldName == "flags" ) { + flags = e.numberInt(); + flagsSet = true; + } + else if ( fieldName == "temp" ) { + temp = e.trueValue(); + } + } + + return Status::OK(); + } + + BSONObj CollectionOptions::toBSON() const { + BSONObjBuilder b; + if ( capped ) { + b.appendBool( "capped", true ); + if ( cappedSize ) + b.appendNumber( "size", cappedSize ); + if ( cappedMaxDocs ) + b.appendNumber( "max", cappedMaxDocs ); + } + + if ( initialNumExtents ) + b.appendNumber( "$nExtents", initialNumExtents ); + if ( !initialExtentSizes.empty() ) + b.append( "$nExtents", initialExtentSizes ); + + if ( autoIndexId != DEFAULT ) + b.appendBool( "autoIndexId", autoIndexId == YES ); + + if ( flagsSet ) + b.append( "flags", flags ); + + if ( temp ) + b.appendBool( "temp", true ); + + return b.obj(); + } + +} diff --git a/src/mongo/db/catalog/collection_options.h b/src/mongo/db/catalog/collection_options.h new file mode 100644 index 00000000000..96323cdf881 --- /dev/null +++ b/src/mongo/db/catalog/collection_options.h @@ -0,0 +1,77 @@ +/** + * Copyright (C) 2014 MongoDB 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/>. + * + * As a special exception, the copyright holders give permission to link the + * code of portions of this program with the OpenSSL library under certain + * conditions as described in each individual source file and distribute + * linked combinations including the program with the OpenSSL library. You + * must comply with the GNU Affero General Public License in all respects for + * all of the code used other than as permitted herein. If you modify file(s) + * with this exception, you may extend this exception to your version of the + * file(s), but you are not obligated to do so. If you do not wish to do so, + * delete this exception statement from your version. If you delete this + * exception statement from all source files in the program, then also delete + * it in the license file. + */ + +#pragma once + +#include "mongo/base/status.h" +#include "mongo/db/jsobj.h" + +namespace mongo { + + struct CollectionOptions { + CollectionOptions() { + reset(); + } + + void reset(); + + Status parse( const BSONObj& obj ); + BSONObj toBSON() const; + + /** + * @param max in and out, will be adjusted + * @return if the value is valid at all + */ + static bool validMaxCappedDocs( long long* max ); + + // ---- + + bool capped; + long long cappedSize; + long long cappedMaxDocs; + + // following 2 are mutually exclusive, can only have one set + long long initialNumExtents; + std::vector<long long> initialExtentSizes; + + // behavior of _id index creation when collection created + void setNoIdIndex() { autoIndexId = NO; } + enum { + DEFAULT, // currently yes for most collections, NO for some system ones + YES, // create _id index + NO // do not create _id index + } autoIndexId; + + // user flags + int flags; + bool flagsSet; + + bool temp; + }; + +} diff --git a/src/mongo/db/catalog/collection_options_test.cpp b/src/mongo/db/catalog/collection_options_test.cpp new file mode 100644 index 00000000000..bdcc764d927 --- /dev/null +++ b/src/mongo/db/catalog/collection_options_test.cpp @@ -0,0 +1,58 @@ +/** + * Copyright (C) 2014 MongoDB 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/>. + * + * As a special exception, the copyright holders give permission to link the + * code of portions of this program with the OpenSSL library under certain + * conditions as described in each individual source file and distribute + * linked combinations including the program with the OpenSSL library. You + * must comply with the GNU Affero General Public License in all respects for + * all of the code used other than as permitted herein. If you modify file(s) + * with this exception, you may extend this exception to your version of the + * file(s), but you are not obligated to do so. If you do not wish to do so, + * delete this exception statement from your version. If you delete this + * exception statement from all source files in the program, then also delete + * it in the license file. + */ + +#include "mongo/platform/basic.h" + +#include "mongo/db/catalog/collection_options.h" + +#include "mongo/db/json.h" +#include "mongo/unittest/unittest.h" + +namespace mongo { + + void checkRoundTrip( const CollectionOptions& options1 ) { + CollectionOptions options2; + options2.parse( options1.toBSON() ); + ASSERT_EQUALS( options1.toBSON(), options2.toBSON() ); + } + + TEST( CollectionOptions, SimpleRoundTrip ) { + CollectionOptions options; + checkRoundTrip( options ); + + options.capped = true; + options.cappedSize = 10240; + options.cappedMaxDocs = 1111; + checkRoundTrip( options ); + + options.setNoIdIndex(); + options.flags = 5; + checkRoundTrip( options ); + } + +} diff --git a/src/mongo/db/catalog/database.cpp b/src/mongo/db/catalog/database.cpp index b7ea6ce2df2..969eda34e85 100644 --- a/src/mongo/db/catalog/database.cpp +++ b/src/mongo/db/catalog/database.cpp @@ -40,6 +40,7 @@ #include "mongo/db/background.h" #include "mongo/db/clientcursor.h" #include "mongo/db/catalog/collection_catalog_entry.h" +#include "mongo/db/catalog/collection_options.h" #include "mongo/db/catalog/database_holder.h" #include "mongo/db/dbhelpers.h" #include "mongo/db/structure/catalog/index_details.h" @@ -61,91 +62,6 @@ namespace mongo { - Status CollectionOptions::parse( const BSONObj& options ) { - reset(); - - BSONObjIterator i( options ); - while ( i.more() ) { - BSONElement e = i.next(); - StringData fieldName = e.fieldName(); - - if ( fieldName == "capped" ) { - capped = e.trueValue(); - } - else if ( fieldName == "size" ) { - if ( !e.isNumber() ) - return Status( ErrorCodes::BadValue, "size has to be a number" ); - cappedSize = e.numberLong(); - if ( cappedSize < 0 ) - return Status( ErrorCodes::BadValue, "size has to be >= 0" ); - cappedSize += 0xff; - cappedSize &= 0xffffffffffffff00LL; - } - else if ( fieldName == "max" ) { - if ( !e.isNumber() ) - return Status( ErrorCodes::BadValue, "max has to be a number" ); - cappedMaxDocs = e.numberLong(); - if ( !NamespaceDetails::validMaxCappedDocs( &cappedMaxDocs ) ) - return Status( ErrorCodes::BadValue, - "max in a capped collection has to be < 2^31 or not set" ); - } - else if ( fieldName == "$nExtents" ) { - if ( e.type() == Array ) { - BSONObjIterator j( e.Obj() ); - while ( j.more() ) { - BSONElement inner = j.next(); - initialExtentSizes.push_back( inner.numberInt() ); - } - } - else { - initialNumExtents = e.numberLong(); - } - } - else if ( fieldName == "autoIndexId" ) { - if ( e.trueValue() ) - autoIndexId = YES; - else - autoIndexId = NO; - } - else if ( fieldName == "flags" ) { - flags = e.numberInt(); - flagsSet = true; - } - else if ( fieldName == "temp" ) { - temp = e.trueValue(); - } - } - - return Status::OK(); - } - - BSONObj CollectionOptions::toBSON() const { - BSONObjBuilder b; - if ( capped ) { - b.appendBool( "capped", true ); - if ( cappedSize ) - b.appendNumber( "size", cappedSize ); - if ( cappedMaxDocs ) - b.appendNumber( "max", cappedMaxDocs ); - } - - if ( initialNumExtents ) - b.appendNumber( "$nExtents", initialNumExtents ); - if ( !initialExtentSizes.empty() ) - b.append( "$nExtents", initialExtentSizes ); - - if ( autoIndexId != DEFAULT ) - b.appendBool( "autoIndexId", autoIndexId == YES ); - - if ( flagsSet ) - b.append( "flags", flags ); - - if ( temp ) - b.appendBool( "temp", true ); - - return b.obj(); - } - void massertNamespaceNotIndex( const StringData& ns, const StringData& caller ) { massert( 17320, str::stream() << "cannot do " << caller diff --git a/src/mongo/db/catalog/database.h b/src/mongo/db/catalog/database.h index 8fc2488c1f3..e3a0b37f28c 100644 --- a/src/mongo/db/catalog/database.h +++ b/src/mongo/db/catalog/database.h @@ -31,6 +31,7 @@ #pragma once #include "mongo/bson/bsonobj.h" +#include "mongo/db/catalog/collection_options.h" #include "mongo/db/namespace_string.h" #include "mongo/db/storage_options.h" #include "mongo/util/concurrency/mutex.h" @@ -50,51 +51,6 @@ namespace mongo { class NamespaceIndex; class OperationContext; - struct CollectionOptions { - CollectionOptions() { - reset(); - } - - void reset() { - capped = false; - cappedSize = 0; - cappedMaxDocs = 0; - initialNumExtents = 0; - initialExtentSizes.clear(); - autoIndexId = DEFAULT; - flags = 0; - flagsSet = false; - temp = false; - } - - Status parse( const BSONObj& obj ); - BSONObj toBSON() const; - - // ---- - - bool capped; - long long cappedSize; - long long cappedMaxDocs; - - // following 2 are mutually exclusive, can only have one set - long long initialNumExtents; - std::vector<long long> initialExtentSizes; - - // behavior of _id index creation when collection created - void setNoIdIndex() { autoIndexId = NO; } - enum { - DEFAULT, // currently yes for most collections, NO for some system ones - YES, // create _id index - NO // do not create _id index - } autoIndexId; - - // user flags - int flags; - bool flagsSet; - - bool temp; - }; - /** * Database represents a database database * Each database database has its own set of files -- dbname.ns, dbname.0, dbname.1, ... diff --git a/src/mongo/db/structure/catalog/namespace_details.cpp b/src/mongo/db/structure/catalog/namespace_details.cpp index 3b3216188fa..50fed0200f3 100644 --- a/src/mongo/db/structure/catalog/namespace_details.cpp +++ b/src/mongo/db/structure/catalog/namespace_details.cpp @@ -35,6 +35,7 @@ #include "mongo/base/counter.h" #include "mongo/db/catalog/collection.h" +#include "mongo/db/catalog/collection_options.h" #include "mongo/db/clientcursor.h" #include "mongo/db/commands/server_status.h" #include "mongo/db/db.h" @@ -216,24 +217,10 @@ namespace mongo { void NamespaceDetails::setMaxCappedDocs( OperationContext* txn, long long max ) { massert( 16499, "max in a capped collection has to be < 2^31 or -1", - validMaxCappedDocs( &max ) ); + CollectionOptions::validMaxCappedDocs( &max ) ); maxDocsInCapped = max; } - bool NamespaceDetails::validMaxCappedDocs( long long* max ) { - if ( *max <= 0 || - *max == numeric_limits<long long>::max() ) { - *max = 0x7fffffff; - return true; - } - - if ( *max < ( 0x1LL << 31 ) ) { - return true; - } - - return false; - } - /* ------------------------------------------------------------------------- */ diff --git a/src/mongo/db/structure/catalog/namespace_details.h b/src/mongo/db/structure/catalog/namespace_details.h index 020e9154621..625207a4734 100644 --- a/src/mongo/db/structure/catalog/namespace_details.h +++ b/src/mongo/db/structure/catalog/namespace_details.h @@ -161,12 +161,6 @@ namespace mongo { public: void setMaxCappedDocs( OperationContext* txn, long long max ); - /** - * @param max in and out, will be adjusted - * @return if the value is valid at all - */ - static bool validMaxCappedDocs( long long* max ); - enum UserFlags { Flag_UsePowerOf2Sizes = 1 << 0 }; |