Commit 2fa12536 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent c2d692af
......@@ -1046,9 +1046,9 @@ func (fs *FileStorage) Iterate(tidMin, tidMax zodb.Tid) zodb.IStorageIterator {
return &Iter
}
// ComputeIndex from scratch builds new in-memory index for FileStorage
// computeIndex builds new in-memory index for FileStorage
// XXX naming
func (fs *FileStorage) ComputeIndex(ctx context.Context) (index *Index, err error) {
func (fs *FileStorage) computeIndex(ctx context.Context) (index *Index, err error) {
// TODO err ctx <file>: <reindex>:
// XXX handle ctx cancel
......@@ -1066,7 +1066,7 @@ func (fs *FileStorage) ComputeIndex(ctx context.Context) (index *Index, err erro
loop:
for {
// XXX merge logic infot LoadNext/LoadPrev
// XXX merge logic into LoadNext/LoadPrev
switch txnh.Len {
case -2:
err = txnh.Load(fsSeq, txnh.Pos, LoadNoStrings)
......@@ -1104,3 +1104,75 @@ loop:
}
return index, nil
}
// loadIndex loads on-disk index to RAM
func (fs *FileStorage) loadIndex() error {
// XXX lock?
index, err := LoadIndexFile(fs.file.Name() + ".index")
if err != nil {
return err // XXX err ctx
}
// XXX here?
// TODO verify index sane / topPos matches
if index.TopPos != fs.txnhMax.Pos + fs.txnhMax.Len {
panic("inconsistent index topPos") // XXX
}
fs.index = index
}
// saveIndex flushes in-RAM index to disk
func (fs *FileStorage) saveIndex() error {
// XXX lock?
err = fs.index.SaveFile(fs.file.Name() + ".index")
return err
}
// IndexCorruptError is the error returned when index verification fails
// XXX but io errors during verification return not this
type IndexCorruptError struct {
index *Index
indexOk *Index
}
func (e *IndexCorruptError) Error() string {
// TODO show delta ?
return "index corrupt"
}
// VerifyIndex verifies that index is correct
func (fs *FileStorage) VerifyIndex(ctx context.Context) error {
// XXX lock appends?
// XXX if .index is not yet loaded - load it
indexOk, err := fs.computeIndex(ctx)
if err != nil {
return err // XXX err ctx
}
if !indexOk.Equal(fs.index) {
err = &ErrIndexCorrupt{index: fs.index, indexOk: indexOk}
}
return err
}
// Reindex rebuilds the index
func (fs *FileStorage) Reindex(ctx context.Context) error {
// XXX lock appends?
index, err := fs.computeIndex(ctx)
if err != nil {
return err
}
fs.index = index
err = fs.saveIndex()
return err // XXX ok?
}
......@@ -27,10 +27,32 @@ import (
"log"
)
// XXX text
// Reindex rebuilds index for FileStorage file @ path
func Reindex(path string) error {
// TODO
return nil
// XXX open read-only
fs, err := fs1.Open(contex.Background(), path, fs1.OpenWithoutIndex)
if err != nil {
return nil // XXX err ctx
}
defer fs.Close() // XXX err
err = fs.Reindex()
return err // XXX ok?
}
// VerifyIndexFor verifies that on-disk index for FileStorage file @ path is correct
func VerifyIndexFor(path string) error {
// XXX open read-only
fs, err := fs1.Open(contex.Background(), path, 0)
if err != nil {
return nil // XXX err ctx
}
defer fs.Close() // XXX err
err = fs.VerifyIndex()
return err
//fs.Index()
//fs.ComputeIndex
}
// ----------------------------------------
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment