android - Cloud Console 数据存储过滤器

标签 android google-app-engine google-cloud-datastore

我最近从 JDO 切换到 Objectify 以简化我的一些后端(我是 App Engine 和服务器端一般的初学者)。

我有一个实体 AppVersion,它在 Cloud Console 中以前看起来像这样: enter image description here

当我切换到 objectify 时,它不再具有按 minVersionRequired 过滤的选项,看起来像这样: enter image description here

实体代码(之前)

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class AppVersion {

@Id
private String applicationName;
private int minVersionRequired;

public String getApplicationName() {
    return applicationName;
}
public int getMinVersionRequired() {
    return minVersionRequired;
}
public void setApplicationName(String applicationName) {
    this.applicationName = applicationName;
}
public void setminVersionRequired(int minVersionRequired) {
    this.minVersionRequired = minVersionRequired;
}
}

实体代码(之后)

import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;

@Entity
public class AppVersion {

@Id
private String applicationName;
private int minVersionRequired;

public String getApplicationName() {
    return applicationName;
}
public int getMinVersionRequired() {
    return minVersionRequired;
}
public void setApplicationName(String applicationName) {
    this.applicationName = applicationName;
}
public void setminVersionRequired(int minVersionRequired) {
    this.minVersionRequired = minVersionRequired;
}
}

端点代码(之前)请注意,这是在 Eclipse 中自动生成的

import com.companionfree.zooperthemeviewer.EMF;

import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.ApiNamespace;
import com.google.api.server.spi.response.CollectionResponse;
import com.google.appengine.api.datastore.Cursor;
import com.google.appengine.datanucleus.query.JPACursorHelper;

import java.util.List;

import javax.annotation.Nullable;
import javax.inject.Named;
import javax.persistence.EntityExistsException;
import javax.persistence.EntityNotFoundException;
import javax.persistence.EntityManager;
import javax.persistence.Query;

@Api(name = "appversionendpoint", namespace = @ApiNamespace(ownerDomain = "company.com", ownerName = "company.com", packagePath = "app"))
public class AppVersionEndpoint {

/**
 * This method lists all the entities inserted in datastore.
 * It uses HTTP GET method and paging support.
 *
 * @return A CollectionResponse class containing the list of all entities
 * persisted and a cursor to the next page.
 */
@SuppressWarnings({ "unchecked", "unused" })
@ApiMethod(name = "listAppVersion")
public CollectionResponse<AppVersion> listAppVersion(
        @Nullable @Named("cursor") String cursorString,
        @Nullable @Named("limit") Integer limit) {

    EntityManager mgr = null;
    Cursor cursor = null;
    List<AppVersion> execute = null;

    try {
        mgr = getEntityManager();
        Query query = mgr
                .createQuery("select from AppVersion as AppVersion");
        if (cursorString != null && cursorString != "") {
            cursor = Cursor.fromWebSafeString(cursorString);
            query.setHint(JPACursorHelper.CURSOR_HINT, cursor);
        }

        if (limit != null) {
            query.setFirstResult(0);
            query.setMaxResults(limit);
        }

        execute = (List<AppVersion>) query.getResultList();
        cursor = JPACursorHelper.getCursor(execute);
        if (cursor != null)
            cursorString = cursor.toWebSafeString();

        // Tight loop for fetching all entities from datastore and accomodate
        // for lazy fetch.
        for (AppVersion obj : execute)
            ;
    } finally {
        mgr.close();
    }

    return CollectionResponse.<AppVersion> builder().setItems(execute)
            .setNextPageToken(cursorString).build();
}

/**
 * This method gets the entity having primary key id. It uses HTTP GET method.
 *
 * @param id the primary key of the java bean.
 * @return The entity with primary key id.
 */
@ApiMethod(name = "getAppVersion")
public AppVersion getAppVersion(@Named("id") String id) {
    EntityManager mgr = getEntityManager();
    AppVersion appversion = null;
    try {
        appversion = mgr.find(AppVersion.class, id);
    } finally {
        mgr.close();
    }
    return appversion;
}

/**
 * This inserts a new entity into App Engine datastore. If the entity already
 * exists in the datastore, an exception is thrown.
 * It uses HTTP POST method.
 *
 * @param appversion the entity to be inserted.
 * @return The inserted entity.
 */
@ApiMethod(name = "insertAppVersion")
public AppVersion insertAppVersion(AppVersion appversion) {
    EntityManager mgr = getEntityManager();
    try {
        if (containsAppVersion(appversion)) {
            throw new EntityExistsException("Object already exists");
        }
        mgr.persist(appversion);
    } finally {
        mgr.close();
    }
    return appversion;
}

/**
 * This method is used for updating an existing entity. If the entity does not
 * exist in the datastore, an exception is thrown.
 * It uses HTTP PUT method.
 *
 * @param appversion the entity to be updated.
 * @return The updated entity.
 */
@ApiMethod(name = "updateAppVersion")
public AppVersion updateAppVersion(AppVersion appversion) {
    EntityManager mgr = getEntityManager();
    try {
        if (!containsAppVersion(appversion)) {
            throw new EntityNotFoundException("Object does not exist");
        }
        mgr.persist(appversion);
    } finally {
        mgr.close();
    }
    return appversion;
}

/**
 * This method removes the entity with primary key id.
 * It uses HTTP DELETE method.
 *
 * @param id the primary key of the entity to be deleted.
 */
@ApiMethod(name = "removeAppVersion")
public void removeAppVersion(@Named("id") String id) {
    EntityManager mgr = getEntityManager();
    try {
        AppVersion appversion = mgr.find(AppVersion.class, id);
        mgr.remove(appversion);
    } finally {
        mgr.close();
    }
}

private boolean containsAppVersion(AppVersion appversion) {
    EntityManager mgr = getEntityManager();
    boolean contains = true;
    try {
        AppVersion item = mgr.find(AppVersion.class,
                appversion.getApplicationName());
        if (item == null) {
            contains = false;
        }
    } finally {
        mgr.close();
    }
    return contains;
}

private static EntityManager getEntityManager() {
    return EMF.get().createEntityManager();
}

}

