@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);
 }
예제 #2
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);
    }
  }
예제 #3
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);
     }
   }
 }
예제 #4
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);
     }
   }
 }
예제 #5
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();
    }
예제 #6
0
  /**
   * Reads through the all the edges for a vertex and writes the edges to a temp file which will be
   * read later.
   */
  private void readToEndOfEdgesAndWriteToTemp(final Input input, final Output output)
      throws IOException {
    Object next = kryo.readClassAndObject(input);
    while (!next.equals(EdgeTerminator.INSTANCE)) {
      kryo.writeClassAndObject(output, next);

      // next edge or terminator
      next = kryo.readClassAndObject(input);
    }

    // this should be the vertex terminator
    kryo.readClassAndObject(input);

    kryo.writeClassAndObject(output, EdgeTerminator.INSTANCE);
    kryo.writeClassAndObject(output, VertexTerminator.INSTANCE);
  }
예제 #7
0
 @Override
 public Object decode(final CachedData d) {
   final byte[] bytes = d.getData();
   try (final Input input = new Input(new ByteArrayInputStream(bytes))) {
     final Object obj = kryo.readClassAndObject(input);
     return obj;
   }
 }
  @Test
  public void test_kryo() {
    Kryo kryo = new Kryo();
    kryo.register(AdapterWfLaunchInfo.class);
    {
      Output output = new Output(1024);
      final AdapterWfLaunchInfo object = new AdapterWfLaunchInfo();
      object.setAdapterName("abc");
      kryo.writeClassAndObject(output, object);
      kryo.writeClassAndObject(output, new LogEvent());
      assertTrue(output.getBuffer().length > 0);

      Input input = new Input(output.getBuffer());
      assertEquals(AdapterWfLaunchInfo.class, kryo.readClassAndObject(input).getClass());
      assertEquals(LogEvent.class, kryo.readClassAndObject(input).getClass());
    }
  }
예제 #9
0
 @Override
 public Edge readEdge(final InputStream inputStream, final Function<DetachedEdge, Edge> edgeMaker)
     throws IOException {
   final Input input = new Input(inputStream);
   this.headerReader.read(kryo, input);
   final Object o = kryo.readClassAndObject(input);
   return edgeMaker.apply((DetachedEdge) o);
 }
예제 #10
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;
  }
예제 #11
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);
   }
 }
  @Override
  public ExtensionCriterion read(Kryo kryo, Input input, Class<ExtensionCriterion> type) {
    ExtensionSelectorType exType = (ExtensionSelectorType) kryo.readClassAndObject(input);
    DeviceId deviceId = (DeviceId) kryo.readClassAndObject(input);
    DriverService driverService = DefaultServiceDirectory.getService(DriverService.class);
    byte[] bytes = (byte[]) kryo.readClassAndObject(input);
    ExtensionSelector selector;

    try {
      DriverHandler handler =
          new DefaultDriverHandler(
              new DefaultDriverData(driverService.getDriver(deviceId), deviceId));
      ExtensionSelectorResolver resolver = handler.behaviour(ExtensionSelectorResolver.class);
      selector = resolver.getExtensionSelector(exType);
      selector.deserialize(bytes);
    } catch (ItemNotFoundException | IllegalArgumentException e) {
      selector = new UnresolvedExtensionSelector(bytes, exType);
    }

    return Criteria.extension(selector, deviceId);
  }
예제 #13
0
 @Override
 public List<?> read(final Kryo kryo, final Input input, final Class<List<?>> type) {
   final int length = input.readInt(true);
   Class<?> componentType = kryo.readClass(input).getType();
   if (componentType.isPrimitive()) {
     componentType = getPrimitiveWrapperClass(componentType);
   }
   try {
     final Object items = Array.newInstance(componentType, length);
     for (int i = 0; i < length; i++) {
       Array.set(items, i, kryo.readClassAndObject(input));
     }
     return Arrays.asList((Object[]) items);
   } catch (final Exception e) {
     throw new RuntimeException(e);
   }
 }
  /** {@inheritDoc} */
  @SuppressWarnings("unchecked")
  public Object read(Connection connection, InputStream inputStream) {
    Input input = new Input(inputStream);
    IKryoProvider kryoProvider = serializerQueue.poll();

    // if nothing is available in queue don't wait, create new one
    if (null == kryoProvider) {
      kryoProvider = createKryoProvider();
    }

    try {
      Kryo kryo = kryoProvider.getKryo();
      kryo.getContext().put("connection", connection);
      return kryo.readClassAndObject(input);
    } finally {
      serializerQueue.offer(kryoProvider);
    }
  }
 @Override
 public Object read(final Kryo kryo, final Input input, final Class<Object> type) {
   final InvocationHandler invocationHandler = (InvocationHandler) kryo.readClassAndObject(input);
   final Class<?>[] interfaces = kryo.readObject(input, Class[].class);
   final ClassLoader classLoader = kryo.getClass().getClassLoader(); // TODO: can we do this?
   try {
     return Proxy.newProxyInstance(classLoader, interfaces, invocationHandler);
   } catch (final RuntimeException e) {
     System.err.println(
         getClass().getName()
             + ".read:\n"
             + "Could not create proxy using classLoader "
             + classLoader
             + ","
             + " have invocationhandler.classloader: "
             + invocationHandler.getClass().getClassLoader()
             + " have contextclassloader: "
             + Thread.currentThread().getContextClassLoader());
     throw e;
   }
 }
  public static void main(String[] args) throws Exception {
    Kryo kryo = IObject.getKryo();
    Input input = new Input(new FileInputStream("results_around5_2clusters.dat"));
    List<Configuration> confs = (List<Configuration>) kryo.readClassAndObject(input);
    input.close();
    for (Configuration c : confs) {
      System.out.println(c);
      for (String dataset : c.accuracies.keySet())
        System.out.println(
            dataset
                + ": "
                + Arrays.toString(c.accuracies.get(dataset)[2])
                + " "
                + Arrays.toString(c.mries.get(dataset)[2]));
    }
    /*
    Kryo kryo = IObject.getKryo();
    Input input = new Input(new FileInputStream("results_j48.dat"));
    List<Configuration> confsj48 = (List<Configuration>) kryo.readClassAndObject(input);
    input.close();
    int bestconfj48index = getBestConfIndex(confsj48);
    input = new Input(new FileInputStream("results_knn.dat"));
    List<Configuration> confsknn = (List<Configuration>) kryo.readClassAndObject(input);
    input.close();
    int bestconfknnindex = getBestConfIndex(confsknn);

    System.out.println(confsj48.get(bestconfknnindex));

    System.out.println(confsknn.get(bestconfj48index));

    Configuration p10j48 = getConfiguration(confsj48, 10, 0.15, 0.0, 0.0, true, 3, WekaClassifier.class);
    Configuration p10knn = getConfiguration(confsknn, 10, 0.15, 0.0, 0.0, true, 3, KNNClassifier.class);

    System.out.println(p10j48);
    System.out.println(p10knn);
    */
  }
예제 #17
0
 @SuppressWarnings("unchecked")
 @Override
 public void read(Kryo kryo, Input input) {
   this.nodes = (Node[]) kryo.readClassAndObject(input);
   this.funcs = (Map<Node, BaseValueFunction>) kryo.readClassAndObject(input);
 }
예제 #18
0
 @Override
 public MastershipTerm read(Kryo kryo, Input input, Class<MastershipTerm> type) {
   final NodeId node = (NodeId) kryo.readClassAndObject(input);
   final int term = input.readInt();
   return MastershipTerm.of(node, term);
 }
 private T deserialise(byte[] data) {
   ByteBuffer buffer = ByteBuffer.wrap(data);
   T notification = (T) kryo.readClassAndObject(buffer);
   return notification;
 }
 @Override
 public Object deserialize(byte[] input) {
   kryoIn.setBuffer(input);
   return kryo.readClassAndObject(kryoIn);
 }
 @Override
 public void read(Kryo kryo, Input input) {
   setUser(kryo.readObject(input, KryoUser.class));
   setPassword(input.readString());
   setFriends((List<User>) kryo.readClassAndObject(input));
 }
