Coder Social home page Coder Social logo

Comments (10)

LiangliangSui avatar LiangliangSui commented on July 23, 2024 1

MetaContext will be shared across different Fury objects, that's what it is used for

Ok, got it, if it is reused in different Fury objects, then we do need setMetaContext.

Thank you for helping me solve this problem. Thanks again.

from incubator-fury.

LiangliangSui avatar LiangliangSui commented on July 23, 2024

Could we instantiate a MetaContext object in the SerializationContext constructor with withMetaShare enabled, without requiring the user to manually set a MetaContext instance through the SerializationContext#setMetaContext interface?

For example, the following implementation.

package org.apache.fury.resolver;

import java.util.IdentityHashMap;
import org.apache.fury.config.Config;
import org.apache.fury.config.FuryBuilder;

/**
 * A context is used to add some context-related information, so that the serializers can set up
 * relation between serializing different objects. The context will be reset after finished
 * serializing/deserializing the object tree.
 */
public final class SerializationContext {
  private final IdentityHashMap<Object, Object> objects = new IdentityHashMap<>();
  private final boolean scopedMetaShareEnabled;
  private final boolean withMetaShare;
  private MetaContext metaContext;

  public SerializationContext(Config config) {
    scopedMetaShareEnabled = config.isScopedMetaShareEnabled();
    withMetaShare = config.isMetaShareEnabled();
    if (withMetaShare) {
      metaContext = new MetaContext();
    }
  }

  /** Return the previous value associated with <tt>key</tt>, or <tt>null</tt>. */
  public Object add(Object key, Object value) {
    return objects.put(key, value);
  }

  public boolean containsKey(Object key) {
    return objects.containsKey(key);
  }

  public Object get(Object key) {
    return objects.get(key);
  }

  public MetaContext getMetaContext() {
    return metaContext;
  }

  /**
   * Set meta context, which can be used to share data across multiple serialization call. Note that
   * {@code metaContext} will be cleared after the serialization is finished. Please set the context
   * before every serialization if metaShare is enabled by {@link FuryBuilder#withMetaShare(boolean)}
   */
  public void setMetaContext(MetaContext metaContext) {
    this.metaContext = metaContext;
  }

  public void resetWrite() {
    if (!objects.isEmpty()) {
      objects.clear();
    }
    if (scopedMetaShareEnabled) {
      metaContext.classMap.clear();
      metaContext.writingClassDefs.clear();
    } else if (withMetaShare) {
      metaContext = new MetaContext();
    } else {
      metaContext = null;
    }
  }

  public void resetRead() {
    if (!objects.isEmpty()) {
      objects.clear();
    }
    if (scopedMetaShareEnabled) {
      metaContext.readClassInfos.clear();
      metaContext.readClassDefs.clear();
    } else if (withMetaShare) {
      metaContext = new MetaContext();
    } else {
      metaContext = null;
    }
  }

  public void reset() {
    if (!objects.isEmpty()) {
      objects.clear();
    }
    if (scopedMetaShareEnabled) {
      metaContext.classMap.clear();
      metaContext.writingClassDefs.clear();
      metaContext.readClassInfos.clear();
      metaContext.readClassDefs.clear();
    } else if (withMetaShare) {
      metaContext = new MetaContext();
    } else {
      metaContext = null;
    }
  }
}

from incubator-fury.

chaokunyang avatar chaokunyang commented on July 23, 2024

No, we can't. That's what scopedMetaShare option do. We will make it as the default option in the future. But the meta share itself, will always need to set MetaContext. As you can see, the NPE here is throw by fury on purpose

from incubator-fury.

LiangliangSui avatar LiangliangSui commented on July 23, 2024

Users will not use the created MetaContext directly. What is the purpose of letting users set MetaContext through the setMetaContext interface? Is it just to let users know that MetaContext is shared between multiple serializations?

from incubator-fury.

LiangliangSui avatar LiangliangSui commented on July 23, 2024

If we just want to let users know that MetaContext is shared between multiple serializations, can we explain this problem in the withMetaShare option?

Our goal should be to let users know that MetaContext is shared between multiple serializations, and it can be set optionally through the setMetaContext interface, rather than forcing users to set it when enabling withMetaShare.

I don't know if I understand it correctly

from incubator-fury.

chaokunyang avatar chaokunyang commented on July 23, 2024

I think adding a precheck in org.apache.fury.resolver.ClassResolver#writeClassDefs is enough. We can provide a detailed error message to indicate users need to set MetaContext here.

from incubator-fury.

chaokunyang avatar chaokunyang commented on July 23, 2024

If we just want to let users know that MetaContext is shared between multiple serializations, can we explain this problem in the withMetaShare option?

Our goal should be to let users know that MetaContext is shared between multiple serializations, and it can be set optionally through the setMetaContext interface, rather than forcing users to set it when enabling withMetaShare.

I don't know if I understand it correctly

Not exactly, users will use this API directly, as you can see from the use case:

  public void testSimpleRecordMetaShared(boolean codegen) {
    Fury fury =
        Fury.builder()
            .requireClassRegistration(false)
            .withCodegen(codegen)
            .withMetaShare(true)
            .build();
    Foo foo = new Foo(10, "abc", new ArrayList<>(Arrays.asList("a", "b")), 'x');
    MetaContext context = new MetaContext();
    fury.getSerializationContext().setMetaContext(context);
    byte[] bytes = fury.serialize(foo);
    fury.getSerializationContext().setMetaContext(context);
    Assert.assertEquals(fury.deserialize(bytes), foo);
  }

This will be used by rpc framework too reuse meta across multiple serializaition. Or used by bigdata frameworks to serialize data by batch.

from incubator-fury.

LiangliangSui avatar LiangliangSui commented on July 23, 2024

Not exactly, users will use this API directly, as you can see from the use case:

I mean that users will not directly use any API and data in MetaContext, but it is exposed to users.

This will be used by rpc framework too reuse meta across multiple serializaition. Or used by bigdata frameworks to serialize data by batch.

I have another question, will MetaContext be shared across different Fury objects?

from incubator-fury.

chaokunyang avatar chaokunyang commented on July 23, 2024

Users will use this API:

  @Test(dataProvider = "codegen")
  public void testBatch(boolean codegen) {
    Fury fury =
        Fury.builder()
            .requireClassRegistration(false)
            .withCodegen(codegen)
            .withMetaShare(true)
            .build();
    MetaContext context = new MetaContext();
    for (int i = 0; i < 10000; i++) {
      Foo foo = new Foo(10, "abc", new ArrayList<>(Arrays.asList("a", "b")), 'x');
      fury.getSerializationContext().setMetaContext(context);
      byte[] bytes = fury.serialize(foo);
      fury.getSerializationContext().setMetaContext(context);
      Assert.assertEquals(fury.deserialize(bytes), foo);
    }
  }

from incubator-fury.

chaokunyang avatar chaokunyang commented on July 23, 2024

MetaContext will be shared across different Fury objects, that's what it is used for

from incubator-fury.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.