|
| 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 | +) |
| 12 | +from ravendb.documents.indexes.index_creation import AbstractIndexCreationTaskBase, AbstractIndexDefinitionBuilder |
| 13 | +from ravendb.documents.indexes.spatial.configuration import SpatialOptions, SpatialOptionsFactory |
| 14 | + |
| 15 | + |
| 16 | +class CountersIndexDefinition(IndexDefinition): |
| 17 | + @property |
| 18 | + def source_type(self) -> IndexSourceType: |
| 19 | + return IndexSourceType.COUNTERS |
| 20 | + |
| 21 | + |
| 22 | +class AbstractGenericCountersIndexCreationTask(AbstractIndexCreationTaskBase[CountersIndexDefinition]): |
| 23 | + def __init__(self): |
| 24 | + super(AbstractGenericCountersIndexCreationTask, self).__init__() |
| 25 | + self._reduce: Optional[str] = None |
| 26 | + |
| 27 | + self._stores_strings: Dict[str, FieldStorage] = {} |
| 28 | + self._indexes_strings: Dict[str, FieldIndexing] = {} |
| 29 | + self._analyzers_strings: Dict[str, str] = {} |
| 30 | + self._index_suggestions: Set[str] = set() |
| 31 | + self._term_vectors_strings: Dict[str, FieldTermVector] = {} |
| 32 | + self._spatial_options_strings: Dict[str, SpatialOptions] = {} |
| 33 | + |
| 34 | + self._output_reduce_to_collection: Optional[str] = None |
| 35 | + self._pattern_for_output_reduce_to_collection_references: Optional[str] = None |
| 36 | + self._pattern_references_collection_name: Optional[str] = None |
| 37 | + |
| 38 | + @property |
| 39 | + def is_map_reduce(self) -> bool: |
| 40 | + return self._reduce is not None |
| 41 | + |
| 42 | + def _index(self, field: str, indexing: FieldIndexing) -> None: |
| 43 | + self._indexes_strings[field] = indexing |
| 44 | + |
| 45 | + def _spatial(self, field: str, indexing: Callable[[SpatialOptionsFactory], SpatialOptions]) -> None: |
| 46 | + self._spatial_options_strings[field] = indexing(SpatialOptionsFactory()) |
| 47 | + |
| 48 | + def _store_all_fields(self, storage: FieldStorage) -> None: |
| 49 | + self._stores_strings[constants.Documents.Indexing.Fields.ALL_FIELDS] = storage |
| 50 | + |
| 51 | + def _store(self, field: str, storage: FieldStorage) -> None: |
| 52 | + self._stores_strings[field] = storage |
| 53 | + |
| 54 | + def _analyze(self, field: str, analyzer: str) -> None: |
| 55 | + self._analyzers_strings[field] = analyzer |
| 56 | + |
| 57 | + def _term_vector(self, field: str, term_vector: FieldTermVector) -> None: |
| 58 | + self._term_vectors_strings[field] = term_vector |
| 59 | + |
| 60 | + def _suggestion(self, field: str) -> None: |
| 61 | + self._index_suggestions.add(field) |
| 62 | + |
| 63 | + |
| 64 | +class CountersIndexDefinitionBuilder(AbstractIndexDefinitionBuilder[CountersIndexDefinition]): |
| 65 | + def __init__(self, index_name: Optional[str] = None): |
| 66 | + super(CountersIndexDefinitionBuilder, self).__init__(index_name) |
| 67 | + self.map: Optional[str] = None |
| 68 | + |
| 69 | + def _new_index_definition(self) -> CountersIndexDefinition: |
| 70 | + return CountersIndexDefinition() |
| 71 | + |
| 72 | + def to_index_definition( |
| 73 | + self, conventions: DocumentConventions, validate_map: bool = True |
| 74 | + ) -> CountersIndexDefinition: |
| 75 | + if self.map is None and validate_map: |
| 76 | + raise RuntimeError( |
| 77 | + f"Map is required to generate an index, you " |
| 78 | + f"cannot create an index without a valid map property (in index {self._index_name})." |
| 79 | + ) |
| 80 | + |
| 81 | + return super(CountersIndexDefinitionBuilder, self).to_index_definition(conventions, validate_map) |
| 82 | + |
| 83 | + def _to_index_definition(self, index_definition: CountersIndexDefinition, conventions: DocumentConventions) -> None: |
| 84 | + if self.map is None: |
| 85 | + return |
| 86 | + |
| 87 | + index_definition.maps.add(self.map) |
| 88 | + |
| 89 | + |
| 90 | +class AbstractCountersIndexCreationTask(AbstractGenericCountersIndexCreationTask): |
| 91 | + def __init__(self): |
| 92 | + super(AbstractCountersIndexCreationTask, self).__init__() |
| 93 | + self.map: Optional[str] = None |
| 94 | + |
| 95 | + def create_index_definition(self) -> CountersIndexDefinition: |
| 96 | + if self.conventions is None: |
| 97 | + self.conventions = DocumentConventions() |
| 98 | + |
| 99 | + index_definition_builder = CountersIndexDefinitionBuilder(self.index_name) |
| 100 | + index_definition_builder.indexes_strings = self._indexes_strings |
| 101 | + index_definition_builder.analyzers_strings = self._analyzers_strings |
| 102 | + index_definition_builder.map = self.map |
| 103 | + index_definition_builder.reduce = self._reduce |
| 104 | + index_definition_builder.stores_strings = self._stores_strings |
| 105 | + index_definition_builder.suggestions_options = self._index_suggestions |
| 106 | + index_definition_builder.term_vectors_strings = self._term_vectors_strings |
| 107 | + index_definition_builder.spatial_indexes_strings = self._spatial_options_strings |
| 108 | + index_definition_builder.output_reduce_to_collection = self._output_reduce_to_collection |
| 109 | + index_definition_builder.pattern_for_output_reduce_to_collection_references = ( |
| 110 | + self._pattern_for_output_reduce_to_collection_references |
| 111 | + ) |
| 112 | + index_definition_builder.pattern_references_collection_name = self._pattern_references_collection_name |
| 113 | + index_definition_builder.additional_sources = self.additional_sources |
| 114 | + index_definition_builder.additional_assemblies = self.additional_assemblies |
| 115 | + index_definition_builder.configuration = self.configuration |
| 116 | + index_definition_builder.lock_mode = self.lock_mode |
| 117 | + index_definition_builder.priority = self.priority |
| 118 | + index_definition_builder.state = self.state |
| 119 | + index_definition_builder.deployment_mode = self.deployment_mode |
0 commit comments