Search in sources :

Example 1 with IndexProvider

use of org.janusgraph.diskstorage.indexing.IndexProvider in project janusgraph by JanusGraph.

the class IndexSerializer method createQueryString.

/* ################################################
    	Common code used by executeQuery and executeTotals
	################################################### */
private String createQueryString(IndexQueryBuilder query, final ElementCategory resultType, final StandardJanusGraphTx transaction, MixedIndexType index) {
    Preconditions.checkArgument(index.getElement() == resultType, "Index is not configured for the desired result type: %s", resultType);
    final String backingIndexName = index.getBackingIndexName();
    final IndexProvider indexInformation = (IndexProvider) mixedIndexes.get(backingIndexName);
    final StringBuilder qB = new StringBuilder(query.getQuery());
    final String prefix = query.getPrefix();
    Preconditions.checkNotNull(prefix);
    // Convert query string by replacing
    int replacements = 0;
    int pos = 0;
    while (pos < qB.length()) {
        pos = qB.indexOf(prefix, pos);
        if (pos < 0)
            break;
        final int startPos = pos;
        pos += prefix.length();
        final StringBuilder keyBuilder = new StringBuilder();
        final boolean quoteTerminated = qB.charAt(pos) == '"';
        if (quoteTerminated)
            pos++;
        while (pos < qB.length() && (Character.isLetterOrDigit(qB.charAt(pos)) || (quoteTerminated && qB.charAt(pos) != '"') || qB.charAt(pos) == '*')) {
            keyBuilder.append(qB.charAt(pos));
            pos++;
        }
        if (quoteTerminated)
            pos++;
        final int endPos = pos;
        final String keyName = keyBuilder.toString();
        Preconditions.checkArgument(StringUtils.isNotBlank(keyName), "Found reference to empty key at position [%s]", startPos);
        String replacement;
        if (keyName.equals("*")) {
            replacement = indexInformation.getFeatures().getWildcardField();
        } else if (transaction.containsRelationType(keyName)) {
            final PropertyKey key = transaction.getPropertyKey(keyName);
            Preconditions.checkNotNull(key);
            Preconditions.checkArgument(index.indexesKey(key), "The used key [%s] is not indexed in the targeted index [%s]", key.name(), query.getIndex());
            replacement = key2Field(index, key);
        } else {
            Preconditions.checkArgument(query.getUnknownKeyName() != null, "Found reference to nonexistent property key in query at position [%s]: %s", startPos, keyName);
            replacement = query.getUnknownKeyName();
        }
        Preconditions.checkArgument(StringUtils.isNotBlank(replacement));
        qB.replace(startPos, endPos, replacement);
        pos = startPos + replacement.length();
        replacements++;
    }
    final String queryStr = qB.toString();
    if (replacements <= 0)
        log.warn("Could not convert given {} index query: [{}]", resultType, query.getQuery());
    log.info("Converted query string with {} replacements: [{}] => [{}]", replacements, query.getQuery(), queryStr);
    return queryStr;
}
Also used : IndexProvider(org.janusgraph.diskstorage.indexing.IndexProvider) PropertyKey(org.janusgraph.core.PropertyKey)

Example 2 with IndexProvider

use of org.janusgraph.diskstorage.indexing.IndexProvider in project janusgraph by JanusGraph.

the class ElasticsearchConfigTest method testClientThrowsExceptionIfServerNotReachable.

