Java Code Examples for com.google.cloud.datastore.Entity#getLong()

The following examples show how to use com.google.cloud.datastore.Entity#getLong() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TransactionalWriter.java    From catatumbo with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected <E> E updateWithOptimisticLockingInternal(E entity, PropertyMetadata versionMetadata) {
  try {
    entityManager.executeEntityListeners(CallbackType.PRE_UPDATE, entity);
    Entity nativeEntity = (Entity) Marshaller.marshal(entityManager, entity, Intent.UPDATE);
    Entity storedNativeEntity = nativeTransaction.get(nativeEntity.getKey());
    if (storedNativeEntity == null) {
      throw new OptimisticLockException(
          String.format("Entity does not exist: %s", nativeEntity.getKey()));
    }
    String versionPropertyName = versionMetadata.getMappedName();
    long version = nativeEntity.getLong(versionPropertyName) - 1;
    long storedVersion = storedNativeEntity.getLong(versionPropertyName);
    if (version != storedVersion) {
      throw new OptimisticLockException(
          String.format("Expecting version %d, but found %d", version, storedVersion));
    }
    nativeTransaction.update(nativeEntity);
    E updatedEntity = (E) Unmarshaller.unmarshal(nativeEntity, entity.getClass());
    entityManager.executeEntityListeners(CallbackType.POST_UPDATE, updatedEntity);
    return updatedEntity;
  } catch (DatastoreException exp) {
    throw DatastoreUtils.wrap(exp);
  }
}
 
Example 2
Source File: DatastoreStorage.java    From styx with Apache License 2.0 6 votes vote down vote up
static RunState entityToRunState(Entity entity, WorkflowInstance instance)
    throws IOException {
  final long counter = entity.getLong(PROPERTY_COUNTER);
  final State state = State.valueOf(entity.getString(PROPERTY_STATE));
  final long timestamp = entity.getLong(PROPERTY_STATE_TIMESTAMP);
  final StateData data = StateData.newBuilder()
      .tries((int) entity.getLong(PROPERTY_STATE_TRIES))
      .consecutiveFailures((int) entity.getLong(PROPERTY_STATE_CONSECUTIVE_FAILURES))
      .retryCost(entity.getDouble(PROPERTY_STATE_RETRY_COST))
      .trigger(DatastoreStorage.<String>readOpt(entity, PROPERTY_STATE_TRIGGER_TYPE).map(type ->
          TriggerUtil.trigger(type, entity.getString(PROPERTY_STATE_TRIGGER_ID))))
      .messages(OBJECT_MAPPER.<List<Message>>readValue(entity.getString(PROPERTY_STATE_MESSAGES),
          new TypeReference<List<Message>>() { }))
      .retryDelayMillis(readOpt(entity, PROPERTY_STATE_RETRY_DELAY_MILLIS))
      .lastExit(DatastoreStorage.<Long>readOpt(entity, PROPERTY_STATE_LAST_EXIT).map(Long::intValue))
      .executionId(readOpt(entity, PROPERTY_STATE_EXECUTION_ID))
      .runnerId(readOpt(entity, PROPERTY_STATE_RUNNER_ID))
      .executionDescription(readOptJson(entity, PROPERTY_STATE_EXECUTION_DESCRIPTION,
          ExecutionDescription.class))
      .resourceIds(readOptJson(entity, PROPERTY_STATE_RESOURCE_IDS,
          new TypeReference<Set<String>>() { }))
      .triggerParameters(readOptJson(entity, PROPERTY_STATE_TRIGGER_PARAMETERS, TriggerParameters.class))
      .build();
  return RunState.create(instance, state, data, Instant.ofEpochMilli(timestamp), counter);
}
 
Example 3
Source File: GoogleJobStore.java    From data-transfer-project with Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, Integer> getCounts(UUID jobId) {
  Query<Entity> query = getCountsQuery(jobId);
  QueryResults<Entity> results = datastore.run(query);
  ImmutableMap.Builder<String, Integer> countsMapBuilder = ImmutableMap.builder();

  while (results.hasNext()) {
    Entity result = results.next();
    String dataType = result.getKey().getName();
    long count = result.getLong(COUNTS_FIELD);
    countsMapBuilder.put(dataType, (int) count);
  }

  return countsMapBuilder.build();
}
 
