コード例 #1
0
 private void setSingleBlockValue(
     PropertyBlock block, int keyId, PropertyType type, long longValue) {
   block.setSingleBlock(keyId | (((long) type.intValue()) << 24) | (longValue << 28));
 }
コード例 #2
0
  public void encodeValue(PropertyBlock block, int keyId, Object value) {
    if (value instanceof String) { // Try short string first, i.e. inlined in the property block
      String string = (String) value;
      if (LongerShortString.encode(keyId, string, block, PropertyType.getPayloadSize())) return;

      // Fall back to dynamic string store
      long stringBlockId = nextStringBlockId();
      setSingleBlockValue(block, keyId, PropertyType.STRING, stringBlockId);
      byte[] encodedString = encodeString(string);
      Collection<DynamicRecord> valueRecords = allocateStringRecords(stringBlockId, encodedString);
      for (DynamicRecord valueRecord : valueRecords) {
        valueRecord.setType(PropertyType.STRING.intValue());
        block.addValueRecord(valueRecord);
      }
    } else if (value instanceof Integer)
      setSingleBlockValue(block, keyId, PropertyType.INT, ((Integer) value).longValue());
    else if (value instanceof Boolean)
      setSingleBlockValue(
          block, keyId, PropertyType.BOOL, (((Boolean) value).booleanValue() ? 1L : 0L));
    else if (value instanceof Float)
      setSingleBlockValue(
          block, keyId, PropertyType.FLOAT, Float.floatToRawIntBits(((Float) value).floatValue()));
    else if (value instanceof Long) {
      long keyAndType = keyId | (((long) PropertyType.LONG.intValue()) << 24);
      if (ShortArray.LONG.getRequiredBits((Long) value)
          <= 35) { // We only need one block for this value, special layout compared to, say, an
                   // integer
        block.setSingleBlock(keyAndType | (1L << 28) | (((Long) value).longValue() << 29));
      } else { // We need two blocks for this value
        block.setValueBlocks(new long[] {keyAndType, ((Long) value).longValue()});
      }
    } else if (value instanceof Double)
      block.setValueBlocks(
          new long[] {
            keyId | (((long) PropertyType.DOUBLE.intValue()) << 24),
            Double.doubleToRawLongBits(((Double) value).doubleValue())
          });
    else if (value instanceof Byte)
      setSingleBlockValue(block, keyId, PropertyType.BYTE, ((Byte) value).longValue());
    else if (value instanceof Character)
      setSingleBlockValue(block, keyId, PropertyType.CHAR, ((Character) value).charValue());
    else if (value instanceof Short)
      setSingleBlockValue(block, keyId, PropertyType.SHORT, ((Short) value).longValue());
    else if (value
        .getClass()
        .isArray()) { // Try short array first, i.e. inlined in the property block
      if (ShortArray.encode(keyId, value, block, PropertyType.getPayloadSize())) return;

      // Fall back to dynamic array store
      long arrayBlockId = nextArrayBlockId();
      setSingleBlockValue(block, keyId, PropertyType.ARRAY, arrayBlockId);
      Collection<DynamicRecord> arrayRecords = allocateArrayRecords(arrayBlockId, value);
      for (DynamicRecord valueRecord : arrayRecords) {
        valueRecord.setType(PropertyType.ARRAY.intValue());
        block.addValueRecord(valueRecord);
      }
    } else {
      throw new IllegalArgumentException(
          "Unknown property type on: " + value + ", " + value.getClass());
    }
  }