text:
package org.apache.ignite.internal.commandline.indexreader;

import java.io.File;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import org.apache.ignite.IgniteCheckedException;
import org.apache.ignite.IgniteException;
import org.apache.ignite.IgniteLogger;
import org.apache.ignite.cache.QueryEntity;
import org.apache.ignite.cache.QueryIndex;
import org.apache.ignite.internal.cache.query.index.IndexProcessor;
import org.apache.ignite.internal.cache.query.index.sorted.IndexKeyType;
import org.apache.ignite.internal.cache.query.index.sorted.IndexKeyTypeSettings;
import org.apache.ignite.internal.cache.query.index.sorted.inline.InlineIndexKeyType;
import org.apache.ignite.internal.cache.query.index.sorted.inline.InlineIndexKeyTypeRegistry;
import org.apache.ignite.internal.cache.query.index.sorted.inline.io.AbstractInlineLeafIO;
import org.apache.ignite.internal.cache.query.index.sorted.inline.io.InlineIO;
import org.apache.ignite.internal.cache.query.index.sorted.inline.types.NullableInlineIndexKeyType;
import org.apache.ignite.internal.commandline.CommandHandler;
import org.apache.ignite.internal.commandline.ProgressPrinter;
import org.apache.ignite.internal.commandline.argument.parser.CLIArgumentParser;
import org.apache.ignite.internal.commandline.indexreader.ScanContext.PagesStatistic;
import org.apache.ignite.internal.commandline.systemview.SystemViewCommand;
import org.apache.ignite.internal.pagemem.PageIdAllocator;
import org.apache.ignite.internal.pagemem.PageUtils;
import org.apache.ignite.internal.processors.cache.StoredCacheData;
import org.apache.ignite.internal.processors.cache.persistence.IndexStorageImpl;
import org.apache.ignite.internal.processors.cache.persistence.StorageException;
import org.apache.ignite.internal.processors.cache.persistence.file.AsyncFileIOFactory;
import org.apache.ignite.internal.processors.cache.persistence.file.FilePageStore;
import org.apache.ignite.internal.processors.cache.persistence.file.FilePageStoreManager;
import org.apache.ignite.internal.processors.cache.persistence.file.FilePageStoreV2;
import org.apache.ignite.internal.processors.cache.persistence.file.FileVersionCheckingFactory;
import org.apache.ignite.internal.processors.cache.persistence.freelist.io.PagesListMetaIO;
import org.apache.ignite.internal.processors.cache.persistence.freelist.io.PagesListNodeIO;
import org.apache.ignite.internal.processors.cache.persistence.tree.io.AbstractDataPageIO;
import org.apache.ignite.internal.processors.cache.persistence.tree.io.BPlusIO;
import org.apache.ignite.internal.processors.cache.persistence.tree.io.BPlusInnerIO;
import org.apache.ignite.internal.processors.cache.persistence.tree.io.BPlusLeafIO;
import org.apache.ignite.internal.processors.cache.persistence.tree.io.BPlusMetaIO;
import org.apache.ignite.internal.processors.cache.persistence.tree.io.DataPagePayload;
import org.apache.ignite.internal.processors.cache.persistence.tree.io.PageIO;
import org.apache.ignite.internal.processors.cache.persistence.tree.io.PageMetaIO;
import org.apache.ignite.internal.processors.cache.persistence.tree.io.PagePartitionMetaIO;
import org.apache.ignite.internal.processors.cache.persistence.tree.io.TrackingPageIO;
import org.apache.ignite.internal.processors.cache.persistence.wal.crc.IgniteDataIntegrityViolationException;
import org.apache.ignite.internal.processors.cache.tree.AbstractDataLeafIO;
import org.apache.ignite.internal.processors.cache.tree.PendingRowIO;
import org.apache.ignite.internal.processors.cache.tree.RowLinkIO;
import org.apache.ignite.internal.processors.query.QueryUtils;
import org.apache.ignite.internal.util.GridLongList;
import org.apache.ignite.internal.util.GridStringBuilder;
import org.apache.ignite.internal.util.lang.GridPlainClosure2;
import org.apache.ignite.internal.util.lang.GridTuple3;
import org.apache.ignite.internal.util.lang.IgnitePair;
import org.apache.ignite.internal.util.lang.RunnableX;
import org.apache.ignite.internal.util.typedef.F;
import org.apache.ignite.internal.util.typedef.internal.CU;
import org.apache.ignite.internal.util.typedef.internal.U;
import org.apache.ignite.lang.IgniteBiTuple;
import org.jetbrains.annotations.Nullable;

import static java.lang.Integer.parseInt;
import static java.lang.String.format;
import static java.util.Arrays.asList;
import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
import static org.apache.ignite.configuration.DataStorageConfiguration.DFLT_PAGE_SIZE;
import static org.apache.ignite.internal.commandline.argument.parser.CLIArgument.mandatoryArg;
import static org.apache.ignite.internal.commandline.argument.parser.CLIArgument.optionalArg;
import static org.apache.ignite.internal.pagemem.PageIdAllocator.FLAG_DATA;
import static org.apache.ignite.internal.pagemem.PageIdAllocator.FLAG_IDX;
import static org.apache.ignite.internal.pagemem.PageIdAllocator.INDEX_PARTITION;
import static org.apache.ignite.internal.pagemem.PageIdUtils.flag;
import static org.apache.ignite.internal.pagemem.PageIdUtils.itemId;
import static org.apache.ignite.internal.pagemem.PageIdUtils.pageId;
import static org.apache.ignite.internal.pagemem.PageIdUtils.pageIndex;
import static org.apache.ignite.internal.pagemem.PageIdUtils.partId;
import static org.apache.ignite.internal.processors.cache.persistence.file.FilePageStoreManager.CACHE_DATA_FILENAME;
import static org.apache.ignite.internal.processors.cache.persistence.file.FilePageStoreManager.INDEX_FILE_NAME;
import static org.apache.ignite.internal.processors.cache.persistence.file.FilePageStoreManager.PART_FILE_TEMPLATE;
import static org.apache.ignite.internal.util.GridUnsafe.allocateBuffer;
import static org.apache.ignite.internal.util.GridUnsafe.bufferAddress;
import static org.apache.ignite.internal.util.GridUnsafe.freeBuffer;
import static org.apache.ignite.internal.visor.systemview.VisorSystemViewTask.SimpleType.NUMBER;
import static org.apache.ignite.internal.visor.systemview.VisorSystemViewTask.SimpleType.STRING;

/**
 * Offline reader for index files.
 */
public class IgniteIndexReader implements AutoCloseable {
    /** */
    public static final String META_TREE_NAME = "MetaTree";

    /** */
    public static final String RECURSIVE_TRAVERSE_NAME = "<RECURSIVE> ";

    /** */
    public static final String HORIZONTAL_SCAN_NAME = "<HORIZONTAL> ";

    /** */
    private static final String PAGE_LISTS_PREFIX = "<PAGE_LIST> ";

    /** */
    public static final String ERROR_PREFIX = "<ERROR> ";

    /** */
    private static final String DIR_ARG = "--dir";

    /** */
    private static final String PART_CNT_ARG = "--part-cnt";

    /** */
    private static final String PAGE_SIZE_ARG = "--page-size";

    /** */
    private static final String PAGE_STORE_VER_ARG = "--page-store-ver";

    /** */
    private static final String INDEXES_ARG = "--indexes";

    /** */
    private static final String CHECK_PARTS_ARG = "--check-parts";

    /** */
    private static final Pattern CACHE_TYPE_ID_INDEX_SEARCH_PATTERN =
        Pattern.compile("(?<id>[-0-9]{1,15})_(?<typeId>[-0-9]{1,15})_(?<indexName>.*)##.*");

    /** */
    private static final Pattern CACHE_TYPE_ID_SEARCH_PATTERN =
        Pattern.compile("(?<id>[-0-9]{1,15})_(?<typeId>[-0-9]{1,15})_.*");

    /** */
    private static final Pattern CACHE_ID_SEARCH_PATTERN =
        Pattern.compile("(?<id>[-0-9]{1,15})_.*");

    /** */
    private static final int MAX_ERRORS_CNT = 10;

    /** */
    static final int UNKNOWN_CACHE = -1;

    static {
        IndexProcessor.registerIO();
    }

    /** Directory with data(partitions and index). */
    private final File root;

    /** Page size. */
    private final int pageSize;

    /** Partition count. */
    private final int partCnt;

    /** Check cache data tree in partition files and it's consistency with indexes. */
    private final boolean checkParts;

    /** Index name filter, if {@code null} then is not used. */
    @Nullable private final Predicate<String> idxFilter;

    /** Logger. */
    private final IgniteLogger log;

    /** Page store of {@link FilePageStoreManager#INDEX_FILE_NAME}. */
    private final FilePageStore idxStore;

    /** Partitions page stores, may contains {@code null}. */
    private final FilePageStore[] partStores;

    /** */
    private final Map<Integer, StoredCacheData> storedCacheData = new HashMap<>();

    /** */
    private final Set<Integer> missingPartitions = new HashSet<>();

    /** */
    private final Set<Long> pageIds = new HashSet<>();

    /** */
    private final InnerPageVisitor innerPageVisitor = new InnerPageVisitor();

    /** */
    private final LeafPageVisitor leafPageVisitor = new LeafPageVisitor();

    /** */
    private final MetaPageVisitor metaPageVisitor = new MetaPageVisitor();

    /** */
    private final LevelsPageVisitor levelsPageVisitor = new LevelsPageVisitor();

    /** */
    private final Map<String, GridTuple3<Integer, Integer, String>> cacheTypeIds = new HashMap<>();

