Skip to content

Commit

Permalink
Finish 2.0.0.beta1
Browse files Browse the repository at this point in the history
  • Loading branch information
gkellogg committed Feb 22, 2016
2 parents 4d690c9 + 29f33e2 commit 6b1fb84
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 58 deletions.
11 changes: 7 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ script: "bundle exec rspec spec"
env:
- CI=true
rvm:
- 1.9.3
- 2.0
- 2.1
- 2.2
- jruby
- 2.1.8
- 2.2.4
- 2.3.0
- jruby-9.0.4.0
- rbx-2
services:
- mongodb
cache: bundler
matrix:
allow_failures:
- rvm: rbx-2
6 changes: 4 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ source "http://rubygems.org"
gemspec

gem "rdf", git: "git://github.com/ruby-rdf/rdf.git", branch: "develop"
gem "rdf-spec", git: "git://github.com/ruby-rdf/rdf-spec.git", branch: "develop"
gem 'rdf-isomorphic', git: "git://github.com/ruby-rdf/rdf-isomorphic.git", branch: "develop"

group :development do
gem "rdf-spec", git: "git://github.com/ruby-rdf/rdf-spec.git", branch: "develop"
group :debug do
gem "byebug", platforms: :mri
gem "wirble"
end

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.99.0
2.0.0.beta1
104 changes: 62 additions & 42 deletions lib/rdf/mongo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def self.to_mongo(value, place_in_statement)
t, k1, lt = :ct, :c, :cl
end
h = {k1 => v, t => k, lt => ll}
h.delete_if {|k,v| h[k].nil?}
h.delete_if {|kk,_| h[kk].nil?}
end

##
Expand Down Expand Up @@ -108,48 +108,71 @@ def self.from_mongo(value, value_type = :u, literal_extra = nil)

class Repository < ::RDF::Repository
# The Mongo database instance
# @!attribute [r] db
# @return [Mongo::DB]
attr_reader :db
attr_reader :client

# The collection used for storing quads
# @!attribute [r] coll
# @return [Mongo::Collection]
attr_reader :coll
attr_reader :collection

##
# Initializes this repository instance.
#
# @param [Hash{Symbol => Object}] options
# @option options [URI, #to_s] :uri (nil)
# @option options [String, #to_s] :title (nil)
# @option options [String] :host
# @option options [Integer] :port
# @option options [String] :db
# @option options [String] :user for authentication
# @option options [String] :password for authentication
# @option options [String] :collection ('quads')
# @overload initialize(options = {}, &block)
# @param [Hash{Symbol => Object}] options
# @option options [String, #to_s] :title (nil)
# @option options [URI, #to_s] :uri (nil)
# URI in the form `mongodb://host:port/db`. The URI should also identify the collection use, but appending a `collection` path component such as `mongodb://host:port/db/collection`, this ensures that the collection will be maintained if cloned. See [Mongo::Client options](https://docs.mongodb.org/ecosystem/tutorial/ruby-driver-tutorial-2-0/#uri-options-conversions) for more information on Mongo URIs.
#
# @overload initialize(options = {}, &block)
# @param [Hash{Symbol => Object}] options
# See [Mongo::Client options](https://docs.mongodb.org/ecosystem/tutorial/ruby-driver-tutorial-2-0/#uri-options-conversions) for more information on Mongo Client options.
# @option options [String, #to_s] :title (nil)
# @option options [String] :host
# a single address or an array of addresses, which may contain a port designation
# @option options [Integer] :port (27017) applied to host address(es)
# @option options [String] :database ('quadb')
# @option options [String] :collection ('quads')
#
# @yield [repository]
# @yieldparam [Repository] repository
def initialize(options = {}, &block)
options = {host: 'localhost', port: 27017, db: 'quadb', collection: 'quads'}.merge(options)
@db = ::Mongo::Connection.new(options[:host], options[:port]).db(options[:db])
@db.authenticate(options[:user], options[:password]) if options[:user] && options[:password]
@coll = @db[options[:collection]]
@coll.create_index("s")
@coll.create_index("p")
@coll.create_index("o")
@coll.create_index("c")
@coll.create_index([["s", ::Mongo::ASCENDING], ["p", ::Mongo::ASCENDING]])
@coll.create_index([["s", ::Mongo::ASCENDING], ["o", ::Mongo::ASCENDING]])
@coll.create_index([["p", ::Mongo::ASCENDING], ["o", ::Mongo::ASCENDING]])
collection = nil
if options[:uri]
options = options.dup
uri = RDF::URI(options.delete(:uri))
_, db, coll = uri.path.split('/')
collection = coll || options.delete(:collection)
db ||= "quadb"
uri.path = "/#{db}" if coll
@client = ::Mongo::Client.new(uri.to_s, options)
else
warn "[DEPRECATION] RDF::Mongo::Repository#initialize expects a uri argument. Called from #{Gem.location_of_caller.join(':')}" unless options.empty?
options[:database] ||= options.delete(:db) # 1.x compat
options[:database] ||= 'quadb'
hosts = Array(options[:host] || 'localhost')
hosts.map! {|h| "#{h}:#{options[:port]}"} if options[:port]
@client = ::Mongo::Client.new(hosts, options)
end

@collection = @client[options.delete(:collection) || 'quads']
@collection.indexes.create_many([
{key: {s: 1}},
{key: {p: 1}},
{key: {o: "hashed"}},
{key: {c: 1}},
{key: {s: 1, p: 1}},
#{key: {s: 1, o: "hashed"}}, # Muti-key hashed indexes not allowed
#{key: {p: 1, o: "hashed"}}, # Muti-key hashed indexes not allowed
])
super(options, &block)
end

# @see RDF::Mutable#insert_statement
def supports?(feature)
case feature.to_sym
when :graph_name then true
when :validity then @options.fetch(:with_validity, true)
else false
end
end
Expand All @@ -159,16 +182,16 @@ def insert_statement(statement)
st_mongo = statement.to_mongo
st_mongo[:ct] ||= :default # Indicate statement is in the default graph
#puts "insert statement: #{st_mongo.inspect}"
@coll.update(st_mongo, st_mongo, upsert: true)
@collection.update_one(st_mongo, st_mongo, upsert: true)
end

# @see RDF::Mutable#delete_statement
def delete_statement(statement)
case statement.graph_name
when nil
@coll.remove(statement.to_mongo.merge('ct'=>:default))
@collection.delete_one(statement.to_mongo.merge('ct'=>:default))
else
@coll.remove(statement.to_mongo)
@collection.delete_one(statement.to_mongo)
end
end

Expand All @@ -180,35 +203,33 @@ def durable?; true; end
##
# @private
# @see RDF::Countable#empty?
def empty?; @coll.count == 0; end
def empty?; @collection.count == 0; end

##
# @private
# @see RDF::Countable#count
def count
@coll.count
@collection.count
end

def clear_statements
@coll.remove
@collection.delete_many
end

##
# @private
# @see RDF::Enumerable#has_statement?
def has_statement?(statement)
!!@coll.find_one(statement.to_mongo)
@collection.find(statement.to_mongo).count > 0
end
##
# @private
# @see RDF::Enumerable#each_statement
def each_statement(&block)
@nodes = {} # reset cache. FIXME this should probably be in Node.intern
if block_given?
@coll.find() do |cursor|
cursor.each do |data|
block.call(RDF::Statement.from_mongo(data))
end
@collection.find().each do |document|
block.call(RDF::Statement.from_mongo(document))
end
end
enum_statement
Expand All @@ -219,7 +240,7 @@ def each_statement(&block)
# @private
# @see RDF::Enumerable#has_graph?
def has_graph?(value)
!!@coll.find_one(RDF::Mongo::Conversion.to_mongo(value, :context))
@collection.find(RDF::Mongo::Conversion.to_mongo(value, :graph_name)).count > 0
end

protected
Expand All @@ -228,17 +249,16 @@ def has_graph?(value)
# @private
# @see RDF::Queryable#query_pattern
# @see RDF::Query::Pattern
def query_pattern(pattern, &block)
def query_pattern(pattern, options = {}, &block)
return enum_for(:query_pattern, pattern, options) unless block_given?
@nodes = {} # reset cache. FIXME this should probably be in Node.intern

# A pattern graph_name of `false` is used to indicate the default graph
pm = pattern.to_mongo
pm.merge!(c: nil, ct: :default) if pattern.graph_name == false
#puts "query using #{pm.inspect}"
@coll.find(pm) do |cursor|
cursor.each do |data|
block.call(RDF::Statement.from_mongo(data))
end
@collection.find(pm).each do |document|
block.call(RDF::Statement.from_mongo(document))
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/rdf/mongo/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ def self.to_str() STRING end

##
# @return [Array(Integer, Integer, Integer)]
def self.to_a() [MAJOR, MINOR, TINY] end
def self.to_a() STRING.split(".") end
end
end
8 changes: 4 additions & 4 deletions rdf-mongo.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ Gem::Specification.new do |gem|
gem.test_files = Dir.glob('spec/*.spec')
gem.has_rdoc = false

gem.required_ruby_version = '>= 1.9.3'
gem.required_ruby_version = '>= 2.0'
gem.requirements = []
gem.add_runtime_dependency 'rdf', '~> 1.99'
gem.add_runtime_dependency 'mongo', '~> 1.10'
gem.add_runtime_dependency 'rdf', '>= 2.0.0.beta', '< 3'
gem.add_runtime_dependency 'mongo', '~> 2.2'

gem.add_development_dependency 'rdf-spec', '~> 1.99'
gem.add_development_dependency 'rdf-spec', '>= 2.0.0.beta', '< 3'
gem.add_development_dependency 'rspec', '~> 3.0'
gem.add_development_dependency 'rspec-its', '~> 1.0'
gem.add_development_dependency 'yard' , '~> 0.8'
Expand Down
11 changes: 7 additions & 4 deletions spec/mongo_repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@

describe RDF::Mongo::Repository do
before :each do
@repository = RDF::Mongo::Repository.new() # TODO: Do you need constructor arguments?
@repository.coll.drop
@logger = RDF::Spec.logger
@load_durable = lambda {RDF::Mongo::Repository.new uri: "mongodb://localhost:27017/rdf-mongo/specs", logger: @logger}
@repository = @load_durable.call
@repository.collection.drop
end

after :each do
@repository.coll.drop
after :each do |example|
#puts @logger.to_s if example.exception
@repository.collection.drop
end

# @see lib/rdf/spec/repository.rb in RDF-spec
Expand Down

0 comments on commit 6b1fb84

Please sign in to comment.