Example #1
1
 public void writeRecommendations(Output output, Map<String, Recommendation> recommendations) {
   kryo.writeObject(output, recommendations.size());
   for (String key : recommendations.keySet()) {
     Recommendation rec = recommendations.get(key);
     rec.clean();
     kryo.writeObject(output, rec.source.key());
     kryo.writeObject(output, rec.target.key());
     kryo.writeObject(output, rec);
   }
 }
 private static <T> T deserializeObjectByKryo(Kryo kryo, InputStream in, Class<T> clazz) {
   Input inp = new Input(in);
   kryo.setClassLoader(Utilities.getSessionSpecifiedClassLoader());
   T t = kryo.readObject(inp, clazz);
   inp.close();
   return t;
 }
Example #3
0
 // This registers objects that are going to be sent over the network.
 public static void register(EndPoint endPoint) {
   Kryo kryo = endPoint.getKryo();
   kryo.register(RegisterName.class);
   kryo.register(String[].class);
   kryo.register(UpdateNames.class);
   kryo.register(ChatMessage.class);
 }
 @Override
 public HostLocation read(Kryo kryo, Input input, Class<HostLocation> type) {
   DeviceId deviceId = (DeviceId) kryo.readClassAndObject(input);
   PortNumber portNumber = (PortNumber) kryo.readClassAndObject(input);
   long time = input.readLong();
   return new HostLocation(deviceId, portNumber, time);
 }
Example #5
0
    @Override
    public void write(Kryo kryo, Output output) {
      output.writeInt(objectID, true);

      int methodClassID = kryo.getRegistration(method.getDeclaringClass()).getId();
      output.writeInt(methodClassID, true);

      CachedMethod[] cachedMethods = getMethods(kryo, method.getDeclaringClass());
      CachedMethod cachedMethod = null;
      for (int i = 0, n = cachedMethods.length; i < n; i++) {
        cachedMethod = cachedMethods[i];
        if (cachedMethod.method.equals(method)) {
          output.writeByte(i);
          break;
        }
      }

      for (int i = 0, n = cachedMethod.serializers.length; i < n; i++) {
        Serializer serializer = cachedMethod.serializers[i];
        if (serializer != null) {
          kryo.writeObjectOrNull(output, args[i], serializer);
        } else {
          kryo.writeClassAndObject(output, args[i]);
        }
      }

      output.writeByte(responseID);
    }
Example #6
0
  /** Read the edges from the temp file and load them to the graph. */
  private void readFromTempEdges(final Input input, final Graph graphToWriteTo) {
    while (!input.eof()) {
      // in this case the outId is the id assigned by the graph
      Object next = kryo.readClassAndObject(input);
      while (!next.equals(EdgeTerminator.INSTANCE)) {
        final List<Object> edgeArgs = new ArrayList<>();
        final DetachedEdge detachedEdge = (DetachedEdge) next;
        final Vertex vOut =
            graphToWriteTo.v(detachedEdge.iterators().vertexIterator(Direction.OUT).next().id());
        final Vertex inV =
            graphToWriteTo.v(detachedEdge.iterators().vertexIterator(Direction.IN).next().id());

        detachedEdge
            .iterators()
            .propertyIterator()
            .forEachRemaining(p -> edgeArgs.addAll(Arrays.asList(p.key(), p.value())));
        // detachedEdge.iterators().hiddenPropertyIterator().forEachRemaining(p ->
        // edgeArgs.addAll(Arrays.asList(Graph.Key.hide(p.key()), p.value())));

        appendToArgList(edgeArgs, T.id, detachedEdge.id());

        vOut.addEdge(detachedEdge.label(), inV, edgeArgs.toArray());

        next = kryo.readClassAndObject(input);
      }

      // vertex terminator
      kryo.readClassAndObject(input);
    }
  }
Example #7
0
 public void writeVectorSets(Output output, Map<String, VectorSet> vectorSets) {
   kryo.writeObject(output, vectorSets.size());
   for (String key : vectorSets.keySet()) {
     VectorSet vectorSet = vectorSets.get(key);
     kryo.writeObject(output, vectorSet);
   }
 }
