Class: Libcouchbase::QueryN1QL

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

Constant Summary collapse

N1P_QUERY_STATEMENT =
1
N1P_CONSISTENCY_REQUEST =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, reactor, n1ql, **opts) ⇒ QueryN1QL

Returns a new instance of QueryN1QL.



8
9
10
11
12
13
14
# File 'lib/libcouchbase/query_n1ql.rb', line 8

def initialize(connection, reactor, n1ql, **opts)
    @connection = connection
    @reactor = reactor

    @n1ql = n1ql
    @request_handle = FFI::MemoryPointer.new :pointer, 1
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



16
17
18
# File 'lib/libcouchbase/query_n1ql.rb', line 16

def connection
  @connection
end

#n1qlObject (readonly)

Returns the value of attribute n1ql.



16
17
18
# File 'lib/libcouchbase/query_n1ql.rb', line 16

def n1ql
  @n1ql
end

Instance Method Details

#cancelObject



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

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

#error(obj) ⇒ Object



108
109
110
111
# File 'lib/libcouchbase/query_n1ql.rb', line 108

def error(obj)
    @error = obj
    received_final(nil)
end

#get_count(metadata) ⇒ Object



18
19
20
# File 'lib/libcouchbase/query_n1ql.rb', line 18

def get_count()
    [:metrics][:resultCount]
end

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



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

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

    # customise the size based on the request being made
    orig_limit = @n1ql.limit
    begin
        if orig_limit && limit
            @n1ql.limit = limit if orig_limit > limit
        end
        @query_text = @n1ql.to_s
    rescue
        @query_text = nil
        raise
    ensure
        @n1ql.limit = orig_limit
    end

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

        @cmd = Ext::CMDN1QL.new
        @params = Ext.n1p_new
        err = Ext.n1p_setconsistency(@params, N1P_CONSISTENCY_REQUEST)
        if err == :success
            err = Ext.n1p_setquery(@params, @query_text, @query_text.bytesize, N1P_QUERY_STATEMENT)
            if err == :success

                err = Ext.n1p_mkcmd(@params, @cmd)
                if err == :success
                    pointer = @cmd.to_ptr
                    @connection.requests[pointer.address] = self

                    @cmd[:callback] = @connection.get_callback(:n1ql_callback)
                    @cmd[:handle] = @request_handle

                    err = Ext.n1ql_query(@connection.handle, pointer, @cmd)
                    if err != :success
                    error(Error.lookup(err).new('full text search not scheduled'))
                    end
                else
                    error(Error.lookup(err).new('failed to build full text search command'))
                end
            else
                error(Error.lookup(err).new('failed to build full text search query structure'))
            end
        else
            error(Error.lookup(err).new('failed set consistency value'))
        end
    }
end

#received(row) ⇒ Object

Row is JSON value representing the result



77
78
79
80
81
82
83
# File 'lib/libcouchbase/query_n1ql.rb', line 77

def received(row)
    return if @error
    @callback.call(false, row)
rescue => e
    @error = e
    cancel
end

#received_final(metadata) ⇒ Object

Example metadata :signature=>{:*=>“*”, :results=>[], :status=>“success”,

:metrics=>{:elapsedTime=>"15.298243ms", :executionTime=>"15.256975ms", :resultCount=>12, :resultSize=>8964}}


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/libcouchbase/query_n1ql.rb', line 88

def received_final()
    @query_text = nil

    @connection.requests.delete(@cmd.to_ptr.address)
    @cmd = nil

    Ext.n1p_free(@params)
    @params = nil

    if @error
        if @error == :cancelled
            @callback.call(:final, )
        else
            @callback.call(:error, @error)
        end
    else
        @callback.call(:final, )
    end
end