/usr/lib/ruby/1.9.1/xapian.rb is in libxapian-ruby1.9.1 1.2.8-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | # :title:Ruby Xapian bindings
# =Ruby Xapian bindings
#
# Original version by Paul Legato (plegato@nks.net), 4/20/06.
#
# Copyright (C) 2006 Networked Knowledge Systems, Inc.
# Copyright (C) 2008,2011 Olly Betts
# Copyright (C) 2010 Richard Boulton
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
# USA
#
# ==Underscore methods
# Note: Methods whose names start with an underscore character _ are internal
# methods from the C++ API. Their functionality is not accessible in a
# Ruby-friendly way, so this file provides wrapper code to make it easier to
# use them from a Ruby programming idiom. Most are also dangerous insofar as
# misusing them can cause your program to segfault. In particular, all of
# Xapian's *Iterator classes are wrapped into nice Ruby-friendly Arrays.
#
# It should never be necessary to use any method whose name starts with an
# underscore from user-level code. Make sure you are _VERY_ certain that you
# know exactly what you're doing if you do use one of these methods. Beware.
# You've been warned...
#
module Xapian
######## load the SWIG-generated library
require '_xapian'
# iterate over two dangerous iterators (i.e. those that can cause segfaults
# if used improperly.)
# Return the results as an Array.
# Users should never need to use this method.
#
# Takes a block that returns some appropriate Ruby object to wrap the
# underlying Iterator
def _safelyIterate(dangerousStart, dangerousEnd) #:nodoc:
retval = Array.new
item = dangerousStart
lastTerm = dangerousEnd
return retval if dangerousStart.equals(dangerousEnd)
begin
retval.push(yield(item))
item.next()
end while not item.equals(lastTerm) # must use primitive C++ comparator
return retval
end # _safelyIterate
module_function :_safelyIterate
#--
### safe Ruby wrapper for the dangerous C++ Xapian::TermIterator class
class Xapian::Term
attr_accessor :term, :wdf, :termfreq
def initialize(term, wdf=nil, termfreq=nil)
@term = term
@wdf = wdf
@termfreq = termfreq
end
def ==(other)
return other.is_a?(Xapian::Term) && other.term == @term && other.wdf == @wdf && other.termfreq == @termfreq
end
end # class Term
### Ruby wrapper for a Match, i.e. a Xapian::MSetIterator (Match Set) in C++.
# it's no longer an iterator in the Ruby version, but we want to preserve its
# non-iterative data.
# (MSetIterator is not dangerous, but it is inconvenient to use from a Ruby
# idiom, so we wrap it..)
class Xapian::Match
attr_accessor :docid, :document, :rank, :weight, :collapse_count, :percent
def initialize(docid, document, rank, weight, collapse_count, percent)
@docid = docid
@document = document
@rank = rank
@weight = weight
@collapse_count = collapse_count
@percent = percent
end # initialize
def ==(other)
return other.is_a?(Xapian::Match) && other.docid == @docid && other.rank == @rank &&
other.weight == @weight && other.collapse_count == @collapse_count && other.percent == @percent
end
end # class Xapian::Match
# Ruby wrapper for an ExpandTerm, i.e. a Xapian::ESetIterator in C++
# Not dangerous, but inconvenient to use from a Ruby programming idiom, so we
# wrap it.
class Xapian::ExpandTerm
attr_accessor :name, :weight
def initialize(name, weight)
@name = name
@weight = weight
end # initialize
def ==(other)
return other.is_a?(Xapian::ExpandTerm) && other.name == @name && other.weight == @weight
end
end # Xapian::ExpandTerm
# Ruby wrapper for Xapian::ValueIterator
class Xapian::Value
attr_accessor :value, :valueno, :docid
def initialize(value, valueno, docid)
@value = value
@valueno = valueno
@docid = docid
end # initialize
def ==(other)
return other.is_a?(Xapian::Value) && other.value == @value && other.valueno == @valueno && other.docid == @docid
end
end # Xapian::Value
# Refer to the
# {Xapian::Document C++ API documentation}[http://xapian.org/docs/apidoc/html/classXapian_1_1Document.html]
# for methods not specific to Ruby.
#--
# Extend Xapian::Document with a nice wrapper for its nasty input_iterators
class Xapian::Document
def terms
Xapian._safelyIterate(self._dangerous_termlist_begin(), self._dangerous_termlist_end()) { |item|
Xapian::Term.new(item.term, item.wdf)
}
end # terms
def values
Xapian._safelyIterate(self._dangerous_values_begin(), self._dangerous_values_end()) { |item|
Xapian::Value.new(item.value, item.valueno, 0)
}
end # values
end # class Xapian::Document
# Refer to the
# {Xapian::Query C++ API documentation}[http://xapian.org/docs/apidoc/html/classXapian_1_1Query.html]
# for methods not specific to Ruby.
#--
# Extend Xapian::Query with a nice wrapper for its dangerous iterators
class Xapian::Query
def terms
Xapian._safelyIterate(self._dangerous_terms_begin(), self._dangerous_terms_end()) { |item|
Xapian::Term.new(item.term, item.wdf)
# termfreq is not supported by TermIterators from Queries
}
end # terms
end # Xapian::Query
# Refer to the
# {Xapian::Enquire C++ API documentation}[http://xapian.org/docs/apidoc/html/classXapian_1_1Enquire.html]
# for methods not specific to Ruby.
#--
# Extend Xapian::Enquire with a nice wrapper for its dangerous iterators
class Xapian::Enquire
# Get matching terms for some document.
# document can be either a Xapian::DocID or a Xapian::MSetIterator
def matching_terms(document)
Xapian._safelyIterate(self._dangerous_matching_terms_begin(document),
self._dangerous_matching_terms_end(document)) { |item|
Xapian::Term.new(item.term, item.wdf)
}
end # matching_terms
end # Xapian::Enquire
# Refer to the
# {Xapian::MSet C++ API documentation}[http://xapian.org/docs/apidoc/html/classXapian_1_1MSet.html]
# for methods not specific to Ruby.
#--
# MSetIterators are not dangerous, just inconvenient to use within a Ruby
# programming idiom. So we wrap them.
class Xapian::MSet
def matches
Xapian._safelyIterate(self._begin(),
self._end()) { |item|
Xapian::Match.new(item.docid, item.document, item.rank, item.weight, item.collapse_count, item.percent)
}
end # matches
end # Xapian::MSet
# Refer to the
# {Xapian::ESet C++ API documentation}[http://xapian.org/docs/apidoc/html/classXapian_1_1ESet.html]
# for methods not specific to Ruby.
#--
# ESetIterators are not dangerous, just inconvenient to use within a Ruby
# programming idiom. So we wrap them.
class Xapian::ESet
def terms
Xapian._safelyIterate(self._begin(),
self._end()) { |item|
# note: in the ExpandTerm wrapper, we implicitly rename
# ESetIterator#term() (defined in xapian.i) to ExpandTerm#term()
Xapian::ExpandTerm.new(item.term, item.weight)
}
end # terms
end # Xapian::ESet
#--
# Wrapper for the C++ class Xapian::PostingIterator
class Xapian::Posting
attr_accessor :docid, :doclength, :wdf
def initialize(docid, doclength, wdf)
@docid = docid
@doclength = doclength
@wdf = wdf
end
def ==(other)
return other.is_a?(Xapian::Posting) && other.docid == @docid && other.doclength == @doclength &&
other.wdf == @wdf
end
end # Xapian::Posting
# Refer to the
# {Xapian::Database C++ API documentation}[http://xapian.org/docs/apidoc/html/classXapian_1_1Database.html]
# for methods not specific to Ruby.
#--
# Wrap some dangerous iterators.
class Xapian::Database
# Returns an Array of all Xapian::Terms for this database.
def allterms(pref = '')
Xapian._safelyIterate(self._dangerous_allterms_begin(pref),
self._dangerous_allterms_end(pref)) { |item|
Xapian::Term.new(item.term, 0, item.termfreq)
}
end # allterms
# Returns an Array of Xapian::Postings for the given term.
# term is a string.
def postlist(term)
Xapian._safelyIterate(self._dangerous_postlist_begin(term),
self._dangerous_postlist_end(term)) { |item|
Xapian::Posting.new(item.docid, item.doclength, item.wdf)
}
end # postlist(term)
# Returns an Array of Terms for the given docid.
def termlist(docid)
Xapian._safelyIterate(self._dangerous_termlist_begin(docid),
self._dangerous_termlist_end(docid)) { |item|
Xapian::Term.new(item.term, item.wdf, item.termfreq)
}
end # termlist(docid)
# Returns an Array of Xapian::Termpos objects for the given term (a String)
# in the given docid.
def positionlist(docid, term)
Xapian._safelyIterate(self._dangerous_positionlist_begin(docid, term),
self._dangerous_positionlist_end(docid, term)) { |item|
item.termpos
}
end # positionlist
# Returns an Array of Xapian::Value objects for the given slot in the
# database.
def valuestream(slot)
Xapian._safelyIterate(self._dangerous_valuestream_begin(slot),
self._dangerous_valuestream_end(slot)) { |item|
Xapian::Value.new(item.value, slot, item.docid)
}
end # valuestream(slot)
end # Xapian::Database
# Refer to the
# {Xapian::ValueCountMatchSpy C++ API documentation}[http://xapian.org/docs/apidoc/html/classXapian_1_1ValueCountMatchSpy.html]
# for methods not specific to Ruby.
#--
# Wrap some dangerous iterators.
class Xapian::ValueCountMatchSpy
# Returns an Array of all the values seen, in alphabetical order
def values()
Xapian._safelyIterate(self._dangerous_values_begin(),
self._dangerous_values_end()) { |item|
Xapian::Term.new(item.term, 0, item.termfreq)
}
end # values
# Returns an Array of the top values seen, by frequency
def top_values(maxvalues)
Xapian._safelyIterate(self._dangerous_top_values_begin(maxvalues),
self._dangerous_top_values_end(maxvalues)) { |item|
Xapian::Term.new(item.term, 0, item.termfreq)
}
end # top_values
end # Xapian::Database
end # Xapian module
|