Search in sources :

Example 1 with GridCacheSqlMetadata

use of org.apache.ignite.internal.processors.cache.query.GridCacheSqlMetadata in project ignite by apache.

the class JdbcDatabaseMetadata method updateMetaData.

/**
     * Updates meta data.
     *
     * @throws SQLException In case of error.
     */
@SuppressWarnings("unchecked")
private void updateMetaData() throws SQLException {
    if (conn.isClosed())
        throw new SQLException("Connection is closed.");
    try {
        Ignite ignite = conn.ignite();
        UUID nodeId = conn.nodeId();
        Collection<GridCacheSqlMetadata> metas;
        UpdateMetadataTask task = new UpdateMetadataTask(conn.cacheName(), nodeId == null ? ignite : null);
        metas = nodeId == null ? task.call() : ignite.compute(ignite.cluster().forNodeId(nodeId)).call(task);
        meta = U.newHashMap(metas.size());
        indexes = new ArrayList<>();
        for (GridCacheSqlMetadata m : metas) {
            String name = m.cacheName();
            if (name == null)
                name = "PUBLIC";
            Collection<String> types = m.types();
            Map<String, Map<String, String>> typesMap = U.newHashMap(types.size());
            for (String type : types) {
                typesMap.put(type.toUpperCase(), m.fields(type));
                for (GridCacheSqlIndexMetadata idx : m.indexes(type)) {
                    int cnt = 0;
                    for (String field : idx.fields()) {
                        indexes.add(F.<Object>asList(name, type.toUpperCase(), !idx.unique(), idx.name().toUpperCase(), ++cnt, field, idx.descending(field)));
                    }
                }
            }
            meta.put(name, typesMap);
        }
    } catch (Exception e) {
        throw new SQLException("Failed to get meta data from Ignite.", e);
    }
}
Also used : SQLException(java.sql.SQLException) GridCacheSqlIndexMetadata(org.apache.ignite.internal.processors.cache.query.GridCacheSqlIndexMetadata) SQLException(java.sql.SQLException) GridCacheSqlMetadata(org.apache.ignite.internal.processors.cache.query.GridCacheSqlMetadata) Ignite(org.apache.ignite.Ignite) UUID(java.util.UUID) Map(java.util.Map)

Example 2 with GridCacheSqlMetadata

use of org.apache.ignite.internal.processors.cache.query.GridCacheSqlMetadata in project ignite by apache.

the class IgniteCacheAbstractFieldsQuerySelfTest method testCacheMetaData.

