private DatastoreServiceConfig createDatastoreServiceConfigPrototype(
      PersistenceConfiguration persistenceConfiguration, String... timeoutProps) {
    DatastoreServiceConfig datastoreServiceConfig = DatastoreServiceConfig.Builder.withDefaults();

    for (String timeoutProp : timeoutProps) {
      int defaultDeadline = persistenceConfiguration.getIntProperty(timeoutProp);
      if (defaultDeadline > 0) {
        datastoreServiceConfig.deadline(defaultDeadline / 1000d);
      }
    }
    String defaultReadConsistencyStr =
        persistenceConfiguration.getStringProperty(DATASTORE_READ_CONSISTENCY_PROPERTY);
    if (defaultReadConsistencyStr != null) {
      try {
        datastoreServiceConfig.readPolicy(
            new ReadPolicy(Consistency.valueOf(defaultReadConsistencyStr)));
      } catch (IllegalArgumentException iae) {
        throw new NucleusFatalUserException(
            "Illegal value for "
                + DATASTORE_READ_CONSISTENCY_PROPERTY
                + ".  Valid values are "
                + Arrays.toString(Consistency.values()));
      }
    }
    return datastoreServiceConfig;
  }
  // visible for testing
  static DatastoreServiceConfig copyDatastoreServiceConfig(DatastoreServiceConfig config) {
    DatastoreServiceConfig newConfig =
        DatastoreServiceConfig.Builder.withImplicitTransactionManagementPolicy(
                config.getImplicitTransactionManagementPolicy())
            .readPolicy(config.getReadPolicy());

    if (config.getDeadline() != null) {
      newConfig.deadline(config.getDeadline());
    }
    return newConfig;
  }
示例#3
0
/**
 * Provides datastore instance to use.
 *
 * @author [email protected] (Christian Ohler)
 */
public class DatastoreProvider {

  private DatastoreProvider() {}

  private static final DatastoreService STRONG_READS =
      DatastoreServiceFactory.getDatastoreService(
          DatastoreServiceConfig.Builder.withDeadline(5 /*seconds*/)
              .implicitTransactionManagementPolicy(ImplicitTransactionManagementPolicy.NONE)
              .readPolicy(new ReadPolicy(ReadPolicy.Consistency.STRONG)));

  private static final DatastoreService EVENTUAL_READS =
      DatastoreServiceFactory.getDatastoreService(
          DatastoreServiceConfig.Builder.withDeadline(5 /*seconds*/)
              .implicitTransactionManagementPolicy(ImplicitTransactionManagementPolicy.NONE)
              .readPolicy(new ReadPolicy(ReadPolicy.Consistency.EVENTUAL)));

  public static DatastoreService strongReads() {
    return STRONG_READS;
  }

  public static DatastoreService eventualReads() {
    return EVENTUAL_READS;
  }
}