|
| 1 | +from typing import Dict, Set, Optional, Callable, Union |
| 2 | + |
| 3 | +from ravendb import constants |
| 4 | +from ravendb.documents.conventions import DocumentConventions |
| 5 | +from ravendb.documents.indexes.definitions import ( |
| 6 | + FieldStorage, |
| 7 | + FieldIndexing, |
| 8 | + FieldTermVector, |
| 9 | + IndexDefinition, |
| 10 | + IndexSourceType, |
| 11 | + IndexFieldOptions, |
| 12 | + IndexType, |
| 13 | +) |
| 14 | +from ravendb.documents.indexes.index_creation import AbstractIndexCreationTaskBase, AbstractIndexDefinitionBuilder |
| 15 | +from ravendb.documents.indexes.spatial.configuration import SpatialOptions, SpatialOptionsFactory |
| 16 | + |
| 17 | + |
| 18 | +class CountersIndexDefinition(IndexDefinition): |
| 19 | + @property |
| 20 | + def source_type(self) -> IndexSourceType: |
| 21 | + return IndexSourceType.COUNTERS |
| 22 | + |
| 23 | + |
| 24 | +class AbstractGenericCountersIndexCreationTask(AbstractIndexCreationTaskBase[CountersIndexDefinition]): |
| 25 | + def __init__(self): |
| 26 | + super(AbstractGenericCountersIndexCreationTask, self).__init__() |
| 27 | + self._reduce: Optional[str] = None |
| 28 | + |
| 29 | + self._stores_strings: Dict[str, FieldStorage] = {} |
| 30 | + self._indexes_strings: Dict[str, FieldIndexing] = {} |
| 31 | + self._analyzers_strings: Dict[str, str] = {} |
| 32 | + self._index_suggestions: Set[str] = set() |
| 33 | + self._term_vectors_strings: Dict[str, FieldTermVector] = {} |
| 34 | + self._spatial_options_strings: Dict[str, SpatialOptions] = {} |
| 35 | + |
| 36 | + self._output_reduce_to_collection: Optional[str] = None |
| 37 | + self._pattern_for_output_reduce_to_collection_references: Optional[str] = None |
| 38 | + self._pattern_references_collection_name: Optional[str] = None |
| 39 | + |
| 40 | + @property |
| 41 | + def is_map_reduce(self) -> bool: |
| 42 | + return self._reduce is not None |
| 43 | + |
| 44 | + def _index(self, field: str, indexing: FieldIndexing) -> None: |
| 45 | + self._indexes_strings[field] = indexing |
| 46 | + |
| 47 | + def _spatial(self, field: str, indexing: Callable[[SpatialOptionsFactory], SpatialOptions]) -> None: |
| 48 | + self._spatial_options_strings[field] = indexing(SpatialOptionsFactory()) |
| 49 | + |
| 50 | + def _store_all_fields(self, storage: FieldStorage) -> None: |
| 51 | + self._stores_strings[constants.Documents.Indexing.Fields.ALL_FIELDS] = storage |
| 52 | + |
| 53 | + def _store(self, field: str, storage: FieldStorage) -> None: |
| 54 | + self._stores_strings[field] = storage |
| 55 | + |
| 56 | + def _analyze(self, field: str, analyzer: str) -> None: |
| 57 | + self._analyzers_strings[field] = analyzer |
| 58 | + |
| 59 | + def _term_vector(self, field: str, term_vector: FieldTermVector) -> None: |
| 60 | + self._term_vectors_strings[field] = term_vector |
| 61 | + |
| 62 | + def _suggestion(self, field: str) -> None: |
| 63 | + self._index_suggestions.add(field) |
| 64 | + |
| 65 | + |
| 66 | +class CountersIndexDefinitionBuilder(AbstractIndexDefinitionBuilder[CountersIndexDefinition]): |
| 67 | + def __init__(self, index_name: Optional[str] = None): |
| 68 | + super(CountersIndexDefinitionBuilder, self).__init__(index_name) |
| 69 | + self.map: Optional[str] = None |
| 70 | + |
| 71 | + def _new_index_definition(self) -> CountersIndexDefinition: |
| 72 | + return CountersIndexDefinition() |
| 73 | + |
| 74 | + def to_index_definition( |
| 75 | + self, conventions: DocumentConventions, validate_map: bool = True |
| 76 | + ) -> CountersIndexDefinition: |
| 77 | + if self.map is None and validate_map: |
| 78 | + raise RuntimeError( |
| 79 | + f"Map is required to generate an index, you " |
| 80 | + f"cannot create an index without a valid map property (in index {self._index_name})." |
| 81 | + ) |
| 82 | + |
| 83 | + return super(CountersIndexDefinitionBuilder, self).to_index_definition(conventions, validate_map) |
| 84 | + |
| 85 | + def _to_index_definition(self, index_definition: CountersIndexDefinition, conventions: DocumentConventions) -> None: |
| 86 | + if self.map is None: |
| 87 | + return |
| 88 | + |
| 89 | + index_definition.maps.add(self.map) |
| 90 | + |
| 91 | + |
| 92 | +class AbstractCountersIndexCreationTask(AbstractGenericCountersIndexCreationTask): |
| 93 | + def __init__(self): |
| 94 | + super(AbstractCountersIndexCreationTask, self).__init__() |
| 95 | + self.map: Optional[str] = None |
| 96 | + |
| 97 | + def create_index_definition(self) -> CountersIndexDefinition: |
| 98 | + if self.conventions is None: |
| 99 | + self.conventions = DocumentConventions() |
| 100 | + |
| 101 | + index_definition_builder = CountersIndexDefinitionBuilder(self.index_name) |
| 102 | + index_definition_builder.indexes_strings = self._indexes_strings |
| 103 | + index_definition_builder.analyzers_strings = self._analyzers_strings |
| 104 | + index_definition_builder.map = self.map |
| 105 | + index_definition_builder.reduce = self._reduce |
| 106 | + index_definition_builder.stores_strings = self._stores_strings |
| 107 | + index_definition_builder.suggestions_options = self._index_suggestions |
| 108 | + index_definition_builder.term_vectors_strings = self._term_vectors_strings |
| 109 | + index_definition_builder.spatial_indexes_strings = self._spatial_options_strings |
| 110 | + index_definition_builder.output_reduce_to_collection = self._output_reduce_to_collection |
| 111 | + index_definition_builder.pattern_for_output_reduce_to_collection_references = ( |
| 112 | + self._pattern_for_output_reduce_to_collection_references |
| 113 | + ) |
| 114 | + index_definition_builder.pattern_references_collection_name = self._pattern_references_collection_name |
| 115 | + index_definition_builder.additional_sources = self.additional_sources |
| 116 | + index_definition_builder.additional_assemblies = self.additional_assemblies |
| 117 | + index_definition_builder.configuration = self.configuration |
| 118 | + index_definition_builder.lock_mode = self.lock_mode |
| 119 | + index_definition_builder.priority = self.priority |
| 120 | + index_definition_builder.state = self.state |
| 121 | + index_definition_builder.deployment_mode = self.deployment_mode |
| 122 | + |
| 123 | + return index_definition_builder.to_index_definition(self.conventions) |
| 124 | + |
| 125 | + |
| 126 | +class AbstractJavaScriptCountersIndexCreationTask(AbstractIndexCreationTaskBase[CountersIndexDefinition]): |
| 127 | + def __init__(self): |
| 128 | + super(AbstractJavaScriptCountersIndexCreationTask, self).__init__() |
| 129 | + self._definition = CountersIndexDefinition() |
| 130 | + |
| 131 | + @property |
| 132 | + def maps(self) -> Set[str]: |
| 133 | + return self._definition.maps |
| 134 | + |
| 135 | + @maps.setter |
| 136 | + def maps(self, value: Set[str]): |
| 137 | + self._definition.maps = value |
| 138 | + |
| 139 | + @property |
| 140 | + def fields(self) -> Dict[str, IndexFieldOptions]: |
| 141 | + return self._definition.fields |
| 142 | + |
| 143 | + @fields.setter |
| 144 | + def fields(self, value: Dict[str, IndexFieldOptions]): |
| 145 | + self._definition.fields = value |
| 146 | + |
| 147 | + @property |
| 148 | + def reduce(self) -> str: |
| 149 | + return self._definition.reduce |
| 150 | + |
| 151 | + @reduce.setter |
| 152 | + def reduce(self, value: Dict[str, IndexFieldOptions]): |
| 153 | + self._definition.reduce = value |
| 154 | + |
| 155 | + @property |
| 156 | + def is_map_reduce(self) -> bool: |
| 157 | + return self.reduce is not None |
| 158 | + |
| 159 | + @property |
| 160 | + def output_reduce_to_collection(self) -> str: |
| 161 | + return self._definition.output_reduce_to_collection |
| 162 | + |
| 163 | + @output_reduce_to_collection.setter |
| 164 | + def output_reduce_to_collection(self, value: Dict[str, IndexFieldOptions]): |
| 165 | + self._definition.output_reduce_to_collection = value |
| 166 | + |
| 167 | + @property |
| 168 | + def pattern_references_collection_name(self) -> str: |
| 169 | + return self._definition.pattern_references_collection_name |
| 170 | + |
| 171 | + @pattern_references_collection_name.setter |
| 172 | + def pattern_references_collection_name(self, value: Dict[str, IndexFieldOptions]): |
| 173 | + self._definition.pattern_references_collection_name = value |
| 174 | + |
| 175 | + @property |
| 176 | + def pattern_for_output_reduce_to_collection_references(self) -> str: |
| 177 | + return self._definition.pattern_for_output_reduce_to_collection_references |
| 178 | + |
| 179 | + @pattern_for_output_reduce_to_collection_references.setter |
| 180 | + def pattern_for_output_reduce_to_collection_references(self, value: Dict[str, IndexFieldOptions]): |
| 181 | + self._definition.pattern_for_output_reduce_to_collection_references = value |
| 182 | + |
| 183 | + def create_index_definition(self) -> CountersIndexDefinition: |
| 184 | + self._definition.name = self.index_name |
| 185 | + self._definition.type = IndexType.JAVA_SCRIPT_MAP_REDUCE if self.is_map_reduce else IndexType.JAVA_SCRIPT_MAP |
| 186 | + self._definition.additional_sources = self.additional_sources or {} |
| 187 | + self._definition.additional_assemblies = self.additional_assemblies or set() |
| 188 | + self._definition.configuration = self.configuration |
| 189 | + self._definition.lock_mode = self.lock_mode |
| 190 | + self._definition.priority = self.priority |
| 191 | + self._definition.state = self.state |
| 192 | + self._definition.deployment_mode = self.deployment_mode |
| 193 | + return self._definition |
0 commit comments