-
-
Notifications
You must be signed in to change notification settings - Fork 130
Add compact index support for private sources #392
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
Open
segiddins
wants to merge
18
commits into
main
Choose a base branch
from
segiddins/add-compact-index-support-for-private-sources
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
3c22437
Add missing jruby binstub
segiddins f5b5800
Ensure new enough sqlite3 is used
segiddins cedac9c
Reapply "Add compact index support for private sources"
segiddins 5437e32
Use inner join to simplify query
segiddins 67a908e
Use Sequel builder methods for complex compact index queries
segiddins d7fbc7c
[DNM] Debug sql errors
segiddins 0c34760
Enable string_agg extension
segiddins 801b4d2
[DNM] Enable error_sql extension
segiddins d36a7d2
x-plat string_agg
segiddins 1796ac5
use concat function for mysql compatibility
segiddins 48bce30
Attempt to fix integration specs on postgres on ci
segiddins d37f758
Fail early if running old sqlite
segiddins 203da4b
jruby fixes
segiddins 07925fa
Fix failing spec on ruby 3.3
segiddins a46268c
Bump gem_server_conformance requirement
segiddins 42a33e3
Debug jruby gem server conformance failures
segiddins d0cedb5
Try requiring bundler/setup on jruby binstub
segiddins 2853517
Clean up compact index builder and remove schema.rb
segiddins File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,5 +29,6 @@ group :linting do | |
end | ||
|
||
group :test do | ||
gem "gem_server_conformance", "~> 0.1.5" | ||
gem "mock_redis" | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,238 @@ | ||
# frozen_string_literal: true | ||
|
||
require "active_support/core_ext/string/filters" | ||
require "compact_index" | ||
require "gemstash" | ||
require "stringio" | ||
require "zlib" | ||
|
||
module Gemstash | ||
class CompactIndexBuilder | ||
include Gemstash::Env::Helper | ||
attr_reader :result | ||
|
||
def self.serve(app, ...) | ||
app.content_type "text/plain; charset=utf-8" | ||
body = new(app.auth, ...).serve | ||
app.etag Digest::MD5.hexdigest(body) | ||
sha256 = Digest::SHA256.base64digest(body) | ||
app.headers "Accept-Ranges" => "bytes", "Digest" => "sha-256=#{sha256}", "Repr-Digest" => "sha-256=:#{sha256}:", | ||
"Content-Length" => body.bytesize.to_s | ||
body | ||
end | ||
|
||
def self.invalidate_stored(name) | ||
storage = Gemstash::Storage.for("private").for("compact_index") | ||
storage.resource("names").delete(:names) | ||
storage.resource("versions").delete(:versions) | ||
storage.resource("info/#{name}").delete(:info) | ||
end | ||
|
||
def initialize(auth) | ||
@auth = auth | ||
end | ||
|
||
def serve | ||
check_auth if gemstash_env.config[:protected_fetch] | ||
fetch_from_storage | ||
return result if result | ||
|
||
build_result | ||
store_result | ||
result | ||
end | ||
|
||
private | ||
|
||
def storage | ||
@storage ||= Gemstash::Storage.for("private").for("compact_index") | ||
end | ||
|
||
def fetch_from_storage | ||
resource = fetch_resource | ||
return unless resource.exist?(key) | ||
|
||
@result = resource.load(key).content(key) | ||
rescue StandardError | ||
# On the off-chance of a race condition between specs.exist? and specs.load | ||
@result = nil | ||
end | ||
|
||
def store_result | ||
fetch_resource.save(key => @result) | ||
end | ||
|
||
def check_auth | ||
@auth.check("fetch") | ||
end | ||
|
||
class Versions < CompactIndexBuilder | ||
def fetch_resource | ||
storage.resource("versions") | ||
end | ||
|
||
def build_result(force_rebuild: false) | ||
resource = fetch_resource | ||
base = !force_rebuild && resource.exist?("versions.list") && resource.content("versions.list") | ||
Tempfile.create("versions.list") do |file| | ||
versions_file = CompactIndex::VersionsFile.new(file.path) | ||
if base | ||
file.write(base) | ||
file.close | ||
@result = versions_file.contents( | ||
compact_index_versions(versions_file.updated_at.to_time) | ||
) | ||
else | ||
ts = Time.now.iso8601 | ||
versions_file.create( | ||
compact_index_public_versions(ts), ts.to_s.sub(/\+00:00\z/, "Z") | ||
) | ||
@result = file.read | ||
resource.save("versions.list" => @result) | ||
Comment on lines
+90
to
+91
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ...You'd just need to store to a temp var here. |
||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def compact_index_versions(date) | ||
all_versions = Sequel::Model.db[<<~SQL.squish, date, date].to_a | ||
SELECT r.name as name, v.created_at as date, v.info_checksum as info_checksum, v.number as number, v.platform as platform | ||
FROM rubygems AS r, versions AS v | ||
WHERE v.rubygem_id = r.id AND | ||
v.created_at > ? | ||
|
||
UNION ALL | ||
|
||
SELECT r.name as name, v.yanked_at as date, v.yanked_info_checksum as info_checksum, concat('-', v.number) as number, v.platform as platform | ||
FROM rubygems AS r, versions AS v | ||
WHERE v.rubygem_id = r.id AND | ||
v.indexed is false AND | ||
v.yanked_at > ? | ||
|
||
ORDER BY date, number, platform, name | ||
SQL | ||
|
||
map_gem_versions(all_versions.map {|v| [v[:name], [v]] }) | ||
end | ||
|
||
def compact_index_public_versions(date) | ||
all_versions = Sequel::Model.db[<<~SQL.squish, date, date].to_a | ||
SELECT r.name, v.indexed, COALESCE(v.yanked_at, v.created_at) as stamp, | ||
COALESCE(v.yanked_info_checksum, v.info_checksum) as info_checksum, v.number, v.platform | ||
FROM rubygems AS r, versions AS v | ||
WHERE v.rubygem_id = r.id AND | ||
(v.created_at <= ? OR v.yanked_at <= ?) | ||
ORDER BY name, COALESCE(v.yanked_at, v.created_at), number, platform | ||
SQL | ||
|
||
versions_by_gem = all_versions.group_by {|row| row[:name] } | ||
versions_by_gem.each_value do |versions| | ||
info_checksum = versions.last[:info_checksum] | ||
versions.select! {|v| v[:indexed] == true } | ||
# Set all versions' info_checksum to work around https://github.com/bundler/compact_index/pull/20 | ||
versions.each {|v| v[:info_checksum] = info_checksum } | ||
end | ||
|
||
map_gem_versions(versions_by_gem) | ||
end | ||
|
||
def map_gem_versions(versions_by_gem) | ||
versions_by_gem.map do |name, versions| | ||
CompactIndex::Gem.new( | ||
name, | ||
versions.map do |row| | ||
CompactIndex::GemVersion.new( | ||
row[:number], | ||
row[:platform], | ||
nil, # sha256 | ||
row[:info_checksum], | ||
nil, # dependencies | ||
nil, # version.required_ruby_version, | ||
nil, # version.required_rubygems_version | ||
) | ||
end | ||
) | ||
end | ||
end | ||
|
||
def key | ||
:versions | ||
end | ||
end | ||
|
||
class Info < CompactIndexBuilder | ||
def initialize(auth, name) | ||
super(auth) | ||
@name = name | ||
end | ||
|
||
def fetch_resource | ||
storage.resource("info/#{@name}") | ||
end | ||
|
||
def build_result | ||
@result = CompactIndex.info(requirements_and_dependencies) | ||
end | ||
|
||
private | ||
|
||
def requirements_and_dependencies | ||
DB::Rubygem.association_left_join(versions: :dependencies). | ||
where(name: @name). | ||
where { versions[:indexed] }. | ||
order { [versions[:created_at], versions[:number], versions[:platform], dep_name_agg] }. | ||
select_group do | ||
[versions[:number], versions[:platform], versions[:sha256], versions[:info_checksum], versions[:required_ruby_version], versions[:required_rubygems_version], versions[:created_at]] | ||
end. # rubocop:disable Style/MultilineBlockChain | ||
select_more do | ||
[coalesce(Sequel.string_agg(dependencies[:requirements], "@").order(dependencies[:rubygem_name], dependencies[:id]), "").as(:dep_req_agg), | ||
coalesce(Sequel.string_agg(dependencies[:rubygem_name], ",").order(dependencies[:rubygem_name]), "").as(:dep_name_agg)] | ||
end. # rubocop:disable Style/MultilineBlockChain | ||
map do |row| | ||
reqs = row[:dep_req_agg].split("@") | ||
dep_names = row[:dep_name_agg].split(",") | ||
|
||
raise "Dependencies and requirements are not the same size:\n reqs: #{reqs.inspect}\n dep_names: #{dep_names.inspect}\n row: #{row.inspect}" if dep_names.size != reqs.size | ||
|
||
deps = dep_names.zip(reqs).map! do |name, req| | ||
CompactIndex::Dependency.new(name, req) | ||
end | ||
|
||
CompactIndex::GemVersion.new( | ||
row[:number], | ||
row[:platform], | ||
row[:sha256], | ||
nil, # info_checksum | ||
deps, | ||
row[:required_ruby_version], | ||
row[:required_rubygems_version] | ||
) | ||
end | ||
end | ||
|
||
def key | ||
:info | ||
end | ||
end | ||
|
||
class Names < CompactIndexBuilder | ||
def fetch_resource | ||
storage.resource("names") | ||
end | ||
|
||
def build_result | ||
names = DB::Rubygem.association_join(:versions). | ||
where { versions[:indexed] }. | ||
order(:name).group(:name).select_map(:name) | ||
@result = CompactIndex.names(names).encode("UTF-8") | ||
end | ||
|
||
private | ||
|
||
def key | ||
:names | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: With a few tweaks and this change you could remove all mentions of
@result
and theattr_reader :result
.It also avoids the need to do
@result = nil
in the rescue for race condition.