Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tag: r1.3.0
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 146 lines (113 sloc) 4.439 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
// dbhelpers.h

/**
* Copyright (C) 2008 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/>.
*/

/* db helpers are helper functions and classes that let us easily manipulate the local
database instance.
*/

#pragma once

#include "../stdafx.h"
#include "client.h"
#include "db.h"

namespace mongo {

    class Cursor;
    class CoveredIndexMatcher;

    class CursorIterator {
    public:
        CursorIterator( auto_ptr<Cursor> c , BSONObj filter = BSONObj() );
        BSONObj next();
        bool hasNext();

    private:
        void _advance();

        auto_ptr<Cursor> _cursor;
        auto_ptr<CoveredIndexMatcher> _matcher;
        BSONObj _o;
    };

    /**
all helpers assume locking is handled above them
*/
    struct Helpers {

        /* ensure the specified index exists.

@param keyPattern key pattern, e.g., { ts : 1 }
@param name index name, e.g., "name_1"

This method can be a little (not much) cpu-slow, so you may wish to use
OCCASIONALLY ensureIndex(...);

Note: use ensureHaveIdIndex() for the _id index: it is faster.
Note: does nothing if collection does not yet exist.
*/
        static void ensureIndex(const char *ns, BSONObj keyPattern, bool unique, const char *name);

        /* fetch a single object from collection ns that matches query.
set your db SavedContext first.

@param requireIndex if true, complain if no index for the query. a way to guard against
writing a slow query.

@return true if object found
*/
        static bool findOne(const char *ns, BSONObj query, BSONObj& result, bool requireIndex = false);
        
        static bool findById(const char *ns, BSONObj query, BSONObj& result );

        static auto_ptr<CursorIterator> find( const char *ns , BSONObj query = BSONObj() , bool requireIndex = false );

        /* Get/put the first object from a collection. Generally only useful if the collection
only ever has a single object -- which is a "singleton collection".

You do not need to set the database before calling.
Returns: true if object exists.
*/
        static bool getSingleton(const char *ns, BSONObj& result);
        static void putSingleton(const char *ns, BSONObj obj);


        /* Remove all objects from a collection.
You do not need to set the database before calling.
*/
        static void emptyCollection(const char *ns);

    };

    class Database;

    /* Set database we want to use, then, restores when we finish (are out of scope)
Note this is also helpful if an exception happens as the state if fixed up.
*/
    class DBContext {
        Database *olddb;
        string oldns;
    public:
        DBContext(const char *ns) {
            olddb = cc().database();
            oldns = cc().ns();
            setClient(ns);
        }
        DBContext(string ns) {
            olddb = cc().database();
            oldns = cc().ns();
            setClient(ns.c_str());
        }

        /* this version saves the context but doesn't yet set the new one: */
        DBContext() {
            olddb = cc().database();
            oldns = cc().ns(); }


        ~DBContext() {
            cc().setns(oldns.c_str(), olddb);
        }
    };

    // manage a set using collection backed storage
    class DbSet {
    public:
        DbSet( const string &name = "", const BSONObj &key = BSONObj() ) :
            name_( name ),
            key_( key.getOwned() ) {
        }
        ~DbSet();
        void reset( const string &name = "", const BSONObj &key = BSONObj() );
        bool get( const BSONObj &obj ) const;
        void set( const BSONObj &obj, bool val );
    private:
        string name_;
        BSONObj key_;
    };
    
} // namespace mongo
Something went wrong with that request. Please try again.