    /**
     * Constructor.
     *
     * @param pageSize Page size.
     * @param partCnt Page count.
     * @param filePageStoreVer Version of file page store.
     * @param checkParts Check cache data tree in partition files and it's consistency with indexes.
     * @param root Root directory.
     * @param idxFilter Index name filter, if {@code null} then is not used.
     * @param log Logger.
     * @throws IgniteCheckedException If failed.
     */
    public IgniteIndexReader(
        int pageSize,
        int partCnt,
        int filePageStoreVer,
        File root,
        @Nullable Predicate<String> idxFilter,
        boolean checkParts,
        IgniteLogger log
    ) throws IgniteCheckedException {
        this.pageSize = pageSize;
        this.partCnt = partCnt;
        this.root = root;
        this.checkParts = checkParts;
        this.idxFilter = idxFilter;
        this.log = log;

        FileVersionCheckingFactory storeFactory = new FileVersionCheckingFactory(
            new AsyncFileIOFactory(),
            new AsyncFileIOFactory(),
            () -> pageSize
        ) {
            /** {@inheritDoc} */
            @Override public int latestVersion() {
                return filePageStoreVer;
            }
        };

        idxStore = filePageStore(INDEX_PARTITION, FLAG_IDX, storeFactory);

        if (isNull(idxStore))
            throw new IgniteCheckedException(INDEX_FILE_NAME + " file not found");

        log.info("Analyzing file: " + INDEX_FILE_NAME);

        partStores = new FilePageStore[partCnt];

        for (int i = 0; i < partCnt; i++)
            partStores[i] = filePageStore(i, FLAG_DATA, storeFactory);

        Arrays.stream(root.listFiles(f -> f.getName().endsWith(CACHE_DATA_FILENAME))).forEach(f -> {
            try (ObjectInputStream stream = new ObjectInputStream(Files.newInputStream(f.toPath()))) {
                StoredCacheData data = (StoredCacheData)stream.readObject();

                storedCacheData.put(CU.cacheId(data.config().getName()), data);
            }
            catch (ClassNotFoundException | IOException e) {
                log.warning("Can't read stored cache data. Inline for this cache will not be analyzed [f=" + f.getName() + ']', e);
            }
        });
    }

    /**
     * Entry point.
     *
     * @param args Arguments.
     */
    public static void main(String[] args) {
        System.out.println("THIS UTILITY MUST BE LAUNCHED ON PERSISTENT STORE WHICH IS NOT UNDER RUNNING GRID!");

        CLIArgumentParser p = new CLIArgumentParser(asList(
            mandatoryArg(
                DIR_ARG,
                "partition directory, where " + INDEX_FILE_NAME + " and (optionally) partition files are located.",
                String.class
            ),
            optionalArg(PART_CNT_ARG, "full partitions count in cache group.", Integer.class, () -> 0),
            optionalArg(PAGE_SIZE_ARG, "page size.", Integer.class, () -> DFLT_PAGE_SIZE),
            optionalArg(PAGE_STORE_VER_ARG, "page store version.", Integer.class, () -> FilePageStoreV2.VERSION),
            optionalArg(INDEXES_ARG, "you can specify index tree names that will be processed, separated by comma " +
                "without spaces, other index trees will be skipped.", String[].class, () -> U.EMPTY_STRS),
            optionalArg(CHECK_PARTS_ARG,
                "check cache data tree in partition files and it's consistency with indexes.", Boolean.class, () -> false)
        ));

        if (args.length == 0) {
            System.out.println(p.usage());

            return;
        }

        p.parse(asList(args).iterator());

        Set<String> idxs = new HashSet<>(asList(p.get(INDEXES_ARG)));

        try (IgniteIndexReader reader = new IgniteIndexReader(
            p.get(PAGE_SIZE_ARG),
            p.get(PART_CNT_ARG),
            p.get(PAGE_STORE_VER_ARG),
            new File(p.<String>get(DIR_ARG)),
            idxs.isEmpty() ? null : idxs::contains,
            p.get(CHECK_PARTS_ARG),
            CommandHandler.setupJavaLogger("index-reader", IgniteIndexReader.class)
        )) {
            reader.readIndex();
        }
        catch (IgniteCheckedException e) {
            throw new IgniteException(INDEX_FILE_NAME + " scan problem", e);
        }
    }

    /** Read index file. */
    public void readIndex() throws IgniteCheckedException {
        long pagesCnt = (idxStore.size() - idxStore.headerSize()) / pageSize;

        log.info("Partitions files num: " + Arrays.stream(partStores).filter(Objects::nonNull).count());
        log.info("Going to check " + pagesCnt + " pages.");

        long[] indexPartitionRoots = partitionRoots(PageIdAllocator.META_PAGE_ID);

        Map<String, ScanContext> recursiveScans = scanAllTrees(
            "Scan index trees recursively",
            indexPartitionRoots[0],
            CountOnlyStorage::new,
            this::recursiveTreeScan,
            pagesCnt
        );

        Map<String, ScanContext> horizontalScans = scanAllTrees(
            "Scan index trees horizontally",
            indexPartitionRoots[0],
            checkParts ? LinkStorage::new : CountOnlyStorage::new,
            this::horizontalTreeScan,
            pagesCnt
        );

        printScanResults(RECURSIVE_TRAVERSE_NAME, recursiveScans);
        printScanResults(HORIZONTAL_SCAN_NAME, horizontalScans);

        compareScans(recursiveScans, horizontalScans);

        printPagesListsInfo(indexPartitionRoots[1]);

        printSequentialScanInfo(scanIndexSequentially());

        if (checkParts)
            checkParts(horizontalScans);
    }

    /** Traverse all trees in file and return their info. */
    private Map<String, ScanContext> scanAllTrees(
        String caption,
        long metaTreeRoot,
        Supplier<ItemStorage> itemStorageFactory,
        Scanner scanner,
        long pagesCnt
    ) {
        Map<String, ScanContext> ctxs = new LinkedHashMap<>();

        ProgressPrinter progressPrinter = createProgressPrinter(caption, pagesCnt);

        ScanContext metaTreeCtx = scanner.scan(
            metaTreeRoot,
            META_TREE_NAME,
            new ItemsListStorage<IndexStorageImpl.IndexItem>(),
            progressPrinter
        );

        log.info("Going to scan " + metaTreeCtx.items.size() + " trees.");

        ctxs.put(META_TREE_NAME, metaTreeCtx);

        AtomicInteger treeCnt = new AtomicInteger();

        ((ItemsListStorage<IndexStorageImpl.IndexItem>)metaTreeCtx.items).forEach(item -> {
            log.info("Scanning [num=" + treeCnt.incrementAndGet() + ", of=" + metaTreeCtx.items.size() +
                ", idx=" + item.nameString() + "]");

            if (nonNull(idxFilter) && !idxFilter.test(item.nameString()))
                return;

            ScanContext ctx = scanner.scan(
                normalizePageId(item.pageId()),
                item.nameString(),
                itemStorageFactory.get(),
                progressPrinter
            );

            ctxs.put(item.toString(), ctx);
        });

        return ctxs;
    }

    /**
     * Traverse single index tree from root to leafs.
     *
     * @param rootPageId Root page id.
     * @param idx Index name.
     * @param items Items storage.
     * @param printer Progress printer.
     * @return Tree traversal context.
     */
    ScanContext recursiveTreeScan(long rootPageId, String idx, ItemStorage items, ProgressPrinter printer) {
        ScanContext ctx = createContext(idx, filePageStore(rootPageId), items, RECURSIVE_TRAVERSE_NAME, printer);

        metaPageVisitor.readAndVisit(rootPageId, ctx);

        return ctx;
    }

    /**
     * Traverse single index tree by each level horizontally.
     *
     * @param rootPageId Root page id.
     * @param idx Index name.
     * @param items Items storage.
     * @param printer Progress printer.
     * @return Tree traversal context.
     */
    private ScanContext horizontalTreeScan(long rootPageId, String idx, ItemStorage items, ProgressPrinter printer) {
        ScanContext ctx = createContext(idx, filePageStore(rootPageId), items, HORIZONTAL_SCAN_NAME, printer);

        levelsPageVisitor.readAndVisit(rootPageId, ctx);

        return ctx;
    }

    /**
     * Gets info about page lists.
     *
     * @param metaPageListId Page list meta id.
     * @return Page list info.
     */
    private PageListsInfo pageListsInfo(long metaPageListId) throws IgniteCheckedException {
        return doWithBuffer((buf, addr) -> {
            Map<IgniteBiTuple<Long, Integer>, List<Long>> bucketsData = new HashMap<>();

            Map<Class<? extends PageIO>, PagesStatistic> stats = new HashMap<>();

            long pagesCnt = 0;
            long errCnt = 0;
            long currMetaPageId = metaPageListId;

            while (currMetaPageId != 0) {
                try {
                    PagesListMetaIO io = readPage(idxStore, currMetaPageId, buf);

                    ScanContext.addToStats(io, stats, 1, addr, idxStore.getPageSize());

                    Map<Integer, GridLongList> data = new HashMap<>();

                    io.getBucketsData(addr, data);

                    for (Map.Entry<Integer, GridLongList> e : data.entrySet()) {
                        List<Long> listIds = LongStream.of(e.getValue().array())
                            .map(IgniteIndexReader::normalizePageId)
                            .boxed()
                            .collect(toList());

                        for (Long listId : listIds) {
                            try {
                                pagesCnt += visitPageList(listId, stats);
                            }
                            catch (Exception err) {
                                errCnt++;
                                ScanContext.onError(log, PAGE_LISTS_PREFIX, listId, err.getMessage());
                            }
                        }

                        bucketsData.put(F.t(currMetaPageId, e.getKey()), listIds);
                    }

                    currMetaPageId = io.getNextMetaPageId(addr);
                }
                catch (Exception err) {
                    errCnt++;
                    ScanContext.onError(log, PAGE_LISTS_PREFIX, currMetaPageId, err.getMessage());

                    break;
                }
            }

            return new PageListsInfo(bucketsData, pagesCnt, stats, errCnt);
        });
    }

    /**
     * Visit single page list.
     *
     * @param listStartPageId Id of the start page of the page list.
     * @param stats Page types statistics.
     * @return List of page ids.
     */
    private long visitPageList(long listStartPageId, Map<Class<? extends PageIO>, PagesStatistic> stats) throws IgniteCheckedException {
        return doWithBuffer((nodeBuf, nodeAddr) -> doWithBuffer((pageBuf, pageAddr) -> {
            long res = 0;

            long currPageId = listStartPageId;

            while (currPageId != 0) {
                PagesListNodeIO io = readPage(idxStore, currPageId, nodeBuf);

                ScanContext.addToStats(io, stats, 1, nodeAddr, idxStore.getPageSize());

                ScanContext.addToStats(readPage(idxStore, currPageId, pageBuf), stats, 1, pageAddr, idxStore.getPageSize());

                res += io.getCount(nodeAddr);

                for (int i = 0; i < io.getCount(nodeAddr); i++) {
                    long pageId = normalizePageId(io.getAt(nodeAddr, i));

                    ScanContext.addToStats(readPage(idxStore, pageId, pageBuf), stats, 1, pageAddr, idxStore.getPageSize());
                }

                currPageId = io.getNextId(nodeAddr);
            }

            return res;
        }));
    }