Example 4
Source File: DefaultDatastoreWriter.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
/**
 * Worker method for updating the given entity with optimistic locking.
 * 
 * @param entity
 *          the entity to update
 * @param versionMetadata
 *          the metadata for optimistic locking
 * @return the updated entity
 */
@SuppressWarnings("unchecked")
protected <E> E updateWithOptimisticLockingInternal(E entity, PropertyMetadata versionMetadata) {
  Transaction transaction = null;
  try {
    entityManager.executeEntityListeners(CallbackType.PRE_UPDATE, entity);
    Entity nativeEntity = (Entity) Marshaller.marshal(entityManager, entity, Intent.UPDATE);
    transaction = datastore.newTransaction();
    Entity storedNativeEntity = transaction.get(nativeEntity.getKey());
    if (storedNativeEntity == null) {
      throw new OptimisticLockException(
          String.format("Entity does not exist: %s", nativeEntity.getKey()));
    }
    String versionPropertyName = versionMetadata.getMappedName();
    long version = nativeEntity.getLong(versionPropertyName) - 1;
    long storedVersion = storedNativeEntity.getLong(versionPropertyName);
    if (version != storedVersion) {
      throw new OptimisticLockException(
          String.format("Expecting version %d, but found %d", version, storedVersion));
    }
    transaction.update(nativeEntity);
    transaction.commit();
    E updatedEntity = (E) Unmarshaller.unmarshal(nativeEntity, entity.getClass());
    entityManager.executeEntityListeners(CallbackType.POST_UPDATE, updatedEntity);
    return updatedEntity;
  } catch (DatastoreException exp) {
    throw DatastoreUtils.wrap(exp);
  } finally {
    rollbackIfActive(transaction);
  }
}
 
Example 5
Source File: DatastoreStorage.java    From styx with Apache License 2.0 5 votes vote down vote up
long getLimitForCounter(String counterId) throws IOException {
  if (GLOBAL_RESOURCE_ID.equals(counterId)) {
    // missing global resource means free to go
    return config().globalConcurrency().orElse(Long.MAX_VALUE);
  }

  final Key limitKey = datastore.newKeyFactory().setKind(KIND_COUNTER_LIMIT).newKey(counterId);
  final Entity limitEntity = datastore.get(limitKey);
  if (limitEntity == null) {
    throw new IllegalArgumentException("No limit found in Datastore for " + counterId);
  } else {
    return limitEntity.getLong(PROPERTY_LIMIT);
  }
}
 
Example 6
Source File: DatastoreSession.java    From tomcat-runtime with Apache License 2.0 5 votes vote down vote up
/**
 * Restore the metadata of a session with the values contains in the entity.
 * @param metadata An entity containing the metadata to restore
 */
private void restoreMetadataFromEntity(Entity metadata) {
  creationTime = metadata.getLong(SessionMetadata.CREATION_TIME);
  lastAccessedTime = metadata.getLong(SessionMetadata.LAST_ACCESSED_TIME);
  maxInactiveInterval = (int) metadata.getLong(SessionMetadata.MAX_INACTIVE_INTERVAL);
  isNew = metadata.getBoolean(SessionMetadata.IS_NEW);
  isValid = metadata.getBoolean(SessionMetadata.IS_VALID);
  thisAccessedTime = metadata.getLong(SessionMetadata.THIS_ACCESSED_TIME);
}
 
Example 7
Source File: DatastoreUtils.java    From catatumbo with Apache License 2.0 2 votes vote down vote up
/**
 * Increments the version property of the given entity by one.
 * 
 * @param nativeEntity
 *          the target entity
 * @param versionMetadata
 *          the metadata of the version property
 * @return a new entity (copy of the given), but with the incremented version.
 */
static Entity incrementVersion(Entity nativeEntity, PropertyMetadata versionMetadata) {
  String versionPropertyName = versionMetadata.getMappedName();
  long version = nativeEntity.getLong(versionPropertyName);
  return Entity.newBuilder(nativeEntity).set(versionPropertyName, ++version).build();
}