summaryrefslogtreecommitdiff
path: root/db/index.h
diff options
context:
space:
mode:
authorAntonin Kral <a.kral@bobek.cz>2011-09-14 17:08:06 +0200
committerAntonin Kral <a.kral@bobek.cz>2011-09-14 17:08:06 +0200
commit5d342a758c6095b4d30aba0750b54f13b8916f51 (patch)
tree762e9aa84781f5e3b96db2c02d356c29cf0217c0 /db/index.h
parentcbe2d992e9cd1ea66af9fa91df006106775d3073 (diff)
downloadmongodb-5d342a758c6095b4d30aba0750b54f13b8916f51.tar.gz
Imported Upstream version 2.0.0
Diffstat (limited to 'db/index.h')
-rw-r--r--db/index.h85
1 files changed, 66 insertions, 19 deletions
diff --git a/db/index.h b/db/index.h
index d13bd1d..54b0639 100644
--- a/db/index.h
+++ b/db/index.h
@@ -22,9 +22,34 @@
#include "diskloc.h"
#include "jsobj.h"
#include "indexkey.h"
+#include "key.h"
namespace mongo {
+ class IndexInterface {
+ protected:
+ virtual ~IndexInterface() { }
+ public:
+ virtual int keyCompare(const BSONObj& l,const BSONObj& r, const Ordering &ordering) = 0;
+ virtual long long fullValidate(const DiskLoc& thisLoc, const BSONObj &order) = 0;
+ virtual DiskLoc findSingle(const IndexDetails &indexdetails , const DiskLoc& thisLoc, const BSONObj& key) const = 0;
+ virtual bool unindex(const DiskLoc thisLoc, IndexDetails& id, const BSONObj& key, const DiskLoc recordLoc) const = 0;
+ virtual int bt_insert(const DiskLoc thisLoc, const DiskLoc recordLoc,
+ const BSONObj& key, const Ordering &order, bool dupsAllowed,
+ IndexDetails& idx, bool toplevel = true) const = 0;
+ virtual DiskLoc addBucket(const IndexDetails&) = 0;
+ virtual void uassertIfDups(IndexDetails& idx, vector<BSONObj*>& addedKeys, DiskLoc head,
+ DiskLoc self, const Ordering& ordering) = 0;
+
+ // these are for geo
+ virtual bool isUsed(DiskLoc thisLoc, int pos) = 0;
+ virtual void keyAt(DiskLoc thisLoc, int pos, BSONObj&, DiskLoc& recordLoc) = 0;
+ virtual BSONObj keyAt(DiskLoc thisLoc, int pos) = 0;
+ virtual DiskLoc locate(const IndexDetails &idx , const DiskLoc& thisLoc, const BSONObj& key, const Ordering &order,
+ int& pos, bool& found, const DiskLoc &recordLoc, int direction=1) = 0;
+ virtual DiskLoc advance(const DiskLoc& thisLoc, int& keyOfs, int direction, const char *caller) = 0;
+ };
+
/* Details about a particular index. There is one of these effectively for each object in
system.namespaces (although this also includes the head pointer, which is not in that
collection).
@@ -45,7 +70,7 @@ namespace mongo {
/* Location of index info object. Format:
{ name:"nameofindex", ns:"parentnsname", key: {keypattobject}
- [, unique: <bool>, background: <bool>]
+ [, unique: <bool>, background: <bool>, v:<version>]
}
This object is in the system.indexes collection. Note that since we
@@ -68,7 +93,7 @@ namespace mongo {
only when it's a "multikey" array.
keys will be left empty if key not found in the object.
*/
- void getKeysFromObject( const BSONObj& obj, BSONObjSetDefaultOrder& keys) const;
+ void getKeysFromObject( const BSONObj& obj, BSONObjSet& keys) const;
/* get the key pattern for this object.
e.g., { lastname:1, firstname:1 }
@@ -86,7 +111,6 @@ namespace mongo {
/* true if the specified key is in the index */
bool hasKey(const BSONObj& key);
- bool wouldCreateDup(const BSONObj& key, DiskLoc self);
// returns name of this index's storage area
// database.table.$index
@@ -126,6 +150,21 @@ namespace mongo {
return io.getStringField("ns");
}
+ static int versionForIndexObj( const BSONObj &obj ) {
+ BSONElement e = obj["v"];
+ if( e.type() == NumberInt )
+ return e._numberInt();
+ // should normally be an int. this is for backward compatibility
+ int v = e.numberInt();
+ uassert(14802, "index v field should be Integer type", v == 0);
+ return v;
+ }
+
+ int version() const {
+ return versionForIndexObj( info.obj() );
+ }
+
+ /** @return true if index has unique constraint */
bool unique() const {
BSONObj io = info.obj();
return io["unique"].trueValue() ||
@@ -133,33 +172,43 @@ namespace mongo {
isIdIndex();
}
- /* if set, when building index, if any duplicates, drop the duplicating object */
+ /** return true if dropDups was set when building index (if any duplicates, dropdups drops the duplicating objects) */
bool dropDups() const {
return info.obj().getBoolField( "dropDups" );
}
- /* delete this index. does NOT clean up the system catalog
- (system.indexes or system.namespaces) -- only NamespaceIndex.
+ /** delete this index. does NOT clean up the system catalog
+ (system.indexes or system.namespaces) -- only NamespaceIndex.
*/
void kill_idx();
const IndexSpec& getSpec() const;
- void checkVersion() const {
- // TODO: cache?
- massert( 13658 ,
- str::stream() << "using a newer index version: " << info.obj() << " v: " << info.obj().getIntField("v" ) ,
- info.obj().getIntField("v") <= 0 );
- }
-
string toString() const {
return info.obj().toString();
}
+
+ /** @return true if supported. supported means we can use the index, including adding new keys.
+ it may not mean we can build the index version in question: we may not maintain building
+ of indexes in old formats in the future.
+ */
+ static bool isASupportedIndexVersionNumber(int v) { return (v&1)==v; } // v == 0 || v == 1
+
+ /** @return the interface for this interface, which varies with the index version.
+ used for backward compatibility of index versions/formats.
+ */
+ IndexInterface& idxInterface() const {
+ int v = version();
+ dassert( isASupportedIndexVersionNumber(v) );
+ return *iis[v&1];
+ }
+
+ static IndexInterface *iis[];
};
struct IndexChanges { /*on an update*/
- BSONObjSetDefaultOrder oldkeys;
- BSONObjSetDefaultOrder newkeys;
+ BSONObjSet oldkeys;
+ BSONObjSet newkeys;
vector<BSONObj*> removed; // these keys were removed as part of the change
vector<BSONObj*> added; // these keys were added as part of the change
@@ -169,10 +218,8 @@ namespace mongo {
void dupCheck(IndexDetails& idx, DiskLoc curObjLoc) {
if( added.empty() || !idx.unique() )
return;
- for( vector<BSONObj*>::iterator i = added.begin(); i != added.end(); i++ ) {
- bool dup = idx.wouldCreateDup(**i, curObjLoc);
- uassert( 11001 , "E11001 duplicate key on update", !dup);
- }
+ const Ordering ordering = Ordering::make(idx.keyPattern());
+ idx.idxInterface().uassertIfDups(idx, added, idx.head, curObjLoc, ordering); // "E11001 duplicate key on update"
}
};