|
| 1 | +/* |
| 2 | + * Licensed to Elastic Search and Shay Banon under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. Elastic Search licenses this |
| 6 | + * file to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.index.aliases; |
| 21 | + |
| 22 | +import org.apache.lucene.search.*; |
| 23 | +import org.elasticsearch.common.Nullable; |
| 24 | +import org.elasticsearch.common.collect.ImmutableMap; |
| 25 | +import org.elasticsearch.common.collect.UnmodifiableIterator; |
| 26 | +import org.elasticsearch.common.compress.CompressedString; |
| 27 | +import org.elasticsearch.common.inject.Inject; |
| 28 | +import org.elasticsearch.common.lucene.search.XBooleanFilter; |
| 29 | +import org.elasticsearch.common.settings.Settings; |
| 30 | +import org.elasticsearch.common.xcontent.XContentFactory; |
| 31 | +import org.elasticsearch.common.xcontent.XContentParser; |
| 32 | +import org.elasticsearch.index.AbstractIndexComponent; |
| 33 | +import org.elasticsearch.index.Index; |
| 34 | +import org.elasticsearch.index.query.IndexQueryParserService; |
| 35 | +import org.elasticsearch.index.query.xcontent.XContentIndexQueryParser; |
| 36 | +import org.elasticsearch.index.settings.IndexSettings; |
| 37 | +import org.elasticsearch.indices.AliasFilterParsingException; |
| 38 | + |
| 39 | +import java.io.IOException; |
| 40 | +import java.util.List; |
| 41 | + |
| 42 | +import static org.elasticsearch.common.collect.Lists.newArrayList; |
| 43 | +import static org.elasticsearch.common.collect.MapBuilder.newMapBuilder; |
| 44 | + |
| 45 | +/** |
| 46 | + * @author imotov |
| 47 | + */ |
| 48 | +public class IndexAliasesService extends AbstractIndexComponent implements Iterable<IndexAlias> { |
| 49 | + |
| 50 | + private final IndexQueryParserService indexQueryParserService; |
| 51 | + |
| 52 | + private volatile ImmutableMap<String, IndexAlias> aliases = ImmutableMap.of(); |
| 53 | + |
| 54 | + private final Object mutex = new Object(); |
| 55 | + |
| 56 | + @Inject public IndexAliasesService(Index index, @IndexSettings Settings indexSettings, IndexQueryParserService indexQueryParserService) { |
| 57 | + super(index, indexSettings); |
| 58 | + this.indexQueryParserService = indexQueryParserService; |
| 59 | + } |
| 60 | + |
| 61 | + public boolean hasAlias(String alias) { |
| 62 | + return aliases.containsKey(alias); |
| 63 | + } |
| 64 | + |
| 65 | + public IndexAlias alias(String alias) { |
| 66 | + return aliases.get(alias); |
| 67 | + } |
| 68 | + |
| 69 | + public void add(String alias, @Nullable CompressedString filter) { |
| 70 | + add(new IndexAlias(alias, filter, parse(alias, filter))); |
| 71 | + } |
| 72 | + |
| 73 | + public Filter aliasFilter(String... indices) { |
| 74 | + List<Filter> filters = null; |
| 75 | + for (String alias : indices) { |
| 76 | + // The list contains the index itself - no filtering needed |
| 77 | + if (alias.equals(index.name())) { |
| 78 | + return null; |
| 79 | + } |
| 80 | + IndexAlias indexAlias = aliases.get(alias); |
| 81 | + if (indexAlias != null) { |
| 82 | + // The list contains a non-filtering alias - no filtering needed |
| 83 | + if (indexAlias.parsedFilter() == null) { |
| 84 | + return null; |
| 85 | + } else { |
| 86 | + if (filters == null) { |
| 87 | + filters = newArrayList(); |
| 88 | + } |
| 89 | + filters.add(indexAlias.parsedFilter()); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + if (filters == null) { |
| 94 | + return null; |
| 95 | + } |
| 96 | + if (filters.size() == 1) { |
| 97 | + return filters.get(0); |
| 98 | + } else { |
| 99 | + XBooleanFilter combined = new XBooleanFilter(); |
| 100 | + for (Filter filter : filters) { |
| 101 | + combined.add(new FilterClause(filter, BooleanClause.Occur.SHOULD)); |
| 102 | + } |
| 103 | + return combined; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private void add(IndexAlias indexAlias) { |
| 108 | + synchronized (mutex) { |
| 109 | + aliases = newMapBuilder(aliases).put(indexAlias.alias(), indexAlias).immutableMap(); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + public void remove(String alias) { |
| 114 | + synchronized (mutex) { |
| 115 | + aliases = newMapBuilder(aliases).remove(alias).immutableMap(); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private Filter parse(String alias, CompressedString filter) { |
| 120 | + if (filter == null) { |
| 121 | + return null; |
| 122 | + } |
| 123 | + XContentIndexQueryParser indexQueryParser = (XContentIndexQueryParser) indexQueryParserService.defaultIndexQueryParser(); |
| 124 | + try { |
| 125 | + byte[] filterSource = filter.uncompressed(); |
| 126 | + XContentParser parser = XContentFactory.xContent(filterSource).createParser(filterSource); |
| 127 | + try { |
| 128 | + return indexQueryParser.parseInnerFilter(parser); |
| 129 | + } finally { |
| 130 | + parser.close(); |
| 131 | + } |
| 132 | + } catch (IOException ex) { |
| 133 | + throw new AliasFilterParsingException(index, alias, "Invalid alias filter", ex); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + @Override public UnmodifiableIterator<IndexAlias> iterator() { |
| 138 | + return aliases.values().iterator(); |
| 139 | + } |
| 140 | +} |
0 commit comments