    /**
     * Traverse index file sequentially.
     *
     * @return Traverse results.
     * @throws IgniteCheckedException If failed.
     */
    private ScanContext scanIndexSequentially() throws IgniteCheckedException {
        long pagesNum = (idxStore.size() - idxStore.headerSize()) / pageSize;

        ProgressPrinter progressPrinter = createProgressPrinter("Reading pages sequentially", pagesNum);

        ScanContext ctx = createContext(null, idxStore, new CountOnlyStorage(), "", progressPrinter);

        doWithBuffer((buf, addr) -> {
            for (int i = 0; i < pagesNum; i++) {
                long pageId = -1;

                try {
                    pageId = pageId(INDEX_PARTITION, FLAG_IDX, i);

                    PageIO io = readPage(ctx, pageId, buf, false);

                    if (idxFilter != null)
                        continue;

                    if (io instanceof TrackingPageIO)
                        continue;

                    if (pageIds.contains(normalizePageId(pageId)))
                        continue;

                    ctx.onError(pageId, "Error [step=" + i +
                        ", msg=Possibly orphan " + io.getClass().getSimpleName() + " page" +
                        ", pageId=" + normalizePageId(pageId) + ']');
                }
                catch (Exception | AssertionError e) {
                    ctx.onError(pageId, "Error [step=" + i + ", msg=" + e.getMessage() + ']');
                }
            }

            return null;
        });

        return ctx;
    }

    /**
     * Checks partitions, comparing partition indexes (cache data tree) to indexes given in {@code treesInfo}.
     *
     * @param treesInfo Index trees info to compare cache data tree with.
     * @return Map of errors, bound to partition id.
     */
    private void checkParts(Map<String, ScanContext> treesInfo) {
        log.info("");

        AtomicInteger partWithErrs = new AtomicInteger();
        AtomicInteger errSum = new AtomicInteger();

        ProgressPrinter progressPrinter = createProgressPrinter("Checking partitions", partCnt);

        IntStream.range(0, partCnt).forEach(partId -> {
            progressPrinter.printProgress();

            FilePageStore partStore = partStores[partId];

            if (partStore == null)
                return;

            AtomicInteger errCnt = new AtomicInteger();

            try {
                long partMetaId = pageId(partId, FLAG_DATA, 0);

                doWithBuffer((buf, addr) -> {
                    PagePartitionMetaIO partMetaIO = readPage(partStore, partMetaId, buf);

                    long cacheDataTreeRoot = partMetaIO.getTreeRoot(addr);

                    ScanContext ctx =
                        horizontalTreeScan(cacheDataTreeRoot, "dataTree-" + partId, new ItemsListStorage<>(), null);

                    for (Object dataTreeItem : ctx.items) {
                        CacheAwareLink cacheAwareLink = (CacheAwareLink)dataTreeItem;

                        for (Map.Entry<String, ScanContext> e : treesInfo.entrySet()) {
                            if (e.getKey().equals(META_TREE_NAME))
                                continue;

                            if (cacheAndTypeId(e.getKey()).get1() != cacheAwareLink.cacheId
                                || e.getValue().items.contains(cacheAwareLink))
                                continue;

                            long pageId = pageId(cacheAwareLink.link);

                            errCnt.getAndIncrement();
                            log.error(ERROR_PREFIX + "Entry is missing in index[name=" + e.getKey() +
                                "cacheId=" + cacheAwareLink.cacheId +
                                ", partId=" + partId(pageId) +
                                ", pageIndex=" + pageIndex(pageId) +
                                ", itemId=" + itemId(cacheAwareLink.link) +
                                ", link=" + cacheAwareLink.link + ']');
                        }

                        if (errCnt.get() >= MAX_ERRORS_CNT) {
                            log.error(ERROR_PREFIX + "Too many errors (" + MAX_ERRORS_CNT +
                                ") found for partId=" + partId + ", stopping analysis for this partition.");

                            break;
                        }
                    }

                    return null;
                });
            }
            catch (IgniteCheckedException e) {
                log.error(ERROR_PREFIX + "Partition check failed [partId=" + partId + ']');
            }

            if (errCnt.get() != 0) {
                partWithErrs.getAndIncrement();
                errSum.addAndGet(errCnt.get());
            }
        });

        if (errSum.get() == 0)
            log.info("Partitions check detected no errors.");

        log.info("Partition check finished, total errors: " + errSum.get() +
                ", total problem partitions: " + partWithErrs.get());
    }

    /**
     * Allocates buffer and does some work in closure, then frees the buffer.
     *
     * @param c Closure.
     * @param <T> Result type.
     * @return Result of closure.
     * @throws IgniteCheckedException If failed.
     */
    private <T> T doWithBuffer(GridPlainClosure2<ByteBuffer, Long, T> c) throws IgniteCheckedException {
        ByteBuffer buf = allocateBuffer(pageSize);

        try {
            long addr = bufferAddress(buf);

            return c.apply(buf, addr);
        }
        finally {
            freeBuffer(buf);
        }
    }

    /** */
    private void doWithoutErrors(RunnableX x, ScanContext ctx, long pageId) {
        try {
            x.run();
        }
        catch (Exception | AssertionError e) {
            ctx.onError(pageId, e.getMessage());
        }
    }

    /**
     * Tries to get cache id and type id from index name.
     *
     * @param name Index name.
     * @return Pair of cache id and type id.
     */
    public GridTuple3<Integer, Integer, String> cacheAndTypeId(String name) {
        return cacheTypeIds.computeIfAbsent(name, k -> {
            Matcher xId = CACHE_TYPE_ID_INDEX_SEARCH_PATTERN.matcher(k);

            if (xId.find())
                return new GridTuple3<>(parseInt(xId.group("id")), parseInt(xId.group("typeId")), xId.group("indexName"));

            Matcher mId = CACHE_TYPE_ID_SEARCH_PATTERN.matcher(k);

            if (mId.find())
                return new GridTuple3<>(parseInt(mId.group("id")), parseInt(mId.group("typeId")), null);

            Matcher cId = CACHE_ID_SEARCH_PATTERN.matcher(k);

            if (cId.find())
                return new GridTuple3<>(parseInt(cId.group("id")), 0, null);

            return new GridTuple3<>(0, 0, null);
        });
    }

    /** */
    ScanContext createContext(String idxName, FilePageStore store, ItemStorage items, String prefix, ProgressPrinter printer) {
        GridTuple3<Integer, Integer, String> parsed;

        if (idxName != null)
            parsed = cacheAndTypeId(idxName);
        else
            parsed = new GridTuple3<>(UNKNOWN_CACHE, 0, null);

        return new ScanContext(parsed.get1(), idxName, inlineFieldsCount(parsed), store, items, log, prefix, printer);
    }

    /**
     * Search index definition inside cache query entities.
     *
     * @param parsed Parsed index name.
     * @return Count of inlined fields or {@code 0} if index definition not found.
     * @see QueryEntity
     */
    protected int inlineFieldsCount(GridTuple3<Integer, Integer, String> parsed) {
        if (parsed.get1() == UNKNOWN_CACHE || !storedCacheData.containsKey(parsed.get1()))
            return 0;

        StoredCacheData data = storedCacheData.get(parsed.get1());

        if (Objects.equals(QueryUtils.PRIMARY_KEY_INDEX, parsed.get3())) {
            if (data.queryEntities().size() > 1) {
                log.warning("Can't parse inline for PK index when multiple query entities defined for a cache " +
                    "[idx=" + parsed.get3() + ']');

                return 0;
            }

            QueryEntity qe = data.queryEntities().iterator().next();

            return qe.getKeyFields() == null ? 1 : qe.getKeyFields().size();
        }

        QueryIndex idx = null;

        for (QueryEntity qe : data.queryEntities()) {
            for (QueryIndex idx0 : qe.getIndexes()) {
                if (Objects.equals(idx0.getName(), parsed.get3())) {
                    idx = idx0;

                    break;
                }
            }

            if (idx != null)
                break;
        }

        if (idx == null) {
            log.warning("Can't find index definition. Inline information not available [idx=" + parsed.get3() + ']');

            return 0;
        }

        return idx.getFields().size();
    }

    /** */
    ProgressPrinter createProgressPrinter(String caption, long total) {
        return new ProgressPrinter(System.out, caption, total);
    }

    /**
     * @param rootPageId Root page id.
     * @return File page store of given partition.
     */
    FilePageStore filePageStore(long rootPageId) {
        int partId = partId(rootPageId);

        return partId == INDEX_PARTITION ? idxStore : partStores[partId];
    }

    /** */
    static long normalizePageId(long pageId) {
        return pageId(partId(pageId), flag(pageId), pageIndex(pageId));
    }

    /** */
    private <I extends PageIO> I readPage(FilePageStore store, long pageId, ByteBuffer buf) throws IgniteCheckedException {
        return readPage(store, pageId, buf, true);
    }

    /**
     * Reads pages into buffer.
     *
     * @param store Source for reading pages.
     * @param pageId Page ID.
     * @param buf Buffer.
     * @param addToPageIds If {@code true} then add page ID to global set.
     */
    private <I extends PageIO> I readPage(
        FilePageStore store,
        long pageId,
        ByteBuffer buf,
        boolean addToPageIds
    ) throws IgniteCheckedException {
        try {
            store.read(pageId, (ByteBuffer)buf.rewind(), false);

            long addr = bufferAddress(buf);

            if (store == idxStore && addToPageIds)
                pageIds.add(normalizePageId(pageId));

            I io = PageIO.getPageIO(addr);

            return io;
        }
        catch (IgniteDataIntegrityViolationException | IllegalArgumentException e) {
            // Replacing exception due to security reasons, as IgniteDataIntegrityViolationException prints page content.
            // Catch IllegalArgumentException for output page information.
            String msg = e.getMessage();

            throw new IgniteException("Failed to read page, id=" + pageId + ", idx=" + pageIndex(pageId) +
                ", err=" + (msg.length() > 100 ? msg.substring(0, 97) + "..." : msg) + ", file=" + store.getFileAbsolutePath());
        }
    }

    /** */
    protected <I extends PageIO> I readPage(ScanContext ctx, long pageId, ByteBuffer buf) throws IgniteCheckedException {
        return readPage(ctx, pageId, buf, true);
    }

    /** */
    protected <I extends PageIO> I readPage(
        ScanContext ctx,
        long pageId,
        ByteBuffer buf,
        boolean addToPageIds
    ) throws IgniteCheckedException {
        try {
            final I io = readPage(ctx.store, pageId, buf, addToPageIds);

            ctx.addToStats(io, bufferAddress(buf));

            return io;
        }
        finally {
            ctx.progress();
        }
    }

    /**
     * @return Tuple consisting of meta tree root page and pages list root page.
     * @throws IgniteCheckedException If failed.
     */
    long[] partitionRoots(long metaPageId) throws IgniteCheckedException {
        return doWithBuffer((buf, addr) -> {
            PageMetaIO pageMetaIO = readPage(filePageStore(metaPageId), metaPageId, buf);

            return new long[] {
                normalizePageId(pageMetaIO.getTreeRoot(addr)),
                normalizePageId(pageMetaIO.getReuseListRoot(addr))
            };
        });
    }

