~ubuntu-branches/ubuntu/natty/moin/natty-updates

« back to all changes in this revision

Viewing changes to MoinMoin/support/lupy/index/segment.py

  • Committer: Bazaar Package Importer
  • Author(s): Jonas Smedegaard
  • Date: 2008-06-22 21:17:13 UTC
  • mfrom: (0.9.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080622211713-fpo2zrq3s5dfecxg
Tags: 1.7.0-3
Simplify /etc/moin/wikilist format: "USER URL" (drop unneeded middle
CONFIG_DIR that was wrongly advertised as DATA_DIR).  Make
moin-mass-migrate handle both formats and warn about deprecation of
the old one.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- test-case-name: lupy.test -*- 
2
 
# This module is part of the Lupy project and is Copyright 2003 Amir
3
 
# Bakhtiar (amir@divmod.org). This is free software; you can redistribute
4
 
# it and/or modify it under the terms of version 2.1 of the GNU Lesser
5
 
# General Public License as published by the Free Software Foundation.
6
 
 
7
 
from MoinMoin.support.lupy.index import term
8
 
 
9
 
#import copy #broken, see comments at top of this file:
10
 
from MoinMoin.support import copy
11
 
 
12
 
class SegmentTermEnum:
13
 
 
14
 
    def __init__(self, i, fis, isi):
15
 
        self.input = i
16
 
        self.fieldInfos = fis
17
 
        self.size = self.input.readInt()
18
 
        self.isIndex = isi
19
 
 
20
 
        self.indexPointer = 0
21
 
        self.position = -1
22
 
        self.prev = None
23
 
        self.prevTxt = ''
24
 
        self.term = term.Term('','')
25
 
        self.trmInfo = term.TermInfo()
26
 
    
27
 
    
28
 
    def clone(self):
29
 
        """Return a copy of self.
30
 
        """
31
 
        
32
 
        # TODO: implement as __copy__
33
 
        clone = copy.copy(self)
34
 
        clone.input = self.input.clone()
35
 
       
36
 
        clone.trmInfo = term.TermInfo()
37
 
        clone.trmInfo.setTo(self.trmInfo)
38
 
        #clone.prevTxt = self.term.text()
39
 
        return clone
40
 
 
41
 
 
42
 
    def close(self):
43
 
        self.input.close()
44
 
 
45
 
 
46
 
    def docFreq(self):
47
 
        return self.trmInfo.docFreq
48
 
 
49
 
 
50
 
    def freqPointer(self):
51
 
        return self.trmInfo.freqPointer
52
 
 
53
 
 
54
 
    def next(self):
55
 
        self.position += 1
56
 
        
57
 
        if self.position > self.size -1:
58
 
            self.position += 1
59
 
            self.term = None
60
 
            raise StopIteration
61
 
 
62
 
        self.prev = self.term
63
 
        self.term = self.readTerm()
64
 
 
65
 
        self.trmInfo.docFreq = self.input.readVInt()
66
 
        self.trmInfo.freqPointer += self.input.readVLong()
67
 
        self.trmInfo.proxPointer += self.input.readVLong()
68
 
 
69
 
        if self.isIndex:
70
 
            self.indexPointer += self.input.readVLong()
71
 
            
72
 
        return self.term, self.indexPointer
73
 
 
74
 
    def __iter__(self):
75
 
        return self
76
 
 
77
 
    def proxPointer(self):
78
 
        return self.trmInfo.proxPointer
79
 
 
80
 
 
81
 
    def readTerm(self):
82
 
        # this bit is a mite tricky. in the java version they use a
83
 
        # buffer for reading and just use 'start' as the offset for
84
 
        # putting the read string into the buffer; when strings with
85
 
        # common prefixes were read in, the offset would preserve the
86
 
        # prefix. So here we just remember the last string and slice
87
 
        # the common prefix from it.        
88
 
        start = self.input.readVInt()        
89
 
        self.prevTxt = txt = self.prevTxt[:start] + self.input.readString()        
90
 
        fi = self.input.readVInt()
91
 
        fld = self.fieldInfos.fieldName(fi)        
92
 
        t = term.Term(fld,txt,False)
93
 
        return t
94
 
 
95
 
 
96
 
    def seek(self, pointer, p, t, ti):
97
 
        self.input.seek(pointer)
98
 
        self.position = p
99
 
        self.term = t
100
 
        self.prev = None
101
 
        self.trmInfo.setTo(ti)
102
 
        self.prevTxt = self.term.text()
103
 
 
104
 
    def termInfo(self, ti=None):
105
 
        if ti is None:
106
 
            nti = term.TermInfo()
107
 
            nti.setTo(self.trmInfo)
108
 
            return nti
109
 
        else:
110
 
            ti.setTo(self.trmInfo)
111
 
 
112
 
    def __cmp__(a, b):
113
 
        return cmp(a.term, b.term)
114
 
 
115
 
 
116
 
class SegmentInfo(object):
117
 
 
118
 
    def __init__(self, name, docCount, d):
119
 
        self.name = name
120
 
        self.docCount = docCount
121
 
        self.dir = d
122
 
 
123
 
 
124
 
class SegmentInfos(list):
125
 
 
126
 
    def __init__(self, lst = None):
127
 
        self.counter = 0
128
 
        if lst is not None:
129
 
            self.extend(lst)
130
 
    
131
 
    def __getslice__(self, lo, hi):
132
 
        res = SegmentInfos(list.__getslice__(self, lo, hi))
133
 
        res.counter = self.counter
134
 
        return res
135
 
    
136
 
    def read(self, directory):
137
 
        input = directory.openFile('segments')
138
 
        try:
139
 
            self.counter = input.readInt()      # read counter
140
 
            i = input.readInt()
141
 
            while i > 0:                        # read segment infos
142
 
                si = SegmentInfo(input.readString(),
143
 
                                             input.readInt(),
144
 
                                             directory)
145
 
                self.append(si)
146
 
                i -= 1
147
 
        finally:
148
 
            input.close()
149
 
 
150
 
    def write(self, directory):
151
 
        output = directory.createFile('segments.new')
152
 
        try:
153
 
            output.writeInt(self.counter)
154
 
            output.writeInt(len(self))
155
 
            for si in self:
156
 
                output.writeString(si.name)
157
 
                output.writeInt(si.docCount)
158
 
        finally:
159
 
            output.close()
160
 
 
161
 
        # Install new segment info
162
 
        directory.renameFile('segments.new','segments')
163
 
        
164
 
    def __repr__(self):
165
 
        return 'SegInfo' + list.__repr__(self)
166
 
        
167