예제 #22
0
  @Override
  public void readGraph(final InputStream inputStream, final Graph graphToWriteTo)
      throws IOException {
    this.counter.set(0);
    final Input input = new Input(inputStream);
    this.headerReader.read(kryo, input);

    final BatchGraph graph;
    try {
      // will throw an exception if not constructed properly
      graph =
          BatchGraph.build(graphToWriteTo)
              .vertexIdKey(vertexIdKey)
              .edgeIdKey(edgeIdKey)
              .bufferSize(batchSize)
              .create();
    } catch (Exception ex) {
      throw new IOException("Could not instantiate BatchGraph wrapper", ex);
    }

    try (final Output output = new Output(new FileOutputStream(tempFile))) {
      final boolean supportedMemory = input.readBoolean();
      if (supportedMemory) {
        // if the graph that serialized the data supported sideEffects then the sideEffects needs to
        // be read
        // to advance the reader forward.  if the graph being read into doesn't support the
        // sideEffects
        // then we just setting the data to sideEffects.
        final Map<String, Object> memMap =
            (Map<String, Object>) kryo.readObject(input, HashMap.class);
        if (graphToWriteTo.features().graph().variables().supportsVariables()) {
          final Graph.Variables variables = graphToWriteTo.variables();
          memMap.forEach(variables::set);
        }
      }

      final boolean hasSomeVertices = input.readBoolean();
      if (hasSomeVertices) {
        while (!input.eof()) {
          final List<Object> vertexArgs = new ArrayList<>();
          final DetachedVertex current = (DetachedVertex) kryo.readClassAndObject(input);
          appendToArgList(vertexArgs, T.id, current.id());
          appendToArgList(vertexArgs, T.label, current.label());

          final Vertex v = graph.addVertex(vertexArgs.toArray());
          current
              .iterators()
              .propertyIterator()
              .forEachRemaining(p -> createVertexProperty(graphToWriteTo, v, p, false));
          // current.iterators().hiddenPropertyIterator().forEachRemaining(p ->
          // createVertexProperty(graphToWriteTo, v, p, true));

          // the gio file should have been written with a direction specified
          final boolean hasDirectionSpecified = input.readBoolean();
          final Direction directionInStream = kryo.readObject(input, Direction.class);
          final Direction directionOfEdgeBatch = kryo.readObject(input, Direction.class);

          // graph serialization requires that a direction be specified in the stream and that the
          // direction of the edges be OUT
          if (!hasDirectionSpecified
              || directionInStream != Direction.OUT
              || directionOfEdgeBatch != Direction.OUT)
            throw new IllegalStateException(
                String.format(
                    "Stream must specify edge direction and that direction must be %s",
                    Direction.OUT));

          // if there are edges then read them to end and write to temp, otherwise read what should
          // be
          // the vertex terminator
          if (!input.readBoolean()) kryo.readClassAndObject(input);
          else readToEndOfEdgesAndWriteToTemp(input, output);
        }
      }
    } catch (Exception ex) {
      throw new IOException(ex);
    }
    // done writing to temp

    // start reading in the edges now from the temp file
    try (final Input edgeInput = new Input(new FileInputStream(tempFile))) {
      readFromTempEdges(edgeInput, graph);
      graph.tx().commit();
    } catch (Exception ex) {
      ex.printStackTrace();
      throw new IOException(ex);
    } finally {
      deleteTempFileSilently();
    }
  }
예제 #23
0
 protected TreeSet create(Kryo kryo, Input input, Class<Collection> type) {
   return createTreeSet(type, (Comparator) kryo.readClassAndObject(input));
 }
예제 #24
0
 public Set read(Kryo kryo, Input input, Class type) {
   return Collections.singleton(kryo.readClassAndObject(input));
 }
예제 #25
0
 protected Map create(Kryo kryo, Input input, Class<Map> type) {
   return createTreeMap(type, (Comparator) kryo.readClassAndObject(input));
 }
 @Override
 public OperationTree read(Kryo kryo, Input input, Class<OperationTree> type) {
   return util.toNode((Operation) kryo.readClassAndObject(input));
 }
예제 #27
0
 public Map read(Kryo kryo, Input input, Class type) {
   Object key = kryo.readClassAndObject(input);
   Object value = kryo.readClassAndObject(input);
   return Collections.singletonMap(key, value);
 }