    /**
     * Compares result of traversals.
     *
     * @param recursiveScans Traversal from root to leafs.
     * @param horizontalScans Traversal using horizontal scan.
     */
    private void compareScans(
        Map<String, ScanContext> recursiveScans,
        Map<String, ScanContext> horizontalScans
    ) {
        AtomicInteger errCnt = new AtomicInteger();

        recursiveScans.forEach((name, rctx) -> {
            ScanContext hctx = horizontalScans.get(name);

            if (hctx == null) {
                errCnt.incrementAndGet();
                log.error("Tree was detected in " + RECURSIVE_TRAVERSE_NAME + " but absent in  "
                    + HORIZONTAL_SCAN_NAME + ": " + name);

                return;
            }

            if (rctx.items.size() != hctx.items.size()) {
                errCnt.incrementAndGet();
                log.error(compareError("items", name, rctx.items.size(), hctx.items.size(), null));
            }

            rctx.stats.forEach((cls, stat) -> {
                long scanCnt = hctx.stats.getOrDefault(cls, new PagesStatistic()).cnt;

                if (scanCnt != stat.cnt) {
                    errCnt.incrementAndGet();
                    log.error(compareError("pages", name, stat.cnt, scanCnt, cls));
                }
            });

            hctx.stats.forEach((cls, stat) -> {
                if (!rctx.stats.containsKey(cls)) {
                    errCnt.incrementAndGet();
                    log.error(compareError("pages", name, 0, stat.cnt, cls));
                }
            });
        });

        horizontalScans.forEach((name, hctx) -> {
            if (!recursiveScans.containsKey(name)) {
                errCnt.incrementAndGet();
                log.error("Tree was detected in " + HORIZONTAL_SCAN_NAME + " but absent in  "
                        + RECURSIVE_TRAVERSE_NAME + ": " + name);
            }
        });

        log.info("Comparing traversals detected " + errCnt + " errors.");
        log.info("------------------");
    }

    /** Prints sequential file scan results. */
    private void printSequentialScanInfo(ScanContext ctx) {
        printIoStat("", "---- These pages types were encountered during sequential scan:", ctx.stats);

        log.info("----");

        SystemViewCommand.printTable(
            null,
            Arrays.asList(STRING, NUMBER),
            Arrays.asList(
                Arrays.asList("Total pages encountered during sequential scan:", ctx.stats.values().stream().mapToLong(a -> a.cnt).sum()),
                Arrays.asList("Total errors occurred during sequential scan: ", ctx.errCnt)
            ),
            log
        );

        if (idxFilter != null)
            log.info("Orphan pages were not reported due to --indexes filter.");

        log.info("Note that some pages can be occupied by meta info, tracking info, etc., so total page count can differ " +
            "from count of pages found in index trees and page lists.");
    }

    /** Prints traversal info. */
    private void printScanResults(String prefix, Map<String, ScanContext> ctxs) {
        log.info(prefix + "Tree traversal results");

        Map<Class<? extends PageIO>, PagesStatistic> stats = new HashMap<>();

        int totalErr = 0;

        // Map (cacheId, typeId) -> (map idxName -> size))
        Map<IgnitePair<Integer>, Map<String, Long>> cacheIdxSizes = new HashMap<>();

        for (Map.Entry<String, ScanContext> e : ctxs.entrySet()) {
            String idxName = e.getKey();
            ScanContext ctx = e.getValue();

            log.info(prefix + "-----");
            log.info(prefix + "Index tree: " + idxName);
            printIoStat(prefix, "---- Page stat:", ctx.stats);

            ctx.stats.forEach((cls, stat) -> ScanContext.addToStats(cls, stats, stat));

            log.info(prefix + "---- Count of items found in leaf pages: " + ctx.items.size());

            boolean hasInlineStat = ctx.inline != null && IntStream.of(ctx.inline).anyMatch(i -> i > 0);

            if (hasInlineStat) {
                log.info(prefix + "---- Inline usage statistics [inlineSize=" + ctx.inline.length + " bytes]");

                List<List<?>> data = new ArrayList<>(ctx.inline.length);
                for (int i = 0; i < ctx.inline.length; i++) {
                    if (ctx.inline[i] == 0)
                        continue;

                    data.add(Arrays.asList(prefix, i + 1, ctx.inline[i]));
                }

                SystemViewCommand.printTable(
                    Arrays.asList(prefix, "Used bytes", "Entries count"),
                    Arrays.asList(STRING, NUMBER, NUMBER),
                    data,
                    log
                );
            }

            if (ctx.errCnt == 0)
                log.info(prefix + "No errors occurred while traversing.");

            totalErr += ctx.errCnt;

            GridTuple3<Integer, Integer, String> parsed = cacheAndTypeId(idxName);

            cacheIdxSizes
                .computeIfAbsent(new IgnitePair<>(parsed.get1(), parsed.get2()), k -> new HashMap<>())
                .put(idxName, ctx.items.size());
        }

        log.info(prefix + "----");

        printIoStat(prefix, "Total page stat collected during trees traversal:", stats);

        log.info("");

        boolean sizeConsistencyErrorsFound = false;

        for (Map.Entry<IgnitePair<Integer>, Map<String, Long>> entry : cacheIdxSizes.entrySet()) {
            IgnitePair<Integer> cacheTypeId = entry.getKey();
            Map<String, Long> idxSizes = entry.getValue();

            if (idxSizes.values().stream().distinct().count() > 1) {
                sizeConsistencyErrorsFound = true;

                totalErr++;

                log.error("Index size inconsistency: cacheId=" + cacheTypeId.get1() + ", typeId=" + cacheTypeId.get2());

                idxSizes.forEach((name, size) -> log.error("     Index name: " + name + ", size=" + size));
            }
        }

        if (!sizeConsistencyErrorsFound)
            log.info(prefix + "No index size consistency errors found.");

        log.info("");

        SystemViewCommand.printTable(
            null,
            Arrays.asList(STRING, NUMBER),
            Arrays.asList(
                Arrays.asList(prefix + "Total trees: ", ctxs.keySet().size()),
                Arrays.asList(prefix + "Total pages found in trees: ", stats.values().stream().mapToLong(a -> a.cnt).sum()),
                Arrays.asList(prefix + "Total errors during trees traversal: ", totalErr)
            ),
            log
        );

        log.info("");
        log.info("------------------");
    }

    /**
     * Prints page lists info.
     *
     * @param reuseListRoot Page id.
     */
    private void printPagesListsInfo(long reuseListRoot) throws IgniteCheckedException {
        if (reuseListRoot == 0) {
            log.error("No page lists meta info found.");

            return;
        }

        PageListsInfo pageListsInfo = pageListsInfo(reuseListRoot);

        log.info(PAGE_LISTS_PREFIX + "Page lists info.");

        if (!pageListsInfo.bucketsData.isEmpty())
            log.info(PAGE_LISTS_PREFIX + "---- Printing buckets data:");

        pageListsInfo.bucketsData.forEach((bucket, bucketData) -> {
            GridStringBuilder sb = new GridStringBuilder(PAGE_LISTS_PREFIX)
                .a("List meta id=")
                .a(bucket.get1())
                .a(", bucket number=")
                .a(bucket.get2())
                .a(", lists=[")
                .a(bucketData.stream().map(IgniteIndexReader::normalizePageId).map(String::valueOf).collect(joining(", ")))
                .a("]");

            log.info(sb.toString());
        });

        printIoStat(PAGE_LISTS_PREFIX, "---- Page stat:", pageListsInfo.stats);

        log.info("");

        SystemViewCommand.printTable(
            null,
            Arrays.asList(STRING, NUMBER),
            Arrays.asList(
                Arrays.asList(PAGE_LISTS_PREFIX + "Total index pages found in lists:", pageListsInfo.pagesCnt),
                Arrays.asList(PAGE_LISTS_PREFIX + "Total errors during lists scan:", pageListsInfo.errCnt)
            ),
            log
        );

        log.info("------------------");
    }

    /** */
    private String compareError(String itemName, String idxName, long fromRoot, long scan, Class<? extends PageIO> io) {
        return format(
            "Different count of %s; index: %s, %s:%s, %s:%s" + (io == null ? "" : ", pageType: " + io.getSimpleName()),
            itemName,
            idxName,
            RECURSIVE_TRAVERSE_NAME,
            fromRoot,
            HORIZONTAL_SCAN_NAME,
            scan
        );
    }

    /** */
    private void printIoStat(String prefix, String caption, Map<Class<? extends PageIO>, PagesStatistic> stats) {
        if (caption != null)
            log.info(prefix + caption + (stats.isEmpty() ? " empty" : ""));

        if (stats.isEmpty())
            return;

        List<List<?>> data = new ArrayList<>(stats.size());

        stats.forEach((cls, stat) -> data.add(Arrays.asList(
            prefix + cls.getSimpleName(),
            stat.cnt,
            String.format("%.2f", ((double)stat.freeSpace) / U.KB),
            String.format("%.2f", (stat.freeSpace * 100.0d) / (pageSize * stat.cnt))
        )));

        Collections.sort(data, Comparator.comparingLong(l -> (Long)l.get(1)));

        SystemViewCommand.printTable(
            Arrays.asList(prefix + "Type", "Pages", "Free space (Kb)", "Free space (%)"),
            Arrays.asList(STRING, NUMBER, NUMBER, NUMBER),
            data,
            log
        );
    }

    /**
     * Creating new {@link FilePageStore} and initializing it.
     * It can return {@code null} if partition file were not found, for example: node should not contain it by affinity.
     *
     * @param partId Partition ID.
     * @param type Data type, can be {@link PageIdAllocator#FLAG_IDX} or {@link PageIdAllocator#FLAG_DATA}.
     * @param storeFactory Store factory.
     * @return New instance of {@link FilePageStore} or {@code null}.
     * @throws IgniteCheckedException If there are errors when creating or initializing {@link FilePageStore}.
     */
    @Nullable private FilePageStore filePageStore(
        int partId,
        byte type,
        FileVersionCheckingFactory storeFactory
    ) throws IgniteCheckedException {
        File file = new File(root, partId == INDEX_PARTITION ? INDEX_FILE_NAME : format(PART_FILE_TEMPLATE, partId));

        if (!file.exists())
            return null;

        FilePageStore filePageStore = (FilePageStore)storeFactory.createPageStore(type, file, l -> {});

        filePageStore.ensure();

        return filePageStore;
    }

