|
| 1 | +/** |
| 2 | + * Copyright 2020-present Shanghai Jinmu Information Technology Co., Ltd. |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the Server Side Public License, version 1, |
| 6 | + * as published by Shanghai Jinmu Information Technology Co., Ltd.(The name of the development team is Whaleal.) |
| 7 | + * |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * Server Side Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the Server Side Public License |
| 15 | + * along with this program. If not, see |
| 16 | + * <http://www.whaleal.com/licensing/server-side-public-license>. |
| 17 | + * |
| 18 | + * As a special exception, the copyright holders give permission to link the |
| 19 | + * code of portions of this program with the OpenSSL library under certain |
| 20 | + * conditions as described in each individual source file and distribute |
| 21 | + * linked combinations including the program with the OpenSSL library. You |
| 22 | + * must comply with the Server Side Public License in all respects for |
| 23 | + * all of the code used other than as permitted herein. If you modify file(s) |
| 24 | + * with this exception, you may extend this exception to your version of the |
| 25 | + * file(s), but you are not obligated to do so. If you do not wish to do so, |
| 26 | + * delete this exception statement from your version. If you delete this |
| 27 | + * exception statement from all source files in the program, then also delete |
| 28 | + * it in the license file. |
| 29 | + */ |
| 30 | + |
| 31 | +package com.whaleal.mars.core.index; |
| 32 | + |
| 33 | +import com.mongodb.client.model.CollationAlternate; |
| 34 | +import com.mongodb.client.model.CollationCaseFirst; |
| 35 | +import com.mongodb.client.model.CollationMaxVariable; |
| 36 | +import com.mongodb.client.model.CollationStrength; |
| 37 | +import com.whaleal.mars.core.internal.diagnostics.logging.LogFactory; |
| 38 | +import com.whaleal.mars.core.internal.diagnostics.logging.Logger; |
| 39 | +import com.whaleal.mars.session.option.IndexOptions; |
| 40 | +import org.bson.Document; |
| 41 | +import org.bson.conversions.Bson; |
| 42 | + |
| 43 | +import java.util.ArrayList; |
| 44 | +import java.util.List; |
| 45 | +import java.util.Set; |
| 46 | +import java.util.concurrent.TimeUnit; |
| 47 | + |
| 48 | +/** |
| 49 | + * @author wh |
| 50 | + * |
| 51 | + */ |
| 52 | +public class IndexUtil { |
| 53 | + |
| 54 | + private static final Logger LOGGER = LogFactory.getLogger(IndexUtil.class); |
| 55 | + |
| 56 | + public static Index of( Document indexDoc ) { |
| 57 | + Document o = indexDoc; |
| 58 | + |
| 59 | + IndexOptions indexOptions = new IndexOptions(); |
| 60 | + if (o.get("background") != null) { |
| 61 | + |
| 62 | + //todo 具体值可能为多种类型 如 "true" true 1 1.0 "1.0" "5.0" 甚至为 任意字符"xxx"尤其是老版本 情况很多 这里并不能全部列举 |
| 63 | + // C语言决策. C语言编程假定任何非零和非空值为真,并且如果它是零或null,那么它被假定为假值 |
| 64 | + // 主要为 boolean 类型 String 类型 数值类型 |
| 65 | + if (o.get("background") instanceof Boolean) { |
| 66 | + indexOptions.background((Boolean) o.get("background")); |
| 67 | + } else if (o.get("background") instanceof String) { |
| 68 | + indexOptions.background(true); |
| 69 | + } else if (o.get("background") instanceof Number) { |
| 70 | + // 非 0 为真 |
| 71 | + double v; |
| 72 | + try { |
| 73 | + v = Double.valueOf(o.get("background").toString()); |
| 74 | + if (v > 0) { |
| 75 | + indexOptions.background(true); |
| 76 | + } else indexOptions.background(v < 0); |
| 77 | + } catch (Exception e) { |
| 78 | + LOGGER.warn(String.format("Index background Option parse error from index name %s with background value %s ", o.get("name"), o.get("background"))); |
| 79 | + indexOptions.background(true); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + } |
| 84 | + |
| 85 | + if (o.get("unique") != null) { |
| 86 | + indexOptions.unique((Boolean) o.get("unique")); |
| 87 | + } |
| 88 | + if (o.get("name") != null) { |
| 89 | + indexOptions.name((String) o.get("name")); |
| 90 | + } |
| 91 | + |
| 92 | + if (o.get("partialFilterExpression") != null) { |
| 93 | + indexOptions.partialFilterExpression((Bson) o.get("partialFilterExpression")); |
| 94 | + } |
| 95 | + if (o.get("sparse") != null) { |
| 96 | + indexOptions.sparse((Boolean) o.get("sparse")); |
| 97 | + } |
| 98 | + if (o.get("expireAfterSeconds") != null) { |
| 99 | + Long expireAfter = ((Double) Double.parseDouble(o.get("expireAfterSeconds").toString())).longValue(); |
| 100 | + //秒以下会丢失 |
| 101 | + indexOptions.expireAfter(expireAfter, TimeUnit.SECONDS); |
| 102 | + } |
| 103 | + |
| 104 | + if (o.get("hidden") != null) { |
| 105 | + indexOptions.hidden((Boolean) o.get("hidden")); |
| 106 | + } |
| 107 | + |
| 108 | + if (o.get("storageEngine") != null) { |
| 109 | + //不常用到 |
| 110 | + indexOptions.storageEngine((Bson) o.get("storageEngine")); |
| 111 | + } |
| 112 | + |
| 113 | + //---------deal with Collation |
| 114 | + |
| 115 | + if (o.get("collation") != null) { |
| 116 | + com.mongodb.client.model.Collation.Builder collationBuilder = com.mongodb.client.model.Collation.builder(); |
| 117 | + Document collation = (Document) o.get("collation"); |
| 118 | + if (collation.get("locale") != null) { |
| 119 | + collationBuilder.locale(collation.getString("locale")); |
| 120 | + } |
| 121 | + if (collation.get("caseLevel") != null) { |
| 122 | + collationBuilder.caseLevel(collation.getBoolean("caseLevel")); |
| 123 | + } |
| 124 | + if (collation.get("caseFirst") != null) { |
| 125 | + collationBuilder.collationCaseFirst(CollationCaseFirst.fromString(collation.getString("caseFirst"))); |
| 126 | + } |
| 127 | + if (collation.get("strength") != null) { |
| 128 | + collationBuilder.collationStrength(CollationStrength.fromInt( |
| 129 | + ((Double) Double.parseDouble(collation.get("strength").toString())).intValue() |
| 130 | + )); |
| 131 | + } |
| 132 | + if (collation.get("numericOrdering") != null) { |
| 133 | + collationBuilder.numericOrdering(collation.getBoolean("numericOrdering")); |
| 134 | + } |
| 135 | + if (collation.get("alternate") != null) { |
| 136 | + collationBuilder.collationAlternate(CollationAlternate.fromString(collation.getString("alternate"))); |
| 137 | + } |
| 138 | + if (collation.get("maxVariable") != null) { |
| 139 | + collationBuilder.collationMaxVariable(CollationMaxVariable.fromString(collation.getString("maxVariable"))); |
| 140 | + } |
| 141 | + if (collation.get("normalization") != null) { |
| 142 | + collationBuilder.normalization(collation.getBoolean("normalization")); |
| 143 | + } |
| 144 | + if (collation.get("backwards") != null) { |
| 145 | + collationBuilder.backwards(collation.getBoolean("backwards")); |
| 146 | + } |
| 147 | + indexOptions.collation(collationBuilder.build()); |
| 148 | + } |
| 149 | + |
| 150 | + //---------deal with Text |
| 151 | + |
| 152 | + |
| 153 | + if (o.get("weights") != null) { |
| 154 | + indexOptions.weights((Bson) o.get("weights")); |
| 155 | + } |
| 156 | + if (o.get("textIndexVersion") != null) { |
| 157 | + indexOptions.textVersion(((Double) Double.parseDouble(o.get("textIndexVersion").toString())).intValue()); |
| 158 | + } |
| 159 | + if (o.get("default_language") != null) { |
| 160 | + indexOptions.defaultLanguage((String) o.get("default_language")); |
| 161 | + } |
| 162 | + if (o.get("language_override") != null) { |
| 163 | + indexOptions.languageOverride(o.get("language_override").toString()); |
| 164 | + } |
| 165 | + |
| 166 | + //--------deal with wildcard |
| 167 | + |
| 168 | + if (o.get("wildcardProjection") != null) { |
| 169 | + indexOptions.wildcardProjection((Bson) o.get("wildcardProjection")); |
| 170 | + } |
| 171 | + |
| 172 | + //---------deal with geoHaystack |
| 173 | + if (o.get("bucketSize") != null) { |
| 174 | + indexOptions.bucketSize(Double.parseDouble(o.get("bucketSize").toString())); |
| 175 | + } |
| 176 | + //---------deal with 2d |
| 177 | + |
| 178 | + if (o.get("bits") != null) { |
| 179 | + indexOptions.bits(((Double) Double.parseDouble(o.get("bits").toString())).intValue()); |
| 180 | + } |
| 181 | + if (o.get("max") != null) { |
| 182 | + indexOptions.max((Double.parseDouble(o.get("max").toString()))); |
| 183 | + } |
| 184 | + if (o.get("min") != null) { |
| 185 | + indexOptions.min((Double.parseDouble(o.get("min").toString()))); |
| 186 | + } |
| 187 | + |
| 188 | + //---------------deal with 2dsphere |
| 189 | + |
| 190 | + if (o.get("2dsphereIndexVersion") != null) { |
| 191 | + indexOptions.sphereVersion(((Double) Double.parseDouble((o.get("2dsphereIndexVersion").toString()))).intValue()); |
| 192 | + } |
| 193 | + |
| 194 | + //------ let it be backgroud |
| 195 | + indexOptions.background(true); |
| 196 | + |
| 197 | + Document key = (Document) o.get("key"); |
| 198 | + |
| 199 | + Index index = new Index(); |
| 200 | + |
| 201 | + Set< String > strings = key.keySet(); |
| 202 | + |
| 203 | + for (String keyName : strings) { |
| 204 | + index.on(keyName, IndexDirection.fromValue(key.get(keyName))); |
| 205 | + |
| 206 | + } |
| 207 | + index.setOptions(indexOptions); |
| 208 | + |
| 209 | + return index; |
| 210 | + |
| 211 | + } |
| 212 | + |
| 213 | + |
| 214 | + public static List< Index > of( List< Document > indexesDoc ) { |
| 215 | + List< Index > indexes = new ArrayList<>(); |
| 216 | + for (Document doc : indexesDoc) { |
| 217 | + Index index = IndexUtil.of(doc); |
| 218 | + |
| 219 | + indexes.add(index); |
| 220 | + } |
| 221 | + return indexes; |
| 222 | + } |
| 223 | + |
| 224 | + |
| 225 | +} |
0 commit comments