Exemplo n.º 1
0
  private static Node deserializeNode(DataInput input) throws IOException {
    int level = input.readUnsignedByte();
    long value = input.readLong();
    double weight = input.readDouble();

    return new Node(value, level, weight);
  }
Exemplo n.º 2
0
  public static QuantileDigest deserialize(DataInput input) {
    try {
      double maxError = input.readDouble();
      double alpha = input.readDouble();

      QuantileDigest result = new QuantileDigest(maxError, alpha);

      result.landmarkInSeconds = input.readLong();
      result.min = input.readLong();
      result.max = input.readLong();
      result.totalNodeCount = input.readInt();

      Deque<Node> stack = new ArrayDeque<>();
      for (int i = 0; i < result.totalNodeCount; i++) {
        int flags = input.readByte();

        Node node = deserializeNode(input);

        if ((flags & Flags.HAS_RIGHT) != 0) {
          node.right = stack.pop();
        }

        if ((flags & Flags.HAS_LEFT) != 0) {
          node.left = stack.pop();
        }

        stack.push(node);
        result.weightedCount += node.weightedCount;
        if (node.weightedCount >= ZERO_WEIGHT_THRESHOLD) {
          result.nonZeroNodeCount++;
        }
      }

      if (!stack.isEmpty()) {
        Preconditions.checkArgument(
            stack.size() == 1, "Tree is corrupted. Expected a single root node");
        result.root = stack.pop();
      }

      return result;
    } catch (IOException e) {
      throw Throwables.propagate(e);
    }
  }
Exemplo n.º 3
0
 // Used for internal Hadoop purposes
 // Describes how to read this node from across a network
 public void readFields(DataInput in) throws IOException {
   nodeid = in.readLong();
   pageRank = in.readDouble();
   long next = in.readLong();
   ArrayList<Long> ins = new ArrayList<Long>();
   while (next != -1) {
     ins.add(next);
     next = in.readLong();
   }
   outgoing = new long[ins.size()];
   for (int i = 0; i < ins.size(); i++) {
     outgoing[i] = ins.get(i);
   }
 }
  /**
   * Reads a primitive value of the specified class type from the stream.
   *
   * @param in A stream to read from.
   * @param cls A class type of the primitive.
   * @return A primitive.
   * @throws IOException If an I/O error occurs.
   */
  static Object readPrimitive(DataInput in, Class cls) throws IOException {
    if (cls == byte.class) return in.readByte();

    if (cls == short.class) return in.readShort();

    if (cls == int.class) return in.readInt();

    if (cls == long.class) return in.readLong();

    if (cls == float.class) return in.readFloat();

    if (cls == double.class) return in.readDouble();

    if (cls == boolean.class) return in.readBoolean();

    if (cls == char.class) return in.readChar();

    throw new IllegalArgumentException();
  }
