private void setChanges(
     Instance dest,
     Instance src,
     Function<Instance, Instance> oldFunction,
     long[] changes,
     boolean isInput) {
   AModelType type = InvocationContext.get().getTypeManager().getByPrefix(dest._type());
   List<AIField> fields = type.getAllFields();
   for (AIField f : fields) {
     if (f.getName().equals("id")) {
       dest._setId(src.getId());
       continue;
     }
     // If this field changed
     int index = dest._getPropertyIndex(f.getName());
     if (InstanceImpl._hasBit(changes, index)) {
       if (f.isChild()) {
         setChildValue(dest, src, f, oldFunction, isInput);
       } else {
         Property<?> property = src._get(f.getName());
         if (property == null) {
           continue;
         }
         dest._set(f.getName(), property.getValue());
       }
       dest._markChange(index, null, null);
     }
   }
 }
  private Instance prepareChanges(
      Instance src, Instance dest, Function<Instance, Instance> oldFunction, boolean isInput) {
    InvocationContext context = InvocationContext.get();
    Instance ch = context.getInstanceProvider().newInstance(src._type());
    ch.markInSavingToDB(true);
    if (isInput) {
      if (context.isInRepRetry()) {
        // If in Replication retry, then version of the input will be
        // very old(invalid). So that make the version id of old
        // object.Otherwise When applying this retried version on server
        // will fail.
        ch._setVersion(dest.getVersion());
      } else {
        ch._setVersion(src.getVersion());
      }
    }
    long[] tempChanges = src._getChangesMarkers();

    long[] allChanges = new long[tempChanges.length];
    Arrays.fill(allChanges, -1);
    src._setChangesMarkers(allChanges);
    long[] changes = src._getChanges(dest);
    src._setChangesMarkers(tempChanges);
    setChanges(ch, isInput ? src : dest, oldFunction, changes, isInput);
    InstanceImpl.copyMarkers(ch._getChangesMarkers(), changes);
    ch._setId(src.getId());
    return ch;
  }
 /**
  * Allocates duplicate IDs to newly inserted instances in this organization to reference it from
  * other instance.
  *
  * @param in
  * @param entityId
  */
 private void allocateDuplicateId(Instance in, MutableLong entityId) {
   if (in == null) {
     return;
   }
   if (in._isNew()) {
     long nextId = IdProvider.prepareID(entityId.longValue(), 0, in._type(), true);
     in._setId(nextId);
     entityId.increment();
   }
   in._visit(
       (inst, isChild) -> {
         if (isChild) {
           allocateDuplicateId((Instance) inst, entityId);
         }
       });
 }