Package com.google.appengine.datanucleus.mapping

Examples of com.google.appengine.datanucleus.mapping.DatastoreTable$CallBack


          return EntityUtils.entityToPojo(from, acmd, clr, getExecutionContext(), query.getIgnoreCache(), fp);
        }
      };
    }

    DatastoreTable table = getStoreManager().getDatastoreClass(acmd.getFullClassName(), clr);
    String kind = table.getIdentifier().getIdentifierName();
    QueryData qd = new QueryData(parameters, acmd, table, compilation, new Query(kind), resultType,
        resultTransformer);

    if (compilation.getExprFrom() != null) {
      // Process FROM expression, adding pseudo joins
View Full Code Here


      if (emd == null) {
        throw new NucleusFatalUserException(
            query.getSingleStringQuery() + ": Can only reference properties of a sub-object if "
            + "the sub-object is embedded.");
      }
      DatastoreTable parentTable =
          getStoreManager().getDatastoreClass(parentFullClassName, getClassLoaderResolver());
      parentFullClassName = ammd.getTypeName();
      AbstractMemberMetaData parentField = (AbstractMemberMetaData) emd.getParent();
      EmbeddedMapping embeddedMapping =
          (EmbeddedMapping) parentTable.getMappingForFullFieldName(parentField.getFullFieldName());
      ammd = findMemberMetaDataWithName(tuple, embeddedMapping);
      if (ammd == null) {
        break;
      }
    }
