Changeset 1474

Show
Ignore:
Timestamp:
08/24/08 18:48:15
Author:
rsz
Message:

cleanup

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • HTTP/Finder.ddl

    r1443 r1474  
    2424]], 
    2525 
    26 [ 6 ] =  
    27 [[ 
    28     create table if not exists [v:partition].document 
    29     ( 
    30         id      integer primary key not null, 
    31         name    text not null 
    32     ) 
    33 ]], 
    34  
    35 [ 7 ] =  
    36 [[ 
    37     create unique index if not exists [v:partition].document_name on document( name ) 
    38 ]], 
    39  
    40 [ 8 ] =  
    41 [[ 
    42     create table if not exists [v:partition].document_token 
    43     ( 
    44         document_id integer not null, 
    45         token_id    integer not null, 
    46         weight      integer not null 
    47     ) 
    48 ]], 
    49  
    50 [ 9 ] =  
    51 [[ 
    52     create index if not exists [v:partition].document_token_document_id on document_token( document_id ) 
    53 ]], 
    54  
    55 [ 10 ] =  
    56 [[ 
    57     create index if not exists [v:partition].document_token_token_id on document_token( token_id ) 
    58 ]], 
    59  
    60 [ 11 ] =  
    61 [[ 
    62     create table if not exists [v:partition].token 
    63     ( 
    64         id      integer primary key not null, 
    65         name    text not null 
    66     ) 
    67 ]], 
    68  
    69 [ 12 ] =  
    70 [[ 
    71     create unique index if not exists [v:partition].token_name on token( name ) 
    72 ]], 
    73  
    74 [ 13 ] = 
    75 [[ 
    76     create temporary table if not exists stage  
    77     ( 
    78         document    text not null, 
    79         token       text not null, 
    80         weight      integer not null 
    81     ) 
    82 ]], 
    83  
    84 [ 14 ] = 
     26[ 6 ] = 
    8527[[ 
    8628    create temporary table if not exists hit 
    8729    ( 
    88         document    text not null, 
    89         score       integer not null 
     30        name    text not null, 
     31        extract text not null 
    9032    ) 
    9133]] 
  • HTTP/Finder.dml

    r1466 r1474  
    1919]], 
    2020 
     21CreateDocument =  
     22[[ 
     23    create virtual table [v:partition].document using fts3 
     24    ( 
     25        name,  
     26        content,  
     27        tokenize porter  
     28    ) 
     29]], 
     30 
    2131DeleteDocument = 
    2232[[ 
     
    2838InsertDocument = 
    2939[[ 
    30     insert or ignore into [v:partition].document( name ) values( %s ) 
    31 ]], 
    32  
    33 UpdateDocument = 
    34 [[ 
    35     update or ignore [v:partition].document set name = %s where name = %s 
    36 ]], 
    37  
    38 DeleteStage = 
    39 [[ 
    40     delete from stage 
    41 ]], 
    42  
    43 InsertStage = 
    44 [[ 
    45     insert into stage( document, token, weight ) values( %s, %s, %s ) 
    46 ]], 
    47  
    48 InsertToken = 
    49 [[ 
    50     insert 
    51     into        [v:partition].token( name ) 
    52     select      stage.token as name 
    53     from        stage 
    54     left join   [v:partition].token on token.name = stage.token 
    55     where       token.id is null 
    56 ]], 
    57  
    58 DeleteDocumentToken = 
    59 [[ 
    60     delete  
    61     from    [v:partition].document_token  
    62     where   document_id 
    63     in      ( 
    64                 select  id 
    65                 from    [v:partition].document 
    66                 where   name = %s 
    67             ) 
    68 ]], 
    69  
    70 InsertDocumentToken = 
    71 [[ 
    72     insert  
    73     into        [v:partition].document_token 
    74                 (  
    75                     document_id,  
    76                     token_id,  
    77                     weight  
    78                 ) 
    79     select      document.id as document_id, 
    80                 token.id as token_id, 
    81                 stage.weight as weight 
    82     from        stage 
    83     join        [v:partition].document on document.name = stage.document 
    84     join        [v:partition].token on token.name = stage.token 
    85 ]], 
    86  
    87 SelectDocumentToken = 
    88 [[ 
    89     select      token.name as name, 
    90                 document_token.weight as weight 
    91     from        [v:partition].document 
    92     join        [v:partition].document_token on document_token.document_id = document.id 
    93     join        [v:partition].token on token.id = document_token.token_id 
    94     where       document.name = %s 
    95     order by    token.name, 
    96                 document_token.weight 
     40    insert or ignore into [v:partition].document( name, content ) values( %s, %s ) 
    9741]], 
    9842 
     
    10246    into        hit 
    10347                ( 
    104                     document
    105                     score 
     48                    name
     49                    extract 
    10650                ) 
    10751    select      document.name as name, 
    108                 sum( document_token.weight ) as score 
    109     from        stage 
    110     join        [v:partition].token on token.name >= stage.document and token.name < stage.token 
    111     join        [v:partition].document_token on document_token.token_id = token.id 
    112     join        [v:partition].document on document.id = document_token.document_id 
    113     group by    document.name 
    114     having      count( document_token.token_id ) >=  
    115                 ( 
    116                     select  count( stage.document ) as count 
    117                     from    stage 
    118                 ) 
    119     order by    2 desc, 
    120                 1 
     52                snippet( document, '<i>', '</i>', '…' ) as extract 
     53    from        [v:partition].document 
     54    where       document.content match %s 
     55    order by    1 
    12156    limit       %s 
    12257]], 
     
    12964SelectHit =  
    13065[[ 
    131     select      document
    132                 score 
     66    select      name
     67                extract 
    13368    from        hit 
    134     order by    score desc, 
    135                 document 
     69    order by    name 
    13670    limit       %s 
    13771]] 
  • HTTP/Finder.lua

    r1465 r1474  
    116116end 
    117117 
     118local function CreateDocument( aDB, aPartition ) 
     119    local aCall = function() 
     120        local aStatement = Template( DML[ 'CreateDocument' ] ) 
     121     
     122        aStatement[ 'partition' ] = aPartition 
     123     
     124        aDB( aStatement ) 
     125    end 
     126     
     127    pcall( aCall ) 
     128end 
     129 
    118130local function NewDB( aURL ) 
    119131    local DB = require( 'DB' ) 
    120132    local aDB = DB( aURL ) 
    121133    local aPath = PartitionPath( aURL ) 
    122   
     134     
    123135    for anIndex = 1, PartitionCount do 
    124136        local aStatement = Template( DML[ 'Attach' ] ) 
     
    137149            aDB( aStatement ) 
    138150        end  
     151         
     152        CreateDocument( aDB, aPartition ) 
    139153    end 
    140154     
     
    143157 
    144158-------------------------------------------------------------------------------- 
    145 -- Token 
    146 -------------------------------------------------------------------------------- 
    147  
    148 local function TokenList( anIterator ) 
    149     local aMap = {} 
    150     local aList = {} 
    151      
    152     for anIndex, aToken in anIterator do 
    153         if not aMap[ aToken ] then 
    154             aList[ #aList + 1 ] = aToken 
    155              
    156             aMap[ aToken ] = true 
    157              
    158             if #aList >= 16 then 
    159                 break 
    160             end 
    161         end 
    162     end 
    163  
    164     return aList 
    165 end 
    166  
    167 local function TokenMap( anIterator ) 
    168     local aMap = {} 
    169      
    170     for anIndex, aToken in anIterator do 
    171         aMap[ aToken ] = ( aMap[ aToken ] or 0 ) + 1 
    172     end 
    173      
    174     return aMap 
    175 end 
    176  
    177 local function TokenRange( aValue ) 
    178     local aLength = aValue:len() 
    179     local aLastChar = string.char( aValue:byte( aLength ) + 1 ) 
    180     local aLastValue = aValue:sub( 1, aLength - 1 ) .. aLastChar 
    181      
    182     return aLastValue 
    183 end 
    184  
    185 -------------------------------------------------------------------------------- 
    186159-- DML 
    187160-------------------------------------------------------------------------------- 
    188161 
    189 local function InsertDocument( aDB, aDocument, anIterator
     162local function InsertDocument( aDB, aDocument, aContent
    190163    local aPartition = Partition( aDocument ) 
    191164    local aStatement = nil 
     
    193166    aDB( DML[ 'BeginTransaction' ] ) 
    194167     
    195     aDB( DML[ 'DeleteStage' ] ) 
    196      
    197     for aToken, aWeight in pairs( TokenMap( anIterator ) ) do 
    198         aDB( DML[ 'InsertStage' ], aDocument, aToken, aWeight ) 
    199     end 
     168    aStatement = Template( DML[ 'DeleteDocument' ] ) 
     169    aStatement[ 'partition' ] = aPartition 
     170    aDB( aStatement, aDocument ) 
    200171 
    201172    aStatement = Template( DML[ 'InsertDocument' ] ) 
    202173    aStatement[ 'partition' ] = aPartition 
    203     aDB( aStatement, aDocument ) 
    204      
    205     aStatement = Template( DML[ 'InsertToken' ] ) 
    206     aStatement[ 'partition' ] = aPartition 
    207     aDB( aStatement ) 
    208  
    209     aStatement = Template( DML[ 'DeleteDocumentToken' ] ) 
    210     aStatement[ 'partition' ] = aPartition 
    211     aDB( aStatement, aDocument ) 
    212      
    213     aStatement = Template( DML[ 'InsertDocumentToken' ] ) 
    214     aStatement[ 'partition' ] = aPartition 
    215     aDB( aStatement ) 
     174    aDB( aStatement, aDocument, aContent ) 
    216175     
    217176    aDB( DML[ 'EndTransaction' ] ) 
     
    224183    aDB( DML[ 'BeginTransaction' ] ) 
    225184 
    226     aStatement = Template( DML[ 'DeleteDocumentToken' ] ) 
    227     aStatement[ 'partition' ] = aPartition 
    228     aDB( aStatement, aDocument ) 
    229  
    230185    aStatement = Template( DML[ 'DeleteDocument' ] ) 
    231186    aStatement[ 'partition' ] = aPartition 
     
    235190end 
    236191 
    237 local function UpdateDocument( aDB, aDocument, aNewDocument ) 
    238     local aPartition = Partition( aDocument ) 
    239     local aStatement = nil 
    240  
    241     aDB( DML[ 'BeginTransaction' ] ) 
    242      
    243     aStatement = Template( DML[ 'UpdateDocument' ] ) 
    244     aStatement[ 'partition' ] = aPartition 
    245     aDB( aStatement, aNewDocument, aDocument ) 
    246      
    247     aDB( DML[ 'EndTransaction' ] ) 
    248 end 
    249  
    250192local function SelectDocument( aDB, aDocument ) 
    251193    local aPartition = Partition( aDocument ) 
    252     local aStatement = Template( DML[ 'SelectDocumentToken' ] ) 
     194    local aStatement = Template( DML[ 'SelectDocument' ] ) 
    253195          aStatement[ 'partition' ] = aPartition 
    254196    local anIterator = aDB( aStatement, aDocument ) 
     
    258200         
    259201        if aRow then 
    260             return aRow.name, aRow.weigh
     202            return aRow.name, aRow.conten
    261203        end 
    262204    end 
    263205end 
    264206 
    265 local function FindDocument( aDB, anIterator, aLimit ) 
    266     local aList = TokenList( anIterator ) 
     207local function FindDocument( aDB, aQuery, aLimit ) 
    267208    local aLimit = aLimit or 999 
    268     local aStatement = DML[ 'SelectHit' ] 
    269209    local anIterator = nil 
    270210 
     
    272212     
    273213    aDB( DML[ 'DeleteHit' ] ) 
    274     aDB( DML[ 'DeleteStage' ] ) 
    275      
    276     for anIndex, aToken in ipairs( aList ) do 
    277         aDB( DML[ 'InsertStage' ], aToken, TokenRange( aToken ), 0 ) 
    278     end 
    279214     
    280215    for anIndex = 1, PartitionCount do 
     
    284219        aStatement[ 'partition' ] = aPartition 
    285220     
    286         aDB( aStatement, aLimit ) 
     221        aDB( aStatement, aQuery, aLimit ) 
    287222    end 
    288223 
    289224    aDB( DML[ 'EndTransaction' ] ) 
    290225     
    291     anIterator = aDB( aStatement, aLimit ) 
     226    anIterator = aDB( DML[ 'SelectHit' ], aLimit ) 
    292227     
    293228    return function() 
     
    295230         
    296231        if aRow then 
    297             return aRow.document, aRow.score 
     232            return aRow.name, aRow.extract 
    298233        end 
    299234    end 
     
    321256end 
    322257 
    323 function self:__call( anIterator, aLimit ) 
    324     return Try( FindDocument, self.db, anIterator, aLimit ) 
     258function self:__call( aQuery, aLimit ) 
     259    return Try( FindDocument, self.db, aQuery, aLimit ) 
    325260end 
    326261 
     
    330265 
    331266function self:__newindex( aKey, aValue ) 
    332     if type( aKey ) == 'string' and type( aValue ) == 'function' then 
     267    if type( aKey ) == 'string' and type( aValue ) == 'string' then 
    333268        return Try( InsertDocument, self.db, aKey, aValue ) 
    334     elseif type( aKey ) == 'string' and type( aValue ) == 'string' then 
    335         return Try( UpdateDocument, self.db, aKey, aValue ) 
    336269    elseif type( aKey ) == 'string' and type( aValue ) == 'boolean' and aValue == false then 
    337270        return Try( DeleteDocument, self.db, aKey ) 
  • HTTP/WikiFinder.lua

    r1472 r1474  
    7575end 
    7676 
    77 local stop = { [ 'and' ] = true, [ 'are' ] = true, [ 'but' ] = true, [ 'for' ] = true, [ 'into' ] = true, [ 'not' ] = true, [ 'such' ] = true, [ 'that' ] = true, [ 'the' ] = true, [ 'their' ] = true, [ 'then' ] = true, [ 'there' ] = true, [ 'these' ] = true, [ 'they' ] = true, [ 'this' ] = true, [ 'will' ] = true, [ 'with' ] = true, [ 'yes' ] = true, [ 'you' ] = true, [ 'your' ] = true } 
    78  
    79 local function Token( aValue ) 
    80     local Unidecode = require( 'Unidecode' ) 
    81     local aValue = Unidecode( aValue or '' ):lower():gsub( '%W', ' ' ) 
    82     local anIterator = aValue:gmatch( '([%S]+)' ) 
    83     local anIndex = 0 
    84      
    85     return function() 
    86         local aToken = anIterator() 
    87          
    88         while aToken and ( aToken:len() < 3 or stop[ aToken ] ) do 
    89             aToken = anIterator() 
    90         end 
    91          
    92         if aToken then 
    93             anIndex = anIndex + 1 
    94              
    95             return anIndex, aToken:sub( 1, 9 ) 
    96         end 
    97     end 
    98 end 
    99  
    10077local function Try( aFunction, ... ) 
    10178    local ok, aResult = pcall( aFunction, ... ) 
     
    177154                local WikiService = require( 'WikiService' ) 
    178155                 
    179                 aFinder[ aName ] = Token( WikiService.Text( aContent )
     156                aFinder[ aName ] = WikiService.Text( aContent
    180157            end 
    181158             
     
    189166     
    190167    if aFinder then 
    191         return aFinder( Token( aText ), 10
     168        return aFinder( aText
    192169    end 
    193170     
  • HTTP/WikiSearchService.lua

    r1471 r1474  
    3434 
    3535local getmetatable = getmetatable 
     36local next = next 
    3637local pairs = pairs 
    3738local require = require 
     
    5758    local hasData = false 
    5859     
    59     for aContent, aURL in anIterator do 
     60    for aContent, aURL, anExtract in anIterator do 
    6061        if not someHits[ aContent.name ] then 
    6162            local aNameTemplate = aTemplate[ 'names' ] 
     
    6465            aNameTemplate[ 'name' ] = Encode( aContent.title ) 
    6566            aNameTemplate[ 'tag' ] = Tag( aContent.modification, true ) 
     67            aNameTemplate[ 'extract' ] = anExtract 
    6668         
    6769            aTemplate[ 'names' ] = aNameTemplate 
     
    129131end 
    130132 
     133local function ContentSearch( aQuery ) 
     134    local anIterator = WikiFinder[ aQuery ] 
     135    local aContentIterator = function() 
     136        local HTTPExtra = require( 'HTTPExtra' ) 
     137        local HTTPService = require( 'HTTPService' ) 
     138        local WikiContent = require( 'WikiContent' ) 
     139        local WikiContentService = require( 'WikiContentService' ) 
     140        local aName, anExtract = anIterator() 
     141         
     142        if aName then 
     143            local aContent = WikiContent( aName ) 
     144            local aService = WikiContentService( aContent ) 
     145            local aURL = HTTPService[ aService ] 
     146             
     147            return aContent, aURL, anExtract 
     148        end 
     149    end 
     150     
     151    return aContentIterator 
     152end 
     153 
    131154local function ContentHit( aTemplate, aQuery, someHits ) 
    132     local anIterator = ContentIterator( WikiFinder[ aQuery ]
    133      
     155    local anIterator = ContentSearch( aQuery
     156         
    134157    return Hit( aTemplate, anIterator, someHits ) 
     158end 
     159 
     160local function Search( aQuery ) 
     161    local aTemplate = Template[ 'WikiSearchService.txt' ] 
     162    local someHits = { n = 0, m = false } 
     163    local aKey = nil 
     164    local anIterator = function() 
     165        aKey = next( someHits, aKey ) 
     166         
     167        if aKey then 
     168            return aKey 
     169        end 
     170    end 
     171     
     172    aTemplate[ 'directHit' ] = DirectHit( aTemplate[ 'directHit' ], aQuery, someHits ) 
     173    aTemplate[ 'titleHit' ] = TitleHit( aTemplate[ 'titleHit' ], aQuery, someHits ) 
     174    aTemplate[ 'contentHit' ] = ContentHit( aTemplate[ 'contentHit' ], aQuery, someHits ) 
     175 
     176    return ContentIterator( anIterator ) 
    135177end 
    136178 
     
    192234    local WikiFeed = require( 'WikiFeed' ) 
    193235    local WikiIndex = require( 'WikiIndex' ) 
    194     local anIterator, aCount, hasMore = TitleSearch( self.query ) 
     236    local anIterator = Search( self.query ) 
    195237    local aGenerator = HTML 
    196238    local aQuery = self.query or '' 
  • HTTP/WikiSearchService.txt

    r1473 r1474  
    2020        <li> 
    2121            <a href='[v:href]' title='[v:name]'>[v:name]</a>[v:tag] 
    22             <br/>“…” 
     22            <p>“[v:extract]”</p> 
    2323        </li> 
    2424    [/t:names]