Search in sources :

Example 16 with KeyFactory

use of com.google.cloud.datastore.KeyFactory in project google-cloud-java by GoogleCloudPlatform.

the class TransactionSnippets method rollback.

/**
   * 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();
    // [END rollback]
    return key;
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) Entity(com.google.cloud.datastore.Entity) Datastore(com.google.cloud.datastore.Datastore) KeyFactory(com.google.cloud.datastore.KeyFactory) Key(com.google.cloud.datastore.Key) IncompleteKey(com.google.cloud.datastore.IncompleteKey)

Example 17 with KeyFactory

use of com.google.cloud.datastore.KeyFactory in project google-cloud-java by GoogleCloudPlatform.

the class TransactionSnippets method commit.

/**
   * 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
    }
    return key;
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) Entity(com.google.cloud.datastore.Entity) Datastore(com.google.cloud.datastore.Datastore) DatastoreException(com.google.cloud.datastore.DatastoreException) KeyFactory(com.google.cloud.datastore.KeyFactory) Key(com.google.cloud.datastore.Key) IncompleteKey(com.google.cloud.datastore.IncompleteKey)

Example 18 with KeyFactory

use of com.google.cloud.datastore.KeyFactory in project google-cloud-java by GoogleCloudPlatform.

the class DatastoreExample method main.

@SuppressWarnings("unchecked")
public static void main(String... args) throws Exception {
    String projectId = args.length > 0 ? args[0] : null;
    // If you want to access a local Datastore running via the Google Cloud SDK, do
    //   DatastoreOptions options = DatastoreOptions.newBuilder()
    //       .setProjectId(projectId)
    //       .setNamespace(NAMESPACE)
    //       // change 8080 to the port that the emulator listens to
    //       .setHost("http://localhost:8080")
    //       .build();
    DatastoreOptions options = DatastoreOptions.newBuilder().setProjectId(projectId).setNamespace(NAMESPACE).build();
    String name = args.length > 1 ? args[1] : System.getProperty("user.getName");
    Datastore datastore = options.getService();
    KeyFactory keyFactory = datastore.newKeyFactory().setKind(USER_KIND);
    Key key = keyFactory.newKey(name);
    String actionName = args.length > 2 ? args[2].toLowerCase() : DEFAULT_ACTION;
    DatastoreAction action = ACTIONS.get(actionName);
    if (action == null) {
        System.out.println("Unrecognized action.");
        printUsage();
        return;
    }
    args = args.length > 3 ? Arrays.copyOfRange(args, 3, args.length) : new String[] {};
    Transaction tx = datastore.newTransaction();
    Object request;
    try {
        request = action.parse(args);
    } catch (IllegalArgumentException ex) {
        System.out.printf("Invalid input for action '%s'. %s%n", actionName, ex.getMessage());
        System.out.printf("Expected: %s%n", action.params());
        return;
    } catch (Exception ex) {
        System.out.println("Failed to parse request.");
        ex.printStackTrace();
        return;
    }
    try {
        action.run(tx, key, request);
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
    }
}
Also used : Datastore(com.google.cloud.datastore.Datastore) Transaction(com.google.cloud.datastore.Transaction) DatastoreOptions(com.google.cloud.datastore.DatastoreOptions) KeyFactory(com.google.cloud.datastore.KeyFactory) Key(com.google.cloud.datastore.Key) IncompleteKey(com.google.cloud.datastore.IncompleteKey)

Aggregations

KeyFactory (com.google.cloud.datastore.KeyFactory)18 Key (com.google.cloud.datastore.Key)17 IncompleteKey (com.google.cloud.datastore.IncompleteKey)14 Entity (com.google.cloud.datastore.Entity)12 Datastore (com.google.cloud.datastore.Datastore)11 FullEntity (com.google.cloud.datastore.FullEntity)7 Test (org.junit.Test)3 DatastoreException (com.google.cloud.datastore.DatastoreException)1 DatastoreOptions (com.google.cloud.datastore.DatastoreOptions)1 Transaction (com.google.cloud.datastore.Transaction)1