Ruby mogilefs-client dev/users discussion/patches/bugs/help/...
 help / color / Atom feed
* [PATCH] freeze string literals in a few more places
@ 2017-01-31  1:58 Eric Wong
  0 siblings, 0 replies; only message in thread
From: Eric Wong @ 2017-01-31  1:58 UTC (permalink / raw)
  To: mogilefs-client-public

This should reduce the amount of garbage we produce and
keep the process leaner and meaner.
---
 lib/mogilefs/admin.rb         | 1 +
 lib/mogilefs/http_file.rb     | 8 ++++----
 lib/mogilefs/mysql.rb         | 6 +++---
 lib/mogilefs/paths_size.rb    | 2 +-
 lib/mogilefs/socket_common.rb | 5 ++---
 5 files changed, 11 insertions(+), 11 deletions(-)

diff --git a/lib/mogilefs/admin.rb b/lib/mogilefs/admin.rb
index 96a7898..d330439 100644
--- a/lib/mogilefs/admin.rb
+++ b/lib/mogilefs/admin.rb
@@ -1,4 +1,5 @@
 # -*- encoding: binary -*-
+# frozen_string_literal: true
 
 # \MogileFS administration client, this has little real-world usage
 # and is considered a work-in-progress
diff --git a/lib/mogilefs/http_file.rb b/lib/mogilefs/http_file.rb
index c7949a5..a58801a 100644
--- a/lib/mogilefs/http_file.rb
+++ b/lib/mogilefs/http_file.rb
@@ -54,7 +54,7 @@ def request_put(sock, uri, file_size, input = nil)
                  "Content-Length: #{file_size}\r\n\r\n")
       rv = input ? MogileFS.io.copy_stream(@active = input, sock) : yield(sock)
     else
-      trailers = @md5 ? "Trailer: Content-MD5\r\n" : ""
+      trailers = @md5 ? "Trailer: Content-MD5\r\n".freeze : "".freeze
       sock.write("PUT #{uri.request_uri} HTTP/1.1\r\n" \
                  "Host: #{host_with_port}\r\n#{trailers}" \
                  "Transfer-Encoding: chunked\r\n\r\n")
@@ -143,14 +143,14 @@ def nhp_put(devid, uri)
     end
 
     put = Net::HTTP::Put.new(uri.path)
-    put["Content-Type"] = "application/octet-stream".freeze
+    put["Content-Type".freeze] = "application/octet-stream".freeze
     if md5 = @opts[:content_md5]
       if md5.respond_to?(:call)
         md5 = md5.call.strip
       elsif md5 == :trailer
-        md5 = [ Digest::MD5.digest(string) ].pack("m").chomp!
+        md5 = [ Digest::MD5.digest(string) ].pack("m".freeze).chomp!
       end
-      put["Content-MD5"] = md5
+      put["Content-MD5".freeze] = md5
     end
     put.body = string
     res = @opts[:nhp_put].request(uri, put)
diff --git a/lib/mogilefs/mysql.rb b/lib/mogilefs/mysql.rb
index e2a16e5..81b2a61 100644
--- a/lib/mogilefs/mysql.rb
+++ b/lib/mogilefs/mysql.rb
@@ -28,7 +28,7 @@ def initialize(args = {})
   ##
   # Lists keys starting with +prefix+ follwing +after+ up to +limit+.  If
   # +after+ is nil the list starts at the beginning.
-  def _list_keys(domain, prefix = '', after = '', limit = 1000)
+  def _list_keys(domain, prefix = ''.freeze, after = ''.freeze, limit = 1000)
     # this code is based on server/lib/MogileFS/Worker/Query.pm
     dmid = get_dmid(domain)
 
@@ -43,7 +43,7 @@ def _list_keys(domain, prefix = '', after = '', limit = 1000)
     end
 
     raise MogileFS::Backend::InvalidCharsError if /[%\\]/ =~ prefix
-    prefix.gsub!(/_/, '\_') # not sure why MogileFS::Worker::Query does this...
+    prefix.gsub!(/_/, '\_'.freeze)
 
     sql = <<-EOS
     SELECT dkey,length,devcount FROM file
@@ -150,7 +150,7 @@ def refresh_device(force = false)
         tmp[devid.to_i] = {
           :hostip => hostip.freeze,
           :altip => (altip || hostip).freeze,
-          :readable => (host_status == "alive" &&
+          :readable => (host_status == "alive".freeze &&
                         DEV_STATUS_READABLE.include?(dev_status)),
           :http_port => http_port,
           :http_get_port => http_get_port ?  http_get_port.to_i : http_port,
diff --git a/lib/mogilefs/paths_size.rb b/lib/mogilefs/paths_size.rb
index ba61f9e..0aa676d 100644
--- a/lib/mogilefs/paths_size.rb
+++ b/lib/mogilefs/paths_size.rb
@@ -10,7 +10,7 @@ def self.call(paths)
       begin
         case r = Net::HTTP.start(uri.host, uri.port) { |x| x.head(uri.path) }
         when Net::HTTPOK
-          return r["Content-Length"].to_i
+          return r['Content-Length'.freeze].to_i
         else
           errors[path] = r
         end
diff --git a/lib/mogilefs/socket_common.rb b/lib/mogilefs/socket_common.rb
index d430068..886c738 100644
--- a/lib/mogilefs/socket_common.rb
+++ b/lib/mogilefs/socket_common.rb
@@ -23,18 +23,17 @@ def request_truncated!(written, expect, timeout)
      "request truncated (sent #{written} expected #{expect} timeout=#{timeout})"
   end
 
-  SEP_RE = /\A(.*?#{Regexp.escape("\n")})/
   def timed_gets(timeout = 5)
     unless defined?(@rbuf) && @rbuf
       @rbuf = timed_read(1024, "", timeout) or return # EOF
     end
     begin
-      @rbuf.sub!(SEP_RE, "") and return $1
+      @rbuf.sub!(/\A(.*\n)/o, "".freeze) and return $1
       tmp ||= ""
       if timed_read(1024, tmp, timeout)
         @rbuf << tmp
       else
-        # EOF, return the last buffered bit even without SEP_RE matching
+        # EOF, return the last buffered bit even without separatar matching
         # (not ideal for MogileFS, this is an error)
         return @rbuf.empty? ? nil : @rbuf.slice!(0, @rbuf.size)
       end
-- 
EW


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, back to index

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-01-31  1:58 [PATCH] freeze string literals in a few more places Eric Wong

Ruby mogilefs-client dev/users discussion/patches/bugs/help/...

Archives are clonable:
	git clone --mirror https://yhbt.net/mogilefs-client-public
	git clone --mirror http://ou63pmih66umazou.onion/mogilefs-client-public

Example config snippet for mirrors

Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.file-systems.mogilefs.ruby
	nntp://ou63pmih66umazou.onion/inbox.comp.file-systems.mogilefs.ruby

 note: .onion URLs require Tor: https://www.torproject.org/

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git