github.com/ravendb/ravendb-go-client@v0.0.0-20240229102137-4474ee7aa0fa/index_definition.go (about)

     1  package ravendb
     2  
     3  type IndexDefinition struct {
     4  	Name              string                        `json:"Name"`
     5  	Priority          IndexPriority                 `json:"Priority,omitempty"`
     6  	LockMode          IndexLockMode                 `json:"LockMode,omitempty"`
     7  	AdditionalSources map[string]string             `json:"AdditionalSources"`
     8  	Maps              []string                      `json:"Maps"`
     9  	Reduce            *string                       `json:"Reduce"`
    10  	Fields            map[string]*IndexFieldOptions `json:"Fields"`
    11  	Configuration     IndexConfiguration            `json:"Configuration"`
    12  	IndexType         IndexType                     `json:"Type"`
    13  	//TBD 4.1  bool testIndex;
    14  	OutputReduceToCollection *string 		`json:"OutputReduceToCollection"`
    15  	PatternReferencesCollectionName *string 	`json:"PatternReferencesCollectionName"`
    16  	PatternForOutputReduceToCollectionReferences *string `json:"PatternForOutputReduceToCollectionReferences"`
    17  }
    18  
    19  func toStrPtr(s string) *string {
    20  	if s == "" {
    21  		return nil
    22  	}
    23  	return &s
    24  }
    25  
    26  func NewIndexDefinition() *IndexDefinition {
    27  	res := &IndexDefinition{
    28  		Configuration: NewIndexConfiguration(),
    29  
    30  		// Note: initializing those is possibly wasteful but
    31  		// it's needed to serialize them like Java, as {} and not null
    32  		Fields:            make(map[string]*IndexFieldOptions),
    33  		AdditionalSources: make(map[string]string),
    34  	}
    35  	return res
    36  }
    37  
    38  func (d *IndexDefinition) GetAdditionalSources() map[string]string {
    39  	if d.AdditionalSources == nil {
    40  		d.AdditionalSources = make(map[string]string)
    41  	}
    42  	return d.AdditionalSources
    43  }
    44  
    45  func (d *IndexDefinition) SetAdditionalSources(additionalSources map[string]string) {
    46  	// preserve additionalSources being always non-nil
    47  	// so that JSON serializes it as {} and not 'null'
    48  	if additionalSources == nil {
    49  		if len(d.AdditionalSources) == 0 {
    50  			return
    51  		}
    52  		additionalSources = make(map[string]string)
    53  	}
    54  	d.AdditionalSources = additionalSources
    55  }
    56  
    57  func (d *IndexDefinition) String() string {
    58  	return d.Name
    59  }
    60  
    61  func (d *IndexDefinition) GetFields() map[string]*IndexFieldOptions {
    62  	if d.Fields == nil {
    63  		d.Fields = make(map[string]*IndexFieldOptions)
    64  	}
    65  	return d.Fields
    66  }
    67  
    68  func (d *IndexDefinition) GetConfiguration() IndexConfiguration {
    69  	if d.Configuration == nil {
    70  		d.Configuration = NewIndexConfiguration()
    71  	}
    72  	return d.Configuration
    73  }
    74  
    75  func (d *IndexDefinition) SetConfiguration(configuration IndexConfiguration) {
    76  	d.Configuration = configuration
    77  }
    78  
    79  // Note: this must be called after finishing building index definition to set IndexType
    80  // In Java it's calculated on demand via getType
    81  func (d *IndexDefinition) updateIndexTypeAndMaps() {
    82  	if d.IndexType == "" || d.IndexType == IndexTypeNone {
    83  		d.IndexType = d.detectStaticIndexType()
    84  	}
    85  	d.Maps = stringArrayRemoveDuplicates(d.Maps)
    86  }
    87  
    88  func (d *IndexDefinition) GetType() IndexType {
    89  	if d.IndexType == "" || d.IndexType == IndexTypeNone {
    90  		d.IndexType = d.detectStaticIndexType()
    91  	}
    92  
    93  	return d.IndexType
    94  }
    95  
    96  func (d *IndexDefinition) SetType(indexType IndexType) {
    97  	if indexType == "" {
    98  		indexType = IndexTypeNone
    99  	}
   100  	d.IndexType = indexType
   101  }
   102  
   103  func (d *IndexDefinition) detectStaticIndexType() IndexType {
   104  	if d.Reduce == nil || stringIsBlank(*d.Reduce) {
   105  		return IndexTypeMap
   106  	}
   107  	return IndexTypeMapReduce
   108  }
   109  
   110  //TBD 4.1  bool isTestIndex()
   111  
   112  //TBD 4.1   setTestIndex(bool testIndex)
   113  
   114  func (d *IndexDefinition) GetOutputReduceToCollection() *string {
   115  	return d.OutputReduceToCollection
   116  }
   117  
   118  func (d *IndexDefinition) SetOutputReduceToCollection(outputReduceToCollection string) {
   119  	d.OutputReduceToCollection = toStrPtr(outputReduceToCollection)
   120  }