{"action":"created","comment":{"url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/pulls/comments/48070787","id":48070787,"diff_hunk":"@@ -250,48 +255,115 @@ public void updateIndex() throws CouchbaseLiteException {\n         }\n \n         store.beginTransaction();\n-        Status result = new Status(Status.INTERNAL_SERVER_ERROR);\n+        boolean success = false;\n         Cursor cursor = null;\n-\n         try {\n-            long last = getLastSequenceIndexed();\n-            long dbMaxSequence = store.getLastSequence();\n+            // If the view the update is for doesn't need any update, don't do anything:\n+            final long dbMaxSequence = store.getLastSequence();\n+            final long forViewLastSequence = getLastSequenceIndexed();\n+            if (forViewLastSequence >= dbMaxSequence) {\n+                success = true;\n+                return new Status(Status.NOT_MODIFIED);\n+            }\n+\n+            // Check whether we need to update at all,\n+            // and remove obsolete emitted results from the 'maps' table:\n             long minLastSequence = dbMaxSequence;\n+            long[] viewLastSequence = new long[inputViews.size()];\n+            int deletedCount = 0;\n+            int i = 0;\n+            HashSet<String> docTypes = new HashSet<String>();\n+            HashMap<String, String> viewDocTypes = null;\n+            boolean allDocTypes = false;\n+            HashMap<Integer, Integer> viewTotalRows = new HashMap<Integer, Integer>();\n+            ArrayList<SQLiteViewStore> views = new ArrayList<SQLiteViewStore>();\n+            ArrayList<Mapper> mapBlocks = new ArrayList<Mapper>();\n+\n+            for (ViewStore v : inputViews) {\n+                SQLiteViewStore view = (SQLiteViewStore)v;\n+                ViewStoreDelegate delegate = view.getDelegate();\n+                Mapper map = delegate.getMap();\n+                if (map == null) {\n+                    if (view == this) {\n+                        String msg = String.format(\"Cannot index view %s: \" +\n+                                \"no map block registered\", view.getName());\n+                        Log.e(Log.TAG_VIEW, msg);\n+                        throw new CouchbaseLiteException(msg, new Status(Status.BAD_REQUEST));\n+                    }\n+                    Log.v(Log.TAG_VIEW, \"    %s has no map block; skipping it\", view.getName());\n+                    continue;\n+                }\n+\n+                views.add(view);\n+                mapBlocks.add(map);\n+\n+                int viewID = view.getViewID();\n+                if (viewID <= 0) {\n+                    String message = String.format(\"View '%s' not found in database\",\n+                            view.getName());\n+                    Log.e(Log.TAG_VIEW, message);\n+                    throw new CouchbaseLiteException(message, new Status(Status.NOT_FOUND));\n+                }\n+\n+                int totalRows = view.getTotalRows();\n+                viewTotalRows.put(viewID, totalRows);\n+\n+                long last = view == this ? forViewLastSequence : view.getLastSequenceIndexed();","path":"src/main/java/com/couchbase/lite/store/SQLiteViewStore.java","position":99,"original_position":99,"commit_id":"6c1e7170c64b1fe60dd1f17e9e846e111735134d","original_commit_id":"6c1e7170c64b1fe60dd1f17e9e846e111735134d","user":{"login":"pasin","id":801454,"avatar_url":"https://avatars.githubusercontent.com/u/801454?v=3","gravatar_id":"","url":"https://api.github.com/users/pasin","html_url":"https://github.com/pasin","followers_url":"https://api.github.com/users/pasin/followers","following_url":"https://api.github.com/users/pasin/following{/other_user}","gists_url":"https://api.github.com/users/pasin/gists{/gist_id}","starred_url":"https://api.github.com/users/pasin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pasin/subscriptions","organizations_url":"https://api.github.com/users/pasin/orgs","repos_url":"https://api.github.com/users/pasin/repos","events_url":"https://api.github.com/users/pasin/events{/privacy}","received_events_url":"https://api.github.com/users/pasin/received_events","type":"User","site_admin":false},"body":"It's simpler, but it's better to not calling `view.getLastSequenceIndexed()` to query value from the database again. So let's keep it that way.","created_at":"2015-12-18T21:14:47Z","updated_at":"2015-12-18T21:14:47Z","html_url":"https://github.com/couchbase/couchbase-lite-java-core/pull/876#discussion_r48070787","pull_request_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/pulls/876","_links":{"self":{"href":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/pulls/comments/48070787"},"html":{"href":"https://github.com/couchbase/couchbase-lite-java-core/pull/876#discussion_r48070787"},"pull_request":{"href":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/pulls/876"}}},"pull_request":{"url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/pulls/876","id":54137525,"html_url":"https://github.com/couchbase/couchbase-lite-java-core/pull/876","diff_url":"https://github.com/couchbase/couchbase-lite-java-core/pull/876.diff","patch_url":"https://github.com/couchbase/couchbase-lite-java-core/pull/876.patch","issue_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/issues/876","number":876,"state":"open","locked":false,"title":"Fixed #681 and #682 Catch up View.updateIndexes for SQLite View Storage","user":{"login":"pasin","id":801454,"avatar_url":"https://avatars.githubusercontent.com/u/801454?v=3","gravatar_id":"","url":"https://api.github.com/users/pasin","html_url":"https://github.com/pasin","followers_url":"https://api.github.com/users/pasin/followers","following_url":"https://api.github.com/users/pasin/following{/other_user}","gists_url":"https://api.github.com/users/pasin/gists{/gist_id}","starred_url":"https://api.github.com/users/pasin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pasin/subscriptions","organizations_url":"https://api.github.com/users/pasin/orgs","repos_url":"https://api.github.com/users/pasin/repos","events_url":"https://api.github.com/users/pasin/events{/privacy}","received_events_url":"https://api.github.com/users/pasin/received_events","type":"User","site_admin":false},"body":"- Implemented group view index, views whose name has the same prefix before slash will bet indexed together #681.\r\n\r\n- _conflict map function is implmeneted #682.","created_at":"2015-12-18T19:02:54Z","updated_at":"2015-12-18T21:14:47Z","closed_at":null,"merged_at":null,"merge_commit_sha":"0152367e5f9475528ffc2e1c526e265d5d0e99bc","assignee":{"login":"hideki","id":15889,"avatar_url":"https://avatars.githubusercontent.com/u/15889?v=3","gravatar_id":"","url":"https://api.github.com/users/hideki","html_url":"https://github.com/hideki","followers_url":"https://api.github.com/users/hideki/followers","following_url":"https://api.github.com/users/hideki/following{/other_user}","gists_url":"https://api.github.com/users/hideki/gists{/gist_id}","starred_url":"https://api.github.com/users/hideki/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hideki/subscriptions","organizations_url":"https://api.github.com/users/hideki/orgs","repos_url":"https://api.github.com/users/hideki/repos","events_url":"https://api.github.com/users/hideki/events{/privacy}","received_events_url":"https://api.github.com/users/hideki/received_events","type":"User","site_admin":false},"milestone":null,"commits_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/pulls/876/commits","review_comments_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/pulls/876/comments","review_comment_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/pulls/comments{/number}","comments_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/issues/876/comments","statuses_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/statuses/6c1e7170c64b1fe60dd1f17e9e846e111735134d","head":{"label":"couchbase:feature/group_index","ref":"feature/group_index","sha":"6c1e7170c64b1fe60dd1f17e9e846e111735134d","user":{"login":"couchbase","id":605755,"avatar_url":"https://avatars.githubusercontent.com/u/605755?v=3","gravatar_id":"","url":"https://api.github.com/users/couchbase","html_url":"https://github.com/couchbase","followers_url":"https://api.github.com/users/couchbase/followers","following_url":"https://api.github.com/users/couchbase/following{/other_user}","gists_url":"https://api.github.com/users/couchbase/gists{/gist_id}","starred_url":"https://api.github.com/users/couchbase/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/couchbase/subscriptions","organizations_url":"https://api.github.com/users/couchbase/orgs","repos_url":"https://api.github.com/users/couchbase/repos","events_url":"https://api.github.com/users/couchbase/events{/privacy}","received_events_url":"https://api.github.com/users/couchbase/received_events","type":"Organization","site_admin":false},"repo":{"id":12310030,"name":"couchbase-lite-java-core","full_name":"couchbase/couchbase-lite-java-core","owner":{"login":"couchbase","id":605755,"avatar_url":"https://avatars.githubusercontent.com/u/605755?v=3","gravatar_id":"","url":"https://api.github.com/users/couchbase","html_url":"https://github.com/couchbase","followers_url":"https://api.github.com/users/couchbase/followers","following_url":"https://api.github.com/users/couchbase/following{/other_user}","gists_url":"https://api.github.com/users/couchbase/gists{/gist_id}","starred_url":"https://api.github.com/users/couchbase/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/couchbase/subscriptions","organizations_url":"https://api.github.com/users/couchbase/orgs","repos_url":"https://api.github.com/users/couchbase/repos","events_url":"https://api.github.com/users/couchbase/events{/privacy}","received_events_url":"https://api.github.com/users/couchbase/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/couchbase/couchbase-lite-java-core","description":"Couchbase Lite Java core library","fork":false,"url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core","forks_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/forks","keys_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/keys{/key_id}","collaborators_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/teams","hooks_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/hooks","issue_events_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/issues/events{/number}","events_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/events","assignees_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/assignees{/user}","branches_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/branches{/branch}","tags_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/tags","blobs_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/git/refs{/sha}","trees_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/git/trees{/sha}","statuses_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/statuses/{sha}","languages_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/languages","stargazers_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/stargazers","contributors_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/contributors","subscribers_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/subscribers","subscription_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/subscription","commits_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/commits{/sha}","git_commits_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/git/commits{/sha}","comments_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/comments{/number}","issue_comment_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/issues/comments{/number}","contents_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/contents/{+path}","compare_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/compare/{base}...{head}","merges_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/merges","archive_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/downloads","issues_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/issues{/number}","pulls_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/pulls{/number}","milestones_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/milestones{/number}","notifications_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/labels{/name}","releases_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/releases{/id}","created_at":"2013-08-22T23:13:23Z","updated_at":"2015-12-16T08:53:49Z","pushed_at":"2015-12-18T19:02:54Z","git_url":"git://github.com/couchbase/couchbase-lite-java-core.git","ssh_url":"git@github.com:couchbase/couchbase-lite-java-core.git","clone_url":"https://github.com/couchbase/couchbase-lite-java-core.git","svn_url":"https://github.com/couchbase/couchbase-lite-java-core","homepage":"","size":9545,"stargazers_count":34,"watchers_count":34,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":57,"mirror_url":null,"open_issues_count":143,"forks":57,"open_issues":143,"watchers":34,"default_branch":"master"}},"base":{"label":"couchbase:master","ref":"master","sha":"4912e6642852566d526d886c557865733bb36da6","user":{"login":"couchbase","id":605755,"avatar_url":"https://avatars.githubusercontent.com/u/605755?v=3","gravatar_id":"","url":"https://api.github.com/users/couchbase","html_url":"https://github.com/couchbase","followers_url":"https://api.github.com/users/couchbase/followers","following_url":"https://api.github.com/users/couchbase/following{/other_user}","gists_url":"https://api.github.com/users/couchbase/gists{/gist_id}","starred_url":"https://api.github.com/users/couchbase/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/couchbase/subscriptions","organizations_url":"https://api.github.com/users/couchbase/orgs","repos_url":"https://api.github.com/users/couchbase/repos","events_url":"https://api.github.com/users/couchbase/events{/privacy}","received_events_url":"https://api.github.com/users/couchbase/received_events","type":"Organization","site_admin":false},"repo":{"id":12310030,"name":"couchbase-lite-java-core","full_name":"couchbase/couchbase-lite-java-core","owner":{"login":"couchbase","id":605755,"avatar_url":"https://avatars.githubusercontent.com/u/605755?v=3","gravatar_id":"","url":"https://api.github.com/users/couchbase","html_url":"https://github.com/couchbase","followers_url":"https://api.github.com/users/couchbase/followers","following_url":"https://api.github.com/users/couchbase/following{/other_user}","gists_url":"https://api.github.com/users/couchbase/gists{/gist_id}","starred_url":"https://api.github.com/users/couchbase/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/couchbase/subscriptions","organizations_url":"https://api.github.com/users/couchbase/orgs","repos_url":"https://api.github.com/users/couchbase/repos","events_url":"https://api.github.com/users/couchbase/events{/privacy}","received_events_url":"https://api.github.com/users/couchbase/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/couchbase/couchbase-lite-java-core","description":"Couchbase Lite Java core library","fork":false,"url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core","forks_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/forks","keys_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/keys{/key_id}","collaborators_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/teams","hooks_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/hooks","issue_events_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/issues/events{/number}","events_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/events","assignees_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/assignees{/user}","branches_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/branches{/branch}","tags_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/tags","blobs_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/git/refs{/sha}","trees_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/git/trees{/sha}","statuses_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/statuses/{sha}","languages_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/languages","stargazers_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/stargazers","contributors_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/contributors","subscribers_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/subscribers","subscription_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/subscription","commits_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/commits{/sha}","git_commits_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/git/commits{/sha}","comments_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/comments{/number}","issue_comment_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/issues/comments{/number}","contents_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/contents/{+path}","compare_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/compare/{base}...{head}","merges_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/merges","archive_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/downloads","issues_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/issues{/number}","pulls_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/pulls{/number}","milestones_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/milestones{/number}","notifications_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/labels{/name}","releases_url":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/releases{/id}","created_at":"2013-08-22T23:13:23Z","updated_at":"2015-12-16T08:53:49Z","pushed_at":"2015-12-18T19:02:54Z","git_url":"git://github.com/couchbase/couchbase-lite-java-core.git","ssh_url":"git@github.com:couchbase/couchbase-lite-java-core.git","clone_url":"https://github.com/couchbase/couchbase-lite-java-core.git","svn_url":"https://github.com/couchbase/couchbase-lite-java-core","homepage":"","size":9545,"stargazers_count":34,"watchers_count":34,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":57,"mirror_url":null,"open_issues_count":143,"forks":57,"open_issues":143,"watchers":34,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/pulls/876"},"html":{"href":"https://github.com/couchbase/couchbase-lite-java-core/pull/876"},"issue":{"href":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/issues/876"},"comments":{"href":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/issues/876/comments"},"review_comments":{"href":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/pulls/876/comments"},"review_comment":{"href":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/pulls/876/commits"},"statuses":{"href":"https://api.github.com/repos/couchbase/couchbase-lite-java-core/statuses/6c1e7170c64b1fe60dd1f17e9e846e111735134d"}}}}