Search in sources :

Example 1 with SortedMap

use of java.util.SortedMap in project elasticsearch by elastic.

the class WordDelimiterTokenFilterFactory method parseTypes.

/**
     * parses a list of MappingCharFilter style rules into a custom byte[] type table
     */
static byte[] parseTypes(Collection<String> rules) {
    SortedMap<Character, Byte> typeMap = new TreeMap<>();
    for (String rule : rules) {
        Matcher m = typePattern.matcher(rule);
        if (!m.find())
            throw new RuntimeException("Invalid Mapping Rule : [" + rule + "]");
        String lhs = parseString(m.group(1).trim());
        Byte rhs = parseType(m.group(2).trim());
        if (lhs.length() != 1)
            throw new RuntimeException("Invalid Mapping Rule : [" + rule + "]. Only a single character is allowed.");
        if (rhs == null)
            throw new RuntimeException("Invalid Mapping Rule : [" + rule + "]. Illegal type.");
        typeMap.put(lhs.charAt(0), rhs);
    }
    // ensure the table is always at least as big as DEFAULT_WORD_DELIM_TABLE for performance
    byte[] types = new byte[Math.max(typeMap.lastKey() + 1, WordDelimiterIterator.DEFAULT_WORD_DELIM_TABLE.length)];
    for (int i = 0; i < types.length; i++) types[i] = WordDelimiterIterator.getType(i);
    for (Map.Entry<Character, Byte> mapping : typeMap.entrySet()) types[mapping.getKey()] = mapping.getValue();
    return types;
}
Also used : Matcher(java.util.regex.Matcher) TreeMap(java.util.TreeMap) TreeMap(java.util.TreeMap) Map(java.util.Map) SortedMap(java.util.SortedMap)

Example 2 with SortedMap

use of java.util.SortedMap in project hbase by apache.

the class TestCatalogJanitor method parentWithSpecifiedEndKeyCleanedEvenIfDaughterGoneFirst.

/**
   * Make sure parent with specified end key gets cleaned up even if daughter is cleaned up before it.
   *
   * @param rootDir the test case name, used as the HBase testing utility root
   * @param lastEndKey the end key of the split parent
   * @throws IOException
   * @throws InterruptedException
   */
