graphd 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f2d4e121c4e6a3a5150912b20ae0bca19a48d2e9de535c965615a7d5558bb770
4
+ data.tar.gz: e4af93a38448a47796d19c54a50adf9921222f0dc2c4dbbd18882270bae74924
5
+ SHA512:
6
+ metadata.gz: f3d9a2613043742932f0fc4e50253c9ca82f55611af86a0556a5168ffcb4e7be1c7122c3bce3c9eb6e14c8695c0650fb2f290db951e3cda1517a96a8dc5750fe
7
+ data.tar.gz: 2afdaca018157f54aa5851371257582231574ccd82f6315cd7f8129b9c46ae54c2f9a8f01c6350c4a3508c4326681b2c1d05577e7c3343cc30794aa79add5b8b
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ .byebug_history
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,6 @@
1
+ AllCops:
2
+ Exclude:
3
+ - 'lib/protos'
4
+ - 'lib/graphd/api_pb.rb'
5
+ - 'lib/graphd/api_services_pb.rb'
6
+ TargetRubyVersion: 2.7
@@ -0,0 +1 @@
1
+ 2.7.2
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.6.6
6
+ before_install: gem install bundler -v 2.1.4
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at iamgeorgethomas@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in graphd.gemspec
6
+ gemspec
7
+
8
+ gem 'grpc'
9
+ gem 'rake', '~> 12.0'
10
+ gem 'rspec', '~> 3.0'
11
+ gem 'rubocop'
12
+ gem 'rubocop-rake'
13
+ gem 'rubocop-rspec'
@@ -0,0 +1,69 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ graphd (0.4.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.1)
10
+ diff-lcs (1.4.4)
11
+ google-protobuf (3.14.0)
12
+ googleapis-common-protos-types (1.0.5)
13
+ google-protobuf (~> 3.11)
14
+ grpc (1.34.0)
15
+ google-protobuf (~> 3.13)
16
+ googleapis-common-protos-types (~> 1.0)
17
+ parallel (1.20.1)
18
+ parser (3.0.0.0)
19
+ ast (~> 2.4.1)
20
+ rainbow (3.0.0)
21
+ rake (12.3.3)
22
+ regexp_parser (2.0.2)
23
+ rexml (3.2.4)
24
+ rspec (3.10.0)
25
+ rspec-core (~> 3.10.0)
26
+ rspec-expectations (~> 3.10.0)
27
+ rspec-mocks (~> 3.10.0)
28
+ rspec-core (3.10.0)
29
+ rspec-support (~> 3.10.0)
30
+ rspec-expectations (3.10.0)
31
+ diff-lcs (>= 1.2.0, < 2.0)
32
+ rspec-support (~> 3.10.0)
33
+ rspec-mocks (3.10.0)
34
+ diff-lcs (>= 1.2.0, < 2.0)
35
+ rspec-support (~> 3.10.0)
36
+ rspec-support (3.10.0)
37
+ rubocop (1.7.0)
38
+ parallel (~> 1.10)
39
+ parser (>= 2.7.1.5)
40
+ rainbow (>= 2.2.2, < 4.0)
41
+ regexp_parser (>= 1.8, < 3.0)
42
+ rexml
43
+ rubocop-ast (>= 1.2.0, < 2.0)
44
+ ruby-progressbar (~> 1.7)
45
+ unicode-display_width (>= 1.4.0, < 2.0)
46
+ rubocop-ast (1.3.0)
47
+ parser (>= 2.7.1.5)
48
+ rubocop-rake (0.5.1)
49
+ rubocop
50
+ rubocop-rspec (2.1.0)
51
+ rubocop (~> 1.0)
52
+ rubocop-ast (>= 1.1.0)
53
+ ruby-progressbar (1.10.1)
54
+ unicode-display_width (1.7.0)
55
+
56
+ PLATFORMS
57
+ ruby
58
+
59
+ DEPENDENCIES
60
+ graphd!
61
+ grpc
62
+ rake (~> 12.0)
63
+ rspec (~> 3.0)
64
+ rubocop
65
+ rubocop-rake
66
+ rubocop-rspec
67
+
68
+ BUNDLED WITH
69
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 George Thomas
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,40 @@
1
+ # Graphd
2
+ A Ruby client for [DGraph](https://github.com/dgraph-io/dgraph) that uses [gRPC](https://grpc.io/).
3
+
4
+ This client follows [pydgraph](https://github.com/dgraph-io/pydgraph) closely.
5
+
6
+ **This gem is under active development. The API may change in future versions**
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'graphd'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle install
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install graphd
23
+
24
+ ## Contributing
25
+
26
+ Bug reports and pull requests are welcome on GitHub at https://github.com/thegeorgeous/graphd. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/thegeorgeous/graphd/blob/master/CODE_OF_CONDUCT.md).
27
+
28
+ ## License
29
+
30
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
31
+
32
+ ## Code of Conduct
33
+
34
+ Everyone interacting in the Graphd project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/thegeorgeous/graphd/blob/master/CODE_OF_CONDUCT.md).
35
+
36
+ ## TODO
37
+ - Add support for metadata headers
38
+ - Add support for timeouts
39
+ - Add support for credentials
40
+ - Add support for async calls
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'graphd'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,207 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'date'
4
+ require 'graphd'
5
+
6
+ def client_stub
7
+ @client_stub ||= Graphd::ClientStub.new('localhost:9080')
8
+ end
9
+
10
+ def client(client_stub)
11
+ @client ||= Graphd::Client.new(client_stub)
12
+ end
13
+
14
+ def drop_all(client)
15
+ client.alter(::Api::Operation.new(drop_all: true))
16
+ end
17
+
18
+ def create_schema(client)
19
+ schema = "
20
+ name: string @index(exact) .
21
+ friend: [uid] @reverse .
22
+ age: int .
23
+ married: bool .
24
+ loc: geo .
25
+ dob: datetime .
26
+ type Person {
27
+ name
28
+ friend
29
+ age
30
+ married
31
+ loc
32
+ dob
33
+ }
34
+ "
35
+ client.alter(::Api::Operation.new(schema: schema))
36
+ end
37
+
38
+ def create_data(client)
39
+ # Create a new transaction.
40
+ txn = client.txn
41
+ # Create data.
42
+ p = {
43
+ 'uid': '_:alice',
44
+ 'dgraph.type': 'Person',
45
+ 'name': 'Alice',
46
+ 'age': 26,
47
+ 'married': true,
48
+ 'loc': {
49
+ 'type': 'Point',
50
+ 'coordinates': [1.1, 2]
51
+ },
52
+ 'dob': DateTime.new(1980, 1, 1, 23, 0, 0, 0),
53
+ 'friend': [
54
+ {
55
+ 'uid': '_:bob',
56
+ 'dgraph.type': 'Person',
57
+ 'name': 'Bob',
58
+ 'age': 24
59
+ }
60
+ ],
61
+ 'school': [
62
+ {
63
+ 'name': 'Crown Public School'
64
+ }
65
+ ]
66
+ }
67
+ # Run mutation.
68
+ response = txn.mutate(set_obj: p)
69
+
70
+ # Commit transaction.
71
+ txn.commit
72
+
73
+ # Get uid of the outermost object (person named "Alice").
74
+ # response.uids returns a map from blank node names to uids.
75
+ p "Created person named 'Alice' with uid = #{response.uids[:alice]}"
76
+
77
+ # Clean up. Calling this after txn.commit() is a no-op and hence safe.
78
+ txn.discard
79
+ end
80
+
81
+ # Deleting a data
82
+ def delete_data(client)
83
+ # Create a new transaction.
84
+ txn = client.txn
85
+ query1 = "query all($a: string) {
86
+ all(func: eq(name, $a)) {
87
+ uid
88
+ }
89
+ }"
90
+ variables1 = { '$a': 'Bob' }
91
+ res1 = client.txn(read_only: true).query(query1, variables: variables1)
92
+ ppl1 = JSON.parse(res1.json)
93
+
94
+ ppl1['all'].each do |person|
95
+ p "Bob's UID: #{person['uid']}"
96
+ txn.mutate(del_obj: person)
97
+ p 'Bob deleted'
98
+ txn.commit
99
+ end
100
+
101
+ txn.discard
102
+ end
103
+
104
+ # Query for data.
105
+ def query_alice(client)
106
+ # Run query.
107
+ query = "query all($a: string) {
108
+ all(func: eq(name, $a)) {
109
+ uid
110
+ name
111
+ age
112
+ married
113
+ loc
114
+ dob
115
+ friend {
116
+ name
117
+ age
118
+ }
119
+ school {
120
+ name
121
+ }
122
+ }
123
+ }"
124
+
125
+ variables = { '$a': 'Alice' }
126
+ res = client.txn(read_only: true).query(query, variables: variables)
127
+ ppl = JSON.parse(res.json)
128
+
129
+ # Print results.
130
+ p "Number of people named 'Alice': #{ppl['all'].length}"
131
+ end
132
+
133
+ # Query to check for deleted node
134
+ def query_bob(client)
135
+ query = "query all($b: string) {
136
+ all(func: eq(name, $b)) {
137
+ uid
138
+ name
139
+ age
140
+ friend {
141
+ uid
142
+ name
143
+ age
144
+ }
145
+ ~friend {
146
+ uid
147
+ name
148
+ age
149
+ }
150
+ }
151
+ }"
152
+
153
+ variables = { '$b': 'Bob' }
154
+ res = client.txn(read_only: true).query(query, variables: variables)
155
+ ppl = JSON.parse(res.json)
156
+
157
+ # Print results.
158
+ p "Number of people named 'Bob': #{ppl['all'].length}"
159
+ end
160
+
161
+ def upsert(client)
162
+ txn = client.txn
163
+ query = '{
164
+ u as var(func: eq(name, "Jonas"))
165
+ }'
166
+ nquad = '
167
+ uid(u) <name> "Jonas" .
168
+ uid(u) <age> "25" .
169
+ '
170
+ mutation = txn.create_mutation(set_nquads: nquad)
171
+ request = txn.create_request(query: query, mutations: [mutation], commit_now: true)
172
+ txn.do_request(request)
173
+ end
174
+
175
+ def cond_upsert(client)
176
+ txn = client.txn
177
+ query = '
178
+ {
179
+ user as var(func: eq(name, "Jonas"))
180
+ }
181
+ '
182
+ cond = '@if(eq(len(user), 1))'
183
+ nquads = '
184
+ uid(user) <name> "Jonas Kahnwald" .
185
+ '
186
+ mutation = txn.create_mutation(cond: cond, set_nquads: nquads)
187
+ request = txn.create_request(mutations: [mutation], query: query, commit_now: true)
188
+ txn.do_request(request)
189
+ end
190
+
191
+ def run
192
+ dgraph_client = client(client_stub)
193
+ version = dgraph_client.check_version
194
+ p version
195
+ drop_all(dgraph_client)
196
+ create_schema(dgraph_client)
197
+ create_data(dgraph_client)
198
+ query_alice(dgraph_client)
199
+ query_bob(dgraph_client)
200
+ delete_data(dgraph_client)
201
+ query_alice(dgraph_client)
202
+ query_bob(dgraph_client)
203
+ upsert(dgraph_client)
204
+ cond_upsert(dgraph_client)
205
+ end
206
+
207
+ run
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/graphd/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'graphd'
7
+ spec.version = Graphd::VERSION
8
+ spec.authors = ['George Thomas']
9
+ spec.email = ['iamgeorgethomas@gmail.com']
10
+
11
+ spec.summary = 'Ruby client for DGraph'
12
+ spec.homepage = 'https://github.com/thegeorgeous/graphd'
13
+ spec.license = 'MIT'
14
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.7.0')
15
+
16
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
17
+
18
+ spec.metadata['homepage_uri'] = spec.homepage
19
+ spec.metadata['source_code_uri'] = 'https://rubygems.org/graphd'
20
+ spec.metadata['changelog_uri'] = 'https://github.com/thegeorgeous/graphd'
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
25
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ end
27
+ spec.bindir = 'bin'
28
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
29
+ spec.require_paths = ['lib']
30
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'graphd/version'
4
+ require_relative 'graphd/api_pb'
5
+ require_relative 'graphd/api_services_pb'
6
+ require_relative 'graphd/client_stub'
7
+ require_relative 'graphd/client'
@@ -0,0 +1,10 @@
1
+ # lib/aborted_error.rb
2
+
3
+ module Graphd
4
+ # Error thrown by aborted transaction
5
+ class AbortedError < StandardError
6
+ def initialize(msg = 'Transaction has been aborted. Please retry')
7
+ super(msg)
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,146 @@
1
+ # Generated by the protocol buffer compiler. DO NOT EDIT!
2
+ # source: api.proto
3
+
4
+ require 'google/protobuf'
5
+
6
+ Google::Protobuf::DescriptorPool.generated_pool.build do
7
+ add_file("api.proto", :syntax => :proto3) do
8
+ add_message "api.Request" do
9
+ optional :start_ts, :uint64, 1
10
+ optional :query, :string, 4
11
+ map :vars, :string, :string, 5
12
+ optional :read_only, :bool, 6
13
+ optional :best_effort, :bool, 7
14
+ repeated :mutations, :message, 12, "api.Mutation"
15
+ optional :commit_now, :bool, 13
16
+ end
17
+ add_message "api.Uids" do
18
+ repeated :uids, :string, 1
19
+ end
20
+ add_message "api.Response" do
21
+ optional :json, :bytes, 1
22
+ optional :txn, :message, 2, "api.TxnContext"
23
+ optional :latency, :message, 3, "api.Latency"
24
+ optional :metrics, :message, 4, "api.Metrics"
25
+ map :uids, :string, :string, 12
26
+ end
27
+ add_message "api.Mutation" do
28
+ optional :set_json, :bytes, 1
29
+ optional :delete_json, :bytes, 2
30
+ optional :set_nquads, :bytes, 3
31
+ optional :del_nquads, :bytes, 4
32
+ repeated :set, :message, 5, "api.NQuad"
33
+ repeated :del, :message, 6, "api.NQuad"
34
+ optional :cond, :string, 9
35
+ optional :commit_now, :bool, 14
36
+ end
37
+ add_message "api.Operation" do
38
+ optional :schema, :string, 1
39
+ optional :drop_attr, :string, 2
40
+ optional :drop_all, :bool, 3
41
+ optional :drop_op, :enum, 4, "api.Operation.DropOp"
42
+ optional :drop_value, :string, 5
43
+ optional :run_in_background, :bool, 6
44
+ end
45
+ add_enum "api.Operation.DropOp" do
46
+ value :NONE, 0
47
+ value :ALL, 1
48
+ value :DATA, 2
49
+ value :ATTR, 3
50
+ value :TYPE, 4
51
+ end
52
+ add_message "api.Payload" do
53
+ optional :Data, :bytes, 1
54
+ end
55
+ add_message "api.TxnContext" do
56
+ optional :start_ts, :uint64, 1
57
+ optional :commit_ts, :uint64, 2
58
+ optional :aborted, :bool, 3
59
+ repeated :keys, :string, 4
60
+ repeated :preds, :string, 5
61
+ end
62
+ add_message "api.Check" do
63
+ end
64
+ add_message "api.Version" do
65
+ optional :tag, :string, 1
66
+ end
67
+ add_message "api.Latency" do
68
+ optional :parsing_ns, :uint64, 1
69
+ optional :processing_ns, :uint64, 2
70
+ optional :encoding_ns, :uint64, 3
71
+ optional :assign_timestamp_ns, :uint64, 4
72
+ optional :total_ns, :uint64, 5
73
+ end
74
+ add_message "api.Metrics" do
75
+ map :num_uids, :string, :uint64, 1
76
+ end
77
+ add_message "api.NQuad" do
78
+ optional :subject, :string, 1
79
+ optional :predicate, :string, 2
80
+ optional :object_id, :string, 3
81
+ optional :object_value, :message, 4, "api.Value"
82
+ optional :label, :string, 5
83
+ optional :lang, :string, 6
84
+ repeated :facets, :message, 7, "api.Facet"
85
+ end
86
+ add_message "api.Value" do
87
+ oneof :val do
88
+ optional :default_val, :string, 1
89
+ optional :bytes_val, :bytes, 2
90
+ optional :int_val, :int64, 3
91
+ optional :bool_val, :bool, 4
92
+ optional :str_val, :string, 5
93
+ optional :double_val, :double, 6
94
+ optional :geo_val, :bytes, 7
95
+ optional :date_val, :bytes, 8
96
+ optional :datetime_val, :bytes, 9
97
+ optional :password_val, :string, 10
98
+ optional :uid_val, :uint64, 11
99
+ end
100
+ end
101
+ add_message "api.Facet" do
102
+ optional :key, :string, 1
103
+ optional :value, :bytes, 2
104
+ optional :val_type, :enum, 3, "api.Facet.ValType"
105
+ repeated :tokens, :string, 4
106
+ optional :alias, :string, 5
107
+ end
108
+ add_enum "api.Facet.ValType" do
109
+ value :STRING, 0
110
+ value :INT, 1
111
+ value :FLOAT, 2
112
+ value :BOOL, 3
113
+ value :DATETIME, 4
114
+ end
115
+ add_message "api.LoginRequest" do
116
+ optional :userid, :string, 1
117
+ optional :password, :string, 2
118
+ optional :refresh_token, :string, 3
119
+ end
120
+ add_message "api.Jwt" do
121
+ optional :access_jwt, :string, 1
122
+ optional :refresh_jwt, :string, 2
123
+ end
124
+ end
125
+ end
126
+
127
+ module Api
128
+ Request = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("api.Request").msgclass
129
+ Uids = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("api.Uids").msgclass
130
+ Response = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("api.Response").msgclass
131
+ Mutation = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("api.Mutation").msgclass
132
+ Operation = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("api.Operation").msgclass
133
+ Operation::DropOp = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("api.Operation.DropOp").enummodule
134
+ Payload = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("api.Payload").msgclass
135
+ TxnContext = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("api.TxnContext").msgclass
136
+ Check = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("api.Check").msgclass
137
+ Version = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("api.Version").msgclass
138
+ Latency = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("api.Latency").msgclass
139
+ Metrics = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("api.Metrics").msgclass
140
+ NQuad = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("api.NQuad").msgclass
141
+ Value = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("api.Value").msgclass
142
+ Facet = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("api.Facet").msgclass
143
+ Facet::ValType = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("api.Facet.ValType").enummodule
144
+ LoginRequest = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("api.LoginRequest").msgclass
145
+ Jwt = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("api.Jwt").msgclass
146
+ end
@@ -0,0 +1,32 @@
1
+ # Generated by the protocol buffer compiler. DO NOT EDIT!
2
+ # Source: api.proto for package 'api'
3
+ # Original file comments:
4
+ # Style guide for Protocol Buffer 3.
5
+ # Use CamelCase (with an initial capital) for message names – for example,
6
+ # SongServerRequest. Use underscore_separated_names for field names – for
7
+ # example, song_name.
8
+ #
9
+
10
+ require 'grpc'
11
+ require_relative 'api_pb'
12
+
13
+ module Api
14
+ module Dgraph
15
+ # Graph response.
16
+ class Service
17
+ include GRPC::GenericService
18
+
19
+ self.marshal_class_method = :encode
20
+ self.unmarshal_class_method = :decode
21
+ self.service_name = 'api.Dgraph'
22
+
23
+ rpc :Login, ::Api::LoginRequest, ::Api::Response
24
+ rpc :Query, ::Api::Request, ::Api::Response
25
+ rpc :Alter, ::Api::Operation, ::Api::Payload
26
+ rpc :CommitOrAbort, ::Api::TxnContext, ::Api::TxnContext
27
+ rpc :CheckVersion, ::Api::Check, ::Api::Version
28
+ end
29
+
30
+ Stub = Service.rpc_stub_class
31
+ end
32
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'api_pb'
4
+ require_relative 'transaction'
5
+
6
+ module Graphd
7
+ # Client initialized to talk to a DGraph instance
8
+ # Accepts multiple instances of Graphd::ClientStub
9
+ # Examples:
10
+ # client_stub = Graphd::ClientStub.new('localhost:9080')
11
+ # client = Graphd::Cilent.new(client_stub)
12
+ class Client
13
+ def initialize(*clients)
14
+ raise ClientError unless clients
15
+
16
+ @clients = clients
17
+ @jwt = Api::Jwt.new
18
+ end
19
+
20
+ def check_version
21
+ request = Api::Check.new
22
+ response = client.check_version(request)
23
+ response.tag
24
+ end
25
+
26
+ def alter(operation)
27
+ client.alter(operation)
28
+ end
29
+
30
+ def txn(read_only: false)
31
+ Transaction.new(self, read_only: read_only)
32
+ end
33
+
34
+ def client
35
+ @clients.sample
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Raised when there are errors in the client, duh!
4
+ class ClientError < StandardError
5
+ def initialize(msg = 'No client provided')
6
+ super
7
+ end
8
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'grpc'
4
+ require_relative 'api_services_pb'
5
+
6
+ module Graphd
7
+ # gRPC Client stub for DGraph
8
+ class ClientStub
9
+ attr_reader :stub
10
+
11
+ def initialize(
12
+ host = 'localhost:9080',
13
+ credentials = :this_channel_is_insecure,
14
+ channel_args = {}
15
+ )
16
+ @stub = Api::Dgraph::Stub.new(host, credentials, channel_args)
17
+ end
18
+
19
+ def check_version(request)
20
+ @stub.check_version(request)
21
+ end
22
+
23
+ def alter(operation)
24
+ @stub.alter(operation)
25
+ end
26
+
27
+ def query(request)
28
+ @stub.query(request)
29
+ end
30
+
31
+ def commit_or_abort(transaction_context:)
32
+ @stub.commit_or_abort(transaction_context)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,139 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+
5
+ module Graphd
6
+ class Transaction
7
+ def initialize(client, read_only: false)
8
+ @client = client
9
+ @client_stub = @client.client
10
+ @transaction_context = ::Api::TxnContext.new
11
+ @finished = false
12
+ @mutated = false
13
+ @read_only = read_only
14
+ end
15
+
16
+ def mutate(mutation: nil, set_obj: nil, del_obj: nil, commit_now: nil)
17
+ request_mutation = create_mutation(mutation: mutation, set_obj: set_obj, del_obj: del_obj)
18
+ commit_now ||= request_mutation.commit_now
19
+ request = create_request(mutations: [request_mutation], commit_now: commit_now)
20
+ do_request(request)
21
+ end
22
+
23
+ def query(query, variables: nil)
24
+ request = create_request(query: query, variables: variables)
25
+ do_request(request)
26
+ end
27
+
28
+ def create_mutation(mutation: nil, set_obj: nil, del_obj: nil, set_nquads: nil, del_nquads: nil, cond: nil)
29
+ mutation ||= ::Api::Mutation.new
30
+
31
+ mutation.set_json = set_obj.to_json if set_obj
32
+ mutation.delete_json = del_obj.to_json if del_obj
33
+ mutation.set_nquads = set_nquads if set_nquads
34
+ mutation.del_nquads = del_nquads if del_nquads
35
+ mutation.cond = cond if cond
36
+
37
+ mutation
38
+ end
39
+
40
+ def create_request(query: nil, variables: nil, mutations: nil, commit_now: nil)
41
+ request = ::Api::Request.new(start_ts: @transaction_context.start_ts, commit_now: commit_now)
42
+ variables&.each do |key, value|
43
+ if key.is_a?(String) && value.is_a?(String)
44
+ raise TransactionError, 'Values and keys in variable map must be strings'
45
+ end
46
+
47
+ request.vars[key] = value
48
+ end
49
+
50
+ request.query = query if query
51
+ request.mutations += mutations if mutations
52
+
53
+ request
54
+ end
55
+
56
+ def do_request(request)
57
+ raise TransactionError, 'Transaction has already been committed or discarded' if @finished
58
+
59
+ if request.mutations.length.positive?
60
+ raise TransactionError, 'Readonly transaction cannot run mutations' if @read_only
61
+
62
+ @mutated = true
63
+ end
64
+
65
+ query_error = nil
66
+
67
+ begin
68
+ @response = @client_stub.query(request)
69
+ rescue StandardError => e
70
+ query_error = e
71
+ end
72
+
73
+ discard if query_error
74
+
75
+ @finished = true if request.commit_now
76
+
77
+ merge_context(@response.txn)
78
+
79
+ @response
80
+ end
81
+
82
+ def discard
83
+ return unless common_discard
84
+
85
+ @client_stub.commit_or_abort(transaction_context: @transaction_context)
86
+ end
87
+
88
+ def merge_context(src = nil)
89
+ # This condition will be true only if the server doesn't return a
90
+ # txn context after a query or mutation.
91
+ return unless src
92
+
93
+ if @transaction_context.start_ts.zero?
94
+ @transaction_context.start_ts = src.start_ts
95
+ elsif @transaction_context.start_ts != src.start_ts
96
+ # This condition should never be true.
97
+ raise TransactionError, 'StartTs mismatch'
98
+ end
99
+
100
+ @transaction_context.keys += src.keys
101
+ @transaction_context.preds += src.preds
102
+ end
103
+
104
+ def commit
105
+ return unless common_commit
106
+
107
+ begin
108
+ @client_stub.commit_or_abort(transaction_context: @transaction_context)
109
+ rescue StandardError => e
110
+ common_except_commit(e)
111
+ end
112
+ end
113
+
114
+ private
115
+
116
+ def common_discard
117
+ return false if @finished
118
+
119
+ @finished = true
120
+ return false unless @mutated
121
+
122
+ @transaction_context.aborted = true
123
+ true
124
+ end
125
+
126
+ def common_commit
127
+ raise TransactionError, 'Readonly transaction cannot run mutations or be committed' if @read_only
128
+
129
+ raise TransactionError, 'Transaction has already been committed or discarded' if @finished
130
+
131
+ @finished = true
132
+ @mutated
133
+ end
134
+
135
+ def common_except_commit(error)
136
+ raise error
137
+ end
138
+ end
139
+ end
@@ -0,0 +1,6 @@
1
+ # lib/transaction_error.rb
2
+
3
+ module Graphd
4
+ class TransactionError < StandardError
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Graphd
4
+ VERSION = '0.4.0'
5
+ end
@@ -0,0 +1,176 @@
1
+ // Style guide for Protocol Buffer 3.
2
+ // Use CamelCase (with an initial capital) for message names – for example,
3
+ // SongServerRequest. Use underscore_separated_names for field names – for
4
+ // example, song_name.
5
+
6
+ syntax = "proto3";
7
+
8
+ package api;
9
+
10
+ /* import "gogoproto/gogo.proto"; */
11
+
12
+ /* option (gogoproto.marshaler_all) = true; */
13
+ /* option (gogoproto.sizer_all) = true; */
14
+ /* option (gogoproto.unmarshaler_all) = true; */
15
+ /* option (gogoproto.goproto_getters_all) = true; */
16
+
17
+ option java_package = "io.dgraph";
18
+ option java_outer_classname = "DgraphProto";
19
+
20
+ // Graph response.
21
+ service Dgraph {
22
+ rpc Login (LoginRequest) returns (Response) {}
23
+ rpc Query (Request) returns (Response) {}
24
+ rpc Alter (Operation) returns (Payload) {}
25
+ rpc CommitOrAbort (TxnContext) returns (TxnContext) {}
26
+ rpc CheckVersion(Check) returns (Version) {}
27
+ }
28
+
29
+ message Request {
30
+ uint64 start_ts = 1;
31
+
32
+ string query = 4;
33
+ map<string, string> vars = 5; // Support for GraphQL like variables.
34
+ bool read_only = 6;
35
+ bool best_effort = 7;
36
+
37
+ repeated Mutation mutations = 12;
38
+ bool commit_now = 13;
39
+ }
40
+
41
+ message Uids {
42
+ repeated string uids = 1;
43
+ }
44
+
45
+ message Response {
46
+ bytes json = 1;
47
+ TxnContext txn = 2;
48
+ Latency latency = 3;
49
+ // Metrics contains all metrics related to the query.
50
+ Metrics metrics = 4;
51
+ // uids contains a mapping of blank_node => uid for the node. It only returns uids
52
+ // that were created as part of a mutation.
53
+ map<string, string> uids = 12;
54
+ }
55
+
56
+ message Mutation {
57
+ bytes set_json = 1;
58
+ bytes delete_json = 2;
59
+ bytes set_nquads = 3;
60
+ bytes del_nquads = 4;
61
+ repeated NQuad set = 5;
62
+ repeated NQuad del = 6;
63
+
64
+ // This is being used for upserts.
65
+ string cond = 9;
66
+
67
+ // This field is a duplicate of the one in Request and placed here for convenience.
68
+ bool commit_now = 14;
69
+ }
70
+
71
+ message Operation {
72
+ string schema = 1;
73
+ string drop_attr = 2;
74
+ bool drop_all = 3;
75
+
76
+ enum DropOp {
77
+ NONE = 0;
78
+ ALL = 1;
79
+ DATA = 2;
80
+ ATTR = 3;
81
+ TYPE = 4;
82
+ }
83
+ DropOp drop_op = 4;
84
+
85
+ // If drop_op is ATTR or TYPE, drop_value holds the name of the predicate or
86
+ // type to delete.
87
+ string drop_value = 5;
88
+
89
+ // run indexes in background.
90
+ bool run_in_background = 6;
91
+ }
92
+
93
+ // Worker services.
94
+ message Payload {
95
+ bytes Data = 1;
96
+ }
97
+
98
+ message TxnContext {
99
+ uint64 start_ts = 1;
100
+ uint64 commit_ts = 2;
101
+ bool aborted = 3;
102
+ repeated string keys = 4; // List of keys to be used for conflict detection.
103
+ repeated string preds = 5; // List of predicates involved in this transaction.
104
+ }
105
+
106
+ message Check {}
107
+
108
+ message Version {
109
+ string tag = 1;
110
+ }
111
+
112
+ message Latency {
113
+ uint64 parsing_ns = 1;
114
+ uint64 processing_ns = 2;
115
+ uint64 encoding_ns = 3;
116
+ uint64 assign_timestamp_ns = 4;
117
+ uint64 total_ns = 5;
118
+ }
119
+
120
+ message Metrics {
121
+ // num_uids is the map of number of uids processed by each attribute.
122
+ map<string, uint64> num_uids = 1;
123
+ }
124
+
125
+ message NQuad {
126
+ string subject = 1;
127
+ string predicate = 2;
128
+ string object_id = 3;
129
+ Value object_value = 4;
130
+ string label = 5;
131
+ string lang = 6;
132
+ repeated Facet facets = 7;
133
+ }
134
+
135
+ message Value {
136
+ oneof val {
137
+ string default_val = 1;
138
+ bytes bytes_val = 2;
139
+ int64 int_val = 3;
140
+ bool bool_val = 4;
141
+ string str_val = 5;
142
+ double double_val = 6;
143
+ bytes geo_val = 7; // Geo data in WKB format
144
+ bytes date_val = 8;
145
+ bytes datetime_val = 9;
146
+ string password_val = 10;
147
+ uint64 uid_val=11;
148
+ }
149
+ }
150
+
151
+ message Facet {
152
+ enum ValType {
153
+ STRING = 0;
154
+ INT = 1;
155
+ FLOAT = 2;
156
+ BOOL = 3;
157
+ DATETIME = 4;
158
+ }
159
+
160
+ string key = 1;
161
+ bytes value = 2;
162
+ ValType val_type = 3;
163
+ repeated string tokens = 4; // tokens of value.
164
+ string alias = 5; // not stored, only used for query.
165
+ }
166
+
167
+ message LoginRequest {
168
+ string userid = 1;
169
+ string password = 2;
170
+ string refresh_token = 3;
171
+ }
172
+
173
+ message Jwt {
174
+ string access_jwt = 1;
175
+ string refresh_jwt = 2;
176
+ }
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: graphd
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0
5
+ platform: ruby
6
+ authors:
7
+ - George Thomas
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-12-27 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - iamgeorgethomas@gmail.com
16
+ executables:
17
+ - console
18
+ - setup
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - ".gitignore"
23
+ - ".rspec"
24
+ - ".rubocop.yml"
25
+ - ".ruby-version"
26
+ - ".travis.yml"
27
+ - CODE_OF_CONDUCT.md
28
+ - Gemfile
29
+ - Gemfile.lock
30
+ - LICENSE.txt
31
+ - README.md
32
+ - Rakefile
33
+ - bin/console
34
+ - bin/setup
35
+ - examples/simple_example.rb
36
+ - graphd.gemspec
37
+ - lib/graphd.rb
38
+ - lib/graphd/aborted_error.rb
39
+ - lib/graphd/api_pb.rb
40
+ - lib/graphd/api_services_pb.rb
41
+ - lib/graphd/client.rb
42
+ - lib/graphd/client_error.rb
43
+ - lib/graphd/client_stub.rb
44
+ - lib/graphd/transaction.rb
45
+ - lib/graphd/transaction_error.rb
46
+ - lib/graphd/version.rb
47
+ - lib/protos/api.proto
48
+ homepage: https://github.com/thegeorgeous/graphd
49
+ licenses:
50
+ - MIT
51
+ metadata:
52
+ allowed_push_host: https://rubygems.org
53
+ homepage_uri: https://github.com/thegeorgeous/graphd
54
+ source_code_uri: https://rubygems.org/graphd
55
+ changelog_uri: https://github.com/thegeorgeous/graphd
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 2.7.0
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubygems_version: 3.1.4
72
+ signing_key:
73
+ specification_version: 4
74
+ summary: Ruby client for DGraph
75
+ test_files: []