Skip to content

Instantly share code, notes, and snippets.

@MosakujiHokuto
Last active August 29, 2015 14:01
Show Gist options
  • Save MosakujiHokuto/aabb1af696be384a8483 to your computer and use it in GitHub Desktop.
Save MosakujiHokuto/aabb1af696be384a8483 to your computer and use it in GitHub Desktop.
Simple code search
#!/usr/bin/env perl
use strict;
use warnings;
use Redis;
my @files = (<*.c>,<*.h>,<*.cc>,<*.hh>,<*.cpp>,<*.hpp>);
my $redis = Redis->new;
for my $file (@files) {
open(my $fd,"<",$file) or die "failed when opening $file";
print "Creating index for $file\n";
my %hash = ();
my $ln = 1;
while(<$fd>) {
chomp;
$hash{lc($1)} .= "$ln: $_\n" while(m/([a-zA-Z0-9][a-zA-Z0-9_]+)/g);
$ln++;
}
for my $key (keys %hash) {
$redis->sadd("index:$key",$file);
$redis->set("data:$file:$key",$hash{$key});
}
print "Finished creating index for $file\n";
}
#!/usr/bin/env perl
use strict;
use warnings;
use Redis;
my $redis = Redis->new;
my $mode = lc(shift);
my @keys = @ARGV;
my @query_list = map { "index:".lc($_) } @keys;
my @result;
if ($mode eq "and") {
@result = $redis->sinter(@query_list);
} elsif ($mode eq "or" ) {
@result = $redis->sunion(@query_list);
} else {
die "Dunno what's $mode,stop";
}
print "Find ",scalar(@result)," result\n";
for my $file (@result) {
print "In $file:\n";
foreach (@keys) {
my $key = lc($_);
my $content = $redis->get("data:$file:$key");
if ($content) {
print " $key:\n";
my @lines = split /\n/,$content;
print " $_\n" foreach(@lines);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment