Class: Libcouchbase::QueryView

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

Constant Summary collapse

F_INCLUDE_DOCS =

Set this flag to execute an actual get with each response

1 << 16
F_NOROWPARSE =

Set this flag to only parse the top level row, and not its constituent parts. Note this is incompatible with ‘F_INCLUDE_DOCS`

1 << 17
F_SPATIAL =

This view is spatial. Modifies how the final view path will be constructed

1 << 18

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, reactor, design, view, **opts) ⇒ QueryView

Returns a new instance of QueryView.



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/libcouchbase/query_view.rb', line 16

def initialize(connection, reactor, design, view, **opts)
    @connection = connection
    @reactor = reactor

    @design = design
    @view = view
    @options = opts

    @include_docs = true
    @is_spatial = false
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



28
29
30
# File 'lib/libcouchbase/query_view.rb', line 28

def connection
  @connection
end

#designObject (readonly)

Returns the value of attribute design.



28
29
30
# File 'lib/libcouchbase/query_view.rb', line 28

def design
  @design
end

#include_docsObject

Returns the value of attribute include_docs.



29
30
31
# File 'lib/libcouchbase/query_view.rb', line 29

def include_docs
  @include_docs
end

#is_spatialObject

Returns the value of attribute is_spatial.



29
30
31
# File 'lib/libcouchbase/query_view.rb', line 29

def is_spatial
  @is_spatial
end

#optionsObject (readonly)

Returns the value of attribute options.



28
29
30
# File 'lib/libcouchbase/query_view.rb', line 28

def options
  @options
end

#viewObject (readonly)

Returns the value of attribute view.



28
29
30
# File 'lib/libcouchbase/query_view.rb', line 28

def view
  @view
end

Instance Method Details

#cancelObject

We don’t ever actually cancel a request here. There is an API however it indicates that @connection.handle might be destroyed Testing also indicated that @connection.handle was destroyed with seg faults



131
132
133
# File 'lib/libcouchbase/query_view.rb', line 131

def cancel
    @error = :cancelled
end

#error(obj) ⇒ Object



123
124
125
126
# File 'lib/libcouchbase/query_view.rb', line 123

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

#get_count(metadata) ⇒ Object



31
32
33
# File 'lib/libcouchbase/query_view.rb', line 31

def get_count()
    [:total_rows]
end

#perform(**options, &blk) ⇒ Object



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

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

    options = @options.merge(options)
    # We should never exceed the users results limit
    orig_limit = @options[:limit]
    limit = options[:limit]
    if orig_limit && limit
        options[:limit] = orig_limit if limit > orig_limit
    end

    pairs = []
    options.each { |key, val|
        if key.to_s.include?('key') && val[0] != "["
            pairs << "#{key}=#{[val].to_json[1...-1]}"
        else
            pairs << "#{key}=#{val}"
        end
    }
    opts = pairs.join('&')

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

        @cmd = Ext::CMDVIEWQUERY.new
        Ext.view_query_initcmd(@cmd, @design.to_s, @view.to_s, opts, @connection.get_callback(:viewquery_callback))
        @cmd[:cmdflags] |= F_INCLUDE_DOCS if include_docs
        @cmd[:cmdflags] |= F_SPATIAL if is_spatial

        pointer = @cmd.to_ptr

        @connection.requests[pointer.address] = self
        err = Ext.view_query(@connection.handle, pointer, @cmd)
        if err != :success
            error(Error.lookup(err).new('view request not scheduled'))
        end
    }
end

#received(row) ⇒ Object



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

def received(row)
    return if @error

    key = row[:key].read_string(row[:nkey])
    cas = row[:cas]
    emitted = row[:value].read_string(row[:nvalue]) if row[:nvalue] > 0
    geometry = row[:geometry].read_string(row[:ngeometry]) if row[:ngeometry] > 0
    doc_id = row[:docid].read_string(row[:ndocid]) if row[:ndocid] > 0

    meta = {
        emitted: emitted,
        geometry: geometry,
        key: key
    }

    resp = Response.new(:viewquery_callback, doc_id, cas)
    resp. = meta

    # check for included document here
    if @include_docs && row[:docresp]
        doc = row[:docresp]
        raw_string = doc[:value].null? ? '' : doc[:value].read_string(doc[:nvalue])
        resp.value = @connection.parse_document(raw_string)
        meta[:flags] = doc[:itmflags]
    end

    @callback.call(false, resp)
rescue => e
    @error = e
end

#received_final(metadata) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/libcouchbase/query_view.rb', line 108

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

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