1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
|
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package index
// Index format.
//
// An index stored on disk has the format:
//
// "csearch index 1\n"
// list of paths
// list of names
// list of posting lists
// name index
// posting list index
// trailer
//
// The list of paths is a sorted sequence of NUL-terminated file or directory names.
// The index covers the file trees rooted at those paths.
// The list ends with an empty name ("\x00").
//
// The list of names is a sorted sequence of NUL-terminated file names.
// The initial entry in the list corresponds to file #0,
// the next to file #1, and so on. The list ends with an
// empty name ("\x00").
//
// The list of posting lists are a sequence of posting lists.
// Each posting list has the form:
//
// trigram [3]
// deltas [v]...
//
// The trigram gives the 3 byte trigram that this list describes. The
// delta list is a sequence of varint-encoded deltas between file
// IDs, ending with a zero delta. For example, the delta list [2,5,1,1,0]
// encodes the file ID list 1, 6, 7, 8. The delta list [0] would
// encode the empty file ID list, but empty posting lists are usually
// not recorded at all. The list of posting lists ends with an entry
// with trigram "\xff\xff\xff" and a delta list consisting a single zero.
//
// The indexes enable efficient random access to the lists. The name
// index is a sequence of 4-byte big-endian values listing the byte
// offset in the name list where each name begins. The posting list
// index is a sequence of index entries describing each successive
// posting list. Each index entry has the form:
//
// trigram [3]
// file count [4]
// offset [4]
//
// Index entries are only written for the non-empty posting lists,
// so finding the posting list for a specific trigram requires a
// binary search over the posting list index. In practice, the majority
// of the possible trigrams are never seen, so omitting the missing
// ones represents a significant storage savings.
//
// The trailer has the form:
//
// offset of path list [4]
// offset of name list [4]
// offset of posting lists [4]
// offset of name index [4]
// offset of posting list index [4]
// "\ncsearch trailr\n"
import (
"bytes"
"encoding/binary"
"log"
"os"
"path/filepath"
"runtime"
"sort"
)
const (
magic = "csearch index 1\n"
trailerMagic = "\ncsearch trailr\n"
)
// An Index implements read-only access to a trigram index.
type Index struct {
Verbose bool
data mmapData
pathData uint32
nameData uint32
postData uint32
nameIndex uint32
postIndex uint32
numName int
numPost int
}
const postEntrySize = 3 + 4 + 4
func Open(file string) *Index {
mm := mmap(file)
if len(mm.d) < 4*4+len(trailerMagic) || string(mm.d[len(mm.d)-len(trailerMagic):]) != trailerMagic {
corrupt()
}
n := uint32(len(mm.d) - len(trailerMagic) - 5*4)
ix := &Index{data: mm}
ix.pathData = ix.uint32(n)
ix.nameData = ix.uint32(n + 4)
ix.postData = ix.uint32(n + 8)
ix.nameIndex = ix.uint32(n + 12)
ix.postIndex = ix.uint32(n + 16)
ix.numName = int((ix.postIndex-ix.nameIndex)/4) - 1
ix.numPost = int((n - ix.postIndex) / postEntrySize)
return ix
}
// slice returns the slice of index data starting at the given byte offset.
// If n >= 0, the slice must have length at least n and is truncated to length n.
func (ix *Index) slice(off uint32, n int) []byte {
o := int(off)
if uint32(o) != off || n >= 0 && o+n > len(ix.data.d) {
corrupt()
}
if n < 0 {
return ix.data.d[o:]
}
return ix.data.d[o : o+n]
}
// uint32 returns the uint32 value at the given offset in the index data.
func (ix *Index) uint32(off uint32) uint32 {
return binary.BigEndian.Uint32(ix.slice(off, 4))
}
// uvarint returns the varint value at the given offset in the index data.
func (ix *Index) uvarint(off uint32) uint32 {
v, n := binary.Uvarint(ix.slice(off, -1))
if n <= 0 {
corrupt()
}
return uint32(v)
}
// Paths returns the list of indexed paths.
func (ix *Index) Paths() []string {
off := ix.pathData
var x []string
for {
s := ix.str(off)
if len(s) == 0 {
break
}
x = append(x, string(s))
off += uint32(len(s) + 1)
}
return x
}
// NameBytes returns the name corresponding to the given fileid.
func (ix *Index) NameBytes(fileid uint32) []byte {
off := ix.uint32(ix.nameIndex + 4*fileid)
return ix.str(ix.nameData + off)
}
func (ix *Index) str(off uint32) []byte {
str := ix.slice(off, -1)
i := bytes.IndexByte(str, '\x00')
if i < 0 {
corrupt()
}
return str[:i]
}
// Name returns the name corresponding to the given fileid.
func (ix *Index) Name(fileid uint32) string {
return string(ix.NameBytes(fileid))
}
// listAt returns the index list entry at the given offset.
func (ix *Index) listAt(off uint32) (trigram, count, offset uint32) {
d := ix.slice(ix.postIndex+off, postEntrySize)
trigram = uint32(d[0])<<16 | uint32(d[1])<<8 | uint32(d[2])
count = binary.BigEndian.Uint32(d[3:])
offset = binary.BigEndian.Uint32(d[3+4:])
return
}
func (ix *Index) dumpPosting() {
d := ix.slice(ix.postIndex, postEntrySize*ix.numPost)
for i := 0; i < ix.numPost; i++ {
j := i * postEntrySize
t := uint32(d[j])<<16 | uint32(d[j+1])<<8 | uint32(d[j+2])
count := int(binary.BigEndian.Uint32(d[j+3:]))
offset := binary.BigEndian.Uint32(d[j+3+4:])
log.Printf("%#x: %d at %d", t, count, offset)
}
}
func (ix *Index) findList(trigram uint32) (count int, offset uint32) {
// binary search
d := ix.slice(ix.postIndex, postEntrySize*ix.numPost)
i := sort.Search(ix.numPost, func(i int) bool {
i *= postEntrySize
t := uint32(d[i])<<16 | uint32(d[i+1])<<8 | uint32(d[i+2])
return t >= trigram
})
if i >= ix.numPost {
return 0, 0
}
i *= postEntrySize
t := uint32(d[i])<<16 | uint32(d[i+1])<<8 | uint32(d[i+2])
if t != trigram {
return 0, 0
}
count = int(binary.BigEndian.Uint32(d[i+3:]))
offset = binary.BigEndian.Uint32(d[i+3+4:])
return
}
type postReader struct {
ix *Index
count int
offset uint32
fileid uint32
d []byte
restrict []uint32
}
func (r *postReader) init(ix *Index, trigram uint32, restrict []uint32) {
count, offset := ix.findList(trigram)
if count == 0 {
return
}
r.ix = ix
r.count = count
r.offset = offset
r.fileid = ^uint32(0)
r.d = ix.slice(ix.postData+offset+3, -1)
r.restrict = restrict
}
func (r *postReader) max() int {
return int(r.count)
}
func (r *postReader) next() bool {
for r.count > 0 {
r.count--
delta64, n := binary.Uvarint(r.d)
delta := uint32(delta64)
if n <= 0 || delta == 0 {
corrupt()
}
r.d = r.d[n:]
r.fileid += delta
if r.restrict != nil {
i := 0
for i < len(r.restrict) && r.restrict[i] < r.fileid {
i++
}
r.restrict = r.restrict[i:]
if len(r.restrict) == 0 || r.restrict[0] != r.fileid {
continue
}
}
return true
}
// list should end with terminating 0 delta
if r.d != nil && (len(r.d) == 0 || r.d[0] != 0) {
corrupt()
}
r.fileid = ^uint32(0)
return false
}
func (ix *Index) PostingList(trigram uint32) []uint32 {
return ix.postingList(trigram, nil)
}
func (ix *Index) postingList(trigram uint32, restrict []uint32) []uint32 {
var r postReader
r.init(ix, trigram, restrict)
x := make([]uint32, 0, r.max())
for r.next() {
x = append(x, r.fileid)
}
return x
}
func (ix *Index) PostingAnd(list []uint32, trigram uint32) []uint32 {
return ix.postingAnd(list, trigram, nil)
}
func (ix *Index) postingAnd(list []uint32, trigram uint32, restrict []uint32) []uint32 {
var r postReader
r.init(ix, trigram, restrict)
x := list[:0]
i := 0
for r.next() {
fileid := r.fileid
for i < len(list) && list[i] < fileid {
i++
}
if i < len(list) && list[i] == fileid {
x = append(x, fileid)
i++
}
}
return x
}
func (ix *Index) PostingOr(list []uint32, trigram uint32) []uint32 {
return ix.postingOr(list, trigram, nil)
}
func (ix *Index) postingOr(list []uint32, trigram uint32, restrict []uint32) []uint32 {
var r postReader
r.init(ix, trigram, restrict)
x := make([]uint32, 0, len(list)+r.max())
i := 0
for r.next() {
fileid := r.fileid
for i < len(list) && list[i] < fileid {
x = append(x, list[i])
i++
}
x = append(x, fileid)
if i < len(list) && list[i] == fileid {
i++
}
}
x = append(x, list[i:]...)
return x
}
func (ix *Index) PostingQuery(q *Query) []uint32 {
return ix.postingQuery(q, nil)
}
func (ix *Index) postingQuery(q *Query, restrict []uint32) (ret []uint32) {
var list []uint32
switch q.Op {
case QNone:
// nothing
case QAll:
if restrict != nil {
return restrict
}
list = make([]uint32, ix.numName)
for i := range list {
list[i] = uint32(i)
}
return list
case QAnd:
for _, t := range q.Trigram {
tri := uint32(t[0])<<16 | uint32(t[1])<<8 | uint32(t[2])
if list == nil {
list = ix.postingList(tri, restrict)
} else {
list = ix.postingAnd(list, tri, restrict)
}
if len(list) == 0 {
return nil
}
}
for _, sub := range q.Sub {
if list == nil {
list = restrict
}
list = ix.postingQuery(sub, list)
if len(list) == 0 {
return nil
}
}
case QOr:
for _, t := range q.Trigram {
tri := uint32(t[0])<<16 | uint32(t[1])<<8 | uint32(t[2])
if list == nil {
list = ix.postingList(tri, restrict)
} else {
list = ix.postingOr(list, tri, restrict)
}
}
for _, sub := range q.Sub {
list1 := ix.postingQuery(sub, restrict)
list = mergeOr(list, list1)
}
}
return list
}
func mergeOr(l1, l2 []uint32) []uint32 {
var l []uint32
i := 0
j := 0
for i < len(l1) || j < len(l2) {
switch {
case j == len(l2) || (i < len(l1) && l1[i] < l2[j]):
l = append(l, l1[i])
i++
case i == len(l1) || (j < len(l2) && l1[i] > l2[j]):
l = append(l, l2[j])
j++
case l1[i] == l2[j]:
l = append(l, l1[i])
i++
j++
}
}
return l
}
func corrupt() {
log.Fatal("corrupt index: remove " + File())
}
// An mmapData is mmap'ed read-only data from a file.
type mmapData struct {
f *os.File
d []byte
}
// mmap maps the given file into memory.
func mmap(file string) mmapData {
f, err := os.Open(file)
if err != nil {
log.Fatal(err)
}
return mmapFile(f)
}
// File returns the name of the index file to use.
// It is either $CSEARCHINDEX or $HOME/.csearchindex.
func File() string {
f := os.Getenv("CSEARCHINDEX")
if f != "" {
return f
}
var home string
home = os.Getenv("HOME")
if runtime.GOOS == "windows" && home == "" {
home = os.Getenv("USERPROFILE")
}
return filepath.Clean(home + "/.csearchindex")
}
|