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);
}
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;
}
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;
}
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;
}
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();
}
Aggregations