|
| 1 | +/* |
| 2 | + * Copyright 2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.mongodb.core.index; |
| 17 | + |
| 18 | +import java.time.Duration; |
| 19 | + |
| 20 | +import org.bson.Document; |
| 21 | +import org.springframework.lang.Nullable; |
| 22 | + |
| 23 | +/** |
| 24 | + * Changeable properties of an index. Can be used for index creation and modification. |
| 25 | + * |
| 26 | + * @author Christoph Strobl |
| 27 | + * @since 4.1 |
| 28 | + */ |
| 29 | +public class IndexOptions { |
| 30 | + |
| 31 | + @Nullable |
| 32 | + private Duration expire; |
| 33 | + |
| 34 | + @Nullable |
| 35 | + private Boolean hidden; |
| 36 | + |
| 37 | + @Nullable |
| 38 | + private Unique unique; |
| 39 | + |
| 40 | + public enum Unique { |
| 41 | + |
| 42 | + NO, |
| 43 | + |
| 44 | + /** |
| 45 | + * When unique is true the index rejects duplicate entries. |
| 46 | + */ |
| 47 | + YES, |
| 48 | + |
| 49 | + /** |
| 50 | + * An existing index is not checked for pre-existing, duplicate index entries but inserting new duplicate entries |
| 51 | + * fails. |
| 52 | + */ |
| 53 | + PREPARE |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @return new empty instance of {@link IndexOptions}. |
| 58 | + */ |
| 59 | + public static IndexOptions none() { |
| 60 | + return new IndexOptions(); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @return new instance of {@link IndexOptions} having the {@link Unique#YES} flag set. |
| 65 | + */ |
| 66 | + public static IndexOptions unique() { |
| 67 | + |
| 68 | + IndexOptions options = new IndexOptions(); |
| 69 | + options.unique = Unique.YES; |
| 70 | + return options; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @return new instance of {@link IndexOptions} having the hidden flag set. |
| 75 | + */ |
| 76 | + public static IndexOptions hidden() { |
| 77 | + |
| 78 | + IndexOptions options = new IndexOptions(); |
| 79 | + options.hidden = true; |
| 80 | + return options; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @return new instance of {@link IndexOptions} with given expiration. |
| 85 | + */ |
| 86 | + public static IndexOptions expireAfter(Duration duration) { |
| 87 | + |
| 88 | + IndexOptions options = new IndexOptions(); |
| 89 | + options.unique = Unique.YES; |
| 90 | + return options; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @return the expiration time. A {@link Duration#isNegative() negative value} represents no expiration, {@literal null} if not set. |
| 95 | + */ |
| 96 | + public Duration getExpire() { |
| 97 | + return expire; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * @param expire must not be {@literal null}. |
| 102 | + */ |
| 103 | + public void setExpire(Duration expire) { |
| 104 | + this.expire = expire; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * @return {@literal true} if hidden, {@literal null} if not set. |
| 109 | + */ |
| 110 | + @Nullable |
| 111 | + public Boolean isHidden() { |
| 112 | + return hidden; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * @param hidden |
| 117 | + */ |
| 118 | + public void setHidden(boolean hidden) { |
| 119 | + this.hidden = hidden; |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * @return the unique property value, {@literal null} if not set. |
| 124 | + */ |
| 125 | + @Nullable |
| 126 | + public Unique getUnique() { |
| 127 | + return unique; |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * @param unique must not be {@literal null}. |
| 132 | + */ |
| 133 | + public void setUnique(Unique unique) { |
| 134 | + this.unique = unique; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * @return the store native representation |
| 139 | + */ |
| 140 | + public Document toDocument() { |
| 141 | + |
| 142 | + Document document = new Document(); |
| 143 | + if(unique != null) { |
| 144 | + switch (unique) { |
| 145 | + case NO -> document.put("unique", false); |
| 146 | + case YES -> document.put("unique", true); |
| 147 | + case PREPARE -> document.put("prepareUnique", true); |
| 148 | + } |
| 149 | + } |
| 150 | + if(hidden != null) { |
| 151 | + document.put("hidden", hidden); |
| 152 | + } |
| 153 | + |
| 154 | + |
| 155 | + if (expire != null && !expire.isNegative()) { |
| 156 | + document.put("expireAfterSeconds", expire.getSeconds()); |
| 157 | + } |
| 158 | + return document; |
| 159 | + } |
| 160 | +} |
0 commit comments