예제 #1
0
 @Override
 public void serialize(NamespaceDescriptor ns) {
   super.serialize(ns);
   if (isRootNs(ns)) {
     return;
   }
   sb.append(".");
 }
  private void serialize(Serializer serializer, Collection<EntityType> models) {
    for (EntityType model : models) {
      try {
        Type type = conf.getTypeMappings().getPathType(model, model, true);
        String packageName = type.getPackageName();
        String className =
            !packageName.isEmpty()
                ? (packageName + "." + type.getSimpleName())
                : type.getSimpleName();

        // skip if type is excluded class or in excluded package
        if (conf.isExcludedPackage(model.getPackageName())
            || conf.isExcludedClass(model.getFullName())) {
          continue;
        }

        Set<TypeElement> elements = context.typeElements.get(model.getFullName());

        if (elements == null) {
          elements = new HashSet<TypeElement>();
        }
        for (Property property : model.getProperties()) {
          if (property.getType().getCategory() == TypeCategory.CUSTOM) {
            Set<TypeElement> customElements =
                context.typeElements.get(property.getType().getFullName());
            if (customElements != null) {
              elements.addAll(customElements);
            }
          }
        }

        processingEnv
            .getMessager()
            .printMessage(Kind.NOTE, "Generating " + className + " for " + elements);
        JavaFileObject fileObject =
            processingEnv
                .getFiler()
                .createSourceFile(className, elements.toArray(new Element[elements.size()]));
        Writer writer = fileObject.openWriter();
        try {
          SerializerConfig serializerConfig = conf.getSerializerConfig(model);
          serializer.serialize(model, serializerConfig, new JavaWriter(writer));
        } finally {
          if (writer != null) {
            writer.close();
          }
        }

      } catch (IOException e) {
        System.err.println(e.getMessage());
        processingEnv.getMessager().printMessage(Kind.ERROR, e.getMessage());
      }
    }
  }
예제 #3
0
  @Test
  public void testSetSorting() {
    Set<String> stringHashSet = Sets.newHashSet("b", "a", "z", "g");

    Set<StringObject> stringObjectHashSet =
        Sets.newHashSet(
            new StringObject("b"),
            new StringObject("a"),
            new StringObject("z"),
            new StringObject("g"));
    Bond.obs("stringHashSet", serializer.serialize(stringHashSet))
        .obs("stringObjectHashSet", serializer.serialize(stringObjectHashSet))
        .spy("hash sets");

    Set<String> stringOnlyTreeSet = new TreeSet<>(REVERSE_SORT_COMPARATOR);
    stringOnlyTreeSet.add("b");
    stringOnlyTreeSet.add("a");
    stringOnlyTreeSet.add("z");
    stringOnlyTreeSet.add("g");
    Bond.obs("stringOnlyTreeSet", serializer.serialize(stringOnlyTreeSet)).spy("tree sets");
  }
예제 #4
0
  @Test
  public void testCustomTypeAdapter() {
    serializer =
        serializer.withTypeAdapter(
            StringObject.class,
            new JsonSerializer<StringObject>() {
              @Override
              public JsonElement serialize(
                  StringObject strObj, Type type, JsonSerializationContext jsc) {
                return new JsonPrimitive(strObj.toString() + ", with custom serialization!");
              }
            });

    Bond.obs("StringObject", serializer.serialize(new StringObject("foobar"))).spy();
  }
