/src/com/google/appengine/datanucleus/DatastoreTransaction.java

http://datanucleus-appengine.googlecode.com/ · Java · 137 lines · 89 code · 23 blank · 25 comment · 10 complexity · 560bc55e7cf3bee5ead4c5eb6db58544 MD5 · raw file

  1. /**********************************************************************
  2. Copyright (c) 2009 Google Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. **********************************************************************/
  13. package com.google.appengine.datanucleus;
  14. import com.google.appengine.api.datastore.DatastoreFailureException;
  15. import com.google.appengine.api.datastore.Entity;
  16. import com.google.appengine.api.datastore.Key;
  17. import com.google.appengine.api.datastore.Transaction;
  18. import static com.google.appengine.datanucleus.DatastoreExceptionTranslator.wrapDatastoreFailureException;
  19. import static com.google.appengine.datanucleus.DatastoreExceptionTranslator.wrapIllegalArgumentException;
  20. import java.util.ConcurrentModificationException;
  21. import java.util.List;
  22. import java.util.Map;
  23. import java.util.Set;
  24. import org.datanucleus.exceptions.NucleusDataStoreException;
  25. /**
  26. * The orm's view of a datastore transaction.
  27. * Delegates to a {@link Transaction} and also functions as a txn-level cache.
  28. *
  29. * @author Max Ross <maxr@google.com>
  30. */
  31. public class DatastoreTransaction {
  32. private final Transaction txn;
  33. private final Map<Key, Entity> putEntities = Utils.newHashMap();
  34. private final Set<Key> deletedKeys = Utils.newHashSet();
  35. DatastoreTransaction(Transaction txn) {
  36. if (txn == null) {
  37. throw new NullPointerException("txn cannot be null");
  38. }
  39. this.txn = txn;
  40. }
  41. private void clear() {
  42. putEntities.clear();
  43. deletedKeys.clear();
  44. }
  45. void commit() {
  46. try {
  47. txn.commit();
  48. } catch (IllegalArgumentException e) {
  49. throw wrapIllegalArgumentException(e);
  50. } catch (ConcurrentModificationException e) {
  51. throw new NucleusDataStoreException("Concurrent Modification", e);
  52. } catch (DatastoreFailureException e) {
  53. throw wrapDatastoreFailureException(e);
  54. }
  55. clear();
  56. }
  57. void rollback() {
  58. try {
  59. txn.rollback();
  60. clear();
  61. } catch (IllegalArgumentException e) {
  62. throw wrapIllegalArgumentException(e);
  63. } catch (DatastoreFailureException e) {
  64. throw wrapDatastoreFailureException(e);
  65. }
  66. }
  67. public Transaction getInnerTxn() {
  68. return txn;
  69. }
  70. void addPutEntities(List<Entity> entities) {
  71. for (Entity entity : entities) {
  72. // Make a copy in case someone changes
  73. // the provided entity after we add it to our cache.
  74. putEntities.put(entity.getKey(), makeCopy(entity));
  75. }
  76. }
  77. private Entity makeCopy(Entity entity) {
  78. // We don't check key when we look for changes so it's
  79. // ok that the copy doesn't have its key set.
  80. Entity copy = new Entity(entity.getKind());
  81. EntityUtils.copyProperties(entity, copy);
  82. return copy;
  83. }
  84. void addDeletedKey(Key key) {
  85. deletedKeys.add(key);
  86. }
  87. Map<Key, Entity> getPutEntities() {
  88. return putEntities;
  89. }
  90. Set<Key> getDeletedKeys() {
  91. return deletedKeys;
  92. }
  93. @Override
  94. public boolean equals(Object o) {
  95. if (this == o) {
  96. return true;
  97. }
  98. if (o == null || getClass() != o.getClass()) {
  99. return false;
  100. }
  101. DatastoreTransaction that = (DatastoreTransaction) o;
  102. if (!txn.equals(that.txn)) {
  103. return false;
  104. }
  105. return true;
  106. }
  107. @Override
  108. public int hashCode() {
  109. return txn.hashCode();
  110. }
  111. }