Adding contains method to NoSqlEntityManager #14

Closed
baank opened this Issue Nov 2, 2012 · 12 comments

Projects

None yet

3 participants

@baank

I don't know the best way to do this but it would be nice if there was a method in NoSqlEntityManager:

public boolean contains(Class entityType, String key, String value)

That didn't load in any data.

@deanhiller
Owner

I am not sure at all what that function does? You say "That didn't load in any data" so you mean do not read from the database? and where is the key stored and this value, where am I supposed to check for those? Can you explain a bit more here?

@baank

So an example would be:

boolean emailExists = NoSql.em().contains(User.class, "emailAddress", "joe@smith.com")

And it would only check if the email exists not load any of the other attributes for the User class e.g. name, age etc.

@deanhiller
Owner

There is no row key here though? If you are trying to answer the question is there a user with emailAddress=joe@smith.com, would you mind if that was done like so..... select count(u) from User as u where u.emailAddress = 'joe@smith.com' because I can add that AND it would only scan the index so it would not load the entire user object.

@baank

That looks great. Maybe instead of contains it could also be:

NoSql.em().count(User.class, "emailAddress", "joe@smith.com")

That way it could be generic and used in other situations. Either way is great.

@deanhiller
Owner

hmmmm, I am rethinking this right now.....currently, we could just use the index and I believe you may be able to do this with today's api...it would only read in the primary key of the user itself. If you use TypedSession.runQuery("select t from User as t where t.emailAddress = 'joe@smith.com') it does NOT run the query but returns and object that you iterate over.....well, actually returns 1 object and you can request one of 3 iterators. Anyways, I am wondering if we can put an abstraction on top of that to do what you want but in a general way for all users in different situations.

Vikas, I would love to hear your thoughts here as well???

@easility
Collaborator

Yes, I agree with Dean as we have added support to query on primary/row key as well, it makes sense to use the existing APIs and then see if it is returning anything.

Dean: Here, I am sure, you meant NoSqlTypedSession.createQueryCursor(sql,batchsize) as there is no API like runQuery..

~Vikas

@deanhiller
Owner

yup, that would be the one......any ideas how you can get taligent something he can use in this regard?

thanks,
Dean

@easility
Collaborator

So, you will have to take latest code from master branch if you want to query on primary key(rowkey). Please note that you can still query on primary key or on an indexed column... you can do use any of the 3 iterator as Dean mentioned and do something like:

String sql = "select * from User where emailAddress = \"joe@smith.com\"";
QueryResult result = nosqlTypedSession.createQueryCursor(cmd, 100);

if (result.getAllViewsIter().iterator().hasNext())
//found it
OR

if (result.getAllViewsCursor().next())
//found it
OR
if (result.getPrimaryViewIter().iterator().hasNext())
//found it

@deanhiller
Owner

thanks for adding that exact code. there is one better method that reads in no rows which is using that same result object above, call result.getCursor() which gives the index cursor so no rows are read and only the index is read. It returns the IndexColumnInfo. If anything exists you know your user exists all without reading the row. hmmmmm, any ideas on what to do with this issue from a more usable perspective or should we close it?

@baank

It would be nice to simplify it for the user as for most basic apps there is no need to access the cursor.

@deanhiller
Owner

ok, I am trying to figure out if this belongs on the EntityManager(ie. JPA has no count method on the EntityManager) or maybe we just throw it on TypedSession so it is NoSql.em().getTypedSession().count(User.class, "emailAddress", "myemail");......in fact, I think that might be best. Are you good with that solution? If so, I think we will proceed once the play 2.x plugin is complete.

@easility
Collaborator

One method is added in NoSqlTypedSession
public int count(String columnFamily, String indexedColName, Object value)

I hope that will solve the purpose. I am closing this issue as of now. However, please re-open it if you face any issue in using that.

@easility easility closed this Dec 12, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment