PageRenderTime 28ms CodeModel.GetById 18ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

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