/**
   * {@inheritDoc}
   *
   * @param ctx
   */
  @Override
  public void prepareMarshal(GridCacheSharedContext ctx) throws IgniteCheckedException {
    super.prepareMarshal(ctx);

    if (ownedVals != null) {
      ownedValKeys = ownedVals.keySet();

      ownedValVals = ownedVals.values();

      for (Map.Entry<IgniteTxKey, CacheVersionedValue> entry : ownedVals.entrySet()) {
        GridCacheContext cacheCtx = ctx.cacheContext(entry.getKey().cacheId());

        entry.getKey().prepareMarshal(cacheCtx);

        entry.getValue().prepareMarshal(cacheCtx.cacheObjectContext());
      }
    }

    if (retVal != null && retVal.cacheId() != 0) {
      GridCacheContext cctx = ctx.cacheContext(retVal.cacheId());

      assert cctx != null : retVal.cacheId();

      retVal.prepareMarshal(cctx);
    }

    if (filterFailedKeys != null) {
      for (IgniteTxKey key : filterFailedKeys) {
        GridCacheContext cctx = ctx.cacheContext(key.cacheId());

        key.prepareMarshal(cctx);
      }
    }
  }
  /** {@inheritDoc} */
  @Override
  public void prepareMarshal(GridCacheContext<K, V> ctx) throws GridException {
    super.prepareMarshal(ctx);

    if (entries != null) {
      if (ctx.deploymentEnabled()) prepareObjects(entries, ctx);

      entriesBytes = ctx.marshaller().marshal(entries);
    }
  }
  /**
   * Adds a Near key.
   *
   * @param key Key.
   * @param keyBytes Key bytes.
   * @param ctx Context.
   * @throws GridException If failed.
   */
  public void addNearKey(K key, byte[] keyBytes, GridCacheContext<K, V> ctx) throws GridException {
    if (ctx.deploymentEnabled()) prepareObject(key, ctx);

    if (nearKeyBytes == null) nearKeyBytes = new ArrayList<>();

    nearKeyBytes.add(keyBytes);
  }
  /** {@inheritDoc} */
  @SuppressWarnings("TypeMayBeWeakened")
  @Nullable
  private Collection<Object> unmarshalFieldsCollection(
      @Nullable Collection<byte[]> byteCol, GridCacheContext<K, V> ctx, ClassLoader ldr)
      throws GridException {
    assert ctx != null;
    assert ldr != null;

    Collection<Object> col = unmarshalCollection(byteCol, ctx, ldr);
    Collection<Object> col0 = null;

    if (col != null) {
      col0 = new ArrayList<>(col.size());

      for (Object o : col) {
        List<Object> list = (List<Object>) o;
        List<Object> list0 = new ArrayList<>(list.size());

        for (Object obj : list)
          list0.add(obj != null ? ctx.marshaller().unmarshal((byte[]) obj, ldr) : null);

        col0.add(list0);
      }
    }

    return col0;
  }
  /** {@inheritDoc} */
  @SuppressWarnings("TypeMayBeWeakened")
  @Nullable
  private Collection<byte[]> marshalFieldsCollection(
      @Nullable Collection<Object> col, GridCacheContext<K, V> ctx) throws GridException {
    assert ctx != null;

    if (col == null) return null;

    Collection<List<Object>> col0 = new ArrayList<>(col.size());

    for (Object o : col) {
      List<GridIndexingEntity<?>> list = (List<GridIndexingEntity<?>>) o;
      List<Object> list0 = new ArrayList<>(list.size());

      for (GridIndexingEntity<?> ent : list) {
        if (ent.bytes() != null) list0.add(ent.bytes());
        else {
          if (ctx.deploymentEnabled()) prepareObject(ent.value(), ctx);

          list0.add(CU.marshal(ctx, ent.value()));
        }
      }

      col0.add(list0);
    }

    return marshalCollection(col0, ctx);
  }
  /** {@inheritDoc} */
  @Override
  public void prepareMarshal(GridCacheContext<K, V> ctx) throws GridException {
    super.prepareMarshal(ctx);

    if (err != null) errBytes = ctx.marshaller().marshal(err);

    metaDataBytes = marshalCollection(metadata, ctx);
    dataBytes = fields ? marshalFieldsCollection(data, ctx) : marshalCollection(data, ctx);

    if (ctx.deploymentEnabled() && !F.isEmpty(data)) {
      for (Object o : data) {
        if (o instanceof Map.Entry) {
          Map.Entry e = (Map.Entry) o;

          prepareObject(e.getKey(), ctx);
          prepareObject(e.getValue(), ctx);
        }
      }
    }
  }
  /** {@inheritDoc} */
  @Override
  public void finishUnmarshal(GridCacheContext<K, V> ctx, ClassLoader ldr) throws GridException {
    super.finishUnmarshal(ctx, ldr);

    if (errBytes != null) err = ctx.marshaller().unmarshal(errBytes, ldr);

    metadata = unmarshalCollection(metaDataBytes, ctx, ldr);
    data =
        fields
            ? unmarshalFieldsCollection(dataBytes, ctx, ldr)
            : unmarshalCollection(dataBytes, ctx, ldr);
  }
  /** {@inheritDoc} */
  @Override
  public void prepareMarshal(GridCacheContext<K, V> ctx) throws GridException {
    super.prepareMarshal(ctx);

    if (map != null) {
      if (ctx.deploymentEnabled()) {
        for (K key : map.keySet()) prepareObject(key, ctx);
      }

      mapBytes = CU.marshal(ctx, map);
    }
  }
  /** {@inheritDoc} */
  @Override
  public void finishUnmarshal(GridCacheContext<K, V> ctx, ClassLoader ldr) throws GridException {
    super.finishUnmarshal(ctx, ldr);

    if (entriesBytes != null) entries = ctx.marshaller().unmarshal(entriesBytes, ldr);
  }