forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTemplatesMetadata.java
162 lines (134 loc) · 5.21 KB
/
TemplatesMetadata.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
package org.opensearch.cluster.metadata;
import org.opensearch.cluster.AbstractDiffable;
import org.opensearch.common.annotation.PublicApi;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.core.common.io.stream.VerifiableWriteable;
import org.opensearch.core.xcontent.ToXContentFragment;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.index.translog.BufferedChecksumStreamOutput;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
/**
* Metadata for legacy templates
*
* @opensearch.api
*/
@PublicApi(since = "2.15.0")
public class TemplatesMetadata extends AbstractDiffable<TemplatesMetadata> implements ToXContentFragment, VerifiableWriteable {
public static TemplatesMetadata EMPTY_METADATA = builder().build();
private final Map<String, IndexTemplateMetadata> templates;
public TemplatesMetadata() {
this(Collections.emptyMap());
}
public TemplatesMetadata(Map<String, IndexTemplateMetadata> templates) {
this.templates = Collections.unmodifiableMap(templates);
}
public static Builder builder() {
return new Builder();
}
public Map<String, IndexTemplateMetadata> getTemplates() {
return this.templates;
}
public static TemplatesMetadata fromXContent(XContentParser parser) throws IOException {
return Builder.fromXContent(parser);
}
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
Builder.toXContent(this, builder, params);
return builder;
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeVInt(templates.size());
for (final IndexTemplateMetadata cursor : templates.values()) {
cursor.writeTo(out);
}
}
@Override
public void writeVerifiableTo(StreamOutput out) throws IOException {
((BufferedChecksumStreamOutput) out).writeMapValues(templates, (stream, value) -> value.writeVerifiableTo(stream));
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TemplatesMetadata that = (TemplatesMetadata) o;
return Objects.equals(templates, that.templates);
}
@Override
public int hashCode() {
return templates != null ? templates.hashCode() : 0;
}
@Override
public String toString() {
return "TemplatesMetadata{" + "templates=" + templates + '}';
}
/**
* Builder for the templates metadata
*
* @opensearch.api
*/
@PublicApi(since = "2.15.0")
public static class Builder {
private final Map<String, IndexTemplateMetadata> templates;
public Builder() {
this.templates = new HashMap<String, IndexTemplateMetadata>();
}
public Builder(Map<String, IndexTemplateMetadata> templates) {
this.templates = templates;
}
public Builder put(IndexTemplateMetadata.Builder templateBuilder) {
return put(templateBuilder.build());
}
public Builder put(IndexTemplateMetadata template) {
templates.put(template.name(), template);
return this;
}
public Builder removeTemplate(String templateName) {
templates.remove(templateName);
return this;
}
public Builder templates(Map<String, IndexTemplateMetadata> templates) {
this.templates.putAll(templates);
return this;
}
public TemplatesMetadata build() {
return new TemplatesMetadata(templates);
}
public static void toXContent(TemplatesMetadata templatesMetadata, XContentBuilder builder, Params params) throws IOException {
for (IndexTemplateMetadata cursor : templatesMetadata.getTemplates().values()) {
IndexTemplateMetadata.Builder.toXContentWithTypes(cursor, builder, params);
}
}
public static TemplatesMetadata fromXContent(XContentParser parser) throws IOException {
Builder builder = new Builder();
XContentParser.Token token = parser.currentToken();
String currentFieldName = parser.currentName();
if (currentFieldName == null) {
token = parser.nextToken();
if (token == XContentParser.Token.START_OBJECT) {
// move to the field name
token = parser.nextToken();
}
currentFieldName = parser.currentName();
}
if (currentFieldName != null) {
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
builder.put(IndexTemplateMetadata.Builder.fromXContent(parser, parser.currentName()));
}
}
return builder.build();
}
}
}