/** {@inheritDoc} */ @Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception { IgniteConfiguration cfg = super.getConfiguration(gridName); CacheConfiguration dfltCacheCfg = defaultCacheConfiguration(); QueryEntity queryEntity = new QueryEntity(Object.class.getName(), Object.class.getName()); LinkedHashMap<String, String> fields = new LinkedHashMap<>(); fields.put("exceptionOid", Object.class.getName()); queryEntity.setFields(fields); Set<String> keyFields = new HashSet<>(); keyFields.add("exceptionOid"); queryEntity.setKeyFields(keyFields); dfltCacheCfg.setQueryEntities(F.asList(queryEntity)); cfg.setCacheConfiguration(dfltCacheCfg); return cfg; }
keyFields.add("id"); entity.setKeyFields(keyFields);
/** @throws Exception If failed.*/ private void createCachesForDecimalPrecisionTests() throws Exception { Map<String, Integer> decDecPrecision = new HashMap<>(); decDecPrecision.put(KEY_FIELD_NAME, 4); decDecPrecision.put(VAL_FIELD_NAME, 4); jcache(grid(0), cacheConfiguration(new QueryEntity(BigDecimal.class.getName(), BigDecimal.class.getName()) .setFieldsPrecision(decDecPrecision)), DEC_CACHE_NAME_FOR_PREC); Map<String, Integer> orgEmployeePrecision = new HashMap<>(); orgEmployeePrecision.put("id", 4); orgEmployeePrecision.put("salary", 4); jcache(grid(0), cacheConfiguration(new QueryEntity(DecOrganization.class.getName(), Employee.class.getName()) .setKeyFields(Collections.singleton("id")) .addQueryField("id", "java.math.BigDecimal", "id") .addQueryField("salary", "java.math.BigDecimal", "salary") .setFieldsPrecision(orgEmployeePrecision)), OBJ_CACHE_NAME_FOR_PREC); Map<String, Integer> decEmployeePrecision = new HashMap<>(); decEmployeePrecision.put(KEY_FIELD_NAME, 4); decEmployeePrecision.put("salary", 4); jcache(grid(0), cacheConfiguration(new QueryEntity(BigDecimal.class.getName(), Employee.class.getName()) .addQueryField("salary", "java.math.BigDecimal", "salary") .setFieldsPrecision(decEmployeePrecision)), DEC_EMPL_CACHE_NAME_FOR_PREC); }
@Override public Object call() throws Exception { QueryEntity e = new QueryEntity(); e.setTableName("City"); e.setKeyFields(Collections.singleton("name")); e.setFields(new LinkedHashMap<>(Collections.singletonMap("name", String.class.getName()))); e.setIndexes(Collections.singleton(new QueryIndex("name").setName("idx"))); e.setKeyType("CityKey"); e.setValueType("City"); queryProcessor(client()).dynamicTableCreate("PUBLIC", e, CacheMode.PARTITIONED.name(), null, null, null, null, CacheAtomicityMode.ATOMIC, null, 10, false, false); return null; } }, SchemaOperationException.class, "Index already exists: idx");
.setKeyFields(Collections.singleton("id")) .addQueryField("id", "java.math.BigDecimal", "id") .addQueryField("salary", "java.math.BigDecimal", "salary")
.setValueType(Val.class.getName()) .setFields(fields) .setKeyFields(new HashSet<>(Arrays.asList("keyStr", "keyLong", "keyPojo"))) .setIndexes(indexes) ))
/** @throws Exception If failed.*/ private void createCachesForStringTests() throws Exception { Map<String, Integer> strStrPrecision = new HashMap<>(); strStrPrecision.put(KEY_FIELD_NAME, 5); strStrPrecision.put(VAL_FIELD_NAME, 5); jcache(grid(0), cacheConfiguration(new QueryEntity(String.class.getName(), String.class.getName()) .setFieldsPrecision(strStrPrecision)), STR_CACHE_NAME); Map<String, Integer> orgAddressPrecision = new HashMap<>(); orgAddressPrecision.put("name", 5); orgAddressPrecision.put("address", 5); jcache(grid(0), cacheConfiguration(new QueryEntity(Organization.class.getName(), Address.class.getName()) .setKeyFields(Collections.singleton("name")) .addQueryField("name", "java.lang.String", "name") .addQueryField("address", "java.lang.String", "address") .setFieldsPrecision(orgAddressPrecision)), OBJ_CACHE_NAME); Map<String, Integer> strOrgPrecision = new HashMap<>(); strOrgPrecision.put(KEY_FIELD_NAME, 5); jcache(grid(0), cacheConfiguration(new QueryEntity(String.class.getName(), Organization.class.getName()) .setFieldsPrecision(strOrgPrecision)), STR_ORG_CACHE_NAME); jcache(grid(0), cacheConfiguration(new QueryEntity(String.class.getName(), Organization.class.getName()) .addQueryField("name", "java.lang.String", "name") .addQueryField("address", "java.lang.String", "address") .setFieldsPrecision(strOrgPrecision)), STR_ORG_WITH_FIELDS_CACHE_NAME); }
/** {@inheritDoc} */ @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception { IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName); CacheConfiguration ccfg = new CacheConfiguration(PERSON_CACHE); if (qryEntityCfg) { CacheKeyConfiguration keyCfg = new CacheKeyConfiguration(keyCls.getName(), "name"); cfg.setCacheKeyConfiguration(keyCfg); QueryEntity entity = new QueryEntity(); entity.setKeyType(keyCls.getName()); entity.setValueType(Person.class.getName()); if (keyFieldSpecified) entity.setKeyFields(Stream.of("name").collect(Collectors.toSet())); entity.addQueryField("id", Integer.class.getName(), null); entity.addQueryField("name", String.class.getName(), null); ccfg.setQueryEntities(F.asList(entity)); } else { CacheKeyConfiguration keyCfg = new CacheKeyConfiguration(keyCls); cfg.setCacheKeyConfiguration(keyCfg); ccfg.setIndexedTypes(keyCls, Person.class); } cfg.setCacheConfiguration(ccfg); return cfg; }
queryEntity.setKeyFields(keyFiles);
/** {@inheritDoc} */ @Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception { IgniteConfiguration cfg = super.getConfiguration(gridName); cfg.setLocalHost("127.0.0.1"); cfg.setPeerClassLoadingEnabled(true); TcpDiscoveryVmIpFinder ipFinder = new TcpDiscoveryVmIpFinder(true); ipFinder.setAddresses(Collections.singletonList("127.0.0.1:47500..47509")); TcpDiscoverySpi discoSpi = new TcpDiscoverySpi(); discoSpi.setIpFinder(ipFinder); cfg.setDiscoverySpi(discoSpi); CacheConfiguration<Object, Integer> ccfg = new CacheConfiguration<>(DEFAULT_CACHE_NAME); cfg.setMarshaller(new BinaryMarshaller()); QueryEntity queryEntity = new QueryEntity("KeyType", Integer.class.getName()); LinkedHashMap<String, String> fields = new LinkedHashMap<>(); fields.put("a", "TypeA"); fields.put("b", "TypeB"); queryEntity.setFields(fields); //Specify key fields in upper register HashSet<String> keyFields = new HashSet<>(); keyFields.add("a"); keyFields.add("B"); queryEntity.setKeyFields(keyFields); ccfg.setQueryEntities(F.asList(queryEntity)); cfg.setCacheConfiguration(ccfg); return cfg; }
)) .setKeyFields(Collections.singleton("id")) .setNotNullFields(Collections.singleton("id")) .setDefaultFieldValues(Collections.singletonMap("id", 0))
/** * @return Default cache configuration. */ protected CacheConfiguration<KeyClass, ValueClass> cacheConfiguration() { CacheConfiguration ccfg = new CacheConfiguration().setName(CACHE_NAME); QueryEntity entity = new QueryEntity(); entity.setKeyType(KeyClass.class.getName()); entity.setValueType(ValueClass.class.getName()); entity.setKeyFieldName(FIELD_KEY_ALIAS); entity.addQueryField(FIELD_KEY_ALIAS, entity.getKeyType(), null); entity.addQueryField(FIELD_KEY, Long.class.getName(), null); entity.addQueryField(FIELD_NAME_1_ESCAPED, Long.class.getName(), null); entity.addQueryField(FIELD_NAME_2_ESCAPED, Long.class.getName(), null); entity.setKeyFields(Collections.singleton(FIELD_KEY)); entity.setAliases(Collections.singletonMap(FIELD_NAME_2_ESCAPED, alias(FIELD_NAME_2_ESCAPED))); ccfg.setQueryEntities(Collections.singletonList(entity)); ccfg.setNodeFilter(new NodeFilter()); ccfg.setWriteSynchronizationMode(CacheWriteSynchronizationMode.FULL_SYNC); ccfg.setBackups(1); ccfg.setAffinity(new RendezvousAffinityFunction(false, 128)); return ccfg; }
/** {@inheritDoc} */ @Override protected void beforeTest() throws Exception { super.beforeTest(); Map<String, Object> defsFK2 = new HashMap<>(); defsFK2.put("fk2", DEFAULT_FK2_VAL); cache = jcache(grid(0), defaultCacheConfiguration() .setName("testCache") .setQueryEntities(Arrays.asList( new QueryEntity(Key.class.getName(), Val.class.getName()) .addQueryField("fk1", "java.lang.Long", null) .addQueryField("fk2", "java.lang.Long", null) .addQueryField("fv1", "java.lang.Long", null) .addQueryField("fv2", "java.lang.Long", null) .setTableName("FORGOTTEN_KEY_FLDS"), new QueryEntity(Key.class.getName(), Integer.class.getName()) .addQueryField("fk1", "java.lang.Long", null) .addQueryField("fk2", "java.lang.Long", null) .setDefaultFieldValues(defsFK2) .setKeyFields(new HashSet<>(Arrays.asList("fk1", "fk2"))) .setTableName("WITH_KEY_FLDS"), new QueryEntity(Integer.class.getName(), Val2.class.getName()) .addQueryField("fv1", "java.lang.Long", null) .addQueryField("fv2", "java.lang.Long", null) .setTableName("INT_KEY_TAB"), new QueryEntity(SuperKey.class, String.class) .setTableName("SUPER_TAB") )) , "testCache"); }
/** * @return Default cache configuration. */ private CacheConfiguration cacheConfiguration() { CacheConfiguration<KeyClass, ValueClass> ccfg = new CacheConfiguration<KeyClass, ValueClass>() .setName(CACHE_NAME); QueryEntity entity = new QueryEntity(); entity.setKeyType(KeyClass.class.getName()); entity.setValueType(ValueClass.class.getName()); entity.addQueryField("id", Long.class.getName(), null); entity.addQueryField(FIELD_NAME_1_ESCAPED, String.class.getName(), null); entity.addQueryField(FIELD_NAME_2_ESCAPED, String.class.getName(), null); entity.setKeyFields(Collections.singleton("id")); entity.setAliases(Collections.singletonMap(FIELD_NAME_2_ESCAPED, alias(FIELD_NAME_2_ESCAPED))); ccfg.setQueryEntities(Collections.singletonList(entity)); ccfg.setWriteSynchronizationMode(CacheWriteSynchronizationMode.FULL_SYNC); ccfg.setSqlEscapeAll(true); ccfg.setAtomicityMode(atomicityMode()); ccfg.setCacheMode(cacheMode()); if (nearCache()) ccfg.setNearConfiguration(new NearCacheConfiguration<KeyClass, ValueClass>()); return ccfg; }
/** * @param name Cache name. * @param atomicityMode Cache atomicity mode. * @return Cache configuration. */ private CacheConfiguration cacheConfiguration(String name, CacheAtomicityMode atomicityMode) { CacheConfiguration ccfg = new CacheConfiguration(DEFAULT_CACHE_NAME); ccfg.setName(name); ccfg.setCacheMode(PARTITIONED); ccfg.setWriteSynchronizationMode(FULL_SYNC); ccfg.setAtomicityMode(atomicityMode); ccfg.setBackups(1); QueryEntity qryEntity = new QueryEntity(); qryEntity.setKeyType(TestKey.class.getName()); qryEntity.setValueType(TestValue.class.getName()); qryEntity.addQueryField("id", Integer.class.getName(), null); qryEntity.addQueryField("val", Integer.class.getName(), null); qryEntity.setKeyFields(Collections.singleton("id")); qryEntity.setIndexes(F.asList(new QueryIndex("id"), new QueryIndex("val"))); ccfg.setQueryEntities(F.asList(qryEntity)); return ccfg; }
entity.addQueryField("name", String.class.getName(), null); entity.setKeyFields(new HashSet<>(Arrays.asList("id", "affKey")));
/** * @throws Exception In case of an error. */ @Test public void testEntryReadQueryEntities() throws Exception { Ignite ignite = startGrid(0); LinkedHashMap<String, String> fields = new LinkedHashMap<>(); fields.put("id", "java.lang.Integer"); fields.put("str", "java.lang.String"); IgniteCache binary = ignite.createCache(new CacheConfiguration<>() .setName("binary").setQueryEntities(Collections.singleton(new QueryEntity() .setKeyType("IdKey").setKeyFields(Collections.singleton("id")) .setValueType("Payload").setFields(fields).setTableName("binary")))); binary.query(new SqlFieldsQuery("INSERT INTO binary (id, str) VALUES (1, 'foo');")); binary.query(new SqlFieldsQuery("INSERT INTO binary (id, str) VALUES (2, 'bar');")); GridTestUtils.assertThrowsAnyCause( log, new Callable<Object>() { @Override public Object call() { return ignite.cache("binary").iterator().next(); } }, BinaryInvalidTypeException.class, "IdKey"); }
/** * @throws Exception In case of an error. */ @Test public void testEntryWriteCacheIsolation() throws Exception { Ignite ignite = startGridWithLogCapture(); LinkedHashMap<String, String> fields = new LinkedHashMap<>(); fields.put("id", "java.lang.Integer"); fields.put("str", "java.lang.String"); IgniteCache<IdKey, Payload> regular = ignite.createCache(new CacheConfiguration<IdKey, Payload>() .setName("regular").setQueryEntities(Collections.singleton(new QueryEntity() .setKeyType(getClass().getName() + "$IdKey").setKeyFields(Collections.singleton("id")) .setValueType(getClass().getName() + "$Payload").setFields(fields).setTableName("binary")))); IgniteCache<IdKey, Payload> binary = ignite.createCache(new CacheConfiguration<IdKey, Payload>() .setName("binary").setQueryEntities(Collections.singleton(new QueryEntity() .setKeyType("IdKey").setKeyFields(Collections.singleton("id")) .setValueType("Payload").setFields(fields).setTableName("binary")))); regular.put(new IdKey(1), new Payload("foo")); regular.put(new IdKey(2), new Payload("bar")); binary.put(new IdKey(1), new Payload("foo")); binary.put(new IdKey(2), new Payload("bar")); assertEquals(0, countRows(binary)); assertEquals(2, countRows(regular)); assertContainsExactlyOnce(capture.toString(), MESSAGE_PAYLOAD_VALUE); }
person.setKeyType(personKeyType); person.setValueType(Person.class.getName()); person.setKeyFields(Collections.singleton("id")); person.addQueryField("orgId", Integer.class.getName(), null); person.addQueryField("id", Integer.class.getName(), null);
/** * @throws Exception In case of an error. */ @Test public void testEntryWriteQueryEntities() throws Exception { Ignite ignite = startGridWithLogCapture(); LinkedHashMap<String, String> fields = new LinkedHashMap<>(); fields.put("id", "java.lang.Integer"); fields.put("str", "java.lang.String"); IgniteCache<IdKey, Payload> binary = ignite.createCache(new CacheConfiguration<IdKey, Payload>() .setName("binary").setQueryEntities(Collections.singleton(new QueryEntity() .setKeyType("IdKey").setKeyFields(Collections.singleton("id")) .setValueType("Payload").setFields(fields).setTableName("binary")))); binary.put(new IdKey(1), new Payload("foo")); binary.put(new IdKey(2), new Payload("bar")); assertEquals(0, countRows(binary)); binary.destroy(); binary = ignite.createCache(new CacheConfiguration<IdKey, Payload>() .setName("binary").setQueryEntities(Collections.singleton(new QueryEntity() .setKeyType("IdKey").setKeyFields(Collections.singleton("id")) .setValueType("Payload").setFields(fields).setTableName("binary")))); binary.put(new IdKey(1), new Payload("foo")); binary.put(new IdKey(2), new Payload("bar")); assertEquals(0, countRows(binary)); assertContainsExactlyOnce(capture.toString(), MESSAGE_PAYLOAD_VALUE); }