Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8d46f34e authored by Omer Nebil Yaveroglu's avatar Omer Nebil Yaveroglu
Browse files

Some refactoring for fixing the tests and also eliminating the use of

TreeMap but use a more light weight data structure.

Bug: 145493956
Test: atest FrameworksServicesTests:IntegrityFileManagerTest
Change-Id: I2e7dd5336c4aa3ee41c05d4af0388125751bc207
parent 2b0a51b6
Loading
Loading
Loading
Loading
+14 −7
Original line number Diff line number Diff line
@@ -27,30 +27,37 @@ import java.io.InputStream;
 */
public class BitTrackedInputStream extends BitInputStream {

    private static int sReadBitsCount;
    private int mReadBitsCount;

    /** Constructor with byte array. */
    public BitTrackedInputStream(byte[] inputStream) {
        super(inputStream);
        sReadBitsCount = 0;
        mReadBitsCount = 0;
    }

    /** Constructor with input stream. */
    public BitTrackedInputStream(InputStream inputStream) {
        super(inputStream);
        sReadBitsCount = 0;
        mReadBitsCount = 0;
    }

    /** Obtains an integer value of the next {@code numOfBits}. */
    @Override
    public int getNext(int numOfBits) throws IOException {
        sReadBitsCount += numOfBits;
        mReadBitsCount += numOfBits;
        return super.getNext(numOfBits);
    }

    /** Returns the current cursor position showing the number of bits that are read. */
    public int getReadBitsCount() {
        return sReadBitsCount;
        return mReadBitsCount;
    }

    /**
     * Returns true if we can read more rules by checking whether the end index is not reached yet.
     */
    public boolean canReadMoreRules(int endIndexBytes) {
        return mReadBitsCount < endIndexBytes * 8;
    }

    /**
@@ -59,11 +66,11 @@ public class BitTrackedInputStream extends BitInputStream {
     * Note that the integer parameter specifies the location in bytes -- not bits.
     */
    public void setCursorToByteLocation(int byteLocation) throws IOException {
        int bitCountToRead = byteLocation * 8 - sReadBitsCount;
        int bitCountToRead = byteLocation * 8 - mReadBitsCount;
        if (bitCountToRead < 0) {
            throw new IllegalStateException("The byte position is already read.");
        }
        super.getNext(bitCountToRead);
        sReadBitsCount = byteLocation * 8;
        mReadBitsCount = byteLocation * 8;
    }
}
+1 −2
Original line number Diff line number Diff line
@@ -105,8 +105,7 @@ public class RuleBinaryParser implements RuleParser {
            bitTrackedInputStream.setCursorToByteLocation(range.getStartIndex());

            // Read the rules until we reach the end index.
            while (bitTrackedInputStream.hasNext()
                    && bitTrackedInputStream.getReadBitsCount() < range.getEndIndex()) {
            while (bitTrackedInputStream.canReadMoreRules(range.getEndIndex())) {
                if (bitTrackedInputStream.getNext(SIGNAL_BIT) == 1) {
                    parsedRules.add(parseRule(bitTrackedInputStream));
                }
+8 −8
Original line number Diff line number Diff line
@@ -23,28 +23,28 @@ import android.annotation.Nullable;
 * RuleIndexingController}.
 */
public class RuleIndexRange {
    private static int sStartIndex;
    private static int sEndIndex;
    private int mStartIndex;
    private int mEndIndex;

    /** Constructor with start and end indexes. */
    public RuleIndexRange(int startIndex, int endIndex) {
        this.sStartIndex = startIndex;
        this.sEndIndex = endIndex;
        this.mStartIndex = startIndex;
        this.mEndIndex = endIndex;
    }

    /** Returns the startIndex. */
    public int getStartIndex() {
        return sStartIndex;
        return mStartIndex;
    }

    /** Returns the end index. */
    public int getEndIndex() {
        return sEndIndex;
        return mEndIndex;
    }

    @Override
    public boolean equals(@Nullable Object object) {
        return sStartIndex == ((RuleIndexRange) object).getStartIndex()
                && sEndIndex == ((RuleIndexRange) object).getEndIndex();
        return mStartIndex == ((RuleIndexRange) object).getStartIndex()
                && mEndIndex == ((RuleIndexRange) object).getEndIndex();
    }
}
+2 −2
Original line number Diff line number Diff line
@@ -56,7 +56,7 @@ public class RuleIndexingController {
     * read and evaluated.
     */
    public List<RuleIndexRange> identifyRulesToEvaluate(AppInstallMetadata appInstallMetadata) {
        ArrayList<RuleIndexRange> indexRanges = new ArrayList();
        List<RuleIndexRange> indexRanges = new ArrayList<>();

        // Add the range for package name indexes rules.
        indexRanges.add(
@@ -102,7 +102,7 @@ public class RuleIndexingController {
                        .collect(Collectors.toCollection(TreeSet::new));

        String minIndex = keyTreeSet.floor(searchedKey);
        String maxIndex = keyTreeSet.ceiling(searchedKey);
        String maxIndex = keyTreeSet.higher(searchedKey);

        return new RuleIndexRange(
                indexMap.get(minIndex == null ? START_INDEXING_KEY : minIndex),
+28 −22
Original line number Diff line number Diff line
@@ -52,7 +52,7 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.TreeMap;
import java.util.stream.Collectors;

/** A helper class to serialize rules from the {@link Rule} model to Binary representation. */
public class RuleBinarySerializer implements RuleSerializer {
@@ -80,21 +80,24 @@ public class RuleBinarySerializer implements RuleSerializer {
            throws RuleSerializeException {
        try {
            // Determine the indexing groups and the order of the rules within each indexed group.
            Map<Integer, TreeMap<String, List<Rule>>> indexedRules =
            Map<Integer, Map<String, List<Rule>>> indexedRules =
                    RuleIndexingDetailsIdentifier.splitRulesIntoIndexBuckets(rules);

            // Serialize the rules.
            ByteTrackedOutputStream ruleFileByteTrackedOutputStream =
                    new ByteTrackedOutputStream(rulesFileOutputStream);
            serializeRuleFileMetadata(formatVersion, ruleFileByteTrackedOutputStream);
            Map<String, Integer> packageNameIndexes =
                    serializeRuleList(indexedRules.get(PACKAGE_NAME_INDEXED),
            LinkedHashMap<String, Integer> packageNameIndexes =
                    serializeRuleList(
                            indexedRules.get(PACKAGE_NAME_INDEXED),
                            ruleFileByteTrackedOutputStream);
            Map<String, Integer> appCertificateIndexes =
                    serializeRuleList(indexedRules.get(APP_CERTIFICATE_INDEXED),
            LinkedHashMap<String, Integer> appCertificateIndexes =
                    serializeRuleList(
                            indexedRules.get(APP_CERTIFICATE_INDEXED),
                            ruleFileByteTrackedOutputStream);
            Map<String, Integer> unindexedRulesIndexes =
                    serializeRuleList(indexedRules.get(NOT_INDEXED),
            LinkedHashMap<String, Integer> unindexedRulesIndexes =
                    serializeRuleList(
                            indexedRules.get(NOT_INDEXED),
                            ruleFileByteTrackedOutputStream);

            // Serialize their indexes.
@@ -104,6 +107,9 @@ public class RuleBinarySerializer implements RuleSerializer {
                    true);
            serializeIndexGroup(unindexedRulesIndexes, indexingBitOutputStream, /* isIndexed= */
                    false);
            // TODO(b/147609625): This dummy bit is set for fixing the padding issue. Remove when
            // the issue is fixed and correct the tests that does this padding too.
            indexingBitOutputStream.setNext();
            indexingFileOutputStream.write(indexingBitOutputStream.toByteArray());
        } catch (Exception e) {
            throw new RuleSerializeException(e.getMessage(), e);
@@ -120,24 +126,25 @@ public class RuleBinarySerializer implements RuleSerializer {
        outputStream.write(bitOutputStream.toByteArray());
    }

    private Map<String, Integer> serializeRuleList(TreeMap<String, List<Rule>> rulesMap,
            ByteTrackedOutputStream outputStream)
    private LinkedHashMap<String, Integer> serializeRuleList(
            Map<String, List<Rule>> rulesMap, ByteTrackedOutputStream outputStream)
            throws IOException {
        Preconditions.checkArgument(rulesMap != null,
                "serializeRuleList should never be called with null rule list.");

        BitOutputStream bitOutputStream = new BitOutputStream();
        Map<String, Integer> indexMapping = new LinkedHashMap();
        int indexTracker = 0;

        LinkedHashMap<String, Integer> indexMapping = new LinkedHashMap();
        indexMapping.put(START_INDEXING_KEY, outputStream.getWrittenBytesCount());
        for (Map.Entry<String, List<Rule>> entry : rulesMap.entrySet()) {

        List<String> sortedKeys = rulesMap.keySet().stream().sorted().collect(Collectors.toList());
        int indexTracker = 0;
        for (String key : sortedKeys) {
            if (indexTracker >= INDEXING_BLOCK_SIZE) {
                indexMapping.put(entry.getKey(), outputStream.getWrittenBytesCount());
                indexMapping.put(key, outputStream.getWrittenBytesCount());
                indexTracker = 0;
            }

            for (Rule rule : entry.getValue()) {
            for (Rule rule : rulesMap.get(key)) {
                bitOutputStream.clear();
                serializeRule(rule, bitOutputStream);
                outputStream.write(bitOutputStream.toByteArray());
@@ -222,11 +229,13 @@ public class RuleBinarySerializer implements RuleSerializer {
    }

    private void serializeIndexGroup(
            Map<String, Integer> indexes, BitOutputStream bitOutputStream, boolean isIndexed) {
            LinkedHashMap<String, Integer> indexes,
            BitOutputStream bitOutputStream,
            boolean isIndexed) {

        // Output the starting location of this indexing group.
        serializeStringValue(START_INDEXING_KEY, /* isHashedValue= */false,
                bitOutputStream);
        serializeStringValue(
                START_INDEXING_KEY, /* isHashedValue= */false, bitOutputStream);
        serializeIntValue(indexes.get(START_INDEXING_KEY), bitOutputStream);

        // If the group is indexed, output the locations of the indexes.
@@ -244,9 +253,6 @@ public class RuleBinarySerializer implements RuleSerializer {
        // Output the end location of this indexing group.
        serializeStringValue(END_INDEXING_KEY, /*isHashedValue= */ false, bitOutputStream);
        serializeIntValue(indexes.get(END_INDEXING_KEY), bitOutputStream);

        // This dummy bit is set for fixing the padding issue. songpan@ will fix it and remove it.
        bitOutputStream.setNext();
    }

    private void serializeStringValue(
Loading