• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

apache / iotdb / #9999

05 Sep 2023 08:10AM CUT coverage: 47.669% (-0.03%) from 47.697%
#9999

push

travis_ci

web-flow
[IOTDB-6130] Delete data by specific pattern didn't work

80151 of 168139 relevant lines covered (47.67%)

0.48 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.18
/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/path/PathPatternNode.java
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one
3
 * or more contributor license agreements.  See the NOTICE file
4
 * distributed with this work for additional information
5
 * regarding copyright ownership.  The ASF licenses this file
6
 * 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.apache.iotdb.commons.path;
21

22
import org.apache.iotdb.tsfile.utils.PublicBAOS;
23
import org.apache.iotdb.tsfile.utils.ReadWriteIOUtils;
24

25
import java.io.DataOutputStream;
26
import java.io.IOException;
27
import java.nio.ByteBuffer;
28
import java.util.ArrayList;
29
import java.util.Collection;
30
import java.util.HashMap;
31
import java.util.HashSet;
32
import java.util.List;
33
import java.util.Map;
34
import java.util.Objects;
35
import java.util.Set;
36
import java.util.function.BiConsumer;
37
import java.util.function.Supplier;
38

39
import static org.apache.iotdb.commons.conf.IoTDBConstant.MULTI_LEVEL_PATH_WILDCARD;
40
import static org.apache.iotdb.commons.conf.IoTDBConstant.ONE_LEVEL_PATH_WILDCARD;
41

