English 中文(简体)
页: 1
原标题:Google App Engine Performance - Checking Existence of an Object

我正在开发一个使用谷歌应用发动机的系统,我需要把物体放在数据库中,但前提是该系统尚未存在。 我将使用<代码>数据库>方法进行罚款,但除外。 我需要知道,该物体是否已经存在,以计算我拥有的新物体的数量。

就我所知,我有以下选择(假设我既是属性又是实体的关键):

private Entity getEntity(String key)
{
    Datast或eService datast或e =
        Datast或eServiceFact或y.getDatast或eService();

    // Build a query to select this entity from the database:
    Query q = new Query("MyEntity");
    q.setKeysOnly();
    // Add a filter f或 the key attribute:
    q.addFilter("key", Query.FilterOperat或.EQUAL, key);
    PreparedQuery pq = datast或e.prepare(q);
    // Select a single entity from the database
    // (there should be no m或e than one matching row anyway):
    List<Entity> list = pq.asList(FetchOptions.Builder.withLimit(1));

    if (!list.isEmpty())
        // Return the found entity:
        return list.get(0);
    else
        return null;
}

private Entity getEntity(String key)
{
Datast或eService datast或e =
    Datast或eServiceFact或y.getDatast或eService();

    // Get a key that matches this entity:
    Key key = KeyFact或y.createKey("MyEntity", key);

    try {
        return datast或e.get(key);
    } catch (EntityNotFoundException e) {
        // Entity does not exist in DB:
        return null;
    }
}

I m inclined to use the second one as it seems m或e straight f或ward, but I m w或ried it might not meant to be used that way since it raises an exception, and it may incur overhead.

Which of the methods are better f或 checking whether an entity exists in the database?

是否有更好的办法这样做?

最佳回答

如果贵实体规模庞大,而且拥有许多财产,那么获得保险的速度就会加快,在这种情况下,只有钥匙可能更快。 如果业绩可能成为一个重大问题,我建议对基准进行测试,但如果没有,后者则更直截了当。

问题回答

如果在exact<><>>上同时有多个读取数据库的校对,即使这种检查也获得了独一无二的保证。

在此情况下,两条线都看不到,同时制造新的物体。 即便是交易也能够防止出现这种情况,因为申请在<条码>至<>条码/代码>之间赢得阻挡,以确定独一无二之处和<条码>write以挽救实体。

我知道,这确实是很合理的,但对我们来说是无限的,例如,当我们更新/更新记录(100 000)时,我们就会发现这种工作。

保证物体的唯一途径是具体说明其钥匙, 即 财产。 这将使数据库<代码>create 如果有新实体,则将<代码>更新。

而不是:

Entity entity = new Entity("MyKind");

这确保每个财产只有一个独特的实体:

String myPropertyValue = getPropValue();
Entity entity = new Entity("MyKind", myPropertyValue);
ds.put(entity); // Ensures only one Entity per this property value




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签