lib/code_zauker.rb in code_zauker-0.0.7 vs lib/code_zauker.rb in code_zauker-0.0.8
- old
+ new
@@ -1,12 +1,15 @@
# -*- mode:ruby ; -*- -*
require "code_zauker/version"
require "code_zauker/constants"
+require 'code_zauker/grep'
require 'redis/connection/hiredis'
require 'redis'
require 'set'
require 'pdf/reader'
+require 'date'
+
# This module implements a simple reverse indexer
# based on Redis
# The idea is ispired by http://swtch.com/~rsc/regexp/regexp4.html
module CodeZauker
GRAM_SIZE=3
@@ -100,12 +103,51 @@
lines=f.readlines()
}
end
return lines
end
+ end
-
+ # Manage the index and keep it well organized
+ class IndexManager
+ def initialize(redisConnection=nil)
+ if redisConnection==nil
+ @redis=Redis.new
+ else
+ @redis=redisConnection
+ end
+ end
+
+ def check_repair
+
+ puts "Staring index check"
+ dbversion=@redis.hget("codezauker","db_version")
+ if dbversion==nil
+ puts "DB Version <=0.7"
+ @redis.hset("codezauker","db_version",CodeZauker::DB_VERSION)
+ # no other checks to do right now
+ else
+ if dbversion.to_i() > CodeZauker::DB_VERSION
+ raise "DB Version #{dbversion} is greater than my #{CodeZauker::DB_VERSION}"
+ else
+ puts "Migrating from #{dbversion} to #{CodeZauker::DB_VERSION}"
+ # Nothing to do right now
+ end
+ end
+ puts "Summary....."
+ dbversion=@redis.hget("codezauker","db_version")
+ last_check=@redis.hget("codezauker","last_check")
+ puts "DB Version: #{dbversion}"
+ puts "Last Check: #{last_check}"
+ puts "Checking...."
+ @redis.hset("codezauker","last_check",DateTime.now().to_s())
+ puts "Issuing save..."
+ @redis.save()
+ puts "Save successful"
+ @redis.quit()
+ puts "Disconnected from redis"
+ end
end
# Scan a file and push it inside redis...
# then it can provide handy method to find file scontaining the trigram...
class FileScanner
@@ -117,11 +159,16 @@
end
end
def disconnect()
- @redis.quit
+ begin
+ @redis.quit
+ rescue Errno::EAGAIN =>e
+ # Nothing to do...
+ puts "Ignored EAGAIN ERROR during disconnect..."
+ end
end
@@ -249,11 +296,11 @@
def split_in_trigrams(term, prefix)
trigramInAnd=Set.new()
# Search=> Sea AND ear AND arc AND rch
for j in 0...term.length
- currentTrigram=term[j,GRAM_SIZE]
+ currentTrigram=term[j,GRAM_SIZE]
if currentTrigram.length <GRAM_SIZE
# We are at the end...
break
end
trigramInAnd.add("#{prefix}:#{currentTrigram}")
@@ -285,9 +332,44 @@
if trigramInAnd.length==0
return []
end
fileIds= @redis.sinter(*trigramInAnd)
return map_ids_to_files(fileIds)
+ end
+
+ # = wild cards search
+ # You can search trigram in the form
+ # public*class*Apple
+ # will match java declaration of MyApple but not
+ # YourAppManager
+ def wsearch(term)
+ # Split stuff
+ puts "Wild Search request:#{term}"
+ m=term.split("*")
+ if m.length>0
+ trigramInAnd=Set.new()
+ puts "*= Found:#{m.length}"
+ m.each do | wtc |
+ wt=wtc.downcase()
+ #puts "Splitting #{wt}"
+ trigSet=split_in_trigrams(wt,"trigram:ci")
+ trigramInAnd=trigramInAnd.merge(trigSet)
+ end
+ # puts "Trigrams: #{trigramInAnd.length}"
+ # trigramInAnd.each do | x |
+ # puts "#{x}"
+ # end
+ if trigramInAnd.length==0
+ return []
+ end
+ fileIds=@redis.sinter(*trigramInAnd)
+ fileNames=map_ids_to_files(fileIds)
+ #puts "DEBUG #{fileIds} #{fileNames}"
+ return fileNames
+ else
+ puts "Warn no Wild!"
+ return search(term)
+ end
end
# = search
# Find a list of file candidates to a search string