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

Commit a987949a authored by Shashwat Razdan's avatar Shashwat Razdan Committed by Automerger Merge Worker
Browse files

Merge "Returning the evicted element from CircularQueue. Calling destroy() on...

Merge "Returning the evicted element from CircularQueue. Calling destroy() on the returned element in CloudSearchPerUserService" into tm-dev am: dafb69ab

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/18004261



Change-Id: Idd057504e56a2ce8bd8aa39c49d4077ae683d946
Signed-off-by: default avatarAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
parents 97443b40 dafb69ab
Loading
Loading
Loading
Loading
+4 −1
Original line number Original line Diff line number Diff line
@@ -147,7 +147,10 @@ public class CloudSearchPerUserService extends
                }
                }
            });
            });
            if (sessionInfo.linkToDeath()) {
            if (sessionInfo.linkToDeath()) {
                mCallbackQueue.put(requestId, sessionInfo);
                CloudSearchCallbackInfo removedInfo = mCallbackQueue.put(requestId, sessionInfo);
                if (removedInfo != null) {
                    removedInfo.destroy();
                }
            } else {
            } else {
                // destroy the session if calling process is already dead
                // destroy the session if calling process is already dead
                onDestroyLocked(requestId);
                onDestroyLocked(requestId);
+8 −4
Original line number Original line Diff line number Diff line
@@ -16,6 +16,7 @@


package com.android.server;
package com.android.server;


import android.annotation.Nullable;
import android.util.ArrayMap;
import android.util.ArrayMap;


import java.util.Collection;
import java.util.Collection;
@@ -43,16 +44,19 @@ public class CircularQueue<K, V> extends LinkedList<K> {
    /**
    /**
     * Put a (key|value) pair in the CircularQueue. Only the key will be added to the queue. Value
     * Put a (key|value) pair in the CircularQueue. Only the key will be added to the queue. Value
     * will be added to the ArrayMap.
     * will be added to the ArrayMap.
     * @return {@code true} (as specified by {@link Collection#add})
     * @return the most recently removed value if keys were removed, or {@code null} if no keys were
     * removed.
     */
     */
    public boolean put(K key, V value) {
    @Nullable
    public V put(K key, V value) {
        super.add(key);
        super.add(key);
        mArrayMap.put(key, value);
        mArrayMap.put(key, value);
        V removedValue = null;
        while (size() > mLimit) {
        while (size() > mLimit) {
            K removedKey = super.remove();
            K removedKey = super.remove();
            mArrayMap.remove(removedKey);
            removedValue = mArrayMap.remove(removedKey);
        }
        }
        return true;
        return removedValue;
    }
    }


    /**
    /**
+3 −2
Original line number Original line Diff line number Diff line
@@ -43,11 +43,12 @@ public class CircularQueueTest {
        mQueue = new CircularQueue<>(LIMIT);
        mQueue = new CircularQueue<>(LIMIT);
        mQueue.put(1, "A");
        mQueue.put(1, "A");
        mQueue.put(2, "B");
        mQueue.put(2, "B");
        mQueue.put(3, "C");
        String removedElement = mQueue.put(3, "C");
        assertNull(mQueue.getElement(1));
        assertNull(mQueue.getElement(1));
        assertEquals(mQueue.getElement(2), "B");
        assertEquals(mQueue.getElement(2), "B");
        assertEquals(mQueue.getElement(3), "C");
        assertEquals(mQueue.getElement(3), "C");

        // Confirming that put is returning the deleted element
        assertEquals(removedElement, "A");
    }
    }


    @Test
    @Test