/** @throws Exception If failed. */
public void testCacheMetaData() throws Exception {
    for (String cacheName : grid(0).cacheNames()) ((IgniteKernal) grid(0)).getCache(cacheName).getAndPut(new GridCacheInternalKeyImpl("LONG"), new GridCacheAtomicLongValue(0));
    try {
        Collection<GridCacheSqlMetadata> metas = ((IgniteKernal) grid(0)).getCache(intCache.getName()).context().queries().sqlMetadata();
        assert metas != null;
        for (GridCacheSqlMetadata meta : metas) {
            Collection<String> types = meta.types();
            assertNotNull(types);
            if (personCache.getName().equals(meta.cacheName())) {
                assertEquals("Invalid types size", 1, types.size());
                assert types.contains("Person");
                if (binaryMarshaller) {
                    assert Object.class.getName().equals(meta.keyClass("Person"));
                    assert Object.class.getName().equals(meta.valueClass("Person"));
                } else {
                    assert AffinityKey.class.getName().equals(meta.keyClass("Person"));
                    assert Person.class.getName().equals(meta.valueClass("Person"));
                }
                Map<String, String> fields = meta.fields("Person");
                assert fields != null;
                assert fields.size() == 3;
                if (binaryMarshaller) {
                    assert Integer.class.getName().equals(fields.get("AGE"));
                    assert Integer.class.getName().equals(fields.get("ORGID"));
                } else {
                    assert int.class.getName().equals(fields.get("AGE"));
                    assert int.class.getName().equals(fields.get("ORGID"));
                }
                assert String.class.getName().equals(fields.get("NAME"));
                Collection<GridCacheSqlIndexMetadata> indexes = meta.indexes("Person");
                assertNotNull("Indexes should be defined", indexes);
                assertEquals(2, indexes.size());
                Set<String> idxFields = new HashSet<>();
                Iterator<GridCacheSqlIndexMetadata> it = indexes.iterator();
                Collection<String> indFlds = it.next().fields();
                assertNotNull("Fields for first index should be defined", indFlds);
                assertEquals("First index should have one field", indFlds.size(), 1);
                Iterator<String> indFldIt = indFlds.iterator();
                idxFields.add(indFldIt.next());
                indFlds = it.next().fields();
                assertNotNull("Fields for second index should be defined", indFlds);
                assertEquals("Second index should have one field", indFlds.size(), 1);
                indFldIt = indFlds.iterator();
                idxFields.add(indFldIt.next());
                assertTrue(idxFields.contains("AGE"));
                assertTrue(idxFields.contains("ORGID"));
            } else if (orgCache.getName().equals(meta.cacheName())) {
                assertEquals("Invalid types size", 1, types.size());
                assert types.contains("Organization");
                if (binaryMarshaller)
                    assert Object.class.getName().equals(meta.valueClass("Organization"));
                else
                    assert Organization.class.getName().equals(meta.valueClass("Organization"));
                assert String.class.getName().equals(meta.keyClass("Organization"));
                Map<String, String> fields = meta.fields("Organization");
                assert fields != null;
                assertEquals("Fields: " + fields, 2, fields.size());
                if (binaryMarshaller) {
                    assert Integer.class.getName().equals(fields.get("ID"));
                } else {
                    assert int.class.getName().equals(fields.get("ID"));
                }
                assert String.class.getName().equals(fields.get("NAME"));
            } else if (intCache.getName().equals(meta.cacheName())) {
                assertEquals("Invalid types size", 1, types.size());
                assert types.contains("Integer");
                assert Integer.class.getName().equals(meta.valueClass("Integer"));
                assert Integer.class.getName().equals(meta.keyClass("Integer"));
                Map<String, String> fields = meta.fields("Integer");
                assert fields != null;
                assert fields.size() == 2;
                assert Integer.class.getName().equals(fields.get("_KEY"));
                assert Integer.class.getName().equals(fields.get("_VAL"));
            } else if (strCache.getName().equals(meta.cacheName())) {
                assertEquals("Invalid types size", 1, types.size());
                assert types.contains("String");
                assert String.class.getName().equals(meta.valueClass("String"));
                assert String.class.getName().equals(meta.keyClass("String"));
                Map<String, String> fields = meta.fields("String");
                assert fields != null;
                assert fields.size() == 2;
                assert String.class.getName().equals(fields.get("_KEY"));
                assert String.class.getName().equals(fields.get("_VAL"));
            } else if (DEFAULT_CACHE_NAME.equals(meta.cacheName()))
                assertTrue("Invalid types size", types.isEmpty());
            else
                fail("Unknown cache: " + meta.cacheName());
        }
    } finally {
        ((IgniteKernal) grid(0)).getCache(intCache.getName()).remove(new GridCacheInternalKeyImpl("LONG"));
    }
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) GridCacheAtomicLongValue(org.apache.ignite.internal.processors.datastructures.GridCacheAtomicLongValue) GridCacheInternalKeyImpl(org.apache.ignite.internal.processors.datastructures.GridCacheInternalKeyImpl) GridCacheSqlIndexMetadata(org.apache.ignite.internal.processors.cache.query.GridCacheSqlIndexMetadata) GridCacheSqlMetadata(org.apache.ignite.internal.processors.cache.query.GridCacheSqlMetadata) Map(java.util.Map) AffinityKey(org.apache.ignite.cache.affinity.AffinityKey) HashSet(java.util.HashSet)

Example 3 with GridCacheSqlMetadata

use of org.apache.ignite.internal.processors.cache.query.GridCacheSqlMetadata in project ignite by apache.

the class JettyRestProcessorAbstractSelfTest method testMetadata.

/**
     * @param metas Metadata for Ignite caches.
     * @throws Exception If failed.
     */
