Search in sources :

Example 26 with EnumMap

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

the class TestCacheOnWrite method readStoreFile.

private void readStoreFile(boolean useTags) throws IOException {
    HFile.Reader reader = HFile.createReader(fs, storeFilePath, cacheConf, conf);
    LOG.info("HFile information: " + reader);
    HFileContext meta = new HFileContextBuilder().withCompression(compress).withBytesPerCheckSum(CKBYTES).withChecksumType(ChecksumType.NULL).withBlockSize(DATA_BLOCK_SIZE).withDataBlockEncoding(NoOpDataBlockEncoder.INSTANCE.getDataBlockEncoding()).withIncludesTags(useTags).build();
    final boolean cacheBlocks = false;
    final boolean pread = false;
    HFileScanner scanner = reader.getScanner(cacheBlocks, pread);
    assertTrue(testDescription, scanner.seekTo());
    long offset = 0;
    EnumMap<BlockType, Integer> blockCountByType = new EnumMap<>(BlockType.class);
    DataBlockEncoding encodingInCache = NoOpDataBlockEncoder.INSTANCE.getDataBlockEncoding();
    List<Long> cachedBlocksOffset = new ArrayList<>();
    Map<Long, HFileBlock> cachedBlocks = new HashMap<>();
    while (offset < reader.getTrailer().getLoadOnOpenDataOffset()) {
        // Flags: don't cache the block, use pread, this is not a compaction.
        // Also, pass null for expected block type to avoid checking it.
        HFileBlock block = reader.readBlock(offset, -1, false, true, false, true, null, encodingInCache);
        BlockCacheKey blockCacheKey = new BlockCacheKey(reader.getName(), offset);
        HFileBlock fromCache = (HFileBlock) blockCache.getBlock(blockCacheKey, true, false, true);
        boolean isCached = fromCache != null;
        cachedBlocksOffset.add(offset);
        cachedBlocks.put(offset, fromCache);
        boolean shouldBeCached = cowType.shouldBeCached(block.getBlockType());
        assertTrue("shouldBeCached: " + shouldBeCached + "\n" + "isCached: " + isCached + "\n" + "Test description: " + testDescription + "\n" + "block: " + block + "\n" + "encodingInCache: " + encodingInCache + "\n" + "blockCacheKey: " + blockCacheKey, shouldBeCached == isCached);
        if (isCached) {
            if (cacheConf.shouldCacheCompressed(fromCache.getBlockType().getCategory())) {
                if (compress != Compression.Algorithm.NONE) {
                    assertFalse(fromCache.isUnpacked());
                }
                fromCache = fromCache.unpack(meta, reader.getUncachedBlockReader());
            } else {
                assertTrue(fromCache.isUnpacked());
            }
            // block we cached at write-time and block read from file should be identical
            assertEquals(block.getChecksumType(), fromCache.getChecksumType());
            assertEquals(block.getBlockType(), fromCache.getBlockType());
            assertNotEquals(block.getBlockType(), BlockType.ENCODED_DATA);
            assertEquals(block.getOnDiskSizeWithHeader(), fromCache.getOnDiskSizeWithHeader());
            assertEquals(block.getOnDiskSizeWithoutHeader(), fromCache.getOnDiskSizeWithoutHeader());
            assertEquals(block.getUncompressedSizeWithoutHeader(), fromCache.getUncompressedSizeWithoutHeader());
        }
        offset += block.getOnDiskSizeWithHeader();
        BlockType bt = block.getBlockType();
        Integer count = blockCountByType.get(bt);
        blockCountByType.put(bt, (count == null ? 0 : count) + 1);
    }
    LOG.info("Block count by type: " + blockCountByType);
    String countByType = blockCountByType.toString();
    if (useTags) {
        assertEquals("{" + BlockType.DATA + "=2663, LEAF_INDEX=297, BLOOM_CHUNK=9, INTERMEDIATE_INDEX=32}", countByType);
    } else {
        assertEquals("{" + BlockType.DATA + "=2498, LEAF_INDEX=278, BLOOM_CHUNK=9, INTERMEDIATE_INDEX=31}", countByType);
    }
    // iterate all the keyvalue from hfile
    while (scanner.next()) {
        scanner.getCell();
    }
    Iterator<Long> iterator = cachedBlocksOffset.iterator();
    while (iterator.hasNext()) {
        Long entry = iterator.next();
        BlockCacheKey blockCacheKey = new BlockCacheKey(reader.getName(), entry);
        HFileBlock hFileBlock = cachedBlocks.get(entry);
        if (hFileBlock != null) {
            // call return twice because for the isCache cased the counter would have got incremented
            // twice
            blockCache.returnBlock(blockCacheKey, hFileBlock);
            if (cacheCompressedData) {
                if (this.compress == Compression.Algorithm.NONE || cowType == CacheOnWriteType.INDEX_BLOCKS || cowType == CacheOnWriteType.BLOOM_BLOCKS) {
                    blockCache.returnBlock(blockCacheKey, hFileBlock);
                }
            } else {
                blockCache.returnBlock(blockCacheKey, hFileBlock);
            }
        }
    }
    scanner.shipped();
    reader.close();
}
Also used : DataBlockEncoding(org.apache.hadoop.hbase.io.encoding.DataBlockEncoding) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) EnumMap(java.util.EnumMap)

Example 27 with EnumMap

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

the class StringResources method writeArrays.

/**
   * Writes the metadata and strings in the following format to the output stream:
   * [Int: # of arrays]
   * [Int: Smallest resource id among arrays]
   * [[Short: resource id delta] [Byte: #genders] [[Byte: gender enum ordinal] [Int: #elements]
   *  [[Short: length of element] x # of elements]] x # of genders] x # of arrays
   * [Byte array of string value] x Summation of genders over array elements over # of arrays
   * @param outputStream
   * @throws IOException
   */
private void writeArrays(DataOutputStream outputStream) throws IOException {
    outputStream.writeInt(arrays.keySet().size());
    if (arrays.keySet().isEmpty()) {
        return;
    }
    int previousResourceId = arrays.firstKey();
    outputStream.writeInt(previousResourceId);
    try (ByteArrayOutputStream dataStream = new ByteArrayOutputStream()) {
        for (Map.Entry<Integer, EnumMap<Gender, ImmutableList<String>>> entry : arrays.entrySet()) {
            writeShort(outputStream, entry.getKey() - previousResourceId);
            EnumMap<Gender, ImmutableList<String>> genderMap = entry.getValue();
            outputStream.writeByte(genderMap.size());
            for (Map.Entry<Gender, ImmutableList<String>> gender : genderMap.entrySet()) {
                outputStream.writeByte(gender.getKey().ordinal());
                ImmutableList<String> arrayElements = gender.getValue();
                outputStream.writeByte(arrayElements.size());
                for (String arrayValue : arrayElements) {
                    byte[] byteValue = getUnescapedStringBytes(arrayValue);
                    writeShort(outputStream, byteValue.length);
                    dataStream.write(byteValue);
                }
            }
            previousResourceId = entry.getKey();
        }
        outputStream.write(dataStream.toByteArray());
    }
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImmutableMap(com.google.common.collect.ImmutableMap) EnumMap(java.util.EnumMap) TreeMap(java.util.TreeMap) Map(java.util.Map) SortedMap(java.util.SortedMap) EnumMap(java.util.EnumMap)

Example 28 with EnumMap

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

the class StringResources method writeStrings.

/**
   * Writes the metadata and strings in the following format to the output stream:
   * [Int: # of strings]
   * [Int: Smallest resource id among strings]
   * [[Short: resource id delta] [Byte: #genders] [[Byte: gender enum ordinal] [Short: length of
   *    the string] [Byte array of the string value]] x #genders] x # of strings
   * @param outputStream
   * @throws IOException
   */
private void writeStrings(DataOutputStream outputStream) throws IOException {
    outputStream.writeInt(strings.size());
    if (strings.isEmpty()) {
        return;
    }
    int previousResourceId = strings.firstKey();
    outputStream.writeInt(previousResourceId);
    try (ByteArrayOutputStream dataStream = new ByteArrayOutputStream()) {
        for (Map.Entry<Integer, EnumMap<Gender, String>> entry : strings.entrySet()) {
            writeShort(outputStream, entry.getKey() - previousResourceId);
            EnumMap<Gender, String> genderMap = entry.getValue();
            outputStream.writeByte(genderMap.size());
            for (Map.Entry<Gender, String> gender : genderMap.entrySet()) {
                outputStream.writeByte(gender.getKey().ordinal());
                byte[] genderValue = getUnescapedStringBytes(gender.getValue());
                writeShort(outputStream, genderValue.length);
                dataStream.write(genderValue);
            }
            previousResourceId = entry.getKey();
        }
        outputStream.write(dataStream.toByteArray());
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImmutableMap(com.google.common.collect.ImmutableMap) EnumMap(java.util.EnumMap) TreeMap(java.util.TreeMap) Map(java.util.Map) SortedMap(java.util.SortedMap) EnumMap(java.util.EnumMap)

Example 29 with EnumMap

use of java.util.EnumMap in project zxing-android-embedded by journeyapps.

the class DecodeHintManager method parseDecodeHints.

static Map<DecodeHintType, ?> parseDecodeHints(Uri inputUri) {
    String query = inputUri.getEncodedQuery();
    if (query == null || query.isEmpty()) {
        return null;
    }
    // Extract parameters
    Map<String, String> parameters = splitQuery(query);
    Map<DecodeHintType, Object> hints = new EnumMap<>(DecodeHintType.class);
    for (DecodeHintType hintType : DecodeHintType.values()) {
        if (hintType == DecodeHintType.CHARACTER_SET || hintType == DecodeHintType.NEED_RESULT_POINT_CALLBACK || hintType == DecodeHintType.POSSIBLE_FORMATS) {
            // This hint is specified in another way
            continue;
        }
        String parameterName = hintType.name();
        String parameterText = parameters.get(parameterName);
        if (parameterText == null) {
            continue;
        }
        if (hintType.getValueType().equals(Object.class)) {
            // This is an unspecified type of hint content. Use the value as is.
            // TODO: Can we make a different assumption on this?
            hints.put(hintType, parameterText);
            continue;
        }
        if (hintType.getValueType().equals(Void.class)) {
            // Void hints are just flags: use the constant specified by DecodeHintType
            hints.put(hintType, Boolean.TRUE);
            continue;
        }
        if (hintType.getValueType().equals(String.class)) {
            // A string hint: use the decoded value.
            hints.put(hintType, parameterText);
            continue;
        }
        if (hintType.getValueType().equals(Boolean.class)) {
            // An empty parameter is simply a flag-style parameter, assuming true
            if (parameterText.isEmpty()) {
                hints.put(hintType, Boolean.TRUE);
            } else if ("0".equals(parameterText) || "false".equalsIgnoreCase(parameterText) || "no".equalsIgnoreCase(parameterText)) {
                hints.put(hintType, Boolean.FALSE);
            } else {
                hints.put(hintType, Boolean.TRUE);
            }
            continue;
        }
        if (hintType.getValueType().equals(int[].class)) {
            // Strip a trailing comma as in Java style array initialisers.
            if (!parameterText.isEmpty() && parameterText.charAt(parameterText.length() - 1) == ',') {
                parameterText = parameterText.substring(0, parameterText.length() - 1);
            }
            String[] values = COMMA.split(parameterText);
            int[] array = new int[values.length];
            for (int i = 0; i < values.length; i++) {
                try {
                    array[i] = Integer.parseInt(values[i]);
                } catch (NumberFormatException ignored) {
                    Log.w(TAG, "Skipping array of integers hint " + hintType + " due to invalid numeric value: '" + values[i] + '\'');
                    array = null;
                    break;
                }
            }
            if (array != null) {
                hints.put(hintType, array);
            }
            continue;
        }
        Log.w(TAG, "Unsupported hint type '" + hintType + "' of type " + hintType.getValueType());
    }
    Log.i(TAG, "Hints from the URI: " + hints);
    return hints;
}
Also used : DecodeHintType(com.google.zxing.DecodeHintType) EnumMap(java.util.EnumMap)

Example 30 with EnumMap

use of java.util.EnumMap in project LogisticsPipes by RS485.

the class ServerRouter method recheckAdjacent.

/**
	 * Rechecks the piped connection to all adjacent routers as well as discover
	 * new ones.
	 */
private boolean recheckAdjacent() {
    connectionNeedsChecking = 0;
    if (LPConstants.DEBUG) {
        causedBy.clear();
    }
    if (getPipe() != null) {
        /*
			if (getPipe().getDebug() != null && getPipe().getDebug().debugThisPipe) {
				Info info = StackTraceUtil.addTraceInformation("(" + getPipe().getX() + ", " + getPipe().getY() + ", " + getPipe().getZ() + ")");
				StackTraceUtil.printTrace();
				info.end();
			}
			*/
        getPipe().spawnParticle(Particles.LightRedParticle, 5);
    }
    LPTickHandler.adjChecksDone++;
    boolean adjacentChanged = false;
    CoreRoutedPipe thisPipe = getPipe();
    if (thisPipe == null) {
        return false;
    }
    HashMap<CoreRoutedPipe, ExitRoute> adjacent;
    List<Pair<ILogisticsPowerProvider, List<IFilter>>> power;
    List<Pair<ISubSystemPowerProvider, List<IFilter>>> subSystemPower;
    PathFinder finder = new PathFinder(thisPipe.container, Configs.LOGISTICS_DETECTION_COUNT, Configs.LOGISTICS_DETECTION_LENGTH, localChangeListener);
    power = finder.powerNodes;
    subSystemPower = finder.subPowerProvider;
    adjacent = finder.result;
    Map<ForgeDirection, List<CoreRoutedPipe>> pipeDirections = new HashMap<>();
    for (Entry<CoreRoutedPipe, ExitRoute> entry : adjacent.entrySet()) {
        List<CoreRoutedPipe> list = pipeDirections.get(entry.getValue().exitOrientation);
        if (list == null) {
            list = new ArrayList<>();
            pipeDirections.put(entry.getValue().exitOrientation, list);
        }
        list.add(entry.getKey());
    }
    pipeDirections.entrySet().stream().filter(entry -> entry.getValue().size() > Configs.MAX_UNROUTED_CONNECTIONS).forEach(entry -> entry.getValue().forEach(adjacent::remove));
    listenedPipes.stream().filter(list -> !finder.listenedPipes.contains(list)).forEach(list -> list.remove(localChangeListener));
    listenedPipes = finder.listenedPipes;
    for (CoreRoutedPipe pipe : adjacent.keySet()) {
        if (pipe.stillNeedReplace()) {
            return false;
        }
    }
    boolean[] oldSideDisconnected = sideDisconnected;
    sideDisconnected = new boolean[6];
    checkSecurity(adjacent);
    boolean changed = false;
    for (int i = 0; i < 6; i++) {
        changed |= sideDisconnected[i] != oldSideDisconnected[i];
    }
    if (changed) {
        CoreRoutedPipe pipe = getPipe();
        if (pipe != null) {
            pipe.getWorld().notifyBlocksOfNeighborChange(pipe.getX(), pipe.getY(), pipe.getZ(), pipe.getWorld().getBlock(pipe.getX(), pipe.getY(), pipe.getZ()));
            pipe.refreshConnectionAndRender(false);
        }
        adjacentChanged = true;
    }
    if (_adjacent.size() != adjacent.size()) {
        adjacentChanged = true;
    }
    for (CoreRoutedPipe pipe : _adjacent.keySet()) {
        if (!adjacent.containsKey(pipe)) {
            adjacentChanged = true;
        }
    }
    if (_powerAdjacent != null) {
        if (power == null) {
            adjacentChanged = true;
        } else {
            for (Pair<ILogisticsPowerProvider, List<IFilter>> provider : _powerAdjacent) {
                if (!power.contains(provider)) {
                    adjacentChanged = true;
                }
            }
        }
    }
    if (power != null) {
        if (_powerAdjacent == null) {
            adjacentChanged = true;
        } else {
            for (Pair<ILogisticsPowerProvider, List<IFilter>> provider : power) {
                if (!_powerAdjacent.contains(provider)) {
                    adjacentChanged = true;
                }
            }
        }
    }
    if (_subSystemPowerAdjacent != null) {
        if (subSystemPower == null) {
            adjacentChanged = true;
        } else {
            for (Pair<ISubSystemPowerProvider, List<IFilter>> provider : _subSystemPowerAdjacent) {
                if (!subSystemPower.contains(provider)) {
                    adjacentChanged = true;
                }
            }
        }
    }
    if (subSystemPower != null) {
        if (_subSystemPowerAdjacent == null) {
            adjacentChanged = true;
        } else {
            for (Pair<ISubSystemPowerProvider, List<IFilter>> provider : subSystemPower) {
                if (!_subSystemPowerAdjacent.contains(provider)) {
                    adjacentChanged = true;
                }
            }
        }
    }
    for (Entry<CoreRoutedPipe, ExitRoute> pipe : adjacent.entrySet()) {
        ExitRoute oldExit = _adjacent.get(pipe.getKey());
        if (oldExit == null) {
            adjacentChanged = true;
            break;
        }
        ExitRoute newExit = pipe.getValue();
        if (!newExit.equals(oldExit)) {
            adjacentChanged = true;
            break;
        }
    }
    if (!oldTouchedPipes.equals(finder.touchedPipes)) {
        CacheHolder.clearCache(oldTouchedPipes);
        CacheHolder.clearCache(finder.touchedPipes);
        oldTouchedPipes = finder.touchedPipes;
        BitSet visited = new BitSet(ServerRouter.getBiggestSimpleID());
        visited.set(getSimpleID());
        act(visited, new floodClearCache());
    }
    if (adjacentChanged) {
        HashMap<IRouter, ExitRoute> adjacentRouter = new HashMap<>();
        EnumSet<ForgeDirection> routedexits = EnumSet.noneOf(ForgeDirection.class);
        EnumMap<ForgeDirection, Integer> subpowerexits = new EnumMap<>(ForgeDirection.class);
        for (Entry<CoreRoutedPipe, ExitRoute> pipe : adjacent.entrySet()) {
            adjacentRouter.put(pipe.getKey().getRouter(), pipe.getValue());
            if ((pipe.getValue().connectionDetails.contains(PipeRoutingConnectionType.canRouteTo) || pipe.getValue().connectionDetails.contains(PipeRoutingConnectionType.canRequestFrom) && !routedexits.contains(pipe.getValue().exitOrientation))) {
                routedexits.add(pipe.getValue().exitOrientation);
            }
            if (!subpowerexits.containsKey(pipe.getValue().exitOrientation) && pipe.getValue().connectionDetails.contains(PipeRoutingConnectionType.canPowerSubSystemFrom)) {
                subpowerexits.put(pipe.getValue().exitOrientation, PathFinder.messureDistanceToNextRoutedPipe(getLPPosition(), pipe.getValue().exitOrientation, pipe.getKey().getWorld()));
            }
        }
        _adjacent = Collections.unmodifiableMap(adjacent);
        _adjacentRouter_Old = _adjacentRouter;
        _adjacentRouter = Collections.unmodifiableMap(adjacentRouter);
        if (power != null) {
            _powerAdjacent = Collections.unmodifiableList(power);
        } else {
            _powerAdjacent = null;
        }
        if (subSystemPower != null) {
            _subSystemPowerAdjacent = Collections.unmodifiableList(subSystemPower);
        } else {
            _subSystemPowerAdjacent = null;
        }
        _routedExits = routedexits;
        _subPowerExits = subpowerexits;
        SendNewLSA();
    }
    return adjacentChanged;
}
Also used : LogisticsModule(logisticspipes.modules.abstractmodules.LogisticsModule) PriorityQueue(java.util.PriorityQueue) Quartet(logisticspipes.utils.tuples.Quartet) Particles(logisticspipes.pipefxhandlers.Particles) MainProxy(logisticspipes.proxy.MainProxy) ILogisticsPowerProvider(logisticspipes.api.ILogisticsPowerProvider) Configs(logisticspipes.config.Configs) StackTraceUtil(logisticspipes.utils.StackTraceUtil) Map(java.util.Map) ILPTEInformation(logisticspipes.asm.te.ILPTEInformation) EnumSet(java.util.EnumSet) ITileEntityChangeListener(logisticspipes.asm.te.ITileEntityChangeListener) EnumMap(java.util.EnumMap) FluidResource(logisticspipes.request.resources.FluidResource) CacheHolder(logisticspipes.utils.CacheHolder) ItemIdentifier(logisticspipes.utils.item.ItemIdentifier) Set(java.util.Set) LPTickHandler(logisticspipes.ticks.LPTickHandler) UUID(java.util.UUID) DictResource(logisticspipes.request.resources.DictResource) ForgeDirection(net.minecraftforge.common.util.ForgeDirection) Collectors(java.util.stream.Collectors) DoubleCoordinates(network.rs485.logisticspipes.world.DoubleCoordinates) Info(logisticspipes.utils.StackTraceUtil.Info) List(java.util.List) SimpleServiceLocator(logisticspipes.proxy.SimpleServiceLocator) IFilter(logisticspipes.interfaces.routing.IFilter) Pair(logisticspipes.utils.tuples.Pair) Entry(java.util.Map.Entry) CoreRoutedPipe(logisticspipes.pipes.basic.CoreRoutedPipe) ItemResource(logisticspipes.request.resources.ItemResource) Setter(lombok.Setter) Getter(lombok.Getter) LogisticsTileGenericPipe(logisticspipes.pipes.basic.LogisticsTileGenericPipe) HashMap(java.util.HashMap) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ISubSystemPowerProvider(logisticspipes.interfaces.ISubSystemPowerProvider) AccessLevel(lombok.AccessLevel) IRoutingDebugAdapter(logisticspipes.interfaces.IRoutingDebugAdapter) LPTileEntityObject(logisticspipes.asm.te.LPTileEntityObject) PathFinder(logisticspipes.routing.pathfinder.PathFinder) OneList(logisticspipes.utils.OneList) WeakReference(java.lang.ref.WeakReference) LinkedList(java.util.LinkedList) LPConstants(logisticspipes.LPConstants) DimensionManager(net.minecraftforge.common.DimensionManager) RoutingTableUpdateThread(logisticspipes.ticks.RoutingTableUpdateThread) Iterator(java.util.Iterator) World(net.minecraft.world.World) IResource(logisticspipes.request.resources.IResource) Lock(java.util.concurrent.locks.Lock) PipeItemsFirewall(logisticspipes.pipes.PipeItemsFirewall) TileEntity(net.minecraft.tileentity.TileEntity) BitSet(java.util.BitSet) Collections(java.util.Collections) HashMap(java.util.HashMap) CoreRoutedPipe(logisticspipes.pipes.basic.CoreRoutedPipe) ForgeDirection(net.minecraftforge.common.util.ForgeDirection) ILogisticsPowerProvider(logisticspipes.api.ILogisticsPowerProvider) List(java.util.List) ArrayList(java.util.ArrayList) OneList(logisticspipes.utils.OneList) LinkedList(java.util.LinkedList) EnumMap(java.util.EnumMap) ISubSystemPowerProvider(logisticspipes.interfaces.ISubSystemPowerProvider) Pair(logisticspipes.utils.tuples.Pair) BitSet(java.util.BitSet) PathFinder(logisticspipes.routing.pathfinder.PathFinder) IFilter(logisticspipes.interfaces.routing.IFilter)

Aggregations

EnumMap (java.util.EnumMap)156 Map (java.util.Map)30 HashMap (java.util.HashMap)23 Collection (java.util.Collection)22 ArrayList (java.util.ArrayList)21 Test (org.junit.Test)18 DecodeHintType (com.google.zxing.DecodeHintType)17 List (java.util.List)14 IOException (java.io.IOException)12 EncodeHintType (com.google.zxing.EncodeHintType)11 BitMatrix (com.google.zxing.common.BitMatrix)11 Set (java.util.Set)11 Occur (org.apache.lucene.search.BooleanClause.Occur)10 Iterator (java.util.Iterator)9 ImmutableMap (com.google.common.collect.ImmutableMap)7 TreeMap (java.util.TreeMap)7 NoSuchElementException (java.util.NoSuchElementException)6 Bundle (android.os.Bundle)5 QRCodeWriter (com.google.zxing.qrcode.QRCodeWriter)5 URL (java.net.URL)5