github.com/balzaczyy/golucene@v0.0.0-20151210033525-d0be9ee89713/test_framework/baseDirectoryWrapper.go (about)

     1  package test_framework
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/balzaczyy/golucene/core/index"
     6  	"github.com/balzaczyy/golucene/core/store"
     7  )
     8  
     9  // store/BaseDirectoryWrapper.java
    10  
    11  /*
    12  Calls check index on close.
    13  do NOT make any methods in this class synchronized, volatile
    14  do NOT import anything from the concurrency package.
    15  no randoms, no nothing.
    16  */
    17  type BaseDirectoryWrapper interface {
    18  	store.Directory
    19  	IsOpen() bool
    20  }
    21  
    22  type BaseDirectoryWrapperImpl struct {
    23  	isOpen                       bool
    24  	store.Directory              // our delegate in directory
    25  	checkIndexOnClose            bool
    26  	crossCheckTermVectorsOnClose bool
    27  }
    28  
    29  func NewBaseDirectoryWrapper(delegate store.Directory) *BaseDirectoryWrapperImpl {
    30  	return &BaseDirectoryWrapperImpl{
    31  		Directory:                    delegate,
    32  		checkIndexOnClose:            true,
    33  		crossCheckTermVectorsOnClose: true,
    34  	}
    35  }
    36  
    37  func (dw *BaseDirectoryWrapperImpl) Close() error {
    38  	dw.isOpen = false
    39  	if dw.checkIndexOnClose {
    40  		ok, err := index.IsIndexExists(dw)
    41  		if err != nil {
    42  			return err
    43  		}
    44  		if ok {
    45  			CheckIndex(dw, dw.crossCheckTermVectorsOnClose)
    46  		}
    47  	}
    48  	return dw.Directory.Close()
    49  }
    50  
    51  func (dw *BaseDirectoryWrapperImpl) IsOpen() bool {
    52  	return dw.isOpen
    53  }
    54  
    55  func (dw *BaseDirectoryWrapperImpl) String() string {
    56  	return fmt.Sprintf("BaseDirectoryWrapper(%v)", dw.Directory)
    57  }
    58  
    59  func (dw *BaseDirectoryWrapperImpl) ensureOpen() {
    60  	assert2(dw.isOpen, "this Directory is closed")
    61  }