Package org.apache.hadoop.io

Examples of org.apache.hadoop.io.BooleanWritable$Comparator


        BSPPeer<Text, Text, NullWritable, NullWritable, MapWritable> peer)
        throws IOException, SyncException, InterruptedException {
      Text key = null;
      Text value = null;
      MapWritable message = new MapWritable();
      message.put(new Text(KeyValueHashPartitionedBSP.TEST_UNEXPECTED_KEYS), new BooleanWritable(false));
      KeyValuePair<Text, Text> tmp = null;

      while ( (tmp = peer.readNext()) != null) {
        key = tmp.getKey();
        value = tmp.getValue();

        int expectedPeerId = Math.abs(key.hashCode() % numTasks);

        if (expectedPeerId == peer.getPeerIndex()) {
          if (expectedKeys.containsKey(key)) {
            // same key twice, incorrect
            message.put(new Text(KeyValueHashPartitionedBSP.TEST_UNEXPECTED_KEYS), new BooleanWritable(true));
            break;
          } else {
            expectedKeys.put(new Text(key), new Text(value));
          }
        } else {
          message.put(new Text(KeyValueHashPartitionedBSP.TEST_UNEXPECTED_KEYS), new BooleanWritable(true));
          break;
        } //if (expectedPeerId == peer.getPeerIndex())
      } //while (peer.readNext(key, value) != false)
      message.put(new Text(KeyValueHashPartitionedBSP.TEST_INPUT_VALUES), expectedKeys);
     
      int master = peer.getNumPeers()/2;
      String masterName = peer.getPeerName(master);
      peer.send(masterName, message);
      peer.sync();

      if(peer.getPeerIndex() == master) {
        MapWritable msg = null;
        MapWritable values = null;
        BooleanWritable blValue = null;
        HashMap<Integer, Integer> input = new HashMap<Integer, Integer>();

        while ( (msg = peer.getCurrentMessage()) != null ) {
          blValue = (BooleanWritable) msg.get(new Text(KeyValueHashPartitionedBSP.TEST_UNEXPECTED_KEYS));
          assertEquals(false, blValue.get());
          values = (MapWritable) msg.get(new Text(KeyValueHashPartitionedBSP.TEST_INPUT_VALUES));
          for (Map.Entry<Writable,Writable> w : values.entrySet()) {
            input.put( Integer.valueOf( w.getKey().toString() ), Integer.valueOf( w.getValue().toString() ));
          }
        }
View Full Code Here


      switch (ptype.getPrimitiveCategory()) {
      case VOID: {
        return null;
      }
      case BOOLEAN: {
        BooleanWritable r = reuse == null ? new BooleanWritable()
            : (BooleanWritable) reuse;
        byte b = buffer.read(invert);
        assert (b == 1 || b == 2);
        r.set(b == 2);
        return r;
      }
      case BYTE: {
        ByteWritable r = reuse == null ? new ByteWritable()
            : (ByteWritable) reuse;
        r.set((byte) (buffer.read(invert) ^ 0x80));
        return r;
      }
      case SHORT: {
        ShortWritable r = reuse == null ? new ShortWritable()
            : (ShortWritable) reuse;
        int v = buffer.read(invert) ^ 0x80;
        v = (v << 8) + (buffer.read(invert) & 0xff);
        r.set((short) v);
        return r;
      }
      case INT: {
        IntWritable r = reuse == null ? new IntWritable() : (IntWritable) reuse;
        int v = buffer.read(invert) ^ 0x80;
        for (int i = 0; i < 3; i++) {
          v = (v << 8) + (buffer.read(invert) & 0xff);
        }
        r.set(v);
        return r;
      }
      case LONG: {
        LongWritable r = reuse == null ? new LongWritable()
            : (LongWritable) reuse;
        long v = buffer.read(invert) ^ 0x80;
        for (int i = 0; i < 7; i++) {
          v = (v << 8) + (buffer.read(invert) & 0xff);
        }
        r.set(v);
        return r;
      }
      case FLOAT: {
        FloatWritable r = reuse == null ? new FloatWritable()
            : (FloatWritable) reuse;
        int v = 0;
        for (int i = 0; i < 4; i++) {
          v = (v << 8) + (buffer.read(invert) & 0xff);
        }
        if ((v & (1 << 31)) == 0) {
          // negative number, flip all bits
          v = ~v;
        } else {
          // positive number, flip the first bit
          v = v ^ (1 << 31);
        }
        r.set(Float.intBitsToFloat(v));
        return r;
      }
      case DOUBLE: {
        DoubleWritable r = reuse == null ? new DoubleWritable()
            : (DoubleWritable) reuse;
        long v = 0;
        for (int i = 0; i < 8; i++) {
          v = (v << 8) + (buffer.read(invert) & 0xff);
        }
        if ((v & (1L << 63)) == 0) {
          // negative number, flip all bits
          v = ~v;
        } else {
          // positive number, flip the first bit
          v = v ^ (1L << 63);
        }
        r.set(Double.longBitsToDouble(v));
        return r;
      }
      case STRING: {
        Text r = reuse == null ? new Text() : (Text) reuse;
        // Get the actual length first
        int start = buffer.tell();
        int length = 0;
        do {
          byte b = buffer.read(invert);
          if (b == 0) {
            // end of string
            break;
          }
          if (b == 1) {
            // the last char is an escape char. read the actual char
            buffer.read(invert);
          }
          length++;
        } while (true);

        if (length == buffer.tell() - start) {
          // No escaping happened, so we are already done.
          r.set(buffer.getData(), start, length);
        } else {
          // Escaping happened, we need to copy byte-by-byte.
          // 1. Set the length first.
          r.set(buffer.getData(), start, length);
          // 2. Reset the pointer.
          buffer.seek(start);
          // 3. Copy the data.
          byte[] rdata = r.getBytes();
          for (int i = 0; i < length; i++) {
            byte b = buffer.read(invert);
            if (b == 1) {
              // The last char is an escape char, read the actual char.
              // The serialization format escape \0 to \1, and \1 to \2,
              // to make sure the string is null-terminated.
              b = (byte) (buffer.read(invert) - 1);
            }
            rdata[i] = b;
          }
          // 4. Read the null terminator.
          byte b = buffer.read(invert);
          assert (b == 0);
        }
        return r;
      }

      case BINARY: {
        BytesWritable bw = new BytesWritable() ;
        // Get the actual length first
        int start = buffer.tell();
        int length = 0;
        do {
          byte b = buffer.read(invert);
          if (b == 0) {
            // end of string
            break;
          }
          if (b == 1) {
            // the last char is an escape char. read the actual char
            buffer.read(invert);
          }
          length++;
        } while (true);

        if (length == buffer.tell() - start) {
          // No escaping happened, so we are already done.
          bw.set(buffer.getData(), start, length);
        } else {
          // Escaping happened, we need to copy byte-by-byte.
          // 1. Set the length first.
          bw.set(buffer.getData(), start, length);
          // 2. Reset the pointer.
          buffer.seek(start);
          // 3. Copy the data.
          byte[] rdata = bw.getBytes();
          for (int i = 0; i < length; i++) {
            byte b = buffer.read(invert);
            if (b == 1) {
              // The last char is an escape char, read the actual char.
              // The serialization format escape \0 to \1, and \1 to \2,
              // to make sure the string is null-terminated.
              b = (byte) (buffer.read(invert) - 1);
            }
            rdata[i] = b;
          }
          // 4. Read the null terminator.
          byte b = buffer.read(invert);
          assert (b == 0);
        }
        return bw;
      }

      case TIMESTAMP:
        TimestampWritable t = (reuse == null ? new TimestampWritable() :
            (TimestampWritable) reuse);
        byte[] bytes = new byte[8];

        for (int i = 0; i < bytes.length; i++) {
          bytes[i] = buffer.read(invert);
        }
        t.setBinarySortable(bytes, 0);
        return t;

      default: {
        throw new RuntimeException("Unrecognized type: "
            + ptype.getPrimitiveCategory());
      }
      }
    }
    case LIST: {
      ListTypeInfo ltype = (ListTypeInfo) type;
      TypeInfo etype = ltype.getListElementTypeInfo();

      // Create the list if needed
      ArrayList<Object> r = reuse == null ? new ArrayList<Object>()
          : (ArrayList<Object>) reuse;

      // Read the list
      int size = 0;
      while (true) {
        int more = buffer.read(invert);
        if (more == 0) {
          // \0 to terminate
          break;
        }
        // \1 followed by each element
        assert (more == 1);
        if (size == r.size()) {
          r.add(null);
        }
        r.set(size, deserialize(buffer, etype, invert, r.get(size)));
        size++;
      }
      // Remove additional elements if the list is reused
      while (r.size() > size) {
        r.remove(r.size() - 1);
      }
      return r;
    }
    case MAP: {
      MapTypeInfo mtype = (MapTypeInfo) type;
      TypeInfo ktype = mtype.getMapKeyTypeInfo();
      TypeInfo vtype = mtype.getMapValueTypeInfo();

      // Create the map if needed
      Map<Object, Object> r;
      if (reuse == null) {
        r = new HashMap<Object, Object>();
      } else {
        r = (HashMap<Object, Object>) reuse;
        r.clear();
      }

      while (true) {
        int more = buffer.read(invert);
        if (more == 0) {
          // \0 to terminate
          break;
        }
        // \1 followed by each key and then each value
        assert (more == 1);
        Object k = deserialize(buffer, ktype, invert, null);
        Object v = deserialize(buffer, vtype, invert, null);
        r.put(k, v);
      }
      return r;
    }
    case STRUCT: {
      StructTypeInfo stype = (StructTypeInfo) type;
      List<TypeInfo> fieldTypes = stype.getAllStructFieldTypeInfos();
      int size = fieldTypes.size();
      // Create the struct if needed
      ArrayList<Object> r = reuse == null ? new ArrayList<Object>(size)
          : (ArrayList<Object>) reuse;
      assert (r.size() <= size);
      // Set the size of the struct
      while (r.size() < size) {
        r.add(null);
      }
      // Read one field by one field
      for (int eid = 0; eid < size; eid++) {
        r
            .set(eid, deserialize(buffer, fieldTypes.get(eid), invert, r
            .get(eid)));
      }
      return r;
    }
    case UNION: {
      UnionTypeInfo utype = (UnionTypeInfo) type;
      StandardUnion r = reuse == null ? new StandardUnion()
          : (StandardUnion) reuse;
      // Read the tag
      byte tag = buffer.read(invert);
      r.setTag(tag);
      r.setObject(deserialize(buffer, utype.getAllUnionObjectTypeInfos().get(tag),
          invert, null));
      return r;
    }
    default: {
      throw new RuntimeException("Unrecognized type: " + type.getCategory());
View Full Code Here

      case VOID: {
        return null;
      }

      case BOOLEAN: {
        BooleanWritable r = reuse == null ? new BooleanWritable()
            : (BooleanWritable) reuse;
        r = in.readBoolean(r);
        return r;
      }
      case BYTE: {
View Full Code Here

      case VOID: {
        return;
      }
      case BOOLEAN: {
        BooleanObjectInspector boi = (BooleanObjectInspector) poi;
        BooleanWritable r = reuse == null ? new BooleanWritable()
            : (BooleanWritable) reuse;
        r.set(boi.get(o));
        tbOut.write(r);
        return;
      }
      case BYTE: {
        ByteObjectInspector boi = (ByteObjectInspector) poi;
        ByteWritable r = reuse == null ? new ByteWritable()
            : (ByteWritable) reuse;
        r.set(boi.get(o));
        tbOut.write(r);
        return;
      }
      case SHORT: {
        ShortObjectInspector spoi = (ShortObjectInspector) poi;
        ShortWritable r = reuse == null ? new ShortWritable()
            : (ShortWritable) reuse;
        r.set(spoi.get(o));
        tbOut.write(r);
        return;
      }
      case INT: {
        IntObjectInspector ioi = (IntObjectInspector) poi;
        IntWritable r = reuse == null ? new IntWritable() : (IntWritable) reuse;
        r.set(ioi.get(o));
        tbOut.write(r);
        return;
      }
      case LONG: {
        LongObjectInspector loi = (LongObjectInspector) poi;
        LongWritable r = reuse == null ? new LongWritable()
            : (LongWritable) reuse;
        r.set(loi.get(o));
        tbOut.write(r);
        return;
      }
      case FLOAT: {
        FloatObjectInspector foi = (FloatObjectInspector) poi;
        FloatWritable r = reuse == null ? new FloatWritable()
            : (FloatWritable) reuse;
        r.set(foi.get(o));
        tbOut.write(r);
        return;
      }
      case DOUBLE: {
        DoubleObjectInspector doi = (DoubleObjectInspector) poi;
        DoubleWritable r = reuse == null ? new DoubleWritable()
            : (DoubleWritable) reuse;
        r.set(doi.get(o));
        tbOut.write(r);
        return;
      }
      case STRING: {
        StringObjectInspector soi = (StringObjectInspector) poi;
View Full Code Here

  @Test
  public void testAndAggregator() {
    BooleanAndAggregator and = new BooleanAndAggregator();
    assertEquals(true, and.getAggregatedValue().get());
    and.aggregate(new BooleanWritable(true));
    assertEquals(true, and.getAggregatedValue().get());
    and.aggregate(new BooleanWritable(false));
    assertEquals(false, and.getAggregatedValue().get());
    and.setAggregatedValue(new BooleanWritable(true));
    assertEquals(true, and.getAggregatedValue().get());
    BooleanWritable bw = and.createInitialValue();
    assertNotNull(bw);
  }
View Full Code Here

  @Test
  public void testOrAggregator() {
    BooleanOrAggregator or = new BooleanOrAggregator();
    assertEquals(false, or.getAggregatedValue().get());
    or.aggregate(new BooleanWritable(false));
    assertEquals(false, or.getAggregatedValue().get());
    or.aggregate(new BooleanWritable(true));
    assertEquals(true, or.getAggregatedValue().get());
    or.setAggregatedValue(new BooleanWritable(false));
    assertEquals(false, or.getAggregatedValue().get());
    BooleanWritable bw = or.createInitialValue();
    assertNotNull(bw);
  }
View Full Code Here

  }

  @Test
  public void testOverwriteAggregator() {
    BooleanOverwriteAggregator overwrite = new BooleanOverwriteAggregator();
    overwrite.aggregate(new BooleanWritable(true));
    assertEquals(true, overwrite.getAggregatedValue().get());
    overwrite.aggregate(new BooleanWritable(false));
    assertEquals(false, overwrite.getAggregatedValue().get());
    overwrite.setAggregatedValue(new BooleanWritable(true));
    assertEquals(true, overwrite.getAggregatedValue().get());
    BooleanWritable bw = overwrite.createInitialValue();
    assertNotNull(bw);
  }
View Full Code Here

    getAggregatedValue().set(value.get());
  }

  @Override
  public BooleanWritable createInitialValue() {
    return new BooleanWritable(false);
  }
View Full Code Here

    getAggregatedValue().set(getAggregatedValue().get() && value.get());
  }

  @Override
  public BooleanWritable createInitialValue() {
    return new BooleanWritable(true);
  }
View Full Code Here

    getAggregatedValue().set(getAggregatedValue().get() || value.get());
  }

  @Override
  public BooleanWritable createInitialValue() {
    return new BooleanWritable(false);
  }
View Full Code Here

TOP

Related Classes of org.apache.hadoop.io.BooleanWritable$Comparator

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.