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

Commit 2e0f6715 authored by Marco Nelissen's avatar Marco Nelissen Committed by Android (Google) Code Review
Browse files

Merge "Further optimize media scanner."

parents 75400e77 d121cfcb
Loading
Loading
Loading
Loading
+27 −6
Original line number Original line Diff line number Diff line
@@ -34,6 +34,8 @@ import java.util.List;
public class MediaInserter {
public class MediaInserter {
    private final HashMap<Uri, List<ContentValues>> mRowMap =
    private final HashMap<Uri, List<ContentValues>> mRowMap =
            new HashMap<Uri, List<ContentValues>>();
            new HashMap<Uri, List<ContentValues>>();
    private final HashMap<Uri, List<ContentValues>> mPriorityRowMap =
            new HashMap<Uri, List<ContentValues>>();


    private IContentProvider mProvider;
    private IContentProvider mProvider;
    private int mBufferSizePerUri;
    private int mBufferSizePerUri;
@@ -44,26 +46,45 @@ public class MediaInserter {
    }
    }


    public void insert(Uri tableUri, ContentValues values) throws RemoteException {
    public void insert(Uri tableUri, ContentValues values) throws RemoteException {
        List<ContentValues> list = mRowMap.get(tableUri);
        insert(tableUri, values, false);
    }

    public void insertwithPriority(Uri tableUri, ContentValues values) throws RemoteException {
        insert(tableUri, values, true);
    }

    private void insert(Uri tableUri, ContentValues values, boolean priority) throws RemoteException {
        HashMap<Uri, List<ContentValues>> rowmap = priority ? mPriorityRowMap : mRowMap;
        List<ContentValues> list = rowmap.get(tableUri);
        if (list == null) {
        if (list == null) {
            list = new ArrayList<ContentValues>();
            list = new ArrayList<ContentValues>();
            mRowMap.put(tableUri, list);
            rowmap.put(tableUri, list);
        }
        }
        list.add(new ContentValues(values));
        list.add(new ContentValues(values));
        if (list.size() >= mBufferSizePerUri) {
        if (list.size() >= mBufferSizePerUri) {
            flush(tableUri);
            flushAllPriority();
            flush(tableUri, list);
        }
        }
    }
    }


    public void flushAll() throws RemoteException {
    public void flushAll() throws RemoteException {
        flushAllPriority();
        for (Uri tableUri : mRowMap.keySet()){
        for (Uri tableUri : mRowMap.keySet()){
            flush(tableUri);
            List<ContentValues> list = mRowMap.get(tableUri);
            flush(tableUri, list);
        }
        }
        mRowMap.clear();
        mRowMap.clear();
    }
    }


    private void flush(Uri tableUri) throws RemoteException {
    private void flushAllPriority() throws RemoteException {
        List<ContentValues> list = mRowMap.get(tableUri);
        for (Uri tableUri : mPriorityRowMap.keySet()){
            List<ContentValues> list = mPriorityRowMap.get(tableUri);
            flush(tableUri, list);
        }
        mPriorityRowMap.clear();
    }

    private void flush(Uri tableUri, List<ContentValues> list) throws RemoteException {
        if (!list.isEmpty()) {
        if (!list.isEmpty()) {
            ContentValues[] valuesArray = new ContentValues[list.size()];
            ContentValues[] valuesArray = new ContentValues[list.size()];
            valuesArray = list.toArray(valuesArray);
            valuesArray = list.toArray(valuesArray);
+28 −20
Original line number Original line Diff line number Diff line
@@ -59,6 +59,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashMap;
import java.util.HashSet;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Locale;


/**
/**
@@ -374,7 +375,7 @@ public class MediaScanner


    // hashes file path to FileCacheEntry.
    // hashes file path to FileCacheEntry.
    // path should be lower case if mCaseInsensitivePaths is true
    // path should be lower case if mCaseInsensitivePaths is true
    private HashMap<String, FileCacheEntry> mFileCache;
    private LinkedHashMap<String, FileCacheEntry> mFileCache;


    private ArrayList<FileCacheEntry> mPlayLists;
    private ArrayList<FileCacheEntry> mPlayLists;


@@ -922,16 +923,15 @@ public class MediaScanner
                    }
                    }
                }
                }


                // new file, insert it
                // New file, insert it.
                // We insert directories immediately to ensure they are in the database
                // Directories need to be inserted before the files they contain, so they
                // before the files they contain.
                // get priority when bulk inserting.
                // Otherwise we can get duplicate directory entries in the database
                // If the rowId of the inserted file is needed, it gets inserted immediately,
                // if one of the media FileInserters is flushed before the files table FileInserter
                // bypassing the bulk inserter.
                // Also, we immediately insert the file if the rowId of the inserted file is
                if (inserter == null || needToSetSettings) {
                // needed.
                if (inserter == null || needToSetSettings ||
                        entry.mFormat == MtpConstants.FORMAT_ASSOCIATION) {
                    result = mMediaProvider.insert(tableUri, values);
                    result = mMediaProvider.insert(tableUri, values);
                } else if (entry.mFormat == MtpConstants.FORMAT_ASSOCIATION) {
                    inserter.insertwithPriority(tableUri, values);
                } else {
                } else {
                    inserter.insert(tableUri, values);
                    inserter.insert(tableUri, values);
                }
                }
@@ -1029,7 +1029,7 @@ public class MediaScanner
        String[] selectionArgs = null;
        String[] selectionArgs = null;


        if (mFileCache == null) {
        if (mFileCache == null) {
            mFileCache = new HashMap<String, FileCacheEntry>();
            mFileCache = new LinkedHashMap<String, FileCacheEntry>();
        } else {
        } else {
            mFileCache.clear();
            mFileCache.clear();
        }
        }
@@ -1151,7 +1151,8 @@ public class MediaScanner
    }
    }


    static class MediaBulkDeleter {
    static class MediaBulkDeleter {
        StringBuilder idList = new StringBuilder();
        StringBuilder whereClause = new StringBuilder();
        ArrayList<String> whereArgs = new ArrayList<String>(100); 
        IContentProvider mProvider;
        IContentProvider mProvider;
        Uri mBaseUri;
        Uri mBaseUri;


@@ -1161,19 +1162,26 @@ public class MediaScanner
        }
        }


        public void delete(long id) throws RemoteException {
        public void delete(long id) throws RemoteException {
            if (idList.length() != 0) {
            if (whereClause.length() != 0) {
                idList.append(",");
                whereClause.append(",");
            }
            }
            idList.append(id);
            whereClause.append("?");
            if (idList.length() > 1024) {
            whereArgs.add("" + id);
            if (whereArgs.size() > 100) {
                flush();
                flush();
            }
            }
        }
        }
        public void flush() throws RemoteException {
        public void flush() throws RemoteException {
            int size = whereArgs.size();
            if (size > 0) {
                String [] foo = new String [size];
                foo = whereArgs.toArray(foo);
                int numrows = mProvider.delete(mBaseUri, MediaStore.MediaColumns._ID + " IN (" +
                int numrows = mProvider.delete(mBaseUri, MediaStore.MediaColumns._ID + " IN (" +
                    idList.toString() + ")", null);
                        whereClause.toString() + ")", foo);
                //Log.i("@@@@@@@@@", "rows deleted: " + numrows);
                //Log.i("@@@@@@@@@", "rows deleted: " + numrows);
            idList.setLength(0);
                whereClause.setLength(0);
                whereArgs.clear();
            }
        }
        }
    }
    }