{"action":"created","comment":{"url":"https://api.github.com/repos/ankidroid/Anki-Android/pulls/comments/56216297","id":56216297,"diff_hunk":"@@ -185,46 +164,84 @@ private Uri addNote(long did, ContentValues values) {\n      */\n     @Deprecated\n     public boolean checkForDuplicates(long mid, long did, String[] fields) {\n-        return findExistingNoteId(mid, fields) != null;\n+        List<NoteInfo> notes = findNotesByKeys(mid, Collections.singleton(fields[0]));\n+        return notes != null && !notes.isEmpty();\n     }\n \n-\n     /**\n-     * Find the note id of any existing notes which have mid and has identical first field as the input list of fields.\n-     * If multiple notes exist with the same first field, then the first such note is returned.\n-     * @param mid model id\n-     * @param fields list of fields\n-     * @return the note id or null if the note does not exist\n+     * Return all notes for the specified model that match at least one of the specified keys.\n+     * If keys is null or empty then all notes for the model are returned.\n+     *\n+     * @param modelId model id\n+     * @param keys the keys\n+     * @return the matched notes or null if model does not exist or there was some other problem\n      */\n-    public Long findExistingNoteId(long mid, String[] fields) {\n-        NoteInfo note = findExistingNote(mid, fields);\n-        if (note == null) {\n+    public List<NoteInfo> findNotesByKeys(long modelId, Collection<String> keys) {\n+        Cursor cursor;\n+        if (keys == null || keys.isEmpty()) {\n+            cursor = queryNotes(modelId);\n+        }\n+        else {\n+            cursor = getCompat().queryNotes(modelId, keys);\n+        }\n+        if (cursor == null) {\n             return null;\n         }\n-        return note.id;\n+        List<NoteInfo> result = new ArrayList<>();\n+        try {\n+            while (cursor.moveToNext()) {\n+                NoteInfo note = NoteInfo.buildFromCursor(cursor);\n+                if (note == null) {\n+                    return null;\n+                }\n+                if (keys != null && !keys.contains(note.fields[0])) {\n+                    continue;\n+                }\n+                result.add(note);\n+            }\n+        }\n+        finally {\n+            cursor.close();\n+        }\n+        return result;\n     }\n \n+    /** null means problem or model id does not exist */\n+    private Cursor queryNotes(long modelId) {\n+        String[] selectionArgs = {String.format(\"%s=%d\", FlashCardsContract.Note.MID, modelId)};\n+        Cursor cursor = mResolver.query(FlashCardsContract.Note.CONTENT_URI, PROJECTION, null, selectionArgs, null);\n+        return ensureNotesCursorAccordingToModelExistence(cursor, modelId);\n+    }\n \n-    private NoteInfo findExistingNote(long mid, String[] fields) {\n-        String[][] fieldsArray = {fields};\n-        NoteInfo[] notes = getCompat().findExistingNotes(mid, fieldsArray);\n-        if (notes == null) {\n-            return null;\n+    private Cursor ensureNotesCursorAccordingToModelExistence(Cursor cursor, long modelId) {\n+        // make sure we get the null-ness of the cursor right - provider specs behave differently","path":"api/src/main/java/com/ichi2/anki/api/AddContentApi.java","position":247,"original_position":247,"commit_id":"3a41f826d961a33b97156e82dc687fce3659d09e","original_commit_id":"3a41f826d961a33b97156e82dc687fce3659d09e","user":{"login":"marcardar","id":409640,"avatar_url":"https://avatars.githubusercontent.com/u/409640?v=3","gravatar_id":"","url":"https://api.github.com/users/marcardar","html_url":"https://github.com/marcardar","followers_url":"https://api.github.com/users/marcardar/followers","following_url":"https://api.github.com/users/marcardar/following{/other_user}","gists_url":"https://api.github.com/users/marcardar/gists{/gist_id}","starred_url":"https://api.github.com/users/marcardar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/marcardar/subscriptions","organizations_url":"https://api.github.com/users/marcardar/orgs","repos_url":"https://api.github.com/users/marcardar/repos","events_url":"https://api.github.com/users/marcardar/events{/privacy}","received_events_url":"https://api.github.com/users/marcardar/received_events","type":"User","site_admin":false},"body":"I could removed all the ensure() stuff for now, and fix it in a PR later?","created_at":"2016-03-15T18:30:57Z","updated_at":"2016-03-15T18:30:57Z","html_url":"https://github.com/ankidroid/Anki-Android/pull/4134#discussion_r56216297","pull_request_url":"https://api.github.com/repos/ankidroid/Anki-Android/pulls/4134","_links":{"self":{"href":"https://api.github.com/repos/ankidroid/Anki-Android/pulls/comments/56216297"},"html":{"href":"https://github.com/ankidroid/Anki-Android/pull/4134#discussion_r56216297"},"pull_request":{"href":"https://api.github.com/repos/ankidroid/Anki-Android/pulls/4134"}}},"pull_request":{"url":"https://api.github.com/repos/ankidroid/Anki-Android/pulls/4134","id":62869086,"html_url":"https://github.com/ankidroid/Anki-Android/pull/4134","diff_url":"https://github.com/ankidroid/Anki-Android/pull/4134.diff","patch_url":"https://github.com/ankidroid/Anki-Android/pull/4134.patch","issue_url":"https://api.github.com/repos/ankidroid/Anki-Android/issues/4134","number":4134,"state":"open","locked":false,"title":"Api: replace old findExistingNoteId() with new findNotesByKeys()","user":{"login":"marcardar","id":409640,"avatar_url":"https://avatars.githubusercontent.com/u/409640?v=3","gravatar_id":"","url":"https://api.github.com/users/marcardar","html_url":"https://github.com/marcardar","followers_url":"https://api.github.com/users/marcardar/followers","following_url":"https://api.github.com/users/marcardar/following{/other_user}","gists_url":"https://api.github.com/users/marcardar/gists{/gist_id}","starred_url":"https://api.github.com/users/marcardar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/marcardar/subscriptions","organizations_url":"https://api.github.com/users/marcardar/orgs","repos_url":"https://api.github.com/users/marcardar/repos","events_url":"https://api.github.com/users/marcardar/events{/privacy}","received_events_url":"https://api.github.com/users/marcardar/received_events","type":"User","site_admin":false},"body":"I have also removed duplicate checking from addNote() and addNotes() and removed findExistingNoteId().","created_at":"2016-03-15T08:21:10Z","updated_at":"2016-03-15T18:30:57Z","closed_at":null,"merged_at":null,"merge_commit_sha":"6e33e0b62d46a6a15e30665fc58ae131f4deeb7e","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/ankidroid/Anki-Android/pulls/4134/commits","review_comments_url":"https://api.github.com/repos/ankidroid/Anki-Android/pulls/4134/comments","review_comment_url":"https://api.github.com/repos/ankidroid/Anki-Android/pulls/comments{/number}","comments_url":"https://api.github.com/repos/ankidroid/Anki-Android/issues/4134/comments","statuses_url":"https://api.github.com/repos/ankidroid/Anki-Android/statuses/3a41f826d961a33b97156e82dc687fce3659d09e","head":{"label":"marcardar:find-notes-api","ref":"find-notes-api","sha":"3a41f826d961a33b97156e82dc687fce3659d09e","user":{"login":"marcardar","id":409640,"avatar_url":"https://avatars.githubusercontent.com/u/409640?v=3","gravatar_id":"","url":"https://api.github.com/users/marcardar","html_url":"https://github.com/marcardar","followers_url":"https://api.github.com/users/marcardar/followers","following_url":"https://api.github.com/users/marcardar/following{/other_user}","gists_url":"https://api.github.com/users/marcardar/gists{/gist_id}","starred_url":"https://api.github.com/users/marcardar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/marcardar/subscriptions","organizations_url":"https://api.github.com/users/marcardar/orgs","repos_url":"https://api.github.com/users/marcardar/repos","events_url":"https://api.github.com/users/marcardar/events{/privacy}","received_events_url":"https://api.github.com/users/marcardar/received_events","type":"User","site_admin":false},"repo":{"id":52933751,"name":"Anki-Android","full_name":"marcardar/Anki-Android","owner":{"login":"marcardar","id":409640,"avatar_url":"https://avatars.githubusercontent.com/u/409640?v=3","gravatar_id":"","url":"https://api.github.com/users/marcardar","html_url":"https://github.com/marcardar","followers_url":"https://api.github.com/users/marcardar/followers","following_url":"https://api.github.com/users/marcardar/following{/other_user}","gists_url":"https://api.github.com/users/marcardar/gists{/gist_id}","starred_url":"https://api.github.com/users/marcardar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/marcardar/subscriptions","organizations_url":"https://api.github.com/users/marcardar/orgs","repos_url":"https://api.github.com/users/marcardar/repos","events_url":"https://api.github.com/users/marcardar/events{/privacy}","received_events_url":"https://api.github.com/users/marcardar/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/marcardar/Anki-Android","description":"AnkiDroid: Anki on Android","fork":true,"url":"https://api.github.com/repos/marcardar/Anki-Android","forks_url":"https://api.github.com/repos/marcardar/Anki-Android/forks","keys_url":"https://api.github.com/repos/marcardar/Anki-Android/keys{/key_id}","collaborators_url":"https://api.github.com/repos/marcardar/Anki-Android/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/marcardar/Anki-Android/teams","hooks_url":"https://api.github.com/repos/marcardar/Anki-Android/hooks","issue_events_url":"https://api.github.com/repos/marcardar/Anki-Android/issues/events{/number}","events_url":"https://api.github.com/repos/marcardar/Anki-Android/events","assignees_url":"https://api.github.com/repos/marcardar/Anki-Android/assignees{/user}","branches_url":"https://api.github.com/repos/marcardar/Anki-Android/branches{/branch}","tags_url":"https://api.github.com/repos/marcardar/Anki-Android/tags","blobs_url":"https://api.github.com/repos/marcardar/Anki-Android/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/marcardar/Anki-Android/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/marcardar/Anki-Android/git/refs{/sha}","trees_url":"https://api.github.com/repos/marcardar/Anki-Android/git/trees{/sha}","statuses_url":"https://api.github.com/repos/marcardar/Anki-Android/statuses/{sha}","languages_url":"https://api.github.com/repos/marcardar/Anki-Android/languages","stargazers_url":"https://api.github.com/repos/marcardar/Anki-Android/stargazers","contributors_url":"https://api.github.com/repos/marcardar/Anki-Android/contributors","subscribers_url":"https://api.github.com/repos/marcardar/Anki-Android/subscribers","subscription_url":"https://api.github.com/repos/marcardar/Anki-Android/subscription","commits_url":"https://api.github.com/repos/marcardar/Anki-Android/commits{/sha}","git_commits_url":"https://api.github.com/repos/marcardar/Anki-Android/git/commits{/sha}","comments_url":"https://api.github.com/repos/marcardar/Anki-Android/comments{/number}","issue_comment_url":"https://api.github.com/repos/marcardar/Anki-Android/issues/comments{/number}","contents_url":"https://api.github.com/repos/marcardar/Anki-Android/contents/{+path}","compare_url":"https://api.github.com/repos/marcardar/Anki-Android/compare/{base}...{head}","merges_url":"https://api.github.com/repos/marcardar/Anki-Android/merges","archive_url":"https://api.github.com/repos/marcardar/Anki-Android/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/marcardar/Anki-Android/downloads","issues_url":"https://api.github.com/repos/marcardar/Anki-Android/issues{/number}","pulls_url":"https://api.github.com/repos/marcardar/Anki-Android/pulls{/number}","milestones_url":"https://api.github.com/repos/marcardar/Anki-Android/milestones{/number}","notifications_url":"https://api.github.com/repos/marcardar/Anki-Android/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/marcardar/Anki-Android/labels{/name}","releases_url":"https://api.github.com/repos/marcardar/Anki-Android/releases{/id}","deployments_url":"https://api.github.com/repos/marcardar/Anki-Android/deployments","created_at":"2016-03-02T04:26:22Z","updated_at":"2016-03-02T04:26:27Z","pushed_at":"2016-03-15T17:32:31Z","git_url":"git://github.com/marcardar/Anki-Android.git","ssh_url":"git@github.com:marcardar/Anki-Android.git","clone_url":"https://github.com/marcardar/Anki-Android.git","svn_url":"https://github.com/marcardar/Anki-Android","homepage":"","size":102634,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_downloads":false,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"develop"}},"base":{"label":"ankidroid:develop","ref":"develop","sha":"837a827738ef8ea914b91823459f0a12e0f75b4d","user":{"login":"ankidroid","id":3320903,"avatar_url":"https://avatars.githubusercontent.com/u/3320903?v=3","gravatar_id":"","url":"https://api.github.com/users/ankidroid","html_url":"https://github.com/ankidroid","followers_url":"https://api.github.com/users/ankidroid/followers","following_url":"https://api.github.com/users/ankidroid/following{/other_user}","gists_url":"https://api.github.com/users/ankidroid/gists{/gist_id}","starred_url":"https://api.github.com/users/ankidroid/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ankidroid/subscriptions","organizations_url":"https://api.github.com/users/ankidroid/orgs","repos_url":"https://api.github.com/users/ankidroid/repos","events_url":"https://api.github.com/users/ankidroid/events{/privacy}","received_events_url":"https://api.github.com/users/ankidroid/received_events","type":"Organization","site_admin":false},"repo":{"id":7716883,"name":"Anki-Android","full_name":"ankidroid/Anki-Android","owner":{"login":"ankidroid","id":3320903,"avatar_url":"https://avatars.githubusercontent.com/u/3320903?v=3","gravatar_id":"","url":"https://api.github.com/users/ankidroid","html_url":"https://github.com/ankidroid","followers_url":"https://api.github.com/users/ankidroid/followers","following_url":"https://api.github.com/users/ankidroid/following{/other_user}","gists_url":"https://api.github.com/users/ankidroid/gists{/gist_id}","starred_url":"https://api.github.com/users/ankidroid/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ankidroid/subscriptions","organizations_url":"https://api.github.com/users/ankidroid/orgs","repos_url":"https://api.github.com/users/ankidroid/repos","events_url":"https://api.github.com/users/ankidroid/events{/privacy}","received_events_url":"https://api.github.com/users/ankidroid/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/ankidroid/Anki-Android","description":"AnkiDroid: Anki on Android","fork":false,"url":"https://api.github.com/repos/ankidroid/Anki-Android","forks_url":"https://api.github.com/repos/ankidroid/Anki-Android/forks","keys_url":"https://api.github.com/repos/ankidroid/Anki-Android/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ankidroid/Anki-Android/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ankidroid/Anki-Android/teams","hooks_url":"https://api.github.com/repos/ankidroid/Anki-Android/hooks","issue_events_url":"https://api.github.com/repos/ankidroid/Anki-Android/issues/events{/number}","events_url":"https://api.github.com/repos/ankidroid/Anki-Android/events","assignees_url":"https://api.github.com/repos/ankidroid/Anki-Android/assignees{/user}","branches_url":"https://api.github.com/repos/ankidroid/Anki-Android/branches{/branch}","tags_url":"https://api.github.com/repos/ankidroid/Anki-Android/tags","blobs_url":"https://api.github.com/repos/ankidroid/Anki-Android/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ankidroid/Anki-Android/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ankidroid/Anki-Android/git/refs{/sha}","trees_url":"https://api.github.com/repos/ankidroid/Anki-Android/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ankidroid/Anki-Android/statuses/{sha}","languages_url":"https://api.github.com/repos/ankidroid/Anki-Android/languages","stargazers_url":"https://api.github.com/repos/ankidroid/Anki-Android/stargazers","contributors_url":"https://api.github.com/repos/ankidroid/Anki-Android/contributors","subscribers_url":"https://api.github.com/repos/ankidroid/Anki-Android/subscribers","subscription_url":"https://api.github.com/repos/ankidroid/Anki-Android/subscription","commits_url":"https://api.github.com/repos/ankidroid/Anki-Android/commits{/sha}","git_commits_url":"https://api.github.com/repos/ankidroid/Anki-Android/git/commits{/sha}","comments_url":"https://api.github.com/repos/ankidroid/Anki-Android/comments{/number}","issue_comment_url":"https://api.github.com/repos/ankidroid/Anki-Android/issues/comments{/number}","contents_url":"https://api.github.com/repos/ankidroid/Anki-Android/contents/{+path}","compare_url":"https://api.github.com/repos/ankidroid/Anki-Android/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ankidroid/Anki-Android/merges","archive_url":"https://api.github.com/repos/ankidroid/Anki-Android/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ankidroid/Anki-Android/downloads","issues_url":"https://api.github.com/repos/ankidroid/Anki-Android/issues{/number}","pulls_url":"https://api.github.com/repos/ankidroid/Anki-Android/pulls{/number}","milestones_url":"https://api.github.com/repos/ankidroid/Anki-Android/milestones{/number}","notifications_url":"https://api.github.com/repos/ankidroid/Anki-Android/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ankidroid/Anki-Android/labels{/name}","releases_url":"https://api.github.com/repos/ankidroid/Anki-Android/releases{/id}","deployments_url":"https://api.github.com/repos/ankidroid/Anki-Android/deployments","created_at":"2013-01-20T13:55:53Z","updated_at":"2016-03-14T15:27:41Z","pushed_at":"2016-03-15T17:32:32Z","git_url":"git://github.com/ankidroid/Anki-Android.git","ssh_url":"git@github.com:ankidroid/Anki-Android.git","clone_url":"https://github.com/ankidroid/Anki-Android.git","svn_url":"https://github.com/ankidroid/Anki-Android","homepage":"","size":124528,"stargazers_count":508,"watchers_count":508,"language":"Java","has_issues":true,"has_downloads":false,"has_wiki":true,"has_pages":false,"forks_count":414,"mirror_url":null,"open_issues_count":279,"forks":414,"open_issues":279,"watchers":508,"default_branch":"develop"}},"_links":{"self":{"href":"https://api.github.com/repos/ankidroid/Anki-Android/pulls/4134"},"html":{"href":"https://github.com/ankidroid/Anki-Android/pull/4134"},"issue":{"href":"https://api.github.com/repos/ankidroid/Anki-Android/issues/4134"},"comments":{"href":"https://api.github.com/repos/ankidroid/Anki-Android/issues/4134/comments"},"review_comments":{"href":"https://api.github.com/repos/ankidroid/Anki-Android/pulls/4134/comments"},"review_comment":{"href":"https://api.github.com/repos/ankidroid/Anki-Android/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/ankidroid/Anki-Android/pulls/4134/commits"},"statuses":{"href":"https://api.github.com/repos/ankidroid/Anki-Android/statuses/3a41f826d961a33b97156e82dc687fce3659d09e"}}}}