Search in sources :

Example 26 with IteratorScope

use of org.apache.accumulo.core.iterators.IteratorUtil.IteratorScope in project geowave by locationtech.

the class AccumuloOperations method attachIterators.

public boolean attachIterators(final String tableName, final boolean createTable, final boolean enableVersioning, final boolean enableBlockCache, final IteratorConfig... iterators) throws TableNotFoundException {
    final String qName = getQualifiedTableName(tableName);
    if (createTable && !getConnector().tableOperations().exists(qName)) {
        createTable(tableName, enableVersioning, enableBlockCache);
    }
    try {
        if ((iterators != null) && (iterators.length > 0)) {
            final Map<String, EnumSet<IteratorScope>> iteratorScopes = getConnector().tableOperations().listIterators(qName);
            for (final IteratorConfig iteratorConfig : iterators) {
                boolean mustDelete = false;
                boolean exists = false;
                final EnumSet<IteratorScope> existingScopes = iteratorScopes.get(iteratorConfig.getIteratorName());
                EnumSet<IteratorScope> configuredScopes;
                if (iteratorConfig.getScopes() == null) {
                    configuredScopes = EnumSet.allOf(IteratorScope.class);
                } else {
                    configuredScopes = iteratorConfig.getScopes();
                }
                Map<String, String> configuredOptions = null;
                if (existingScopes != null) {
                    if (existingScopes.size() == configuredScopes.size()) {
                        exists = true;
                        for (final IteratorScope s : existingScopes) {
                            if (!configuredScopes.contains(s)) {
                                // this iterator exists with the wrong
                                // scope, we will assume we want to remove
                                // it and add the new configuration
                                LOGGER.warn("found iterator '" + iteratorConfig.getIteratorName() + "' missing scope '" + s.name() + "', removing it and re-attaching");
                                mustDelete = true;
                                break;
                            }
                        }
                    }
                    if (existingScopes.size() > 0) {
                        // see if the options are the same, if they are not
                        // the same, apply a merge with the existing options
                        // and the configured options
                        final Iterator<IteratorScope> it = existingScopes.iterator();
                        while (it.hasNext()) {
                            final IteratorScope scope = it.next();
                            final IteratorSetting setting = getConnector().tableOperations().getIteratorSetting(qName, iteratorConfig.getIteratorName(), scope);
                            if (setting != null) {
                                final Map<String, String> existingOptions = setting.getOptions();
                                configuredOptions = iteratorConfig.getOptions(existingOptions);
                                if (existingOptions == null) {
                                    mustDelete = (configuredOptions == null);
                                } else if (configuredOptions == null) {
                                    mustDelete = true;
                                } else {
                                    // neither are null, compare the size of
                                    // the entry sets and check that they
                                    // are equivalent
                                    final Set<Entry<String, String>> existingEntries = existingOptions.entrySet();
                                    final Set<Entry<String, String>> configuredEntries = configuredOptions.entrySet();
                                    if (existingEntries.size() != configuredEntries.size()) {
                                        mustDelete = true;
                                    } else {
                                        mustDelete = (!existingEntries.containsAll(configuredEntries));
                                    }
                                }
                                // for each scope
                                break;
                            }
                        }
                    }
                }
                if (mustDelete) {
                    getConnector().tableOperations().removeIterator(qName, iteratorConfig.getIteratorName(), existingScopes);
                    exists = false;
                }
                if (!exists) {
                    if (configuredOptions == null) {
                        configuredOptions = iteratorConfig.getOptions(new HashMap<>());
                    }
                    getConnector().tableOperations().attachIterator(qName, new IteratorSetting(iteratorConfig.getIteratorPriority(), iteratorConfig.getIteratorName(), iteratorConfig.getIteratorClass(), configuredOptions), configuredScopes);
                }
            }
        }
    } catch (AccumuloException | AccumuloSecurityException e) {
        LOGGER.warn("Unable to create table '" + qName + "'", e);
    }
    return true;
}
Also used : AccumuloException(org.apache.accumulo.core.client.AccumuloException) EnumSet(java.util.EnumSet) Set(java.util.Set) TreeSet(java.util.TreeSet) SortedSet(java.util.SortedSet) ImmutableSet(com.google.common.collect.ImmutableSet) HashSet(java.util.HashSet) HashMap(java.util.HashMap) EnumSet(java.util.EnumSet) IteratorSetting(org.apache.accumulo.core.client.IteratorSetting) AccumuloSecurityException(org.apache.accumulo.core.client.AccumuloSecurityException) IteratorScope(org.apache.accumulo.core.iterators.IteratorUtil.IteratorScope) IteratorConfig(org.locationtech.geowave.datastore.accumulo.IteratorConfig)

Example 27 with IteratorScope

use of org.apache.accumulo.core.iterators.IteratorUtil.IteratorScope in project accumulo by apache.

the class TableOperationsHelperTest method testAttachIterator.

@Test
public void testAttachIterator() throws Exception {
    TableOperationsHelper t = getHelper();
    Map<String, String> empty = Collections.emptyMap();
    t.attachIterator("table", new IteratorSetting(10, "someName", "foo.bar", empty), EnumSet.of(IteratorScope.scan));
    check(t, "table", new String[] { "table.iterator.scan.someName=10,foo.bar" });
    t.removeIterator("table", "someName", EnumSet.of(IteratorScope.scan));
    check(t, "table", new String[] {});
    IteratorSetting setting = new IteratorSetting(10, "someName", "foo.bar");
    setting.addOptions(Collections.singletonMap("key", "value"));
    t.attachIterator("table", setting, EnumSet.of(IteratorScope.majc));
    setting = new IteratorSetting(10, "someName", "foo.bar");
    t.attachIterator("table", setting, EnumSet.of(IteratorScope.scan));
    check(t, "table", new String[] { "table.iterator.majc.someName=10,foo.bar", "table.iterator.majc.someName.opt.key=value", "table.iterator.scan.someName=10,foo.bar" });
    t.removeIterator("table", "someName", EnumSet.of(IteratorScope.scan));
    setting = new IteratorSetting(20, "otherName", "some.classname");
    setting.addOptions(Collections.singletonMap("key", "value"));
    t.attachIterator("table", setting, EnumSet.of(IteratorScope.majc));
    setting = new IteratorSetting(20, "otherName", "some.classname");
    t.attachIterator("table", setting, EnumSet.of(IteratorScope.scan));
    Map<String, EnumSet<IteratorScope>> two = t.listIterators("table");
    Assert.assertEquals(2, two.size());
    Assert.assertTrue(two.containsKey("otherName"));
    Assert.assertTrue(two.get("otherName").size() == 2);
    Assert.assertTrue(two.get("otherName").contains(IteratorScope.majc));
    Assert.assertTrue(two.get("otherName").contains(IteratorScope.scan));
    Assert.assertTrue(two.containsKey("someName"));
    Assert.assertTrue(two.get("someName").size() == 1);
    Assert.assertTrue(two.get("someName").contains(IteratorScope.majc));
    t.removeIterator("table", "someName", EnumSet.allOf(IteratorScope.class));
    check(t, "table", new String[] { "table.iterator.majc.otherName=20,some.classname", "table.iterator.majc.otherName.opt.key=value", "table.iterator.scan.otherName=20,some.classname" });
    setting = t.getIteratorSetting("table", "otherName", IteratorScope.scan);
    Assert.assertEquals(20, setting.getPriority());
    Assert.assertEquals("some.classname", setting.getIteratorClass());
    Assert.assertTrue(setting.getOptions().isEmpty());
    setting = t.getIteratorSetting("table", "otherName", IteratorScope.majc);
    Assert.assertEquals(20, setting.getPriority());
    Assert.assertEquals("some.classname", setting.getIteratorClass());
    Assert.assertFalse(setting.getOptions().isEmpty());
    Assert.assertEquals(Collections.singletonMap("key", "value"), setting.getOptions());
    t.attachIterator("table", setting, EnumSet.of(IteratorScope.minc));
    check(t, "table", new String[] { "table.iterator.majc.otherName=20,some.classname", "table.iterator.majc.otherName.opt.key=value", "table.iterator.minc.otherName=20,some.classname", "table.iterator.minc.otherName.opt.key=value", "table.iterator.scan.otherName=20,some.classname" });
    try {
        t.attachIterator("table", setting);
        Assert.fail();
    } catch (AccumuloException e) {
    // expected, ignore
    }
    setting.setName("thirdName");
    try {
        t.attachIterator("table", setting);
        Assert.fail();
    } catch (AccumuloException e) {
    }
    setting.setPriority(10);
    t.setProperty("table", "table.iterator.minc.thirdName.opt.key", "value");
    try {
        t.attachIterator("table", setting);
        Assert.fail();
    } catch (AccumuloException e) {
    }
    t.removeProperty("table", "table.iterator.minc.thirdName.opt.key");
    t.attachIterator("table", setting);
}
Also used : AccumuloException(org.apache.accumulo.core.client.AccumuloException) IteratorSetting(org.apache.accumulo.core.client.IteratorSetting) EnumSet(java.util.EnumSet) IteratorScope(org.apache.accumulo.core.iterators.IteratorUtil.IteratorScope) Test(org.junit.Test)

Example 28 with IteratorScope

use of org.apache.accumulo.core.iterators.IteratorUtil.IteratorScope in project accumulo by apache.