Example #8
0
    @Override
    public void read(Kryo kryo, Input input) {
      objectID = input.readInt(true);

      int methodClassID = input.readInt(true);
      Class methodClass = kryo.getRegistration(methodClassID).getType();
      byte methodIndex = input.readByte();
      CachedMethod cachedMethod;
      try {
        cachedMethod = getMethods(kryo, methodClass)[methodIndex];
      } catch (IndexOutOfBoundsException ex) {
        throw new KryoException(
            "Invalid method index " + methodIndex + " for class: " + methodClass.getName());
      }
      method = cachedMethod.method;

      args = new Object[cachedMethod.serializers.length];
      for (int i = 0, n = args.length; i < n; i++) {
        Serializer serializer = cachedMethod.serializers[i];
        if (serializer != null) {
          args[i] = kryo.readObjectOrNull(input, method.getParameterTypes()[i], serializer);
        } else {
          args[i] = kryo.readClassAndObject(input);
        }
      }

      responseID = input.readByte();
    }
Example #9
0
 public static void register(Kryo kryo) {
   kryo.register(TestObject.class);
   kryo.register(MessageWithTestObject.class);
   kryo.register(StackTraceElement.class);
   kryo.register(StackTraceElement[].class);
   kryo.register(UnsupportedOperationException.class);
   ObjectSpace.registerClasses(kryo);
 }
 /**
  * register a class
  *
  * @param kryo
  * @param s name of a class - might not exist
  * @param handled Set of classes already handles
  */
 protected void doRegistration(final Kryo kryo, final Class pC) {
   if (kryo != null) {
     kryo.register(pC);
     // also register arrays of that class
     Class arrayType = Array.newInstance(pC, 0).getClass();
     kryo.register(arrayType);
   }
 }
Example #11
0
  private Vertex readVertex(
      final Direction directionRequested,
      final Function<DetachedVertex, Vertex> vertexMaker,
      final Function<DetachedEdge, Edge> edgeMaker,
      final Input input)
      throws IOException {
    if (null != directionRequested && null == edgeMaker)
      throw new IllegalArgumentException(
          "If a directionRequested is specified then an edgeAdder function should also be specified");

    this.headerReader.read(kryo, input);

    final DetachedVertex detachedVertex = (DetachedVertex) kryo.readClassAndObject(input);
    final Vertex v = vertexMaker.apply(detachedVertex);

    final boolean streamContainsEdgesInSomeDirection = input.readBoolean();
    if (!streamContainsEdgesInSomeDirection && directionRequested != null)
      throw new IllegalStateException(
          String.format(
              "The direction %s was requested but no attempt was made to serialize edges into this stream",
              directionRequested));

    // if there are edges in the stream and the direction is not present then the rest of the stream
    // is
    // simply ignored
    if (directionRequested != null) {
      final Direction directionsInStream = kryo.readObject(input, Direction.class);
      if (directionsInStream != Direction.BOTH && directionsInStream != directionRequested)
        throw new IllegalStateException(
            String.format(
                "Stream contains %s edges, but requesting %s",
                directionsInStream, directionRequested));

      final Direction firstDirection = kryo.readObject(input, Direction.class);
      if (firstDirection == Direction.OUT
          && (directionRequested == Direction.BOTH || directionRequested == Direction.OUT))
        readEdges(input, edgeMaker);
      else {
        // requested direction in, but BOTH must be serialized so skip this.  the
        // illegalstateexception
        // prior to this IF should  have caught a problem where IN is not supported at all
        if (firstDirection == Direction.OUT && directionRequested == Direction.IN) skipEdges(input);
      }

      if (directionRequested == Direction.BOTH || directionRequested == Direction.IN) {
        // if the first direction was OUT then it was either read or skipped.  in that case, the
        // marker
        // of the stream is currently ready to read the IN direction. otherwise it's in the perfect
        // place
        // to start reading edges
        if (firstDirection == Direction.OUT) kryo.readObject(input, Direction.class);

        readEdges(input, edgeMaker);
      }
    }

    return v;
  }
