Class: Fbp::Sort_node

Inherits:
Node
  • Object
show all
Defined in:
lib/fbp/sort-node.rb

Overview

Description

The Sort node will take its input IP and sort that data according to a set of hash keys that specify which data should be sorted.

Discussion

This implementation requires that all of the data to be sorted be available. This is NOT a scalable solution.

I have read some of the literature on sorting data streams but currently I do not have a ready solution. For now this will have to serve.

Instance Attribute Summary

Attributes inherited from Node

#executing, #options, #output

Instance Method Summary collapse

Methods inherited from Node

#clean_option, #execute, #merge_options!, #register_for_output_from_node, #set_option, #stop, #unregister_for_output_from_node, #wait_until_completed, #write_to_input, #write_to_output

Constructor Details

#initialize(sort_keys = nil) ⇒ Sort_node

When creating a new Sort_node instance one can provide an array of symbols which would be used to match keys in an incoming IP. The order of the keys in the array determine the sort order. The first key in the array will be used to sort the data then the subsequent keys if any.



22
23
24
25
26
# File 'lib/fbp/sort-node.rb', line 22

def initialize(sort_keys = nil)
	super()
	@options[:sort_keys] = sort_keys
    write_to_input({:begin_transaction => true})
end

Instance Method Details

#do_node_work(args) ⇒ Object

The Sort_node assumes that will will receive an IP with a :ips key which contains all of the IPs that need to be sorted. If the :ips key is not present then the IP is sent unchanged to the down stream node.

The IPs will be sorted based upon the sort_keys parameter. For each sort_key all of the IPs will be sorted based upon the value in the IP that matches the sort_key. If an IP does not have the sort_key then that IP is excluded from the sort and is disregarded.

Once the IPs have been sorted the IP has its :ips value replaced by the sorted data and the IP is then sent to the down stream node.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/fbp/sort-node.rb', line 48

def do_node_work(args)
    args.delete :completed
	ip_array = args[:ips]
    return super(args) if ip_array.nil?

    sort_keys_array = @options[:sort_keys].respond_to?('each') ? @options[:sort_keys] : [@options[:sort_keys]]

    data_to_sort = Array.new

    ip_array.each {|h| sort_keys_array.each  {|k| data_to_sort << h if h.has_key?(k) && !data_to_sort.include?(h)}}
    sort_keys_array.each {|k| data_to_sort.sort! {|a,b| a[k] <=> b[k]}}
    data_to_sort.each {|a| write_to_output(a)}

	# Update the input IP with the sorted list of IPs
	args[:ips] = data_to_sort
	super(args)
    false
end

#is_ready_to_run?Boolean

Checks to see if this Sort_node instance has any sort keys set. True will be returned if any keys are set, false otherwise.

Returns:

  • (Boolean)


31
32
33
# File 'lib/fbp/sort-node.rb', line 31

def is_ready_to_run?
	@options.has_key? :sort_keys
end