1# Copyright (C) 2002-2011 Stanislav Sinyagin 2# 3# This program is free software; you can redistribute it and/or modify 4# it under the terms of the GNU General Public License as published by 5# the Free Software Foundation; either version 2 of the License, or 6# (at your option) any later version. 7# 8# This program is distributed in the hope that it will be useful, 9# but WITHOUT ANY WARRANTY; without even the implied warranty of 10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11# GNU General Public License for more details. 12# 13# You should have received a copy of the GNU General Public License 14# along with this program; if not, write to the Free Software 15# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. 16 17# Stanislav Sinyagin <ssinyagin@k-open.com> 18 19 20# Search database interface 21 22package Torrus::Search; 23use strict; 24use warnings; 25 26use Torrus::DB; 27use Torrus::Log; 28 29sub new 30{ 31 my $class = shift; 32 my %options = @_; 33 my $self = {}; 34 bless $self, $class; 35 36 %{$self->{'options'}} = %options; 37 38 return $self; 39} 40 41 42sub openTree 43{ 44 my $self = shift; 45 my $tree = shift; 46 47 my $db = new Torrus::DB 48 ( 'searchwords', 49 -Subdir => $tree, 50 -Btree => 1, 51 -Duplicates => 1, 52 -WriteAccess => $self->{'options'}{'-WriteAccess'}, 53 -Truncate => $self->{'options'}{'-WriteAccess'} ); 54 55 $self->{'db_treewords'}{$tree} = $db; 56 return; 57} 58 59 60sub closeTree 61{ 62 my $self = shift; 63 my $tree = shift; 64 65 $self->{'db_treewords'}{$tree}->closeNow(); 66 return; 67} 68 69 70sub openGlobal 71{ 72 my $self = shift; 73 74 my $db = new Torrus::DB 75 ( 'globsearchwords', 76 -Btree => 1, 77 -Duplicates => 1, 78 -WriteAccess => $self->{'options'}{'-WriteAccess'}, 79 -Truncate => $self->{'options'}{'-WriteAccess'} ); 80 81 $self->{'db_globwords'} = $db; 82 return; 83} 84 85 86sub storeKeyword 87{ 88 my $self = shift; 89 my $tree = shift; 90 my $keyword = lc( shift ); 91 my $path = shift; 92 my $param = shift; 93 94 my $val = $path; 95 if( defined( $param ) ) 96 { 97 $val .= ':' . $param; 98 } 99 100 my $lookupkey = join( ':', $tree, $keyword, $val ); 101 if( not $self->{'stored'}{$lookupkey} ) 102 { 103 $self->{'db_treewords'}{$tree}->put( $keyword, $val ); 104 if( defined( $self->{'db_globwords'} ) ) 105 { 106 $self->{'db_globwords'}->put( $keyword, join(':', $tree, $val) ); 107 } 108 109 $self->{'stored'}{$lookupkey} = 1; 110 } 111 return; 112} 113 114sub searchPrefix 115{ 116 my $self = shift; 117 my $prefix = lc( shift ); 118 my $tree = shift; 119 120 my $db = defined( $tree ) ? 121 $self->{'db_treewords'}{$tree} : $self->{'db_globwords'}; 122 123 my $result = $db->searchPrefix( $prefix ); 124 125 my $ret = []; 126 127 if( defined( $result ) ) 128 { 129 foreach my $pair ( @{$result} ) 130 { 131 my $retstrings = []; 132 push( @{$retstrings}, split(':', $pair->[1]) ); 133 push( @{$ret}, $retstrings ); 134 } 135 } 136 137 return $ret; 138} 139 140 141 142 143 1441; 145 146 147# Local Variables: 148# mode: perl 149# indent-tabs-mode: nil 150# perl-indent-level: 4 151# End: 152