-
Notifications
You must be signed in to change notification settings - Fork 477
/
IndexingPolicyDefinition.cs
168 lines (150 loc) · 6.31 KB
/
IndexingPolicyDefinition.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace Microsoft.Azure.Cosmos.Fluent
{
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
/// <summary>
/// Indexing Policy fluent definition.
/// </summary>
/// <seealso cref="IndexingPolicy"/>
public class IndexingPolicyDefinition<T>
{
private readonly IndexingPolicy indexingPolicy = new IndexingPolicy();
private readonly T parent;
private readonly Action<IndexingPolicy> attachCallback;
private PathsDefinition<IndexingPolicyDefinition<T>> includedPathsBuilder;
private PathsDefinition<IndexingPolicyDefinition<T>> excludedPathsBuilder;
/// <summary>
/// Creates an instance for unit-testing
/// </summary>
public IndexingPolicyDefinition()
{
}
internal IndexingPolicyDefinition(
T parent,
Action<IndexingPolicy> attachCallback)
{
this.parent = parent;
this.attachCallback = attachCallback;
}
/// <summary>
/// Defines the <see cref="Container"/>'s <see cref="Cosmos.IndexingMode"/>.
/// </summary>
/// <param name="indexingMode">An <see cref="Cosmos.IndexingMode"/></param>
/// <returns>An instance of <see cref="IndexingPolicyDefinition{T}"/>.</returns>
/// <remarks>
/// If multiple calls are made to this method within the same <see cref="IndexingPolicyDefinition{T}"/>, the last one will apply.
/// </remarks>
public IndexingPolicyDefinition<T> WithIndexingMode(IndexingMode indexingMode)
{
this.indexingPolicy.IndexingMode = indexingMode;
return this;
}
/// <summary>
/// Defines the <see cref="Container"/>'s automatic indexing.
/// </summary>
/// <param name="enabled">Defines whether Automatic Indexing is enabled or not.</param>
/// <returns>An instance of <see cref="IndexingPolicyDefinition{T}"/>.</returns>
public IndexingPolicyDefinition<T> WithAutomaticIndexing(bool enabled)
{
this.indexingPolicy.Automatic = enabled;
return this;
}
/// <summary>
/// Defines the <see cref="Container"/>'s <see cref="IndexingPolicy.IncludedPaths"/>.
/// </summary>
/// <returns>An instance of <see cref="PathsDefinition{T}"/>.</returns>
public PathsDefinition<IndexingPolicyDefinition<T>> WithIncludedPaths()
{
if (this.includedPathsBuilder == null)
{
this.includedPathsBuilder = new PathsDefinition<IndexingPolicyDefinition<T>>(
this,
(paths) => this.AddIncludedPaths(paths));
}
return this.includedPathsBuilder;
}
/// <summary>
/// Defines the <see cref="Container"/>'s <see cref="IndexingPolicy.ExcludedPaths"/>.
/// </summary>
/// <returns>An instance of <see cref="PathsDefinition{T}"/>.</returns>
public PathsDefinition<IndexingPolicyDefinition<T>> WithExcludedPaths()
{
if (this.excludedPathsBuilder == null)
{
this.excludedPathsBuilder = new PathsDefinition<IndexingPolicyDefinition<T>>(
this,
(paths) => this.AddExcludedPaths(paths));
}
return this.excludedPathsBuilder;
}
/// <summary>
/// Defines a Composite Index in the current <see cref="Container"/>'s definition.
/// </summary>
/// <returns>An instance of <see cref="CompositeIndexDefinition{T}"/>.</returns>
public CompositeIndexDefinition<IndexingPolicyDefinition<T>> WithCompositeIndex()
{
return new CompositeIndexDefinition<IndexingPolicyDefinition<T>>(
this,
(compositePaths) => this.AddCompositePaths(compositePaths));
}
/// <summary>
/// Defines a <see cref="Cosmos.SpatialIndex"/> in the current <see cref="Container"/>'s definition.
/// </summary>
/// <returns>An instance of <see cref="SpatialIndexDefinition{T}"/>.</returns>
public SpatialIndexDefinition<IndexingPolicyDefinition<T>> WithSpatialIndex()
{
return new SpatialIndexDefinition<IndexingPolicyDefinition<T>>(
this,
(spatialIndex) => this.AddSpatialPath(spatialIndex));
}
/// <summary>
/// Defines a <see cref="VectorIndexPath"/> in the current <see cref="Container"/>'s definition.
/// </summary>
/// <returns>An instance of <see cref="VectorIndexDefinition{T}"/>.</returns>
internal VectorIndexDefinition<IndexingPolicyDefinition<T>> WithVectorIndex()
{
return new VectorIndexDefinition<IndexingPolicyDefinition<T>>(
this,
(vectorIndex) => this.AddVectorIndexPath(vectorIndex));
}
/// <summary>
/// Applies the current definition to the parent.
/// </summary>
/// <returns>An instance of the parent.</returns>
public T Attach()
{
this.attachCallback(this.indexingPolicy);
return this.parent;
}
private void AddCompositePaths(Collection<CompositePath> compositePaths)
{
this.indexingPolicy.CompositeIndexes.Add(compositePaths);
}
private void AddSpatialPath(SpatialPath spatialSpec)
{
this.indexingPolicy.SpatialIndexes.Add(spatialSpec);
}
private void AddVectorIndexPath(VectorIndexPath vectorIndexPath)
{
this.indexingPolicy.VectorIndexes.Add(vectorIndexPath);
}
private void AddIncludedPaths(IEnumerable<string> paths)
{
foreach (string path in paths)
{
this.indexingPolicy.IncludedPaths.Add(new IncludedPath() { Path = path });
}
}
private void AddExcludedPaths(IEnumerable<string> paths)
{
foreach (string path in paths)
{
this.indexingPolicy.ExcludedPaths.Add(new ExcludedPath() { Path = path });
}
}
}
}