Class: Libcouchbase::QueryFullText

Inherits:
Object
  • Object
show all
Defined in:
lib/libcouchbase/query_full_text.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, reactor, include_docs: true, **opts) ⇒ QueryFullText

Returns a new instance of QueryFullText.



5
6
7
8
9
10
11
12
# File 'lib/libcouchbase/query_full_text.rb', line 5

def initialize(connection, reactor, include_docs: true, **opts)
    @connection = connection
    @reactor = reactor

    @options = opts
    @include_docs = include_docs
    @request_handle = FFI::MemoryPointer.new :pointer, 1
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



14
15
16
# File 'lib/libcouchbase/query_full_text.rb', line 14

def connection
  @connection
end

#include_docsObject

Returns the value of attribute include_docs.



15
16
17
# File 'lib/libcouchbase/query_full_text.rb', line 15

def include_docs
  @include_docs
end

#optionsObject (readonly)

Returns the value of attribute options.



14
15
16
# File 'lib/libcouchbase/query_full_text.rb', line 14

def options
  @options
end

Instance Method Details

#cancelObject



112
113
114
115
116
117
118
119
120
# File 'lib/libcouchbase/query_full_text.rb', line 112

def cancel
    @error = :cancelled unless @error
    @reactor.schedule {
        if @connection.handle && @cmd
            Ext.fts_cancel(@connection.handle, @handle_ptr.get_pointer(0))
            received_final(true)
        end
    }
end

#error(obj) ⇒ Object



105
106
107
108
109
110
# File 'lib/libcouchbase/query_full_text.rb', line 105

def error(obj)
    @error = obj
    # This sets metadata to true, however it'll be ignored as error is set
    # We set it to true as we may be fetching documents in parallel and need to wait
    received_final(true)
end

#get_count(metadata) ⇒ Object



21
22
23
# File 'lib/libcouchbase/query_full_text.rb', line 21

def get_count()
    [:total_hits]
end

#indexObject



17
18
19
# File 'lib/libcouchbase/query_full_text.rb', line 17

def index
    @options[:indexName]
end

#perform(limit: nil, **options, &blk) ⇒ Object



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
# File 'lib/libcouchbase/query_full_text.rb', line 25

def perform(limit: nil, **options, &blk)
    raise 'not connected' unless @connection.handle
    raise 'query already in progress' if @query_cstr
    raise 'callback required' unless block_given?

    # customise the size based on the request being made
    orig_size = @options[:size] || 10 # 10 is the couchbase default
    new_size = limit || orig_size
    begin
        @options[:size] = new_size if orig_size > new_size
        @query_text = JSON.generate(@options)
        @query_cstr = FFI::MemoryPointer.from_string(@query_text)
    rescue
        @query_cstr = nil
        @query_text = nil
        raise
    ensure
        @options[:size] = orig_size
    end

    @reactor.schedule {
        @error = nil
        @doc_count = 0
        @callback = blk

        @cmd = Ext::CMDFTS.new
        @cmd[:query] = @query_cstr
        @cmd[:nquery] = @query_text.bytesize
        @cmd[:callback] = @connection.get_callback(:fts_callback)
        @cmd[:handle] = @request_handle

        pointer = @cmd.to_ptr
        @connection.requests[pointer.address] = self

        err = Ext.fts_query(@connection.handle, pointer, @cmd)
        if err != :success
            error(Error.lookup(err).new('full text search not scheduled'))
        end
    }
end

#received(row) ⇒ Object

Example Row: :id=>“dep_1-18”, :score=>1.3540229098345296,

:locations=>{:class_name=>{:toshiba=>[{:pos=>1, :start=>2, :end=>9, :array_positions=>nil]},
:name=>:start=>0, :end=>7, :array_positions=>nil]}}}


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
# File 'lib/libcouchbase/query_full_text.rb', line 70

def received(row)
    return if @error

    resp = Response.new(:fts_callback, row[:id])
    resp. = row

    # TODO:: this could cause results to be returned out of order
    if @include_docs
        @doc_count += 1
        doc = @connection.get(resp.key).value
        resp.value = doc.value
        resp.cas = doc.cas
        resp..merge! doc.
    end

    @callback.call(false, resp)
rescue => e
    @error = e
    cancel
ensure
    if @include_docs
        @doc_count -= 1
        process_final if @metadata && @doc_count == 0
    end
end

#received_final(metadata) ⇒ Object

Example metadata :failed=>0, :successful=>2, :request=>:boost=>1,

:size=>10, :from=>0, :highlight=>nil, :fields=>nil, :facets=>nil, :explain=>false}, :hits=>[],
:total_hits=>4, :max_score=>1.6405488681166451, :took=>10182765, :facets=>{}}


100
101
102
103
# File 'lib/libcouchbase/query_full_text.rb', line 100

def received_final()
    @metadata = 
    process_final unless @doc_count > 0
end