Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup error handling/docs in TaskList #147

Merged
merged 2 commits into from
Apr 2, 2016
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.google.datastore.snippets;

import com.google.gcloud.datastore.Datastore;
import com.google.gcloud.datastore.DatastoreException;
import com.google.gcloud.datastore.DatastoreOptions;
import com.google.gcloud.datastore.DateTime;
import com.google.gcloud.datastore.Entity;
Expand Down Expand Up @@ -51,7 +50,8 @@ public class TaskList {
* Adds a task entity to the Datastore.
*
* @param description The task description
* @return The {@link Key} of the entity.
* @return The {@link Key} of the entity
* @throws DatastoreException if the Datastore put fails
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

technically it could also throw DatastoreException if Id allocation failed.

*/
Key addTask(String description) {
Key key = datastore.allocateId(keyFactory.newKey());
Expand All @@ -70,14 +70,18 @@ Key addTask(String description) {
* Marks a task entity as done.
*
* @param id The ID of the task entity as given by {@link Key#id()}
* @throws DatastoreException if the task does not exist
* @return true if the task was found, false if not
* @throws DatastoreException if the transaction commit fails
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For similar reason to the above (there is also get) if you want to be picky you can say "if Datastore transaction fails"

*/
void markDone(long id) {
boolean markDone(long id) {
Transaction transaction = datastore.newTransaction();
try {
Entity task = transaction.get(keyFactory.newKey(id));
transaction.put(Entity.builder(task).set("done", true).build());
if (task != null) {
transaction.put(Entity.builder(task).set("done", true).build());
}
transaction.commit();
return task != null;
} finally {
if (transaction.active()) {
transaction.rollback();
Expand All @@ -89,6 +93,8 @@ void markDone(long id) {
// [START retrieve_entities]
/**
* Returns a list of all task entities in ascending order of creation time.
*
* @throws DatastoreException if the query fails
*/
Iterator<Entity> listTasks() {
Query<Entity> query =
Expand All @@ -102,6 +108,7 @@ Iterator<Entity> listTasks() {
* Deletes a task entity.
*
* @param id The ID of the task entity as given by {@link Key#id()}
* @throws DatastoreException if the delete fails
*/
void deleteTask(long id) {
datastore.delete(keyFactory.newKey(id));
Expand Down Expand Up @@ -158,10 +165,9 @@ void handleCommandLine(String commandLine) {
case "done":
assertArgsLength(args, 2);
long id = Long.parseLong(args[1]);
try {
markDone(id);
if (markDone(id)) {
System.out.println("task marked done");
} catch (DatastoreException e) {
} else {
System.out.printf("did not find a Task entity with ID %d%n", id);
}
break;
Expand All @@ -178,7 +184,7 @@ void handleCommandLine(String commandLine) {
case "delete":
assertArgsLength(args, 2);
deleteTask(Long.parseLong(args[1]));
System.out.println("task deleted");
System.out.println("task deleted (if it existed)");
break;
default:
throw new IllegalArgumentException("unrecognized command: " + command);
Expand Down