private void parentWithSpecifiedEndKeyCleanedEvenIfDaughterGoneFirst(final String rootDir, final byte[] lastEndKey) throws IOException, InterruptedException {
    HBaseTestingUtility htu = new HBaseTestingUtility();
    setRootDirAndCleanIt(htu, rootDir);
    MasterServices services = new MockMasterServices(htu);
    CatalogJanitor janitor = new CatalogJanitor(services);
    final HTableDescriptor htd = createHTableDescriptor();
    // Create regions: aaa->{lastEndKey}, aaa->ccc, aaa->bbb, bbb->ccc, etc.
    // Parent
    HRegionInfo parent = new HRegionInfo(htd.getTableName(), Bytes.toBytes("aaa"), lastEndKey);
    // Sleep a second else the encoded name on these regions comes out
    // same for all with same start key and made in same second.
    Thread.sleep(1001);
    // Daughter a
    HRegionInfo splita = new HRegionInfo(htd.getTableName(), Bytes.toBytes("aaa"), Bytes.toBytes("ccc"));
    Thread.sleep(1001);
    // Make daughters of daughter a; splitaa and splitab.
    HRegionInfo splitaa = new HRegionInfo(htd.getTableName(), Bytes.toBytes("aaa"), Bytes.toBytes("bbb"));
    HRegionInfo splitab = new HRegionInfo(htd.getTableName(), Bytes.toBytes("bbb"), Bytes.toBytes("ccc"));
    // Daughter b
    HRegionInfo splitb = new HRegionInfo(htd.getTableName(), Bytes.toBytes("ccc"), lastEndKey);
    Thread.sleep(1001);
    // Make Daughters of daughterb; splitba and splitbb.
    HRegionInfo splitba = new HRegionInfo(htd.getTableName(), Bytes.toBytes("ccc"), Bytes.toBytes("ddd"));
    HRegionInfo splitbb = new HRegionInfo(htd.getTableName(), Bytes.toBytes("ddd"), lastEndKey);
    // First test that our Comparator works right up in CatalogJanitor.
    // Just fo kicks.
    SortedMap<HRegionInfo, Result> regions = new TreeMap<>(new CatalogJanitor.SplitParentFirstComparator());
    // Now make sure that this regions map sorts as we expect it to.
    regions.put(parent, createResult(parent, splita, splitb));
    regions.put(splitb, createResult(splitb, splitba, splitbb));
    regions.put(splita, createResult(splita, splitaa, splitab));
    // Assert its properly sorted.
    int index = 0;
    for (Map.Entry<HRegionInfo, Result> e : regions.entrySet()) {
        if (index == 0) {
            assertTrue(e.getKey().getEncodedName().equals(parent.getEncodedName()));
        } else if (index == 1) {
            assertTrue(e.getKey().getEncodedName().equals(splita.getEncodedName()));
        } else if (index == 2) {
            assertTrue(e.getKey().getEncodedName().equals(splitb.getEncodedName()));
        }
        index++;
    }
    // Now play around with the cleanParent function.  Create a ref from splita
    // up to the parent.
    Path splitaRef = createReferences(services, htd, parent, splita, Bytes.toBytes("ccc"), false);
    // Make sure actual super parent sticks around because splita has a ref.
    assertFalse(janitor.cleanParent(parent, regions.get(parent)));
    //splitba, and split bb, do not have dirs in fs.  That means that if
    // we test splitb, it should get cleaned up.
    assertTrue(janitor.cleanParent(splitb, regions.get(splitb)));
    // Now remove ref from splita to parent... so parent can be let go and so
    // the daughter splita can be split (can't split if still references).
    // BUT make the timing such that the daughter gets cleaned up before we
    // can get a chance to let go of the parent.
    FileSystem fs = FileSystem.get(htu.getConfiguration());
    assertTrue(fs.delete(splitaRef, true));
    // Create the refs from daughters of splita.
    Path splitaaRef = createReferences(services, htd, splita, splitaa, Bytes.toBytes("bbb"), false);
    Path splitabRef = createReferences(services, htd, splita, splitab, Bytes.toBytes("bbb"), true);
    // Test splita.  It should stick around because references from splitab, etc.
    assertFalse(janitor.cleanParent(splita, regions.get(splita)));
    // Now clean up parent daughter first.  Remove references from its daughters.
    assertTrue(fs.delete(splitaaRef, true));
    assertTrue(fs.delete(splitabRef, true));
    assertTrue(janitor.cleanParent(splita, regions.get(splita)));
    // Super parent should get cleaned up now both splita and splitb are gone.
    assertTrue(janitor.cleanParent(parent, regions.get(parent)));
    services.stop("test finished");
    janitor.cancel(true);
}
Also used : Path(org.apache.hadoop.fs.Path) SplitParentFirstComparator(org.apache.hadoop.hbase.master.CatalogJanitor.SplitParentFirstComparator) TreeMap(java.util.TreeMap) HTableDescriptor(org.apache.hadoop.hbase.HTableDescriptor) Result(org.apache.hadoop.hbase.client.Result) RegionActionResult(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.RegionActionResult) HRegionInfo(org.apache.hadoop.hbase.HRegionInfo) HBaseTestingUtility(org.apache.hadoop.hbase.HBaseTestingUtility) FileSystem(org.apache.hadoop.fs.FileSystem) Map(java.util.Map) SortedMap(java.util.SortedMap) TreeMap(java.util.TreeMap)

Example 3 with SortedMap

use of java.util.SortedMap in project hive by apache.

the class HBaseUtils method hashStorageDescriptor.

/**
   * Produce a hash for the storage descriptor
   * @param sd storage descriptor to hash
   * @param md message descriptor to use to generate the hash
   * @return the hash as a byte array
   */
static byte[] hashStorageDescriptor(StorageDescriptor sd, MessageDigest md) {
    // Note all maps and lists have to be absolutely sorted.  Otherwise we'll produce different
    // results for hashes based on the OS or JVM being used.
    md.reset();
    for (FieldSchema fs : sd.getCols()) {
        md.update(fs.getName().getBytes(ENCODING));
        md.update(fs.getType().getBytes(ENCODING));
        if (fs.getComment() != null)
            md.update(fs.getComment().getBytes(ENCODING));
    }
    if (sd.getInputFormat() != null) {
        md.update(sd.getInputFormat().getBytes(ENCODING));
    }
    if (sd.getOutputFormat() != null) {
        md.update(sd.getOutputFormat().getBytes(ENCODING));
    }
    md.update(sd.isCompressed() ? "true".getBytes(ENCODING) : "false".getBytes(ENCODING));
    md.update(Integer.toString(sd.getNumBuckets()).getBytes(ENCODING));
    if (sd.getSerdeInfo() != null) {
        SerDeInfo serde = sd.getSerdeInfo();
        if (serde.getName() != null) {
            md.update(serde.getName().getBytes(ENCODING));
        }
        if (serde.getSerializationLib() != null) {
            md.update(serde.getSerializationLib().getBytes(ENCODING));
        }
        if (serde.getParameters() != null) {
            SortedMap<String, String> params = new TreeMap<>(serde.getParameters());
            for (Map.Entry<String, String> param : params.entrySet()) {
                md.update(param.getKey().getBytes(ENCODING));
                md.update(param.getValue().getBytes(ENCODING));
            }
        }
    }
    if (sd.getBucketCols() != null) {
        SortedSet<String> bucketCols = new TreeSet<>(sd.getBucketCols());
        for (String bucket : bucketCols) md.update(bucket.getBytes(ENCODING));
    }
    if (sd.getSortCols() != null) {
        SortedSet<Order> orders = new TreeSet<>(sd.getSortCols());
        for (Order order : orders) {
            md.update(order.getCol().getBytes(ENCODING));
            md.update(Integer.toString(order.getOrder()).getBytes(ENCODING));
        }
    }
    if (sd.getSkewedInfo() != null) {
        SkewedInfo skewed = sd.getSkewedInfo();
        if (skewed.getSkewedColNames() != null) {
            SortedSet<String> colnames = new TreeSet<>(skewed.getSkewedColNames());
            for (String colname : colnames) md.update(colname.getBytes(ENCODING));
        }
        if (skewed.getSkewedColValues() != null) {
            SortedSet<String> sortedOuterList = new TreeSet<>();
            for (List<String> innerList : skewed.getSkewedColValues()) {
                SortedSet<String> sortedInnerList = new TreeSet<>(innerList);
                sortedOuterList.add(StringUtils.join(sortedInnerList, "."));
            }
            for (String colval : sortedOuterList) md.update(colval.getBytes(ENCODING));
        }
        if (skewed.getSkewedColValueLocationMaps() != null) {
            SortedMap<String, String> sortedMap = new TreeMap<>();
            for (Map.Entry<List<String>, String> smap : skewed.getSkewedColValueLocationMaps().entrySet()) {
                SortedSet<String> sortedKey = new TreeSet<>(smap.getKey());
                sortedMap.put(StringUtils.join(sortedKey, "."), smap.getValue());
            }
            for (Map.Entry<String, String> e : sortedMap.entrySet()) {
                md.update(e.getKey().getBytes(ENCODING));
                md.update(e.getValue().getBytes(ENCODING));
            }
        }
    }
    return md.digest();
}
Also used : Order(org.apache.hadoop.hive.metastore.api.Order) FieldSchema(org.apache.hadoop.hive.metastore.api.FieldSchema) SerDeInfo(org.apache.hadoop.hive.metastore.api.SerDeInfo) ByteString(com.google.protobuf.ByteString) TreeMap(java.util.TreeMap) SkewedInfo(org.apache.hadoop.hive.metastore.api.SkewedInfo) TreeSet(java.util.TreeSet) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) SortedMap(java.util.SortedMap) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap)

Example 4 with SortedMap

use of java.util.SortedMap in project buck by facebook.

the class RuleKeyBuilder method setReflectively.

/** Recursively serializes the value. Serialization of the key is handled outside. */
protected RuleKeyBuilder<RULE_KEY> setReflectively(@Nullable Object val) {
    if (val instanceof RuleKeyAppendable) {
        return setAppendableRuleKey((RuleKeyAppendable) val);
    }
    if (val instanceof BuildRule) {
        return setBuildRule((BuildRule) val);
    }
    if (val instanceof Supplier) {
        try (Scope containerScope = scopedHasher.wrapperScope(Wrapper.SUPPLIER)) {
            Object newVal = ((Supplier<?>) val).get();
            return setReflectively(newVal);
        }
    }
    if (val instanceof Optional) {
        Object o = ((Optional<?>) val).orElse(null);
        try (Scope wraperScope = scopedHasher.wrapperScope(Wrapper.OPTIONAL)) {
            return setReflectively(o);
        }
    }
    if (val instanceof Either) {
        Either<?, ?> either = (Either<?, ?>) val;
        if (either.isLeft()) {
            try (Scope wraperScope = scopedHasher.wrapperScope(Wrapper.EITHER_LEFT)) {
                return setReflectively(either.getLeft());
            }
        } else {
            try (Scope wraperScope = scopedHasher.wrapperScope(Wrapper.EITHER_RIGHT)) {
                return setReflectively(either.getRight());
            }
        }
    }
    // Note {@link java.nio.file.Path} implements "Iterable", so we explicitly exclude it here.
    if (val instanceof Iterable && !(val instanceof Path)) {
        try (ContainerScope containerScope = scopedHasher.containerScope(Container.LIST)) {
            for (Object element : (Iterable<?>) val) {
                try (Scope elementScope = containerScope.elementScope()) {
                    setReflectively(element);
                }
            }
            return this;
        }
    }
    if (val instanceof Iterator) {
        Iterator<?> iterator = (Iterator<?>) val;
        try (ContainerScope containerScope = scopedHasher.containerScope(Container.LIST)) {
            while (iterator.hasNext()) {
                try (Scope elementScope = containerScope.elementScope()) {
                    setReflectively(iterator.next());
                }
            }
        }
        return this;
    }
    if (val instanceof Map) {
        if (!(val instanceof SortedMap || val instanceof ImmutableMap)) {
            logger.warn("Adding an unsorted map to the rule key. " + "Expect unstable ordering and caches misses: %s", val);
        }
        try (ContainerScope containerScope = scopedHasher.containerScope(Container.MAP)) {
            for (Map.Entry<?, ?> entry : ((Map<?, ?>) val).entrySet()) {
                try (Scope elementScope = containerScope.elementScope()) {
                    setReflectively(entry.getKey());
                }
                try (Scope elementScope = containerScope.elementScope()) {
                    setReflectively(entry.getValue());
                }
            }
        }
        return this;
    }
    if (val instanceof Path) {
        throw new HumanReadableException("It's not possible to reliably disambiguate Paths. They are disallowed from rule keys");
    }
    if (val instanceof SourcePath) {
        try {
            return setSourcePath((SourcePath) val);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    if (val instanceof NonHashableSourcePathContainer) {
        SourcePath sourcePath = ((NonHashableSourcePathContainer) val).getSourcePath();
        return setNonHashingSourcePath(sourcePath);
    }
    if (val instanceof SourceWithFlags) {
        SourceWithFlags source = (SourceWithFlags) val;
        try {
            setSourcePath(source.getSourcePath());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        setReflectively(source.getFlags());
        return this;
    }
    return setSingleValue(val);
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) ArchiveMemberSourcePath(com.facebook.buck.rules.ArchiveMemberSourcePath) ArchiveMemberPath(com.facebook.buck.io.ArchiveMemberPath) BuildTargetSourcePath(com.facebook.buck.rules.BuildTargetSourcePath) Path(java.nio.file.Path) PathSourcePath(com.facebook.buck.rules.PathSourcePath) Optional(java.util.Optional) ContainerScope(com.facebook.buck.rules.keys.RuleKeyScopedHasher.ContainerScope) NonHashableSourcePathContainer(com.facebook.buck.rules.NonHashableSourcePathContainer) IOException(java.io.IOException) SourceWithFlags(com.facebook.buck.rules.SourceWithFlags) ImmutableMap(com.google.common.collect.ImmutableMap) SourcePath(com.facebook.buck.rules.SourcePath) ArchiveMemberSourcePath(com.facebook.buck.rules.ArchiveMemberSourcePath) BuildTargetSourcePath(com.facebook.buck.rules.BuildTargetSourcePath) PathSourcePath(com.facebook.buck.rules.PathSourcePath) Scope(com.facebook.buck.rules.keys.RuleKeyScopedHasher.Scope) ContainerScope(com.facebook.buck.rules.keys.RuleKeyScopedHasher.ContainerScope) HumanReadableException(com.facebook.buck.util.HumanReadableException) SortedMap(java.util.SortedMap) Iterator(java.util.Iterator) Either(com.facebook.buck.model.Either) BuildRule(com.facebook.buck.rules.BuildRule) Supplier(com.google.common.base.Supplier) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) SortedMap(java.util.SortedMap) RuleKeyAppendable(com.facebook.buck.rules.RuleKeyAppendable)

Example 5 with SortedMap

use of java.util.SortedMap in project mapdb by jankotek.

the class ConcurrentSkipListSubMapTest method testHeadMapContents.

/**
     * headMap returns map with keys in requested range
     */
@Test
public void testHeadMapContents() {
    ConcurrentNavigableMap map = map5();
    SortedMap sm = map.headMap(four);
    assertTrue(sm.containsKey(one));
    assertTrue(sm.containsKey(two));
    assertTrue(sm.containsKey(three));
    assertFalse(sm.containsKey(four));
    assertFalse(sm.containsKey(five));
    Iterator i = sm.keySet().iterator();
    Object k;
    k = (Integer) (i.next());
    assertEquals(one, k);
    k = (Integer) (i.next());
    assertEquals(two, k);
    k = (Integer) (i.next());
    assertEquals(three, k);
    assertFalse(i.hasNext());
    if (isReadOnly())
        return;
    sm.clear();
    assertTrue(sm.isEmpty());
    assertEquals(2, map.size());
    assertEquals(four, map.firstKey());
}
Also used : SortedMap(java.util.SortedMap) Iterator(java.util.Iterator) ConcurrentNavigableMap(java.util.concurrent.ConcurrentNavigableMap) Test(org.junit.Test)

Aggregations

SortedMap (java.util.SortedMap)560 Map (java.util.Map)411 TreeMap (java.util.TreeMap)318 HashMap (java.util.HashMap)182 ArrayList (java.util.ArrayList)109 Iterator (java.util.Iterator)88 NavigableMap (java.util.NavigableMap)78 Test (org.junit.Test)70 IOException (java.io.IOException)66 List (java.util.List)66 ImmutableMap (com.google.common.collect.ImmutableMap)53 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)51 Set (java.util.Set)50 IExpr (org.matheclipse.core.interfaces.IExpr)50 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)39 HashSet (java.util.HashSet)35 Entry (java.util.Map.Entry)33 File (java.io.File)30 LinkedHashMap (java.util.LinkedHashMap)29 ConcurrentNavigableMap (java.util.concurrent.ConcurrentNavigableMap)29