public void deleteIndex(CMSPriority p, String repoName, String metaclassName, String indexName, MetadataContext context) { logger.info("cms deleteIndex: {}, {}, {}, {}", new Object[] { repoName, metaclassName, indexName, context.getSourceIp() }); checkServerState(p, true); Repository repo = repositoryService.getRepository(repoName); MetaOption options = new MetaOption(); IndexInfo ii = new IndexInfo(indexName); options.addIndex(ii); context.setDbConfig(dbConfig); repo.getMetadataService().updateMetaOption(metaclassName, options, context); updateMetadataIndexOnBranch(metaclassName, context, repo); }
public void addIndex(CMSPriority p, String repoName, String metaclassName, List<IndexInfo> index, MetadataContext context) { String[] indexNames = new String[index.size()]; for (int i = 0; i < index.size(); i++) { indexNames[i] = index.get(i).getIndexName(); } logger.info("cms addIndex: {}, {}, {}, {}", new Object[] { repoName, metaclassName, indexNames, context.getSourceIp() }); checkServerState(p, true); Repository repo = repositoryService.getRepository(repoName); MetaOption options = new MetaOption(); for (IndexInfo ii : index) { options.addIndex(ii); } context.setDbConfig(dbConfig); repo.getMetadataService().updateMetaOption(metaclassName, options, context); try { updateMetadataIndexOnBranch(metaclassName, context, repo); } catch (CmsDalException e) { for (IndexInfo ii : index) { context.setOptionChangeMode(UpdateOptionMode.DELETE); deleteIndex(p, repoName, metaclassName, ii.getIndexName(), context); } throw e; } }
IndexInfo ii = new IndexInfo(HistoryMetaClass.INDEX_NAME); options.addIndex(ii); metaService.updateMetaOption(HistoryMetaClass.NAME, options, context);
context.setSourceIp("127.0.0.1"); context.setOptionChangeMode(UpdateOptionMode.DELETE); metaService.updateMetaOption(metaClass.getName(), newOption, context);
@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 testUpdateAddIndexWithInvalidKey() { MetaClass metaClass = createMetaClass(); metaClass.setName("updateOptionAddInvalidIndex"); MetaAttribute attribute = new MetaAttribute(); attribute.setName("attr_Name2"); attribute.setDataType(DataTypeEnum.STRING); metaClass.addField(attribute); IMetadataService metaService = repositoryService.getRepository(repoName).getMetadataService(); metaService.createMetaClass(metaClass, new MetadataContext()); MetaOption newOption = new MetaOption(); IndexInfo newIndex = new IndexInfo("indexName2"); newIndex.getKeyList().add("invalid_name"); newIndex.addOption(IndexOptionEnum.unique); newOption.addIndex(newIndex); MetadataContext context = new MetadataContext(); context.setSourceIp("127.0.0.1"); context.setOptionChangeMode(UpdateOptionMode.ADD); try { metaService.updateMetaOption(metaClass.getName(), newOption, context); } catch (IndexOptionOperationException e) { Assert.assertTrue(e.getMessage().contains("index indexName2 key list contains non-existing field")); } }
@Test public void testUpdateAddIndex() { MetaClass metaClass = createMetaClass(); metaClass.setName("updateOptionAddIndex"); 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"); 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.ADD); metaService.updateMetaOption(metaClass.getName(), newOption, context); MetaClass getMeta = metaService.getMetaClass(metaClass.getName()); Assert.assertEquals(4, getMeta.getIndexes().size()); Assert.assertTrue(getMeta.getIndexNames().contains(newIndex.getIndexName())); Assert.assertEquals(1, getMeta.getVersion()); }
@Test(expected=IndexExistsException.class) public void testUpdateAddIndexAlreadyExisting() { MetaClass metaClass = createMetaClass(); metaClass.setName("updateOptionAddIndexAlreadyExisitng"); 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"); 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.ADD); metaService.updateMetaOption(metaClass.getName(), newOption, context); }
@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(expected = IndexNotExistsException.class) public void testUpdateRemoveIndexNotExists() { MetaClass metaClass = createMetaClass(); metaClass.setName("removeOptionIndexNotExists"); 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 exsisting 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.DELETE); metaService.updateMetaOption(metaClass.getName(), newOption, context); }