Java Code Examples for org.apache.ignite.binary.BinaryObject#field()
The following examples show how to use
org.apache.ignite.binary.BinaryObject#field() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ComputeClientTask.java From ignite with Apache License 2.0 | 6 votes |
/** {@inheritDoc} */ @Nullable @Override public Object execute() { long sum = 0; int cnt = 0; for (BinaryObject employee : employees) { System.out.println(">>> Processing employee: " + employee.field("name")); // Get salary from binary object. Note that object // doesn't need to be fully deserialized. long salary = employee.field("salary"); sum += salary; cnt++; } return new IgniteBiTuple<>(sum, cnt); }
Example 2
Source File: CacheQueryExample.java From ignite with Apache License 2.0 | 6 votes |
/** * Example for scan query based on a predicate using binary objects. */ private static void scanQuery() { IgniteCache<BinaryObject, BinaryObject> cache = Ignition.ignite() .cache(PERSON_CACHE).withKeepBinary(); ScanQuery<BinaryObject, BinaryObject> scan = new ScanQuery<>( new IgniteBiPredicate<BinaryObject, BinaryObject>() { @Override public boolean apply(BinaryObject key, BinaryObject person) { return person.<Double>field("salary") <= 1000; } } ); // Execute queries for salary ranges. print("People with salaries between 0 and 1000 (queried with SCAN query): ", cache.query(scan).getAll()); }
Example 3
Source File: BinaryObjectBuilderDefaultMappersSelfTest.java From ignite with Apache License 2.0 | 6 votes |
/** * @throws Exception If failed. */ @Test public void testCollectionField() throws Exception { BinaryObjectBuilder builder = builder("Class"); builder.setField("collectionField", Arrays.asList(new Value(1), new Value(2))); builder.setField("collectionField2", Arrays.asList(new Value(1), new Value(2)), Collection.class); BinaryObject po = builder.build(); assertEquals(expectedHashCode("Class"), po.type().typeId()); assertEquals(BinaryArrayIdentityResolver.instance().hashCode(po), po.hashCode()); List<Value> list = po.field("collectionField"); assertEquals(2, list.size()); assertEquals(1, list.get(0).i); assertEquals(2, list.get(1).i); List<BinaryObject> list2 = po.field("collectionField2"); assertEquals(2, list2.size()); assertEquals(1, list2.get(0).<Value>deserialize().i); assertEquals(2, list2.get(1).<Value>deserialize().i); }
Example 4
Source File: BinaryMetadataUpdatesFlowTest.java From ignite with Apache License 2.0 | 6 votes |
/** * Validates that all updates are readable on the specified node. * * @param ignite Ignite instance. */ private void validateCache(Ignite ignite) { String name = ignite.name(); for (Cache.Entry entry : ignite.cache(DEFAULT_CACHE_NAME).withKeepBinary()) { BinaryObject binObj = (BinaryObject)entry.getValue(); Integer idx = binObj.field(SEQ_NUM_FLD); BinaryUpdateDescription desc = updatesList.get(idx - 1); Object val = binObj.field(desc.fieldName); String errMsg = "Field " + desc.fieldName + " has unexpeted value (index=" + idx + ", node=" + name + ")"; if (desc.fieldType == FieldType.OBJECT) assertTrue(errMsg, val instanceof BinaryObject); else if (desc.fieldType == FieldType.ARRAY) assertArrayEquals(errMsg, (byte[])desc.val, (byte[])val); else assertEquals(errMsg, desc.val, binObj.field(desc.fieldName)); } }
Example 5
Source File: ReadStoreExtractor.java From spring-boot-akka-event-sourcing-starter with Apache License 2.0 | 6 votes |
/** * @param msg the cache entry event which contain the added/update value object * @return the mapped read cache entry which will be streamed to the target cache */ @Override public Map.Entry<String, JournalReadItem> extract(CacheEntryEvent<Object, BinaryObject> msg) { IgniteBiTuple<String, JournalReadItem> readRecord = null; final BinaryObject value = msg.getValue(); final String orderId = value.field(FieldNames.persistenceId.name()); final byte[] payload = value.field(FieldNames.payload.name()); try { final MessageFormats.PersistentMessage persistentMessage = MessageFormats.PersistentMessage.parseFrom(payload); if (persistentMessage.hasPayload()) { final OrderEvent event = (OrderEvent) orderOrderManagerSerializer.fromBinary(persistentMessage.getPayload().getPayload().toByteArray(), persistentMessage.getPayload().getPayloadManifest().toStringUtf8()); readRecord = new IgniteBiTuple<>(event.getOrderId(), new JournalReadItem(event.getOrderId(), event.getOrderStatus().name())); } } catch (InvalidProtocolBufferException | NotSerializableException e) { log.error("error in parsing the msg for id {} with error {}", orderId, e); } return readRecord; }
Example 6
Source File: IgniteBinaryTest.java From ignite with Apache License 2.0 | 6 votes |
/** */ 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()); }
Example 7
Source File: IgniteCacheDistributedPartitionQueryAbstractSelfTest.java From ignite with Apache License 2.0 | 6 votes |
/** {@inheritDoc} */ @Override public int partition(Object key) { Integer regionId; if (key instanceof RegionKey) regionId = ((RegionKey)key).regionId; else if (key instanceof BinaryObject) { BinaryObject bo = (BinaryObject)key; regionId = bo.field("regionId"); } else throw new IgniteException("Unsupported key for region aware affinity"); List<Integer> range = REGION_TO_PART_MAP.get(regionId); Integer cnt = range.get(1); return U.safeAbs(key.hashCode() % cnt) + range.get(0); // Assign partition in region's range. }
Example 8
Source File: CacheClientBinaryPutGetExample.java From ignite with Apache License 2.0 | 6 votes |
/** * Execute individual put and get, getting value in binary format, without de-serializing it. * * @param cache Cache. */ private static void putGetBinary(IgniteCache<Integer, Organization> cache) { // Create new Organization binary object to store in cache. Organization org = new Organization( "Microsoft", // Name. new Address("1096 Eddy Street, San Francisco, CA", 94109), // Address. OrganizationType.PRIVATE, // Type. new Timestamp(System.currentTimeMillis())); // Last update time. // Put created data entry to cache. cache.put(1, org); // Get cache that will get values as binary objects. IgniteCache<Integer, BinaryObject> binaryCache = cache.withKeepBinary(); // Get recently created organization as a binary object. BinaryObject po = binaryCache.get(1); // Get organization's name from binary object (note that // object doesn't need to be fully deserialized). String name = po.field("name"); System.out.println(); System.out.println(">>> Retrieved organization name from binary object: " + name); }
Example 9
Source File: IgniteBinaryQueryTest.java From ignite with Apache License 2.0 | 6 votes |
/** */ 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()); }
Example 10
Source File: RecommendationBinaryDatasetDataBuilder.java From ignite with Apache License 2.0 | 6 votes |
/** {@inheritDoc} */ @Override public RecommendationDatasetData<Serializable, Serializable> build(LearningEnvironment env, Iterator<UpstreamEntry<Object, BinaryObject>> upstreamData, long upstreamDataSize, EmptyContext ctx) { List<ObjectSubjectRatingTriplet<Serializable, Serializable>> ratings = new ArrayList<>(); while (upstreamData.hasNext()) { UpstreamEntry<Object, BinaryObject> e = upstreamData.next(); BinaryObject val = e.getValue(); Serializable objId = val.field(objFieldName); Serializable subjId = val.field(subjFieldName); Number rating = val.field(ratingFieldName); ratings.add(new ObjectSubjectRatingTriplet<>(objId, subjId, rating.doubleValue())); } return new RecommendationDatasetData<>(ratings); }
Example 11
Source File: IgniteEmbeddedAssociationSnapshot.java From hibernate-ogm-ignite with GNU Lesser General Public License v2.1 | 6 votes |
public IgniteEmbeddedAssociationSnapshot(AssociationKey associationKey, Tuple tuple) { this.associationMetadata = associationKey.getMetadata(); this.tuple = tuple; BinaryObject obj = ( (IgniteTupleSnapshot) tuple.getSnapshot() ).getCacheValue(); Object objects[] = obj != null ? (Object[]) obj.field( StringHelper.realColumnName( associationMetadata.getCollectionRole() ) ) : null; rows = new HashMap<>(); if ( objects != null ) { String rowKeyColumnNames[] = new String[ associationMetadata.getRowKeyColumnNames().length ]; for ( int i = 0; i < rowKeyColumnNames.length; i++ ) { rowKeyColumnNames[i] = StringHelper.stringAfterPoint( associationMetadata.getRowKeyColumnNames()[i] ); } for ( int i = 0; i < objects.length; i++ ) { BinaryObject itemObject = (BinaryObject) objects[i]; Object rowKeyColumnValues[] = new Object[rowKeyColumnNames.length]; for ( int j = 0; j < rowKeyColumnNames.length; j++ ) { rowKeyColumnValues[j] = itemObject.field( rowKeyColumnNames[j] ); } RowKey rowKey = new RowKey( associationMetadata.getRowKeyColumnNames(), rowKeyColumnValues ); this.rows.put( rowKey, new IgniteTupleSnapshot( null, itemObject, associationMetadata.getAssociatedEntityKeyMetadata().getEntityKeyMetadata() ) ); } } }
Example 12
Source File: JdbcResultSetSelfTest.java From ignite with Apache License 2.0 | 5 votes |
/** * Checks particular field from original binary object * * @param original binary object representation of original object * @param strToCheck string from result set, to be checked for presence of all fields * @param fieldName field name have being checked */ private static void checkFieldPresenceInToString(final BinaryObject original, final String strToCheck, final String fieldName) { final Object fieldVal = original.field(fieldName); String strValToSearch = Objects.toString(fieldVal); if (fieldVal != null) { final Class<?> aCls = fieldVal.getClass(); if (aCls.isArray()) { final Class<?> elemCls = aCls.getComponentType(); if (elemCls == Byte.TYPE) strValToSearch = Arrays.toString((byte[])fieldVal); } else if (BinaryObject.class.isAssignableFrom(aCls)) { // hack to avoid search of unpredictable toString representation like // JdbcResultSetSelfTest$TestObjectField [idHash=1518952510, hash=11433031, a=100, b=AAAA] // in toString // other way to fix: iterate on binary object fields: final BinaryObject binVal = (BinaryObject)fieldVal; strValToSearch = ""; } } assertTrue("Expected to find field " + fieldName + " having value " + strValToSearch + " in toString representation [" + strToCheck + "]", strToCheck.contains(fieldName + "=" + strValToSearch)); }
Example 13
Source File: QueryBinaryProperty.java From ignite with Apache License 2.0 | 5 votes |
/** * Gets field value for the given binary object. * * @param obj Binary object. * @return Field value. */ @SuppressWarnings("IfMayBeConditional") private Object fieldValue(BinaryObject obj) { BinaryField field = binaryField(obj); if (field != null) return field.value(obj); else return obj.field(propName); }
Example 14
Source File: BinaryObjectVectorizer.java From ignite with Apache License 2.0 | 5 votes |
/** {@inheritDoc} */ @Override protected Double feature(String coord, K key, BinaryObject value) { HashMap<Object, Double> mapping = featureValMappings.get(coord); if (mapping != null) return mapping.get(coord); Number val = value.field(coord); return val != null ? val.doubleValue() : null; }
Example 15
Source File: BinaryObjectBuilderDefaultMappersSelfTest.java From ignite with Apache License 2.0 | 5 votes |
/** * @throws Exception If failed. */ @Test public void testSeveralFields() throws Exception { BinaryObjectBuilder builder = builder("Class"); builder.setField("i", 111); builder.setField("f", 111.111f); builder.setField("iArr", new int[] {1, 2, 3}); builder.setField("obj", new Key(1)); builder.setField("col", Arrays.asList(new Value(1), new Value(2)), Collection.class); BinaryObject po = builder.build(); assertEquals(expectedHashCode("Class"), po.type().typeId()); assertEquals(BinaryArrayIdentityResolver.instance().hashCode(po), po.hashCode()); assertEquals(111, po.<Integer>field("i").intValue()); assertEquals(111.111f, po.<Float>field("f").floatValue(), 0); assertTrue(Arrays.equals(new int[] {1, 2, 3}, po.<int[]>field("iArr"))); assertEquals(1, po.<BinaryObject>field("obj").<Key>deserialize().i); List<BinaryObject> list = po.field("col"); assertEquals(2, list.size()); assertEquals(1, list.get(0).<Value>deserialize().i); assertEquals(2, list.get(1).<Value>deserialize().i); }
Example 16
Source File: BinaryMarshallerSelfTest.java From ignite with Apache License 2.0 | 4 votes |
/** * @throws Exception If failed. */ @Test public void testSimpleObject() throws Exception { BinaryMarshaller marsh = binaryMarshaller(Arrays.asList( new BinaryTypeConfiguration(SimpleObject.class.getName()) )); SimpleObject obj = simpleObject(); BinaryObject po = marshal(obj, marsh); assertEquals(obj, po.deserialize()); assertEquals(obj.b, (byte)po.field("b")); assertEquals(obj.s, (short)po.field("s")); assertEquals(obj.i, (int)po.field("i")); assertEquals(obj.l, (long)po.field("l")); assertEquals(obj.f, (float)po.field("f"), 0); assertEquals(obj.d, (double)po.field("d"), 0); assertEquals(obj.c, (char)po.field("c")); assertEquals(obj.bool, (boolean)po.field("bool")); assertEquals(obj.str, po.field("str")); assertEquals(obj.uuid, po.field("uuid")); assertEquals(obj.date, po.field("date")); assertEquals(Date.class, obj.date.getClass()); assertEquals(obj.ts, po.field("ts")); assertArrayEquals(obj.bArr, (byte[])po.field("bArr")); assertArrayEquals(obj.sArr, (short[])po.field("sArr")); assertArrayEquals(obj.iArr, (int[])po.field("iArr")); assertArrayEquals(obj.lArr, (long[])po.field("lArr")); assertArrayEquals(obj.fArr, (float[])po.field("fArr"), 0); assertArrayEquals(obj.dArr, (double[])po.field("dArr"), 0); assertArrayEquals(obj.cArr, (char[])po.field("cArr")); assertBooleanArrayEquals(obj.boolArr, (boolean[])po.field("boolArr")); assertArrayEquals(obj.strArr, (String[])po.field("strArr")); assertArrayEquals(obj.uuidArr, (UUID[])po.field("uuidArr")); assertArrayEquals(obj.dateArr, (Date[])po.field("dateArr")); assertArrayEquals(obj.objArr, (Object[])po.field("objArr")); assertEquals(obj.col, po.field("col")); assertEquals(obj.map, po.field("map")); assertEquals(new Integer(obj.enumVal.ordinal()), new Integer(((BinaryObject)po.field("enumVal")).enumOrdinal())); assertArrayEquals(ordinals(obj.enumArr), ordinals((BinaryObject[])po.field("enumArr"))); assertNull(po.field("unknown")); BinaryObject innerPo = po.field("inner"); assertEquals(obj.inner, innerPo.deserialize()); assertEquals(obj.inner.b, (byte)innerPo.field("b")); assertEquals(obj.inner.s, (short)innerPo.field("s")); assertEquals(obj.inner.i, (int)innerPo.field("i")); assertEquals(obj.inner.l, (long)innerPo.field("l")); assertEquals(obj.inner.f, (float)innerPo.field("f"), 0); assertEquals(obj.inner.d, (double)innerPo.field("d"), 0); assertEquals(obj.inner.c, (char)innerPo.field("c")); assertEquals(obj.inner.bool, (boolean)innerPo.field("bool")); assertEquals(obj.inner.str, innerPo.field("str")); assertEquals(obj.inner.uuid, innerPo.field("uuid")); assertEquals(obj.inner.date, innerPo.field("date")); assertEquals(Date.class, obj.inner.date.getClass()); assertEquals(obj.inner.ts, innerPo.field("ts")); assertArrayEquals(obj.inner.bArr, (byte[])innerPo.field("bArr")); assertArrayEquals(obj.inner.sArr, (short[])innerPo.field("sArr")); assertArrayEquals(obj.inner.iArr, (int[])innerPo.field("iArr")); assertArrayEquals(obj.inner.lArr, (long[])innerPo.field("lArr")); assertArrayEquals(obj.inner.fArr, (float[])innerPo.field("fArr"), 0); assertArrayEquals(obj.inner.dArr, (double[])innerPo.field("dArr"), 0); assertArrayEquals(obj.inner.cArr, (char[])innerPo.field("cArr")); assertBooleanArrayEquals(obj.inner.boolArr, (boolean[])innerPo.field("boolArr")); assertArrayEquals(obj.inner.strArr, (String[])innerPo.field("strArr")); assertArrayEquals(obj.inner.uuidArr, (UUID[])innerPo.field("uuidArr")); assertArrayEquals(obj.inner.dateArr, (Date[])innerPo.field("dateArr")); assertArrayEquals(obj.inner.objArr, (Object[])innerPo.field("objArr")); assertEquals(obj.inner.col, innerPo.field("col")); assertEquals(obj.inner.map, innerPo.field("map")); assertEquals(new Integer(obj.inner.enumVal.ordinal()), new Integer(((BinaryObject)innerPo.field("enumVal")).enumOrdinal())); assertArrayEquals(ordinals(obj.inner.enumArr), ordinals((BinaryObject[])innerPo.field("enumArr"))); assertNull(innerPo.field("inner")); assertNull(innerPo.field("unknown")); }
Example 17
Source File: BinaryObjectBuilderDefaultMappersSelfTest.java From ignite with Apache License 2.0 | 4 votes |
/** * @throws Exception If failed. */ @Test public void testBooleanArrayField() throws Exception { BinaryObjectBuilder builder = builder("Class"); builder.setField("booleanArrayField", new boolean[] {true, false}); BinaryObject po = builder.build(); assertEquals(expectedHashCode("Class"), po.type().typeId()); assertEquals(BinaryArrayIdentityResolver.instance().hashCode(po), po.hashCode()); boolean[] arr = po.field("booleanArrayField"); assertEquals(2, arr.length); assertTrue(arr[0]); assertFalse(arr[1]); }
Example 18
Source File: BinaryObjectBuilderDefaultMappersSelfTest.java From ignite with Apache License 2.0 | 4 votes |
/** * @throws Exception If failed. */ @Test public void testObjectArrayField() throws Exception { BinaryObjectBuilder builder = builder("Class"); builder.setField("objectArrayField", new Value[] {new Value(1), new Value(2)}); BinaryObject po = builder.build(); assertEquals(expectedHashCode("Class"), po.type().typeId()); assertEquals(BinaryArrayIdentityResolver.instance().hashCode(po), po.hashCode()); Object[] arr = po.field("objectArrayField"); assertEquals(2, arr.length); assertEquals(1, ((BinaryObject)arr[0]).<Value>deserialize().i); assertEquals(2, ((BinaryObject)arr[1]).<Value>deserialize().i); }
Example 19
Source File: BinaryObjectBuilderDefaultMappersSelfTest.java From ignite with Apache License 2.0 | 4 votes |
/** * @throws Exception If failed. */ @Test public void testOffheapBinary() throws Exception { BinaryObjectBuilder builder = builder("Class"); builder.setField("i", 111); builder.setField("f", 111.111f); builder.setField("iArr", new int[] {1, 2, 3}); builder.setField("obj", new Key(1)); builder.setField("col", Arrays.asList(new Value(1), new Value(2)), Collection.class); BinaryObject po = builder.build(); byte[] arr = ((CacheObjectBinaryProcessorImpl)(grid(0)).context().cacheObjects()).marshal(po); long ptr = GridUnsafe.allocateMemory(arr.length + 5); try { long ptr0 = ptr; GridUnsafe.putBoolean(null, ptr0++, false); int len = arr.length; if (BIG_ENDIAN) GridUnsafe.putIntLE(ptr0, len); else GridUnsafe.putInt(ptr0, len); GridUnsafe.copyHeapOffheap(arr, GridUnsafe.BYTE_ARR_OFF, ptr0 + 4, arr.length); BinaryObject offheapObj = (BinaryObject) ((CacheObjectBinaryProcessorImpl)(grid(0)).context().cacheObjects()).unmarshal(ptr, false); assertEquals(BinaryObjectOffheapImpl.class, offheapObj.getClass()); assertEquals(expectedHashCode("Class"), offheapObj.type().typeId()); assertEquals(BinaryArrayIdentityResolver.instance().hashCode(po), offheapObj.hashCode()); assertEquals(111, offheapObj.<Integer>field("i").intValue()); assertEquals(111.111f, offheapObj.<Float>field("f").floatValue(), 0); assertTrue(Arrays.equals(new int[] {1, 2, 3}, offheapObj.<int[]>field("iArr"))); assertEquals(1, offheapObj.<BinaryObject>field("obj").<Key>deserialize().i); List<BinaryObject> list = offheapObj.field("col"); assertEquals(2, list.size()); assertEquals(1, list.get(0).<Value>deserialize().i); assertEquals(2, list.get(1).<Value>deserialize().i); assertEquals(po, offheapObj); assertEquals(offheapObj, po); } finally { GridUnsafe.freeMemory(ptr); } }
Example 20
Source File: BinaryMetadataMoveLegacyFolderTest.java From ignite with Apache License 2.0 | 2 votes |
/** * Test that binary_meta directory, that was previously located in workdir's root, is successfully moving to PDS * folder. */ @Test public void testBinaryMetadataDirectoryMigration() throws Exception { IgniteConfiguration configuration = getConfiguration(); String typeName = "TestBinaryType"; String fieldName = "testField"; // build binary type and get byte representation byte[] testBinaryTypeDefinition = createBinaryType(typeName, fieldName); File legacyDir = new File(U.resolveWorkDirectory( U.defaultWorkDirectory(), "binary_meta", false ), U.maskForFileName(configuration.getConsistentId().toString())); legacyDir.mkdirs(); // just some random type id File testBinaryTypeDefFile = new File(legacyDir, "708045005.bin"); // write binary type definition to legacy folder Files.write(testBinaryTypeDefFile.toPath(), testBinaryTypeDefinition); IgniteEx grid = startGrid(configuration); // legacy metadata dir must be deleted at this moment assertFalse(legacyDir.exists()); IgniteBinary binary = grid.binary(); // binary type must exist BinaryType type = binary.type(typeName); assertNotNull(type); // it must be the type that we created in the beginning (1 field with specific name) Collection<String> strings = type.fieldNames(); assertEquals(1, strings.size()); assertTrue(strings.contains(fieldName)); // try using this binary type to create, put and get object BinaryObjectBuilder builder = binary.builder(typeName); builder.setField(fieldName, 1); IgniteCache<Object, Object> testCache = grid.getOrCreateCache("test").withKeepBinary(); testCache.put("some", builder.build()); BinaryObject some = (BinaryObject)testCache.get("some"); Integer test = some.<Integer>field(fieldName); assertEquals(1, test.intValue()); }