Files
frameworks_base/media/java/android/media/MediaInserter.java
Marco Nelissen d121cfcbb4 Further optimize media scanner.
Inserts of directories can be done in bulk as long as they're inserted before
the files contained within. Extend MediaInserter to accommodate giving priority
treatment to directories.
Bulk deleting of entries can be further sped up (by a factor of ~3 in my tests)
by deleting entries in database order. Switch the file cache to use
LinkedHashMap instead of HashMap to allow iterating over the cache in database
order. Also use bindArgs to allow for better caching of sql statements.

Change-Id: Ieb9ffc4e866c6cd505bf795eb80ff5d03ffc56bd
2012-02-13 15:39:15 -08:00

96 lines
3.2 KiB
Java

/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.media;
import android.content.ContentValues;
import android.content.IContentProvider;
import android.net.Uri;
import android.os.RemoteException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* A MediaScanner helper class which enables us to do lazy insertion on the
* given provider. This class manages buffers internally and flushes when they
* are full. Note that you should call flushAll() after using this class.
* {@hide}
*/
public class MediaInserter {
private final HashMap<Uri, List<ContentValues>> mRowMap =
new HashMap<Uri, List<ContentValues>>();
private final HashMap<Uri, List<ContentValues>> mPriorityRowMap =
new HashMap<Uri, List<ContentValues>>();
private IContentProvider mProvider;
private int mBufferSizePerUri;
public MediaInserter(IContentProvider provider, int bufferSizePerUri) {
mProvider = provider;
mBufferSizePerUri = bufferSizePerUri;
}
public void insert(Uri tableUri, ContentValues values) throws RemoteException {
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) {
list = new ArrayList<ContentValues>();
rowmap.put(tableUri, list);
}
list.add(new ContentValues(values));
if (list.size() >= mBufferSizePerUri) {
flushAllPriority();
flush(tableUri, list);
}
}
public void flushAll() throws RemoteException {
flushAllPriority();
for (Uri tableUri : mRowMap.keySet()){
List<ContentValues> list = mRowMap.get(tableUri);
flush(tableUri, list);
}
mRowMap.clear();
}
private void flushAllPriority() throws RemoteException {
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()) {
ContentValues[] valuesArray = new ContentValues[list.size()];
valuesArray = list.toArray(valuesArray);
mProvider.bulkInsert(tableUri, valuesArray);
list.clear();
}
}
}