lucene 0.5.0.beta.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ include Java
2
+
3
+ module Lucene
4
+ require 'lucene/jars/lucene-core-2.9.1.jar'
5
+ end
@@ -0,0 +1,135 @@
1
+ module Lucene
2
+
3
+ class Expression #:nodoc:
4
+ attr_accessor :left, :right, :op, :query
5
+
6
+ def self.new_complete(left, op, right)
7
+ expr = Expression.new
8
+ expr.left = left
9
+ expr.op = op
10
+ expr.right = right
11
+ expr
12
+ end
13
+
14
+ def self.new_uncomplete(left, query)
15
+ expr = Expression.new
16
+ expr.left = left
17
+ expr.query = query
18
+ expr
19
+ end
20
+
21
+ def ==(other)
22
+ @op = :==
23
+ @right = other
24
+ @query
25
+ end
26
+
27
+ def >(other)
28
+ @op = :>
29
+ @right = other
30
+ @query
31
+ end
32
+
33
+ #
34
+ # Returns the fields being used in a query
35
+ #
36
+ def _fields(fields = [])
37
+ if (@left.kind_of? Expression)
38
+ @left._fields(fields)
39
+ else
40
+ fields << @left
41
+ end
42
+ if (@right.kind_of? Expression)
43
+ @right._fields(fields)
44
+ end
45
+ fields
46
+ end
47
+
48
+ def to_lucene(field_infos)
49
+ $LUCENE_LOGGER.debug{"QueryDSL.to_lucene '#{to_s}'"}
50
+
51
+ if @left.kind_of? Lucene::Expression
52
+ left_query = @left.to_lucene(field_infos)
53
+ raise ArgumentError.new("Right term is not an Expression, but a '#{@right.class.to_s}'") unless @right.kind_of? Lucene::Expression
54
+ right_query = @right.to_lucene(field_infos)
55
+ query = org.apache.lucene.search.BooleanQuery.new
56
+ clause = (@op == :&) ? org.apache.lucene.search.BooleanClause::Occur::MUST : org.apache.lucene.search.BooleanClause::Occur::SHOULD
57
+ query.add(left_query, clause)
58
+ query.add(right_query, clause)
59
+ return query
60
+ else
61
+ field_info = field_infos[@left]
62
+ field_info.convert_to_query(@left, @right)
63
+ end
64
+ end
65
+
66
+ def to_s
67
+ "(#@left #@op #@right)"
68
+ end
69
+ end
70
+
71
+ class QueryDSL
72
+ attr_reader :stack
73
+
74
+ def initialize
75
+ @stack = []
76
+ #yield self
77
+ end
78
+
79
+ def self.find(field_infos = IndexInfo.new(:id), &expr)
80
+ exp = QueryDSL.parse(&expr)
81
+ exp.to_lucene(field_infos)
82
+ end
83
+
84
+
85
+
86
+ def self.parse(&query)
87
+ query_dsl = QueryDSL.new
88
+ query_dsl.instance_eval(&query)
89
+ query_dsl.stack.last
90
+ end
91
+
92
+ def method_missing(methodname, *args)
93
+ expr = Expression.new_uncomplete(methodname, self)
94
+ @stack.push expr
95
+ expr
96
+ end
97
+
98
+ def ==(other)
99
+ puts "WRONG == '#{other}'"
100
+ end
101
+
102
+ def <=>(to)
103
+ from = @stack.last.right
104
+ @stack.last.right = Range.new(from, to)
105
+ @stack.last
106
+ end
107
+
108
+
109
+ def &(other)
110
+ raise ArgumentError.new("Expected at least two expression on stack, got #{@stack.size}") if @stack.size < 2
111
+ right = @stack.pop
112
+ left = @stack.pop
113
+ expr = Expression.new_complete(left, :&, right)
114
+ @stack.push expr
115
+ self
116
+ end
117
+
118
+ def |(other)
119
+ raise ArgumentError.new("Expected at least two expression on stack, got #{@stack.size}") if @stack.size < 2
120
+ right = @stack.pop
121
+ left = @stack.pop
122
+ expr = Expression.new_complete(left, :|, right)
123
+ @stack.push expr
124
+ self
125
+ end
126
+
127
+ def to_s
128
+ @stack.last.to_s
129
+ end
130
+
131
+ end
132
+
133
+
134
+ end
135
+
@@ -0,0 +1,117 @@
1
+ module Lucene
2
+
3
+ class TransactionAlreadyRunningException < StandardError; end
4
+ class TransactionNotRunningException < StandardError; end
5
+
6
+ class Transaction
7
+
8
+ def initialize
9
+ raise TransactionAlreadyRunningException.new if Transaction.running?
10
+ Thread.current[:lucene_transaction] = self
11
+
12
+ @rollback = false
13
+ @commited = false
14
+ @indexes = {} # key is the path to index, value is the index instance
15
+ end
16
+
17
+ def to_s
18
+ "Transaction [commited=#@commited, rollback=#@rollback, indexes=#{@indexes.size}, object_id=#{object_id}]"
19
+ end
20
+
21
+
22
+ # Commits all registered Indexes.
23
+ # Stops this transaction (running? will be false)
24
+ #
25
+ def commit
26
+ if !@rollback
27
+ @indexes.each_value do |index|
28
+ index.commit
29
+ end
30
+ end
31
+ @commited = true
32
+ @indexes.clear
33
+ Thread.current[:lucene_transaction] = nil
34
+ end
35
+
36
+ def failure
37
+ @rollback = true
38
+ $LUCENE_LOGGER.debug{"Rollback Lucene Transaction"}
39
+ end
40
+
41
+ def rollback?
42
+ @rollback
43
+ end
44
+
45
+ def rollback!
46
+ @rollback = true
47
+ end
48
+
49
+ #
50
+ # Registers an index to take part of this transaction
51
+ #
52
+ def register_index(key, index)
53
+ @indexes[key] = index
54
+ $LUCENE_LOGGER.debug{"Registered index for #{index}"}
55
+ end
56
+
57
+ #
58
+ # Deregister the index so that it will not be part of the transaction
59
+ # any longer.
60
+ #
61
+ def deregister_index(index)
62
+ @indexes.delete index.path
63
+ $LUCENE_LOGGER.debug{"Deregistered index for #{index}"}
64
+ end
65
+
66
+ #
67
+ # Deregister all indexes, used for testing purpose.
68
+ #
69
+ def deregister_all_indexes
70
+ @indexes.clear
71
+ $LUCENE_LOGGER.debug{"Deregistered all index, #{@indexes.inspect}"}
72
+ end
73
+
74
+
75
+ def index?(path)
76
+ @indexes[path] != nil
77
+ end
78
+
79
+ def index(path)
80
+ @indexes[path]
81
+ end
82
+
83
+ #
84
+ # Class methods
85
+ #
86
+ class << self
87
+ def run
88
+ tx = Transaction.new
89
+ begin
90
+ yield tx
91
+ rescue => ex
92
+ tx.failure
93
+ # TODO reuse of error handling and logging
94
+ $LUCENE_LOGGER.error{"Got exception #{ex}"}
95
+ ex.backtrace.each {|t| $LUCENE_LOGGER.error(t)}
96
+ raise
97
+ ensure
98
+ tx.commit unless tx.rollback?
99
+ end
100
+ end
101
+
102
+ #
103
+ # Returns the current transaction or nil
104
+ #
105
+ def current
106
+ Thread.current[:lucene_transaction]
107
+ end
108
+
109
+
110
+ def running?
111
+ self.current != nil
112
+ end
113
+ end
114
+ end
115
+
116
+
117
+ end
@@ -0,0 +1,3 @@
1
+ module Lucene
2
+ VERSION = '0.5.0.beta.1'
3
+ end
@@ -0,0 +1,23 @@
1
+ lib = File.expand_path('../lib/', __FILE__)
2
+ $:.unshift lib unless $:.include?(lib)
3
+
4
+ require 'lucene/version'
5
+
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = "lucene"
9
+ s.version = Lucene::VERSION
10
+ # s.platform = Gem::Platform::CURRENT # will probably support C Ruby via RJB also in the future
11
+ s.authors = "Andreas Ronge"
12
+ s.email = 'andreas.ronge@gmail.com'
13
+ s.homepage = "http://github.com/andreasronge/lucene/tree"
14
+ s.rubyforge_project = 'lucene'
15
+ s.summary = "A lucene wrapper for JRuby"
16
+ s.description = s.summary
17
+ s.require_path = 'lib'
18
+ s.files = Dir.glob("{bin,lib}/**/*") + %w(README.rdoc CHANGELOG CONTRIBUTORS Gemfile lucene.gemspec)
19
+ s.has_rdoc = true
20
+ s.extra_rdoc_files = %w( README.rdoc )
21
+ s.rdoc_options = ["--quiet", "--title", "Lucene.rb", "--opname", "index.html", "--line-numbers", "--main", "README.rdoc", "--inline-source"]
22
+ s.required_ruby_version = ">= 1.8.7"
23
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lucene
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: true
5
+ segments:
6
+ - 0
7
+ - 5
8
+ - 0
9
+ - beta
10
+ - 1
11
+ version: 0.5.0.beta.1
12
+ platform: ruby
13
+ authors:
14
+ - Andreas Ronge
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2010-10-20 00:00:00 +02:00
20
+ default_executable:
21
+ dependencies: []
22
+
23
+ description: A lucene wrapper for JRuby
24
+ email: andreas.ronge@gmail.com
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files:
30
+ - README.rdoc
31
+ files:
32
+ - lib/lucene.rb
33
+ - lib/lucene/document.rb
34
+ - lib/lucene/index_searcher.rb
35
+ - lib/lucene/config.rb
36
+ - lib/lucene/transaction.rb
37
+ - lib/lucene/field_info.rb
38
+ - lib/lucene/index_info.rb
39
+ - lib/lucene/hits.rb
40
+ - lib/lucene/jars.rb
41
+ - lib/lucene/index.rb
42
+ - lib/lucene/query_dsl.rb
43
+ - lib/lucene/version.rb
44
+ - lib/lucene/jars/lucene-core-2.9.1.jar
45
+ - README.rdoc
46
+ - CHANGELOG
47
+ - CONTRIBUTORS
48
+ - Gemfile
49
+ - lucene.gemspec
50
+ has_rdoc: true
51
+ homepage: http://github.com/andreasronge/lucene/tree
52
+ licenses: []
53
+
54
+ post_install_message:
55
+ rdoc_options:
56
+ - --quiet
57
+ - --title
58
+ - Lucene.rb
59
+ - --opname
60
+ - index.html
61
+ - --line-numbers
62
+ - --main
63
+ - README.rdoc
64
+ - --inline-source
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ segments:
72
+ - 1
73
+ - 8
74
+ - 7
75
+ version: 1.8.7
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">"
79
+ - !ruby/object:Gem::Version
80
+ segments:
81
+ - 1
82
+ - 3
83
+ - 1
84
+ version: 1.3.1
85
+ requirements: []
86
+
87
+ rubyforge_project: lucene
88
+ rubygems_version: 1.3.6
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: A lucene wrapper for JRuby
92
+ test_files: []
93
+