Anonymous avatar Anonymous committed ada28a6

expose selection of riak client id in Jiak Javascript wrapper

select a random id and keep it, if none is provided

Comments (0)

Files changed (2)

client_lib/jiak.js

         this.baseurl += '/';
 
     this.opts = Opts||{};
+
+    // utility to convert an integer to base64-encoded 32-bits
+    base64 = function(N) {
+        var base64digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
+        return base64digits[(N >>> 26)]
+            +base64digits[((N >>> 20)&63)]
+            +base64digits[((N >>> 14)&63)]
+            +base64digits[((N >>> 8)&63)]
+            +base64digits[((N >>> 2)&63)]
+            +base64digits[((N << 4)&63)]
+            +'==';
+    }
+
+    if (('clientId' in this.opts) && !!this.opts.clientId) {
+        if (typeof this.opts.clientId == "number"
+            && this.opts.clientId > 0 && this.opts.clientId < 4294967296) {
+            this.opts.clientId = base64(this.opts.clientId);
+        }
+        //otherwise, just use whatever clientId was given
+    } else {
+        //choose a client id if the caller didn't provide one
+        this.opts.clientId = base64(Math.floor(Math.random()*4294967296));
+    }
 }
 
 JiakClient.prototype.store = function(Object, Callback, NoReturnBody, W, DW, R) {
+    var cid = this.opts.clientId;
     var req = {
         contentType: "application/json",
-        dataType: "json"
+        dataType: "json",
+        beforeSend: function(xhr) {
+            xhr.setRequestHeader("X-Riak-ClientId", cid);
+        }
     };
 
     if (this.opts.alwaysPost || !Object.key)
 }
 
 JiakClient.prototype.remove = function(Bucket, Key, Callback, RW) {
+    var cid = this.opts.clientId;
     return $.ajax({
         type:    'DELETE',
         url:     this.path(Bucket, Key)+
                    ((RW||this.opts.rw)?('?rw='+(RW||this.opts.rw)):''),
-        success: Callback
+        success: Callback,
+        beforeSend: function(xhr) {
+            xhr.setRequestHeader("X-Riak-ClientId", cid);
+        }
     });
 }
 

demo/stickynotes/priv/www/js/jiak.js

         this.baseurl += '/';
 
     this.opts = Opts||{};
+
+    // utility to convert an integer to base64-encoded 32-bits
+    base64 = function(N) {
+        var base64digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
+        return base64digits[(N >>> 26)]
+            +base64digits[((N >>> 20)&63)]
+            +base64digits[((N >>> 14)&63)]
+            +base64digits[((N >>> 8)&63)]
+            +base64digits[((N >>> 2)&63)]
+            +base64digits[((N << 4)&63)]
+            +'==';
+    }
+
+    if (('clientId' in this.opts) && !!this.opts.clientId) {
+        if (typeof this.opts.clientId == "number"
+            && this.opts.clientId > 0 && this.opts.clientId < 4294967296) {
+            this.opts.clientId = base64(this.opts.clientId);
+        }
+        //otherwise, just use whatever clientId was given
+    } else {
+        //choose a client id if the caller didn't provide one
+        this.opts.clientId = base64(Math.floor(Math.random()*4294967296));
+    }
 }
 
 JiakClient.prototype.store = function(Object, Callback, NoReturnBody, W, DW, R) {
+    var cid = this.opts.clientId;
     var req = {
         contentType: "application/json",
-        dataType: "json"
+        dataType: "json",
+        beforeSend: function(xhr) {
+            xhr.setRequestHeader("X-Riak-ClientId", cid);
+        }
     };
 
     if (this.opts.alwaysPost || !Object.key)
 }
 
 JiakClient.prototype.remove = function(Bucket, Key, Callback, RW) {
+    var cid = this.opts.clientId;
     return $.ajax({
         type:    'DELETE',
         url:     this.path(Bucket, Key)+
                    ((RW||this.opts.rw)?('?rw='+(RW||this.opts.rw)):''),
-        success: Callback
+        success: Callback,
+        beforeSend: function(xhr) {
+            xhr.setRequestHeader("X-Riak-ClientId", cid);
+        }
     });
 }
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.