Module: Pacer::Routes::RouteOperations

Includes:
BulkOperations
Included in:
Pacer::Route
Defined in:
lib/pacer/route/mixin/route_operations.rb,
lib/pacer/transform/map.rb,
lib/pacer/transform/group.rb,
lib/pacer/transform/gather.rb,
lib/pacer/filter/uniq_filter.rb,
lib/pacer/filter/loop_filter.rb,
lib/pacer/filter/block_filter.rb,
lib/pacer/side_effect/counted.rb,
lib/pacer/filter/range_filter.rb,
lib/pacer/filter/future_filter.rb,
lib/pacer/side_effect/aggregate.rb,
lib/pacer/transform/stream_uniq.rb,
lib/pacer/transform/stream_sort.rb,
lib/pacer/side_effect/group_count.rb,
lib/pacer/filter/expression_filter.rb

Overview

Additional convenience and data analysis methods that can be mixed into routes if they support the full route interface.

Instance Method Summary (collapse)

Methods included from BulkOperations

#bulk_job, #bulk_map

Instance Method Details

- (Object) aggregate



3
4
5
# File 'lib/pacer/side_effect/aggregate.rb', line 3

def aggregate
  chain_route :side_effect => :aggregate
end

- (Object) as(name)

Store the current intermediate element in the route's vars hash by the given name so that it is accessible subsequently in the processing of the route.



82
83
84
# File 'lib/pacer/route/mixin/route_operations.rb', line 82

def as(name)
  chain_route :modules => VariableRouteModule, :variable_name => name
end

- (Object) at(pos)



19
20
21
# File 'lib/pacer/filter/range_filter.rb', line 19

def at(pos)
  chain_route :filter => :range, :index => pos
end

- (Object) context



12
13
14
# File 'lib/pacer/route/mixin/route_operations.rb', line 12

def context
  ContextRoute.new(self)
end

- (Object) count



8
9
10
# File 'lib/pacer/side_effect/counted.rb', line 8

def count
  counted.count
end

- (Object) counted



4
5
6
# File 'lib/pacer/side_effect/counted.rb', line 4

def counted
  chain_route :side_effect => :counted
end

- (Boolean) edges_route?

Returns true if this route countains only edges.

Returns:

  • (Boolean)


97
98
99
# File 'lib/pacer/route/mixin/route_operations.rb', line 97

def edges_route?
  self.is_a? Pacer::Core::Graph::EdgesRoute
end

- (Object) fast_group_count(hash_map = nil)



3
4
5
# File 'lib/pacer/side_effect/group_count.rb', line 3

def fast_group_count(hash_map = nil)
  chain_route :side_effect => :group_count, :hash_map => hash_map
end

- (Object) gather



3
4
5
# File 'lib/pacer/transform/gather.rb', line 3

def gather
  aggregate.cap
end

- (Object) group



4
5
6
# File 'lib/pacer/transform/group.rb', line 4

def group
  chain_route :transform => :group
end

- (Object) group_count(*props)

Creates a hash where the key is the properties and return value of the given block, and the value is the number of times each key was found in the results set.



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
# File 'lib/pacer/route/mixin/route_operations.rb', line 31

def group_count(*props)
  result = Hash.new(0)
  props = props.collect { |p| p.to_s }
  if props.empty? and block_given?
    each { |e| result[yield(e)] += 1 }
  elsif block_given?
    each do |e|
      key = props.collect { |p| e.get_property(p) }
      key << yield(e)
      result[key] += 1
    end
  elsif props.count == 1
    prop = props.first
    each do |e|
      result[e.get_property(prop)] += 1
    end
  elsif props.any?
    each do |e|
      result[props.collect { |p| e.get_property(p) }] += 1
    end
  else
    each do |e|
      result[e] += 1
    end
  end
  result
end

- (Boolean) has?(element)

Returns:

  • (Boolean)


24
25
26
# File 'lib/pacer/route/mixin/route_operations.rb', line 24

def has?(element)
  any? { |e| e == element }
end

- (Object) limit(max)



11
12
13
# File 'lib/pacer/filter/range_filter.rb', line 11

def limit(max)
  chain_route :filter => :range, :limit => max
end

- (Object) lookahead(&block)



4
5
6
# File 'lib/pacer/filter/future_filter.rb', line 4

def lookahead(&block)
  chain_route :filter => :future, :block => block
end

- (Object) loop(&block)



6
7
8
# File 'lib/pacer/filter/loop_filter.rb', line 6

def loop(&block)
  chain_route :looping_route => block
end

- (Object) map(opts = {}, &block)



4
5
6
# File 'lib/pacer/transform/map.rb', line 4

def map(opts = {}, &block)
  chain_route({:transform => :map, :block => block, :element_type => :object}.merge(opts))
end

- (Boolean) mixed_route?

Returns true if this route could contain both vertices and edges.

Returns:

  • (Boolean)


87
88
89
# File 'lib/pacer/route/mixin/route_operations.rb', line 87

def mixed_route?
  self.is_a? Pacer::Core::Graph::MixedRoute
end

- (Object) most_frequent(range = 0, include_counts = false)



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/pacer/route/mixin/route_operations.rb', line 59

def most_frequent(range = 0, include_counts = false)
  if include_counts
    result = group_count.sort_by { |k, v| -v }[range]
    if not result and range.is_a? Fixnum
      []
    else
      result
    end
  else
    result = group_count.sort_by { |k, v| -v }[range]
    if range.is_a? Fixnum
      result.first if result
    elsif result
      result.collect { |k, v| k }.to_route(:based_on => self)
    else
      [].to_route(:based_on => self)
    end
  end
end

- (Object) neg_lookahead(&block)



8
9
10
# File 'lib/pacer/filter/future_filter.rb', line 8

def neg_lookahead(&block)
  chain_route :filter => :future, :neg_block => block
end

- (Object) offset(amount)



15
16
17
# File 'lib/pacer/filter/range_filter.rb', line 15

def offset(amount)
  chain_route :filter => :range, :offset => amount
end

- (Object) pages(elements_per_page = 1000) {|page| ... }

Yields:

  • (page)


125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/pacer/route/mixin/route_operations.rb', line 125

def pages(elements_per_page = 1000)
  page = []
  results = []
  idx = 0
  each do |e|
    page << e
    idx += 1
    if idx % elements_per_page == 0
      results << yield(page)
      page = []
    end
  end
  yield page unless page.empty?
  results
end

- (Object) paths



8
9
10
# File 'lib/pacer/route/mixin/route_operations.rb', line 8

def paths
  PathsRoute.new(self)
end

- (Object) random(bias = 0.5)

Return elements based on a bias:1 chance.

If given an integer (n) > 0, bias is calcualated at 1 / n.



19
20
21
22
# File 'lib/pacer/route/mixin/route_operations.rb', line 19

def random(bias = 0.5)
  bias = 1 / bias.to_f if bias.is_a? Fixnum and bias > 0
  chain_route :pipe_class => Pacer::Pipes::RandomFilterPipe, :pipe_args => bias
end

- (Object) range(from, to)



4
5
6
7
8
9
# File 'lib/pacer/filter/range_filter.rb', line 4

def range(from, to)
  args = { :filter => :range }
  args[:begin] = from if from
  args[:end] = to if to
  chain_route args
end

- (Object) reject(&block)



8
9
10
# File 'lib/pacer/filter/block_filter.rb', line 8

def reject(&block)
  chain_route :filter => :block, :block => block, :invert => true, :route_name => 'Reject'
end

- (Object) repeat(range)

Apply the given path fragment multiple times in succession. If a range is given, the route is branched and each number of repeats is processed in a seperate branch before being merged back. That is useful if a pattern may be nested to varying depths.



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/pacer/route/mixin/route_operations.rb', line 104

def repeat(range)
  # TODO: switch to using loop
 #route = if range.is_a? Fixnum
 #    range.to_enum(:times).inject(self) do |route_end, count|
 #      yield route_end
 #    end
 #  else
 #    br = BranchedRoute.new(self)
 #    range.each do |count|
 #      br.branch do |branch_root|
 #        count.to_enum(:times).inject(branch_root) do |route_end, count|
 #          yield route_end
 #        end
 #      end
 #    end
 #    br
 #  end
 #route.add_extensions extensions
 #route
end

- (Object) select(&block)



4
5
6
# File 'lib/pacer/filter/block_filter.rb', line 4

def select(&block)
  chain_route :filter => :block, :block => block, :route_name => 'Select'
end

- (Object) stream_sort(buffer = 1000, silo = 100)



3
4
5
# File 'lib/pacer/transform/stream_sort.rb', line 3

def stream_sort(buffer = 1000, silo = 100)
  chain_route :transform => :stream_sort, :buffer => buffer, :silo => silo
end

- (Object) stream_uniq(buffer = 1000)



3
4
5
# File 'lib/pacer/transform/stream_uniq.rb', line 3

def stream_uniq(buffer = 1000)
  chain_route :transform => :stream_uniq, :buffer => buffer
end

- (Object) uniq(*filters, &block)

Do not return duplicate elements.



5
6
7
8
9
# File 'lib/pacer/filter/uniq_filter.rb', line 5

def uniq(*filters, &block)
  Pacer::Route.property_filter_before(self, filters, block) do |r|
    chain_route :pipe_class => Pacer::Pipes::DuplicateFilterPipe, :route_name => 'uniq'
  end
end

- (Boolean) vertices_route?

Returns true if this route countains only vertices.

Returns:

  • (Boolean)


92
93
94
# File 'lib/pacer/route/mixin/route_operations.rb', line 92

def vertices_route?
  self.is_a? Pacer::Core::Graph::VerticesRoute
end

- (Object) where(str, vars = {})



7
8
9
# File 'lib/pacer/filter/expression_filter.rb', line 7

def where(str, vars = {})
  chain_route :filter => :expression, :where => str, :vars => vars
end