Example #12
0
 // Required by Kryo serialization.
 public void read(Kryo kryo, Input in) {
   intVal = kryo.readObject(in, Integer.class);
   floatVal = kryo.readObject(in, Float.class);
   shortVal = kryo.readObject(in, Short.class);
   byteArr = kryo.readObject(in, byte[].class);
   longArr = kryo.readObject(in, long[].class);
   dblArr = kryo.readObject(in, double[].class);
   str = kryo.readObject(in, String.class);
 }
Example #13
0
 @Override
 public Event fromBytes(byte[] bytes) {
   Kryo kryo = pool.borrow();
   try (Input in = new Input(bytes)) {
     return (Event) kryo.readClassAndObject(in);
   } finally {
     pool.release(kryo);
   }
 }
Example #14
0
 private void skipEdges(final Input input) {
   if (input.readBoolean()) {
     Object next = kryo.readClassAndObject(input);
     while (!next.equals(EdgeTerminator.INSTANCE)) {
       // next edge to skip or the terminator
       next = kryo.readClassAndObject(input);
     }
   }
 }
Example #15
0
 private void readEdges(final Input input, final Function<DetachedEdge, Edge> edgeMaker) {
   if (input.readBoolean()) {
     Object next = kryo.readClassAndObject(input);
     while (!next.equals(EdgeTerminator.INSTANCE)) {
       final DetachedEdge detachedEdge = (DetachedEdge) next;
       edgeMaker.apply(detachedEdge);
       next = kryo.readClassAndObject(input);
     }
   }
 }
Example #16
0
 @Override
 public byte[] toBytes(Event event) {
   Kryo kryo = pool.borrow();
   try (Output out = new Output(new java.io.ByteArrayOutputStream())) {
     kryo.writeClassAndObject(out, event);
     return out.toBytes();
   } finally {
     pool.release(kryo);
   }
 }
Example #17
0
 // Required by Kryo serialization.
 public void write(Kryo kryo, Output out) {
   kryo.writeObject(out, intVal);
   kryo.writeObject(out, floatVal);
   kryo.writeObject(out, shortVal);
   kryo.writeObject(out, byteArr);
   kryo.writeObject(out, longArr);
   kryo.writeObject(out, dblArr);
   kryo.writeObject(out, str);
   out.flush();
 }
 @Override
 public void write(Kryo kryo, Output output) {
   output.writeString(this.firstname);
   output.writeString(this.lastname);
   kryo.writeObjectOrNull(output, this.emailAddress, SerializableEmailAddress.class);
   output.writeInt(this.addresses.size());
   for (Address address : this.addresses) {
     kryo.writeObject(output, address);
   }
 }
 public DbMessageLogger() {
   kryo =
       new Kryo() {
         public Serializer newSerializer(Class aClass) {
           if (SubListSerializer.canSerialize(aClass)) return new SubListSerializer(this);
           return super.newSerializer(aClass);
         }
       };
   kryo.register(Arrays.asList("").getClass(), new ArraysAsListSerializer(kryo));
   kryo.setRegistrationOptional(true);
 }
 @Override
 public void read(Kryo kryo, Input input) {
   this.firstname = input.readString();
   this.lastname = input.readString();
   this.emailAddress = kryo.readObjectOrNull(input, SerializableEmailAddress.class);
   this.addresses = new HashSet<Address>();
   int numAddresses = input.readInt();
   for (int i = 0; i < numAddresses; i++) {
     this.add(kryo.readObject(input, SerializableAddress.class));
   }
 }
  /**
   * do the real work of registering all classes
   *
   * @param kryo
   */
  @Override
  public void registerClasses(@Nonnull Kryo kryo) {
    kryo.register(Object[].class);
    kryo.register(scala.Tuple2[].class);

    doRegistration(kryo, "scala.collection.mutable.WrappedArray$ofRef");
    doRegistration(kryo, "com.nielsen.perfengg.SparkD");

    // and many more similar nines

  }
  /**
   * Checks if the given class is registered with the RPC service.
   *
   * @param throwableType the class to check
   * @return <code>true</code> if the given class is registered with the RPC service, <code>false
   *     </code> otherwise
   */
  private boolean isThrowableRegistered(final Class<? extends Throwable> throwableType) {

    final Kryo kryo = KryoUtil.getKryo();
    try {
      kryo.getRegistration(throwableType);
    } catch (final IllegalArgumentException e) {
      return false;
    }

    return true;
  }
