Reputation: 3474
I just started with the Datastore and Objectify (complete newbie) and I am seeking for a bit of advice before modelling my entities.
I am developing some kind of stock trading application where the users can track stocks by defining the stock id, target price and condition (lower or higher price than the target).
The application fetches the stocks from an external website and keeps them in an array. It then needs to check which StockTracking is matching those Stocks prices kept in the array.
The StockTracking has this structure: StockCode, TargetPrice, Condition (lower or higher)
What I want to do is something like:
for (Stock in stockArray) {
SELECT * FROM StockTracking WHERE StockCode = stock.code AND
((Condition = 'Lower' AND TargetPrice <= Stock.price) OR
(Condition = 'Higher' AND TargetPrice >= Stock.price))
}
I need some help in determining how and which indexes should be created to be able to query like this. Also, although the number of stocks is fixed and not that big (under 50) I wonder if there's a way to perform this functionality in a more optimal way (ie. lower query times).
Thanks in advance
Upvotes: 1
Views: 1213
Reputation: 1404
Below is a Generic DAO Class from one of my work, where I have implemented almost all basic queries in Objectify. It is actually a Generic Implementation of basic Objectify functions using Template Classes.
package com.myweb.common.dao;
import static com.googlecode.objectify.ObjectifyService.ofy;
import java.util.List;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.ObjectifyService;
import com.myweb.common.exception.DatabaseException;
import com.myweb.model.User;
public abstract class AbstractGenericDaoImpl implements GenericDao{
static {
ObjectifyService.register(User.class);
}
@Override
public <T> void create(T t) {
ofy().save().entity(t).now();
}
@Override
public <T> String createWithKey(T t) {
Key<T> key = ofy().save().entity(t).now();
return key.getString();
}
@Override
public <T> Long createWithID(T t) {
Key<T> key = ofy().save().entity(t).now();
return key.getId();
}
@Override
public <T> void update(Class<T> clazz, Long id, T t) throws DatabaseException {
if (id == null) {
throw new DatabaseException("ID cannot be null");
}
T tnew = ofy().load().type(clazz).id(id).get();
ofy().save().entity(tnew).now();
}
@Override
public <T> void update(Class<T> clazz, String key, T t) throws DatabaseException {
if (key == null) {
throw new DatabaseException("ID cannot be null");
}
T tnew = ofy().load().type(clazz).id(key).get();
ofy().save().entity(tnew).now();
}
@Override
public <T> T getById(Class<T> clazz, Long id) throws DatabaseException {
if (id == null) {
throw new DatabaseException("ID cannot be null");
}
return ofy().load().type(clazz).id(id).get();
}
@Override
public <T> T getByKey(Class<T> clazz, String key) throws DatabaseException {
if (key == null) {
throw new DatabaseException("ID cannot be null");
}
return ofy().load().type(clazz).id(key).get();
}
@Override
public <T> List<T> list(Class<T> clazz) {
List<T> list = ofy().load().type(clazz).list();
return list;
}
@Override
public <T> void delete(Class<T> clazz, Long id) throws DatabaseException {
if (id == null) {
throw new DatabaseException("ID cannot be null");
}
T t = ofy().load().type(clazz).id(id).get();
if(t != null){
ofy().delete().entity(t).now();
}
}
@Override
public <T> void deleteByKey(Class<T> clazz, String key) throws DatabaseException {
if (key == null) {
throw new DatabaseException("ID cannot be null");
}
T t = ofy().load().type(clazz).id(key).get();
if(t != null){
ofy().delete().entity(t).now();
}
}
}
Below is a method which uses filter query example in Objectify.
public User getUserByEmail(String email) {
return ofy().load().type(User.class).filter("email", email).first().get();
}
Listing fields with Filter
public List<User> getUsers(String f1, String f2) {
return ofy().load().type(User.class).filter("f1", f1).filter("f2", f2).list();
}
Upvotes: 3