Skip to content
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

Add support for partial indexes #1303

Merged
merged 3 commits into from
Jan 13, 2016
Merged

Add support for partial indexes #1303

merged 3 commits into from
Jan 13, 2016

Conversation

alcaeus
Copy link
Member

@alcaeus alcaeus commented Dec 11, 2015

MongoDB 3.2 adds support for partial indexes. This PR adds support for adding partialIndexExpression options to the index specification.

Examples:

  • YAML:
  indexes:
    index1:
      keys:
        username: desc
      options:
        unique: true
        dropDups: false
        partialFilterExpression:
          version: { $gt: 1 }
          discr: { $eq: 'default' }
  • XML:
<indexes>
    <index unique="true">
        <key name="username" order="desc" />
        <option name="dropDups" value="false" />
        <partialFilterExpression>
            <field name="version" value="1" operator="gt" />
            <field name="discr" operator="eq" value="default" />
        </partialFilterExpression>
    </index>
</indexes>
  • Annotations:
/**
 * @ODM\Indexes(
 *     @ODM\Index(keys={"username"="asc"},dropDups=true,partialFilterExpression={"counter"={"$gt"=5}})
 * )
 */

List of TODOs:

  • Figure out how to properly map $and in XML
  • Ensure SchemaManager::isMongoIndexEquivalentToDocumentIndex() works properly
  • You can always add more tests

@alcaeus
Copy link
Member Author

alcaeus commented Jan 6, 2016

Long time no see, I've updated the XML mapping. The example in the comment above outlines the straight-forward mapping. For more complicated mappings, there are additional possiblilities. You can either check properties of embedded documents (checking only those properties mentioned):

<partialFilterExpression>
    <field name="nested">
        <field name="count" operator="gt" value="0" />
        <field name="test" value="foo" />
    </field>
</partialFilterExpression>

Or check a document for equality (ensuring that a document looks exactly like what's provided):

<partialFilterExpression>
    <field name="object" operator="eq">
        <field name="property" value="foo" />
        <field name="count" value="bleh" />
    </field>
</partialFilterExpression>

With this it's also possible to use the $and operator. MongoDB supports this operator only at the root level:

<partialFilterExpression>
    <and>
        <field name="nested">
            <field name="count" operator="gt" value="0" />
            <field name="test" value="foo" />
        </field>
    </and>
    <and>
        <field name="object" operator="eq">
            <field name="property" value="foo" />
            <field name="count" value="bleh" />
        </field>
    </and>
</partialFilterExpression>

When using and, you cannot add any field mappings at the root level. You also can't nest the and operator in a field mapping.

@alcaeus alcaeus added this to the 1.1 milestone Jan 6, 2016
@alcaeus alcaeus changed the title [WIP] Add support for partial indexes Add support for partial indexes Jan 9, 2016
@@ -237,6 +237,9 @@ private function addIndex(ClassMetadataInfo $class, $index, array $keys = array(
$options[$name] = $index->$name;
}
}
if (! empty($index->partialFilterExpression)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing space before ! ;)

@malarzm
Copy link
Member

malarzm commented Jan 10, 2016

I think we should have tests for expression with and since XML driver has a check on that?

@alcaeus
Copy link
Member Author

alcaeus commented Jan 10, 2016

@malarzm Good idea, I'll add a few extra documents to be safe.

In this case, we treat an empty filter expression like a missing one to avoid recreating indexes because of differences in what's defined as "empty".
@alcaeus
Copy link
Member Author

alcaeus commented Jan 13, 2016

@malarzm Added tests for various filter expressions being loaded via XML and YAML to ensure they are the same. Invalid filter expressions will only be fixed by the XML driver as YAML and annotations are schemaless and will just let the database throw a fit if the expression is invalid.

@malarzm
Copy link
Member

malarzm commented Jan 13, 2016

👍

malarzm added a commit that referenced this pull request Jan 13, 2016
@malarzm malarzm merged commit 9b6aac9 into doctrine:master Jan 13, 2016
@alcaeus alcaeus deleted the partial-index-support branch January 13, 2016 21:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants