Search in sources :

Example 26 with SortedMapIterator

use of org.apache.accumulo.core.iteratorsImpl.system.SortedMapIterator in project accumulo by apache.

the class RegExFilterTest method test1.

@Test
public void test1() throws IOException {
    TreeMap<Key, Value> tm = new TreeMap<>();
    Key k1 = newKeyValue(tm, "boo1", "yup", "20080201", "dog");
    Key k2 = newKeyValue(tm, "boo1", "yap", "20080202", "cat");
    Key k3 = newKeyValue(tm, "boo2", "yip", "20080203", "hamster");
    RegExFilter rei = new RegExFilter();
    rei.describeOptions();
    IteratorSetting is = new IteratorSetting(1, RegExFilter.class);
    RegExFilter.setRegexs(is, ".*2", null, null, null, false);
    assertTrue(rei.validateOptions(is.getOptions()));
    rei.init(new SortedMapIterator(tm), is.getOptions(), new DefaultIteratorEnvironment());
    rei.seek(new Range(), EMPTY_COL_FAMS, false);
    assertTrue(rei.hasTop());
    assertEquals(rei.getTopKey(), k3);
    rei.next();
    assertFalse(rei.hasTop());
    // -----------------------------------------------------
    // Test substring regex
    is.clearOptions();
    // Should only match hamster
    RegExFilter.setRegexs(is, null, null, null, "amst", false, true);
    rei.validateOptions(is.getOptions());
    rei.init(new SortedMapIterator(tm), is.getOptions(), new DefaultIteratorEnvironment());
    rei.seek(new Range(), EMPTY_COL_FAMS, false);
    assertTrue(rei.hasTop());
    assertEquals(rei.getTopKey(), k3);
    rei.next();
    assertFalse(rei.hasTop());
    // -----------------------------------------------------
    is.clearOptions();
    RegExFilter.setRegexs(is, null, "ya.*", null, null, false);
    assertTrue(rei.validateOptions(is.getOptions()));
    rei.init(new SortedMapIterator(tm), is.getOptions(), new DefaultIteratorEnvironment());
    rei.seek(new Range(), EMPTY_COL_FAMS, false);
    assertTrue(rei.hasTop());
    assertEquals(rei.getTopKey(), k2);
    rei.next();
    assertFalse(rei.hasTop());
    // -----------------------------------------------------
    is.clearOptions();
    RegExFilter.setRegexs(is, null, null, ".*01", null, false);
    assertTrue(rei.validateOptions(is.getOptions()));
    rei.init(new SortedMapIterator(tm), is.getOptions(), new DefaultIteratorEnvironment());
    rei.seek(new Range(), EMPTY_COL_FAMS, false);
    assertTrue(rei.hasTop());
    assertEquals(rei.getTopKey(), k1);
    rei.next();
    assertFalse(rei.hasTop());
    // -----------------------------------------------------
    is.clearOptions();
    RegExFilter.setRegexs(is, null, null, null, ".*at", false);
    assertTrue(rei.validateOptions(is.getOptions()));
    rei.init(new SortedMapIterator(tm), is.getOptions(), new DefaultIteratorEnvironment());
    rei.seek(new Range(), EMPTY_COL_FAMS, false);
    assertTrue(rei.hasTop());
    assertEquals(rei.getTopKey(), k2);
    rei.next();
    assertFalse(rei.hasTop());
    // -----------------------------------------------------
    is.clearOptions();
    RegExFilter.setRegexs(is, null, null, null, ".*ap", false);
    rei.init(new SortedMapIterator(tm), is.getOptions(), new DefaultIteratorEnvironment());
    rei.seek(new Range(), EMPTY_COL_FAMS, false);
    assertFalse(rei.hasTop());
    // -----------------------------------------------------
    is.clearOptions();
    RegExFilter.setRegexs(is, null, "ya.*", null, ".*at", false);
    rei.init(new SortedMapIterator(tm), is.getOptions(), new DefaultIteratorEnvironment());
    rei.seek(new Range(), EMPTY_COL_FAMS, false);
    assertTrue(rei.hasTop());
    assertEquals(rei.getTopKey(), k2);
    rei.next();
    assertFalse(rei.hasTop());
    // -----------------------------------------------------
    is.clearOptions();
    RegExFilter.setRegexs(is, null, "ya.*", null, ".*ap", false);
    rei.init(new SortedMapIterator(tm), is.getOptions(), new DefaultIteratorEnvironment());
    rei.seek(new Range(), EMPTY_COL_FAMS, false);
    assertFalse(rei.hasTop());
    // -----------------------------------------------------
    is.clearOptions();
    RegExFilter.setRegexs(is, "boo1", null, null, null, false);
    rei.init(new SortedMapIterator(tm), is.getOptions(), new DefaultIteratorEnvironment());
    rei.seek(new Range(), EMPTY_COL_FAMS, false);
    assertTrue(rei.hasTop());
    assertEquals(rei.getTopKey(), k2);
    rei.next();
    assertTrue(rei.hasTop());
    assertEquals(rei.getTopKey(), k1);
    rei.next();
    assertFalse(rei.hasTop());
    // -----------------------------------------------------
    is.clearOptions();
    rei.init(new SortedMapIterator(tm), is.getOptions(), new DefaultIteratorEnvironment());
    rei.seek(new Range(), EMPTY_COL_FAMS, false);
    assertTrue(rei.hasTop());
    assertEquals(rei.getTopKey(), k2);
    rei.next();
    assertTrue(rei.hasTop());
    assertEquals(rei.getTopKey(), k1);
    rei.next();
    assertTrue(rei.hasTop());
    assertEquals(rei.getTopKey(), k3);
    rei.next();
    assertFalse(rei.hasTop());
    // -----------------------------------------------------
    is.clearOptions();
    RegExFilter.setRegexs(is, "hamster", null, "hamster", "hamster", true);
    rei.init(new SortedMapIterator(tm), is.getOptions(), new DefaultIteratorEnvironment());
    rei.seek(new Range(), EMPTY_COL_FAMS, false);
    assertTrue(rei.hasTop());
    assertEquals(rei.getTopKey(), k3);
    rei.next();
    assertFalse(rei.hasTop());
    // -----------------------------------------------------
    is.clearOptions();
    RegExFilter.setRegexs(is, null, "ya.*", "hamster", null, true);
    rei.init(new SortedMapIterator(tm), is.getOptions(), new DefaultIteratorEnvironment());
    rei.seek(new Range(), EMPTY_COL_FAMS, false);
    assertTrue(rei.hasTop());
    assertEquals(rei.getTopKey(), k2);
    rei.next();
    assertFalse(rei.hasTop());
    is.clearOptions();
    RegExFilter.setRegexs(is, null, "ya.*", "hamster", null, true);
    rei.init(new SortedMapIterator(tm), is.getOptions(), new DefaultIteratorEnvironment());
    rei.seek(new Range(), EMPTY_COL_FAMS, false);
    rei.deepCopy(new DefaultIteratorEnvironment());
    // -----------------------------------------------------
    String multiByteText = new String("\u6d67\u6F68\u7067");
    String multiByteRegex = new String(".*\u6F68.*");
    Key k4 = new Key("boo4".getBytes(), "hoo".getBytes(), "20080203".getBytes(), "".getBytes(), 1L);
    Value inVal = new Value(multiByteText);
    tm.put(k4, inVal);
    is.clearOptions();
    RegExFilter.setRegexs(is, null, null, null, multiByteRegex, true);
    rei.init(new SortedMapIterator(tm), is.getOptions(), new DefaultIteratorEnvironment());
    rei.seek(new Range(), EMPTY_COL_FAMS, false);
    assertTrue(rei.hasTop());
    Value outValue = rei.getTopValue();
    String outVal = new String(outValue.get(), UTF_8);
    assertEquals(outVal, multiByteText);
}
Also used : IteratorSetting(org.apache.accumulo.core.client.IteratorSetting) DefaultIteratorEnvironment(org.apache.accumulo.core.iterators.DefaultIteratorEnvironment) Value(org.apache.accumulo.core.data.Value) TreeMap(java.util.TreeMap) SortedMapIterator(org.apache.accumulo.core.iteratorsImpl.system.SortedMapIterator) Range(org.apache.accumulo.core.data.Range) Key(org.apache.accumulo.core.data.Key) Test(org.junit.jupiter.api.Test)

Example 27 with SortedMapIterator

use of org.apache.accumulo.core.iteratorsImpl.system.SortedMapIterator in project accumulo by apache.

the class RegExFilterTest method testNullByteInKey.

@Test
public void testNullByteInKey() throws IOException {
    TreeMap<Key, Value> tm = new TreeMap<>();
    String s1 = "first", s2 = "second";
    byte[] b1 = s1.getBytes(), b2 = s2.getBytes(), ball;
    ball = new byte[b1.length + b2.length + 1];
    System.arraycopy(b1, 0, ball, 0, b1.length);
    ball[b1.length] = (byte) 0;
    System.arraycopy(b2, 0, ball, b1.length + 1, b2.length);
    Key key = new Key(ball, new byte[0], new byte[0], new byte[0], 90, false);
    Value val = new Value();
    tm.put(key, val);
    IteratorSetting is = new IteratorSetting(5, RegExFilter.class);
    RegExFilter.setRegexs(is, s2, null, null, null, true, true);
    RegExFilter filter = new RegExFilter();
    filter.init(new SortedMapIterator(tm), is.getOptions(), null);
    filter.seek(new Range(), EMPTY_COL_FAMS, false);
    assertTrue(filter.hasTop(), "iterator couldn't find a match when it should have");
}
Also used : IteratorSetting(org.apache.accumulo.core.client.IteratorSetting) Value(org.apache.accumulo.core.data.Value) TreeMap(java.util.TreeMap) SortedMapIterator(org.apache.accumulo.core.iteratorsImpl.system.SortedMapIterator) Range(org.apache.accumulo.core.data.Range) Key(org.apache.accumulo.core.data.Key) Test(org.junit.jupiter.api.Test)

Example 28 with SortedMapIterator

use of org.apache.accumulo.core.iteratorsImpl.system.SortedMapIterator in project accumulo by apache.

the class RowFilterTest method deepCopyCopiesTheSource.

@Test
public void deepCopyCopiesTheSource() throws Exception {
    SortedMapIterator source = new SortedMapIterator(createKeyValues());
    RowFilter filter = new RowZeroOrOneFilter();
    filter.init(source, Collections.emptyMap(), new DefaultIteratorEnvironment());
    filter.seek(new Range(), Collections.emptySet(), false);
    // Save off the first key and value
    Key firstKey = filter.getTopKey();
    Value firstValue = filter.getTopValue();
    // Assert that the row is valid given our filter
    assertEquals("0", firstKey.getRow().toString());
    // Read some extra data, just making sure it's all valid
    Key lastKeyRead = null;
    for (int i = 0; i < 5; i++) {
        filter.next();
        lastKeyRead = filter.getTopKey();
        assertEquals("0", lastKeyRead.getRow().toString());
    }
    // Make a copy of the original RowFilter
    RowFilter copy = (RowFilter) filter.deepCopy(new DefaultIteratorEnvironment());
    // Because it's a copy, we should be able to safely seek this one without affecting the original
    copy.seek(new Range(), Collections.emptySet(), false);
    assertTrue(copy.hasTop(), "deepCopy'ed RowFilter did not have a top key");
    Key firstKeyFromCopy = copy.getTopKey();
    Value firstValueFromCopy = copy.getTopValue();
    // Verify that we got the same first k-v pair we did earlier
    assertEquals(firstKey, firstKeyFromCopy);
    assertEquals(firstValue, firstValueFromCopy);
    filter.next();
    Key finalKeyRead = filter.getTopKey();
    // Make sure we got a Key that was greater than the last Key we read from the original RowFilter
    assertTrue(lastKeyRead.compareTo(finalKeyRead) < 0, "Expected next key read to be greater than the previous after deepCopy");
}
Also used : DefaultIteratorEnvironment(org.apache.accumulo.core.iterators.DefaultIteratorEnvironment) Value(org.apache.accumulo.core.data.Value) SortedMapIterator(org.apache.accumulo.core.iteratorsImpl.system.SortedMapIterator) Range(org.apache.accumulo.core.data.Range) Key(org.apache.accumulo.core.data.Key) Test(org.junit.jupiter.api.Test)

Example 29 with SortedMapIterator

use of org.apache.accumulo.core.iteratorsImpl.system.SortedMapIterator in project accumulo by apache.

the class RowFilterTest method testChainedRowFilters.

@Test
public void testChainedRowFilters() throws Exception {
    SortedMapIterator source = new SortedMapIterator(createKeyValues());
    RowFilter filter0 = new TrueFilter();
    filter0.init(source, Collections.emptyMap(), new DefaultIteratorEnvironment());
    RowFilter filter = new TrueFilter();
    filter.init(filter0, Collections.emptyMap(), new DefaultIteratorEnvironment());
    filter.seek(new Range(), Collections.emptySet(), false);
    assertEquals(Set.of("0", "1", "2", "3", "4"), getRows(filter));
}
Also used : DefaultIteratorEnvironment(org.apache.accumulo.core.iterators.DefaultIteratorEnvironment) SortedMapIterator(org.apache.accumulo.core.iteratorsImpl.system.SortedMapIterator) Range(org.apache.accumulo.core.data.Range) Test(org.junit.jupiter.api.Test)

Example 30 with SortedMapIterator

use of org.apache.accumulo.core.iteratorsImpl.system.SortedMapIterator in project accumulo by apache.

the class FilterTest method testTimestampFilter.

@Test
public void testTimestampFilter() throws IOException, ParseException {
    Text colf = new Text("a");
    Text colq = new Text("b");
    Value dv = new Value();
    TreeMap<Key, Value> tm = new TreeMap<>();
    for (int i = 0; i < 100; i++) {
        Key k = new Key(new Text(String.format("%02d", i)), colf, colq);
        k.setTimestamp(i);
        tm.put(k, dv);
    }
    assertEquals(100, tm.size());
    SimpleDateFormat dateParser = new SimpleDateFormat("yyyyMMddHHmmssz");
    long baseTime = dateParser.parse("19990101000000GMT").getTime();
    tm.clear();
    for (int i = 0; i < 100; i++) {
        Key k = new Key(new Text(String.format("%02d", i)), colf, colq);
        k.setTimestamp(baseTime + (i * 1000));
        tm.put(k, dv);
    }
    assertEquals(100, tm.size());
    TimestampFilter a = new TimestampFilter();
    IteratorSetting is = new IteratorSetting(1, TimestampFilter.class);
    TimestampFilter.setRange(is, "19990101010011GMT+01:00", "19990101010031GMT+01:00");
    a.init(new SortedMapIterator(tm), is.getOptions(), null);
    a = (TimestampFilter) a.deepCopy(null);
    a.seek(new Range(), EMPTY_COL_FAMS, false);
    assertEquals(21, size(a));
    TimestampFilter.setRange(is, baseTime + 11000, baseTime + 31000);
    a.init(new SortedMapIterator(tm), is.getOptions(), null);
    a.seek(new Range(), EMPTY_COL_FAMS, false);
    assertEquals(21, size(a));
    TimestampFilter.setEnd(is, "19990101000031GMT", false);
    a.init(new SortedMapIterator(tm), is.getOptions(), null);
    a.seek(new Range(), EMPTY_COL_FAMS, false);
    assertEquals(20, size(a));
    TimestampFilter.setStart(is, "19990101000011GMT", false);
    a.init(new SortedMapIterator(tm), is.getOptions(), null);
    a.seek(new Range(), EMPTY_COL_FAMS, false);
    assertEquals(19, size(a));
    TimestampFilter.setEnd(is, "19990101000031GMT", true);
    a.init(new SortedMapIterator(tm), is.getOptions(), null);
    a.seek(new Range(), EMPTY_COL_FAMS, false);
    assertEquals(20, size(a));
    is.clearOptions();
    TimestampFilter.setStart(is, "19990101000011GMT", true);
    a.init(new SortedMapIterator(tm), is.getOptions(), null);
    a.seek(new Range(), EMPTY_COL_FAMS, false);
    assertEquals(89, size(a));
    TimestampFilter.setStart(is, "19990101000011GMT", false);
    assertTrue(a.validateOptions(is.getOptions()));
    a.init(new SortedMapIterator(tm), is.getOptions(), null);
    a.seek(new Range(), EMPTY_COL_FAMS, false);
    assertEquals(88, size(a));
    is.clearOptions();
    TimestampFilter.setEnd(is, "19990101000031GMT", true);
    a.init(new SortedMapIterator(tm), is.getOptions(), null);
    a.seek(new Range(), EMPTY_COL_FAMS, false);
    assertEquals(32, size(a));
    TimestampFilter.setEnd(is, "19990101000031GMT", false);
    assertTrue(a.validateOptions(is.getOptions()));
    a.init(new SortedMapIterator(tm), is.getOptions(), null);
    a.seek(new Range(), EMPTY_COL_FAMS, false);
    assertEquals(31, size(a));
    TimestampFilter.setEnd(is, 253402300800001L, true);
    a.init(new SortedMapIterator(tm), is.getOptions(), null);
    is.clearOptions();
    is.addOption(TimestampFilter.START, "19990101000011GMT");
    assertTrue(a.validateOptions(is.getOptions()));
    a.init(new SortedMapIterator(tm), is.getOptions(), null);
    a.seek(new Range(), EMPTY_COL_FAMS, false);
    assertEquals(89, size(a));
    is.clearOptions();
    is.addOption(TimestampFilter.END, "19990101000031GMT");
    assertTrue(a.validateOptions(is.getOptions()));
    a.init(new SortedMapIterator(tm), is.getOptions(), null);
    a.seek(new Range(), EMPTY_COL_FAMS, false);
    assertEquals(32, size(a));
    final TimestampFilter finalA = a;
    assertThrows(IllegalArgumentException.class, () -> finalA.validateOptions(EMPTY_OPTS));
}
Also used : IteratorSetting(org.apache.accumulo.core.client.IteratorSetting) Value(org.apache.accumulo.core.data.Value) Text(org.apache.hadoop.io.Text) TreeMap(java.util.TreeMap) SortedMapIterator(org.apache.accumulo.core.iteratorsImpl.system.SortedMapIterator) Range(org.apache.accumulo.core.data.Range) SimpleDateFormat(java.text.SimpleDateFormat) Key(org.apache.accumulo.core.data.Key) Test(org.junit.jupiter.api.Test)

Aggregations

SortedMapIterator (org.apache.accumulo.core.iteratorsImpl.system.SortedMapIterator)109 Test (org.junit.jupiter.api.Test)97 Range (org.apache.accumulo.core.data.Range)93 Key (org.apache.accumulo.core.data.Key)89 Value (org.apache.accumulo.core.data.Value)89 TreeMap (java.util.TreeMap)82 IteratorSetting (org.apache.accumulo.core.client.IteratorSetting)38 Text (org.apache.hadoop.io.Text)29 PartialKey (org.apache.accumulo.core.data.PartialKey)16 Combiner (org.apache.accumulo.core.iterators.Combiner)12 LongCombiner (org.apache.accumulo.core.iterators.LongCombiner)12 TypedValueCombiner (org.apache.accumulo.core.iterators.TypedValueCombiner)12 ArrayList (java.util.ArrayList)11 HashSet (java.util.HashSet)9 DefaultIteratorEnvironment (org.apache.accumulo.core.iterators.DefaultIteratorEnvironment)8 ColumnFamilySkippingIterator (org.apache.accumulo.core.iteratorsImpl.system.ColumnFamilySkippingIterator)8 MultiIterator (org.apache.accumulo.core.iteratorsImpl.system.MultiIterator)8 ByteSequence (org.apache.accumulo.core.data.ByteSequence)7 SortedKeyValueIterator (org.apache.accumulo.core.iterators.SortedKeyValueIterator)7 SourceSwitchingIterator (org.apache.accumulo.core.iteratorsImpl.system.SourceSwitchingIterator)7