예제 #5
0
  @Test
  public void testMapSorting() {
    Map<String, String> stringOnlyHashMap = new HashMap<>();
    stringOnlyHashMap.put("b", "bar");
    stringOnlyHashMap.put("a", "foo");
    stringOnlyHashMap.put("z", "baz");
    stringOnlyHashMap.put("g", "test");

    Map<StringObject, String> stringObjectHashMap = new HashMap<>();
    stringObjectHashMap.put(new StringObject("b"), "bar");
    stringObjectHashMap.put(new StringObject("a"), "foo");
    stringObjectHashMap.put(new StringObject("z"), "baz");
    stringObjectHashMap.put(new StringObject("g"), "test");
    Bond.obs("stringOnlyHashMap", serializer.serialize(stringOnlyHashMap))
        .obs("stringObjectHashMap", serializer.serialize(stringObjectHashMap))
        .spy("hash maps");

    Map<String, String> stringOnlyTreeMap = new TreeMap<>(REVERSE_SORT_COMPARATOR);
    stringOnlyTreeMap.put("b", "bar");
    stringOnlyTreeMap.put("a", "foo");
    stringOnlyTreeMap.put("z", "baz");
    stringOnlyTreeMap.put("g", "test");
    Bond.obs("stringOnlyTreeMap", serializer.serialize(stringOnlyTreeMap)).spy("tree maps");
  }
예제 #6
0
  @Test
  public void ln_serialization() throws IOException {
    HTreeMap.LinkedNode n = new HTreeMap.LinkedNode(123456, 123L, 456L);

    DataOutput2 out = new DataOutput2();

    Serializer ln_serializer = new HTreeMap(recman, true).LN_SERIALIZER;
    ln_serializer.serialize(out, n);

    DataInput2 in = swap(out);

    HTreeMap.LinkedNode n2 = (HTreeMap.LinkedNode) ln_serializer.deserialize(in, -1);

    assertEquals(123456, n2.next);
    assertEquals(123L, n2.key);
    assertEquals(456L, n2.value);
  }
예제 #7
0
 private void sendResponse(long requestId, Object value, Class<?> declaredType)
     throws IOException {
   DataOutputStream out = newFlusher();
   out.writeByte(RESPONSE);
   out.writeLong(requestId);
   if (value == null) {
     out.writeBoolean(false);
   } else {
     out.writeBoolean(true);
     Class<?> clazz = value.getClass();
     out.writeUTF(clazz.getName());
     Serializer<Object> s = serializerFor(clazz, declaredType);
     s.serialize(out, value);
   }
   out.writeBoolean(false);
   out.flush();
 }
예제 #8
0
  @Test
  public void testNestedSorting() {
    Set<String> stringHashSet = Sets.newHashSet("b", "a", "z", "g");
    Map<String, String> stringHashMap = new HashMap<>();
    stringHashMap.put("b", "bar");
    stringHashMap.put("a", "foo");
    stringHashMap.put("z", "baz");
    stringHashMap.put("g", "test");

    NestingObject no = new NestingObject();
    no.stringMap = stringHashMap;
    no.stringSet = stringHashSet;

    Map<String, Object> objectMap = new HashMap<>();
    objectMap.put("nestingObject", no);
    objectMap.put("hashSet", stringHashSet);
    objectMap.put("hashMap", stringHashMap);

    Set<Object> objectSet = Sets.newHashSet(stringHashSet, stringHashMap, no);

    Bond.obs("objectMap", serializer.serialize(objectMap))
        .obs("objectSet", serializer.serialize(objectSet))
        .spy();
  }
예제 #9
0
 @Override
 public void serialize(ClassDescriptor clazz) {
   super.serialize(clazz);
   sb.append(".");
 }
