{"action":"created","comment":{"url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/pulls/comments/77530728","id":77530728,"diff_hunk":"@@ -0,0 +1,246 @@\n+package com.couchbase.client.commons;\n+\n+import java.util.AbstractSet;\n+import java.util.ConcurrentModificationException;\n+import java.util.Iterator;\n+import java.util.Set;\n+\n+import com.couchbase.client.core.message.ResponseStatus;\n+import com.couchbase.client.core.message.kv.subdoc.multi.Lookup;\n+import com.couchbase.client.core.message.kv.subdoc.multi.Mutation;\n+import com.couchbase.client.java.Bucket;\n+import com.couchbase.client.java.document.JsonArrayDocument;\n+import com.couchbase.client.java.document.json.JsonArray;\n+import com.couchbase.client.java.document.json.JsonObject;\n+import com.couchbase.client.java.error.CASMismatchException;\n+import com.couchbase.client.java.error.DocumentAlreadyExistsException;\n+import com.couchbase.client.java.error.subdoc.MultiMutationException;\n+import com.couchbase.client.java.subdoc.DocumentFragment;\n+\n+/**\n+ * A CouchbaseArraySet is a {@link Set} backed by a {@link Bucket Couchbase} document (more\n+ * specifically a {@link JsonArrayDocument JSON array}).\n+ *\n+ * Note that as such, a CouchbaseArraySet is restricted to the types that a {@link JsonArray JSON array}\n+ * can contain. JSON objects and sub-arrays can be represented as {@link JsonObject} and {@link JsonArray}\n+ * respectively. Similarly, null entries are not supported.\n+ *\n+ * @param <T> the type of values in the set.\n+ */\n+public class CouchbaseArraySet<T> extends AbstractSet<T> {\n+\n+    public static final int MAX_OPTIMISTIC_LOCKING_ATTEMPTS = CouchbaseArrayList.MAX_OPTIMISTIC_LOCKING_ATTEMPTS;\n+\n+    private final String id;\n+    private final Bucket bucket;\n+\n+    /**\n+     * Create a new {@link CouchbaseArraySet}, backed by the document identified by <code>id</code>\n+     * in the given Couchbase <code>bucket</code>. Note that if the document already exists,\n+     * its content will be used as initial content for this collection. Otherwise it is created empty.\n+     *\n+     * @param id the id of the Couchbase document to back the set.\n+     * @param bucket the {@link Bucket} through which to interact with the document.\n+     */\n+    public CouchbaseArraySet(String id, Bucket bucket) {\n+        this.id = id;\n+        this.bucket = bucket;\n+\n+        try {\n+            this.bucket.insert(JsonArrayDocument.create(id));\n+        } catch (DocumentAlreadyExistsException e) {\n+            //use a pre-existing document\n+        }\n+    }\n+\n+    /**\n+     * Create a new {@link CouchbaseArraySet}, backed by the document identified by <code>id</code>\n+     * in the given Couchbase <code>bucket</code>. Note that if the document already exists, its content is\n+     * reset to the values copied from the given <code>data</code> Map.\n+     *\n+     * A null or empty data map will re-initialize any pre-existing document to an empty content.\n+     *\n+     * @param id the id of the Couchbase document to back the set.\n+     * @param bucket the {@link Bucket} through which to interact with the document.\n+     * @param initialData Set of the elements to initially store in the CouchbaseArraySet.\n+     */\n+    public CouchbaseArraySet(String id, Bucket bucket, Set<? extends T> initialData) {\n+        this.id = id;\n+        this.bucket = bucket;\n+\n+        JsonArray data = JsonArray.create();\n+        if (initialData != null && !initialData.isEmpty()) {\n+            for (Object o : data) {\n+                data.add(o);\n+            }\n+        }\n+        bucket.upsert(JsonArrayDocument.create(id, data));\n+    }\n+\n+    @Override\n+    public int size() {\n+        //TODO use subdoc GET_COUNT when available\n+        JsonArrayDocument current = bucket.get(id, JsonArrayDocument.class);","path":"src/main/java/com/couchbase/client/commons/CouchbaseArraySet.java","position":83,"original_position":83,"commit_id":"ae9c1e284756c4300c96f8509af6a8a2a103953e","original_commit_id":"ae9c1e284756c4300c96f8509af6a8a2a103953e","user":{"login":"daschl","id":29086,"avatar_url":"https://avatars.githubusercontent.com/u/29086?v=3","gravatar_id":"","url":"https://api.github.com/users/daschl","html_url":"https://github.com/daschl","followers_url":"https://api.github.com/users/daschl/followers","following_url":"https://api.github.com/users/daschl/following{/other_user}","gists_url":"https://api.github.com/users/daschl/gists{/gist_id}","starred_url":"https://api.github.com/users/daschl/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/daschl/subscriptions","organizations_url":"https://api.github.com/users/daschl/orgs","repos_url":"https://api.github.com/users/daschl/repos","events_url":"https://api.github.com/users/daschl/events{/privacy}","received_events_url":"https://api.github.com/users/daschl/received_events","type":"User","site_admin":false},"body":"I wonder if stuff like this and empty() all of those that are similar could be refactored into a helper class? so once GET_COUNT and friends come along we only fix it in one place?","created_at":"2016-09-05T14:35:53Z","updated_at":"2016-09-05T14:35:53Z","html_url":"https://github.com/couchbaselabs/cbd-commons-java/pull/7#discussion_r77530728","pull_request_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/pulls/7","_links":{"self":{"href":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/pulls/comments/77530728"},"html":{"href":"https://github.com/couchbaselabs/cbd-commons-java/pull/7#discussion_r77530728"},"pull_request":{"href":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/pulls/7"}}},"pull_request":{"url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/pulls/7","id":84009883,"html_url":"https://github.com/couchbaselabs/cbd-commons-java/pull/7","diff_url":"https://github.com/couchbaselabs/cbd-commons-java/pull/7.diff","patch_url":"https://github.com/couchbaselabs/cbd-commons-java/pull/7.patch","issue_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/issues/7","number":7,"state":"open","locked":false,"title":"Initial implementation of an JSON array based Set","user":{"login":"simonbasle","id":6986166,"avatar_url":"https://avatars.githubusercontent.com/u/6986166?v=3","gravatar_id":"","url":"https://api.github.com/users/simonbasle","html_url":"https://github.com/simonbasle","followers_url":"https://api.github.com/users/simonbasle/followers","following_url":"https://api.github.com/users/simonbasle/following{/other_user}","gists_url":"https://api.github.com/users/simonbasle/gists{/gist_id}","starred_url":"https://api.github.com/users/simonbasle/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/simonbasle/subscriptions","organizations_url":"https://api.github.com/users/simonbasle/orgs","repos_url":"https://api.github.com/users/simonbasle/repos","events_url":"https://api.github.com/users/simonbasle/events{/privacy}","received_events_url":"https://api.github.com/users/simonbasle/received_events","type":"User","site_admin":false},"body":"@daschl this is ready for your review :)","created_at":"2016-09-05T14:25:47Z","updated_at":"2016-09-05T14:35:53Z","closed_at":null,"merged_at":null,"merge_commit_sha":"f59fb318441618ce3d77a25c70983ccf007928f4","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/pulls/7/commits","review_comments_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/pulls/7/comments","review_comment_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/pulls/comments{/number}","comments_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/issues/7/comments","statuses_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/statuses/ae9c1e284756c4300c96f8509af6a8a2a103953e","head":{"label":"couchbaselabs:set/arrayInitial","ref":"set/arrayInitial","sha":"ae9c1e284756c4300c96f8509af6a8a2a103953e","user":{"login":"couchbaselabs","id":636956,"avatar_url":"https://avatars.githubusercontent.com/u/636956?v=3","gravatar_id":"","url":"https://api.github.com/users/couchbaselabs","html_url":"https://github.com/couchbaselabs","followers_url":"https://api.github.com/users/couchbaselabs/followers","following_url":"https://api.github.com/users/couchbaselabs/following{/other_user}","gists_url":"https://api.github.com/users/couchbaselabs/gists{/gist_id}","starred_url":"https://api.github.com/users/couchbaselabs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/couchbaselabs/subscriptions","organizations_url":"https://api.github.com/users/couchbaselabs/orgs","repos_url":"https://api.github.com/users/couchbaselabs/repos","events_url":"https://api.github.com/users/couchbaselabs/events{/privacy}","received_events_url":"https://api.github.com/users/couchbaselabs/received_events","type":"Organization","site_admin":false},"repo":{"id":62245451,"name":"cbd-commons-java","full_name":"couchbaselabs/cbd-commons-java","owner":{"login":"couchbaselabs","id":636956,"avatar_url":"https://avatars.githubusercontent.com/u/636956?v=3","gravatar_id":"","url":"https://api.github.com/users/couchbaselabs","html_url":"https://github.com/couchbaselabs","followers_url":"https://api.github.com/users/couchbaselabs/followers","following_url":"https://api.github.com/users/couchbaselabs/following{/other_user}","gists_url":"https://api.github.com/users/couchbaselabs/gists{/gist_id}","starred_url":"https://api.github.com/users/couchbaselabs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/couchbaselabs/subscriptions","organizations_url":"https://api.github.com/users/couchbaselabs/orgs","repos_url":"https://api.github.com/users/couchbaselabs/repos","events_url":"https://api.github.com/users/couchbaselabs/events{/privacy}","received_events_url":"https://api.github.com/users/couchbaselabs/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/couchbaselabs/cbd-commons-java","description":"Couchbase Datastructures Commons - Java","fork":false,"url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java","forks_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/forks","keys_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/keys{/key_id}","collaborators_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/teams","hooks_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/hooks","issue_events_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/issues/events{/number}","events_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/events","assignees_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/assignees{/user}","branches_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/branches{/branch}","tags_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/tags","blobs_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/git/refs{/sha}","trees_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/git/trees{/sha}","statuses_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/statuses/{sha}","languages_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/languages","stargazers_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/stargazers","contributors_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/contributors","subscribers_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/subscribers","subscription_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/subscription","commits_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/commits{/sha}","git_commits_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/git/commits{/sha}","comments_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/comments{/number}","issue_comment_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/issues/comments{/number}","contents_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/contents/{+path}","compare_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/compare/{base}...{head}","merges_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/merges","archive_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/downloads","issues_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/issues{/number}","pulls_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/pulls{/number}","milestones_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/milestones{/number}","notifications_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/labels{/name}","releases_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/releases{/id}","deployments_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/deployments","created_at":"2016-06-29T17:39:38Z","updated_at":"2016-06-29T17:40:48Z","pushed_at":"2016-09-05T14:25:47Z","git_url":"git://github.com/couchbaselabs/cbd-commons-java.git","ssh_url":"git@github.com:couchbaselabs/cbd-commons-java.git","clone_url":"https://github.com/couchbaselabs/cbd-commons-java.git","svn_url":"https://github.com/couchbaselabs/cbd-commons-java","homepage":null,"size":33,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":3,"forks":0,"open_issues":3,"watchers":0,"default_branch":"master"}},"base":{"label":"couchbaselabs:master","ref":"master","sha":"06634b2f32eb968c121a51ff2105459ead450394","user":{"login":"couchbaselabs","id":636956,"avatar_url":"https://avatars.githubusercontent.com/u/636956?v=3","gravatar_id":"","url":"https://api.github.com/users/couchbaselabs","html_url":"https://github.com/couchbaselabs","followers_url":"https://api.github.com/users/couchbaselabs/followers","following_url":"https://api.github.com/users/couchbaselabs/following{/other_user}","gists_url":"https://api.github.com/users/couchbaselabs/gists{/gist_id}","starred_url":"https://api.github.com/users/couchbaselabs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/couchbaselabs/subscriptions","organizations_url":"https://api.github.com/users/couchbaselabs/orgs","repos_url":"https://api.github.com/users/couchbaselabs/repos","events_url":"https://api.github.com/users/couchbaselabs/events{/privacy}","received_events_url":"https://api.github.com/users/couchbaselabs/received_events","type":"Organization","site_admin":false},"repo":{"id":62245451,"name":"cbd-commons-java","full_name":"couchbaselabs/cbd-commons-java","owner":{"login":"couchbaselabs","id":636956,"avatar_url":"https://avatars.githubusercontent.com/u/636956?v=3","gravatar_id":"","url":"https://api.github.com/users/couchbaselabs","html_url":"https://github.com/couchbaselabs","followers_url":"https://api.github.com/users/couchbaselabs/followers","following_url":"https://api.github.com/users/couchbaselabs/following{/other_user}","gists_url":"https://api.github.com/users/couchbaselabs/gists{/gist_id}","starred_url":"https://api.github.com/users/couchbaselabs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/couchbaselabs/subscriptions","organizations_url":"https://api.github.com/users/couchbaselabs/orgs","repos_url":"https://api.github.com/users/couchbaselabs/repos","events_url":"https://api.github.com/users/couchbaselabs/events{/privacy}","received_events_url":"https://api.github.com/users/couchbaselabs/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/couchbaselabs/cbd-commons-java","description":"Couchbase Datastructures Commons - Java","fork":false,"url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java","forks_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/forks","keys_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/keys{/key_id}","collaborators_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/teams","hooks_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/hooks","issue_events_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/issues/events{/number}","events_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/events","assignees_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/assignees{/user}","branches_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/branches{/branch}","tags_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/tags","blobs_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/git/refs{/sha}","trees_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/git/trees{/sha}","statuses_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/statuses/{sha}","languages_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/languages","stargazers_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/stargazers","contributors_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/contributors","subscribers_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/subscribers","subscription_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/subscription","commits_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/commits{/sha}","git_commits_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/git/commits{/sha}","comments_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/comments{/number}","issue_comment_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/issues/comments{/number}","contents_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/contents/{+path}","compare_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/compare/{base}...{head}","merges_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/merges","archive_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/downloads","issues_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/issues{/number}","pulls_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/pulls{/number}","milestones_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/milestones{/number}","notifications_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/labels{/name}","releases_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/releases{/id}","deployments_url":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/deployments","created_at":"2016-06-29T17:39:38Z","updated_at":"2016-06-29T17:40:48Z","pushed_at":"2016-09-05T14:25:47Z","git_url":"git://github.com/couchbaselabs/cbd-commons-java.git","ssh_url":"git@github.com:couchbaselabs/cbd-commons-java.git","clone_url":"https://github.com/couchbaselabs/cbd-commons-java.git","svn_url":"https://github.com/couchbaselabs/cbd-commons-java","homepage":null,"size":33,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":3,"forks":0,"open_issues":3,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/pulls/7"},"html":{"href":"https://github.com/couchbaselabs/cbd-commons-java/pull/7"},"issue":{"href":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/issues/7"},"comments":{"href":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/issues/7/comments"},"review_comments":{"href":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/pulls/7/comments"},"review_comment":{"href":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/pulls/7/commits"},"statuses":{"href":"https://api.github.com/repos/couchbaselabs/cbd-commons-java/statuses/ae9c1e284756c4300c96f8509af6a8a2a103953e"}}}}
{"actor":{"display_login":"daschl"}}