the class TableOperationsHelper method listIterators.

@Override
public Map<String, EnumSet<IteratorScope>> listIterators(String tableName) throws AccumuloSecurityException, AccumuloException, TableNotFoundException {
    Map<String, EnumSet<IteratorScope>> result = new TreeMap<>();
    for (Entry<String, String> property : this.getProperties(tableName)) {
        String name = property.getKey();
        String[] parts = name.split("\\.");
        if (parts.length == 4) {
            if (parts[0].equals("table") && parts[1].equals("iterator")) {
                IteratorScope scope = IteratorScope.valueOf(parts[2]);
                if (!result.containsKey(parts[3]))
                    result.put(parts[3], EnumSet.noneOf(IteratorScope.class));
                result.get(parts[3]).add(scope);
            }
        }
    }
    return result;
}
Also used : EnumSet(java.util.EnumSet) IteratorScope(org.apache.accumulo.core.iterators.IteratorUtil.IteratorScope) TreeMap(java.util.TreeMap)

Example 29 with IteratorScope

use of org.apache.accumulo.core.iterators.IteratorUtil.IteratorScope in project accumulo by apache.

the class NamespaceOperationsHelper method attachIterator.

@Override
public void attachIterator(String namespace, IteratorSetting setting, EnumSet<IteratorScope> scopes) throws AccumuloSecurityException, AccumuloException, NamespaceNotFoundException {
    checkIteratorConflicts(namespace, setting, scopes);
    for (IteratorScope scope : scopes) {
        String root = String.format("%s%s.%s", Property.TABLE_ITERATOR_PREFIX, scope.name().toLowerCase(), setting.getName());
        for (Entry<String, String> prop : setting.getOptions().entrySet()) {
            this.setProperty(namespace, root + ".opt." + prop.getKey(), prop.getValue());
        }
        this.setProperty(namespace, root, setting.getPriority() + "," + setting.getIteratorClass());
    }
}
Also used : IteratorScope(org.apache.accumulo.core.iterators.IteratorUtil.IteratorScope)

Example 30 with IteratorScope

use of org.apache.accumulo.core.iterators.IteratorUtil.IteratorScope in project accumulo by apache.

the class SummingCombinerTest method getIteratorInput.

private static IteratorTestInput getIteratorInput() {
    IteratorSetting setting = new IteratorSetting(50, SummingCombiner.class);
    LongCombiner.setEncodingType(setting, LongCombiner.Type.STRING);
    Combiner.setCombineAllColumns(setting, true);
    Combiner.setReduceOnFullCompactionOnly(setting, false);
    return new IteratorTestInput(SummingCombiner.class, setting.getOptions(), new Range(), INPUT_DATA, new SimpleIteratorEnvironment() {

        @Override
        public IteratorScope getIteratorScope() {
            return IteratorScope.majc;
        }
    });
}
Also used : SimpleIteratorEnvironment(org.apache.accumulo.iteratortest.environments.SimpleIteratorEnvironment) IteratorSetting(org.apache.accumulo.core.client.IteratorSetting) IteratorTestInput(org.apache.accumulo.iteratortest.IteratorTestInput) IteratorScope(org.apache.accumulo.core.iterators.IteratorUtil.IteratorScope) Range(org.apache.accumulo.core.data.Range)

Aggregations

IteratorScope (org.apache.accumulo.core.iterators.IteratorUtil.IteratorScope)48 IteratorSetting (org.apache.accumulo.core.client.IteratorSetting)18 EnumSet (java.util.EnumSet)13 TreeMap (java.util.TreeMap)11 AccumuloException (org.apache.accumulo.core.client.AccumuloException)10 Test (org.junit.Test)6 Test (org.junit.jupiter.api.Test)6 HashMap (java.util.HashMap)4 AccumuloClient (org.apache.accumulo.core.client.AccumuloClient)4 NamespaceNotFoundException (org.apache.accumulo.core.client.NamespaceNotFoundException)4 HashSet (java.util.HashSet)3 AccumuloSecurityException (org.apache.accumulo.core.client.AccumuloSecurityException)3 KeepCountOnlyUidAggregator (datawave.ingest.table.aggregator.KeepCountOnlyUidAggregator)2 ShardIndexKeyFunctor (datawave.ingest.table.bloomfilter.ShardIndexKeyFunctor)2 Map (java.util.Map)2 Column (org.apache.accumulo.core.client.IteratorSetting.Column)2 TableNotFoundException (org.apache.accumulo.core.client.TableNotFoundException)2 NewTableConfiguration (org.apache.accumulo.core.client.admin.NewTableConfiguration)2 TableOperations (org.apache.accumulo.core.client.admin.TableOperations)2 Range (org.apache.accumulo.core.data.Range)2