Class: XapianFu::XapianDocValueAccessor

Inherits:
Object
  • Object
show all
Defined in:
lib/xapian_fu/xapian_doc_value_accessor.rb

Overview

A XapianDocValueAccessor is used to provide the XapianDoc#values interface to read and write field values to a XapianDb. It is usually set up by a XapianDoc so you shouldn’t need to set up your own.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xapian_doc) ⇒ XapianDocValueAccessor

Returns a new instance of XapianDocValueAccessor.



92
93
94
# File 'lib/xapian_fu/xapian_doc_value_accessor.rb', line 92

def initialize(xapian_doc)
  @doc = xapian_doc
end

Class Method Details

.value_key(key) ⇒ Object

Convert the given key to an integer that can be used as a Xapian value number



143
144
145
# File 'lib/xapian_fu/xapian_doc_value_accessor.rb', line 143

def self.value_key(key)
  (key.is_a?(Integer) ? key : Zlib.crc32(key.to_s))
end

Instance Method Details

#delete(key) ⇒ Object

Remove the value with the given key from the XapianDoc and return it



135
136
137
138
139
# File 'lib/xapian_fu/xapian_doc_value_accessor.rb', line 135

def delete(key)
  value = fetch(key)
  @doc.xapian_document.remove_value(XapianDocValueAccessor.value_key(key))
  value
end

#fetch(key, type = nil) ⇒ Object Also known as: []

Retrieve the value with the given key from the XapianDoc. key can be a symbol or string, in which case it’s hashed to get an integer value number. Or you can give the integer value number if you know it.

If the class specified in the database fields for this key (or as the optional argument) has a from_xapian_fu_storage_value method then it is used to instaniate the object from the stored value. This is usually paired with a to_xapian_fu_storage_value instance method.

Due to the design of Xapian, if the value does not exist then an empty string is returned.



123
124
125
126
# File 'lib/xapian_fu/xapian_doc_value_accessor.rb', line 123

def fetch(key, type = nil)
  value = @doc.xapian_document.value(XapianDocValueAccessor.value_key(key))
  @doc.db.unserialize_value(key, value, type)
end

#sizeObject

Count the values stored in the XapianDoc



130
131
132
# File 'lib/xapian_fu/xapian_doc_value_accessor.rb', line 130

def size
  @doc.xapian_document.values_count
end

#store(key, value, type = nil) ⇒ Object Also known as: []=

Add the given value with the given key to the XapianDoc. If the value has a to_xapian_fu_storage_value method then it is used to generate the final value to be stored, otherwise to_s is used. This is usually paired with a from_xapian_fu_storage_value class method on retrieval.



102
103
104
105
106
# File 'lib/xapian_fu/xapian_doc_value_accessor.rb', line 102

def store(key, value, type = nil)
  converted_value = @doc.db.serialize_value(key, value, type)
  @doc.xapian_document.add_value(XapianDocValueAccessor.value_key(key), converted_value)
  value
end