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

Commit 10432c77 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Some refactoring for fixing the tests and also eliminating the use of...

Merge "Some refactoring for fixing the tests and also eliminating the use of TreeMap but use a more light weight data structure."
parents 997d76d8 8d46f34e
Loading
Loading
Loading
Loading
+14 −7
Original line number Original line Diff line number Diff line
@@ -27,30 +27,37 @@ import java.io.InputStream;
 */
 */
public class BitTrackedInputStream extends BitInputStream {
public class BitTrackedInputStream extends BitInputStream {


    private static int sReadBitsCount;
    private int mReadBitsCount;


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


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


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


    /** Returns the current cursor position showing the number of bits that are read. */
    /** Returns the current cursor position showing the number of bits that are read. */
    public int getReadBitsCount() {
    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.
     * Note that the integer parameter specifies the location in bytes -- not bits.
     */
     */
    public void setCursorToByteLocation(int byteLocation) throws IOException {
    public void setCursorToByteLocation(int byteLocation) throws IOException {
        int bitCountToRead = byteLocation * 8 - sReadBitsCount;
        int bitCountToRead = byteLocation * 8 - mReadBitsCount;
        if (bitCountToRead < 0) {
        if (bitCountToRead < 0) {
            throw new IllegalStateException("The byte position is already read.");
            throw new IllegalStateException("The byte position is already read.");
        }
        }
        super.getNext(bitCountToRead);
        super.getNext(bitCountToRead);
        sReadBitsCount = byteLocation * 8;
        mReadBitsCount = byteLocation * 8;
    }
    }
}
}
+1 −2
Original line number Original line Diff line number Diff line
@@ -105,8 +105,7 @@ public class RuleBinaryParser implements RuleParser {
            bitTrackedInputStream.setCursorToByteLocation(range.getStartIndex());
            bitTrackedInputStream.setCursorToByteLocation(range.getStartIndex());


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


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


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


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


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


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


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


        return new RuleIndexRange(
        return new RuleIndexRange(
                indexMap.get(minIndex == null ? START_INDEXING_KEY : minIndex),
                indexMap.get(minIndex == null ? START_INDEXING_KEY : minIndex),
+28 −22
Original line number Original line Diff line number Diff line
@@ -52,7 +52,7 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.List;
import java.util.Map;
import java.util.Map;
import java.util.Optional;
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. */
/** A helper class to serialize rules from the {@link Rule} model to Binary representation. */
public class RuleBinarySerializer implements RuleSerializer {
public class RuleBinarySerializer implements RuleSerializer {
@@ -80,21 +80,24 @@ public class RuleBinarySerializer implements RuleSerializer {
            throws RuleSerializeException {
            throws RuleSerializeException {
        try {
        try {
            // Determine the indexing groups and the order of the rules within each indexed group.
            // 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);
                    RuleIndexingDetailsIdentifier.splitRulesIntoIndexBuckets(rules);


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


            // Serialize their indexes.
            // Serialize their indexes.
@@ -104,6 +107,9 @@ public class RuleBinarySerializer implements RuleSerializer {
                    true);
                    true);
            serializeIndexGroup(unindexedRulesIndexes, indexingBitOutputStream, /* isIndexed= */
            serializeIndexGroup(unindexedRulesIndexes, indexingBitOutputStream, /* isIndexed= */
                    false);
                    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());
            indexingFileOutputStream.write(indexingBitOutputStream.toByteArray());
        } catch (Exception e) {
        } catch (Exception e) {
            throw new RuleSerializeException(e.getMessage(), e);
            throw new RuleSerializeException(e.getMessage(), e);
@@ -120,24 +126,25 @@ public class RuleBinarySerializer implements RuleSerializer {
        outputStream.write(bitOutputStream.toByteArray());
        outputStream.write(bitOutputStream.toByteArray());
    }
    }


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


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

        indexMapping.put(START_INDEXING_KEY, outputStream.getWrittenBytesCount());
        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) {
            if (indexTracker >= INDEXING_BLOCK_SIZE) {
                indexMapping.put(entry.getKey(), outputStream.getWrittenBytesCount());
                indexMapping.put(key, outputStream.getWrittenBytesCount());
                indexTracker = 0;
                indexTracker = 0;
            }
            }


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


    private void serializeIndexGroup(
    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.
        // Output the starting location of this indexing group.
        serializeStringValue(START_INDEXING_KEY, /* isHashedValue= */false,
        serializeStringValue(
                bitOutputStream);
                START_INDEXING_KEY, /* isHashedValue= */false, bitOutputStream);
        serializeIntValue(indexes.get(START_INDEXING_KEY), bitOutputStream);
        serializeIntValue(indexes.get(START_INDEXING_KEY), bitOutputStream);


        // If the group is indexed, output the locations of the indexes.
        // 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.
        // Output the end location of this indexing group.
        serializeStringValue(END_INDEXING_KEY, /*isHashedValue= */ false, bitOutputStream);
        serializeStringValue(END_INDEXING_KEY, /*isHashedValue= */ false, bitOutputStream);
        serializeIntValue(indexes.get(END_INDEXING_KEY), 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(
    private void serializeStringValue(
Loading