Exemplo n.º 1
0
  // Retrieves the row from the table and check if it exists and has not been flagged as deleted
  protected Map<RecordId, Result> getRows(List<RecordId> recordIds, List<FieldType> fields)
      throws RecordException {
    Map<RecordId, Result> results = new HashMap<RecordId, Result>();

    try {
      List<Get> gets = new ArrayList<Get>();
      for (RecordId recordId : recordIds) {
        Get get = new Get(recordId.toBytes());
        // Add the columns for the fields to get
        addFieldsToGet(get, fields);
        get.setMaxVersions(1); // Only retrieve the most recent version of each field
        gets.add(get);
      }

      // Retrieve the data from the repository
      int i = 0;
      for (Result result : recordTable.get(gets)) {
        if (result == null || result.isEmpty()) {
          i++; // Skip this recordId (instead of throwing a RecordNotFoundException)
          continue;
        }
        // Check if the record was deleted
        byte[] deleted = recdec.getLatest(result, RecordCf.DATA.bytes, RecordColumn.DELETED.bytes);
        if ((deleted == null) || (Bytes.toBoolean(deleted))) {
          i++; // Skip this recordId (instead of throwing a RecordNotFoundException)
          continue;
        }
        results.put(recordIds.get(i++), result);
      }
    } catch (IOException e) {
      throw new RecordException(
          "Exception occurred while retrieving records '" + recordIds + "' from HBase table", e);
    }
    return results;
  }
Exemplo n.º 2
0
  /**
   * Builds Object from Bytes Picked from Hbase.
   *
   * @param b
   * @param klass
   * @return
   */
  public static Object toObject(byte[] b, Class<?> klass) {

    if (klass.isAssignableFrom(String.class)) {
      return Bytes.toString(b);
    } else if (klass.equals(int.class) || klass.isAssignableFrom(Integer.class)) {
      return Bytes.toInt(b);
    } else if (klass.equals(long.class) || klass.isAssignableFrom(Long.class)) {
      return Bytes.toLong(b);
    } else if (klass.equals(boolean.class) || klass.isAssignableFrom(Boolean.class)) {
      return Bytes.toBoolean(b);
    } else if (klass.equals(double.class) || klass.isAssignableFrom(Double.class)) {
      return Bytes.toDouble(b);
    } else if (klass.equals(float.class) || klass.isAssignableFrom(Float.class)) {
      return Bytes.toFloat(b);
    } else if (klass.equals(short.class) || klass.isAssignableFrom(Short.class)) {
      return Bytes.toShort(b);
    } else if (klass.equals(BigDecimal.class)) {
      return Bytes.toBigDecimal(b);
    } else {
      throw new MetaModelException("Could Not find a suitable Type for " + klass.getName());
    }
  }
Exemplo n.º 3
0
 public java.lang.String toString(byte[] bytes) {
   return java.lang.Boolean.toString(Bytes.toBoolean(bytes));
 }
Exemplo n.º 4
0
  /**
   * Converts the given value from bytes based on data type and cardinality information for the
   * given property. For 'many' or multi-valued properties, if the SDO Java type for the property is
   * not already String, the value is first converted from a String using the SDO conversion which
   * uses java.util.Arrays formatting, resulting in an array of primitive types. For non 'many' or
   * singular properties, only the HBase Bytes utility is used.
   *
   * @param targetProperty the property
   * @param value the bytes value
   * @return the converted object
   * @throws IllegalArgumentException if the given property is not a data type property
   */
  public Object fromBytes(Property targetProperty, byte[] value) {
    Object result = null;

    if (!targetProperty.getType().isDataType())
      throw new IllegalArgumentException(
          "property " + targetProperty.toString() + " is not a datatype property");

    DataType targetDataType = DataType.valueOf(targetProperty.getType().getName());

    switch (targetDataType) {
        // Data types stored as String bytes in HBase
      case String:
      case Strings:
      case URI:
      case Month:
      case MonthDay:
      case Day:
      case Time:
      case Year:
      case YearMonth:
      case YearMonthDay:
      case Duration:
        String resultStr = Bytes.toString(value);
        result = DataConverter.INSTANCE.fromString(targetProperty, resultStr);
        break;
      case Date:
        resultStr = Bytes.toString(value);
        result = DataConverter.INSTANCE.fromString(targetProperty, resultStr);
        break;
      case DateTime:
        // NOTE: remember datetime is a String Java representation in SDO 2.1
        resultStr = Bytes.toString(value);
        result = DataConverter.INSTANCE.fromString(targetProperty, resultStr);
        break;

        // Data types stored by directly converting from primitive types to bytes in HBase.
        // TODO: for these data types determine if there is a way to "delimit" multiple values yet
        // not take the extra and expensive step of first converting to delimited String.
      case Decimal:
        if (!targetProperty.isMany()) result = Bytes.toBigDecimal(value);
        else result = DataConverter.INSTANCE.fromString(targetProperty, Bytes.toString(value));
        break;
      case Bytes:
        if (!targetProperty.isMany()) result = value; // already bytes
        else result = DataConverter.INSTANCE.fromString(targetProperty, Bytes.toString(value));
        break;
      case Byte:
        if (!targetProperty.isMany()) {
          // NOTE: no toByte method as would expect as there is opposite method, see below
          // e.g. Bytes.toByte(value);
          if (value != null) {
            if (value.length > 2)
              log.warn(
                  "truncating "
                      + String.valueOf(value.length)
                      + " length byte array for target data type 'byte'");
            result = value[0];
          }
        } else result = DataConverter.INSTANCE.fromString(targetProperty, Bytes.toString(value));
        break;
      case Boolean:
        if (!targetProperty.isMany()) result = Bytes.toBoolean(value);
        else result = DataConverter.INSTANCE.fromString(targetProperty, Bytes.toString(value));
        break;
      case Character:
        if (!targetProperty.isMany()) result = Character.valueOf(Bytes.toString(value).charAt(0));
        else result = DataConverter.INSTANCE.fromString(targetProperty, Bytes.toString(value));
        break;
      case Double:
        if (!targetProperty.isMany()) result = Bytes.toDouble(value);
        else result = DataConverter.INSTANCE.fromString(targetProperty, Bytes.toString(value));
        break;
      case Float:
        if (!targetProperty.isMany()) result = Bytes.toFloat(value);
        else result = DataConverter.INSTANCE.fromString(targetProperty, Bytes.toString(value));
        break;
      case Int:
        if (!targetProperty.isMany()) result = Bytes.toInt(value);
        else result = DataConverter.INSTANCE.fromString(targetProperty, Bytes.toString(value));
        break;
      case Integer:
        if (!targetProperty.isMany()) result = new BigInteger(value);
        else result = DataConverter.INSTANCE.fromString(targetProperty, Bytes.toString(value));
        break;
      case Long:
        if (!targetProperty.isMany()) result = Bytes.toLong(value);
        else result = DataConverter.INSTANCE.fromString(targetProperty, Bytes.toString(value));
        break;
      case Short:
        if (!targetProperty.isMany()) result = Bytes.toShort(value);
        else result = DataConverter.INSTANCE.fromString(targetProperty, Bytes.toString(value));
        break;
      case Object:
        // FIXME: custom serialization?
      default:
        result = Bytes.toString(value);
        break;
    }

    return result;
  }