@Override public Repository createRepository(Repository repo) { return createRepository(repo, WriteConcern.SAFE); }
private void refreshRepositoryCache() { getRepositories(new MetadataContext(true, true)); }
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(); }
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; } }
MongoUtils.ensureIndex(metaCollection, MetaClass.PLURAL_NAME, true, true); createServiceForRepository(repo); refreshRepositoryCache(); return repo; } catch (InterruptedException e) {
@Override public Repository getRepository(String repositoryName) { Repository repo = null; BasicDBObject query = new BasicDBObject(); query.put(Repository.REPOSITORY_FIELD_NAME, repositoryName); query.put(Repository.STATE_FIELD, Repository.StateEnum.normal.toString()); boolean suggestRefresh = false; DBObject object = repoCollection.findOne(query); if (object != null) { repo = cache.getObject(repositoryName); if (repo == null) { suggestRefresh = true; } } else if (cache.getObject(repositoryName) != null) { suggestRefresh = true; } // find something different between the cache and the read db. if (suggestRefresh) { refreshRepositoryCache(); repo = cache.getObject(repositoryName); } if (repo == null) { throw new RepositoryNotExistsException(repositoryName); } return repo; }
@Override public List<Repository> getRepositories(MetadataContext ctx) { MetadataContext context = ctx != null ? ctx : new MetadataContext(); if (!context.isRefreshRepsitory()) { return cache.values(); } ExpirableCache<Repository> newCache = new ExpirableCache<Repository>(maxCacheSize, cacheExpiredTime); BasicDBObject query = new BasicDBObject(); query.put(Repository.STATE_FIELD, Repository.StateEnum.normal.toString()); List<Repository> result = new ArrayList<Repository>(); DBCursor cursor = repoCollection.find(query); while (cursor.hasNext()) { DBObject object = cursor.next(); Repository r = repositoryConverter.fromBson(object, Repository.class); createServiceForRepository(r); result.add(r); newCache.putObject(r.getRepositoryName(), r); } cache = newCache; return result; }
public MongoRepositoryServiceImpl(Mongo mongo, int maxCacheSize, int cacheExpiredTime, int collectionCountCacheSize, int collectionCountCacheExpiredTime, ICMSLock metadataLock, WriteConcern writeConcern) { CheckConditions.checkNotNull(mongo, "mongo can not be null"); CheckConditions.checkArgument(maxCacheSize > 0, "maximumSize can not be negtive value"); CheckConditions.checkArgument(cacheExpiredTime > 0, "expireAfterSeconds can not be negtive value"); CheckConditions.checkArgument(metadataLock != null, "metadataLock should not be null"); this.metadataLock = metadataLock; this.mongo = mongo; this.maxCacheSize = maxCacheSize; this.cacheExpiredTime = cacheExpiredTime; cache = new ExpirableCache<Repository>(maxCacheSize, cacheExpiredTime); this.collectionCountCacheSize = collectionCountCacheSize; this.collectionCountCacheExpiredTime = collectionCountCacheExpiredTime; prepareSystemDB(mongo); repoCollection = this.mongo.getDB(CMSConsts.SYS_DB).getCollection(CMSConsts.REPOSITORY_COLL); // read from primary only repoCollection.setReadPreference(ReadPreference.primary()); repoCollection.setWriteConcern(writeConcern); }
@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 } }
throw new MongoOperationException(e); refreshRepositoryCache(); } catch (InterruptedException e) { logger.info("lock interrupted for delete " + repositoryName);
refreshRepositoryCache(); } catch (InterruptedException e) { logger.info("lock interrupted for createRepository " + repositoryName);