protected DocIndexMetaData merge( DocIndexMetaData other, TransportPutIndexTemplateAction transportPutIndexTemplateAction, boolean thisIsCreatedFromTemplate) throws IOException { if (schemaEquals(other)) { return this; } else if (thisIsCreatedFromTemplate) { if (this.references.size() < other.references.size()) { // this is older, update template and return other // settings in template are always authoritative for table information about // number_of_shards and number_of_replicas updateTemplate(other, transportPutIndexTemplateAction, this.metaData.settings()); // merge the new mapping with the template settings return new DocIndexMetaData( IndexMetaData.builder(other.metaData).settings(this.metaData.settings()).build(), other.ident) .build(); } else if (references().size() == other.references().size() && !references().keySet().equals(other.references().keySet())) { XContentHelper.update(defaultMappingMap, other.defaultMappingMap, false); // update the template with new information updateTemplate(this, transportPutIndexTemplateAction, this.metaData.settings()); return this; } // other is older, just return this return this; } else { throw new TableAliasSchemaException(other.ident.name()); } }
private DocIndexMetaData buildDocIndexMetaData(String index) { DocIndexMetaData docIndexMetaData; try { docIndexMetaData = new DocIndexMetaData(functions, metaData.index(index), ident); } catch (IOException e) { throw new UnhandledServerException("Unable to build DocIndexMetaData", e); } return docIndexMetaData.build(); }
private DocIndexMetaData buildDocIndexMetaDataFromTemplate(String index, String templateName) { IndexTemplateMetaData indexTemplateMetaData = metaData.getTemplates().get(templateName); DocIndexMetaData docIndexMetaData; try { IndexMetaData.Builder builder = new IndexMetaData.Builder(index); builder.putMapping( Constants.DEFAULT_MAPPING_TYPE, indexTemplateMetaData.getMappings().get(Constants.DEFAULT_MAPPING_TYPE).toString()); Settings settings = indexTemplateMetaData.settings(); builder.settings(settings); // default values builder.numberOfShards(settings.getAsInt(SETTING_NUMBER_OF_SHARDS, 5)); builder.numberOfReplicas(settings.getAsInt(SETTING_NUMBER_OF_REPLICAS, 1)); docIndexMetaData = new DocIndexMetaData(functions, builder.build(), ident); } catch (IOException e) { throw new UnhandledServerException("Unable to build DocIndexMetaData from template", e); } return docIndexMetaData.build(); }
private DocIndexMetaData docIndexMetaData() { DocIndexMetaData docIndexMetaData; String templateName = PartitionName.templateName(ident.schema(), ident.name()); boolean createdFromTemplate = false; if (metaData.getTemplates().containsKey(templateName)) { docIndexMetaData = buildDocIndexMetaDataFromTemplate(ident.indexName(), templateName); createdFromTemplate = true; concreteIndices = metaData.concreteIndices(IndicesOptions.lenientExpandOpen(), ident.indexName()); } else { try { concreteIndices = metaData.concreteIndices(IndicesOptions.strictExpandOpen(), ident.indexName()); if (concreteIndices.length == 0) { // no matching index found throw new TableUnknownException(ident); } docIndexMetaData = buildDocIndexMetaData(concreteIndices[0]); } catch (IndexMissingException ex) { throw new TableUnknownException(ident.fqn(), ex); } } if ((!createdFromTemplate && concreteIndices.length == 1) || !checkAliasSchema) { return docIndexMetaData; } for (int i = 0; i < concreteIndices.length; i++) { try { docIndexMetaData = docIndexMetaData.merge( buildDocIndexMetaData(concreteIndices[i]), transportPutIndexTemplateAction, createdFromTemplate); } catch (IOException e) { throw new UnhandledServerException("Unable to merge/build new DocIndexMetaData", e); } } return docIndexMetaData; }
private void updateTemplate( DocIndexMetaData md, TransportPutIndexTemplateAction transportPutIndexTemplateAction, Settings updateSettings) { String templateName = PartitionName.templateName(ident.schema(), ident.name()); PutIndexTemplateRequest request = new PutIndexTemplateRequest(templateName) .mapping(Constants.DEFAULT_MAPPING_TYPE, md.defaultMappingMap) .create(false) .settings(updateSettings) .template(templateName + "*"); for (String alias : md.aliases()) { request = request.alias(new Alias(alias)); } transportPutIndexTemplateAction.execute(request); }
public DocTableInfo build() { DocIndexMetaData md = docIndexMetaData(); List<PartitionName> partitions = new ArrayList<>(); if (md.partitionedBy().size() > 0) { for (String index : concreteIndices) { if (PartitionName.isPartition(index)) { try { PartitionName partitionName = PartitionName.fromIndexOrTemplate(index); assert partitionName.tableIdent().equals(ident); partitions.add(partitionName); } catch (IllegalArgumentException e) { // ignore logger.warn( String.format( Locale.ENGLISH, "Cannot build partition %s of index %s", index, ident.indexName())); } } } } return new DocTableInfo( ident, md.columns(), md.partitionedByColumns(), md.generatedColumnReferences(), md.indices(), md.references(), md.analyzers(), md.primaryKey(), md.routingCol(), md.isAlias(), md.hasAutoGeneratedPrimaryKey(), concreteIndices, clusterService, md.numberOfShards(), md.numberOfReplicas(), md.tableParameters(), md.partitionedBy(), partitions, md.columnPolicy(), executorService); }