Search in sources :

Example 6 with VisibleForTesting

use of io.crate.common.annotations.VisibleForTesting in project crate by crate.

the class TransportShardUpsertAction method insert.

@VisibleForTesting
protected IndexItemResponse insert(ShardUpsertRequest request, ShardUpsertRequest.Item item, IndexShard indexShard, boolean isRetry, @Nullable ReturnValueGen returnGen, InsertSourceGen insertSourceGen) throws Exception {
    assert insertSourceGen != null : "InsertSourceGen must not be null";
    BytesReference rawSource;
    Map<String, Object> source = null;
    try {
        // the rawSource
        if (insertSourceGen instanceof FromRawInsertSource) {
            rawSource = insertSourceGen.generateSourceAndCheckConstraintsAsBytesReference(item.insertValues());
        } else {
            source = insertSourceGen.generateSourceAndCheckConstraints(item.insertValues());
            rawSource = BytesReference.bytes(XContentFactory.jsonBuilder().map(source, SOURCE_WRITERS));
        }
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
    item.source(rawSource);
    long version = request.duplicateKeyAction() == DuplicateKeyAction.OVERWRITE ? Versions.MATCH_ANY : Versions.MATCH_DELETED;
    long seqNo = SequenceNumbers.UNASSIGNED_SEQ_NO;
    long primaryTerm = SequenceNumbers.UNASSIGNED_PRIMARY_TERM;
    Engine.IndexResult indexResult = index(item, indexShard, isRetry, seqNo, primaryTerm, version);
    Object[] returnvalues = null;
    if (returnGen != null) {
        // when return values are requested
        if (source == null) {
            source = JsonXContent.JSON_XCONTENT.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, BytesReference.toBytes(rawSource)).map();
        }
        returnvalues = returnGen.generateReturnValues(// we want to avoid. The docId is anyway just valid with the lifetime of a searcher and can change afterwards.
        new Doc(-1, indexShard.shardId().getIndexName(), item.id(), indexResult.getVersion(), indexResult.getSeqNo(), indexResult.getTerm(), source, rawSource::utf8ToString));
    }
    return new IndexItemResponse(indexResult.getTranslogLocation(), returnvalues);
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) UncheckedIOException(java.io.UncheckedIOException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) Doc(io.crate.expression.reference.Doc) Engine(org.elasticsearch.index.engine.Engine) VisibleForTesting(io.crate.common.annotations.VisibleForTesting)

Example 7 with VisibleForTesting

use of io.crate.common.annotations.VisibleForTesting in project crate by crate.

the class TransportCreateUserAction method putUser.

/**
 * Puts a user into the meta data and creates an empty privileges set.
 *
 * @return boolean true if the user already exists, otherwise false
 */
@VisibleForTesting
static boolean putUser(Metadata.Builder mdBuilder, String name, @Nullable SecureHash secureHash) {
    UsersMetadata oldMetadata = (UsersMetadata) mdBuilder.getCustom(UsersMetadata.TYPE);
    if (oldMetadata != null && oldMetadata.contains(name)) {
        return true;
    }
    // create a new instance of the metadata, to guarantee the cluster changed action.
    UsersMetadata newMetadata = UsersMetadata.newInstance(oldMetadata);
    newMetadata.put(name, secureHash);
    assert !newMetadata.equals(oldMetadata) : "must not be equal to guarantee the cluster change action";
    mdBuilder.putCustom(UsersMetadata.TYPE, newMetadata);
    // create empty privileges for this user
    UsersPrivilegesMetadata privilegesMetadata = UsersPrivilegesMetadata.copyOf((UsersPrivilegesMetadata) mdBuilder.getCustom(UsersPrivilegesMetadata.TYPE));
    privilegesMetadata.createPrivileges(name, Collections.emptySet());
    mdBuilder.putCustom(UsersPrivilegesMetadata.TYPE, privilegesMetadata);
    return false;
}
Also used : UsersPrivilegesMetadata(io.crate.user.metadata.UsersPrivilegesMetadata) UsersMetadata(io.crate.user.metadata.UsersMetadata) VisibleForTesting(io.crate.common.annotations.VisibleForTesting)

Example 8 with VisibleForTesting

use of io.crate.common.annotations.VisibleForTesting in project crate by crate.

the class UserActions method getUserPasswordProperty.

@VisibleForTesting
@Nullable
static SecureString getUserPasswordProperty(GenericProperties<Symbol> userStmtProperties, Row parameters, TransactionContext txnCtx, NodeContext nodeCtx) throws IllegalArgumentException {
    Function<? super Symbol, Object> eval = x -> SymbolEvaluator.evaluate(txnCtx, nodeCtx, x, parameters, SubQueryResults.EMPTY);
    Map<String, Object> properties = userStmtProperties.map(eval).properties();
    final String PASSWORD_PROPERTY = "password";
    for (String key : properties.keySet()) {
        if (PASSWORD_PROPERTY.equals(key)) {
            String value = DataTypes.STRING.sanitizeValue(properties.get(key));
            if (value != null) {
                return new SecureString(value.toCharArray());
            }
            // Password will be reset
            return null;
        } else {
            throw new IllegalArgumentException(String.format(Locale.ENGLISH, "\"%s\" is not a valid user property", key));
        }
    }
    return null;
}
Also used : TransactionContext(io.crate.metadata.TransactionContext) NodeContext(io.crate.metadata.NodeContext) GenericProperties(io.crate.sql.tree.GenericProperties) Function(java.util.function.Function) SymbolEvaluator(io.crate.analyze.SymbolEvaluator) GeneralSecurityException(java.security.GeneralSecurityException) Row(io.crate.data.Row) Symbol(io.crate.expression.symbol.Symbol) DataTypes(io.crate.types.DataTypes) Locale(java.util.Locale) Map(java.util.Map) SubQueryResults(io.crate.planner.operators.SubQueryResults) SecureString(org.elasticsearch.common.settings.SecureString) VisibleForTesting(io.crate.common.annotations.VisibleForTesting) Nullable(javax.annotation.Nullable) SecureString(org.elasticsearch.common.settings.SecureString) SecureString(org.elasticsearch.common.settings.SecureString) VisibleForTesting(io.crate.common.annotations.VisibleForTesting) Nullable(javax.annotation.Nullable)