Example #23
0
 @Before
 public void setUp() throws Exception {
   StoreConfig config = new StoreConfig();
   config.setDataFileName(DATA_FILE_NAME);
   config.setDataMappedMemorySize(DATA_MAPPED_BUFFER_SIZE);
   config.setInitialFileSize(DATA_FILE_SIZE);
   Kryo kryo = new Kryo();
   kryo.register(VertexImpl.class);
   TypeResolver.registerClass(VertexImpl.class, (byte) 0);
   store = new VertexStore(config, kryo);
   store.start();
 }
 private static byte[] serializeObjectToKryo(Serializable object) {
   ByteArrayOutputStream baos = new ByteArrayOutputStream();
   Output output = new Output(baos);
   Kryo kryo = borrowKryo();
   try {
     kryo.writeObject(output, object);
   } finally {
     releaseKryo(kryo);
   }
   output.close();
   return baos.toByteArray();
 }
Example #25
0
 public void write(Kryo kryo, Output output, EnumSet object) {
   Serializer serializer;
   if (object.isEmpty()) {
     EnumSet tmp = EnumSet.complementOf(object);
     if (tmp.isEmpty())
       throw new KryoException("An EnumSet must have a defined Enum to be serialized.");
     serializer = kryo.writeClass(output, tmp.iterator().next().getClass()).getSerializer();
   } else {
     serializer = kryo.writeClass(output, object.iterator().next().getClass()).getSerializer();
   }
   output.writeInt(object.size(), true);
   for (Object element : object) serializer.write(kryo, output, element);
 }
  // This registers objects that are going to be sent over the network.
  public static void register(EndPoint endPoint) {
    Kryo kryo = endPoint.getKryo();
    kryo.register(RegisterName.class);
    kryo.register(String[].class);
    kryo.register(UpdateNames.class);
    kryo.register(ChatMessage.class);

    FieldSerializer<Data> dataClassSerializer = new FieldSerializer<Data>(kryo, Data.class);
    dataClassSerializer.getField("bytes").setClass(byte[].class, new ByteArraySerializer());
    dataClassSerializer.getField("string").setClass(String.class, new StringSerializer());
    dataClassSerializer.getField("string").setCanBeNull(true);
    kryo.register(Data.class, dataClassSerializer);
  }
Example #27
0
 public SerializablePage loadPage(InputStream in) {
   synchronized (serializeLock) {
     Input input = null;
     try {
       input = new Input(in);
       input.readString();
       kryo.readObject(input, UrlPageModel.class);
       return kryo.readObject(input, SerializablePage.class);
     } finally {
       IOUtils.closeQuietly(input);
     }
   }
 }
 private static <T extends Serializable> T deserializeObjectFromKryo(
     byte[] bytes, Class<T> clazz) {
   Input inp = new Input(new ByteArrayInputStream(bytes));
   Kryo kryo = borrowKryo();
   T func = null;
   try {
     func = kryo.readObject(inp, clazz);
   } finally {
     releaseKryo(kryo);
   }
   inp.close();
   return func;
 }
Example #29
0
  public static byte[] serialize(Object object) throws SerializeException {
    if (object == null) {
      throw new SerializeException("指向object对象指针为空");
    }

    Kryo kryo = kryoThreadLocal.get();
    ByteArrayOutputStream stream = new ByteArrayOutputStream(20000);
    Output output = new Output(stream);
    kryo.writeObject(output, object);
    output.close();

    return stream.toByteArray();
  }
Example #30
0
 public void savePage(
     OutputStream out, String title, UrlPageModel pageModel, SerializablePage page) {
   synchronized (serializeLock) {
     Output output = null;
     try {
       output = isHoneycomb() ? new KryoOutputHC(out) : new Output(out);
       output.writeString(title);
       kryo.writeObject(output, pageModel);
       kryo.writeObject(output, page);
     } finally {
       IOUtils.closeQuietly(output);
     }
   }
 }