|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.skywalking.banyandb.v1.client; |
| 20 | + |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Set; |
| 24 | + |
| 25 | +import lombok.Setter; |
| 26 | +import org.apache.skywalking.banyandb.property.v1.BanyandbProperty; |
| 27 | +import org.apache.skywalking.banyandb.v1.client.grpc.exception.BanyanDBException; |
| 28 | +import org.apache.skywalking.banyandb.v1.client.metadata.MetadataCache; |
| 29 | + |
| 30 | +/** |
| 31 | + * PropertyQuery is the high-level query API for the property model. |
| 32 | + */ |
| 33 | +@Setter |
| 34 | +public class PropertyQuery extends AbstractQuery<BanyandbProperty.QueryRequest> { |
| 35 | + /** |
| 36 | + * The limit size of the query. Default value is 20. |
| 37 | + */ |
| 38 | + private int limit; |
| 39 | + |
| 40 | + /** |
| 41 | + * Specific property IDs to query |
| 42 | + */ |
| 43 | + private List<String> ids; |
| 44 | + |
| 45 | + /** |
| 46 | + * Node selector for distributed query routing |
| 47 | + */ |
| 48 | + private String nodeSelector; |
| 49 | + |
| 50 | + /** |
| 51 | + * Construct a property query with required fields |
| 52 | + */ |
| 53 | + public PropertyQuery(final List<String> groups, final String name, final Set<String> projections) { |
| 54 | + super(groups, name, null, projections); |
| 55 | + this.limit = 20; |
| 56 | + this.ids = new ArrayList<>(); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Add a property ID to filter query results |
| 61 | + * @param id property ID |
| 62 | + * @return this query instance for chaining |
| 63 | + */ |
| 64 | + public PropertyQuery id(String id) { |
| 65 | + if (id != null && !id.isEmpty()) { |
| 66 | + this.ids.add(id); |
| 67 | + } |
| 68 | + return this; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Add multiple property IDs to filter query results |
| 73 | + * @param ids list of property IDs |
| 74 | + * @return this query instance for chaining |
| 75 | + */ |
| 76 | + public PropertyQuery ids(List<String> ids) { |
| 77 | + if (ids != null) { |
| 78 | + this.ids.addAll(ids); |
| 79 | + } |
| 80 | + return this; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Set a node selector for query routing |
| 85 | + * @param nodeSelector the node selector expression |
| 86 | + * @return this query instance for chaining |
| 87 | + */ |
| 88 | + public PropertyQuery nodeSelector(String nodeSelector) { |
| 89 | + this.nodeSelector = nodeSelector; |
| 90 | + return this; |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public PropertyQuery and(PairQueryCondition<?> condition) { |
| 95 | + return (PropertyQuery) super.and(condition); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public PropertyQuery or(PairQueryCondition<?> condition) { |
| 100 | + return (PropertyQuery) super.or(condition); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + BanyandbProperty.QueryRequest build(MetadataCache.EntityMetadata ignored) throws BanyanDBException { |
| 105 | + return build(); |
| 106 | + } |
| 107 | + |
| 108 | + public BanyandbProperty.QueryRequest build() throws BanyanDBException { |
| 109 | + final BanyandbProperty.QueryRequest.Builder builder = BanyandbProperty.QueryRequest.newBuilder(); |
| 110 | + builder.setName(this.name); |
| 111 | + builder.addAllGroups(this.groups); |
| 112 | + builder.addAllTagProjection(this.tagProjections); |
| 113 | + buildCriteria().ifPresent(builder::setCriteria); |
| 114 | + builder.setLimit(this.limit); |
| 115 | + builder.setTrace(this.trace); |
| 116 | + |
| 117 | + if (!this.ids.isEmpty()) { |
| 118 | + builder.addAllIds(this.ids); |
| 119 | + } |
| 120 | + |
| 121 | + if (this.nodeSelector != null && !this.nodeSelector.isEmpty()) { |
| 122 | + builder.setNodeSelector(this.nodeSelector); |
| 123 | + } |
| 124 | + |
| 125 | + return builder.build(); |
| 126 | + } |
| 127 | +} |
0 commit comments