Class: ZCC::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/zcc/query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(term, zservers) ⇒ Query

Returns a new instance of Query.



6
7
8
9
10
11
12
13
14
15
# File 'lib/zcc/query.rb', line 6

def initialize(term, zservers)
  self.term = term #=> string of original query post any filtering
  #puts "term: " + self.term
  @zservers = []
  @zservers << zservers #=>array of Zserver objects
  self.zservers.compact!
  self.zservers.flatten!
  #puts self.inspect
  #puts self.zservers[0].to_s
end

Instance Attribute Details

#termObject

Returns the value of attribute term.



4
5
6
# File 'lib/zcc/query.rb', line 4

def term
  @term
end

#typeObject (readonly)

Returns the value of attribute type.



4
5
6
# File 'lib/zcc/query.rb', line 4

def type
  @type
end

#zsearchObject (readonly)

Returns the value of attribute zsearch.



4
5
6
# File 'lib/zcc/query.rb', line 4

def zsearch
  @zsearch
end

#zserversObject (readonly)

Returns the value of attribute zservers.



4
5
6
# File 'lib/zcc/query.rb', line 4

def zservers
  @zservers
end

Instance Method Details

#zerror_log(error) ⇒ Object

FIXME probably should move this to a different class that contants more cli stuff



107
108
109
110
111
# File 'lib/zcc/query.rb', line 107

def zerror_log error
  File.open("#{ROOT}/zerror_log", "a+") do |f|
    f.write error + "\n"
  end
end

#zoom(show = 10) ⇒ Object Also known as: search

This is the main search logic of the whole shebang. Aliased as ‘search’. The method on a query object and is passed the number of records from each host to present to the user. Default number of records is 10. Still need to work on a way to get options in so this might change. Currently only MARC21 is supported.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
# File 'lib/zcc/query.rb', line 52

def zoom(show = 10)
  result_set = ResultSet.new(self)
  #puts zservers.inspect
  search_threads=[]
  puts zservers.size.to_s + " z-servers in group " + zservers[0].group.to_s
  z = 0
  self.zservers.each do |server|
      z += 1
      #zservers are made up of...
      #puts server.to_s
    search_threads << Thread.new(server, z) do |myserver, myz|
      begin
        ZOOM::Connection.open("#{myserver.host}:#{server.port}/#{myserver.database}") do |conn|
          #conn.connect(myserver.host, server.port) #do |conn 
          conn.set_option('charset', 'UTF-8')
          conn.preferred_record_syntax = 'MARC21'
          puts "#{myz} Searching:          #{myserver.to_s} | #{self.zsearch}"
          rset = conn.search(self.zsearch)
          say(myz.to_s.headline + " Finished searching: #{myserver.to_s} | rset.size: " + "#{rset.size}".red.bold)
          rset_recs = rset[0, show]
          #puts "rset_recs in query.search: " 
          #puts rset_recs
          i = 0
          rset_recs.each do |rec|
            #puts myserver.to_s
            #puts rec
            #puts
            marc_record = ZCC.convert_char(rec)
            #puts "gets past character conversion"
            #puts "-------------\n"
            #puts marc_record
            #puts "---------------\n"
            zcc_record = Record.new(marc_record, myserver)
            result_set.ingest(zcc_record)
            puts "#{myz} record #{i} from       #{myserver}..."
            i += 1
          end
        conn = nil  
        end
      rescue Exception => e
        zerror_log("dead thread: " + myserver.to_s + " | " + e)
        puts "\a#{myz}!!!!!!!! Thread died #{myserver} !!!!!"
      end
      #puts "end: #{Thread.list}"
      #puts "Results processed from:  #{myserver.to_s}"
    end
    
  end
  search_threads.each{|thread| thread.join}
  return result_set
end