    /** {@inheritDoc} */
    @Override public void close() throws StorageException {
        idxStore.stop(false);

        for (FilePageStore store : partStores) {
            if (nonNull(store))
                store.stop(false);
        }
    }

    /** */
    private interface Scanner {
        /** */
        ScanContext scan(long rootId, String idx, ItemStorage items, ProgressPrinter printer);
    }

    /** Processor for page IOs. */
    private abstract class TreePageVisitor {
        /** */
        protected void readAndVisit(long pageId, ScanContext ctx) {
            doWithoutErrors(() -> doWithBuffer((buf, addr) -> {
                final PageIO io = readPage(ctx, pageId, buf);

                if (io instanceof BPlusLeafIO)
                    leafPageVisitor.visit(addr, ctx);
                else if (io instanceof BPlusInnerIO)
                    innerPageVisitor.visit(addr, ctx);
                else
                    throw new IllegalArgumentException("Unknown io [io=" + io.getClass().getSimpleName() + ']');

                return null;
            }), ctx, pageId);
        }
    }

    /** */
    private class MetaPageVisitor extends TreePageVisitor {
        /** {@inheritDoc} */
        @Override protected void readAndVisit(long pageId, ScanContext ctx) {
            doWithoutErrors(() -> {
                long firstPageId = doWithBuffer((buf, addr) -> {
                    BPlusMetaIO io = readPage(ctx, pageId, buf);

                    return io.getFirstPageId(addr, io.getRootLevel(addr));
                });

                super.readAndVisit(firstPageId, ctx);
            }, ctx, pageId);
        }
    }

    /** */
    private class LevelsPageVisitor extends TreePageVisitor {
        /** {@inheritDoc} */
        @Override protected void readAndVisit(long rootPageId, ScanContext ctx) {
            doWithoutErrors(() -> doWithBuffer((buf, addr) -> {
                PageIO pageIO = readPage(ctx, rootPageId, buf);

                if (!(pageIO instanceof BPlusMetaIO))
                    throw new IgniteException("Root page is not meta, pageId=" + rootPageId);

                BPlusMetaIO metaIO = (BPlusMetaIO)pageIO;

                int lvlsCnt = metaIO.getLevelsCount(addr);

                long[] firstPageIds = IntStream.range(0, lvlsCnt).mapToLong(i -> metaIO.getFirstPageId(addr, i)).toArray();

                for (int i = 0; i < lvlsCnt; i++) {
                    long pageId = firstPageIds[i];

                    try {
                        while (pageId > 0) {
                            pageIO = readPage(ctx, pageId, buf);

                            if (i == 0 && !(pageIO instanceof BPlusLeafIO))
                                throw new IgniteException("Not-leaf page found on leaf level [pageId=" + pageId + ", level=0]");

                            if (!(pageIO instanceof BPlusIO))
                                throw new IgniteException("Not-BPlus page found [pageId=" + pageId + ", level=" + i + ']');

                            if (pageIO instanceof BPlusLeafIO)
                                leafPageVisitor.visit(addr, ctx);

                            pageId = ((BPlusIO<?>)pageIO).getForward(addr);
                        }
                    }
                    catch (Exception | AssertionError e) {
                        ctx.onError(pageId, e.getMessage());
                    }
                }

                return null;
            }), ctx, rootPageId);
        }
    }

    /** */
    private class InnerPageVisitor extends TreePageVisitor {
        /** */
        public void visit(long addr, ScanContext ctx) throws IgniteCheckedException {
            BPlusInnerIO<?> io = PageIO.getPageIO(addr);

            for (long id : children(io, addr))
                readAndVisit(id, ctx);
        }

        /** */
        private long[] children(BPlusInnerIO<?> io, long addr) {
            int cnt = io.getCount(addr);

            if (cnt == 0) {
                long left = io.getLeft(addr, 0);

                return left == 0 ? U.EMPTY_LONGS : new long[] {left};
            }

            long[] children = new long[cnt + 1];

            for (int i = 0; i < cnt; i++)
                children[i] = io.getLeft(addr, i);

            children[cnt] = io.getRight(addr, cnt - 1);

            return children;
        }
    }

    /** */
    private class LeafPageVisitor extends TreePageVisitor {
        /** */
        public void visit(long addr, ScanContext ctx) throws IgniteCheckedException {
            List<Object> items = new LinkedList<>();

            BPlusLeafIO<?> io = PageIO.getPageIO(addr);

            if (io instanceof AbstractInlineLeafIO)
                visitInline(addr, (AbstractInlineLeafIO)io, ctx);

            doWithoutErrors(() -> {
                for (int i = 0; i < io.getCount(addr); i++) {
                    if (io instanceof IndexStorageImpl.MetaStoreLeafIO)
                        items.add(((BPlusIO<IndexStorageImpl.IndexItem>)io).getLookupRow(null, addr, i));
                    else
                        items.add(leafItem(io, addr, i, ctx));
                }
            }, ctx, PageIO.getPageId(addr));

            ctx.onLeafPage(PageIO.getPageId(addr), items);
        }

        /** */
        private void visitInline(long addr, AbstractInlineLeafIO io, ScanContext ctx) {
            int inlineSz = ((InlineIO)io).inlineSize();

            if (ctx.inlineFldCnt == 0)
                return;

            if (ctx.inline == null)
                ctx.inline = new int[inlineSz];

            IndexKeyTypeSettings settings = new IndexKeyTypeSettings();

            for (int i = 0; i < io.getCount(addr); i++) {
                int itemOff = io.offset(i);
                int realInlineSz = 0;
                int fldCnt = 0;

                while (realInlineSz < inlineSz && fldCnt < ctx.inlineFldCnt) {
                    int type0 = PageUtils.getByte(addr, itemOff + realInlineSz);

                    IndexKeyType idxKeyType;

                    try {
                        idxKeyType = IndexKeyType.forCode(type0);
                    }
                    catch (Exception | AssertionError t) {
                        if (log.isDebugEnabled())
                            log.debug("Unknown index key type [type=" + type0 + ']');

                        break;
                    }

                    if (idxKeyType == IndexKeyType.UNKNOWN || idxKeyType == IndexKeyType.NULL) {
                        realInlineSz += 1;

                        continue;
                    }

                    InlineIndexKeyType type = InlineIndexKeyTypeRegistry.get(idxKeyType, settings);

                    if (type == null) {
                        if (log.isDebugEnabled())
                            log.debug("Unknown inline type [type=" + type0 + ']');

                        break;
                    }

                    if (type.keySize() == UNKNOWN_CACHE) {
                        try {
                            // Assuming all variable length keys written using `writeBytes` method.
                            byte[] bytes = NullableInlineIndexKeyType.readBytes(addr, itemOff + realInlineSz);

                            realInlineSz += Short.BYTES; /* size of the array is short number. */
                            realInlineSz += bytes.length;
                        }
                        catch (Exception | AssertionError e) {
                            log.warning("Error while reading inline [msg=" + e.getMessage() + ']');

                            break;
                        }
                    }
                    else
                        realInlineSz += type.keySize();

                    realInlineSz++; // One more byte for type.
                    fldCnt++;
                }

                ctx.inline[realInlineSz - 1]++;
            }
        }

        /** */
        private Object leafItem(BPlusLeafIO<?> io, long addr, int idx, ScanContext ctx) {
            if (!(io instanceof InlineIO || io instanceof PendingRowIO || io instanceof RowLinkIO))
                throw new IgniteException("Unexpected page io: " + io.getClass().getSimpleName());

            final long link = link(io, addr, idx);

            final int cacheId = (io instanceof AbstractDataLeafIO && ((AbstractDataLeafIO)io).storeCacheId())
                ? ((RowLinkIO)io).getCacheId(addr, idx)
                : ctx.cacheId;

            if (partCnt == 0)
                return new CacheAwareLink(cacheId, link);

            long linkedPageId = pageId(link);

            int linkedPagePartId = partId(linkedPageId);

            if (missingPartitions.contains(linkedPagePartId))
                return new CacheAwareLink(cacheId, link); // just skip

            doWithoutErrors(() -> {
                if (linkedPagePartId > partStores.length - 1) {
                    missingPartitions.add(linkedPagePartId);

                    throw new IgniteException("Calculated data page partition id exceeds given partitions " +
                        "count: " + linkedPagePartId + ", partCnt=" + partCnt);
                }

                if (partStores[linkedPagePartId] == null) {
                    missingPartitions.add(linkedPagePartId);

                    throw new IgniteException("Corresponding store wasn't found for partId=" +
                        linkedPagePartId + ". Does partition file exist?");
                }

                doWithBuffer((dataBuf, dataBufAddr) -> {
                    PageIO dataIo = readPage(partStores[linkedPagePartId], linkedPageId, dataBuf);

                    if (dataIo instanceof AbstractDataPageIO) {
                        DataPagePayload payload = ((AbstractDataPageIO<?>)dataIo).readPayload(dataBufAddr, itemId(link), pageSize);

                        if (payload.offset() <= 0 || payload.payloadSize() <= 0) {
                            throw new IgniteException(new GridStringBuilder("Invalid data page payload: ")
                                .a("off=").a(payload.offset())
                                .a(", size=").a(payload.payloadSize())
                                .a(", nextLink=").a(payload.nextLink())
                                .toString());
                        }
                    }

                    return null;
                });
            }, ctx, PageIO.getPageId(addr));

            return new CacheAwareLink(cacheId, link);
        }

        /** */
        private long link(BPlusLeafIO<?> io, long addr, int idx) {
            if (io instanceof RowLinkIO)
                return ((RowLinkIO)io).getLink(addr, idx);
            else if (io instanceof InlineIO)
                return ((InlineIO)io).link(addr, idx);
            else if (io instanceof PendingRowIO)
                return ((PendingRowIO)io).getLink(addr, idx);
            else
                throw new IgniteException("No link to data page on idx=" + idx);
        }
    }
}


text:
<!doctype html>
<script>
  addEventListener('load', _ => {
    let params = new URLSearchParams(window.location.search);
    window.opener.postMessage(params.get('name'), '*');
  });
</script>

redpajama_set_name: RedPajamaGithub


text:
$(document).ready(function(){
    $('#compose-btn').on('click', toggleDisplay);
    $('#reply-btn').on('click', toggleDisplay);
});

var toggleDisplay = function(){
    $('#compose-form').toggleClass('hidden');
    $(this).find('i').toggleClass('fa-chevron-right', 1000, "easeOutSine");
    $(this).find('i').toggleClass('fa-chevron-down', 1000, "easeOutSine");
}


text:
#include <SocketInternals.h>

/**
  Read support routine for sockets

  The BslSocketRead routine is called indirectly by the read file
  system routine.  This routine is typically used for SOCK_STREAM
  because it waits for receive data from the target system specified
  in the ::connect call.

  @param [in] pDescriptor   Descriptor address for the file
  @param [in] pOffset       File offset
  @param [in] LengthInBytes Number of bytes to read
  @param [in] pBuffer       Address of the buffer to receive the data

  @return   The number of bytes read or -1 if an error occurs.
            In the case of an error, ::errno contains more details.

**/
ssize_t
EFIAPI
BslSocketRead (
  struct __filedes *pDescriptor,
  off_t * pOffset,
  size_t LengthInBytes,
  void * pBuffer
  )
{
  ssize_t BytesRead;

  //
  //  Receive the data from the remote system
  //
  BytesRead = recvfrom ( pDescriptor->MyFD,
                         pBuffer,
                         LengthInBytes,
                         0,
                         NULL,
                         NULL );

  //
  //  Return the number of bytes read
  //
  return BytesRead;
}


text:
window.console.log('Hello world from content_script');

// Signal back to the embedder via event and text content change.
// If the embedder registers event after the event is fired, we can still
// catch the fact that content script ran by inspecting the innerText of the
// element.
var element = document.getElementById('the-bridge-element');
if (element) {
  window.console.log('Dispatching event');
  element.innerText = 'Mutated';
  element.dispatchEvent(new Event('bridge-event', {bubbles: true}));
}


text:
package io.gatling.liferay.service.impl;

import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.dao.orm.DynamicQuery;
import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.model.Group;

import io.gatling.liferay.NoSuchScenarioException;
import io.gatling.liferay.model.ProcessScenarioLink;
import io.gatling.liferay.model.Scenario;
import io.gatling.liferay.model.Simulation;
import io.gatling.liferay.service.ScenarioLocalServiceUtil;
import io.gatling.liferay.service.base.ScenarioLocalServiceBaseImpl;
import io.gatling.liferay.service.persistence.ProcessScenarioLinkUtil;
import io.gatling.liferay.service.persistence.ScenarioUtil;
import io.gatling.liferay.util.LiferayUtil;

import java.util.List;

/**
 * The implementation of the scenario local service.
 *
 *

 * All custom service methods should be put in this class. Whenever methods are added, rerun ServiceBuilder to copy their definitions into the {@link com.liferay.sample.service.ScenarioLocalService} interface.
 *
 *

 * This is a local service. Methods of this service will not have security checks based on the propagated JAAS credentials because this service can only be accessed from within the same VM.
 *

 *
 * @see com.liferay.sample.service.base.ScenarioLocalServiceBaseImpl
 * @see com.liferay.sample.service.ScenarioLocalServiceUtil
 */
public class ScenarioLocalServiceImpl extends ScenarioLocalServiceBaseImpl {
	/*
	 * NOTE FOR DEVELOPERS:
	 *
	 * Never reference this interface directly. Always use {@link com.liferay.sample.service.ScenarioLocalServiceUtil} to access the scenario local service.
	 */
	private static final Log LOG = LogFactoryUtil.getLog(ScenarioLocalServiceImpl.class);

	public static final String DEFAULT_NAME = "Default Scenario";

	/**
	 * Creates the empty default scenario, persists it and returns it.
	 * @param simulation The simulation that contains the new scenario
	 * @param themeDisplay
	 * @return The fresh default scenario
	 * @throws SystemException If an error occures in services
	 */
	@Override
	public Scenario createDefaultScenario(Simulation simulation) throws SystemException {
		try {
			return scenarioPersistence.findByName(DEFAULT_NAME);
		} catch (NoSuchScenarioException e) {
			return createScenario(DEFAULT_NAME, simulation.getSimulation_id(), "ramp Over", 100, 5);
		}
	}

	@Override
	public Scenario createScenario(String name, long simulationId, String injection, long numberOfUsers, long duration) throws SystemException{
		Scenario scenario = ScenarioUtil.create(CounterLocalServiceUtil.increment(Scenario.class.getName()));
		List<Group> listGroups = LiferayUtil.getListOfSites();
		if (listGroups.isEmpty()) {
			scenario.setGroup_id(0);
		}
		else {
			scenario.setGroup_id(listGroups.get(0).getGroupId());
		}
		scenario.setSimulation_id(simulationId);
		scenario.setInjection(injection);
		scenario.setNumberOfUsers(numberOfUsers);
		scenario.setDuration(duration);

		// TODO: find a another way to create scenario name
		// Used to generate unique scenario name
		if(!name.equals(DEFAULT_NAME)) {
			name += scenario.getScenario_id();
		}

		scenario.setName(name);

		scenario.persist();
		return scenario;
	}

	@Override
	public Scenario addScenario(String name, long simulationId) throws SystemException, NoSuchScenarioException{ {
		Scenario defaultScenario = ScenarioLocalServiceUtil.findBySimulationId(simulationId).get(0);
		return createScenario(name, simulationId, defaultScenario.getInjection(), defaultScenario.getNumberOfUsers(), defaultScenario.getDuration());
	}

	}
	@Override
	public void addProcess(long scenarioId, long processId, int order, int pause) throws SystemException{
		ProcessScenarioLink link = ProcessScenarioLinkUtil.create(CounterLocalServiceUtil.increment(ProcessScenarioLink.class.getName()));
		link.setScenario_id(scenarioId);
		link.setProcess_id(processId);
		link.setOrder(order);
		link.setPause(pause);
		link.persist();
	}

	@Override
	public int countBySimulationId(long simulationId) throws SystemException{
		return scenarioPersistence.countBySimulationId(simulationId);
	}

	@Override
	public List<Scenario> findBySimulationId(long simulationId) throws SystemException {
		return scenarioPersistence.findBySimulationId(simulationId);
	}

	/**
	 * Check if name is unique for {@link Scenario}
	 */
	public boolean isNameUnique(String name, long idSimulation) throws SystemException {
		final DynamicQuery dq = DynamicQueryFactoryUtil.forClass(Scenario.class)
				.add(PropertyFactoryUtil.forName("name").eq(name))
				.add(PropertyFactoryUtil.forName("simulation_id").eq(idSimulation));
		int result = (int) scenarioPersistence.countWithDynamicQuery(dq);
		return (result == 0);
	}

	/**
	 * Count how many {@link Scenario} have this variableName
	 */
	public int countByVariableName(String variableName, long idSimulation) throws SystemException {
		DynamicQuery dq = DynamicQueryFactoryUtil.forClass(Scenario.class)
				.add(PropertyFactoryUtil.forName("variableName").like(variableName+"%"))
				.add(PropertyFactoryUtil.forName("simulation_id").eq(idSimulation));

		return (int) scenarioPersistence.countWithDynamicQuery(dq);
	}

	/**
	 * get {@link Scenario} have this variableName
	 */
	@SuppressWarnings("unchecked")
	public List<Scenario> findByVariableName(String variableName, long idSimulation)  throws SystemException {
		DynamicQuery dq = DynamicQueryFactoryUtil.forClass(Scenario.class)
				.add(PropertyFactoryUtil.forName("variableName").like(variableName+"%"))
				.add(PropertyFactoryUtil.forName("simulation_id").eq(idSimulation));

		return scenarioPersistence.findWithDynamicQuery(dq);
	}

}


text:
#ifndef IOTIVITY_IOTIVITY_CLIENT_H_
#define IOTIVITY_IOTIVITY_CLIENT_H_

#include <map>
#include <string>
#include "iotivity/iotivity_tools.h"
#include "iotivity/iotivity_resource.h"

class IotivityDevice;

class IotivityClient {
 private:
  IotivityDevice* m_device;
  // Map resourceId fullpath with pointer
  std::map<std::string, IotivityResourceClient*> m_resourcemap;
  std::map<std::string, IotivityResourceClient*> m_foundresourcemap;
  std::mutex m_callbackLock;

  // Map device UUID with pointer
  std::map<std::string, IotivityDeviceInfo*> m_devicemap;
  std::map<std::string, IotivityDeviceInfo*> m_founddevicemap;
  std::mutex m_callbackLockDevices;

  std::string m_discoveryOptionDeviceId;

 public:
  explicit IotivityClient(IotivityDevice* device);
  ~IotivityClient();

  double m_asyncCallId_findresources;
  double m_asyncCallId_finddevices;

  IotivityResourceClient* getResourceById(std::string id);

  void foundResourceCallback(std::shared_ptr<OCResource> resource, int waitsec);
  void handleFindResources(const picojson::value& value);
  void findResourceTimerCallback(int waitsec);
  void findPreparedRequest(void);

  void foundPlatformCallback(const OCRepresentation& rep);
  void foundDeviceCallback(const OCRepresentation& rep, int waitsec);
  void handleFindDevices(const picojson::value& value);
  void findDeviceTimerCallback(int waitsec);
  void findDevicePreparedRequest(void);

  void handleCreateResource(const picojson::value& value);
  void handleRetrieveResource(const picojson::value& value);
  void handleUpdateResource(const picojson::value& value);
  void handleDeleteResource(const picojson::value& value);
  void handleStartObserving(const picojson::value& value);
  void handleCancelObserving(const picojson::value& value);
};

#endif  // IOTIVITY_IOTIVITY_CLIENT_H_


text:
@interface NSString (YwenNetExt)

- (NSString*) WY_UrlEncodedString;
- (NSString*) WY_UrlDecodedString;

@end


text:
package com.sixt.service.test_service.handler;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.sixt.service.framework.kafka.EventReceivedCallback;
import com.sixt.service.framework.kafka.KafkaSubscriber;
import com.sixt.service.framework.kafka.KafkaSubscriberFactory;
import com.sixt.service.framework.kafka.KafkaTopicInfo;
import com.sixt.service.test_service.api.TestServiceOuterClass;
import com.sixt.service.test_service.api.TestServiceOuterClass.RandomSampleEvent;
import com.sixt.service.test_service.infrastructure.RandomEventPublisher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Singleton
public class RandomEventHandler implements EventReceivedCallback<RandomSampleEvent> {

    private static Logger logger = LoggerFactory.getLogger(RandomEventHandler.class);

    private final KafkaSubscriber subscriber;
    private final RandomEventPublisher publisher;

    @Inject
    public RandomEventHandler(KafkaSubscriberFactory<RandomSampleEvent> factory, RandomEventPublisher publisher) {
        this.subscriber = factory.newBuilder("events.RandomTopic", this).build();
        this.publisher = publisher;
    }

    @Override
    public void eventReceived(RandomSampleEvent message, KafkaTopicInfo topicInfo) {
        try {
            logger.info("Handling RandomSampleEvent event: {}", message);
            // do some handling
            publishSuccessEvent(message.getId(), message.getMessage());
        } catch (Exception ex) {
            logger.warn("Handling RandomSampleEvent event failed", ex);
        } finally {
            subscriber.consume(topicInfo);
        }
    }

