private DBObject buildIndexOptionObject(IndexInfo index) { DBObject optionsObject = new BasicDBObject(); for (IndexOptionEnum option : index.getIndexOptions()) { optionsObject.put(option.name(), true); } optionsObject.put("name", index.getIndexName()); optionsObject.put("background", "true"); return optionsObject; }
public final void dropIndex(IndexInfo index) { for (String fieldName : index.getKeyList()) { if(fieldToIndexesMap.containsKey(fieldName)){ fieldToIndexesMap.remove(fieldName); } } indexes.remove(index.getIndexName()); }
@Test public void testAddIndex() { IndexInfo index = new IndexInfo("ut_appTierIndex2_cmsServer"); index.addKeyField("appId"); index.addKeyField("name"); index.addOption(IndexOptionEnum.unique); List<IndexInfo> indexes = new ArrayList<IndexInfo>(); indexes.add(index); metaContext.setOptionChangeMode(UpdateOptionMode.ADD); server.addIndex(CMSPriority.NON_CRITICAL, RAPTOR_PAAS, APPLICATION_SERVICE, indexes, metaContext); }
@Override protected IndexInfo readObject(JsonNode node, String fieldName) throws IOException, JsonParseException, JsonMappingException { IndexInfo f; f = mapper.readValue(node, IndexInfo.class); if (f.getIndexName() == null) { f.setIndexName(fieldName); } return f; }
@Override public void serialize(Collection<IndexInfo> values, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { Map<String, IndexInfo> target = new HashMap<String, IndexInfo>(); for (IndexInfo v : values) { if (!v.isInternal()) { target.put(v.getIndexName(), v); } } jgen.writeObject(target); } }
final int oldSize = info.getKeyList().size(); Assert.assertTrue(oldIndexes.containsKey(info.getIndexName())); Assert.assertEquals(((DBObject)oldIndexes.get(info.getIndexName()).get("key")).keySet().size(), info.getKeyList().size()); info.addKeyField("_oid"); final int newSize = info.getKeyList().size(); persistenceService.ensureIndex(metas, raptorContext, true); Map<String, DBObject> newIndexes = getCollectionIndexMap(metaClass); Assert.assertTrue(newIndexes.containsKey(info.getIndexName())); Assert.assertEquals(((DBObject) newIndexes.get(info.getIndexName()).get("key")).keySet().size(), info .getKeyList().size()); Assert.assertEquals(oldSize + 1, newSize); info.getKeyList().remove("_oid"); final int newSize = info.getKeyList().size(); persistenceService.ensureIndex(metas, raptorContext, true); Map<String, DBObject> newIndexes = getCollectionIndexMap(metaClass); Assert.assertTrue(newIndexes.containsKey(info.getIndexName())); Assert.assertEquals(((DBObject) newIndexes.get(info.getIndexName()).get("key")).keySet().size(), info .getKeyList().size()); Assert.assertEquals(oldSize, newSize); info.addOption(IndexOptionEnum.sparse); persistenceService.ensureIndex(metas, raptorContext, true); Map<String, DBObject> new2Indexes = getCollectionIndexMap(metaClass); Assert.assertTrue(new2Indexes.containsKey(info.getIndexName())); DBObject indexDbo = new2Indexes.get(info.getIndexName()); for (IndexOptionEnum ioe : info.getIndexOptions()) { Assert.assertTrue(indexDbo.containsField(ioe.name().toLowerCase()));
@Test public void testUpdateChangeIndex() { MetaClass metaClass = createMetaClass(); metaClass.setName("updateOptionChangeIndex"); MetaAttribute attribute = new MetaAttribute(); attribute.setName("attr_Name2"); attribute.setDataType(DataTypeEnum.STRING); metaClass.addField(attribute); IndexInfo index = new IndexInfo("indexName2"); index.getKeyList().add(attribute.getName()); metaClass.addIndex(index); IMetadataService metaService = repositoryService.getRepository(repoName).getMetadataService(); metaService.createMetaClass(metaClass, new MetadataContext()); MetaOption newOption = new MetaOption(); IndexInfo newIndex = new IndexInfo("indexName2"); newIndex.getKeyList().add(attribute.getName()); newIndex.addOption(IndexOptionEnum.unique); newOption.addIndex(newIndex); MetadataContext context = new MetadataContext(); context.setSourceIp("127.0.0.1"); context.setOptionChangeMode(UpdateOptionMode.UPDATE); metaService.updateMetaOption(metaClass.getName(), newOption, context); MetaClass getMeta = metaService.getMetaClass(metaClass.getName()); Assert.assertEquals(3, getMeta.getIndexes().size());// check no new index added Assert.assertTrue(getMeta.getIndexByName(index.getIndexName()).getIndexOptions().size() == 1); Assert.assertTrue(getMeta.getIndexByName(index.getIndexName()).getIndexOptions().get(0) == IndexOptionEnum.unique); }
@Test public void testAddEnsureIndex() { IMetadataService metaService = repoService.getRepository(RAPTOR_REPO).getMetadataService(); MetaClass metaClass = metaService.getMetaClass(indexMetaName); if (metaClass == null) { metaClass = createMetaClassWithIndex(indexMetaName, idxName, metaService); } IndexInfo indexInfo = new IndexInfo("addedIndex"); indexInfo.addKeyField(metaClass.getFieldNames().iterator().next()); indexInfo.addOption(IndexOptionEnum.unique); metaClass.addIndex(indexInfo); List<MetaClass> metas = new ArrayList<MetaClass>(); metas.add(metaClass); persistenceService.ensureIndex(metas, raptorContext, true); Assert.assertTrue(getCollectionIndexMap(metaClass).containsKey(indexInfo.getIndexName())); }
@Test(expected = IndexNotExistsException.class) public void testUpdateChangeIndexNotExists() { MetaClass metaClass = createMetaClass(); metaClass.setName("updateOptionChangeIndexNotExists"); MetaAttribute attribute = new MetaAttribute(); attribute.setName("attr_Name2"); attribute.setDataType(DataTypeEnum.STRING); metaClass.addField(attribute); IndexInfo index = new IndexInfo("indexName2"); index.getKeyList().add(attribute.getName()); metaClass.addIndex(index); IMetadataService metaService = repositoryService.getRepository(repoName).getMetadataService(); metaService.createMetaClass(metaClass, new MetadataContext()); MetaOption newOption = new MetaOption(); IndexInfo newIndex = new IndexInfo("indexName3");//an not existing index index.getKeyList().add(attribute.getName()); index.addOption(IndexOptionEnum.unique); newOption.addIndex(newIndex); MetadataContext context = new MetadataContext(); context.setSourceIp("127.0.0.1"); context.setOptionChangeMode(UpdateOptionMode.UPDATE); metaService.updateMetaOption(metaClass.getName(), newOption, context); }
@Test public void overrideIndex() { final String idxName = "descriptionOidIndex"; IndexInfo parentIndex = new IndexInfo(idxName); parentIndex.addKeyField("description"); parentIndex.addKeyField("_oid"); parentIndex.addOption(IndexOptionEnum.sparse); List<IndexInfo> indexes1 = new ArrayList<IndexInfo>(); indexes1.add(parentIndex); IndexInfo newIndex = new IndexInfo(); newIndex.setIndexName(idxName); newIndex.addKeyField("description"); newIndex.addKeyField("_oid"); newIndex.addKeyField("label"); server.addIndex(CMSPriority.NON_CRITICAL, STRATUS_REPO, ENVIRONMENT, Arrays.asList(newIndex), metaContext); Assert.assertEquals(parentIndex.getKeyList().size() + 1, overIndex.getKeyList().size()); Assert.assertTrue(overIndex.getKeyList().contains("description")); Assert.assertTrue(overIndex.getKeyList().contains("_oid")); Assert.assertTrue(overIndex.getKeyList().contains("label")); Assert.assertFalse(overIndex.getIndexOptions().contains(IndexOptionEnum.sparse)); Assert.assertFalse(overIndex.getIndexOptions().contains(IndexOptionEnum.unique));
context.setOptionChangeMode(UpdateOptionMode.DELETE); MetaOption options = new MetaOption(); IndexInfo ii = new IndexInfo(HistoryMetaClass.INDEX_NAME); options.addIndex(ii); metaService.updateMetaOption(HistoryMetaClass.NAME, options, context); Assert.assertEquals(2, index.getKeyList().size()); Assert.assertTrue(index.getKeyList().contains(HistoryMetaClass.EntityId)); Assert.assertTrue(index.getKeyList().contains(HistoryMetaClass.EntityVersion));
private DBObject buildIndexKeyObject(MetaClass metadata, IndexInfo index, boolean onMainBranch) { DBObject keyObject = new BasicDBObject(); // in sharded case. sharding key must be prefix of the unique key boolean hashed = index.getIndexOptions().contains(IndexOptionEnum.hashed); for (String key : index.getKeyList()) { String[] keySeg = key.split("\\."); StringBuilder sb = new StringBuilder(); getKeyName(metadata, keySeg, 0, sb); keyObject.put(sb.toString(), hashed ? "hashed" : 1); } //since objects in different sub branches are in the same collection, // need to add the branch id into the index. // so the objects on different sub branches will not conflict on "unique index" if (!onMainBranch) { keyObject.put(InternalFieldEnum.BRANCH.getDbName(), 1); } return keyObject; }
@Test(expected = IndexExistsException.class) public void testMetaClassCreationDuplicateIndex() { MetaClass metaClass = createMetaClass(); metaClass.setName("metaWithIndex2"); MetaAttribute attribute = new MetaAttribute(); attribute.setName("attr_Name2"); attribute.setDataType(DataTypeEnum.STRING); metaClass.addField(attribute); IndexInfo index = new IndexInfo("indexName2"); index.getKeyList().add(attribute.getName()); metaClass.addIndex(index); IMetadataService metaService = repositoryService.getRepository(repoName).getMetadataService(); metaService.createMetaClass(metaClass, new MetadataContext()); MetaClass subMetaClass = createMetaClass(); subMetaClass.setName("subMetaClass2"); subMetaClass.setParent(metaClass.getName()); MetaAttribute subAttr = new MetaAttribute(); subAttr.setName("subAttribute2"); subAttr.setDataType(DataTypeEnum.DOUBLE); subMetaClass.addField(subAttr); IndexInfo subIndex = new IndexInfo("indexName2"); subIndex.addKeyField(attribute.getName()); subMetaClass.addIndex(subIndex); metaService.createMetaClass(subMetaClass, new MetadataContext()); }
protected void appendSetCommand(BasicDBObject query, BasicDBObject ups, BasicDBObject indexOptions, IndexInfo index, boolean update) { String fieldKey = "options.indexes." + index.getIndexName(); query.append(fieldKey, new BasicDBObject(MongoOperand.exists, update)); ups.append(fieldKey, indexOptions.get(index.getIndexName())); }
attribute.setDataType(DataTypeEnum.STRING); metaClass.addField(attribute); IndexInfo index = new IndexInfo("indexName1"); index.getKeyList().add(attribute.getName()); metaClass.addIndex(index); subMetaClass.addField(subAttr); IndexInfo subIndex = new IndexInfo("subIndex"); subIndex.addKeyField(subAttr.getName()); subMetaClass.addIndex(subIndex); Set<String> getIndexNames = new HashSet<String>(); for (IndexInfo getIndex : getIndexes) { getIndexNames.add(getIndex.getIndexName()); Assert.assertTrue(getIndexNames.contains(index.getIndexName())); Assert.assertTrue(getIndexNames.contains(subIndex.getIndexName()));
@Test public void testMetaClassCreationWithIndex() { MetaClass metaClass = createMetaClass(); metaClass.setName("metaWithIndex"); MetaAttribute attribute = new MetaAttribute(); attribute.setName("attr_Name"); attribute.setDataType(DataTypeEnum.STRING); metaClass.addField(attribute); IndexInfo index = new IndexInfo("indexName"); index.getKeyList().add(attribute.getName()); metaClass.addIndex(index); IMetadataService metaService = repositoryService.getRepository(repoName).getMetadataService(); metaService.createMetaClass(metaClass, new MetadataContext()); MetaClass getMeta = metaService.getMetaClass(metaClass.getName()); Assert.assertNotNull(getMeta); Assert.assertTrue(getMeta.getFields().size() > 2); Assert.assertNotNull(getMeta.getIndexes()); Assert.assertTrue(getMeta.getIndexes().size() == 3); Collection<IndexInfo> getIndexes = getMeta.getIndexes(); Set<String> getIndexNames = new HashSet<String>(); for (IndexInfo getIndex : getIndexes) { getIndexNames.add(getIndex.getIndexName()); } Assert.assertTrue(getIndexNames.contains(index.getIndexName())); Assert.assertEquals(1, getMeta.getIndexesOnField(attribute.getName()).size()); }
public void addReferenceIndexes() { Collection<MetaField> fields = getClassFields(); for (MetaField field : fields) { if (field.getDataType() == DataTypeEnum.RELATIONSHIP && ((MetaRelationship) field).getRelationType() != RelationTypeEnum.Embedded) { String indexName = IndexInfo.INDEX_PREFIX + field.getName() + IndexInfo.INDEX_POSTFIX; IndexInfo relationshipIndex = new IndexInfo(indexName, true); relationshipIndex.addKeyField(field.getName()); options.addIndex(relationshipIndex); } } }
Collection<IndexInfo> newIndexes = newOption.getIndexes(); for (IndexInfo ii : newIndexes) { IndexInfo newIndex = new IndexInfo(ii.getIndexName()); tempOption.addIndex(newIndex); Map<String, IndexInfo> oldIndexMap = new HashMap<String, IndexInfo>(); for (IndexInfo oldi : oldIndexes) { oldIndexMap.put(oldi.getIndexName(), oldi); checkIndexSize(metaClass, newOption, tempGraph); for (IndexInfo ii : newIndexes) { if (ii.isInternal()) { continue; if (oldIndexMap.containsKey(ii.getIndexName())) { sb.append(MessageFormat.format("index {0} already exsits on metaclass {1}!", ii.getIndexName(), metaClass.getName())).append("\n"); ii.validate(); validateKeyList(metaClass, ii); checkIndexSize(metaClass, newOption, tempGraph); for (IndexInfo ii : newIndexes) { if (ii.isInternal()) { continue; ii.validate(); validateKeyList(metaClass, ii); if (ii.isInternal()) {
@Test public void testAddIndexOnParent() { IndexInfo index = new IndexInfo("descriptionIndex"); index.addKeyField("description"); List<IndexInfo> indexes = new ArrayList<IndexInfo>(); indexes.add(index); metaContext.setOptionChangeMode(UpdateOptionMode.ADD); server.addIndex(CMSPriority.NON_CRITICAL, STRATUS_REPO, "Base", indexes, metaContext); // case 1 : the meta and all its existing metadata indexes are loaded IMetadataService metaService = server.getRepository(CMSPriority.NEUTRAL, STRATUS_REPO).getMetadataService(); MetaClass meta = metaService.getMetaClass("Base"); checkIndexesLoaded(metaService, meta); // also make sure indexes are added for descendants for (MetaClass descMeta : meta.getDescendants()) { checkIndexesLoaded(metaService, descMeta); } // case 2: all newly create meta after the index will have indexes // built. MetaClass newChild = new MetaClass(); newChild.setName("newChildMeta"); newChild.setRepository(meta.getRepository()); newChild.setParent("Base"); server.batchUpsert(CMSPriority.NEUTRAL, STRATUS_REPO, Arrays.asList(newChild), metaContext); MetaClass getChild = server.getMetaClass(CMSPriority.NEUTRAL, STRATUS_REPO, newChild.getName()); Assert.assertTrue(getChild.getIndexNames().contains(index.getIndexName())); checkIndexesLoaded(metaService, getChild); }
while (indexIter.hasNext()) { IndexInfo indexInfo = indexIter.next(); if (indexInfo.isInternal()) { continue; if (existingMetadata.getIndexByName(indexInfo.getIndexName()) != null) { indexIter.remove(); continue; for (String idxField : indexInfo.getKeyList()) { if (!fieldNames.contains(idxField)) { throw new MetaDataException(MetaErrCodeEnum.META_FIELD_NOT_EXISTS, MessageFormat.format("index {0} has field {1} that doesn''t exsiting", indexInfo.getIndexName(), idxField)); if (existingMetadata.getIndexByName(indexInfo.getIndexName()) != null) { throw new IndexExistsException(MessageFormat.format("index name {0} already exsits. Can not update index options with metaclass update", indexInfo.getIndexName()));