@Test
public void testClientThrowsExceptionIfServerNotReachable() throws BackendException, InterruptedException {
    final ModifiableConfiguration config = esr.setConfiguration(GraphDatabaseConfiguration.buildGraphConfiguration(), INDEX_NAME);
    Configuration indexConfig = config.restrictTo(INDEX_NAME);
    IndexProvider idx = open(indexConfig);
    simpleWriteAndQuery(idx);
    idx.close();
    config.set(INDEX_HOSTS, new String[] { "localhost:" + esr.getPort() + 1 }, INDEX_NAME);
    final Configuration wrongHostConfig = config.restrictTo(INDEX_NAME);
    assertThrows(Exception.class, () -> new ElasticSearchIndex(wrongHostConfig));
}
Also used : IndexProvider(org.janusgraph.diskstorage.indexing.IndexProvider) BasicConfiguration(org.janusgraph.diskstorage.configuration.BasicConfiguration) Configuration(org.janusgraph.diskstorage.configuration.Configuration) CommonsConfiguration(org.janusgraph.diskstorage.configuration.backend.CommonsConfiguration) GraphDatabaseConfiguration(org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration) ModifiableConfiguration(org.janusgraph.diskstorage.configuration.ModifiableConfiguration) ModifiableConfiguration(org.janusgraph.diskstorage.configuration.ModifiableConfiguration) Test(org.junit.jupiter.api.Test) IndexProviderTest(org.janusgraph.diskstorage.indexing.IndexProviderTest) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with IndexProvider

use of org.janusgraph.diskstorage.indexing.IndexProvider in project janusgraph by JanusGraph.

the class ElasticsearchConfigTest method testExternalDynamic.

private void testExternalDynamic(boolean withUpdateMapping, boolean useMappingsForES7) throws Exception {
    final Duration maxWrite = Duration.ofMillis(2000L);
    final String storeName = "test_mapping";
    final Configuration indexConfig = buildIndexConfigurationForExternalDynamic(withUpdateMapping, useMappingsForES7);
    final IndexProvider idx = open(indexConfig);
    // Test that the "date" property throws an exception.
    final KeyInformation.IndexRetriever indexRetriever = IndexProviderTest.getIndexRetriever(IndexProviderTest.getMapping(idx.getFeatures(), ANALYZER_ENGLISH, ANALYZER_KEYWORD));
    final BaseTransactionConfig txConfig = StandardBaseTransactionConfig.of(TimestampProviders.MILLI);
    final IndexTransaction itx = new IndexTransaction(idx, indexRetriever, txConfig, maxWrite);
    try {
        idx.register(storeName, "date", IndexProviderTest.getMapping(idx.getFeatures(), ANALYZER_ENGLISH, ANALYZER_KEYWORD).get("date"), itx);
        fail("should fail");
    } catch (final PermanentBackendException e) {
        log.debug(e.getMessage(), e);
    }
    if (isMappingUsed(idx)) {
        executeRequestWithStringEntity(idx, "janusgraph_" + storeName, readTypesMapping("/dynamic_mapping.json"));
    } else {
        executeRequestWithStringEntity(idx, "janusgraph_" + storeName, readTypelessMapping("/typeless_dynamic_mapping.json"));
    }
    // Test that the "date" property works well.
    idx.register(storeName, "date", IndexProviderTest.getMapping(idx.getFeatures(), ANALYZER_ENGLISH, ANALYZER_KEYWORD).get("date"), itx);
    // Test that the "weight" property works well due to dynamic mapping.
    idx.register(storeName, "weight", IndexProviderTest.getMapping(idx.getFeatures(), ANALYZER_ENGLISH, ANALYZER_KEYWORD).get("weight"), itx);
    itx.rollback();
    idx.close();
    final ElasticSearchClient client = ElasticSearchSetup.REST_CLIENT.connect(indexConfig).getClient();
    final Map<String, Object> properties = client.getMapping("janusgraph_" + storeName, storeName).getProperties();
    assertEquals(withUpdateMapping, properties.containsKey("weight"), properties.toString());
}
Also used : IndexProvider(org.janusgraph.diskstorage.indexing.IndexProvider) BasicConfiguration(org.janusgraph.diskstorage.configuration.BasicConfiguration) Configuration(org.janusgraph.diskstorage.configuration.Configuration) CommonsConfiguration(org.janusgraph.diskstorage.configuration.backend.CommonsConfiguration) GraphDatabaseConfiguration(org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration) ModifiableConfiguration(org.janusgraph.diskstorage.configuration.ModifiableConfiguration) PermanentBackendException(org.janusgraph.diskstorage.PermanentBackendException) RestElasticSearchClient(org.janusgraph.diskstorage.es.rest.RestElasticSearchClient) IndexTransaction(org.janusgraph.diskstorage.indexing.IndexTransaction) Duration(java.time.Duration) StandardBaseTransactionConfig(org.janusgraph.diskstorage.util.StandardBaseTransactionConfig) BaseTransactionConfig(org.janusgraph.diskstorage.BaseTransactionConfig) KeyInformation(org.janusgraph.diskstorage.indexing.KeyInformation)