예제 #10
0
파일: AQuery.java 프로젝트: james-jw/basex
  /**
   * Evaluates the specified query.
   *
   * @param query query
   * @return success flag
   */
  final boolean query(final String query) {
    final Performance p = new Performance();
    String error;
    if (exception != null) {
      error = Util.message(exception);
    } else {
      try {
        long hits = 0;
        final boolean run = options.get(MainOptions.RUNQUERY);
        final boolean serial = options.get(MainOptions.SERIALIZE);
        final int runs = Math.max(1, options.get(MainOptions.RUNS));
        for (int r = 0; r < runs; ++r) {
          // reuse existing processor instance
          if (r != 0) qp = null;
          qp(query, context);
          parse(p);
          if (r == 0) plan(false);

          qp.compile();
          info.compiling += p.time();
          if (r == 0) plan(true);
          if (!run) continue;

          final PrintOutput po = r == 0 && serial ? out : new NullOutput();
          try (final Serializer ser = qp.getSerializer(po)) {
            if (maxResults >= 0) {
              result = qp.cache(maxResults);
              info.evaluating += p.time();
              result.serialize(ser);
              hits = result.size();
            } else {
              hits = 0;
              final Iter ir = qp.iter();
              info.evaluating += p.time();
              for (Item it; (it = ir.next()) != null; ) {
                ser.serialize(it);
                ++hits;
                checkStop();
              }
            }
          }
          qp.close();
          info.serializing += p.time();
        }
        // dump some query info
        // out.flush();

        // remove string list if global locking is used and if query is updating
        if (soptions.get(StaticOptions.GLOBALLOCK) && qp.updating) {
          info.readLocked = null;
          info.writeLocked = null;
        }
        return info(info.toString(qp, out.size(), hits, options.get(MainOptions.QUERYINFO)));

      } catch (final QueryException | IOException ex) {
        exception = ex;
        error = Util.message(ex);
      } catch (final ProcException ex) {
        error = INTERRUPTED;
      } catch (final StackOverflowError ex) {
        Util.debug(ex);
        error = BASX_STACKOVERFLOW.desc;
      } catch (final RuntimeException ex) {
        extError("");
        Util.debug(info());
        throw ex;
      } finally {
        // close processor after exceptions
        if (qp != null) qp.close();
      }
    }
    return extError(error);
  }
예제 #11
0
  @Test
  public void testUseToStringSerialization() {
    serializer = serializer.withToStringSerialization(StringObject.class);

    Bond.obs("StringObject.toString()", serializer.serialize(new StringObject("foobar"))).spy();
  }
예제 #12
0
 @Test
 public void testOverwriteOldDoublePrecision() {
   serializer = serializer.withDoublePrecision(2);
   serializer = serializer.withDoublePrecision(7);
   Bond.obs("pi", serializer.serialize(Math.PI)).spy();
 }
예제 #13
0
 @Test
 public void testRestrictedFloatPrecision() {
   serializer = serializer.withFloatPrecision(5);
   Bond.obs("1.23456789f", serializer.serialize(1.23456789f)).spy();
 }
예제 #14
0
 @Test
 public void testRestrictedDoublePrecision() {
   serializer = serializer.withDoublePrecision(3);
   Bond.obs("pi", serializer.serialize(Math.PI)).spy();
 }
/**
 * Class with static methods for saving and restoring objects implementing the VersionedObject
 * interface
 */
public class VersionedObjectSerializer {

  private static final byte[] STRING_TYPE = Serializer.serialize("String");
  private static final byte[] BOOLEAN_TYPE = Serializer.serialize("Boolean");
  private static final byte[] BYTE_TYPE = Serializer.serialize("Byte");
  private static final byte[] SHORT_TYPE = Serializer.serialize("Short");
  private static final byte[] INTEGER_TYPE = Serializer.serialize("Integer");
  private static final byte[] LONG_TYPE = Serializer.serialize("Long");
  private static final byte[] FLOAT_TYPE = Serializer.serialize("Float");
  private static final byte[] DOUBLE_TYPE = Serializer.serialize("Double");
  private static final byte[] ENUM_TYPE = Serializer.serialize("Enum");
  private static final byte[] BYTE_ARRAY_TYPE = Serializer.serialize("ByteArray");
  private static final byte[] SERIALIZABLE_TYPE = Serializer.serialize("Serializable");

  public static void serialize(
      List<? extends VersionedObject> versionedObjectList, String path, String... backupPaths)
      throws IOException {
    FragmentedByteArray byteArray = new FragmentedByteArray();
    for (VersionedObject versionedObject : versionedObjectList) {
      byteArray.add(serialize(versionedObject));
    }
    byte[] data = byteArray.generateArray();
    FileReaderWriter.writeBytes(path, data);
    for (String backupPath : backupPaths) {
      FileReaderWriter.writeBytes(backupPath, data);
    }
  }

