Java Code Examples for com.google.cloud.datastore.Datastore#allocateId()

The following examples show how to use com.google.cloud.datastore.Datastore#allocateId() . 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: MessageRepositoryImpl.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Override
public void save(Message message) {
  // Save message to "messages"
  Datastore datastore = getDatastoreInstance();
  Key key = datastore.allocateId(keyFactory.newKey());

  Entity.Builder messageEntityBuilder = Entity.newBuilder(key)
      .set("messageId", message.getMessageId());

  if (message.getData() != null) {
    messageEntityBuilder = messageEntityBuilder.set("data", message.getData());
  }

  if (message.getPublishTime() != null) {
    messageEntityBuilder = messageEntityBuilder.set("publishTime", message.getPublishTime());
  }
  datastore.put(messageEntityBuilder.build());
}
 
Example 2
Source File: MessageRepositoryImpl.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Override
public void save(Message message) {
  // Save message to "messages"
  Datastore datastore = getDatastoreInstance();
  Key key = datastore.allocateId(keyFactory.newKey());

  Entity.Builder messageEntityBuilder =
      Entity.newBuilder(key).set("messageId", message.getMessageId());

  if (message.getData() != null) {
    messageEntityBuilder = messageEntityBuilder.set("data", message.getData());
  }

  if (message.getPublishTime() != null) {
    messageEntityBuilder = messageEntityBuilder.set("publishTime", message.getPublishTime());
  }
  datastore.put(messageEntityBuilder.build());
}
 
Example 3
Source File: TransactionSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/** Example of committing a transaction. */
// [TARGET commit()]
public Key commit() {
  Datastore datastore = transaction.getDatastore();
  // [START commit]
  // create an entity
  KeyFactory keyFactory = datastore.newKeyFactory().setKind("MyKind");
  Key key = datastore.allocateId(keyFactory.newKey());
  Entity entity = Entity.newBuilder(key).set("description", "commit()").build();

  // add the entity and commit
  try {
    transaction.put(entity);
    transaction.commit();
  } catch (DatastoreException ex) {
    // handle exception
  }
  // [END commit]

  return key;
}
 
Example 4
Source File: TransactionSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/** Example of rolling back a transaction. */
// [TARGET rollback()]
public Key rollback() {
  Datastore datastore = transaction.getDatastore();
  // [START rollback]
  // create an entity
  KeyFactory keyFactory = datastore.newKeyFactory().setKind("MyKind");
  Key key = datastore.allocateId(keyFactory.newKey());
  Entity entity = Entity.newBuilder(key).set("description", "rollback()").build();

  // add the entity and rollback
  transaction.put(entity);
  transaction.rollback();
  // calling transaction.commit() now would fail
  // [END rollback]
  return key;
}
 
Example 5
Source File: TransactionSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/** Example of verifying if a transaction is active. */
// [TARGET active()]
public Key active() {
  Datastore datastore = transaction.getDatastore();
  // [START active]
  // create an entity
  KeyFactory keyFactory = datastore.newKeyFactory().setKind("MyKind");
  Key key = datastore.allocateId(keyFactory.newKey());
  Entity entity = Entity.newBuilder(key).set("description", "active()").build();
  // calling transaction.active() now would return true
  try {
    // add the entity and commit
    transaction.put(entity);
    transaction.commit();
  } finally {
    // if committing succeeded
    // then transaction.isActive() will be false
    if (transaction.isActive()) {
      // otherwise it's true and we need to rollback
      transaction.rollback();
    }
  }
  // [END active]
  return key;
}
 
Example 6
Source File: TransactionSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/** Example of verifying if a transaction is active. */
// [TARGET isActive()]
public Key isActive() {
  Datastore datastore = transaction.getDatastore();
  // [START isActive]
  // create an entity
  KeyFactory keyFactory = datastore.newKeyFactory().setKind("MyKind");
  Key key = datastore.allocateId(keyFactory.newKey());
  Entity entity = Entity.newBuilder(key).set("description", "active()").build();
  // calling transaction.active() now would return true
  try {
    // add the entity and commit
    transaction.put(entity);
    transaction.commit();
  } finally {
    // if committing succeeded
    // then transaction.active() will be false
    if (transaction.isActive()) {
      // otherwise it's true and we need to rollback
      transaction.rollback();
    }
  }
  // [END isActive]
  return key;
}
 
Example 7
Source File: MessageRepositoryImpl.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Override
public void saveClaim(String claim) {
  // Save message to "messages"
  Datastore datastore = getDatastoreInstance();
  Key key = datastore.allocateId(claimsKindKeyFactory.newKey());

  Entity.Builder claimEntityBuilder = Entity.newBuilder(key).set("claim", claim);

  datastore.put(claimEntityBuilder.build());
}
 
Example 8
Source File: MessageRepositoryImpl.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Override
public void saveToken(String token) {
  // Save message to "messages"
  Datastore datastore = getDatastoreInstance();
  Key key = datastore.allocateId(tokensKindKeyFactory.newKey());

  Entity.Builder tokenEntityBuilder = Entity.newBuilder(key).set("token", token);

  datastore.put(tokenEntityBuilder.build());
}
 
Example 9
Source File: MessageRepositoryImpl.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Override
public void save(Message message) {
  // Save message to "messages"
  Datastore datastore = getDatastoreInstance();
  Key key = datastore.allocateId(keyFactory.newKey());

  Entity.Builder messageEntityBuilder =
      Entity.newBuilder(key).set("messageId", message.getMessageId());

  String translated = message.getTranslated();
  if (translated != null) {
    messageEntityBuilder = messageEntityBuilder.set("data", translated);
  }

  if (message.getPublishTime() != null) {
    messageEntityBuilder = messageEntityBuilder.set("publishTime", message.getPublishTime());
  }

  if (message.getSourceLang() != null) {
    messageEntityBuilder = messageEntityBuilder.set("sourceLang", message.getSourceLang());
  }

  if (message.getTargetLang() != null) {
    messageEntityBuilder = messageEntityBuilder.set("targetLang", message.getTargetLang());
  }
  datastore.put(messageEntityBuilder.build());
}