42
public class PathPatternNode<V, VSerializer extends PathPatternNode.Serializer<V>> {
43

44
  private final String name;
45
  private final Map<String, PathPatternNode<V, VSerializer>> children;
46
  private Set<V> valueSet;
47
  /**
48
   * Used only in PatternTreeMap to identify whether from root to the current node is a registered
49
   * path pattern. In PatternTreeMap, it can be replaced by the size of the valueSet
50
   */
51
  private boolean mark = false;
1✔
52

53
  private final VSerializer serializer;
54

55
  public PathPatternNode(String name, VSerializer serializer) {
1✔
56
    this.name = name;
1✔
57
    this.children = new HashMap<>();
1✔
58
    this.serializer = serializer;
1✔
59
  }
1✔
60

61
  public PathPatternNode(String name, Supplier<? extends Set<V>> supplier, VSerializer serialize) {
1✔
62
    this.name = name;
1✔
63
    this.children = new HashMap<>();
1✔
64
    this.valueSet = supplier.get();
1✔
65
    this.serializer = serialize;
1✔
66
  }
1✔
67

68
  public String getName() {
69
    return name;
1✔
70
  }
71

72
  public PathPatternNode<V, VSerializer> getChildren(String nodeName) {
73
    return children.getOrDefault(nodeName, null);
1✔
74
  }
75

76
  public List<PathPatternNode<V, VSerializer>> getMatchChildren(String nodeName) {
77
    List<PathPatternNode<V, VSerializer>> res = new ArrayList<>();
1✔
78
    if (children.containsKey(nodeName)) {
1✔
79
      res.add(children.get(nodeName));
1✔
80
    }
81
    if (children.containsKey(ONE_LEVEL_PATH_WILDCARD)) {
1✔
82
      res.add(children.get(ONE_LEVEL_PATH_WILDCARD));
1✔
83
    }
84
    if (children.containsKey(MULTI_LEVEL_PATH_WILDCARD)) {
1✔
85
      res.add(children.get(MULTI_LEVEL_PATH_WILDCARD));
1✔
86
    }
87
    return res;
1✔
88
  }
89

90
  public Map<String, PathPatternNode<V, VSerializer>> getChildren() {
91
    return children;
1✔
92
  }
93

94
  public void addChild(PathPatternNode<V, VSerializer> tmpNode) {
95
    children.put(tmpNode.getName(), tmpNode);
1✔
96
  }
1✔
97

98
  public void deleteChild(PathPatternNode<V, VSerializer> tmpNode) {
99
    children.remove(tmpNode.getName());
1✔
100
  }
1✔
101

102
  public void appendValue(V value, BiConsumer<V, Set<V>> remappingFunction) {
103
    remappingFunction.accept(value, valueSet);
1✔
104
  }
1✔
105

106
  public void deleteValue(V value, BiConsumer<V, Set<V>> remappingFunction) {
107
    remappingFunction.accept(value, valueSet);
1✔
108
  }
1✔
109

110
  public Collection<V> getValues() {
111
    return valueSet;
1✔
112
  }
113

114
  /** @return true if from root to the current node is a registered path pattern. */
115
  public boolean isPathPattern() {
116
    return mark || isLeaf();
1✔
117
  }
118

119
  public boolean isLeaf() {
120
    return children.isEmpty();
1✔
121
  }
122

123
  public boolean isWildcard() {
124
    return PathPatternUtil.hasWildcard(name);
×
125
  }
126

127
  public boolean isMultiLevelWildcard() {
128
    return name.equals(MULTI_LEVEL_PATH_WILDCARD);
1✔
129
  }
130

131
  /** set true if from root to the current node is a registered path pattern. */
132
  public void markPathPattern(boolean mark) {
133
    this.mark = mark;
1✔
134
  }
1✔
135

136
  @Override
137
  public boolean equals(Object o) {
138
    if (this == o) return true;
1✔
139
    if (o == null || getClass() != o.getClass()) return false;
1✔
140
    PathPatternNode<?, ?> that = (PathPatternNode<?, ?>) o;
1✔
141
    return mark == that.mark
1✔
142
        && Objects.equals(name, that.name)
1✔
143
        && Objects.equals(children, that.children)
1✔
144
        && Objects.equals(valueSet, that.valueSet);
1✔
145
  }
146

147
  @Override
148
  public int hashCode() {
149
    return Objects.hash(name, children, valueSet, mark);
×
150
  }
151

152
  /**
153
   * Serialize PathPatternNode
154
   *
155
   * <ul>
156
   *   <li>[required] 1 string: name. It specifies the name of node
157
   *   <li>[required] 1 int: nodeType or size of valueSet.
158
   *       <ul>
159
   *         <li>If this node is being used by PathPatternTree, it specifies the type of this node.
160
   *             -1 means from root to the current node is a registered path pattern. Otherwise -2.
161
   *         <li>If this node is being used by PatternTreeMap, it specifies the size of valueSet
162
   *             which will be serialized next.(>=0)
163
   *       </ul>
164
   *   <li>[optional] valueSet
165
   *   <li>[required] 1 int: children size.
166
   *   <li>[optional] children
167
   * </ul>
168
   */
169
  public void serialize(ByteBuffer buffer) {
170
    ReadWriteIOUtils.write(name, buffer);
1✔
171
    if (valueSet == null) {
1✔
172
      ReadWriteIOUtils.write(mark ? -1 : -2, buffer);
1✔
173
    } else {
174
      ReadWriteIOUtils.write(valueSet.size(), buffer);
×
175
      for (V value : valueSet) {
×
176
        serializer.write(value, buffer);
×
177
      }
×
178
    }
179
    ReadWriteIOUtils.write(children.size(), buffer);
1✔
180
    serializeChildren(buffer);
1✔
181
  }
1✔
182

183
  void serializeChildren(ByteBuffer buffer) {
184
    for (PathPatternNode<V, VSerializer> childNode : children.values()) {
1✔
185
      childNode.serialize(buffer);
1✔
186
    }
1✔
187
  }
1✔
188

189
  public void serialize(PublicBAOS outputStream) throws IOException {
190
    ReadWriteIOUtils.write(name, outputStream);
1✔
191
    if (valueSet == null) {
1✔
192
      ReadWriteIOUtils.write(mark ? -1 : -2, outputStream);
1✔
193
    } else {
194
      ReadWriteIOUtils.write(valueSet.size(), outputStream);
×
195
      for (V value : valueSet) {
×
196
        serializer.write(value, outputStream);
×
197
      }
×
198
    }
199
    ReadWriteIOUtils.write(children.size(), outputStream);
1✔
200
    serializeChildren(outputStream);
1✔
201
  }
1✔
202

203
  public void serialize(DataOutputStream outputStream) throws IOException {
204
    ReadWriteIOUtils.write(name, outputStream);
1✔
205
    if (valueSet == null) {
1✔
206
      ReadWriteIOUtils.write(mark ? -1 : -2, outputStream);
1✔
207
    } else {
208
      ReadWriteIOUtils.write(valueSet.size(), outputStream);
×
209
      for (V value : valueSet) {
×
210
        serializer.write(value, outputStream);
×
211
      }
×
212
    }
213
    ReadWriteIOUtils.write(children.size(), outputStream);
1✔
214
    serializeChildren(outputStream);
1✔
215
  }
1✔
216

217
  void serializeChildren(PublicBAOS outputStream) throws IOException {
218
    for (PathPatternNode<V, VSerializer> childNode : children.values()) {
1✔
219
      childNode.serialize(outputStream);
1✔
220
    }
1✔
221
  }
1✔
222

223
  void serializeChildren(DataOutputStream outputStream) throws IOException {
224
    for (PathPatternNode<V, VSerializer> childNode : children.values()) {
1✔
225
      childNode.serialize(outputStream);
1✔
226
    }
1✔
227
  }
1✔
228

229
  public static <V, T extends PathPatternNode.Serializer<V>> PathPatternNode<V, T> deserializeNode(
230
      ByteBuffer buffer, T serializer) {
231
    PathPatternNode<V, T> node =
1✔
232
        new PathPatternNode<>(ReadWriteIOUtils.readString(buffer), serializer);
1✔
233
    int typeOrValueSize = ReadWriteIOUtils.readInt(buffer);
1✔
234
    if (typeOrValueSize >= 0) {
1✔
235
      // measurement node in PatternTreeMap
236
      Set<V> valueSet = new HashSet<>();
×
237
      for (int i = 0; i < typeOrValueSize; i++) {
×
238
        valueSet.add(serializer.read(buffer));
×
239
      }
240
      node.valueSet = valueSet;
×
241
    } else if (typeOrValueSize == -1) {
1✔
242
      // node in PathPatternTree
243
      node.markPathPattern(true);
1✔
244
    }
245
    int childrenSize = ReadWriteIOUtils.readInt(buffer);
1✔
246
    while (childrenSize > 0) {
1✔
247
      PathPatternNode<V, T> tmpNode = deserializeNode(buffer, serializer);
1✔
248
      node.addChild(tmpNode);
1✔
249
      childrenSize--;
1✔
250
    }
1✔
251
    return node;
1✔
252
  }
253

254
  /**
255
   * Interface to support serialize and deserialize valueSet.
256
   *
257
   * @param <T> Type of value.
258
   */
259
  public interface Serializer<T> {
260

261
    void write(T t, ByteBuffer buffer);
262

263
    void write(T t, PublicBAOS stream) throws IOException;
264

265
    void write(T t, DataOutputStream stream) throws IOException;
266

267
    T read(ByteBuffer buffer);
268
  }
269

270
  public static class VoidSerializer implements PathPatternNode.Serializer<Void> {
271

272
    private static class VoidSerializerHolder {
273
      private static final VoidSerializer INSTANCE = new VoidSerializer();
1✔
274

275
      private VoidSerializerHolder() {}
276
    }
277

278
    public static VoidSerializer getInstance() {
279
      return VoidSerializer.VoidSerializerHolder.INSTANCE;
1✔
280
    }
281

282
    private VoidSerializer() {}
283

284
    @Override
285
    public void write(Void unused, ByteBuffer buffer) {}
×
286

287
    @Override
288
    public void write(Void unused, PublicBAOS stream) {}
×
289

290
    @Override
291
    public void write(Void unused, DataOutputStream stream) {}
×
292

293
    @Override
294
    public Void read(ByteBuffer buffer) {
295
      return null;
×
296
    }
297
  }
298
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc