Java Code Examples for com.google.common.hash.PrimitiveSink#putByte()

The following examples show how to use com.google.common.hash.PrimitiveSink#putByte() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HashTestUtils.java    From zetasketch with Apache License 2.0 5 votes vote down vote up
@Override
void performAction(Random random, Iterable<? extends PrimitiveSink> sinks) {
  int value = random.nextInt();
  for (PrimitiveSink sink : sinks) {
    sink.putByte((byte) value);
  }
}
 
Example 2
Source File: ProbableIntersectionCursorState.java    From fdb-record-layer with Apache License 2.0 5 votes vote down vote up
@Override
public void funnel(List<Object> objects, PrimitiveSink primitiveSink) {
    for (Object o : objects) {
        if (o == null) {
            primitiveSink.putByte((byte)0x00);
        } else if (o instanceof byte[]) {
            primitiveSink.putBytes((byte[])o);
        } else if (o instanceof ByteString) {
            primitiveSink.putBytes(((ByteString)o).toByteArray());
        } else if (o instanceof ByteBuffer) {
            primitiveSink.putBytes((ByteBuffer) o);
        } else if (o instanceof String) {
            primitiveSink.putString((String)o, Charsets.UTF_8);
        } else if (o instanceof Float) {
            primitiveSink.putFloat((float)o);
        } else if (o instanceof Double) {
            primitiveSink.putDouble((double)o);
        } else if (o instanceof Integer) {
            primitiveSink.putInt((int)o);
        } else if (o instanceof Long) {
            primitiveSink.putLong((long) o);
        } else if (o instanceof Boolean) {
            primitiveSink.putBoolean((boolean)o);
        } else if (o instanceof Enum) {
            primitiveSink.putInt(((Enum)o).ordinal());
        } else {
            primitiveSink.putBytes(Tuple.from(o).pack());
        }
    }
}
 
Example 3
Source File: HashingUtil.java    From datacollector with Apache License 2.0 4 votes vote down vote up
@Override
public void funnel(Record record, PrimitiveSink sink) {
  for (String path : getFieldsToHash(record)) {
    Field field = record.get(path);
    if (field == null) {
      throw new IllegalArgumentException(
          Utils.format("Field Path {}  does not exist in the record", path)
      );
    }
    if (field.getValue() != null) {
      switch (field.getType()) {
        case BOOLEAN:
          sink.putBoolean(field.getValueAsBoolean());
          break;
        case CHAR:
          sink.putChar(field.getValueAsChar());
          break;
        case BYTE:
          sink.putByte(field.getValueAsByte());
          break;
        case SHORT:
          sink.putShort(field.getValueAsShort());
          break;
        case INTEGER:
          sink.putInt(field.getValueAsInteger());
          break;
        case LONG:
          sink.putLong(field.getValueAsLong());
          break;
        case FLOAT:
          sink.putFloat(field.getValueAsFloat());
          break;
        case DOUBLE:
          sink.putDouble(field.getValueAsDouble());
          break;
        case DATE:
          sink.putLong(field.getValueAsDate().getTime());
          break;
        case TIME:
          sink.putLong(field.getValueAsTime().getTime());
          break;
        case DATETIME:
          sink.putLong(field.getValueAsDatetime().getTime());
          break;

        case DECIMAL:
        case STRING:
          sink.putString(field.getValueAsString(), Charset.defaultCharset());
          break;

        case BYTE_ARRAY:
          sink.putBytes(field.getValueAsByteArray());
          break;
        case FILE_REF:
          throw new IllegalStateException(
              Utils.format(
                  "Hashing not supported for field: {} of type {}",
                  path,
                  field.getType()
              )
          );
        default:
          break;
      }
    } else {
      sink.putBoolean(true);
    }
    if(useSeparators) {
      sink.putString(java.nio.CharBuffer.wrap(new char[] {separator}), Charset.forName("UTF-8"));
    }
  }

  if (this.includeRecordHeader) {
    for (String attrName : record.getHeader().getAttributeNames()) {
      String headerAttr = record.getHeader().getAttribute(attrName);
      if (headerAttr != null) {
        sink.putString(headerAttr, Charset.defaultCharset());
      } else {
        sink.putBoolean(true);
      }

      if(useSeparators) {
        sink.putString(java.nio.CharBuffer.wrap(new char[] {separator}), Charset.forName("UTF-8"));
      }
    }
  }
}
 
Example 4
Source File: TestFieldHasherProcessor.java    From datacollector with Apache License 2.0 4 votes vote down vote up
static String computeHash(Field.Type fieldType, Object value, HashType hashType) {
  HashFunction hasher = HashingUtil.getHasher(hashType.getHashType());
  PrimitiveSink sink = hasher.newHasher();
  switch (fieldType) {
    case BOOLEAN:
      sink.putBoolean(new BooleanTypeSupport().convert(value));
      break;
    case CHAR:
      sink.putChar(new CharTypeSupport().convert(value));
      break;
    case BYTE:
      sink.putByte(new ByteTypeSupport().convert(value));
      break;
    case SHORT:
      sink.putShort(new ShortTypeSupport().convert(value));
      break;
    case INTEGER:
      sink.putInt(new IntegerTypeSupport().convert(value));
      break;
    case LONG:
      sink.putLong(new LongTypeSupport().convert(value));
      break;
    case FLOAT:
      sink.putFloat(new FloatTypeSupport().convert(value));
      break;
    case DOUBLE:
      sink.putDouble(new DoubleTypeSupport().convert(value));
      break;
    case DATE:
      sink.putLong(new DateTypeSupport().convert(value).getTime());
      break;
    case TIME:
      sink.putLong(new DateTypeSupport().convert(value).getTime());
      break;
    case DATETIME:
      sink.putLong(new DateTypeSupport().convert(value).getTime());
      break;
    case DECIMAL:
      sink.putString(new StringTypeSupport().convert(value), Charset.defaultCharset());
      break;
    case STRING:
      sink.putString(new StringTypeSupport().convert(value), Charset.defaultCharset());
      break;
    case BYTE_ARRAY:
      sink.putBytes(new ByteArrayTypeSupport().convert(value));
      break;
    case MAP:
    case LIST:
    default:
      return null;
  }

  // this was the problem in SDC-6540.
  sink.putByte((byte)0);

  return ((Hasher)sink).hash().toString();
}