Beispiel #1
0
def example():
    mongo = yield txmongo.MongoConnection()

    foo = mongo.foo  # `foo` database
    test = foo.test  # `test` collection

    idx = filter.sort(filter.ASCENDING("something") + filter.DESCENDING("else"))
    print "IDX:", idx

    result = yield test.create_index(idx)
    print "create_index:", result

    result = yield test.index_information()
    print "index_information:", result

    result = yield test.drop_index(idx)
    print "drop_index:", result

    # Geohaystack example
    geoh_idx = filter.sort(filter.GEOHAYSTACK("loc") + filter.ASCENDING("type"))
    print "IDX:", geoh_idx
    result = yield test.create_index(geoh_idx, **{'bucketSize':1})
    print "index_information:", result

    result = yield test.drop_index(geoh_idx)
    print "drop_index:", result

    # 2D geospatial index
    geo_idx = filter.sort(filter.GEO2D("pos"))
    print "IDX:", geo_idx
    result = yield test.create_index(geo_idx, **{ 'min':-100, 'max':100 })
    print "index_information:", result

    result = yield test.drop_index(geo_idx)
    print "drop_index:", result
Beispiel #2
0
    def test_index_info(self):
        db = self.db

        yield db.test.drop_indexes()
        yield db.test.remove({})

        db.test.save({})  # create collection
        ix_info = yield db.test.index_information()
        self.assertEqual(len(ix_info), 1)
        self.assertEqual(ix_info["_id_"]["name"], "_id_")

        yield db.test.create_index(qf.sort(qf.ASCENDING("hello")))
        ix_info = yield db.test.index_information()
        self.assertEqual(len(ix_info), 2)
        self.assertEqual(ix_info["hello_1"]["name"], "hello_1")

        yield db.test.create_index(
            qf.sort(qf.DESCENDING("hello") + qf.ASCENDING("world")),
            unique=True,
            sparse=True)
        ix_info = yield db.test.index_information()
        self.assertEqual(ix_info["hello_1"]["name"], "hello_1")
        self.assertEqual(len(ix_info), 3)
        self.assertEqual({
            "hello": -1,
            "world": 1
        }, ix_info["hello_-1_world_1"]["key"])
        self.assertEqual(True, ix_info["hello_-1_world_1"]["unique"])
        self.assertEqual(True, ix_info["hello_-1_world_1"]["sparse"])

        yield db.test.drop_indexes()
        yield db.test.remove({})
Beispiel #3
0
    def test_index_info(self):
        db = self.db

        yield db.test.drop_indexes()
        yield db.test.remove({})

        db.test.save({})  # create collection
        ix_info = yield db.test.index_information()
        self.assertEqual(len(ix_info), 1)

        self.assert_("_id_" in ix_info)

        yield db.test.create_index(filter.sort(filter.ASCENDING("hello")))
        ix_info = yield db.test.index_information()
        self.assertEqual(len(ix_info), 2)

        self.assertEqual(ix_info["hello_1"], [("hello", 1)])

        yield db.test.create_index(filter.sort(
            filter.DESCENDING("hello") + filter.ASCENDING("world")),
                                   unique=True)
        ix_info = yield db.test.index_information()

        self.assertEqual(ix_info["hello_1"], [("hello", 1)])
        self.assertEqual(len(ix_info), 3)
        self.assertEqual([("world", 1), ("hello", -1)],
                         ix_info["hello_-1_world_1"])
        # Unique key will not show until index_information is updated with changes introduced in version 1.7
        #self.assertEqual(True, ix_info["hello_-1_world_1"]["unique"])

        yield db.test.drop_indexes()
        yield db.test.remove({})
Beispiel #4
0
 def render_GET(self, request):
     def handle_posts(posts):
         context = {'posts': posts}
         request.write(render_response('posts.html', context))
         request.finish()
     f = _filter.sort(_filter.DESCENDING('date'))
     deferred = _db.posts.find(filter=f)
     deferred.addCallback(handle_posts)
     return NOT_DONE_YET
Beispiel #5
0
    def test_FilterMerge(self):
        self.assertEqual(
            qf.sort(qf.ASCENDING('x') + qf.DESCENDING('y')),
            qf.sort(qf.ASCENDING('x')) + qf.sort(qf.DESCENDING('y')))

        comment = "hello world"

        yield self.db.command("profile", 2)
        yield self.coll.find({},
                             filter=qf.sort(qf.ASCENDING('x')) +
                             qf.comment(comment))
        yield self.db.command("profile", 0)

        cnt = yield self.db.system.profile.count({
            "query.$orderby.x": 1,
            "query.$comment": comment
        })
        self.assertEqual(cnt, 1)