Example 4 with IndexProvider

use of org.janusgraph.diskstorage.indexing.IndexProvider in project janusgraph by JanusGraph.

the class ElasticsearchConfigTest method testExternalMappingsViaMapping.

@ParameterizedTest
@MethodSource("useMappingsForES7Configuration")
public void testExternalMappingsViaMapping(Boolean useMappingsForES7) throws Exception {
    final Duration maxWrite = Duration.ofMillis(2000L);
    final String storeName = "test_mapping";
    final ModifiableConfiguration modifiableConfiguration = esr.setConfiguration(GraphDatabaseConfiguration.buildGraphConfiguration(), INDEX_NAME).set(USE_EXTERNAL_MAPPINGS, true, INDEX_NAME);
    if (useMappingsForES7) {
        modifiableConfiguration.set(USE_MAPPING_FOR_ES7, true, INDEX_NAME);
    }
    final Configuration indexConfig = modifiableConfiguration.restrictTo(INDEX_NAME);
    final IndexProvider idx = open(indexConfig);
    // Test that the "date" property throws an exception.
    final KeyInformation.IndexRetriever indexRetriever = IndexProviderTest.getIndexRetriever(IndexProviderTest.getMapping(idx.getFeatures(), ANALYZER_ENGLISH, ANALYZER_KEYWORD));
    final BaseTransactionConfig txConfig = StandardBaseTransactionConfig.of(TimestampProviders.MILLI);
    final IndexTransaction itx = new IndexTransaction(idx, indexRetriever, txConfig, maxWrite);
    try {
        idx.register(storeName, "date", IndexProviderTest.getMapping(idx.getFeatures(), ANALYZER_ENGLISH, ANALYZER_KEYWORD).get("date"), itx);
        fail("should fail");
    } catch (final PermanentBackendException e) {
        log.debug(e.getMessage(), e);
    }
    if (isMappingUsed(idx)) {
        executeRequestWithStringEntity(idx, "janusgraph_" + storeName, readTypesMapping("/strict_mapping.json"));
    } else {
        executeRequestWithStringEntity(idx, "janusgraph_" + storeName, readTypelessMapping("/typeless_strict_mapping.json"));
    }
    // Test that the "date" property works well.
    idx.register(storeName, "date", IndexProviderTest.getMapping(idx.getFeatures(), ANALYZER_ENGLISH, ANALYZER_KEYWORD).get("date"), itx);
    // Test that the "weight" property throws an exception.
    try {
        idx.register(storeName, "weight", IndexProviderTest.getMapping(idx.getFeatures(), ANALYZER_ENGLISH, ANALYZER_KEYWORD).get("weight"), itx);
        fail("should fail");
    } catch (final BackendException e) {
        log.debug(e.getMessage(), e);
    }
    itx.rollback();
    idx.close();
}
Also used : IndexProvider(org.janusgraph.diskstorage.indexing.IndexProvider) BasicConfiguration(org.janusgraph.diskstorage.configuration.BasicConfiguration) Configuration(org.janusgraph.diskstorage.configuration.Configuration) CommonsConfiguration(org.janusgraph.diskstorage.configuration.backend.CommonsConfiguration) GraphDatabaseConfiguration(org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration) ModifiableConfiguration(org.janusgraph.diskstorage.configuration.ModifiableConfiguration) PermanentBackendException(org.janusgraph.diskstorage.PermanentBackendException) IndexTransaction(org.janusgraph.diskstorage.indexing.IndexTransaction) Duration(java.time.Duration) ModifiableConfiguration(org.janusgraph.diskstorage.configuration.ModifiableConfiguration) StandardBaseTransactionConfig(org.janusgraph.diskstorage.util.StandardBaseTransactionConfig) BaseTransactionConfig(org.janusgraph.diskstorage.BaseTransactionConfig) KeyInformation(org.janusgraph.diskstorage.indexing.KeyInformation) BackendException(org.janusgraph.diskstorage.BackendException) PermanentBackendException(org.janusgraph.diskstorage.PermanentBackendException) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 5 with IndexProvider

