项目作者: github

项目描述 :
A collection of Ruby libraries for working with SQL on top of ActiveRecord's connection
高级语言: Ruby
项目地址: git://github.com/github/github-ds.git
创建时间: 2016-12-08T00:19:19Z
项目社区:https://github.com/github/github-ds

开源协议:MIT License

下载


GitHub::DS

GitHub::DS is a collection of Ruby libraries for working with SQL on top of ActiveRecord’s connection.

  • GitHub::KV is a key/value data store backed by MySQL.
  • GitHub::SQL is for building and executing a SQL query. This class uses ActiveRecord’s connection class, but provides a better API for bind values and raw data access.
  • GitHub::Result makes it easier to bake in resiliency through the use of a Result object instead of raising exceptions.

Current Status: Used in production extensively at GitHub. Because of this, all changes will be thoroughly vetted, which could slow down the process of contributing. We will do our best to actively communicate status of pull requests with any contributors. If you have any substantial changes that you would like to make, it would be great to first open an issue to discuss them with us.

Installation

Add this line to your application’s Gemfile:

  1. gem 'github-ds'

And then execute:

  1. $ bundle

Or install it yourself as:

  1. $ gem install github-ds

Usage

Below is a taste of what you can do with these libraries. If you want to see more, check out the examples directory.

GitHub::KV

First, you’ll need to create the key_values table using the included Rails migration generator.

  1. rails generate github:ds:active_record
  2. rails db:migrate

If you need to change the name of the table used for storing the key-values, you can configure your table name as such, before running the migration:

  1. GitHub::KV.configure do |config|
  2. config.table_name = "new_key_values_table"
  3. end

Once you have created and executed the migration, KV can do neat things like this:

  1. require "pp"
  2. # Create new instance using ActiveRecord's default connection.
  3. kv = GitHub::KV.new { ActiveRecord::Base.connection }
  4. # Get a key.
  5. pp kv.get("foo")
  6. #<GitHub::Result:0x3fd88cd3ea9c value: nil>
  7. # Set a key.
  8. kv.set("foo", "bar")
  9. # nil
  10. # Get the key again.
  11. pp kv.get("foo")
  12. #<GitHub::Result:0x3fe810d06e4c value: "bar">
  13. # Get multiple keys at once.
  14. pp kv.mget(["foo", "bar"])
  15. #<GitHub::Result:0x3fccccd1b57c value: ["bar", nil]>
  16. # Check for existence of a key.
  17. pp kv.exists("foo")
  18. #<GitHub::Result:0x3fd4ae55ce8c value: true>
  19. # Check for existence of key that does not exist.
  20. pp kv.exists("bar")
  21. #<GitHub::Result:0x3fd4ae55c554 value: false>
  22. # Check for existence of multiple keys at once.
  23. pp kv.mexists(["foo", "bar"])
  24. #<GitHub::Result:0x3ff1e98e18e8 value: [true, false]>
  25. # Set a key's value if the key does not already exist.
  26. pp kv.setnx("foo", "bar")
  27. # false
  28. # Delete a key.
  29. pp kv.del("bar")
  30. # nil
  31. # Delete multiple keys at once.
  32. pp kv.mdel(["foo", "bar"])
  33. # nil

Note that due to MySQL’s default collation, KV keys are case-insensitive.

GitHub::SQL

  1. # Select, insert, update, delete or whatever you need...
  2. GitHub::SQL.results <<-SQL
  3. SELECT * FROM example_key_values
  4. SQL
  5. GitHub::SQL.run <<-SQL, key: "foo", value: "bar"
  6. INSERT INTO example_key_values (`key`, `value`)
  7. VALUES (:key, :value)
  8. SQL
  9. GitHub::SQL.value <<-SQL, key: "foo"
  10. SELECT value FROM example_key_values WHERE `key` = :key
  11. SQL
  12. # Or slowly build up a query based on conditionals...
  13. sql = GitHub::SQL.new <<-SQL
  14. SELECT `value` FROM example_key_values
  15. SQL
  16. key = ENV["KEY"]
  17. unless key.nil?
  18. sql.add <<-SQL, key: key
  19. WHERE `key` = :key
  20. SQL
  21. end
  22. limit = ENV["LIMIT"]
  23. unless limit.nil?
  24. sql.add <<-SQL, limit: limit.to_i
  25. ORDER BY `key` ASC
  26. LIMIT :limit
  27. SQL
  28. end
  29. p sql.results

GitHub::Result

  1. def do_something
  2. 1
  3. end
  4. def do_something_that_errors
  5. raise "noooooppppeeeee"
  6. end
  7. result = GitHub::Result.new { do_something }
  8. p result.ok? # => true
  9. p result.value! # => 1
  10. result = GitHub::Result.new { do_something_that_errors }
  11. p result.ok? # => false
  12. p result.value { "default when error happens" } # => "default when error happens"
  13. begin
  14. result.value! # raises exception because error happened
  15. rescue => error
  16. p result.error
  17. p error
  18. end
  19. # Outputs Step 1, 2, 3
  20. result = GitHub::Result.new {
  21. GitHub::Result.new { puts "Step 1: success!" }
  22. }.then { |value|
  23. GitHub::Result.new { puts "Step 2: success!" }
  24. }.then { |value|
  25. GitHub::Result.new { puts "Step 3: success!" }
  26. }
  27. p result.ok? # => true
  28. # Outputs Step 1, 2 and stops.
  29. result = GitHub::Result.new {
  30. GitHub::Result.new { puts "Step 1: success!" }
  31. }.then { |value|
  32. GitHub::Result.new {
  33. puts "Step 2: failed!"
  34. raise
  35. }
  36. }.then { |value|
  37. GitHub::Result.new {
  38. puts "Step 3: should not get here because previous step failed!"
  39. }
  40. }
  41. p result.ok? # => false

Caveats

GitHub::KV Expiration

KV supports expiring keys and obeys expiration when performing operations, but does not actually purge expired rows. At GitHub, we use pt-archiver to nibble expired rows. We configure it to do a replica lag check and use the following options:

  • index_name: "index_key_values_on_expires_at"
  • limit: 1000
  • where: "expires_at <= NOW()"

Development

After checking out the repo, run script/bootstrap to install dependencies. Then, run script/test to run the tests. You can also run script/console for an interactive prompt that will allow you to experiment.

Note: You will need a MySQL database with no password set for the root user for the tests. Running docker-compose up will boot just that. This functionality is not currently used by GitHub and was from a contributor, so please let us know if it does not work or gets out of date (pull request is best, but an issue will do).

To install this gem onto your local machine, run script/install. To release a new version, update the version number in version.rb, commit, and then run script/release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/github/github-ds. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct. We recommend reading the contributing guide as well.

Roadmap

Nothing currently on our radar other than continued maintenance. Have a big idea? Let us know.

Maintainers

pic @mention
<a href=@haileysome"> @haileysome
<a href=@jnunemaker"> @jnunemaker
<a href=@miguelff"> @miguelff
<a href=@zerowidth"> @zerowidth

License

The gem is available as open source under the terms of the MIT License.