|
22 | 22 | import static org.junit.jupiter.api.Assertions.assertFalse;
|
23 | 23 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
24 | 24 |
|
| 25 | +import java.io.IOException; |
25 | 26 | import java.util.ArrayList;
|
26 | 27 | import java.util.List;
|
| 28 | +import java.util.Map; |
27 | 29 | import java.util.Map.Entry;
|
| 30 | +import java.util.Set; |
| 31 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 32 | +import java.util.function.Predicate; |
| 33 | +import java.util.regex.Pattern; |
| 34 | +import java.util.stream.Collectors; |
28 | 35 |
|
29 | 36 | import org.apache.accumulo.core.client.Accumulo;
|
30 | 37 | import org.apache.accumulo.core.client.AccumuloClient;
|
31 | 38 | import org.apache.accumulo.core.client.BatchWriter;
|
32 | 39 | import org.apache.accumulo.core.client.ClientSideIteratorScanner;
|
33 | 40 | import org.apache.accumulo.core.client.IteratorSetting;
|
34 | 41 | import org.apache.accumulo.core.client.Scanner;
|
| 42 | +import org.apache.accumulo.core.clientImpl.ClientContext; |
| 43 | +import org.apache.accumulo.core.clientImpl.OfflineScanner; |
35 | 44 | import org.apache.accumulo.core.data.Key;
|
36 | 45 | import org.apache.accumulo.core.data.Mutation;
|
37 | 46 | import org.apache.accumulo.core.data.PartialKey;
|
38 | 47 | import org.apache.accumulo.core.data.Value;
|
| 48 | +import org.apache.accumulo.core.iterators.Filter; |
| 49 | +import org.apache.accumulo.core.iterators.IteratorEnvironment; |
| 50 | +import org.apache.accumulo.core.iterators.SortedKeyValueIterator; |
39 | 51 | import org.apache.accumulo.core.iterators.user.IntersectingIterator;
|
40 | 52 | import org.apache.accumulo.core.iterators.user.VersioningIterator;
|
41 | 53 | import org.apache.accumulo.core.security.Authorizations;
|
@@ -154,4 +166,100 @@ public void testVersioning() throws Exception {
|
154 | 166 | assertFalse(csis.iterator().hasNext());
|
155 | 167 | }
|
156 | 168 | }
|
| 169 | + |
| 170 | + private static final AtomicBoolean initCalled = new AtomicBoolean(false); |
| 171 | + |
| 172 | + public static class TestPropFilter extends Filter { |
| 173 | + |
| 174 | + private Predicate<Key> keyPredicate; |
| 175 | + |
| 176 | + private Predicate<Key> createRegexPredicate(String regex) { |
| 177 | + Predicate<Key> kp = k -> true; |
| 178 | + if (regex != null) { |
| 179 | + var pattern = Pattern.compile(regex); |
| 180 | + kp = k -> pattern.matcher(k.getRowData().toString()).matches(); |
| 181 | + } |
| 182 | + |
| 183 | + return kp; |
| 184 | + } |
| 185 | + |
| 186 | + @Override |
| 187 | + public void init(SortedKeyValueIterator<Key,Value> source, Map<String,String> options, |
| 188 | + IteratorEnvironment env) throws IOException { |
| 189 | + super.init(source, options, env); |
| 190 | + Predicate<Key> generalPredicate = |
| 191 | + createRegexPredicate(env.getPluginEnv().getConfiguration().getCustom("testRegex")); |
| 192 | + Predicate<Key> tablePredicate = createRegexPredicate( |
| 193 | + env.getPluginEnv().getConfiguration(env.getTableId()).getTableCustom("testRegex")); |
| 194 | + keyPredicate = generalPredicate.and(tablePredicate); |
| 195 | + initCalled.set(true); |
| 196 | + } |
| 197 | + |
| 198 | + @Override |
| 199 | + public boolean accept(Key k, Value v) { |
| 200 | + return keyPredicate.test(k); |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + private void runPluginEnvTest(Set<String> expected) throws Exception { |
| 205 | + try (var scanner = client.createScanner(tableName)) { |
| 206 | + initCalled.set(false); |
| 207 | + var csis = new ClientSideIteratorScanner(scanner); |
| 208 | + csis.addScanIterator(new IteratorSetting(100, "filter", TestPropFilter.class)); |
| 209 | + assertEquals(expected, |
| 210 | + csis.stream().map(e -> e.getKey().getRowData().toString()).collect(Collectors.toSet())); |
| 211 | + // this check is here to ensure the iterator executed client side and not server side |
| 212 | + assertTrue(initCalled.get()); |
| 213 | + } |
| 214 | + |
| 215 | + // The offline scanner also runs iterators client side, so test its client side access to |
| 216 | + // accumulo config from iterators also. |
| 217 | + client.tableOperations().offline(tableName, true); |
| 218 | + var context = (ClientContext) client; |
| 219 | + try (OfflineScanner offlineScanner = |
| 220 | + new OfflineScanner(context, context.getTableId(tableName), Authorizations.EMPTY)) { |
| 221 | + initCalled.set(false); |
| 222 | + offlineScanner.addScanIterator(new IteratorSetting(100, "filter", TestPropFilter.class)); |
| 223 | + assertEquals(expected, offlineScanner.stream().map(e -> e.getKey().getRowData().toString()) |
| 224 | + .collect(Collectors.toSet())); |
| 225 | + assertTrue(initCalled.get()); |
| 226 | + } |
| 227 | + client.tableOperations().online(tableName, true); |
| 228 | + } |
| 229 | + |
| 230 | + /** |
| 231 | + * Test an iterators ability to access accumulo config in an iterator running client side. |
| 232 | + */ |
| 233 | + @Test |
| 234 | + public void testPluginEnv() throws Exception { |
| 235 | + Set<String> rows = Set.of("1234", "abc", "xyz789"); |
| 236 | + |
| 237 | + client.tableOperations().create(tableName); |
| 238 | + try (BatchWriter bw = client.createBatchWriter(tableName)) { |
| 239 | + for (var row : rows) { |
| 240 | + Mutation m = new Mutation(row); |
| 241 | + m.put("f", "q", "v"); |
| 242 | + bw.addMutation(m); |
| 243 | + } |
| 244 | + } |
| 245 | + |
| 246 | + runPluginEnvTest(rows); |
| 247 | + |
| 248 | + // The iterator should see the following system property and filter based on it |
| 249 | + client.instanceOperations().setProperty("general.custom.testRegex", ".*[a-z]+.*"); |
| 250 | + runPluginEnvTest(Set.of("abc", "xyz789")); |
| 251 | + |
| 252 | + // The iterator should see the following table property and filter based on the table and system |
| 253 | + // property |
| 254 | + client.tableOperations().setProperty(tableName, "table.custom.testRegex", ".*[0-9]+.*"); |
| 255 | + runPluginEnvTest(Set.of("xyz789")); |
| 256 | + |
| 257 | + // Remove the system property, so filtering should only happen based on the table property |
| 258 | + client.instanceOperations().removeProperty("general.custom.testRegex"); |
| 259 | + runPluginEnvTest(Set.of("1234", "xyz789")); |
| 260 | + |
| 261 | + // Iterator should do no filtering after removing this property |
| 262 | + client.tableOperations().removeProperty(tableName, "table.custom.testRegex"); |
| 263 | + runPluginEnvTest(rows); |
| 264 | + } |
157 | 265 | }
|
0 commit comments