pg_flash_json 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 23bf70c17789f86129e82dc3c0c8a1dff5d211c4
4
+ data.tar.gz: ea3f4682399bb792ecd0ae93105aeb91f5b3a441
5
+ SHA512:
6
+ metadata.gz: 8efe31dfe5076160270fd47144a3cb61a12fbc186ccace3900a6cd688f263cb9f7c1fa0ba19f62fa731438648d9db8d3148e9c4938cee3722b7ebd683745da42
7
+ data.tar.gz: add04407c27a4dfcea86b07ac99e682de36fa2bbdc048477f52b82ded579f239b7e610e6c95b18cc17d868a83749e732914a593d092f482dba40e28f8d843f68
@@ -0,0 +1,2 @@
1
+ .idea/
2
+ pkg/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in pg_flash_json.gemspec
4
+ gemspec
@@ -0,0 +1,60 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ pg_flash_json (0.1.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ activemodel (4.2.4)
10
+ activesupport (= 4.2.4)
11
+ builder (~> 3.1)
12
+ activerecord (4.2.4)
13
+ activemodel (= 4.2.4)
14
+ activesupport (= 4.2.4)
15
+ arel (~> 6.0)
16
+ activesupport (4.2.4)
17
+ i18n (~> 0.7)
18
+ json (~> 1.7, >= 1.7.7)
19
+ minitest (~> 5.1)
20
+ thread_safe (~> 0.3, >= 0.3.4)
21
+ tzinfo (~> 1.1)
22
+ arel (6.0.3)
23
+ benchmark-ips (2.3.0)
24
+ builder (3.2.2)
25
+ byebug (6.0.2)
26
+ diff-lcs (1.2.5)
27
+ i18n (0.7.0)
28
+ json (1.8.3)
29
+ minitest (5.8.1)
30
+ pg (0.18.3)
31
+ rake (10.4.2)
32
+ rspec (3.3.0)
33
+ rspec-core (~> 3.3.0)
34
+ rspec-expectations (~> 3.3.0)
35
+ rspec-mocks (~> 3.3.0)
36
+ rspec-core (3.3.2)
37
+ rspec-support (~> 3.3.0)
38
+ rspec-expectations (3.3.1)
39
+ diff-lcs (>= 1.2.0, < 2.0)
40
+ rspec-support (~> 3.3.0)
41
+ rspec-mocks (3.3.2)
42
+ diff-lcs (>= 1.2.0, < 2.0)
43
+ rspec-support (~> 3.3.0)
44
+ rspec-support (3.3.0)
45
+ thread_safe (0.3.5)
46
+ tzinfo (1.2.2)
47
+ thread_safe (~> 0.1)
48
+
49
+ PLATFORMS
50
+ ruby
51
+
52
+ DEPENDENCIES
53
+ activerecord (~> 4.2)
54
+ benchmark-ips
55
+ bundler
56
+ byebug
57
+ pg
58
+ pg_flash_json!
59
+ rake (~> 10.0)
60
+ rspec (~> 3.0)
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 dandlezzz
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,77 @@
1
+ # PgFlashJson
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'pg_flash_json'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install pg_flash_json
18
+
19
+ ## Usage
20
+
21
+ PGFlashJSON turns your ActiveRecord relations into json using the power of postgres' json support.
22
+ It works by using the attributes of the model to build the proper postgres json query.
23
+
24
+ ```ruby
25
+ class Post < ActiveRecord::Base
26
+ # attributes :id, :title, :content
27
+ end
28
+
29
+ Post.where(id: 1).to_pg_json
30
+ # SELECT json_agg(
31
+ # json_build_object(
32
+ # 'id', t582.id,
33
+ # 'title', t582.title,
34
+ # 'content', t582.content
35
+ # ))
36
+ # as json
37
+ # FROM(SELECT "posts".* FROM "posts" WHERE "posts"."id" = 1)t582
38
+ ```
39
+ This query is run and the aliases in the above example will be generated on the fly to ensure uniqueness.
40
+
41
+ To use simply call #to_pg_json on any active record association.
42
+
43
+ ## Benchmarks
44
+
45
+ Since the json is generated by postgres, we can skip object instantiation, accordingly PgFlashJson is faster when used to serialize a large number of records.
46
+ Consider the following benchmarks. These ips benchmarks call the separate json methods on 100 Post models, each one has an id, title, and content. The json they produce is identical.
47
+
48
+ ```
49
+ PgFlashJson#to_pg_json
50
+ 179.000 i/100ms
51
+ ActiveRecord#to_json 20.000 i/100ms
52
+ -------------------------------------------------
53
+ PgFlashJson#to_pg_json
54
+ 1.819k (± 3.9%) i/s - 9.129k
55
+ ActiveRecord#to_json 208.296 (± 4.3%) i/s - 1.040k
56
+
57
+ Comparison:
58
+ PgFlashJson#to_pg_json: 1819.5 i/s
59
+ ActiveRecord#to_json: 208.3 i/s - 8.73x slower
60
+
61
+ ```
62
+
63
+ ## Development
64
+
65
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. Additionally,
66
+ the bin/benchmarks task will allow you to run the benchmarks locally.
67
+
68
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
69
+
70
+ ## Contributing
71
+
72
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/pg_flash_json.
73
+
74
+
75
+ ## License
76
+
77
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,56 @@
1
+ #!/usr/bin/env ruby
2
+ require 'benchmark/ips'
3
+ require "bundler/setup"
4
+ require "pg_flash_json"
5
+ require 'active_record'
6
+
7
+ ActiveRecord::Base.establish_connection adapter: :postgresql, database: 'pg_flash_json_test', username: ENV['PG_DB_USERNAME'], password: ENV['PG_DB_PASSWORD']
8
+
9
+ class Post < ActiveRecord::Base
10
+ has_many :comments
11
+ end
12
+
13
+ class Comment < ActiveRecord::Base
14
+ belongs_to :post
15
+ end
16
+
17
+ def seed_data
18
+ create_post_table unless ActiveRecord::Migration.table_exists?(:posts)
19
+ create_comment_table unless ActiveRecord::Migration.table_exists?(:comments)
20
+ 200.times do |i|
21
+ post = Post.create!(title: "Post ##{i}", content: "some post content #{(i+2).to_s}")
22
+ 1.times {|ci| Comment.create(content: "comment content #{ci}", post_id: post.id)}
23
+ end
24
+ end
25
+
26
+ def create_post_table
27
+ ActiveRecord::Migration.create_table :posts do |t|
28
+ t.string :title
29
+ t.text :content
30
+ end
31
+ end
32
+
33
+ def create_comment_table
34
+ ActiveRecord::Migration.create_table :comments do |t|
35
+ t.string :content
36
+ t.integer :post_id
37
+ end
38
+ end
39
+
40
+ seed_data
41
+
42
+ # ActiveRecord::Base.logger = Logger.new(STDOUT)
43
+
44
+ Benchmark.ips do |x|
45
+
46
+ x.report("PgFlashJson#to_pg_json") do
47
+ Post.limit(100).offset(100).to_pg_json
48
+ end
49
+
50
+ x.report("ActiveRecord#to_json") do
51
+ Post.limit(100).to_json
52
+ end
53
+
54
+ x.compare!
55
+ end
56
+
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,5 @@
1
+ {
2
+ "result": {
3
+ "covered_percent": 92.14
4
+ }
5
+ }
File without changes
@@ -0,0 +1,4 @@
1
+ require "pg_flash_json/version"
2
+ require "pg_flash_json/pg_json_builder"
3
+ require "pg_flash_json/sql_alias"
4
+ require "pg_flash_json/pg_json_extension"
@@ -0,0 +1,40 @@
1
+ class PGJsonBuilder
2
+
3
+ attr_accessor :attrs, :relation, :_alias
4
+
5
+ def initialize(relation, opts={})
6
+ @relation = relation
7
+ raise ArgumentError unless @relation.is_a?(ActiveRecord::Relation) || @relation.is_a?(ActiveRecord::AssociationRelation)
8
+ @_alias = SqlAlias.new.sql_alias
9
+ @attrs = opts.fetch(:attrs, @relation.column_names)
10
+ SqlAlias.clear
11
+ self
12
+ end
13
+
14
+ def attr_pairs_string
15
+ aps = ""
16
+ @attrs.each do |a|
17
+ a = a.to_s
18
+ value = "#{@_alias}.#{a}"
19
+ aps << "'#{a}', #{value},"
20
+ end
21
+ aps[0..-2]
22
+ end
23
+
24
+ def to_sql
25
+ build_json_object_query + query_end
26
+ end
27
+
28
+ def build_json_object_query(from_sub=false, relation_aps=attr_pairs_string)
29
+ first_char = from_sub ? "(" : ""
30
+ "#{first_char}SELECT json_agg( json_build_object(#{relation_aps}))"
31
+ end
32
+
33
+ def query_end
34
+ " as json FROM(#{@relation.to_sql})#{@_alias}"
35
+ end
36
+
37
+ def json
38
+ @relation.connection.execute(to_sql).first["json"]
39
+ end
40
+ end
@@ -0,0 +1,14 @@
1
+ require 'active_record'
2
+
3
+ module ActiveRecord
4
+ module PgJsonExtension
5
+
6
+ def to_pg_json
7
+ PGJsonBuilder.new(self).json
8
+ end
9
+
10
+
11
+ end
12
+ end
13
+
14
+ ActiveRecord::Relation.include ActiveRecord::PgJsonExtension
@@ -0,0 +1,27 @@
1
+ class SqlAlias
2
+ @used = []
3
+
4
+ attr_accessor :sql_alias
5
+
6
+ def self.used
7
+ @used
8
+ end
9
+
10
+ def self.clear
11
+ @used = []
12
+ end
13
+
14
+ def initialize
15
+ @sql_alias = new_alias
16
+ self.class.used << @sql_alias
17
+ end
18
+
19
+ def new_alias
20
+ alias_needed = true
21
+ while alias_needed do
22
+ potential_alias = "t#{rand(100000)}"
23
+ alias_needed = self.class.used.include?(potential_alias)
24
+ end
25
+ potential_alias
26
+ end
27
+ end
@@ -0,0 +1,3 @@
1
+ module PgFlashJson
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'pg_flash_json/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "pg_flash_json"
8
+ spec.version = PgFlashJson::VERSION
9
+ spec.authors = ["dandlezzz"]
10
+ spec.email = ["danm@workwithopal.com"]
11
+
12
+ spec.summary = %{Use postgres to generate json from active record relations.}
13
+ spec.description = %q{This gem allows you to rapidly generate large amount of json in an activerecord application. By relying on postgres' native functionality,
14
+ we can skip object instantiation and turn active record queries directly into json.}
15
+ spec.homepage = "https://github.com/dandlezzz/pg_flash_json"
16
+ spec.license = "MIT"
17
+
18
+
19
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|pkg)/}) }
20
+ spec.bindir = "bin"
21
+ spec.executables = ["benchmarks"]
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ spec.add_development_dependency "activerecord", "~> 4.2"
28
+ spec.add_development_dependency "pg"
29
+ spec.add_development_dependency "byebug"
30
+ spec.add_development_dependency "benchmark-ips"
31
+ end
metadata ADDED
@@ -0,0 +1,161 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pg_flash_json
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - dandlezzz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-01-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activerecord
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '4.2'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '4.2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pg
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: byebug
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: benchmark-ips
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: |-
112
+ This gem allows you to rapidly generate large amount of json in an activerecord application. By relying on postgres' native functionality,
113
+ we can skip object instantiation and turn active record queries directly into json.
114
+ email:
115
+ - danm@workwithopal.com
116
+ executables:
117
+ - benchmarks
118
+ extensions: []
119
+ extra_rdoc_files: []
120
+ files:
121
+ - ".gitignore"
122
+ - Gemfile
123
+ - Gemfile.lock
124
+ - LICENSE.txt
125
+ - README.md
126
+ - Rakefile
127
+ - bin/benchmarks
128
+ - bin/setup
129
+ - coverage/.last_run.json
130
+ - coverage/.resultset.json.lock
131
+ - lib/pg_flash_json.rb
132
+ - lib/pg_flash_json/pg_json_builder.rb
133
+ - lib/pg_flash_json/pg_json_extension.rb
134
+ - lib/pg_flash_json/sql_alias.rb
135
+ - lib/pg_flash_json/version.rb
136
+ - pg_flash_json.gemspec
137
+ homepage: https://github.com/dandlezzz/pg_flash_json
138
+ licenses:
139
+ - MIT
140
+ metadata: {}
141
+ post_install_message:
142
+ rdoc_options: []
143
+ require_paths:
144
+ - lib
145
+ required_ruby_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ required_rubygems_version: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ requirements: []
156
+ rubyforge_project:
157
+ rubygems_version: 2.4.8
158
+ signing_key:
159
+ specification_version: 4
160
+ summary: Use postgres to generate json from active record relations.
161
+ test_files: []