예제 #1
0
 /*
  * The following two static methods are expensive. right now, they are used by RSSFeeds and Twitter feed
  * TODO: Get rid of them
  */
 public static void writeRecord(
     AMutableRecord record, DataOutput dataOutput, IARecordBuilder recordBuilder)
     throws HyracksDataException {
   ArrayBackedValueStorage fieldValue = new ArrayBackedValueStorage();
   int numFields = record.getType().getFieldNames().length;
   for (int pos = 0; pos < numFields; pos++) {
     fieldValue.reset();
     IAObject obj = record.getValueByPos(pos);
     IDataParser.writeObject(obj, fieldValue.getDataOutput());
     recordBuilder.addField(pos, fieldValue);
   }
   recordBuilder.write(dataOutput, true);
 }
예제 #2
0
  @SuppressWarnings("unchecked")
  public static void writeObject(IAObject obj, DataOutput dataOutput) throws HyracksDataException {
    switch (obj.getType().getTypeTag()) {
      case RECORD:
        {
          IARecordBuilder recordBuilder = new RecordBuilder();
          recordBuilder.reset((ARecordType) obj.getType());
          recordBuilder.init();
          writeRecord((AMutableRecord) obj, dataOutput, recordBuilder);
          break;
        }

      case ORDEREDLIST:
        {
          OrderedListBuilder listBuilder = new OrderedListBuilder();
          listBuilder.reset((AOrderedListType) ((AMutableOrderedList) obj).getType());
          IACursor cursor = ((AMutableOrderedList) obj).getCursor();
          ArrayBackedValueStorage listItemValue = new ArrayBackedValueStorage();
          while (cursor.next()) {
            listItemValue.reset();
            IAObject item = cursor.get();
            writeObject(item, listItemValue.getDataOutput());
            listBuilder.addItem(listItemValue);
          }
          listBuilder.write(dataOutput, true);
          break;
        }

      case UNORDEREDLIST:
        {
          UnorderedListBuilder listBuilder = new UnorderedListBuilder();
          listBuilder.reset((AUnorderedListType) ((AMutableUnorderedList) obj).getType());
          IACursor cursor = ((AMutableUnorderedList) obj).getCursor();
          ArrayBackedValueStorage listItemValue = new ArrayBackedValueStorage();
          while (cursor.next()) {
            listItemValue.reset();
            IAObject item = cursor.get();
            writeObject(item, listItemValue.getDataOutput());
            listBuilder.addItem(listItemValue);
          }
          listBuilder.write(dataOutput, true);
          break;
        }

      default:
        AqlSerializerDeserializerProvider.INSTANCE
            .getSerializerDeserializer(obj.getType())
            .serialize(obj, dataOutput);
        break;
    }
  }
  private void parseRecord(ARecordType recType, DataOutput out, Boolean datasetRec)
      throws IOException, AsterixException, AdmLexerException {

    ArrayBackedValueStorage fieldValueBuffer = getTempBuffer();
    ArrayBackedValueStorage fieldNameBuffer = getTempBuffer();
    IARecordBuilder recBuilder = getRecordBuilder();

    BitSet nulls = null;
    if (datasetRec) {
      if (recType != null) {
        nulls = new BitSet(recType.getFieldNames().length);
        recBuilder.reset(recType);
      } else {
        recBuilder.reset(null);
      }
    } else if (recType != null) {
      nulls = new BitSet(recType.getFieldNames().length);
      recBuilder.reset(recType);
    } else {
      recBuilder.reset(null);
    }

    recBuilder.init();
    int token;
    boolean inRecord = true;
    boolean expectingRecordField = false;
    boolean first = true;

    Boolean openRecordField = false;
    int fieldId = 0;
    IAType fieldType = null;
    do {
      token = admLexer.next();
      switch (token) {
        case AdmLexer.TOKEN_END_RECORD:
          {
            if (expectingRecordField) {
              throw new ParseException("Found END_RECORD while expecting a record field.");
            }
            inRecord = false;
            break;
          }
        case AdmLexer.TOKEN_STRING_LITERAL:
          {
            // we've read the name of the field
            // now read the content
            fieldNameBuffer.reset();
            fieldValueBuffer.reset();
            expectingRecordField = false;

            if (recType != null) {
              String fldName =
                  admLexer
                      .getLastTokenImage()
                      .substring(1, admLexer.getLastTokenImage().length() - 1);
              fieldId = recBuilder.getFieldId(fldName);
              if (fieldId < 0 && !recType.isOpen()) {
                throw new ParseException("This record is closed, you can not add extra fields !!");
              } else if (fieldId < 0 && recType.isOpen()) {
                aStringFieldName.setValue(
                    admLexer
                        .getLastTokenImage()
                        .substring(1, admLexer.getLastTokenImage().length() - 1));
                stringSerde.serialize(aStringFieldName, fieldNameBuffer.getDataOutput());
                openRecordField = true;
                fieldType = null;
              } else {
                // a closed field
                nulls.set(fieldId);
                fieldType = recType.getFieldTypes()[fieldId];
                openRecordField = false;
              }
            } else {
              aStringFieldName.setValue(
                  admLexer
                      .getLastTokenImage()
                      .substring(1, admLexer.getLastTokenImage().length() - 1));
              stringSerde.serialize(aStringFieldName, fieldNameBuffer.getDataOutput());
              openRecordField = true;
              fieldType = null;
            }

            token = admLexer.next();
            if (token != AdmLexer.TOKEN_COLON) {
              throw new ParseException(
                  "Unexpected ADM token kind: "
                      + AdmLexer.tokenKindToString(token)
                      + " while expecting \":\".");
            }

            token = admLexer.next();
            this.admFromLexerStream(token, fieldType, fieldValueBuffer.getDataOutput(), false);
            if (openRecordField) {
              if (fieldValueBuffer.getByteArray()[0] != ATypeTag.NULL.serialize()) {
                recBuilder.addField(fieldNameBuffer, fieldValueBuffer);
              }
            } else if (NonTaggedFormatUtil.isOptional(recType)) {
              if (fieldValueBuffer.getByteArray()[0] != ATypeTag.NULL.serialize()) {
                recBuilder.addField(fieldId, fieldValueBuffer);
              }
            } else {
              recBuilder.addField(fieldId, fieldValueBuffer);
            }

            break;
          }
        case AdmLexer.TOKEN_COMMA:
          {
            if (first) {
              throw new ParseException("Found COMMA before any record field.");
            }
            if (expectingRecordField) {
              throw new ParseException("Found COMMA while expecting a record field.");
            }
            expectingRecordField = true;
            break;
          }
        default:
          {
            throw new ParseException(
                "Unexpected ADM token kind: "
                    + AdmLexer.tokenKindToString(token)
                    + " while parsing record fields.");
          }
      }
      first = false;
    } while (inRecord);

    if (recType != null) {
      nullableFieldId = checkNullConstraints(recType, nulls);
      if (nullableFieldId != -1) {
        throw new ParseException(
            "Field: " + recType.getFieldNames()[nullableFieldId] + " can not be null");
      }
    }
    recBuilder.write(out, true);
  }