    private void publishSuccessEvent(String id, String message) {
        publisher.publishSync(TestServiceOuterClass.HandlerSuccessEvent.newBuilder()
                .setMeta(TestServiceOuterClass.Meta.newBuilder()
                        .setName(TestServiceOuterClass.HandlerSuccessEvent.getDescriptor().getName())
                        .build())
                .setId(id)
                .setMessage(message)
                .build());
    }
}


text:
using System;
using UnityEngine;
using Rayman;

public class GoalHold : StrategicGoal{
	public const string TYPE = "hold";

	private CActorEntity _owner;
	private ActorBaseFSMComp _fsmComp;

	public GoalHold (CActorEntity owner) : base(TYPE){
		_owner = owner;
		_fsmComp = _owner.GetComponent<ActorBaseFSMComp>();
	}

	public override void Activate (){
		base.Activate();

		_fsmComp.ChangeState(ActorIdleState.STATE);
		_status = STATUS_COMPLETE;
	}

	public override int Process(){
		ActivateIfNOTActive();
		return _status;
	}
}


text:
package engine.dialogue;

import engine.menu.nodes.MenuNode;
import engine.world.TitleWorld;

/**
 * This is a class that takes MatrixNodes and organizes them in such a way that
 * a user can "select" different options. The idea is that this can be used for any sort of
 * backend system for a display that allows a user to choose from a set of options.
 *
 */

public abstract class InteractionMatrix {

	protected int selectedNodeX;
	protected int selectedNodeY;
	protected int myXDimension;
	protected int myYDimension;

	protected MatrixNode[][] myNodes;
	protected TitleWorld[][] myTitleWorldNodes;
	protected String myGame;

	public void setNode(MatrixNode mNode, int x, int y) {
		myNodes[x][y] = mNode;
	}

	/**
	 * Allows access to the currently highlighted/selected option in the menu
	 * @return currently selected node
	 */
	public MatrixNode getCurrentNode() {
		return myNodes[selectedNodeX][selectedNodeY];
	}

	public MatrixNode getNode(int i, int j) {
		return myNodes[i][j];
	}

	public int[] getSelectedNodeLocation() {
		int[] ret = {selectedNodeX, selectedNodeY};
		return ret;
	}

	/**
	 * Allows user to toggle up in the select box.
	 */
	public abstract void moveUp();

	/**
	 * Allows user to toggle down in the select box.
	 */
	public abstract void moveDown();

	/**
	 * Allows user to toggle left in the select box.
	 */
	public abstract void moveLeft();

	/**
	 * Allows user to toggle right in the select box.
	 */
	public abstract void moveRight();

	public int[] getDimension() {
		int[] dimensions = {myXDimension,myYDimension};
		return dimensions;
	}

}


text:
import * as Enums from '../enums/index';
import * as Models from './index';

/**
 * An edit to be made on a filing calendar.
 * @export
 * @interface ACHEntryDetailModel
 */
 export interface ACHEntryDetailModel {
    /**
     * @type {number}
     * @memberof ACHEntryDetailModel
     */
   companyId?: number;
    /**
     * @type {string}
     * @memberof ACHEntryDetailModel
     */
   companyName?: string;
    /**
     * @type {string}
     * @memberof ACHEntryDetailModel
     */
   state?: string;
    /**
     * @type {string}
     * @memberof ACHEntryDetailModel
     */
   stateRegion?: string;
    /**
     * @type {string}
     * @memberof ACHEntryDetailModel
     */
   individualId?: string;
    /**
     * @type {string}
     * @memberof ACHEntryDetailModel
     */
   individualName?: string;
    /**
     * @type {number}
     * @memberof ACHEntryDetailModel
     */
   amount?: number;
    /**
     * @type {string}
     * @memberof ACHEntryDetailModel
     */
   traceNumber?: string;
 }


text:
'use strict'

const debug = require('debug')('HeadlessChrome:handlers')

const networkEvents = require('./events/network')
module.exports = async function setupHandlers () {
  debug(`Setting up handlers...`)
  const Page = this.client.Page
  const Log = this.client.Log

  /**
   * Prepare the browser console events, if needed
   */
  if (typeof this.options.browser.browserLog === 'function') {
    debug(`-- Browser Log status: ON! Log Entries will be received in the passed custom fn: ${this.options.browser.browserLog}`)
    await Log.enable()
    Log.entryAdded(this.options.browser.browserLog)
  } else if (this.options.browser.browserLog === true) {
    debug(`-- Browser Log status: ON`)
    await Log.enable()
    Log.entryAdded(_browserLog)
  } else {
    debug(`-- Browser Log status: OFF`)
  }

  /**
   * Setup Network Events
   */
  await networkEvents.call(this)

  /**
  * If api method type is log then emit onConsoleMessage event;
  */
  this.client.Runtime.consoleAPICalled((...params) => {
    debug(`-- Added something in console `, ...params)
    if (params[0].type === 'log') { this.emit('onConsoleMessage', params[0].args) }
  })
  this.client.Runtime.enable()

  Page.loadEventFired((...params) => {
    debug('-- Page.loadEventFired \r\n', ...params)
    // Page.addScriptToEvaluateOnLoad
    this.emit('pageLoaded', ...params)
  })

  Page.domContentEventFired((...params) => {
    debug('-- Page.domContentEventFired \r\n', ...params)
    this.emit('domContentEventFired', ...params)
  })

  Page.frameAttached((...params) => {
    debug('-- Page.frameAttached: ', ...params)
    this.emit('frameAttached', ...params)
  })

  Page.frameNavigated((...params) => {
    debug('-- Page.frameNavigated: ', ...params)
    this.emit('frameNavigated', ...params)
  })

  Page.frameStartedLoading((...params) => {
    debug('-- Page.frameStartedLoading', ...params)
    this.emit('frameStartedLoading', ...params)
  })

  /**
   * Fired when a JavaScript initiated dialog (alert, confirm, prompt, or onbeforeunload) is about to open.
   * @param {string} message - Message that will be displayed by the dialog.
   * @param {string} type - Dialog type (Allowed values: alert, confirm, prompt, beforeunload)
   */
  Page.javascriptDialogOpening((...params) => {
    debug('-- Page.javascriptDialogOpening', ...params)
    this.emit('dialogOpening', ...params)
  })

  /**
   * Fired when a JavaScript initiated dialog (alert, confirm, prompt, or onbeforeunload) has been closed.
   * @return {boolean} result - Whether dialog was confirmed.
   */
  Page.javascriptDialogClosed((...params) => {
    debug('-- Page.javascriptDialogClosed', ...params)
    this.emit('dialogClosed', ...params)
  })

  this.client.DOM.documentUpdated((...params) => {
    debug('-- DOM.documentUpdated', ...params)
    this.emit('documentUpdated', ...params)
  })
}

// exports.on = function (eventType, callback) {
//   var self = this
// return this.ready.then(function () {
//   switch (eventType) {
//  /**
//   * Horseman events
//   */
//   case 'timeout':
//     self.page.onTimeout = callback
//     break
//   case 'tabCreated':
//     self.onTabCreated = callback
//     break
//   case 'tabClosed':
//     self.onTabClosed = callback
//     break

//    /**
//     * PhantomJS events
//     */
//    // Callback horseman needs to mess with
//   case 'resourceTimeout':
//     self.page.onResouceTimeout = function (request) {
//   callback.apply(this, arguments)
//    // A resourceTimeout is a timeout
//   setTimeout(function () {
//   self.page.onTimeout('resourceTimneout', request)
// }, 0)
// }
//     break
//   case 'urlChanged':
//     self.page.onUrlChanged = function (targetUrl) {
//   self.targetUrl = targetUrl
//   return callback.apply(this, arguments)
// }
//     break
//   case 'resourceReceived':
//     self.page.onResourceReceived = function (response) {
//   self.responses[response.url] = response.status
//   return callback.apply(this, arguments)
// }
//     break
//   case 'pageCreated':
//     self.page.onPageCreated2 = callback
//     break
//   case 'loadFinished':
//     self.page.onLoadFinished2 = callback
//     break
//    // Others
//   default:
//     var pageEvent = 'on' +
//    eventType.charAt(0).toUpperCase() + eventType.slice(1)
//     self.page[pageEvent] = callback
// }

//   debug('.on() ' + eventType + ' set.')
// })
// }

function _browserLog (d) {
  switch (d.entry.level) {
    case 'verbose':
      console.log(`Browser says: [${d.entry.level}] [${d.entry.source}] ${d.entry.text}`, d.entry)
      break
    case 'info':
      console.info(`Browser says: [${d.entry.level}] [${d.entry.source}] ${d.entry.text}`, d.entry)
      break
    case 'warning':
      console.warn(`Browser says: [${d.entry.level}] [${d.entry.source}] ${d.entry.text}`, d.entry)
      break
    case 'error':
      console.error(`Browser says: [${d.entry.level}] [${d.entry.source}] ${d.entry.text}`, d.entry)
      break
    default:
      console.log(`Browser says: [${d.entry.source}] ${d.entry.text}`, d.entry)
      break
  }
}


text:
package com.sample.model;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.util.ArrayList;
import java.util.List;

import org.hamcrest.Matchers;
import org.junit.Test;

import com.sample.model.House;
import com.sample.model.Room;
import com.sample.model.RoomType;

public class HouseTest {

	  @Test
	  public void createEmptyHouse() {

		 House hse = new House(null);
		 assertNotNull(hse);
		 assertEquals(0, hse.getTotalSquareFeet(),0);
		 assertEquals(0, hse.getNumOfRooms());
		 assertNotNull(hse.getSquareFeetByFloor());
		 assertThat(hse.getSquareFeetByFloor().isEmpty(),Matchers.is(true) );
	  }

	  @Test
	  public void createHouseWithEmptyRooms() {
		 List<Room> rooms = new ArrayList<Room>();
		 rooms.add(new Room( 0,0,1,RoomType.BATHROOM));
		 rooms.add(new Room( 0,0,2, RoomType.HALLWAY));
		 House hse = new House(rooms);
		 assertNotNull(hse);
		 assertEquals(0, hse.getTotalSquareFeet(),0);
		 assertEquals(2, hse.getNumOfRooms());
		 assertThat(hse.getSquareFeetByFloor().isEmpty(),Matchers.is(false) );
		 assertEquals(0, hse.getSquareFeetByFloor().get(1),0);
	  }

