@Override public Repository createRepository(Repository repo) { return createRepository(repo, WriteConcern.SAFE); }
private MetaClass getMetaClassFromDb(DBObject query) { DBObject object = collection.findOne(query); if (object == null) { return null; } MetaClass m = converter.fromBson(object, MetaClass.class); setUpMetaClass(m, graph); cacheManager.addMetaClassToCache(m); return m; }
private void appendCommand(BasicDBObject query, BasicDBObject ups, BasicDBObject indexOptions, IndexInfo index, MetadataContext context) { switch (context.getOptionChangeMode()) { case ADD: appendSetCommand(query, ups, indexOptions, index, false); break; case UPDATE: appendSetCommand(query, ups, indexOptions, index, true); break; case DELETE: appendUnsetCommand(query, ups, indexOptions, index); break; default: throw new IllegalStateException("Illegal update mode for metaclass option change"); } }
@Override public final List<MetaClass> getMetaClasses(MetadataContext ctx) { MetadataContext context = ctx != null ? ctx : new MetadataContext(); ArrayList<MetaClass> result = new ArrayList<MetaClass>(); if (!context.isRefreshMetadata()) { return cacheManager.getMetaClassesFromCache(); } DBCursor cursor = collection.find(); while(cursor.hasNext()) { DBObject o = cursor.next(); MetaClass m = converter.fromBson(o, MetaClass.class); result.add(m); } MetaClassGraph newGraph = new MetaClassGraph(result); ExpirableCache<MetaClass> newNameCache = new ExpirableCache<MetaClass>(maxCacheSize, cacheExpiredTime); ExpirableCache<MetaClass> pluralNameCache = new ExpirableCache<MetaClass>(maxCacheSize, cacheExpiredTime); for (MetaClass m : result) { setUpMetaClass(m, newGraph); cacheManager.addMetaClassToCache(m, newNameCache, pluralNameCache); newGraph.updateMetaClass(m); } // ## this might still not thread safe between the cache and graphs. But the time window should be small enough this.graph = newGraph; cacheManager.refreshCache(newNameCache, pluralNameCache); return result; }
@Override public void updateMetaOption(String className, MetaOption options, MetadataContext context) { CheckConditions.checkNotNull(context.getOptionChangeMode(), "option update mode must specify when update metaclass options"); CheckConditions.checkNotNull(options, "option can not be null when update metaclass options"); MetaClass targetMetadata = getMetaClass(className); if (targetMetadata == null) { throw new IllegalMetaClassException("can not find meta data with name " + className); } // validation here: add/update/delete MetadataOptionValidator validator = new MetadataOptionValidator(options, context); targetMetadata.traverse(validator); // checkIndexSize(className, options, targetMetadata); IMetadataCommand command = new UpdateOptionCommand(targetMetadata, options, this.collection); command.execute(context); addMetaHistory(targetMetadata, "updateMetaOption", context); cacheManager.deleteMetaClassFromCache(targetMetadata); MetaClass result = getMetaClass(targetMetadata.getName()); graph.updateMetaClass(result); }
@Override public MetaClass updateMetaField(MetaClass metaClass, String fieldName, MetadataContext context) { try { metadataLock.lock(); MetaClass existingMeta = getMetaClass(metaClass.getName()); checkMetaClassVersion(metaClass); metaClass.setMetadataService(this); Map<String, MetaClass> metas = prepareMetaClassMap(Arrays.asList(metaClass)); validator.validateForUpdateField(metaClass, metas, fieldName); cacheManager.deleteMetaClassFromCache(metaClass); graph.deleteMetaClass(metaClass); MetaClass meta = innerUpdateMetaClass(metaClass, true, true); addMetaHistory(existingMeta, "updateMetaField", context); return meta; } catch (InterruptedException e) { logger.info("lock interrupted for updateSingleMetaField"); throw new MetaDataException(MetaErrCodeEnum.LOCK_INTERRUPTED, "lock interrupted for updateSingleMetaField ", e); } finally { metadataLock.unlock(); } }
@Override public MetaClass getMetaClass(String className, int version, MetadataContext context) { MetaClass m = getMetaClass(className); if (m == null || version < 0 || version == m.getVersion()) { return m; } if (historyService != null) { m = historyService.getHistory(repo.getRepositoryName(), className, version, context); if (m != null) { m.setMetadataService(this); setUpMetaClass(m, graph); } return m; } return null; }
private MetaClass innerDropMetaField(MetaClass metaClass, String fieldName) { DBObject queryObj = new BasicDBObject(); queryObj.put(MetaClass.NAME, metaClass.getName()); // delete the field with given field name DBObject updateObj = new BasicDBObject(); BasicDBObject ups = new BasicDBObject(); ups.put("fields." + fieldName, ""); updateObj.put(MongoOperand.unset, ups); BasicDBObject versionObject = new BasicDBObject(); versionObject.put("version", 1); updateObj.put(MongoOperand.inc, versionObject); collection.update(queryObj, updateObj); cacheManager.deleteMetaClassFromCache(metaClass); MetaClass result = getMetaClass(metaClass.getName()); graph.updateMetaClass(result); return result; }
@SuppressWarnings("deprecation") @Test public void testRepoServiceInvalidArg() { try { new MongoRepositoryServiceImpl(null, 1, 1, 1, 1, null, WriteConcern.SAFE); } catch (Exception e) { // expected } try { new MongoRepositoryServiceImpl(new Mongo(), 0, 1, 1, 1, null, WriteConcern.SAFE); } catch (Exception e) { // expected } try { new MongoRepositoryServiceImpl(new Mongo(), 1, 0, 1, 1, null, WriteConcern.SAFE); } catch (Exception e) { // expected } try { new MongoRepositoryServiceImpl(new Mongo(), 1, 1, 1, 1, null, WriteConcern.SAFE); } catch (Exception e) { // expected } }
@Override public MetaClass getMetaClass(String className) { CheckConditions.checkNotNull(className, "MetaClass name can't be null!"); MetaClass m = cacheManager.getMetaClassFromCache(className); if (m == null) { //use className as name to query BasicDBObject query = new BasicDBObject(); query.put(MetaClass.NAME, className); m = getMetaClassFromDb(query); if (m != null) { return m; } //use className as pluralName to query query = new BasicDBObject(); query.put(MetaClass.PLURAL_NAME, className); m = getMetaClassFromDb(query); } return m; }
private void createServiceForRepository(Repository repo) { IMetadataService metadataService = new MongoMetadataServiceImpl(mongo, maxCacheSize, cacheExpiredTime, collectionCountCacheSize, collectionCountCacheExpiredTime, repo, metadataLock); metadataService.setMetaHistoryService(historyService); repo.setMetadataService(metadataService); repo.setRepositoryService(this); }
public void cleanUp() { //jianxu1: RepoService.getRepositories won't list repository with deleting status //if we failed to call deleteRepository due to exception, data loader won't be able to //clean all the data. //IRepositoryService oldRepo = RepositoryServiceFactory.createRepositoryService(ds); //for (Repository r : oldRepo.getRepositories()) { for(String repoName: getAllRepositoryNames()){ cleanDatabase(repoName); } cleanDatabase(CMSConsts.SYS_DB); MongoRepositoryServiceImpl.prepareSystemDB(mongo); RepositoryServiceFactory.clearRepositoryServiceCache(); }
private void checkMetaClassVersion(MetaClass metaClass) { MetaClass existingMeta = getMetaClass(metaClass.getName()); if (existingMeta == null) { throw new MetaClassNotExistsException(metaClass.getRepository(), metaClass.getName()); } if (metaClass.getVersion() > 0 && metaClass.getVersion() != existingMeta.getVersion()) { throw new MetaDataException(MetaErrCodeEnum.VERSION_CONFLICTED, "MetaClass version conflict: " + metaClass.getName()); } metaClass.setVersion(existingMeta.getVersion()); }
@Test(expected=IllegalMetaClassException.class) public void testInternalFields() { MetaClass metaClass = createMetaClass(); MetaAttribute attribute = new MetaAttribute(); attribute.setName("_type"); attribute.setDataType(MetaField.DataTypeEnum.INTEGER); metaClass.addField(attribute); }
private void refreshRepositoryCache() { getRepositories(new MetadataContext(true, true)); }
private void innerDeleteMetaClass(MetaClass metaClass) { DBObject obj = new BasicDBObject(); obj.put(MetaClass.NAME, metaClass.getName()); collection.remove(obj); cacheManager.deleteMetaClassFromCache(metaClass); graph.deleteMetaClass(metaClass); }
@BeforeClass public static void createRepoService() { MongoDataSource ds = new MongoDataSource(getConnectionString()); MetadataDataLoader.getInstance(ds).loadTestDataFromResource(); repositoryService = RepositoryServiceFactory.createRepositoryService(ds, "localCMSServer"); }
public static synchronized IRepositoryService createRepositoryService(MongoDataSource ds, String clientName, WriteConcern writeConcern) { IRepositoryService service = repoServices.get(ds); if (service == null) { CMSDBConfig dbConfig = new CMSDBConfig(ds); int cacheSize = (Integer) dbConfig.get(CMSDBConfig.REPOSITORY_CACHE_SIZE_KEY); int expireSeconds = (Integer) dbConfig.get(CMSDBConfig.REPOSITORY_CACHE_EXPIRE_SECONDS_KEY); int collectionCountCacheSize = (Integer) dbConfig.get(CMSDBConfig.COLLECTION_COUNT_CACHE_SIZE_KEY); int collectionCountCacheExpiredTime = (Integer) dbConfig.get(CMSDBConfig.COLLECTION_COUNT_CACHE_EXPIRE_SECONDS_KEY); ICMSLock lock = createMetadataLock(ds.getMongoInstance(), dbConfig, clientName); service = new MongoRepositoryServiceImpl(ds.getMongoInstance(), cacheSize, expireSeconds, collectionCountCacheSize, collectionCountCacheExpiredTime, lock, writeConcern); repoServices.put(ds, service); } return service; } }
@Test (expected=IllegalMetaClassException.class) public void testInherit_referenceCircle() { IMetadataService ms = repositoryService.getRepository(repoName).getMetadataService(); MetaClass mp41 = createMetaClass(); mp41.setName("mp41"); mp41.setParent("mp42"); MetaClass mp42 = createMetaClass(); mp42.setName("mp42"); mp42.setParent("mp41"); ms.batchUpsert(Arrays.asList(mp42, mp42), metaContext); }
@Test (expected=IllegalMetaClassException.class) public void testInherit_parentRefSelf() { IMetadataService ms = repositoryService.getRepository(repoName).getMetadataService(); MetaClass mp72 = createMetaClass(); mp72.setName("mp72"); mp72.setParent("mp72"); ms.createMetaClass(mp72, new MetadataContext()); }