Exemplo n.º 5
0
  @Override
  public void readFields(DataInput in) throws IOException {
    /*
     * extract pkt len.
     *
     * GPSQL-1107:
     * The DataInput might already be empty (EOF), but we can't check it beforehand.
     * If that's the case, pktlen is updated to -1, to mark that the object is still empty.
     * (can be checked with isEmpty()).
     */
    pktlen = readPktLen(in);
    if (isEmpty()) {
      return;
    }

    /* extract the version and col cnt */
    int version = in.readShort();
    int curOffset = 4 + 2;
    int colCnt;

    /* !!! Check VERSION !!! */
    if (version != GPDBWritable.VERSION && version != GPDBWritable.PREV_VERSION) {
      throw new IOException(
          "Current GPDBWritable version("
              + GPDBWritable.VERSION
              + ") does not match input version("
              + version
              + ")");
    }

    if (version == GPDBWritable.VERSION) {
      errorFlag = in.readByte();
      curOffset += 1;
    }

    colCnt = in.readShort();
    curOffset += 2;

    /* Extract Column Type */
    colType = new int[colCnt];
    DBType[] coldbtype = new DBType[colCnt];
    for (int i = 0; i < colCnt; i++) {
      int enumType = (in.readByte());
      curOffset += 1;
      if (enumType == DBType.BIGINT.ordinal()) {
        colType[i] = BIGINT.getOID();
        coldbtype[i] = DBType.BIGINT;
      } else if (enumType == DBType.BOOLEAN.ordinal()) {
        colType[i] = BOOLEAN.getOID();
        coldbtype[i] = DBType.BOOLEAN;
      } else if (enumType == DBType.FLOAT8.ordinal()) {
        colType[i] = FLOAT8.getOID();
        coldbtype[i] = DBType.FLOAT8;
      } else if (enumType == DBType.INTEGER.ordinal()) {
        colType[i] = INTEGER.getOID();
        coldbtype[i] = DBType.INTEGER;
      } else if (enumType == DBType.REAL.ordinal()) {
        colType[i] = REAL.getOID();
        coldbtype[i] = DBType.REAL;
      } else if (enumType == DBType.SMALLINT.ordinal()) {
        colType[i] = SMALLINT.getOID();
        coldbtype[i] = DBType.SMALLINT;
      } else if (enumType == DBType.BYTEA.ordinal()) {
        colType[i] = BYTEA.getOID();
        coldbtype[i] = DBType.BYTEA;
      } else if (enumType == DBType.TEXT.ordinal()) {
        colType[i] = TEXT.getOID();
        coldbtype[i] = DBType.TEXT;
      } else {
        throw new IOException("Unknown GPDBWritable.DBType ordinal value");
      }
    }

    /* Extract null bit array */
    byte[] nullbytes = new byte[getNullByteArraySize(colCnt)];
    in.readFully(nullbytes);
    curOffset += nullbytes.length;
    boolean[] colIsNull = byteArrayToBooleanArray(nullbytes, colCnt);

    /* extract column value */
    colValue = new Object[colCnt];
    for (int i = 0; i < colCnt; i++) {
      if (!colIsNull[i]) {
        /* Skip the alignment padding */
        int skipbytes = roundUpAlignment(curOffset, coldbtype[i].getAlignment()) - curOffset;
        for (int j = 0; j < skipbytes; j++) {
          in.readByte();
        }
        curOffset += skipbytes;

        /* For fixed length type, increment the offset according to type type length here.
         * For var length type (BYTEA, TEXT), we'll read 4 byte length header and the
         * actual payload.
         */
        int varcollen = -1;
        if (coldbtype[i].isVarLength()) {
          varcollen = in.readInt();
          curOffset += 4 + varcollen;
        } else {
          curOffset += coldbtype[i].getTypeLength();
        }

        switch (DataType.get(colType[i])) {
          case BIGINT:
            {
              colValue[i] = in.readLong();
              break;
            }
          case BOOLEAN:
            {
              colValue[i] = in.readBoolean();
              break;
            }
          case FLOAT8:
            {
              colValue[i] = in.readDouble();
              break;
            }
          case INTEGER:
            {
              colValue[i] = in.readInt();
              break;
            }
          case REAL:
            {
              colValue[i] = in.readFloat();
              break;
            }
          case SMALLINT:
            {
              colValue[i] = in.readShort();
              break;
            }

            /* For BYTEA column, it has a 4 byte var length header. */
          case BYTEA:
            {
              colValue[i] = new byte[varcollen];
              in.readFully((byte[]) colValue[i]);
              break;
            }
            /* For text formatted column, it has a 4 byte var length header
             * and it's always null terminated string.
             * So, we can remove the last "\0" when constructing the string.
             */
          case TEXT:
            {
              byte[] data = new byte[varcollen];
              in.readFully(data, 0, varcollen);
              colValue[i] = new String(data, 0, varcollen - 1, CHARSET);
              break;
            }

          default:
            throw new IOException("Unknown GPDBWritable ColType");
        }
      }
    }

    /* Skip the ending alignment padding */
    int skipbytes = roundUpAlignment(curOffset, 8) - curOffset;
    for (int j = 0; j < skipbytes; j++) {
      in.readByte();
    }
    curOffset += skipbytes;

    if (errorFlag != 0) {
      throw new IOException("Received error value " + errorFlag + " from format");
    }
  }
Exemplo n.º 6
0
  /** Read a {@link Writable}, {@link String}, primitive type, or an array of the preceding. */
  @SuppressWarnings("unchecked")
  public static Object readObject(DataInput in, ObjectWritable objectWritable, Configuration conf)
      throws IOException {
    String className = UTF8.readString(in);
    Class<?> declaredClass = PRIMITIVE_NAMES.get(className);
    if (declaredClass == null) {
      declaredClass = loadClass(conf, className);
    }

    Object instance;

    if (declaredClass.isPrimitive()) { // primitive types

      if (declaredClass == Boolean.TYPE) { // boolean
        instance = Boolean.valueOf(in.readBoolean());
      } else if (declaredClass == Character.TYPE) { // char
        instance = Character.valueOf(in.readChar());
      } else if (declaredClass == Byte.TYPE) { // byte
        instance = Byte.valueOf(in.readByte());
      } else if (declaredClass == Short.TYPE) { // short
        instance = Short.valueOf(in.readShort());
      } else if (declaredClass == Integer.TYPE) { // int
        instance = Integer.valueOf(in.readInt());
      } else if (declaredClass == Long.TYPE) { // long
        instance = Long.valueOf(in.readLong());
      } else if (declaredClass == Float.TYPE) { // float
        instance = Float.valueOf(in.readFloat());
      } else if (declaredClass == Double.TYPE) { // double
        instance = Double.valueOf(in.readDouble());
      } else if (declaredClass == Void.TYPE) { // void
        instance = null;
      } else {
        throw new IllegalArgumentException("Not a primitive: " + declaredClass);
      }

    } else if (declaredClass.isArray()) { // array
      int length = in.readInt();
      instance = Array.newInstance(declaredClass.getComponentType(), length);
      for (int i = 0; i < length; i++) {
        Array.set(instance, i, readObject(in, conf));
      }

    } else if (declaredClass == String.class) { // String
      instance = UTF8.readString(in);
    } else if (declaredClass.isEnum()) { // enum
      instance = Enum.valueOf((Class<? extends Enum>) declaredClass, UTF8.readString(in));
    } else { // Writable
      Class instanceClass = null;
      String str = UTF8.readString(in);
      instanceClass = loadClass(conf, str);

      Writable writable = WritableFactories.newInstance(instanceClass, conf);
      writable.readFields(in);
      instance = writable;

      if (instanceClass == NullInstance.class) { // null
        declaredClass = ((NullInstance) instance).declaredClass;
        instance = null;
      }
    }

    if (objectWritable != null) { // store values
      objectWritable.declaredClass = declaredClass;
      objectWritable.instance = instance;
    }

    return instance;
  }