/** {@inheritDoc} */ @Override public Collection<String> fieldNames() { return target().fieldNames(); }
/** * Default constructor. */ public VisorBinaryMetadata(IgniteBinary binary, BinaryType binaryType) { typeName = binaryType.typeName(); typeId = binary.typeId(typeName); affinityKeyFieldName = binaryType.affinityKeyFieldName(); Collection<String> binaryTypeFields = binaryType.fieldNames(); fields = new ArrayList<>(binaryTypeFields.size()); for (String metaField : binaryTypeFields) fields.add(new VisorBinaryMetadataField(metaField, binaryType.fieldTypeName(metaField), null)); }
/** {@inheritDoc} */ @Override public void serialize(BinaryObjectImpl bin, JsonGenerator gen, SerializerProvider ser) throws IOException { try { BinaryType meta = bin.rawType(); // Serialize to JSON if we have metadata. if (meta != null && !F.isEmpty(meta.fieldNames())) { gen.writeStartObject(); for (String name : meta.fieldNames()) { Object val = bin.field(name); if (val instanceof BinaryObjectImpl) { BinaryObjectImpl ref = (BinaryObjectImpl)val; if (ref.hasCircularReferences()) throw ser.mappingException("Failed convert to JSON object for circular references"); } gen.writeObjectField(name, val); } gen.writeEndObject(); } else { // Otherwise serialize as Java object. Object obj = bin.deserialize(); gen.writeObject(obj); } } catch (BinaryObjectException ignore) { gen.writeNull(); } } };
@Override public void examine(IgniteCache cache) { BinaryObject bo = (BinaryObject) cache.get(2); BinaryType type = bo.type(); assertFalse(bo.type().isEnum()); assertEquals(DYNAMIC_TYPE_NAME, type.typeName()); Collection<String> fieldNames = type.fieldNames(); assertEquals(2, fieldNames.size()); assertTrue(fieldNames.contains(DYNAMIC_INT_FIELD_NAME)); assertTrue(fieldNames.contains(DYNAMIC_STR_FIELD_NAME)); } };
@Override public void examine(IgniteCache cache) { BinaryObject bo = (BinaryObject) cache.get(2); BinaryType type = bo.type(); assertFalse(type.isEnum()); assertEquals(DYNAMIC_TYPE_NAME, type.typeName()); Collection<String> fieldNames = type.fieldNames(); assertEquals(1, fieldNames.size()); assertTrue(fieldNames.contains(DYNAMIC_INT_FIELD_NAME)); assertEquals(DYNAMIC_INT_FIELD_NAME, type.affinityKeyFieldName()); } };
/** * Transforms cache entry to map (column name -> value). * * @param meta Meta information (QueryEntity) about table. * @param key Key of the cache entry. * @param val Value of the cache entry. */ private static Map<String, Object> entryToMap(QueryEntity meta, Object key, Object val) { Map<String, Object> row = new LinkedHashMap<>(); // Look up for the field in the key if (key instanceof BinaryObject) { BinaryObject compositeKey = (BinaryObject)key; for (String field : compositeKey.type().fieldNames()) row.put(field, compositeKey.field(field)); } else row.put(meta.getKeyFieldName(), key); // And in the value. if (val instanceof BinaryObject) { BinaryObject compositeVal = (BinaryObject)val; for (String field : compositeVal.type().fieldNames()) row.put(field, compositeVal.field(field)); } else row.put(meta.getValueFieldName(), val); return row; }
/** */ private void assertBinaryTypesEqual(BinaryType exp, BinaryType actual) { assertEquals(exp.typeId(), actual.typeId()); assertEquals(exp.typeName(), actual.typeName()); assertArrayEquals(exp.fieldNames().toArray(), actual.fieldNames().toArray()); for (String f : exp.fieldNames()) assertEquals(exp.fieldTypeName(f), actual.fieldTypeName(f)); assertEquals(exp.affinityKeyFieldName(), actual.affinityKeyFieldName()); assertEquals(exp.isEnum(), actual.isEnum()); }
/** * The test must be refactored after {@link IgniteSystemProperties#IGNITE_BINARY_SORT_OBJECT_FIELDS} * is removed. * * @throws Exception If failed. */ @Test public void testFieldOrder() throws Exception { if (BinaryUtils.FIELDS_SORTED_ORDER) return; BinaryMarshaller m = binaryMarshaller(); BinaryObjectImpl binObj = marshal(simpleObject(), m); Collection<String> fieldsBin = binObj.type().fieldNames(); Field[] fields = SimpleObject.class.getDeclaredFields(); assertEquals(fields.length, fieldsBin.size()); int i = 0; for (String fieldName : fieldsBin) { assertEquals(fields[i].getName(), fieldName); ++i; } }
/** {@inheritDoc} */ @Nullable @Override public Object execute() { BinaryObjectEx arg0 = ((BinaryObjectEx)arg); BinaryType meta = ignite.binary().type(arg0.typeId()); if (meta == null) throw new IgniteException("Metadata doesn't exist."); if (meta.fieldNames() == null || !meta.fieldNames().contains("Field")) throw new IgniteException("Field metadata doesn't exist."); if (!F.eq("int", meta.fieldTypeName("Field"))) throw new IgniteException("Invalid field type: " + meta.fieldTypeName("Field")); if (meta.affinityKeyFieldName() != null) throw new IgniteException("Unexpected affinity key: " + meta.affinityKeyFieldName()); return arg0.field("field"); }
/** */ private void assertBinaryObjectsEqual(BinaryObject exp, BinaryObject actual) throws Exception { assertBinaryTypesEqual(exp.type(), actual.type()); for (String f : exp.type().fieldNames()) { Object expVal = exp.field(f); Class<?> cls = expVal.getClass(); if (cls.getMethod("equals", Object.class).getDeclaringClass() == cls) assertEquals(expVal, actual.field(f)); } if (exp.type().isEnum()) assertEquals(exp.enumOrdinal(), actual.enumOrdinal()); }
/** */ private void assertBinaryTypesEqual(BinaryType exp, BinaryType actual) { assertEquals(exp.typeId(), actual.typeId()); assertEquals(exp.typeName(), actual.typeName()); assertEquals(exp.fieldNames(), actual.fieldNames()); for (String f : exp.fieldNames()) assertEquals(exp.fieldTypeName(f), actual.fieldTypeName(f)); assertEquals(exp.affinityKeyFieldName(), actual.affinityKeyFieldName()); assertEquals(exp.isEnum(), actual.isEnum()); }
/** */ private void assertBinaryObjectsEqual(BinaryObject exp, BinaryObject actual) throws Exception { assertBinaryTypesEqual(exp.type(), actual.type()); for (String f : exp.type().fieldNames()) { Object expVal = exp.field(f); Class<?> cls = expVal.getClass(); if (cls.getMethod("equals", Object.class).getDeclaringClass() == cls) assertEquals(expVal, actual.field(f)); } if (exp.type().isEnum()) assertEquals(exp.enumOrdinal(), actual.enumOrdinal()); } }
/** * @throws Exception If failed. */ @Test public void testReflection() throws Exception { BinaryType meta = binaries().type(TestObject1.class); assertNotNull(meta); assertEquals(expectedTypeName(TestObject1.class.getName()), meta.typeName()); Collection<String> fields = meta.fieldNames(); assertEquals(7, fields.size()); assertTrue(fields.contains("intVal")); assertTrue(fields.contains("strVal")); assertTrue(fields.contains("arrVal")); assertTrue(fields.contains("obj1Val")); assertTrue(fields.contains("obj2Val")); assertTrue(fields.contains("decVal")); assertTrue(fields.contains("decArrVal")); assertEquals("int", meta.fieldTypeName("intVal")); assertEquals("String", meta.fieldTypeName("strVal")); assertEquals("byte[]", meta.fieldTypeName("arrVal")); assertEquals("Object", meta.fieldTypeName("obj1Val")); assertEquals("Object", meta.fieldTypeName("obj2Val")); assertEquals("decimal", meta.fieldTypeName("decVal")); assertEquals("decimal[]", meta.fieldTypeName("decArrVal")); }
/** * The test must be refactored after {@link IgniteSystemProperties#IGNITE_BINARY_SORT_OBJECT_FIELDS} * is removed. * * @throws Exception If failed. */ @Test public void testFieldOrderByBuilder() throws Exception { if (BinaryUtils.FIELDS_SORTED_ORDER) return; BinaryMarshaller m = binaryMarshaller(); BinaryObjectBuilder builder = new BinaryObjectBuilderImpl(binaryContext(m), "MyFakeClass"); String[] fieldNames = {"field9", "field8", "field0", "field1", "field2"}; for (String fieldName : fieldNames) builder.setField(fieldName, 0); BinaryObject binObj = builder.build(); Collection<String> fieldsBin = binObj.type().fieldNames(); assertEquals(fieldNames.length, fieldsBin.size()); int i = 0; for (String fieldName : fieldsBin) { assertEquals(fieldNames[i], fieldName); ++i; } }
/** * @throws Exception If failed. */ @Test public void testBinaryMarshalAware() throws Exception { binaries().toBinary(new TestObject2()); BinaryType meta = binaries().type(TestObject2.class); assertNotNull(meta); assertEquals(expectedTypeName(TestObject2.class.getName()), meta.typeName()); Collection<String> fields = meta.fieldNames(); assertEquals(7, fields.size()); assertTrue(fields.contains("boolVal")); assertTrue(fields.contains("dateVal")); assertTrue(fields.contains("uuidArrVal")); assertTrue(fields.contains("objVal")); assertTrue(fields.contains("mapVal")); assertTrue(fields.contains("decVal")); assertTrue(fields.contains("decArrVal")); assertEquals("boolean", meta.fieldTypeName("boolVal")); assertEquals("Date", meta.fieldTypeName("dateVal")); assertEquals("UUID[]", meta.fieldTypeName("uuidArrVal")); assertEquals("Object", meta.fieldTypeName("objVal")); assertEquals("Map", meta.fieldTypeName("mapVal")); assertEquals("decimal", meta.fieldTypeName("decVal")); assertEquals("decimal[]", meta.fieldTypeName("decArrVal")); }
Collection<String> fields = meta.fieldNames(); fields = meta.fieldNames();
Collection<String> fields = meta.fieldNames();
/** * Tests that joining node metadata correctly handled on client. * @throws Exception If fails. */ @Test public void testJoiningNodeBinaryMetaOnClient() throws Exception { IgniteEx ig0 = (IgniteEx)startGrids(2); ig0.cluster().active(true); addBinaryType(ig0, "test_1", new IgniteBiTuple<>("name", String.class)); stopGrid(0); Ignite ig1 = grid(1); // Modify existing type. addBinaryType(ig1, "test_1", new IgniteBiTuple<>("id", Integer.class)); // Add new type. addBinaryType(ig1, "test_2", new IgniteBiTuple<>("name", String.class)); stopGrid(1); startGrid(0); IgniteEx client = startGrid(getConfiguration("client")); startGrid(1); awaitPartitionMapExchange(); // Check that new metadata from grid_1 was handled without NPE on client. assertNull(client.context().failure().failureContext()); // Check that metadata from grid_1 correctly loaded on client. assertTrue(client.binary().type("test_1").fieldNames().containsAll(Arrays.asList("id", "name"))); assertTrue(client.binary().type("test_2").fieldNames().contains("name")); }
/** * */ @Test public void testMetadataChanging() { GridBinaryTestClasses.TestObjectContainer c = new GridBinaryTestClasses.TestObjectContainer(); BinaryObjectBuilderImpl mutableObj = wrap(c); mutableObj.setField("intField", 1); mutableObj.setField("intArrField", new int[] {1}); mutableObj.setField("arrField", new String[] {"1"}); mutableObj.setField("strField", "1"); mutableObj.setField("colField", Lists.newArrayList("1")); mutableObj.setField("mapField", Maps.newHashMap(ImmutableMap.of(1, "1"))); mutableObj.setField("enumField", GridBinaryTestClasses.TestObjectEnum.A); mutableObj.setField("enumArrField", new Enum[] {GridBinaryTestClasses.TestObjectEnum.A}); mutableObj.build(); BinaryType metadata = binaries().type(c.getClass()); assertTrue(metadata.fieldNames().containsAll(Arrays.asList("intField", "intArrField", "arrField", "strField", "colField", "mapField", "enumField", "enumArrField"))); assertEquals("int", metadata.fieldTypeName("intField")); assertEquals("int[]", metadata.fieldTypeName("intArrField")); assertEquals("String[]", metadata.fieldTypeName("arrField")); assertEquals("String", metadata.fieldTypeName("strField")); assertEquals("Collection", metadata.fieldTypeName("colField")); assertEquals("Map", metadata.fieldTypeName("mapField")); assertEquals("Enum", metadata.fieldTypeName("enumField")); assertEquals("Enum[]", metadata.fieldTypeName("enumArrField")); }
if (m1.typeName().equals(m2.typeName())) { assertEquals(m1.affinityKeyFieldName(), m2.affinityKeyFieldName()); assertEquals(m1.fieldNames(), m2.fieldNames());