use of org.janusgraph.diskstorage.indexing.IndexProvider in project janusgraph by JanusGraph.

the class Backend method beginTransaction.

/**
 * Opens a new transaction against all registered backend system wrapped in one {@link BackendTransaction}.
 *
 * @return
 * @throws BackendException
 */
public BackendTransaction beginTransaction(TransactionConfiguration configuration, KeyInformation.Retriever indexKeyRetriever) throws BackendException {
    StoreTransaction tx = storeManagerLocking.beginTransaction(configuration);
    // Cache
    CacheTransaction cacheTx = new CacheTransaction(tx, storeManagerLocking, bufferSize, maxWriteTime, configuration.hasEnabledBatchLoading());
    // Index transactions
    final Map<String, IndexTransaction> indexTx = new HashMap<>(indexes.size());
    for (Map.Entry<String, IndexProvider> entry : indexes.entrySet()) {
        indexTx.put(entry.getKey(), new IndexTransaction(entry.getValue(), indexKeyRetriever.get(entry.getKey()), configuration, maxWriteTime));
    }
    return new BackendTransaction(cacheTx, configuration, storeFeatures, edgeStore, indexStore, txLogStore, maxReadTime, indexTx, threadPool);
}
Also used : MetricInstrumentedIndexProvider(org.janusgraph.diskstorage.util.MetricInstrumentedIndexProvider) IndexProvider(org.janusgraph.diskstorage.indexing.IndexProvider) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) StoreTransaction(org.janusgraph.diskstorage.keycolumnvalue.StoreTransaction) IndexTransaction(org.janusgraph.diskstorage.indexing.IndexTransaction) CacheTransaction(org.janusgraph.diskstorage.keycolumnvalue.cache.CacheTransaction) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Aggregations

IndexProvider (org.janusgraph.diskstorage.indexing.IndexProvider)10 BasicConfiguration (org.janusgraph.diskstorage.configuration.BasicConfiguration)5 Configuration (org.janusgraph.diskstorage.configuration.Configuration)5 ModifiableConfiguration (org.janusgraph.diskstorage.configuration.ModifiableConfiguration)5 CommonsConfiguration (org.janusgraph.diskstorage.configuration.backend.CommonsConfiguration)5 GraphDatabaseConfiguration (org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration)5 IndexTransaction (org.janusgraph.diskstorage.indexing.IndexTransaction)4 MetricInstrumentedIndexProvider (org.janusgraph.diskstorage.util.MetricInstrumentedIndexProvider)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 Duration (java.time.Duration)3 HashMap (java.util.HashMap)3 BaseTransactionConfig (org.janusgraph.diskstorage.BaseTransactionConfig)3 PermanentBackendException (org.janusgraph.diskstorage.PermanentBackendException)3 KeyInformation (org.janusgraph.diskstorage.indexing.KeyInformation)3 StandardBaseTransactionConfig (org.janusgraph.diskstorage.util.StandardBaseTransactionConfig)3 MethodSource (org.junit.jupiter.params.provider.MethodSource)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 BackendException (org.janusgraph.diskstorage.BackendException)2 RestElasticSearchClient (org.janusgraph.diskstorage.es.rest.RestElasticSearchClient)2 Map (java.util.Map)1