PageRenderTime 21ms CodeModel.GetById 11ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 1ms

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

http://datanucleus-appengine.googlecode.com/
Java | 88 lines | 36 code | 12 blank | 40 comment | 0 complexity | e4fb8e5e194f290d9bee099d4a98ae91 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.Key;
19
20import org.datanucleus.store.connection.ManagedConnection;
21import org.datanucleus.ExecutionContext;
22import org.datanucleus.store.StoreManager;
23
24import java.util.HashSet;
25import java.util.IdentityHashMap;
26import java.util.Map;
27import java.util.Set;
28
29/**
30 * A registry mechanism to aid in the identification of parent objects when inserting new (owned) objects.
31 *
32 * @author Max Ross <maxr@google.com>
33 */
34public class KeyRegistry {
35  /**
36   * Convenience accessor for the {@link KeyRegistry} associated with the current datasource connection.
37   * @param ec ExecutionContext
38   * @return The KeyRegistry
39   */
40  public static KeyRegistry getKeyRegistry(ExecutionContext ec) {
41    StoreManager storeManager = ec.getStoreManager();
42    ManagedConnection mconn = storeManager.getConnection(ec);
43    return ((EmulatedXAResource) mconn.getXAResource()).getKeyRegistry();
44  }
45
46  /**
47   * Map of required parent key keyed by the child object.
48   * We use an IdentityHashMap here because we want reference equality, not object equality.
49   */
50  private final Map<Object, Key> parentKeyMap = new IdentityHashMap<Object, Key>();
51
52  /** Set of objects that are going to be persisted and are unowned (so don't look for a parent key). */
53  private final Set<Object> unownedObjects = new HashSet();
54
55  /**
56   * Method to register the parent key for a child object (when it is known and we are about to persist the child).
57   * @param obj Child object whose parent we are registering
58   * @param parentKey Key of parent
59   */
60  public void registerParentKeyForOwnedObject(Object obj, Key parentKey) {
61    parentKeyMap.put(obj, parentKey);
62  }
63
64  /**
65   * Accessor for the parent key of a child object, if already registered.
66   * @param obj Child object whose parent we are requesting
67   * @return parentKey Key of parent (or null if not known).
68   */
69  public Key getParentKeyForOwnedObject(Object obj) {
70    return parentKeyMap.get(obj);
71  }
72
73  public void clearParentKeys() {
74    parentKeyMap.clear();
75  }
76
77  public void registerUnownedObject(Object obj) {
78    this.unownedObjects.add(obj);
79  }
80
81  public boolean isUnowned(Object obj) {
82    return unownedObjects.contains(obj);
83  }
84
85  public void clearUnownedObjects() {
86    unownedObjects.clear();
87  }
88}