端点代码(之后)注意这是我在 Android Studio 中创建的

import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.ApiNamespace;
import com.google.api.server.spi.response.CollectionResponse;
import java.util.List;

import javax.inject.Named;

import static com.company.backend.OfyService.ofy;

@Api(name = "appversionendpoint", version = "v1", namespace =
@ApiNamespace(ownerDomain = "backend.company.com",
    ownerName = "backend.company.com", packagePath = ""))
public class AppVersionEndpoint {

@ApiMethod(name = "listAppVersion")
public CollectionResponse<AppVersion> listAppVersion() {
    List<AppVersion> execute;
   execute =  ofy().load().type(AppVersion.class).list();
return CollectionResponse.<AppVersion> builder().setItems(execute).build();

}


/**
 * This method gets the entity having primary key id. It uses HTTP GET method.
 *
 * @param id the primary key of the java bean.
 * @return The entity with primary key id (null if DNE).
 */
@ApiMethod(name = "getAppVersion")
public AppVersion getAppVersion(@Named("id") String id) {
    return ofy().load().type(AppVersion.class).id(id).now();

}

/**
 * This inserts a new entity into App Engine datastore. If the entity already
 * exists in the datastore, an exception is thrown.
 * It uses HTTP POST method.
 *
 * @param appversion the entity to be inserted.
 * @return The inserted entity.
 */
@ApiMethod(name = "insertAppVersion")
public AppVersion insertAppVersion(AppVersion appversion) {
    AppVersion exist = getAppVersion(appversion.getApplicationName());
    AppVersion result;
    if (exist == null) {
        ofy().save().entity(appversion).now();
        result = getAppVersion(appversion.getApplicationName());
    } else {
        throw new IllegalArgumentException(appversion.getApplicationName() + " exists already");
    }
    return result;
}
}

我更希望它像原来那样可过滤,但我不知道为什么会有所不同。谁能帮我填一下?

最佳答案

这里有两件事:首先,默认情况下,Objectify 假设您不想为您的类的属性编制索引(这使您的数据存储索引保持精简和平均)。其次,我认为新 Datastore 控制台的筛选器 UI​​ 仅显示具有与之关联的索引的属性(因为您无法筛选未索引的属性)。

因此,如果您希望能够按 minVersionRequired 进行查询或排序,只需将 @Index 注释添加到您的 POJO 中的该字段,Objectify 将使用 setIndexedProperty() 低级数据存储 API 中底层实体类中的方法。

如果你想默认索引你类中的所有属性,你可以在类上加上 @Index 注释,然后 @Unindex 任何你特别不喜欢的不想编入索引。

关于android - Cloud Console 数据存储过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24255226/

相关文章:

android - 我们如何使用 CameraX 获取预览帧?

google-app-engine - 使用 GAE 检测/重定向不支持 SNI 的浏览器

Java、Google App Engine 和 JSONArray/JSONObject

python - 迁移到 GAE

java - App Engine 应用程序性能测试

google-app-engine - 将 Google Datastore 备份从数据存储加载到 Google BigQuery

android - LibGDX 的 TexturePacker 与 gradle

java - 应用程序关闭后广播接收器仍在运行 - Android

java - (Android+Retrofit 2) - 将静态 header 与拦截器一起使用是否有缺点?

python-2.7 - 导入错误 : no module named pwd in app engine