github.com/balzaczyy/golucene@v0.0.0-20151210033525-d0be9ee89713/core/index/model/termsEnum.go (about)

     1  package model
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/balzaczyy/golucene/core/analysis/tokenattributes"
     6  	"github.com/balzaczyy/golucene/core/util"
     7  	"sort"
     8  )
     9  
    10  // TermsEnum.java
    11  /*
    12  Iterator to seek, or step through terms to obtain frequency information, or
    13  for the current term.
    14  
    15  Term enumerations are always ordered by specified Comparator. Each term in the
    16  enumeration is greater than the one before it.
    17  
    18  The TermsEnum is unpositioned when you first obtain it and you must first
    19  succesfully call next() or one of the seek methods.
    20  */
    21  type TermsEnum interface {
    22  	util.BytesRefIterator
    23  
    24  	Attributes() *util.AttributeSource
    25  	/* Attempts to seek to the exact term, returning
    26  	true if the term is found. If this returns false, the
    27  	enum is unpositioned. For some codecs, seekExact may
    28  	be substantially faster than seekCeil. */
    29  	SeekExact(text []byte) (ok bool, err error)
    30  	/* Seeks to the specified term, if it exists, or to the
    31  	next (ceiling) term. Returns SeekStatus to
    32  	indicate whether exact term was found, a different
    33  	term was found, or EOF was hit. The target term may
    34  	be before or after the current term. If this returns
    35  	SeekStatus.END, then enum is unpositioned. */
    36  	SeekCeil(text []byte) SeekStatus
    37  	/* Seeks to the specified term by ordinal (position) as
    38  	previously returned by ord. The target ord
    39  	may be before or after the current ord, and must be
    40  	within bounds. */
    41  	SeekExactByPosition(ord int64) error
    42  	/* Expert: Seeks a specific position by TermState previously obtained
    43  	from termState(). Callers shoudl maintain the TermState to
    44  	use this method. Low-level implementations may position the TermsEnum
    45  	without re-seeking the term dictionary.
    46  
    47  	Seeking by TermState should only be used iff the state was obtained
    48  	from the same instance.
    49  
    50  	NOTE: Using this method with an incompatible TermState might leave
    51  	this TermsEnum in undefiend state. On a segment level
    52  	TermState instances are compatible only iff the source and the
    53  	target TermsEnum operate on the same field. If operating on segment
    54  	level, TermState instances must not be used across segments.
    55  
    56  	NOTE: A seek by TermState might not restore the
    57  	AttributeSource's state. AttributeSource state must be
    58  	maintained separately if the method is used. */
    59  	SeekExactFromLast(text []byte, state TermState) error
    60  	/* Returns current term. Do not call this when enum
    61  	is unpositioned. */
    62  	Term() []byte
    63  	/* Returns ordinal position for current term. This is an
    64  	optional method (the codec may panic). Do not call this
    65  	when the enum is unpositioned. */
    66  	Ord() int64
    67  	/* Returns the number of documentsw containing the current
    68  	term. Do not call this when enum is unpositioned. */
    69  	DocFreq() (df int, err error)
    70  	/* Returns the total numnber of occurrences of this term
    71  	across all documents (the sum of the freq() for each
    72  	doc that has this term). This will be -1 if the
    73  	codec doesn't support this measure. Note that, like
    74  	other term measures, this measure does not take
    75  	deleted documents into account. */
    76  	TotalTermFreq() (tf int64, err error)
    77  	/* Get DocsEnum for the current term. Do not
    78  	call this when the enum is unpositioned. This method
    79  	will not return nil. */
    80  	Docs(liveDocs util.Bits, reuse DocsEnum) (de DocsEnum, err error)
    81  	/* Get DocsEnum for the current term, with
    82  	control over whether freqs are required. Do not
    83  	call this when the enum is unpositioned. This method
    84  	will not return nil. */
    85  	DocsByFlags(liveDocs util.Bits, reuse DocsEnum, flags int) (de DocsEnum, err error)
    86  	/* Get DocsAndPositionEnum for the current term.
    87  	Do not call this when the enum is unpositioned. This
    88  	method will return nil if positions were not
    89  	indexed. */
    90  	DocsAndPositions(liveDocs util.Bits, reuse DocsAndPositionsEnum) DocsAndPositionsEnum
    91  	/* Get DocsAndPositionEnum for the current term,
    92  	with control over whether offsets and payloads are
    93  	required. Some codecs may be able to optimize their
    94  	implementation when offsets and/or payloads are not required.
    95  	Do not call this when the enum is unpositioned. This
    96  	will return nil if positions were not indexed. */
    97  	DocsAndPositionsByFlags(liveDocs util.Bits, reuse DocsAndPositionsEnum, flags int) DocsAndPositionsEnum
    98  	/* Expert: Returns the TermsEnum internal state to position the TermsEnum
    99  	without re-seeking the term dictionary.
   100  
   101  	NOTE: A sek by TermState might not capture the
   102  	AttributeSource's state. Callers must maintain the
   103  	AttributeSource states separately. */
   104  	TermState() (ts TermState, err error)
   105  }
   106  
   107  type SeekStatus int
   108  
   109  const (
   110  	SEEK_STATUS_END       = 1
   111  	SEEK_STATUS_FOUND     = 2
   112  	SEEK_STATUS_NOT_FOUND = 3
   113  )
   114  
   115  type TermsEnumImpl struct {
   116  	TermsEnum
   117  	atts *util.AttributeSource
   118  }
   119  
   120  func NewTermsEnumImpl(self TermsEnum) *TermsEnumImpl {
   121  	return &TermsEnumImpl{self, util.NewAttributeSourceWith(tokenattributes.DEFAULT_ATTRIBUTE_FACTORY)}
   122  }
   123  
   124  func (e *TermsEnumImpl) Attributes() *util.AttributeSource {
   125  	return e.atts
   126  }
   127  
   128  func (e *TermsEnumImpl) SeekExact(text []byte) (ok bool, err error) {
   129  	return e.SeekCeil(text) == SEEK_STATUS_FOUND, nil
   130  }
   131  
   132  func (e *TermsEnumImpl) SeekExactFromLast(text []byte, state TermState) error {
   133  	ok, err := e.SeekExact(text)
   134  	if err != nil {
   135  		return err
   136  	}
   137  	if !ok {
   138  		panic(fmt.Sprintf("term %v does not exist", text))
   139  	}
   140  	return nil
   141  }
   142  
   143  func (e *TermsEnumImpl) Docs(liveDocs util.Bits, reuse DocsEnum) (DocsEnum, error) {
   144  	assert(e != nil)
   145  	return e.DocsByFlags(liveDocs, reuse, DOCS_ENUM_FLAG_FREQS)
   146  }
   147  
   148  func (e *TermsEnumImpl) DocsAndPositions(liveDocs util.Bits, reuse DocsAndPositionsEnum) DocsAndPositionsEnum {
   149  	return e.DocsAndPositionsByFlags(liveDocs, reuse, DOCS_POSITIONS_ENUM_FLAG_OFF_SETS|DOCS_POSITIONS_ENUM_FLAG_PAYLOADS)
   150  }
   151  
   152  func (e *TermsEnumImpl) TermState() (ts TermState, err error) {
   153  	return EMPTY_TERM_STATE, nil
   154  }
   155  
   156  var EMPTY_TERM_STATE = &EmptyTermState{}
   157  
   158  type EmptyTermState struct{}
   159  
   160  func (ts *EmptyTermState) CopyFrom(other TermState) {
   161  	panic("not supported!")
   162  }
   163  
   164  func (ts *EmptyTermState) Clone() TermState {
   165  	return ts
   166  }
   167  
   168  var (
   169  	EMPTY_TERMS_ENUM = &EmptyTermsEnum{}
   170  )
   171  
   172  /* An empty TermsEnum for quickly returning an empty instance e.g.
   173  in MultiTermQuery
   174  Please note: This enum should be unmodifiable,
   175  but it is currently possible to add Attributes to it.
   176  This should not be a problem, as the enum is always empty and
   177  the existence of unused Attributes does not matter. */
   178  type EmptyTermsEnum struct {
   179  	*TermsEnumImpl
   180  }
   181  
   182  func (e *EmptyTermsEnum) SeekCeilUsingCache(term []byte, useCache bool) SeekStatus {
   183  	return SEEK_STATUS_END
   184  }
   185  
   186  func (e *EmptyTermsEnum) SeekExactByPosition(ord int64) error {
   187  	return nil
   188  }
   189  
   190  func (e *EmptyTermsEnum) Term() []byte {
   191  	panic("this method should never be called")
   192  }
   193  
   194  func (e *EmptyTermsEnum) Comparator() sort.Interface {
   195  	return nil
   196  }
   197  
   198  func (e *EmptyTermsEnum) DocFreq() (df int, err error) {
   199  	panic("this method should never be called")
   200  }
   201  
   202  func (e *EmptyTermsEnum) TotalTermFreq() (tf int64, err error) {
   203  	panic("this method should never be called")
   204  }
   205  
   206  func (e *EmptyTermsEnum) Ord() int64 {
   207  	panic("this method should never be called")
   208  }
   209  
   210  func (e *EmptyTermsEnum) DocsByFlags(liveDocs util.Bits, reuse DocsEnum, flags int) (de DocsEnum, err error) {
   211  	panic("this method should never be called")
   212  }
   213  
   214  func (e *EmptyTermsEnum) DocsAndPositionsByFlags(liveDocs util.Bits, reuse DocsAndPositionsEnum, flags int) DocsAndPositionsEnum {
   215  	panic("this method should never be called")
   216  }
   217  
   218  func (e *EmptyTermsEnum) Next() (term []byte, err error) {
   219  	return nil, nil
   220  }
   221  
   222  func (e *EmptyTermsEnum) TermState() (ts TermState, err error) {
   223  	panic("this method should never be called")
   224  }
   225  
   226  func (e *EmptyTermsEnum) SeekExactFromLast(term []byte, state TermState) error {
   227  	panic("this method should never be called")
   228  }