solrj_wrapper 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,25 @@
1
+ ## PROJECT
2
+ .bundle
3
+ rdoc
4
+ doc
5
+ .yardoc
6
+ *.lock
7
+ solrmarc.log*
8
+
9
+ ## vim
10
+ *.swp
11
+
12
+ ## rcov
13
+ coverage.data
14
+ coverage
15
+
16
+ ## MAC OS
17
+ .DS_Store
18
+
19
+ ## TextMate
20
+ *.tmproj
21
+ tmtags
22
+
23
+ ## Rubymine
24
+ .idea/*
25
+
data/.gitmodules ADDED
@@ -0,0 +1,3 @@
1
+ [submodule "solrmarc"]
2
+ path = solrmarc
3
+ url = git://github.com/solrmarc/stanford-solr-marc.git
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use jruby-1.6.7@solrj_wrapper --create
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source :rubygems
2
+
3
+ # See solrj_wrapper.gemspec for this gem's dependencies
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Naomi Dushay
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,40 @@
1
+ = SolrjWrapper
2
+
3
+ Ruby wrapper for interacting with Solrj objects, such as org.apache.solr.client.solrj.impl.StreamingUpdateSolrServer.
4
+
5
+ == Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'solrj_wrapper'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install solrj_wrapper
18
+
19
+ == Usage
20
+
21
+ This gem must be run under JRuby, and also requires a directory containing Solrj jar files and solr url (see config/settings.yml)
22
+
23
+ Here are some examples of use cribbed from the specs:
24
+
25
+ sjw = SolrjWrapper.new(solrj_jar_dir, solr_url, solrj_queue_size, solrj_num_threads)
26
+ sjw.get_query_result_docs(q).should be_an_instance_of(Java::OrgApacheSolrCommon::SolrDocumentList)
27
+ sjw.streaming_update_server.should be_an_instance_of(Java::OrgApacheSolrClientSolrjImpl::StreamingUpdateSolrServer)
28
+ sid = Java::OrgApacheSolrCommon::SolrInputDocument.new
29
+ sjw.add_vals_to_fld(sid, "single", ["val"])
30
+
31
+
32
+ == Contributing
33
+
34
+ See also the wiki page for committers: https://github.com/sul-dlss/solrj_wrapper/wiki/For-Committers
35
+
36
+ 1. Fork it
37
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
38
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
39
+ 4. Push to the branch (`git push origin my-new-feature`)
40
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env rake
2
+ require 'bundler/setup'
3
+ require "bundler/gem_tasks"
4
+
5
+ require 'rake'
6
+ require 'bundler'
7
+
8
+ begin
9
+ Bundler.setup(:default, :development)
10
+ rescue Bundler::BundlerError => e
11
+ $stderr.puts e.message
12
+ $stderr.puts "Run `bundle install` to install missing gems"
13
+ exit e.status_code
14
+ end
15
+
16
+ Dir.glob('lib/tasks/*.rake').each { |r| import r }
17
+
18
+ namespace :sj_wrap do
19
+ task :default => :ci
20
+ task :rspec => :rspec_plain
21
+ task :spec => :rspec_plain
22
+ end
@@ -0,0 +1,23 @@
1
+ # settings for production
2
+ prod:
3
+ solr_url: your_production_solr_url
4
+ solrj_jar_dir: your_solrj_jar_dir
5
+ solrj_queue_size: 20
6
+ solrj_num_threads: 1
7
+ log_level: error
8
+
9
+ # settings for local development
10
+ dev:
11
+ solr_url: http://127.0.0.1:8985/solr/dev
12
+ solrj_jar_dir: solrmarc/dist/lib
13
+ solrj_queue_size: 20
14
+ solrj_num_threads: 1
15
+ log_level: info
16
+
17
+ # settings for continuous integration
18
+ ci:
19
+ solr_url: http://127.0.0.1:8985/solr/test
20
+ solrj_jar_dir: solrmarc/dist/lib
21
+ solrj_queue_size: 20
22
+ solrj_num_threads: 1
23
+ log_level: info
@@ -0,0 +1,45 @@
1
+ require 'yaml'
2
+
3
+ # Read the .yml file containing the configuration values
4
+ class Settings
5
+
6
+ attr_reader :solr_url, :solrj_jar_dir, :solrj_queue_size, :solrj_num_threads, :log_level, :log_file
7
+
8
+ def initialize(settings_group)
9
+ yml = YAML.load_file('lib/config/settings.yml')[settings_group]
10
+ @solr_url = yml["solr_url"]
11
+ @solrj_jar_dir = yml["solrj_jar_dir"]
12
+ @solrj_queue_size = yml["solrj_queue_size"]
13
+ @solrj_num_threads = yml["solrj_num_threads"]
14
+ @log_level = yml["log_level"]
15
+ @log_file = yml["log_file"]
16
+ end
17
+
18
+ # @return the attributes of this class as a Hash
19
+ def as_hash
20
+ {:solr_url => @solr_url,
21
+ :solrj_jar_dir => @solrj_jar_dir,
22
+ :solrj_queue_size => @solrj_queue_size,
23
+ :solrj_num_threads => @solrj_num_threads,
24
+ :log_level => get_log_level,
25
+ :log_file => @log_file
26
+ }
27
+ end
28
+
29
+ def get_log_level
30
+ case (@log_level)
31
+ when "debug"
32
+ logger_level = Logger::DEBUG
33
+ when "warn"
34
+ logger_level = Logger::WARN
35
+ when "error"
36
+ logger_level = Logger::ERROR
37
+ when "fatal"
38
+ logger_level = Logger::FATAL
39
+ else
40
+ logger_level = Logger::INFO
41
+ end
42
+ logger_level
43
+ end
44
+
45
+ end
@@ -0,0 +1,3 @@
1
+ class SolrjWrapper
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,124 @@
1
+ include Java
2
+
3
+ require "solrj_wrapper/version"
4
+ require 'logger'
5
+
6
+ # Methods required to interact with SolrJ objects, such as org.apache.solr.client.solrj.impl.StreamingUpdateSolrServer
7
+ class SolrjWrapper
8
+
9
+ attr_reader :streaming_update_server, :query_server
10
+ attr_accessor :query
11
+
12
+ # @param solrj_jar_dir the location of Solrj jars needed to use SolrJ here
13
+ # @param solr_url base url of the solr instance
14
+ # @param queue_size the number of Solr documents to buffer before writing to Solr
15
+ # @param num_threads the number of threads to use when writing to Solr (should not be more than the number of cpu cores avail)
16
+ # @param log_level level of Logger messages to output; defaults to Logger::INFO
17
+ # @param log_file file to receive Logger output; defaults to STDERR
18
+ def initialize(solrj_jar_dir, solr_url, queue_size, num_threads, log_level=Logger::INFO, log_file=STDERR)
19
+ if not defined? JRUBY_VERSION
20
+ raise "SolrjWrapper only runs under jruby"
21
+ end
22
+ @logger = Logger.new(log_file)
23
+ @logger.level = log_level
24
+ load_solrj(solrj_jar_dir)
25
+ @query_server = org.apache.solr.client.solrj.impl.CommonsHttpSolrServer.new(solr_url)
26
+ @streaming_update_server = org.apache.solr.client.solrj.impl.StreamingUpdateSolrServer.new(solr_url, queue_size, num_threads)
27
+ useJavabin!
28
+ end
29
+
30
+ # send the query to Solr and get the SolrDocumentList from the response
31
+ # @param org.apache.solr.client.solrj.SolrQuery object populated with query information to send to Solr
32
+ # @return Java::OrgApacheSolrCommon::SolrDocumentList per the query. The list size will be the number of rows in the Solr response
33
+ def get_query_result_docs(query_obj)
34
+ response = @query_server.query(query_obj)
35
+ response.getResults
36
+ end
37
+
38
+ # Send requests using the Javabin binary format instead of serializing to XML
39
+ # Requires /update/javabin to be defined in solrconfig.xml as
40
+ # <requestHandler name="/update/javabin" class="solr.BinaryUpdateRequestHandler" />
41
+ def useJavabin!
42
+ @streaming_update_server.setRequestWriter Java::org.apache.solr.client.solrj.impl.BinaryRequestWriter.new
43
+ end
44
+
45
+ # given a SolrInputDocument, add the field and/or the values. This will not add empty values, and it will not add duplicate values
46
+ # @param solr_input_doc - the SolrInputDocument object receiving a new field value
47
+ # @param fld_name - the name of the Solr field
48
+ # @param val_array - an array of values for the Solr field
49
+ def add_vals_to_fld(solr_input_doc, fld_name, val_array)
50
+ unless val_array.nil? || solr_input_doc.nil? || fld_name.nil?
51
+ val_array.each { |value|
52
+ add_val_to_fld(solr_input_doc, fld_name, value)
53
+ }
54
+ end
55
+ end
56
+
57
+ # given a SolrInputDocument, add the field and/or the value. This will not add empty values, and it will not add duplicate values
58
+ # @param solr_input_doc - the SolrInputDocument object receiving a new field value
59
+ # @param fld_name - the name of the Solr field
60
+ # @param value - the value to add to the Solr field
61
+ def add_val_to_fld(solr_input_doc, fld_name, value)
62
+ if !solr_input_doc.nil? && !fld_name.nil? && fld_name.size > 0 && !value.nil? && value.size > 0
63
+ if !solr_input_doc[fld_name].nil? && solr_input_doc
64
+ existing_vals = solr_input_doc[fld_name].getValues
65
+ end
66
+ if existing_vals.nil? || !existing_vals.contains(value)
67
+ solr_input_doc.addField(fld_name, value, 1.0)
68
+ end
69
+ end
70
+ end
71
+
72
+ # given a SolrInputDocument, replace all the values of the field with the new values.
73
+ # If the values to be added are an empty array, the field will be removed.
74
+ # If the field doesn't exist in the document, then it will be created (if the value array isn't empty)
75
+ # @param solr_input_doc - the SolrInputDocument object receiving a new field value
76
+ # @param fld_name - the name of the Solr field
77
+ # @param value - an array of values for the Solr field
78
+ def replace_field_values(solr_input_doc, fld_name, val_array)
79
+ solr_input_doc.removeField(fld_name)
80
+ add_vals_to_fld(solr_input_doc, fld_name, val_array)
81
+ end
82
+
83
+ # add the doc to Solr by calling add on the Solrj StreamingUpdateServer object
84
+ # @param solr_input_doc - the SolrInputDocument to be added to the Solr index
85
+ # @param id - the id of the Solr document, used for log messages
86
+ def add_doc_to_ix(solr_input_doc, id)
87
+ unless solr_input_doc.nil?
88
+ begin
89
+ @streaming_update_server.add(solr_input_doc)
90
+ @logger.info("updating Solr document #{id}")
91
+ rescue org.apache.solr.common.SolrException => e
92
+ @logger.error("SolrException while indexing document #{id}")
93
+ @logger.error("#{e.message}")
94
+ @logger.error("#{e.backtrace}")
95
+ end
96
+ end
97
+ end
98
+
99
+ # send a commit to the Solrj StreamingUpdateServer object
100
+ def commit
101
+ begin
102
+ update_response = @streaming_update_server.commit
103
+ rescue org.apache.solr.common.SolrException => e
104
+ @logger.error("SolrException while committing updates")
105
+ @logger.error("#{e.message}")
106
+ @logger.error("#{e.backtrace}")
107
+ end
108
+ end
109
+
110
+ # remove all docs from the Solr index. Assumes default request handler has type dismax
111
+ def empty_ix
112
+ delete_response = @streaming_update_server.deleteByQuery("*:*")
113
+ commit
114
+ end
115
+
116
+
117
+ protected
118
+
119
+ # require all the necessary jars to use Solrj classes
120
+ def load_solrj(solrj_jar_dir)
121
+ Dir["#{solrj_jar_dir}/*.jar"].each {|jar_file| require jar_file }
122
+ end
123
+
124
+ end
data/lib/tasks/ci.rake ADDED
@@ -0,0 +1,9 @@
1
+ namespace :sj_wrap do
2
+
3
+ desc "run continuous integration suite (tests, coverage, docs)"
4
+ task :ci do
5
+ Rake::Task["sj_wrap:rspec_wrapped"].invoke
6
+ Rake::Task["sj_wrap:doc"].invoke
7
+ end
8
+
9
+ end
@@ -0,0 +1,23 @@
1
+ require 'yard'
2
+ require 'yard/rake/yardoc_task'
3
+
4
+ namespace :sj_wrap do
5
+
6
+ # Use yard to build docs
7
+ begin
8
+ project_root = File.expand_path(File.dirname(__FILE__) + "/../..")
9
+ doc_dest_dir = File.join(project_root, 'doc')
10
+
11
+ YARD::Rake::YardocTask.new(:doc) do |yt|
12
+ yt.files = Dir.glob(File.join(project_root, 'lib', '**', '*.rb')) +
13
+ [ File.join(project_root, 'README.rdoc') ]
14
+ yt.options = ['--output-dir', doc_dest_dir, '--readme', 'README.rdoc', '--title', 'SolrJ_Wrapper Documentation']
15
+ end
16
+ rescue LoadError
17
+ desc "Generate YARD Documentation"
18
+ task :doc do
19
+ abort "Please install the YARD gem to generate rdoc."
20
+ end
21
+ end
22
+
23
+ end
@@ -0,0 +1,38 @@
1
+ require 'jettywrapper'
2
+
3
+ namespace :sj_wrap do
4
+ namespace :jetty do
5
+
6
+ desc "Modify test jetty solr to be multicore"
7
+ task :config => ['sj_wrap:solrmarc:setup_test_jetty'] do
8
+
9
+ # copy the solr.xml file for multi-core to test jetty solr
10
+ jetty_solr_dir = "solrmarc/test/jetty/solr"
11
+ cp('spec/solr/solr.xml', jetty_solr_dir, :verbose => true)
12
+
13
+ # set up solr dev and test conf directories
14
+ dev_conf_dir = "#{jetty_solr_dir}/dev/conf"
15
+ test_conf_dir = "#{jetty_solr_dir}/test/conf"
16
+ mkdir_p(dev_conf_dir) unless Dir.exists?(dev_conf_dir)
17
+ mkdir_p(test_conf_dir) unless Dir.exists?(test_conf_dir)
18
+ single_core_conf_dir = "#{jetty_solr_dir}/conf"
19
+ Dir["#{single_core_conf_dir}/*"].each { |f|
20
+ cp_r(f, dev_conf_dir, :verbose => true)
21
+ cp_r(f, test_conf_dir, :verbose => true)
22
+ }
23
+
24
+ require 'fileutils'
25
+ # remove single core conf directory
26
+ FileUtils.rm_rf("#{jetty_solr_dir}/conf/.", :verbose => true)
27
+ Dir.foreach(jetty_solr_dir + "/conf") { |f|
28
+ fn = File.join(jetty_solr_dir, f)
29
+ File.delete(fn) if f != '.' && f != '..'
30
+ }
31
+
32
+ end
33
+
34
+ desc "Copies the SOLR config files and starts up the test-jetty instance"
35
+ task :load => [:config, 'jetty:start']
36
+
37
+ end
38
+ end
@@ -0,0 +1,17 @@
1
+ namespace :sj_wrap do
2
+ namespace :solrmarc do
3
+ require 'ant'
4
+
5
+ desc "run ant dist_site target in solrmarc"
6
+ task :ant_dist_site do
7
+ ant '-f solrmarc/build.xml dist_site'
8
+ end
9
+
10
+ desc "run ant target to set up testing jetty per straight solrmarc"
11
+ task :setup_test_jetty do
12
+ ant '-f solrmarc/build.xml test_clean_site'
13
+ ant '-f solrmarc/build.xml site_setup_test_jetty'
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,26 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ namespace :sj_wrap do
4
+
5
+ desc "Run all specs, with jetty instance running"
6
+ task :rspec_wrapped => ['sj_wrap:solrmarc:ant_dist_site', 'sj_wrap:jetty:config'] do
7
+ test_jetty_dir = File.expand_path(File.dirname(__FILE__) + '../../../solrmarc/test/jetty')
8
+ require 'jettywrapper'
9
+ jetty_params = Jettywrapper.load_config.merge({
10
+ :jetty_home => test_jetty_dir,
11
+ :solr_home => test_jetty_dir + '/solr',
12
+ :java_opts => "-Dsolr.data.dir=" + test_jetty_dir + "/solr/test/data",
13
+ :jetty_port => 8985,
14
+ :startup_wait => 25
15
+ })
16
+ error = Jettywrapper.wrap(jetty_params) do
17
+ Rake::Task['sj_wrap:rspec_plain'].invoke
18
+ end
19
+ raise "TEST FAILURES: #{error}" if error
20
+ end
21
+
22
+ RSpec::Core::RakeTask.new(:rspec_plain) do |spec|
23
+ spec.rspec_opts = ["-c", "-f progress", "-r ./spec/spec_helper.rb"]
24
+ end
25
+
26
+ end
@@ -0,0 +1,32 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.join(File.dirname(__FILE__), 'lib/solrj_wrapper/version')
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.name = "solrj_wrapper"
6
+ gem.version = SolrjWrapper::VERSION
7
+ gem.authors = ["Naomi Dushay"]
8
+ gem.email = ["ndushay@stanford.edu"]
9
+ gem.summary = "Ruby wrapper for interacting with Solrj objects"
10
+ gem.description = "Ruby wrapper for interacting with Solrj objects, such as org.apache.solr.client.solrj.impl.StreamingUpdateSolrServer"
11
+ gem.summary = "This gem must be run under JRuby, and also requires a directory containing SolrJ jars and solr url (see config/settings.yml)"
12
+
13
+ gem.files = `git ls-files`.split($\)
14
+ # gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
15
+ gem.test_files = gem.files.grep(%r{^(spec|features)/})
16
+ gem.require_paths = ["lib"]
17
+
18
+ # No Runtime dependencies
19
+
20
+ # Bundler will install these gems too if you've checked out solrjwrapper source from git and run 'bundle install'
21
+ # It will not add these as dependencies if you require solrj_wrapper for other projects
22
+ gem.add_development_dependency "rake"
23
+ # docs
24
+ gem.add_development_dependency "rdoc"
25
+ gem.add_development_dependency "yard"
26
+ # tests
27
+ gem.add_development_dependency 'rspec'
28
+ gem.add_development_dependency 'simplecov'
29
+ gem.add_development_dependency 'simplecov-rcov'
30
+ gem.add_development_dependency "jettywrapper"
31
+
32
+ end
@@ -0,0 +1,12 @@
1
+ <?xml version="1.0" encoding="UTF-8" ?>
2
+ <!--
3
+ All (relative) paths are relative to the installation path
4
+ persistent: Save changes made via the API to this file
5
+ sharedLib: path to a lib directory that will be shared across all cores
6
+ -->
7
+ <solr persistent="false" sharedLib="lib">
8
+ <cores adminPath="/admin/cores" shareSchema="true">
9
+ <core name="dev" instanceDir="dev" />
10
+ <core name="test" instanceDir="test" />
11
+ </cores>
12
+ </solr>
@@ -0,0 +1,188 @@
1
+ require File.expand_path('../spec_helper', __FILE__)
2
+ require 'solrj_wrapper'
3
+
4
+ describe SolrjWrapper do
5
+
6
+ before(:all) do
7
+ @solrj_wrapper = SolrjWrapper.new(@@settings.solrj_jar_dir, @@settings.solr_url, @@settings.solrj_queue_size, @@settings.solrj_num_threads, @@settings.log_level, @@settings.log_file)
8
+ end
9
+
10
+ it "should initialize a query_server object" do
11
+ @solrj_wrapper.query_server.should be_an_instance_of(Java::OrgApacheSolrClientSolrjImpl::CommonsHttpSolrServer)
12
+ end
13
+
14
+ context "get_query_result_docs" do
15
+ it "should return a SolrDocumentList object" do
16
+ q = org.apache.solr.client.solrj.SolrQuery.new
17
+ @solrj_wrapper.get_query_result_docs(q).should be_an_instance_of(Java::OrgApacheSolrCommon::SolrDocumentList)
18
+ end
19
+
20
+ it "should return an object of size 0 when there are no hits" do
21
+ q = org.apache.solr.client.solrj.SolrQuery.new
22
+ q.setQuery("zzzzzznohitszzzzzzzz")
23
+ @solrj_wrapper.get_query_result_docs(q).size.should == 0
24
+ end
25
+
26
+ it "should return an object of size 0 when rows = 0" do
27
+ q = org.apache.solr.client.solrj.SolrQuery.new
28
+ q.setRows(0)
29
+ @solrj_wrapper.get_query_result_docs(q).size.should == 0
30
+ end
31
+
32
+ it "should return an object of size > 1 when there are hits and rows is > 0" do
33
+ sid = Java::OrgApacheSolrCommon::SolrInputDocument.new
34
+ @solrj_wrapper.add_val_to_fld(sid, "id", "test_rec")
35
+ @solrj_wrapper.add_doc_to_ix(sid, "test_rec")
36
+ @solrj_wrapper.commit
37
+ q = org.apache.solr.client.solrj.SolrQuery.new
38
+ @solrj_wrapper.get_query_result_docs(q).size.should_not == 0
39
+ @solrj_wrapper.empty_ix
40
+ end
41
+ end
42
+
43
+ it "should initialize a streaming_update_server object" do
44
+ @solrj_wrapper.streaming_update_server.should be_an_instance_of(Java::OrgApacheSolrClientSolrjImpl::StreamingUpdateSolrServer)
45
+ end
46
+
47
+ context "add_vals_to_fld" do
48
+ it "should do nothing if the field name or value is nil or of size 0" do
49
+ sid = Java::OrgApacheSolrCommon::SolrInputDocument.new
50
+ @solrj_wrapper.add_vals_to_fld(sid, nil, ["val"])
51
+ sid.isEmpty.should be_true
52
+ @solrj_wrapper.add_vals_to_fld(sid, "", ["val"])
53
+ sid.isEmpty.should be_true
54
+ @solrj_wrapper.add_vals_to_fld(sid, "fldname", nil)
55
+ sid.isEmpty.should be_true
56
+ @solrj_wrapper.add_vals_to_fld(sid, "fldname", [])
57
+ sid.isEmpty.should be_true
58
+ end
59
+
60
+ it "should create a new field when none exists" do
61
+ sid = Java::OrgApacheSolrCommon::SolrInputDocument.new
62
+ @solrj_wrapper.add_vals_to_fld(sid, "single", ["val"])
63
+ vals = sid["single"].getValues
64
+ vals.size.should == 1
65
+ vals[0].should == "val"
66
+ @solrj_wrapper.add_vals_to_fld(sid, "mult", ["val1", "val2"])
67
+ vals = sid["mult"].getValues
68
+ vals.size.should == 2
69
+ vals[0].should == "val1"
70
+ vals[1].should == "val2"
71
+ end
72
+
73
+ it "should keep the existing values when it adds a value to a field" do
74
+ sid = Java::OrgApacheSolrCommon::SolrInputDocument.new
75
+ @solrj_wrapper.add_vals_to_fld(sid, "fld", ["val"])
76
+ vals = sid["fld"].getValues
77
+ vals.size.should == 1
78
+ vals[0].should == "val"
79
+ @solrj_wrapper.add_vals_to_fld(sid, "fld", ["val1", "val2"])
80
+ vals = sid["fld"].getValues
81
+ vals.size.should == 3
82
+ vals.contains("val").should_not be_nil
83
+ vals.contains("val1").should_not be_nil
84
+ vals.contains("val2").should_not be_nil
85
+ end
86
+
87
+ it "should add all values, except those already present" do
88
+ sid = Java::OrgApacheSolrCommon::SolrInputDocument.new
89
+ @solrj_wrapper.add_vals_to_fld(sid, "fld", ["val"])
90
+ vals = sid["fld"].getValues
91
+ vals.size.should == 1
92
+ vals[0].should == "val"
93
+ @solrj_wrapper.add_vals_to_fld(sid, "fld", ["val1", "val2", "val"])
94
+ vals = sid["fld"].getValues
95
+ vals.size.should == 3
96
+ vals.contains("val").should_not be_nil
97
+ vals.contains("val1").should_not be_nil
98
+ vals.contains("val2").should_not be_nil
99
+ end
100
+ end # context add_vals_to_fld
101
+
102
+ context "add_val_to_fld" do
103
+ it "should do nothing if the field name or value is nil or of size 0" do
104
+ sid = Java::OrgApacheSolrCommon::SolrInputDocument.new
105
+ @solrj_wrapper.add_val_to_fld(sid, nil, "val")
106
+ sid.isEmpty.should be_true
107
+ @solrj_wrapper.add_val_to_fld(sid, "", "val")
108
+ sid.isEmpty.should be_true
109
+ @solrj_wrapper.add_val_to_fld(sid, "fldname", nil)
110
+ sid.isEmpty.should be_true
111
+ @solrj_wrapper.add_val_to_fld(sid, "fldname", [])
112
+ sid.isEmpty.should be_true
113
+ end
114
+
115
+ it "should create a new field when none exists" do
116
+ sid = Java::OrgApacheSolrCommon::SolrInputDocument.new
117
+ @solrj_wrapper.add_val_to_fld(sid, "single", "val")
118
+ vals = sid["single"].getValues
119
+ vals.size.should == 1
120
+ vals[0].should == "val"
121
+ end
122
+
123
+ it "should keep the existing values when it adds a value to a field" do
124
+ sid = Java::OrgApacheSolrCommon::SolrInputDocument.new
125
+ @solrj_wrapper.add_vals_to_fld(sid, "fld", ["val1", "val2"])
126
+ @solrj_wrapper.add_val_to_fld(sid, "fld", "val")
127
+ vals = sid["fld"].getValues
128
+ vals.size.should == 3
129
+ vals.contains("val").should_not be_nil
130
+ vals.contains("val1").should_not be_nil
131
+ vals.contains("val2").should_not be_nil
132
+ end
133
+
134
+ it "should add all values, except those already present" do
135
+ sid = Java::OrgApacheSolrCommon::SolrInputDocument.new
136
+ @solrj_wrapper.add_vals_to_fld(sid, "fld", ["val1", "val2", "val"])
137
+ @solrj_wrapper.add_val_to_fld(sid, "fld", "val")
138
+ vals = sid["fld"].getValues
139
+ vals.size.should == 3
140
+ vals.contains("val").should_not be_nil
141
+ vals.contains("val1").should_not be_nil
142
+ vals.contains("val2").should_not be_nil
143
+ end
144
+ end # context add_vals_to_fld
145
+
146
+ context "replace_field_values" do
147
+ it "should work for disjoint sets of field values" do
148
+ sid = Java::OrgApacheSolrCommon::SolrInputDocument.new
149
+ @solrj_wrapper.add_vals_to_fld(sid, "fld", ["val1", "val2", "val3"])
150
+ @solrj_wrapper.replace_field_values(sid, "fld", ["val4", "val5"])
151
+ vals = sid["fld"].getValues
152
+ vals.size.should == 2
153
+ vals.contains("val1").should be_false
154
+ vals.contains("val2").should be_false
155
+ vals.contains("val3").should be_false
156
+ vals.contains("val4").should be_true
157
+ vals.contains("val5").should be_true
158
+ end
159
+
160
+ it "should retain unchanged values" do
161
+ sid = Java::OrgApacheSolrCommon::SolrInputDocument.new
162
+ @solrj_wrapper.add_vals_to_fld(sid, "fld", ["val1", "val2"])
163
+ @solrj_wrapper.replace_field_values(sid, "fld", ["val2", "val3"])
164
+ vals = sid["fld"].getValues
165
+ vals.size.should == 2
166
+ vals.contains("val1").should be_false
167
+ vals.contains("val2").should be_true
168
+ vals.contains("val3").should be_true
169
+ end
170
+
171
+ it "should create a field when none existed before" do
172
+ sid = Java::OrgApacheSolrCommon::SolrInputDocument.new
173
+ sid["fld"].should be_nil
174
+ @solrj_wrapper.replace_field_values(sid, "fld", ["val1"])
175
+ vals = sid["fld"].getValues
176
+ vals.size.should == 1
177
+ vals.contains("val1").should be_true
178
+ end
179
+
180
+ it "should remove a field if there are no values to add" do
181
+ sid = Java::OrgApacheSolrCommon::SolrInputDocument.new
182
+ @solrj_wrapper.add_vals_to_fld(sid, "fld", ["val1", "val2"])
183
+ @solrj_wrapper.replace_field_values(sid, "fld", [])
184
+ sid["fld"].should be_nil
185
+ end
186
+ end
187
+
188
+ end
@@ -0,0 +1,24 @@
1
+ # for test coverage
2
+ require 'simplecov'
3
+ require 'simplecov-rcov'
4
+ class SimpleCov::Formatter::MergedFormatter
5
+ def format(result)
6
+ SimpleCov::Formatter::HTMLFormatter.new.format(result)
7
+ SimpleCov::Formatter::RcovFormatter.new.format(result)
8
+ end
9
+ end
10
+ SimpleCov.formatter = SimpleCov::Formatter::MergedFormatter
11
+ SimpleCov.start do
12
+ add_filter "/spec/"
13
+ add_filter "/lib/config/"
14
+ end
15
+
16
+ require 'solrj_wrapper/settings'
17
+
18
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
19
+
20
+ RSpec.configure do |config|
21
+ # Set up the environment for testing and make all variables available to the specs
22
+ settings_env = ENV["SETTINGS"] ||= 'dev'
23
+ @@settings = Settings.new(settings_env)
24
+ end
metadata ADDED
@@ -0,0 +1,153 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: solrj_wrapper
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Naomi Dushay
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-18 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ version_requirements: &3808 !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ! '>='
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ none: false
22
+ requirement: *3808
23
+ prerelease: false
24
+ type: :development
25
+ - !ruby/object:Gem::Dependency
26
+ name: rdoc
27
+ version_requirements: &3826 !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ! '>='
30
+ - !ruby/object:Gem::Version
31
+ version: '0'
32
+ none: false
33
+ requirement: *3826
34
+ prerelease: false
35
+ type: :development
36
+ - !ruby/object:Gem::Dependency
37
+ name: yard
38
+ version_requirements: &3842 !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ none: false
44
+ requirement: *3842
45
+ prerelease: false
46
+ type: :development
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ version_requirements: &3858 !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ none: false
55
+ requirement: *3858
56
+ prerelease: false
57
+ type: :development
58
+ - !ruby/object:Gem::Dependency
59
+ name: simplecov
60
+ version_requirements: &3874 !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ none: false
66
+ requirement: *3874
67
+ prerelease: false
68
+ type: :development
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov-rcov
71
+ version_requirements: &3890 !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ none: false
77
+ requirement: *3890
78
+ prerelease: false
79
+ type: :development
80
+ - !ruby/object:Gem::Dependency
81
+ name: jettywrapper
82
+ version_requirements: &3906 !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ none: false
88
+ requirement: *3906
89
+ prerelease: false
90
+ type: :development
91
+ description: Ruby wrapper for interacting with Solrj objects, such as org.apache.solr.client.solrj.impl.StreamingUpdateSolrServer
92
+ email:
93
+ - ndushay@stanford.edu
94
+ executables: []
95
+ extensions: []
96
+ extra_rdoc_files: []
97
+ files:
98
+ - .gitignore
99
+ - .gitmodules
100
+ - .rvmrc
101
+ - Gemfile
102
+ - LICENSE
103
+ - README.rdoc
104
+ - Rakefile
105
+ - lib/config/settings.yml
106
+ - lib/solrj_wrapper.rb
107
+ - lib/solrj_wrapper/settings.rb
108
+ - lib/solrj_wrapper/version.rb
109
+ - lib/tasks/ci.rake
110
+ - lib/tasks/doc.rake
111
+ - lib/tasks/jetty.rake
112
+ - lib/tasks/solrmarc.rake
113
+ - lib/tasks/spec.rake
114
+ - solrj_wrapper.gemspec
115
+ - spec/solr/solr.xml
116
+ - spec/solrj_wrapper_spec.rb
117
+ - spec/spec_helper.rb
118
+ homepage:
119
+ licenses: []
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ! '>='
127
+ - !ruby/object:Gem::Version
128
+ segments:
129
+ - 0
130
+ hash: 2
131
+ version: '0'
132
+ none: false
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ! '>='
136
+ - !ruby/object:Gem::Version
137
+ segments:
138
+ - 0
139
+ hash: 2
140
+ version: '0'
141
+ none: false
142
+ requirements: []
143
+ rubyforge_project:
144
+ rubygems_version: 1.8.15
145
+ signing_key:
146
+ specification_version: 3
147
+ summary: This gem must be run under JRuby, and also requires a directory containing SolrJ jars and solr url (see config/settings.yml)
148
+ test_files:
149
+ - spec/solr/solr.xml
150
+ - spec/solrj_wrapper_spec.rb
151
+ - spec/spec_helper.rb
152
+ has_rdoc:
153
+ ...