Commits

Anonymous committed d60f991

expose selection of riak client id in Jiak Ruby wrapper

Comments (0)

Files changed (1)

client_lib/jiak.rb

     @port = port
     @prefix = jiakPrefix
     @opts = options
+
+    if (@opts['clientId'])
+      if (@opts['clientId'].kind_of? Integer &&
+          @opts['clientId'] > 0 &&
+          @opts['clientId'] < 4294967296)
+        @opts['clientId'] = base64(@opts['clientId'])
+      end
+    else
+      @opts['clientId'] = base64(rand(4294967296))
+    end
   end
 
   # Set the schema for 'bucket'.  The schema parameter
       'r'=>(r||@opts['r'])
     }
     if (object['key'])
-      req = Net::HTTP::Put.new(path(object['bucket'], object['key'], q))
+      req = Net::HTTP::Put.new(path(object['bucket'], object['key'], q),
+                               initheader={"X-Riak-ClientId" => @opts['clientId']})
       code = '200'
     else
-      req = Net::HTTP::Post.new(path(object['bucket'], nil, q))
+      req = Net::HTTP::Post.new(path(object['bucket'], nil, q),
+                                initheader={"X-Riak-ClientId" => @opts['clientId']})
       code = '201'
     end
 
   # Delete the data stored in 'bucket' at 'key'
   def delete(bucket, key, rw=nil)
     do_req(Net::HTTP::Delete.new(path(bucket, key,
-                                      {'rw'=>(rw||@opts['rw'])})),
+                                      {'rw'=>(rw||@opts['rw'])}),
+                                 initheader={"X-Riak-ClientId" => @opts['clientId']}),
            '204')
   end
 
       raise JiakException.new(res.code+' '+res.message+' '+res.body)
     end
   end
+
+  def base64(n)
+    base64digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
+    "%c%c%c%c%c%c==" %
+      [base64digits[(n >> 26)],
+       base64digits[((n >> 20)&63)],
+       base64digits[((n >> 14)&63)],
+       base64digits[((n >> 8)&63)],
+       base64digits[((n >> 2)&63)],
+       base64digits[((n << 4)&63)]]
+  end
   private:convert_walk_spec
   private:path
   private:set_data
   private:do_req
+  private:base64
 end
 
 class JiakException<Exception
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.