PageRenderTime 29ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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