Search in sources :

Example 51 with Collection

use of com.ichi2.libanki.Collection in project Anki-Android by Ramblurr.

the class Finder method findDupes.

public static List<Pair<String, List<Long>>> findDupes(Collection col, String fieldName, String search) {
    // limit search to notes with applicable field name
    if (search != null && search.length() > 0) {
        search = "(" + search + ") ";
    }
    search += "'" + fieldName + ":*'";
    // go through notes
    String sql = "select id, mid, flds from notes where id in " + Utils.ids2str(col.findNotes(search).toArray(new Long[] {}));
    Cursor cur = null;
    Map<Long, Integer> fields = new HashMap<Long, Integer>();
    Map<String, List<Long>> vals = new HashMap<String, List<Long>>();
    List<Pair<String, List<Long>>> dupes = new ArrayList<Pair<String, List<Long>>>();
    try {
        cur = col.getDb().getDatabase().rawQuery(sql, null);
        while (cur.moveToNext()) {
            long nid = cur.getLong(0);
            long mid = cur.getLong(1);
            String[] flds = Utils.splitFields(cur.getString(2));
            // inlined ordForMid(mid)
            if (!fields.containsKey(mid)) {
                JSONObject model = col.getModels().get(mid);
                fields.put(mid, col.getModels().fieldMap(model).get(fieldName).first);
            }
            String val = flds[fields.get(mid)];
            // empty does not count as duplicate
            if (val.equals("")) {
                continue;
            }
            if (!vals.containsKey(val)) {
                vals.put(val, new ArrayList<Long>());
            }
            vals.get(val).add(nid);
            if (vals.get(val).size() == 2) {
                dupes.add(new Pair<String, List<Long>>(val, vals.get(val)));
            }
        }
    } finally {
        if (cur != null) {
            cur.close();
        }
    }
    return dupes;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Pair(com.ichi2.anki.Pair)

Aggregations

Collection (com.ichi2.libanki.Collection)40 JSONObject (org.json.JSONObject)20 File (java.io.File)13 JSONException (org.json.JSONException)12 IOException (java.io.IOException)11 Resources (android.content.res.Resources)10 DialogInterface (android.content.DialogInterface)6 DeckTask (com.ichi2.async.DeckTask)6 TaskData (com.ichi2.async.DeckTask.TaskData)6 StyledDialog (com.ichi2.themes.StyledDialog)6 AnkiDb (com.ichi2.anki.AnkiDb)5 FileInputStream (java.io.FileInputStream)5 ArrayList (java.util.ArrayList)5 Intent (android.content.Intent)4 Note (com.ichi2.libanki.Note)4 FileNotFoundException (java.io.FileNotFoundException)4 FileOutputStream (java.io.FileOutputStream)4 InputStream (java.io.InputStream)4 JSONArray (org.json.JSONArray)4 OnCancelListener (android.content.DialogInterface.OnCancelListener)3