1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.allanbank.mongodb.client.message;
21
22 import com.allanbank.mongodb.ReadPreference;
23 import com.allanbank.mongodb.Version;
24 import com.allanbank.mongodb.bson.Document;
25 import com.allanbank.mongodb.bson.DocumentAssignable;
26 import com.allanbank.mongodb.bson.Element;
27 import com.allanbank.mongodb.bson.NumericElement;
28 import com.allanbank.mongodb.bson.builder.BuilderFactory;
29 import com.allanbank.mongodb.bson.builder.DocumentBuilder;
30 import com.allanbank.mongodb.client.VersionRange;
31
32
33
34
35
36
37
38
39
40
41
42 public class CreateIndexCommand extends Command {
43
44
45
46
47 public static final Version REQUIRED_VERSION = Version.VERSION_2_6;
48
49
50 public static final VersionRange REQUIRED_VERSION_RANGE = VersionRange
51 .minimum(REQUIRED_VERSION);
52
53
54
55
56
57
58
59
60 public static String buildIndexName(final Element... keys) {
61 final StringBuilder nameBuilder = new StringBuilder();
62 for (final Element key : keys) {
63 if (nameBuilder.length() > 0) {
64 nameBuilder.append('_');
65 }
66 nameBuilder.append(key.getName().replace(' ', '_'));
67 nameBuilder.append("_");
68 if (key instanceof NumericElement) {
69 nameBuilder.append(((NumericElement) key).getIntValue());
70 }
71 else {
72 nameBuilder.append(key.getValueAsString());
73 }
74 }
75 return nameBuilder.toString();
76 }
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93 private static Document buildCommand(final String collectionName,
94 final Element[] keys, final String indexName,
95 final DocumentAssignable options) {
96 final DocumentBuilder builder = BuilderFactory.start();
97
98 builder.add("createIndexes", collectionName);
99
100 final DocumentBuilder index = builder.pushArray("indexes").push();
101 final DocumentBuilder keysDoc = index.push("key");
102 for (final Element key : keys) {
103 keysDoc.add(key);
104 }
105
106 index.add("name", indexName == null ? buildIndexName(keys) : indexName);
107
108 if (options != null) {
109 for (final Element option : options.asDocument()) {
110 index.add(option);
111 }
112 }
113
114 return builder.build();
115 }
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133 public CreateIndexCommand(final String databaseName,
134 final String collectionName, final Element[] keys,
135 final String indexName, final DocumentAssignable options) {
136 super(databaseName, collectionName, buildCommand(collectionName, keys,
137 indexName, options), ReadPreference.PRIMARY,
138 REQUIRED_VERSION_RANGE);
139 }
140
141
142
143
144
145
146
147
148
149
150 @Override
151 public boolean equals(final Object object) {
152 boolean result = false;
153 if (this == object) {
154 result = true;
155 }
156 else if ((object != null) && (getClass() == object.getClass())) {
157 result = super.equals(object);
158 }
159 return result;
160 }
161
162
163
164
165
166
167 @Override
168 public int hashCode() {
169 int result = 1;
170 result = (31 * result) + super.hashCode();
171 return result;
172 }
173 }