|
| 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 | + * https://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 | +package org.apache.accumulo.server.metadata.iterators; |
| 20 | + |
| 21 | +import static org.apache.accumulo.server.metadata.iterators.SetEncodingIterator.getTabletRow; |
| 22 | + |
| 23 | +import java.io.IOException; |
| 24 | +import java.util.Collection; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.NoSuchElementException; |
| 27 | +import java.util.Set; |
| 28 | + |
| 29 | +import org.apache.accumulo.core.client.IteratorSetting; |
| 30 | +import org.apache.accumulo.core.data.ByteSequence; |
| 31 | +import org.apache.accumulo.core.data.Condition; |
| 32 | +import org.apache.accumulo.core.data.Key; |
| 33 | +import org.apache.accumulo.core.data.PartialKey; |
| 34 | +import org.apache.accumulo.core.data.Range; |
| 35 | +import org.apache.accumulo.core.data.Value; |
| 36 | +import org.apache.accumulo.core.iterators.IteratorEnvironment; |
| 37 | +import org.apache.accumulo.core.iterators.SortedKeyValueIterator; |
| 38 | +import org.apache.accumulo.core.iterators.WrappingIterator; |
| 39 | +import org.apache.accumulo.server.metadata.ConditionalTabletMutatorImpl; |
| 40 | +import org.apache.hadoop.io.Text; |
| 41 | + |
| 42 | +import com.google.common.base.Preconditions; |
| 43 | + |
| 44 | +/** |
| 45 | + * Iterator that checks if a column family size is less than or equal a limit as part of a |
| 46 | + * conditional mutation. |
| 47 | + */ |
| 48 | +public class ColumnFamilySizeLimitIterator extends WrappingIterator { |
| 49 | + |
| 50 | + private static final String LIMIT_OPT = "limit"; |
| 51 | + private static final Text EMPTY = new Text(); |
| 52 | + |
| 53 | + private Long limit; |
| 54 | + |
| 55 | + private Key startKey = null; |
| 56 | + private Value topValue = null; |
| 57 | + |
| 58 | + @Override |
| 59 | + public void init(SortedKeyValueIterator<Key,Value> source, Map<String,String> options, |
| 60 | + IteratorEnvironment env) throws IOException { |
| 61 | + super.init(source, options, env); |
| 62 | + limit = Long.parseLong(options.get(LIMIT_OPT)); |
| 63 | + Preconditions.checkState(limit >= 0); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public void seek(Range range, Collection<ByteSequence> columnFamilies, boolean inclusive) |
| 68 | + throws IOException { |
| 69 | + Text tabletRow = getTabletRow(range); |
| 70 | + Text family = range.getStartKey().getColumnFamily(); |
| 71 | + |
| 72 | + Preconditions.checkArgument( |
| 73 | + family.getLength() > 0 && range.getStartKey().getColumnQualifier().getLength() == 0); |
| 74 | + |
| 75 | + startKey = new Key(tabletRow, family); |
| 76 | + Key endKey = startKey.followingKey(PartialKey.ROW_COLFAM); |
| 77 | + |
| 78 | + Range r = new Range(startKey, true, endKey, false); |
| 79 | + |
| 80 | + var source = getSource(); |
| 81 | + source.seek(r, Set.of(startKey.getColumnFamilyData()), true); |
| 82 | + |
| 83 | + long count = 0; |
| 84 | + while (source.hasTop()) { |
| 85 | + source.next(); |
| 86 | + count++; |
| 87 | + } |
| 88 | + |
| 89 | + if (count <= limit) { |
| 90 | + topValue = new Value("1"); |
| 91 | + } else { |
| 92 | + topValue = null; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public boolean hasTop() { |
| 98 | + if (startKey == null) { |
| 99 | + throw new IllegalStateException("never been seeked"); |
| 100 | + } |
| 101 | + return topValue != null; |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public void next() throws IOException { |
| 106 | + if (startKey == null) { |
| 107 | + throw new IllegalStateException("never been seeked"); |
| 108 | + } |
| 109 | + topValue = null; |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public Key getTopKey() { |
| 114 | + if (startKey == null) { |
| 115 | + throw new IllegalStateException("never been seeked"); |
| 116 | + } |
| 117 | + if (topValue == null) { |
| 118 | + throw new NoSuchElementException(); |
| 119 | + } |
| 120 | + |
| 121 | + return startKey; |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public Value getTopValue() { |
| 126 | + if (startKey == null) { |
| 127 | + throw new IllegalStateException("never been seeked"); |
| 128 | + } |
| 129 | + if (topValue == null) { |
| 130 | + throw new NoSuchElementException(); |
| 131 | + } |
| 132 | + return topValue; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Create a condition that checks if the specified column family's size is less than or equal to |
| 137 | + * the given limit. |
| 138 | + */ |
| 139 | + public static Condition createCondition(Text family, long limit) { |
| 140 | + IteratorSetting is = new IteratorSetting(ConditionalTabletMutatorImpl.INITIAL_ITERATOR_PRIO, |
| 141 | + ColumnFamilySizeLimitIterator.class); |
| 142 | + is.addOption(LIMIT_OPT, limit + ""); |
| 143 | + return new Condition(family, EMPTY).setValue("1").setIterators(is); |
| 144 | + } |
| 145 | +} |
0 commit comments