-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Index Support #51
Closed
Closed
Index Support #51
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,7 @@ Mongothon.egg-info | |
build | ||
dist | ||
env | ||
#*# | ||
*# | ||
.#* | ||
*~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,39 @@ | ||
from bson.objectid import ObjectId | ||
import pymongo | ||
import schemer | ||
|
||
|
||
class IndexSpec(object): | ||
def __init__(self, name, key_spec, **kwargs): | ||
self.name = name | ||
self.key_spec = key_spec | ||
self.kwargs = kwargs | ||
|
||
def apply_to(self, collection): | ||
collection.create_index(self.key_spec, name=self.name, **self.kwargs) | ||
|
||
def validate(self): | ||
if not self.name: | ||
raise ValueError("Must specify a non-nil name for every index") | ||
if not self.key_spec: | ||
raise ValueError("Must specify the actual index for {}".format(self.name)) | ||
for name, index_type in self.key_spec: | ||
if index_type not in {pymongo.ASCENDING, pymongo.DESCENDING, pymongo.HASHED}: | ||
raise ValueError('Unsupported Index Type {} for {}'.format(index_type, self.name)) | ||
return self | ||
|
||
|
||
|
||
class Schema(schemer.Schema): | ||
"""A Schema encapsulates the structure and constraints of a Mongo document.""" | ||
|
||
def __init__(self, doc_spec, **kwargs): | ||
indexes = [] | ||
|
||
def __init__(self, doc_spec, indexes=[], **kwargs): | ||
super(Schema, self).__init__(doc_spec, **kwargs) | ||
|
||
# Every mongothon schema should expect an ID field. | ||
if '_id' not in self._doc_spec: | ||
self._doc_spec['_id'] = {"type": ObjectId} | ||
|
||
self.indexes = [i.validate() for i in indexes] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from mongothon.schema import Schema, IndexSpec | ||
from mock import Mock | ||
import unittest | ||
|
||
class TestSchema(unittest.TestCase): | ||
def test_indexes(self): | ||
Schema({}, indexes=[IndexSpec('myindex', [('key', 1)])]) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Set
indexes=None
. See http://effbot.org/zone/default-values.htm