  public static void serialize(VersionedObject versionedObject, String path, String... backupPaths)
      throws IOException {
    byte[] data = serialize(versionedObject);
    FileReaderWriter.writeBytes(path, data);
    for (String backupPath : backupPaths) {
      FileReaderWriter.writeBytes(backupPath, data);
    }
  }

  public static void serialize(
      List<? extends VersionedObject> versionedObjectList,
      int CRCBytes,
      String path,
      String... backupPaths)
      throws IOException {
    FragmentedByteArray byteArray = new FragmentedByteArray();
    for (VersionedObject versionedObject : versionedObjectList) {
      byteArray.add(serialize(versionedObject, CRCBytes));
    }
    byte[] data = byteArray.generateArray();
    FileReaderWriter.writeBytes(path, data);
    for (String backupPath : backupPaths) {
      FileReaderWriter.writeBytes(backupPath, data);
    }
  }

  public static void serialize(
      VersionedObject versionedObject, int CRCBytes, String path, String... backupPaths)
      throws IOException {
    byte[] data = serialize(versionedObject, CRCBytes);
    FileReaderWriter.writeBytes(path, data);
    for (String backupPath : backupPaths) {
      FileReaderWriter.writeBytes(backupPath, data);
    }
  }

  public static byte[] serialize(List<? extends VersionedObject> versionedObjectList)
      throws NotSerializableException {
    FragmentedByteArray byteArray = new FragmentedByteArray();
    for (VersionedObject versionedObject : versionedObjectList) {
      byteArray.add(serialize(versionedObject));
    }
    return byteArray.generateArray();
  }

  public static byte[] serialize(VersionedObject versionedObject) throws NotSerializableException {
    return serialize(versionedObject, 0);
  }

  public static byte[] serialize(List<? extends VersionedObject> versionedObjectList, int CRCBytes)
      throws NotSerializableException {
    FragmentedByteArray byteArray = new FragmentedByteArray();
    for (VersionedObject versionedObject : versionedObjectList) {
      byteArray.add(serialize(versionedObject, CRCBytes));
    }
    return byteArray.generateArray();
  }

  public static byte[] serialize(VersionedObject versionedObject, int CRCBytes)
      throws NotSerializableException {
    FragmentedByteArray data =
        new FragmentedByteArray(
            Serializer.serializeObject(versionedObject.getCurrentVersion().toArrayList()));
    Map<String, Serializable> attributes = versionedObject.serialize();
    data.add(Serializer.serialize(attributes.size()));
    for (Map.Entry<String, Serializable> entry : attributes.entrySet()) {
      byte[] attributeName = Serializer.serialize(entry.getKey());
      // find the type of the attributes
      byte[] type;
      byte[] attributeArray;
      Object attribute = entry.getValue();
      if (attribute instanceof String) {
        type = STRING_TYPE;
        attributeArray = Serializer.serialize((String) attribute);
      } else if (attribute instanceof Boolean || attribute == null) {
        // null value are serialized as a null Boolean
        type = BOOLEAN_TYPE;
        attributeArray = Serializer.serialize((Boolean) attribute);
      } else if (attribute instanceof Byte) {
        type = BYTE_TYPE;
        attributeArray = Serializer.serialize((Byte) attribute);
      } else if (attribute instanceof Short) {
        type = SHORT_TYPE;
        attributeArray = Serializer.serialize((Short) attribute);
      } else if (attribute instanceof Integer) {
        type = INTEGER_TYPE;
        attributeArray = Serializer.serialize((Integer) attribute);
      } else if (attribute instanceof Long) {
        type = LONG_TYPE;
        attributeArray = Serializer.serialize((Long) attribute);
      } else if (attribute instanceof Float) {
        type = FLOAT_TYPE;
        attributeArray = Serializer.serialize((Float) attribute);
      } else if (attribute instanceof Double) {
        type = DOUBLE_TYPE;
        attributeArray = Serializer.serialize((Double) attribute);
      } else if (attribute instanceof Enum<?>) {
        type = ENUM_TYPE;
        attributeArray =
            Serializer.addArrays(
                Serializer.serialize(attribute.getClass().getName()),
                Serializer.serialize((Enum) attribute));
      } else if (attribute instanceof byte[]) {
        type = BYTE_ARRAY_TYPE;
        attributeArray = Serializer.serialize((byte[]) attribute);
      } else {
        type = SERIALIZABLE_TYPE;
        attributeArray = Serializer.serializeObject((Serializable) attribute);
      }
      data.add(attributeName, type, attributeArray).generateArray();
    }
    return CRC.addCRC(data.generateArray(), CRCBytes, true);
  }