private void testMetadata(Collection<GridCacheSqlMetadata> metas, String ret) throws Exception {
    JsonNode arr = jsonResponse(ret);
    assertTrue(arr.isArray());
    assertEquals(metas.size(), arr.size());
    for (JsonNode item : arr) {
        JsonNode cacheNameNode = item.get("cacheName");
        final String cacheName = (cacheNameNode == null || "null".equals(cacheNameNode.asText())) ? null : cacheNameNode.asText();
        GridCacheSqlMetadata meta = F.find(metas, null, new P1<GridCacheSqlMetadata>() {

            @Override
            public boolean apply(GridCacheSqlMetadata meta) {
                return F.eq(meta.cacheName(), cacheName);
            }
        });
        assertNotNull("REST return metadata for unexpected cache: " + cacheName, meta);
        JsonNode types = item.get("types");
        assertNotNull(types);
        assertFalse(types.isNull());
        assertEqualsCollections(meta.types(), JSON_MAPPER.treeToValue(types, Collection.class));
        JsonNode keyClasses = item.get("keyClasses");
        assertNotNull(keyClasses);
        assertFalse(keyClasses.isNull());
        assertTrue(meta.keyClasses().equals(JSON_MAPPER.treeToValue(keyClasses, Map.class)));
        JsonNode valClasses = item.get("valClasses");
        assertNotNull(valClasses);
        assertFalse(valClasses.isNull());
        assertTrue(meta.valClasses().equals(JSON_MAPPER.treeToValue(valClasses, Map.class)));
        JsonNode fields = item.get("fields");
        assertNotNull(fields);
        assertFalse(fields.isNull());
        assertTrue(meta.fields().equals(JSON_MAPPER.treeToValue(fields, Map.class)));
        JsonNode indexesByType = item.get("indexes");
        assertNotNull(indexesByType);
        assertFalse(indexesByType.isNull());
        assertEquals(meta.indexes().size(), indexesByType.size());
        for (Map.Entry<String, Collection<GridCacheSqlIndexMetadata>> metaIndexes : meta.indexes().entrySet()) {
            JsonNode indexes = indexesByType.get(metaIndexes.getKey());
            assertNotNull(indexes);
            assertFalse(indexes.isNull());
            assertEquals(metaIndexes.getValue().size(), indexes.size());
            for (final GridCacheSqlIndexMetadata metaIdx : metaIndexes.getValue()) {
                JsonNode idx = F.find(indexes, null, new P1<JsonNode>() {

                    @Override
                    public boolean apply(JsonNode idx) {
                        return metaIdx.name().equals(idx.get("name").asText());
                    }
                });
                assertNotNull(idx);
                assertEqualsCollections(metaIdx.fields(), JSON_MAPPER.treeToValue(idx.get("fields"), Collection.class));
                assertEqualsCollections(metaIdx.descendings(), JSON_MAPPER.treeToValue(idx.get("descendings"), Collection.class));
                assertEquals(metaIdx.unique(), idx.get("unique").asBoolean());
            }
        }
    }
}
Also used : GridCacheSqlMetadata(org.apache.ignite.internal.processors.cache.query.GridCacheSqlMetadata) Collection(java.util.Collection) JsonNode(com.fasterxml.jackson.databind.JsonNode) GridCacheSqlIndexMetadata(org.apache.ignite.internal.processors.cache.query.GridCacheSqlIndexMetadata) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Aggregations

Map (java.util.Map)3 GridCacheSqlIndexMetadata (org.apache.ignite.internal.processors.cache.query.GridCacheSqlIndexMetadata)3 GridCacheSqlMetadata (org.apache.ignite.internal.processors.cache.query.GridCacheSqlMetadata)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 SQLException (java.sql.SQLException)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 UUID (java.util.UUID)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Ignite (org.apache.ignite.Ignite)1 AffinityKey (org.apache.ignite.cache.affinity.AffinityKey)1 IgniteKernal (org.apache.ignite.internal.IgniteKernal)1 GridCacheAtomicLongValue (org.apache.ignite.internal.processors.datastructures.GridCacheAtomicLongValue)1 GridCacheInternalKeyImpl (org.apache.ignite.internal.processors.datastructures.GridCacheInternalKeyImpl)1