View Full Code Here

      Object assignedParentPk = fieldMgr.establishEntityGroup();
      Entity entity = fieldMgr.getEntity();

      if (!datastoreMgr.storageVersionAtLeast(StorageVersion.READ_OWNED_CHILD_KEYS_FROM_PARENTS)) {
        // Older storage versions : store list positions in the element
        DatastoreTable table = datastoreMgr.getDatastoreClass(op.getClassMetaData().getFullClassName(),
            ec.getClassLoaderResolver());
        Collection<JavaTypeMapping> orderMappings = table.getExternalOrderMappings().values();
        for (JavaTypeMapping orderMapping : orderMappings) {
          if (orderMapping instanceof IndexMapping) {
            Object orderValue = op.getAssociatedValue(orderMapping);
            if (orderValue != null) {
              // Set order index on the entity
View Full Code Here

      return null;
    } else if (RelationType.isRelationSingleValued(relationType)) {
      boolean owned = MetaDataUtils.isOwnedRelation(mmd, getStoreManager());
      if (owned) {
        // Owned relation, so 1-1_uni/1-1_bi store the relation in the property, and all others at other side
        DatastoreTable dt = getDatastoreTable();
        JavaTypeMapping mapping = dt.getMemberMappingInDatastoreClass(mmd);
        if (relationType == RelationType.ONE_TO_ONE_BI || relationType == RelationType.ONE_TO_ONE_UNI) {
          // Even though the mapping is 1 to 1, we model it as a 1 to many and then
          // just throw a runtime exception if we get multiple children.  We would
          // prefer to store the child id on the parent, but we can't because creating
          // a parent and child at the same time involves 3 distinct writes:
          // 1) We put the parent object in order to get a Key.
          // 2) We put the child object, which needs the Key of the parent as
          // the parent of its own Key so that parent and child reside in the
          // same entity group.
          // 3) We re-put the parent object, adding the Key of the child object
          // as a property on the parent.
          // The problem is that the datastore does not support multiple writes
          // to the same entity within a single transaction, so there's no way
          // to perform this sequence of events atomically, and that's a problem.

          // We have 2 scenarios here.  The first is that we're loading the parent
          // side of a 1 to 1 and we want the child.  In that scenario we're going
          // to issue a parent query against the child table with the expectation
          // that there is either 1 result or 0.

          // The second scearnio is that we're loading the child side of a
          // bidirectional 1 to 1 and we want the parent.  In that scenario
          // the key of the parent is part of the child's key so we can just
          // issue a fetch using the parent's key.
          if (dt.isParentKeyProvider(mmd)) {
            // bidir 1 to 1 and we are the child
            return lookupParent(mmd, mapping, false);
          } else {
            // bidir 1 to 1 and we are the parent
            return lookupOneToOneChild(mmd, clr);
View Full Code Here

    parentKey = KeyRegistry.getKeyRegistry(ec).getParentKeyForOwnedObject(op.getObject());

    if (parentKey == null) {
      DatastoreManager storeMgr = (DatastoreManager)ec.getStoreManager();
      // Mechanism 2, from the parent end of a bidir relation where this is a child
      DatastoreTable table = storeMgr.getDatastoreClass(op.getClassMetaData().getFullClassName(),
          ec.getClassLoaderResolver());
      AbstractMemberMetaData parentField = table.getParentMappingMemberMetaData();
      if (parentField != null && MetaDataUtils.isOwnedRelation(parentField, storeMgr)) {
        Object parent = op.provideField(parentField.getAbsoluteFieldNumber());
        parentKey = parent == null ? null : EntityUtils.getKeyForObject(parent, ec);
      }
    }
View Full Code Here

      // No relations waiting to be persisted
      return false;
    }

    ObjectProvider op = getObjectProvider();
    DatastoreTable table = getDatastoreTable();
    if (datastoreEntity.getKey() != null) {
      Key key = datastoreEntity.getKey();
      AbstractClassMetaData acmd = op.getClassMetaData();
      int[] relationFieldNums = acmd.getRelationMemberPositions(ec.getClassLoaderResolver(), ec.getMetaDataManager());
      if (relationFieldNums != null) {
        for (int i=0;i<relationFieldNums.length;i++) {
          AbstractMemberMetaData mmd = acmd.getMetaDataForManagedMemberAtAbsolutePosition(relationFieldNums[i]);
          boolean owned = MetaDataUtils.isOwnedRelation(mmd, getStoreManager());
          if (owned) {
            // Register parent key for all owned related objects
            Object childValue = op.provideField(mmd.getAbsoluteFieldNumber());
            if (childValue != null) {
              if (childValue instanceof Object[]) {
                childValue = Arrays.asList((Object[]) childValue);
              }

              String expectedType = mmd.getTypeName();
              if (mmd.getCollection() != null) {
                CollectionMetaData cmd = mmd.getCollection();
                expectedType = cmd.getElementType();
              } else if (mmd.getArray() != null) {
                ArrayMetaData amd = mmd.getArray();
                expectedType = amd.getElementType();
              }

              if (childValue instanceof Iterable) {
                // TODO(maxr): Make sure we're not pulling back unnecessary data when we iterate over the values.
                for (Object element : (Iterable) childValue) {
                  addToParentKeyMap(keyRegistry, element, key, ec, expectedType, true);
                }
              } else if (childValue.getClass().isArray()) {
                for (int j=0;j<Array.getLength(childValue);i++) {
                  addToParentKeyMap(keyRegistry, Array.get(childValue, i), key, ec, expectedType, true);
                }
              } else if (childValue instanceof Map) {
                boolean persistableKey = (mmd.getMap().getKeyClassMetaData(ec.getClassLoaderResolver(), ec.getMetaDataManager()) != null);
                boolean persistableVal = (mmd.getMap().getValueClassMetaData(ec.getClassLoaderResolver(), ec.getMetaDataManager()) != null);
                Iterator entryIter = ((Map)childValue).entrySet().iterator();
                while (entryIter.hasNext()) {
                  Map.Entry entry = (Map.Entry)entryIter.next();
                  if (persistableKey) {
                    addToParentKeyMap(keyRegistry, entry.getKey(), key, ec, expectedType, true);
                  }
                  if (persistableVal) {
                    addToParentKeyMap(keyRegistry, entry.getValue(), key, ec, expectedType, true);
                  }
                }
              } else {
                addToParentKeyMap(keyRegistry, childValue, key, ec, expectedType,
                    !table.isParentKeyProvider(mmd));
              }
            }
          } else {
            // Register that related object(s) is unowned
            Object childValue = op.provideField(mmd.getAbsoluteFieldNumber());
            if (childValue != null) {
              if (childValue instanceof Object[]) {
                childValue = Arrays.asList((Object[]) childValue);
              }

              if (childValue instanceof Iterable) {
                // TODO(maxr): Make sure we're not pulling back unnecessary data when we iterate over the values.
                for (Object element : (Iterable) childValue) {
                  keyRegistry.registerUnownedObject(element);
                }
              } else {
                keyRegistry.registerUnownedObject(childValue);
              }
            }
          }
        }
      }
    }

    boolean modifiedEntity = false;

    // Stage 1 : process FKs
    for (RelationStoreInformation relInfo : relationStoreInfos) {
      AbstractMemberMetaData mmd = relInfo.mmd;
      try {
        JavaTypeMapping mapping = table.getMemberMappingInDatastoreClass(relInfo.mmd);
        if (mapping instanceof EmbeddedPCMapping ||
            mapping instanceof SerialisedPCMapping ||
            mapping instanceof SerialisedReferenceMapping ||
            mapping instanceof PersistableMapping ||
            mapping instanceof InterfaceMapping) {
          boolean owned = MetaDataUtils.isOwnedRelation(mmd, getStoreManager());
          RelationType relationType = mmd.getRelationType(ec.getClassLoaderResolver());
          if (owned) {
            // Owned relation
            boolean owner = false;
            if (relationType == RelationType.ONE_TO_ONE_UNI || relationType == RelationType.ONE_TO_MANY_UNI ||
                relationType == RelationType.ONE_TO_MANY_BI) {
              owner = true;
            } else if (relationType == RelationType.ONE_TO_ONE_BI && mmd.getMappedBy() == null) {
              owner = true;
            }

            if (!table.isParentKeyProvider(mmd)) {
              // Make sure the parent key is set properly between parent and child objects
              if (!owner) {
                ObjectProvider parentOP = ec.findObjectProvider(relInfo.value);
                EntityUtils.checkParentage(op.getObject(), parentOP);
              } else {
                EntityUtils.checkParentage(relInfo.value, op);
              }
              mapping.setObject(ec, datastoreEntity, IS_FK_VALUE_ARR, relInfo.value, op, mmd.getAbsoluteFieldNumber());
            }
          } else {
            // Unowned relation
            mapping.setObject(ec, datastoreEntity, IS_FK_VALUE_ARR, relInfo.value, op, mmd.getAbsoluteFieldNumber());
          }
        }
      } catch (NotYetFlushedException e) {
        // Ignore this. We always have the object in the datastore, at least partially to get the key
      }
    }

    // Stage 2 : postInsert/postUpdate
    for (RelationStoreInformation relInfo : relationStoreInfos) {
      try {
        JavaTypeMapping mapping = table.getMemberMappingInDatastoreClass(relInfo.mmd);
        if (mapping instanceof ArrayMapping || mapping instanceof MapMapping) {
          // Ignore postInsert/update for arrays/maps since don't support backing stores
        } else if (mapping instanceof MappingCallbacks) {
          if (insert) {
            ((MappingCallbacks)mapping).postInsert(op);
View Full Code Here

      return buildStoreData(cmd, clr);
    } else if (MetaDataUtils.isNewOrSuperclassTableInheritanceStrategy(cmd)) {
      // Table mapped into table of superclass
      // Find the superclass - should have been created first
      AbstractClassMetaData[] managingCmds = getClassesManagingTableForClass(cmd, clr);
      DatastoreTable superTable;
      if (managingCmds != null && managingCmds.length == 1) {
        MappedStoreData superData = (MappedStoreData) storeDataMgr.get(managingCmds[0].getFullClassName());
        if (superData != null) {
          // Specify the table if it already exists
          superTable = (DatastoreTable) superData.getDatastoreContainerObject();
View Full Code Here

    return sdNew;
  }

  private StoreData buildStoreData(ClassMetaData cmd, ClassLoaderResolver clr) {
    String kindName = EntityUtils.getKindName(getIdentifierFactory(), cmd);
    DatastoreTable table = new DatastoreTable(kindName, this, cmd, clr, dba);
    StoreData sd = new MappedStoreData(cmd, table, true);
    registerStoreData(sd);
    // needs to be called after we register the store data to avoid stack overflow
    table.buildMapping();
    return sd;
  }
View Full Code Here

TOP

Related Classes of com.google.appengine.datanucleus.mapping.DatastoreTable$CallBack

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.