  public static void deserialize(
      VersionedObject versionedObject, String path, String... backupPaths)
      throws VersionedSerializationException, IOException {
    deserialize(versionedObject, path, false, backupPaths);
  }

  public static List<String> deserialize(
      VersionedObject versionedObject, String path, boolean repairIfBroken, String... backupPaths)
      throws VersionedSerializationException, IOException {
    try {
      byte[] data = FileReaderWriter.readBytes(path);
      deserialize(versionedObject, data);
      return new ArrayList<>();
    } catch (VersionedSerializationException | IOException e) {
      // try with backup
      if (backupPaths.length > 0) {
        String newPath = backupPaths[0];
        backupPaths = Arrays.copyOfRange(backupPaths, 1, backupPaths.length);
        List<String> repairedFiles =
            deserialize(versionedObject, newPath, repairIfBroken, backupPaths);
        if (repairIfBroken) {
          Files.copy(Paths.get(newPath), Paths.get(path), StandardCopyOption.REPLACE_EXISTING);
          repairedFiles.add(path);
        }
        return repairedFiles;
      } else {
        throw e;
      }
    }
  }

  public static void deserialize(VersionedObject versionedObject, byte[] data)
      throws VersionedSerializationException {
    deserialize(versionedObject, data, new MutableOffset());
  }