Beispiel #6
0
    def test_FilterMerge(self):
        self.assertEqual(
            qf.sort(qf.ASCENDING('x') + qf.DESCENDING('y')),
            qf.sort(qf.ASCENDING('x')) + qf.sort(qf.DESCENDING('y')))

        comment = "hello world"

        yield self.db.command("profile", 2)
        yield self.coll.find({},
                             filter=qf.sort(qf.ASCENDING('x')) +
                             qf.comment(comment))
        yield self.db.command("profile", 0)

        if (yield self.__3_2_or_higher()):
            profile_filter = {"query.sort.x": 1, "query.comment": comment}
        else:
            profile_filter = {"query.$orderby.x": 1, "query.$comment": comment}
        cnt = yield self.db.system.profile.count(profile_filter)
        self.assertEqual(cnt, 1)
Beispiel #7
0
    def test_create_index(self):
        db = self.db
        coll = self.coll

        self.assertFailure(coll.create_index(5), TypeError)
        self.assertFailure(coll.create_index({"hello": 1}), TypeError)

        yield coll.insert({'c': 1})  # make sure collection exists.

        yield coll.drop_indexes()
        count = yield db.system.indexes.count({"ns": u"mydb.mycol"})
        self.assertEqual(count, 1)
        self.assertIsInstance(count, int)

        yield coll.create_index(qf.sort(qf.ASCENDING("hello")))
        yield coll.create_index(
            qf.sort(qf.ASCENDING("hello") + qf.DESCENDING("world")))

        count = yield db.system.indexes.count({"ns": u"mydb.mycol"})
        self.assertEqual(count, 3)

        yield coll.drop_indexes()
        ix = yield coll.create_index(
            qf.sort(qf.ASCENDING("hello") + qf.DESCENDING("world")),
            name="hello_world")
        self.assertEquals(ix, "hello_world")

        yield coll.drop_indexes()
        count = yield db.system.indexes.count({"ns": u"mydb.mycol"})
        self.assertEqual(count, 1)

        yield coll.create_index(qf.sort(qf.ASCENDING("hello")))
        indices = yield db.system.indexes.find({"ns": u"mydb.mycol"})
        self.assert_(u"hello_1" in [a["name"] for a in indices])

        yield coll.drop_indexes()
        count = yield db.system.indexes.count({"ns": u"mydb.mycol"})
        self.assertEqual(count, 1)

        ix = yield coll.create_index(
            qf.sort(qf.ASCENDING("hello") + qf.DESCENDING("world")))
        self.assertEquals(ix, "hello_1_world_-1")
Beispiel #8
0
    def test_create_index(self):
        db = self.db
        coll = self.coll

        self.assertRaises(TypeError, coll.create_index, 5)
        self.assertRaises(TypeError, coll.create_index, {"hello": 1})

        yield coll.insert({'c': 1})  # make sure collection exists.

        yield coll.drop_indexes()
        count = len((yield coll.index_information()))
        self.assertEqual(count, 1)
        self.assertIsInstance(count, int)

        yield coll.create_index(qf.sort(qf.ASCENDING("hello")))
        yield coll.create_index(
            qf.sort(qf.ASCENDING("hello") + qf.DESCENDING("world")))

        count = len((yield coll.index_information()))
        self.assertEqual(count, 3)

        yield coll.drop_indexes()
        ix = yield coll.create_index(
            qf.sort(qf.ASCENDING("hello") + qf.DESCENDING("world")),
            name="hello_world")
        self.assertEquals(ix, "hello_world")

        yield coll.drop_indexes()
        count = len((yield coll.index_information()))
        self.assertEqual(count, 1)

        yield coll.create_index(qf.sort(qf.ASCENDING("hello")))
        indices = yield coll.index_information()
        self.assert_(u"hello_1" in indices)

        yield coll.drop_indexes()
        count = len((yield coll.index_information()))
        self.assertEqual(count, 1)

        ix = yield coll.create_index(
            qf.sort(qf.ASCENDING("hello") + qf.DESCENDING("world")))
        self.assertEquals(ix, "hello_1_world_-1")
Beispiel #9
0
    def test_Sort(self):
        doc = yield self.coll.find_one_and_delete({'x': {"$exists": True}},
                                                  sort=qf.sort(qf.ASCENDING('y')))
        self.assertEqual(doc['x'], 1)

        doc = yield self.coll.find_one_and_delete({'x': {"$exists": True}},
                                                  sort=qf.sort(qf.DESCENDING('y')))
        self.assertEqual(doc['x'], 3)

        cnt = yield self.coll.count()
        self.assertEqual(cnt, 1)
Beispiel #10
0
    def test_create_index(self):
        db = self.db
        coll = self.coll

        self.assertRaises(TypeError, coll.create_index, 5)
        self.assertRaises(TypeError, coll.create_index, {"hello": 1})

        yield coll.insert({'c': 1})  # make sure collection exists.

        yield coll.drop_indexes()
        count = yield db.system.indexes.count({"ns": u"mydb.mycol"})
        self.assertEqual(count, 1)

        result1 = yield coll.create_index(
            filter.sort(filter.ASCENDING("hello")))
        result2 = yield coll.create_index(filter.sort(filter.ASCENDING("hello") + \
                                          filter.DESCENDING("world")))

        count = yield db.system.indexes.count({"ns": u"mydb.mycol"})
        self.assertEqual(count, 3)

        yield coll.drop_indexes()
        ix = yield coll.create_index(filter.sort(filter.ASCENDING("hello") + \
                                   filter.DESCENDING("world")), name="hello_world")
        self.assertEquals(ix, "hello_world")

        yield coll.drop_indexes()
        count = yield db.system.indexes.count({"ns": u"mydb.mycol"})
        self.assertEqual(count, 1)

        yield coll.create_index(filter.sort(filter.ASCENDING("hello")))
        indices = yield db.system.indexes.find({"ns": u"mydb.mycol"})
        self.assert_(u"hello_1" in [a["name"] for a in indices])

        yield coll.drop_indexes()
        count = yield db.system.indexes.count({"ns": u"mydb.mycol"})
        self.assertEqual(count, 1)

        ix = yield coll.create_index(filter.sort(filter.ASCENDING("hello") + \
                                   filter.DESCENDING("world")))
        self.assertEquals(ix, "hello_1_world_-1")
Beispiel #11
0
    def test_drop_index(self):
        index = qf.sort(qf.ASCENDING("hello") + qf.DESCENDING("world"))

        yield self.coll.create_index(index, name="myindex")
        res = yield self.coll.drop_index("myindex")
        self.assertEqual(res["ok"], 1)

        yield self.coll.create_index(index)
        res = yield self.coll.drop_index(index)
        self.assertEqual(res["ok"], 1)

        self.assertRaises(TypeError, self.coll.drop_index, 123)
Beispiel #12
0
    def test_Sort(self):
        doc = yield self.coll.find_one_and_update({}, {"$set": {'y': 5}},
                                                  projection={"_id": 0},
                                                  sort=qf.sort(qf.ASCENDING('y')))
        self.assertEqual(doc, {'x': 10, 'y': 10})

        doc = yield self.coll.find_one_and_update({}, {"$set": {'y': 35}},
                                                  projection={"_id": 0},
                                                  sort=qf.sort(qf.DESCENDING('y')))
        self.assertEqual(doc, {'x': 30, 'y': 30})

        ys = yield self.coll.distinct('y')
        self.assertEqual(set(ys), {5, 20, 35})
Beispiel #13
0
    def render_GET(self, request):
        session = request.getSession()
        if session.uid not in sessions:
            request.redirect('/login')
            return ''

        def handle_posts(posts):
            context = {'posts': posts}
            request.write(render_response('read.html', context))
            request.finish()
        f = _filter.sort(_filter.DESCENDING('date'))
        d = _db.posts.find(filter=f)
        d.addCallback(handle_posts)
        return NOT_DONE_YET
Beispiel #14
0
def get_sorted_user_score_iter(campaign_id, timestamp, limit):
    """

    :param campaign_id:
    :param timestamp:
    :param limit:
    :return: Descending by score sorted list of user score to limit.
    """
    timestamp = common_utils.timestamp2hour(timestamp)
    collection = yield db.get_user_score_collection()

    defer.returnValue(QueryIterator(collection.find({'campaign_id': campaign_id, 'timestamp': timestamp},
                                                    sort=txfilter.sort(txfilter.DESCENDING("score")),
                                                    limit=limit, cursor=True)))
Beispiel #15
0
def example():
    mongo = yield txmongo.MongoConnection()

    foo = mongo.foo  # `foo` database
    test = foo.test  # `test` collection

    idx = filter.sort(
        filter.ASCENDING("something") + filter.DESCENDING("else"))
    print "IDX:", idx

    result = yield test.create_index(idx)
    print "create_index:", result

    result = yield test.index_information()
    print "index_information:", result

    result = yield test.drop_index(idx)
    print "drop_index:", result