Example 9 with VisibleForTesting

use of io.crate.common.annotations.VisibleForTesting in project crate by crate.

the class TransportDropUserAction method dropUser.

@VisibleForTesting
static boolean dropUser(Metadata.Builder mdBuilder, @Nullable UsersMetadata oldMetadata, String name) {
    if (oldMetadata == null || oldMetadata.contains(name) == false) {
        return false;
    }
    // create a new instance of the metadata, to guarantee the cluster changed action.
    UsersMetadata newMetadata = UsersMetadata.newInstance(oldMetadata);
    newMetadata.remove(name);
    assert !newMetadata.equals(oldMetadata) : "must not be equal to guarantee the cluster change action";
    mdBuilder.putCustom(UsersMetadata.TYPE, newMetadata);
    // removes all privileges for this user
    UsersPrivilegesMetadata privilegesMetadata = UsersPrivilegesMetadata.copyOf((UsersPrivilegesMetadata) mdBuilder.getCustom(UsersPrivilegesMetadata.TYPE));
    privilegesMetadata.dropPrivileges(name);
    mdBuilder.putCustom(UsersPrivilegesMetadata.TYPE, privilegesMetadata);
    return true;
}
Also used : UsersPrivilegesMetadata(io.crate.user.metadata.UsersPrivilegesMetadata) UsersMetadata(io.crate.user.metadata.UsersMetadata) VisibleForTesting(io.crate.common.annotations.VisibleForTesting)

Example 10 with VisibleForTesting

use of io.crate.common.annotations.VisibleForTesting in project crate by crate.

the class TransportSchemaUpdateAction method updateTemplate.

@VisibleForTesting
static ClusterState updateTemplate(NamedXContentRegistry xContentRegistry, ClusterState currentState, String templateName, Map<String, Object> newMapping) throws Exception {
    IndexTemplateMetadata template = currentState.metadata().templates().get(templateName);
    if (template == null) {
        throw new ResourceNotFoundException("Template \"" + templateName + "\" for partitioned table is missing");
    }
    IndexTemplateMetadata.Builder templateBuilder = new IndexTemplateMetadata.Builder(template);
    for (ObjectObjectCursor<String, CompressedXContent> cursor : template.mappings()) {
        Map<String, Object> source = parseMapping(xContentRegistry, cursor.value.toString());
        mergeIntoSource(source, newMapping);
        try (XContentBuilder xContentBuilder = JsonXContent.contentBuilder()) {
            templateBuilder.putMapping(cursor.key, Strings.toString(xContentBuilder.map(source)));
        }
    }
    Metadata.Builder builder = Metadata.builder(currentState.metadata()).put(templateBuilder);
    return ClusterState.builder(currentState).metadata(builder).build();
}
Also used : IndexTemplateMetadata(org.elasticsearch.cluster.metadata.IndexTemplateMetadata) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Metadata(org.elasticsearch.cluster.metadata.Metadata) IndexTemplateMetadata(org.elasticsearch.cluster.metadata.IndexTemplateMetadata) ResourceNotFoundException(org.elasticsearch.ResourceNotFoundException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) VisibleForTesting(io.crate.common.annotations.VisibleForTesting)

Aggregations

VisibleForTesting (io.crate.common.annotations.VisibleForTesting)36 Row (io.crate.data.Row)12 Symbol (io.crate.expression.symbol.Symbol)12 SubQueryResults (io.crate.planner.operators.SubQueryResults)11 Settings (org.elasticsearch.common.settings.Settings)11 RowConsumer (io.crate.data.RowConsumer)10 NodeContext (io.crate.metadata.NodeContext)10 DependencyCarrier (io.crate.planner.DependencyCarrier)10 Plan (io.crate.planner.Plan)10 PlannerContext (io.crate.planner.PlannerContext)10 Function (java.util.function.Function)10 SymbolEvaluator (io.crate.analyze.SymbolEvaluator)9 Row1 (io.crate.data.Row1)9 CoordinatorTxnCtx (io.crate.metadata.CoordinatorTxnCtx)9 ArrayList (java.util.ArrayList)8 OneRowActionListener (io.crate.execution.support.OneRowActionListener)7 Map (java.util.Map)7 ColumnIdent (io.crate.metadata.ColumnIdent)6 DocTableInfo (io.crate.metadata.doc.DocTableInfo)5 Nullable (javax.annotation.Nullable)5