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

@IndexOptions not defaulting properly like deprecated @Index when using ensureIndexes(Class<T> clazz, boolean background) if background is true #791

Closed
dinosavings opened this issue May 30, 2015 · 7 comments
Labels
Milestone

Comments

@dinosavings
Copy link

Version 0.111

I migrated to the new IndexOptions but noticed that my indexes were not being created in background when using IndexOptions in conjunction with ensureClasses(classz, background) where background is true:

It appears that the background boolean is not taken into account.

See org.mongodb.morphia.DatastoreImpl#processClassAnnotations, specifically line 470.
ensureIndex(mc, dbColl, index.fields(), index.options());
background value is being ignored (i.e not used).

Everywhere else in the code use the following convention

index.background() ? index.background() : background

@dinosavings dinosavings changed the title IndexOptions not defaulting properly like deprecated Index when using ensureIndexes(Class<T> clazz, boolean background) when background is true IndexOptions not defaulting properly like deprecated Index when using ensureIndexes(Class<T> clazz, boolean background) if background is true Jun 1, 2015
@dinosavings dinosavings changed the title IndexOptions not defaulting properly like deprecated Index when using ensureIndexes(Class<T> clazz, boolean background) if background is true @IndexOptions not defaulting properly like deprecated @Index when using ensureIndexes(Class<T> clazz, boolean background) if background is true Jun 1, 2015
@evanchooly evanchooly modified the milestone: 1.1.0 Jun 4, 2015
@evanchooly evanchooly added the bug label Jun 15, 2015
@evanchooly
Copy link
Member

It gets extracted out in extractOptions()

Can you confirm me we're talking about the same thing here? It looks like there's no issue here.

@dinosavings
Copy link
Author

In my code, I did not specify the background value to true in the IndexOption annotation. Thus, in extractOptions, "background" will not be set in DBObject.

When calling ensureIndexes with background == true flag, this is not taken into consideration on line 470 like it does on line 478 (deprecated interface).

In other words, parameter background is totally ignored on line 470.

@evanchooly
Copy link
Member

Did you look at the link above? That's what this line check:

putIfTrue(opts, "background", options.background());

@dinosavings
Copy link
Author

The best way to explain the issue is to use a unit test (you need to modify the unit test the way you can grab mongo and datastore instances):

public class MongoDbContainerTest ...

// initialized...
private Mongo mongo;
private Datastore datastore;

public void testIndices() {

    final DB base = mongo.getDB("ut_test");
    datastore.delete(datastore.createQuery(TestWithIndexOption.class));

    final DBCollection indexOptionColl = base.getCollection(TestWithIndexOption.class.getSimpleName());
    indexOptionColl.drop();
    System.out.println("IndexOptionColl index after drop " + indexOptionColl.getIndexInfo());
    final DBCollection depIndexColl = base.getCollection(TestWithDeprecatedIndex.class.getSimpleName());
    depIndexColl.drop();
    System.out.println("DepIndexColl index after drop " + depIndexColl.getIndexInfo());

    datastore.ensureIndexes(TestWithIndexOption.class, true);
    System.out.println("IndexOptionColl index after ensure " + indexOptionColl.getIndexInfo());

    datastore.ensureIndexes(TestWithDeprecatedIndex.class, true);
    System.out.println("DepIndexColl index after ensure " + depIndexColl.getIndexInfo());

}

@Entity(noClassnameStored = true)
@Indexes({
    @Index(
        options = @IndexOptions(),
        fields = {
            @Field(value = "name")
        })
})
public static class TestWithIndexOption {

    private String name;

}

@Entity(noClassnameStored = true)
@Indexes({@Index("name")})
public static class TestWithDeprecatedIndex {

    private String name;

}

}

Here's the output:

IndexOptionColl index after drop []
DepIndexColl index after drop []
IndexOptionColl index after ensure [{ "v" : 1 , "key" : { "_id" : 1} , "name" : "id" , "ns" : "ut_base.TestWithIndexOption"}, { "v" : 1 , "key" : { "name" : 1} , "name" : "name_1" , "ns" : "ut_base.TestWithIndexOption"}]
Jul 06, 2015 12:34:03 PM org.mongodb.morphia.DatastoreImpl processClassAnnotations
WARNING: This index on 'estalea.base.setup.springframework.mongo.MongoDbContainerTest$TestWithDeprecatedIndex' is using deprecated configuration options. Please update to use the fields value on @Index: @org.mongodb.morphia.annotations.Index(expireAfterSeconds=-1, dropDups=false, sparse=false, background=false, unique=false, disableValidation=false, name=, [email protected](expireAfterSeconds=-1, dropDups=false, languageOverride=, sparse=false, background=false, unique=false, disableValidation=false, userDefined=false, name=, language=), fields=[], value=name)
DepIndexColl index after ensure [{ "v" : 1 , "key" : { "_id" : 1} , "name" : "id" , "ns" : "ut_base.TestWithDeprecatedIndex"}, { "v" : 1 , "key" : { "name" : 1} , "name" : "name_1" , "ns" : "ut_base.TestWithDeprecatedIndex" , "background" : true}]

As you can see, the deprecated usage of Index generates a "background": true. Whereas the IndexOptions does not.

@evanchooly
Copy link
Member

oooooh. I get it. Ironically, I'm working on that code now and completely glossed over the method parameter. I get what the issue is now. Thanks.

@evanchooly
Copy link
Member

This should cover this issue: https://github.com/mongodb/morphia/blob/master/morphia/src/test/java/org/mongodb/morphia/indexes/TestIndexes.java#L37-37. Do you see anything I missed? I think I have those holes plugged now.

@dinosavings
Copy link
Author

Perfect. I spaced out and forgot to write the actual assert!

On Mon, Jul 6, 2015 at 1:27 PM, Justin Lee [email protected] wrote:

This should cover this issue:
https://github.com/mongodb/morphia/blob/master/morphia/src/test/java/org/mongodb/morphia/indexes/TestIndexes.java#L37-37.
Do you see anything I missed? I think I have those holes plugged now.


Reply to this email directly or view it on GitHub
#791 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants