Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid filesystem limits using a trie #26

Merged
merged 3 commits into from
Oct 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions lib/gemstash/storage.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "gemstash"
require "digest"
require "pathname"
require "fileutils"
require "yaml"
Expand Down Expand Up @@ -36,11 +37,20 @@ def path_valid?(path)

#:nodoc:
class Resource
attr_accessor :name
attr_accessor :name, :folder
def initialize(folder, name)
@base_path = folder
@name = name
@folder = File.join(@base_path, @name)
# Avoid odd characters in paths, in case of issues with the file system
safe_name = @name.gsub(/[^a-zA-Z0-9_]/, "_")
# Use a trie structure to avoid file system limits causing too many files in 1 folder
# Downcase to avoid issues with case insensitive file systems
trie_parents = safe_name[0...3].downcase.split("")
# The digest is included in case the name differs only by case
# Some file systems are case insensitive, so such collisions will be a problem
digest = Digest::MD5.hexdigest(@name)
child_folder = "#{safe_name}-#{digest}"
@folder = File.join(@base_path, *trie_parents, child_folder)
end

def exist?
Expand Down
31 changes: 31 additions & 0 deletions spec/gemstash/storage_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,36 @@
expect(resource.content).to eq(content)
end
end

context "with resource name that is unique by case only" do
let(:first_resource_id) { "SomeResource" }
let(:second_resource_id) { "someresource" }

it "stores the content separately" do
storage.resource(first_resource_id).save("first content")
storage.resource(second_resource_id).save("second content")
expect(storage.resource(first_resource_id).load.content).to eq("first content")
expect(storage.resource(second_resource_id).load.content).to eq("second content")
end

it "uses different downcased paths to avoid issues with case insensitive file systems" do
first_resource = storage.resource(first_resource_id)
second_resource = storage.resource(second_resource_id)
expect(first_resource.folder.downcase).to_not eq(second_resource.folder.downcase)
end
end

context "with resource name that includes odd characters" do
let(:resource_id) { ".=$&resource" }

it "stores and retrieves the data" do
storage.resource(resource_id).save("odd name content")
expect(storage.resource(resource_id).load.content).to eq("odd name content")
end

it "doesn't include the odd characters in the path" do
expect(storage.resource(resource_id).folder).to_not match(/[.=$&]/)
end
end
end
end