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

Commit 883e30a1 authored by mrulhania's avatar mrulhania Committed by Manjeet Rulhania
Browse files

SQLite app ops optimize evict methods

Refactor evict() & evict(opCode) into one method,
Also optimize evict() when there is no event to evict,
in earlier method we were creating a copy of cache.

Bug: 377584611
Flag: EXEMPT bug fix
Test: presubmit
Change-Id: Ib1c85e238e49ad42a43b4771566e4b5469dca8b2
parent f624fcb7
Loading
Loading
Loading
Loading
+32 −40
Original line number Diff line number Diff line
@@ -52,12 +52,14 @@ import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Random;
import java.util.Set;
import java.util.function.Predicate;

/**
 * Helper class to read/write aggregated app op access events.
@@ -506,58 +508,38 @@ public class AppOpHistoryHelper {
         * Evict older events i.e. events from previous time windows.
         */
        private List<AggregatedAppOpAccessEvent> evict() {
            synchronized (this) {
                List<AggregatedAppOpAccessEvent> evictedEvents = new ArrayList<>();
                ArrayMap<AppOpAccessEvent, AggregatedAppOpValues> snapshot =
                        new ArrayMap<>(mCache);
                long evictionTimestamp = System.currentTimeMillis() - mQuantizationMillis;
                evictionTimestamp = discretizeTimestamp(evictionTimestamp);
                for (Map.Entry<AppOpAccessEvent, AggregatedAppOpValues> opEvent :
                        snapshot.entrySet()) {
                    if (opEvent.getKey().mAccessTime <= evictionTimestamp) {
                        evictedEvents.add(
                                getAggregatedAppOpEvent(opEvent.getKey(), opEvent.getValue()));
                        mCache.remove(opEvent.getKey());
                    }
                }
                return evictedEvents;
            }
            final long evictionTimestamp = discretizeTimestamp(
                    System.currentTimeMillis() - mQuantizationMillis);
            return evict(opAccessEvent -> opAccessEvent.mAccessTime <= evictionTimestamp);
        }

        private AggregatedAppOpAccessEvent getAggregatedAppOpEvent(AppOpAccessEvent accessEvent,
                AggregatedAppOpValues appOpValues) {
            return new AggregatedAppOpAccessEvent(accessEvent.mUid, accessEvent.mPackageName,
                    accessEvent.mOpCode, accessEvent.mDeviceId, accessEvent.mAttributionTag,
                    accessEvent.mOpFlags, accessEvent.mUidState, accessEvent.mAttributionFlags,
                    accessEvent.mAttributionChainId, accessEvent.mAccessTime,
                    accessEvent.mDuration, appOpValues.mTotalDuration,
                    appOpValues.mTotalAccessCount, appOpValues.mTotalRejectCount);
        }

        /**
         * Evict specified app ops from cache, and return the list of evicted ops.
         */
        private List<AggregatedAppOpAccessEvent> evict(IntArray ops) {
        private List<AggregatedAppOpAccessEvent> evict(Predicate<AppOpAccessEvent> predicate) {
            synchronized (this) {
                List<AggregatedAppOpAccessEvent> cachedOps = new ArrayList<>();
                List<AppOpAccessEvent> keysToBeRemoved = new ArrayList<>();
                if (mCache.isEmpty()) {
                    return cachedOps;
                    return Collections.emptyList();
                }

                List<AggregatedAppOpAccessEvent> evictedOps = new ArrayList<>();
                List<AppOpAccessEvent> keysToBeRemoved = new ArrayList<>();
                for (Map.Entry<AppOpAccessEvent, AggregatedAppOpValues> event :
                        mCache.entrySet()) {
                    if (ops.contains(event.getKey().mOpCode)) {
                    if (predicate.test(event.getKey())) {
                        keysToBeRemoved.add(event.getKey());
                        cachedOps.add(getAggregatedAppOpEvent(event.getKey(), event.getValue()));
                        evictedOps.add(getAggregatedAppOpEvent(event.getKey(), event.getValue()));
                    }
                }
                if (!cachedOps.isEmpty()) {
                for (AppOpAccessEvent eventKey : keysToBeRemoved) {
                    mCache.remove(eventKey);
                }
                return evictedOps;
            }
                return cachedOps;
        }

        /**
         * Evict specified app ops from cache, and return the list of evicted ops.
         */
        private List<AggregatedAppOpAccessEvent> evict(IntArray ops) {
            return evict(appOpAccessEvent -> ops.contains(appOpAccessEvent.mOpCode));
        }

        /**
@@ -573,6 +555,16 @@ public class AppOpHistoryHelper {
            }
        }

        private AggregatedAppOpAccessEvent getAggregatedAppOpEvent(AppOpAccessEvent accessEvent,
                AggregatedAppOpValues appOpValues) {
            return new AggregatedAppOpAccessEvent(accessEvent.mUid, accessEvent.mPackageName,
                    accessEvent.mOpCode, accessEvent.mDeviceId, accessEvent.mAttributionTag,
                    accessEvent.mOpFlags, accessEvent.mUidState, accessEvent.mAttributionFlags,
                    accessEvent.mAttributionChainId, accessEvent.mAccessTime,
                    accessEvent.mDuration, appOpValues.mTotalDuration,
                    appOpValues.mTotalAccessCount, appOpValues.mTotalRejectCount);
        }

        /**
         * Remove all entries from the cache.
         */