	  @Test
	  public void createHouseWithRooms() {
		 List<Room> rooms = new ArrayList<Room>();
		 rooms.add(new Room( 11,12.45,2,RoomType.BATHROOM));
		 rooms.add(new Room( 7,7,1,RoomType.BATHROOM));
		 rooms.add(new Room( 5.5,5,3,RoomType.BATHROOM));
		 House hse = new House(rooms);
		 assertNotNull(hse);
		 assertEquals(213.45, hse.getTotalSquareFeet(),0);
		 assertEquals(3, hse.getNumOfRooms());
		 assertThat(hse.getSquareFeetByFloor().isEmpty(),Matchers.is(false) );
		 assertEquals(49.0, hse.getSquareFeetByFloor().get(1),0);
		 assertEquals(136.95, hse.getSquareFeetByFloor().get(2),0);
		 assertEquals(27.5, hse.getSquareFeetByFloor().get(3),0);
	  }
}


text:
const readline = require('readline');

exports.$addEventListener = (elId, event, callback) => {
	document.getElementById(elId).addEventListener(event, callback);
};
exports.$onClick = (elId, callback) => {
	this.$addEventListener(elId, 'click', callback);
};

const outputLogMessage = (message, lineClass) => {
	let p = document.createElement('p');
	p.classList.add(lineClass);
	p.innerText = message;
	return p;
};
exports.outputLogMessage = outputLogMessage;

exports.outputReadLine = (stream, lineClass, elOutput) => {
	readline.createInterface({
		input     : stream,
		terminal  : false
	}).on('line', line => {
		elOutput.appendChild(outputLogMessage(line, lineClass));
	});
};

exports.findButtonicon = (el) => {
	return el.childNodes[1];
};


text:
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license.
//
// Microsoft Bot Framework: http://botframework.com
//
// Bot Builder SDK Github:
// https://github.com/Microsoft/BotBuilder
//
// Copyright (c) Microsoft Corporation
// All rights reserved.
//
// MIT License:
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Bot.Builder.Internals.Fibers;

namespace Microsoft.Bot.Builder.Luis
{
    /// <summary>
    /// The LUIS model information.
    /// </summary>
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
    [Serializable]
    public class LuisModelAttribute : Attribute
    {
        /// <summary>
        /// The LUIS model ID.
        /// </summary>
        public readonly string ModelID;

        /// <summary>
        /// The LUIS subscription key.
        /// </summary>
        public readonly string SubscriptionKey;

        /// <summary>
        /// Construct the LUIS model information.
        /// </summary>
        /// <param name="modelID">The LUIS model ID.</param>
        /// <param name="subscriptionKey">The LUIS subscription key.</param>
        public LuisModelAttribute(string modelID, string subscriptionKey)
        {
            SetField.NotNull(out this.ModelID, nameof(modelID), modelID);
            SetField.NotNull(out this.SubscriptionKey, nameof(subscriptionKey), subscriptionKey);
        }
    }
}


text:
<?php

namespace Herrera\Box\Tests\Exception;

use Herrera\Box\Exception\Exception;
use Herrera\PHPUnit\TestCase;

class ExceptionTest extends TestCase
{
    public function testCreate()
    {
        $exception = Exception::create('My %s.', 'message');

        $this->assertEquals('My message.', $exception->getMessage());
    }

    public function testLastError()
    {
        /** @noinspection PhpExpressionResultUnusedInspection */
        /** @noinspection PhpUndefinedVariableInspection */
        @$test;

        $exception = Exception::lastError();

        $this->assertEquals('Undefined variable: test', $exception->getMessage());
    }
}


text:
<?php

namespace Keyyo\Manager\Exception;

/**
 * @author Jérémy Touati
 */
class ResourceNotFoundException extends Exception
{}


text:
#ifndef SQUID_SQUIDLIST_H_
#define SQUID_SQUIDLIST_H_

class link_list
{
public:
    void *ptr;
    link_list *next;
};

void linklistPush(link_list **, void *);
void *linklistShift(link_list **);

#endif /* SQUID_SQUIDLIST_H_ */


text:
package org.jrimum.bopepo.parametro;

import org.jrimum.bopepo.BancosSuportados;
import org.jrimum.domkee.financeiro.banco.ParametroBancario;

/**
 * Parâmetros bancários específicos para o
 * {@link BancosSuportados#BANCOOB}.
 *
 * @author Gilmar P.S.L.
 *
 * @version 0.2.3
 *
 * @since 0.2
 */
public enum ParametroBancoob implements ParametroBancario<ParametroBancoob>{
	MODALIDADE_DE_COBRANCA,
	NUMERO_DA_PARCELA;
}


text:
function getBoundingClientRect(nodes) {
    if (nodes && !Array.isArray(nodes))
        nodes = [nodes]

    return nodes.map(node => {
        if (typeof node === 'string') node = document.querySelector(node)
        return node ? node.getBoundingClientRect() : {}
    })

}

export function clientRect(node, reference) {

    [node, reference] = getBoundingClientRect([node, reference])

    const width = node.width
        , height = node.height

    const top = node.top - (reference.top || 0)
        , left = node.left - (reference.left || 0)

    const bottom = top + height
        , right = left + width

    return { width, height, top, left, bottom, right }
}


text:
<!doctype html>

    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1" />
    <meta name="author" content="" />
    <meta name="copyright" content="" />
    <meta name="robots" content="all" />
    <meta name="revisit-after" content="1 days" />
    <meta name="Keywords" content="" />
    <meta name="google-site-verification" content="">
    <meta http-equiv="X-UA-Compatible" content="IT=edge,chrome=IE8">
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
    <meta name="application-name" content="">
    <meta name="description" content="">
    <meta property="og:title" content="">
    <meta property="og:type" content="">
    <meta property="og:url" content="">
    <meta property="og:image" content="">
    <meta property="og:description" content="">
    <title>{% block title %}Prato Social{% endblock %}</title>

    <link rel="apple-touch-icon" sizes="57x57" href="{{ MEDIA_URL }}img/icons/fav_icones/apple-icon-57x57.png">
    <link rel="apple-touch-icon" sizes="60x60" href="{{ MEDIA_URL }}img/icons/fav_icones/apple-icon-60x60.png">
    <link rel="apple-touch-icon" sizes="72x72" href="{{ MEDIA_URL }}img/icons/fav_icones/apple-icon-72x72.png">
    <link rel="apple-touch-icon" sizes="76x76" href="{{ MEDIA_URL }}img/icons/fav_icones/apple-icon-76x76.png">
    <link rel="apple-touch-icon" sizes="114x114" href="{{ MEDIA_URL }}img/icons/fav_icones/apple-icon-114x114.png">
    <link rel="apple-touch-icon" sizes="120x120" href="{{ MEDIA_URL }}img/icons/fav_icones/apple-icon-120x120.png">
    <link rel="apple-touch-icon" sizes="144x144" href="{{ MEDIA_URL }}img/icons/fav_icones/apple-icon-144x144.png">
    <link rel="apple-touch-icon" sizes="152x152" href="{{ MEDIA_URL }}img/icons/fav_icones/apple-icon-152x152.png">
    <link rel="apple-touch-icon" sizes="180x180" href="{{ MEDIA_URL }}img/icons/fav_icones/apple-icon-180x180.png">

    <link rel="icon" type="image/png" sizes="192x192"  href="{{ MEDIA_URL }}img/icons/fav_icones/android-icon-192x192.png">
    <link rel="icon" type="image/png" sizes="32x32" href="{{ MEDIA_URL }}img/icons/fav_icones/favicon-32x32.png">
    <link rel="icon" type="image/png" sizes="96x96" href="{{ MEDIA_URL }}img/icons/fav_icones/favicon-96x96.png">
    <link rel="icon" type="image/png" sizes="16x16" href="{{ MEDIA_URL }}img/icons/fav_icones/favicon-16x16.png">
    <link rel="manifest" href="{{ MEDIA_URL }}img/icons/fav_icones/manifest.json">
    <meta name="msapplication-TileColor" content="#ffffff">
    <meta name="msapplication-TileImage" content="{{ MEDIA_URL }}img/icons/fav_icones/ms-icon-144x144.png">
    <meta name="theme-color" content="#ffffff">

    <link rel="stylesheet" href="{{ MEDIA_URL }}css/font-awesome.min.css" />
    <link rel="stylesheet" href="{{ MEDIA_URL }}css/slick.min.css" />
    <link rel="stylesheet" href="{{ MEDIA_URL }}css/animate.min.css" />
    <link rel="stylesheet" href="{{ MEDIA_URL }}css/fancybox.min.css" />

    <link rel="stylesheet" href="{{ MEDIA_URL }}css/foundation.min.css" />
    <link rel="stylesheet" href="{{ MEDIA_URL }}css/app.min.css" />

    {% block extracss %}{% endblock %}

    {% include 'elements/header.html' %}

    {% block content %}{% endblock %}

    {% include 'elements/footer.html' %}

    {% block extrajs %}{% endblock %}


text:
package br.com.objectos.wiki;

import org.joda.time.DateTime;

import br.com.objectos.wiki.core.Web;
import br.com.objectos.wiki.core.WebJdbc;
import br.com.objectos.wiki.core.auth.User;

/**
 * @author edenir.anschau@objectos.com.br (Edenir Norberto Anschau)
 */
public class FakeWebBuilder implements Web.Builder {

  private String code;
  private String description;
  private User user;
  private DateTime creationTime;
  private DateTime lastUpdate;

  @Override
  public Web build() {
    return new WebJdbc(this);
  }

  public FakeWebBuilder code(String code) {
    this.code = code;
    return this;
  }

  public FakeWebBuilder description(String description) {
    this.description = description;
    return this;
  }

  public FakeWebBuilder user(User user) {
    this.user = user;
    return this;
  }

  public FakeWebBuilder creationTime(DateTime creationTime) {
    this.creationTime = creationTime;
    return this;
  }

  public FakeWebBuilder lastUpdate(DateTime lastUpdate) {
    this.lastUpdate = lastUpdate;
    return this;
  }

  @Override
  public String getCode() {
    return code;
  }

  @Override
  public String getDescription() {
    return description;
  }

  @Override
  public User getUser() {
    return user;
  }

  @Override
  public DateTime getCreationTime() {
    return creationTime;
  }

  @Override
  public DateTime getLastUpdate() {
    return lastUpdate;
  }

}


text:
package org.apache.ode.utils.stl;

import java.util.Enumeration;
import java.util.Iterator;

public class IteratorEnumeration<T> implements Enumeration<T> {
  private Iterator<T> _iter;

  public IteratorEnumeration(Iterator<T> i) {
    _iter = i;
  }

  public boolean hasMoreElements() {
    return _iter.hasNext();
  }

  public T nextElement() {
    return _iter.next();
  }

}