Class: MarkLogic::Cursor

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/marklogic/cursor.rb

Constant Summary collapse

DEFAULT_PAGE_LENGTH =
25

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection, options = {}) ⇒ Cursor

Returns a new instance of Cursor.


10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/marklogic/cursor.rb', line 10

def initialize(collection, options = {})
  @options = options || {}
  @query = options.delete(:query)
  @collection = collection
  @transformer = options.delete(:transformer)
  @fields = options.delete(:fields)
  @fields = convert_fields_for_query(@fields) if @fields
  @connection = @collection.database.connection
  @limit = options.delete(:limit)
  @cache = []
  @query_run = false
  @visited = 0
end

Instance Attribute Details

#queriesObject

Returns the value of attribute queries


6
7
8
# File 'lib/marklogic/cursor.rb', line 6

def queries
  @queries
end

Instance Method Details

#convert_fields_for_query(fields) ⇒ Object


70
71
72
73
74
75
76
77
78
79
80
# File 'lib/marklogic/cursor.rb', line 70

def convert_fields_for_query(fields)
  case fields
    when String, Symbol
      [ fields ]
    when Array
      return nil if fields.length.zero?
      fields
    when Hash
      return fields.keys
  end
end

#countObject

Raises:

  • (Exception)

24
25
26
27
28
29
30
# File 'lib/marklogic/cursor.rb', line 24

def count
  col_name = collection.nil? ? "" : %Q{"#{collection.collection}"}
  query_to_run = %Q{xdmp:estimate(cts:search(fn:collection(#{col_name}), #{query.to_xqy}, ("unfiltered")))}
  response = @connection.run_query(query_to_run, "xquery")
  raise Exception.new("Invalid response: #{response.code.to_i}: #{response.body}") if (response.code.to_i != 200)
  response.body.to_i
end

#each(&block) ⇒ Object


82
83
84
85
86
# File 'lib/marklogic/cursor.rb', line 82

def each(&block)
  while doc = self.next
    yield doc
  end
end

#nextObject Also known as: next_document


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
# File 'lib/marklogic/cursor.rb', line 37

def next
  refresh unless @query_run

  return nil if @visited == @limit

  if @cache.length == 0
    if page < total_pages
      self.page = page + 1
      refresh
    end

    if @cache.length == 0
      return nil
    end
  end

  @visited = @visited + 1
  doc = @cache.shift

  # TODO: Move this server side
  if @fields
    @fields << :_id unless @fields.include?(:_id) || @fields.include?('_id')
    doc = @fields.each_with_object(doc.class.new) { |key, result| result[key] = doc[key.to_s] if doc.has_key?(key.to_s) }
  end

  if @transformer.nil?
    doc
  else
    @transformer.call(doc) if doc
  end
end

#paged_resultsObject


32
33
34
35
# File 'lib/marklogic/cursor.rb', line 32

def paged_results
  refresh unless @query_run
  @cache
end

#rewind!Object


92
93
94
95
96
# File 'lib/marklogic/cursor.rb', line 92

def rewind!
  self.page = 1
  @query_run = false
  @visited = 0
end

#to_aObject


88
89
90
# File 'lib/marklogic/cursor.rb', line 88

def to_a
  super
end