  public static void deserialize(VersionedObject versionedObject, byte[] data, MutableOffset offset)
      throws VersionedSerializationException {
    VersionStack versionStack = null;
    Map<String, Object> attributes = new HashMap<>();
    try {
      data = CRC.extractDataWithCRC(data, offset);
      MutableOffset dataOffset = new MutableOffset();
      versionStack =
          new VersionStack((ArrayList<String>) Serializer.deserializeObject(data, dataOffset));
      int attributeCount = Serializer.deserializeIntValue(data, dataOffset);
      for (int i = 0; i < attributeCount; i++) {
        String attributeName = Serializer.deserializeString(data, dataOffset);
        String type = Serializer.deserializeString(data, dataOffset);
        if (type == null) {
          throw new RuntimeException();
        }
        switch (type) {
          case "String":
            attributes.put(attributeName, Serializer.deserializeString(data, dataOffset));
            break;

          case "Boolean":
            attributes.put(attributeName, Serializer.deserializeBoolean(data, dataOffset));
            break;

          case "Byte":
            attributes.put(attributeName, Serializer.deserializeByte(data, dataOffset));
            break;

          case "Short":
            attributes.put(attributeName, Serializer.deserializeShort(data, dataOffset));
            break;

          case "Integer":
            attributes.put(attributeName, Serializer.deserializeInt(data, dataOffset));
            break;

          case "Long":
            attributes.put(attributeName, Serializer.deserializeLong(data, dataOffset));
            break;

          case "Float":
            attributes.put(attributeName, Serializer.deserializeFloat(data, dataOffset));
            break;

          case "Double":
            attributes.put(attributeName, Serializer.deserializeDouble(data, dataOffset));
            break;

          case "Enum":
            String enumType = Serializer.deserializeString(data, dataOffset);
            Class enumClass = Class.forName(enumType);
            attributes.put(attributeName, Serializer.deserializeEnum(enumClass, data, dataOffset));
            break;

          case "ByteArray":
            attributes.put(attributeName, Serializer.deserializeBytes(data, dataOffset));
            break;

          case "Serializable":
            attributes.put(attributeName, Serializer.deserializeObject(data, dataOffset));
            break;

          default:
            // the VersionedObjectSerializer failed when deserializing the byte array
            throw new RuntimeException("Unexpected type: " + type);
        }
      }
      versionedObject.deserialize(versionStack.retrieveVersion(), attributes, versionStack);
    } catch (RuntimeException e) {
      throw new VersionedSerializationException(
          versionStack, attributes, VersionedSerializationException.Reason.INCORRECT_DATA);
    } catch (UnrecognizedVersionException e) {
      throw new VersionedSerializationException(
          versionStack, attributes, VersionedSerializationException.Reason.UNRECOGNIZED_VERSION);
    } catch (ClassNotFoundException e) {
      throw new VersionedSerializationException(
          versionStack, attributes, VersionedSerializationException.Reason.CLASS_NOT_FOUND);
    } catch (CRCMismatchException e) {
      throw new VersionedSerializationException(
          null, attributes, VersionedSerializationException.Reason.CRC_MISMATCH);
    }
  }
}
 public static byte[] serialize(VersionedObject versionedObject, int CRCBytes)
     throws NotSerializableException {
   FragmentedByteArray data =
       new FragmentedByteArray(
           Serializer.serializeObject(versionedObject.getCurrentVersion().toArrayList()));
   Map<String, Serializable> attributes = versionedObject.serialize();
   data.add(Serializer.serialize(attributes.size()));
   for (Map.Entry<String, Serializable> entry : attributes.entrySet()) {
     byte[] attributeName = Serializer.serialize(entry.getKey());
     // find the type of the attributes
     byte[] type;
     byte[] attributeArray;
     Object attribute = entry.getValue();
     if (attribute instanceof String) {
       type = STRING_TYPE;
       attributeArray = Serializer.serialize((String) attribute);
     } else if (attribute instanceof Boolean || attribute == null) {
       // null value are serialized as a null Boolean
       type = BOOLEAN_TYPE;
       attributeArray = Serializer.serialize((Boolean) attribute);
     } else if (attribute instanceof Byte) {
       type = BYTE_TYPE;
       attributeArray = Serializer.serialize((Byte) attribute);
     } else if (attribute instanceof Short) {
       type = SHORT_TYPE;
       attributeArray = Serializer.serialize((Short) attribute);
     } else if (attribute instanceof Integer) {
       type = INTEGER_TYPE;
       attributeArray = Serializer.serialize((Integer) attribute);
     } else if (attribute instanceof Long) {
       type = LONG_TYPE;
       attributeArray = Serializer.serialize((Long) attribute);
     } else if (attribute instanceof Float) {
       type = FLOAT_TYPE;
       attributeArray = Serializer.serialize((Float) attribute);
     } else if (attribute instanceof Double) {
       type = DOUBLE_TYPE;
       attributeArray = Serializer.serialize((Double) attribute);
     } else if (attribute instanceof Enum<?>) {
       type = ENUM_TYPE;
       attributeArray =
           Serializer.addArrays(
               Serializer.serialize(attribute.getClass().getName()),
               Serializer.serialize((Enum) attribute));
     } else if (attribute instanceof byte[]) {
       type = BYTE_ARRAY_TYPE;
       attributeArray = Serializer.serialize((byte[]) attribute);
     } else {
       type = SERIALIZABLE_TYPE;
       attributeArray = Serializer.serializeObject((Serializable) attribute);
     }
     data.add(attributeName, type, attributeArray).generateArray();
   }
   return CRC.addCRC(data.generateArray(), CRCBytes, true);
 }