List of usage examples for com.google.common.collect MapMaker MapMaker
public MapMaker()
From source file:com.hipu.bdb.util.ObjectIdentityBdbManualCache.java
/** * Call this method when you have an instance when you used the * default constructor or when you have a deserialized instance that you * want to reconnect with an extant bdbje environment. Do not * call this method if you used the/*from w w w. j a v a2s.c om*/ * {@link #CachedBdbMap(File, String, Class, Class)} constructor. * @param env * @param keyClass * @param valueClass * @param classCatalog * @throws DatabaseException */ @SuppressWarnings("unchecked") public void initialize(final Environment env, String dbName, final Class valueClass, final StoredClassCatalog classCatalog) throws DatabaseException { // TODO: tune capacity for actual threads, expected size of key caches? this.memMap = new MapMaker().concurrencyLevel(64).initialCapacity(8192).softValues().makeMap(); this.db = openDatabase(env, dbName); this.diskMap = createDiskMap(this.db, classCatalog, valueClass); // keep a record of items that must be persisted; auto-persist if // unchanged after 5 minutes, or more than 10K would collect this.dirtyItems = new MapMaker().concurrencyLevel(64).maximumSize(10000) .expireAfterWrite(5, TimeUnit.MINUTES).evictionListener(this).makeMap(); this.count = new AtomicLong(diskMap.size()); }
From source file:org.apache.shindig.social.opensocial.jpa.ApplicationDataMapDb.java
/** * set the state of the object after load *//*from w w w . j av a 2s .com*/ @PostLoad public void postLoad() { values = new MapMaker().makeMap(); for (Entry<String, ApplicationDataMapValueDb> e : valuesDb.entrySet()) { values.put(e.getKey(), e.getValue().value); } }
From source file:fr.xebia.training.troubleshooting.TroubleshootingTrainingInfrastructureCreator.java
public void generateDocs() { Filter filter = new Filter("tag:TrainingSession", Lists.newArrayList("Troubleshooting")); List<Reservation> reservations = ec2.describeInstances(new DescribeInstancesRequest().withFilters(filter)) .getReservations();/*from w ww .java 2 s .c o m*/ Iterable<Instance> instances = AmazonAwsUtils.toEc2Instances(reservations); Iterable<Instance> runningInstances = Iterables.filter(instances, AmazonAwsUtils.PREDICATE_RUNNING_OR_PENDING_INSTANCE); runningInstances = AmazonAwsUtils.awaitForEc2Instances(runningInstances, ec2); Map<String, Instance> runningInstancesByInstanceId = Maps.uniqueIndex(runningInstances, AmazonAwsFunctions.EC2_INSTANCE_TO_INSTANCE_ID); List<String> runningInstanceIds = Lists.newArrayList( Iterables.transform(runningInstances, AmazonAwsFunctions.EC2_INSTANCE_TO_INSTANCE_ID)); List<TagDescription> tags = ec2 .describeTags(new DescribeTagsRequest().withFilters(new Filter("resource-id", runningInstanceIds))) .getTags(); Map<String, Map<String, String>> tagsByInstanceId = new MapMaker() .makeComputingMap(new Function<String, Map<String, String>>() { @Override public Map<String, String> apply(String instanceId) { return Maps.newHashMap(); } }); for (TagDescription tag : tags) { tagsByInstanceId.get(tag.getResourceId()).put(tag.getKey(), tag.getValue()); } Map<String, Map<String, Object>> tomcatTagsPerTeamIdentifier = Maps.newHashMap(); for (Map.Entry<String, Map<String, String>> entry : tagsByInstanceId.entrySet()) { String instanceId = entry.getKey(); Map<String, String> instanceTags = entry.getValue(); String teamIdentifier = instanceTags.get("TeamIdentifier"); Instance tomcatInstance = runningInstancesByInstanceId.get(instanceId); Map<String, Object> tomcatTags = Maps.newHashMap(); tomcatTags.putAll(instanceTags); tomcatTags.put("PublicDnsName", tomcatInstance.getPublicDnsName()); tomcatTags.put("Instance", tomcatInstance); tomcatTagsPerTeamIdentifier.put(teamIdentifier, tomcatTags); } Map<String, Object> rootMap = Maps.newHashMap(); rootMap.put("infrastructures", tomcatTagsPerTeamIdentifier); String wikiPage = FreemarkerUtils.generate(rootMap, "/fr/xebia/training/troubleshooting/wiki-page.ftl"); System.out.println(wikiPage); }
From source file:co.cask.cdap.data.stream.service.ConcurrentStreamWriter.java
ConcurrentStreamWriter(StreamCoordinatorClient streamCoordinatorClient, StreamAdmin streamAdmin, StreamFileWriterFactory writerFactory, int workerThreads, StreamMetricsCollectorFactory metricsCollectorFactory) { this.streamCoordinatorClient = streamCoordinatorClient; this.streamAdmin = streamAdmin; this.workerThreads = workerThreads; this.metricsCollectorFactory = metricsCollectorFactory; this.eventQueues = new MapMaker().concurrencyLevel(workerThreads).makeMap(); this.streamFileFactory = new StreamFileFactory(writerFactory); this.generationWatched = Sets.newHashSet(); this.cancellables = Lists.newArrayList(); this.createLock = new ReentrantLock(); }
From source file:net.conquiris.index.DefaultWriter.java
/** * Default writer./* w ww. j av a2 s . com*/ * @param log Log context. * @param writer Lucene index writer to use. * @param overrideCheckpoint Whether to override the checkpoint. * @param checkpoint Overridden checkpoint value. * @param created Whether the index has been requested to be created. */ DefaultWriter(ContextLog log, IndexWriter writer, boolean overrideCheckpoint, @Nullable String checkpoint, boolean created) throws IndexException { this.log = checkNotNull(log, "The log context must be provided"); this.writer = checkNotNull(writer, "The index writer must be provided"); this.properties = new MapMaker().makeMap(); this.keys = Collections.unmodifiableSet(this.properties.keySet()); // Read properties try { final Map<String, String> commitData; final int documents; if (created) { commitData = ImmutableMap.of(); documents = 0; } else { final IndexReader reader = IndexReader.open(writer, false); boolean threw = true; try { Map<String, String> data = reader.getIndexCommit().getUserData(); if (overrideCheckpoint) { final Map<String, String> modified = Maps.newHashMap(); if (data != null) { modified.putAll(data); } modified.put(IndexInfo.CHECKPOINT, checkpoint); commitData = modified; } else { commitData = data; } documents = reader.numDocs(); threw = false; } finally { Closeables.close(reader, threw); } } this.indexInfo = IndexInfo.fromMap(documents, commitData); this.checkpoint = this.indexInfo.getCheckpoint(); this.targetCheckpoint = this.indexInfo.getTargetCheckpoint(); this.properties.putAll(this.indexInfo.getProperties()); } catch (LockObtainFailedException e) { indexStatus.compareAndSet(IndexStatus.OK, IndexStatus.LOCKED); throw new IndexException(e); } catch (CorruptIndexException e) { indexStatus.compareAndSet(IndexStatus.OK, IndexStatus.CORRUPT); throw new IndexException(e); } catch (IOException e) { indexStatus.compareAndSet(IndexStatus.OK, IndexStatus.IOERROR); throw new IndexException(e); } catch (RuntimeException e) { indexStatus.compareAndSet(IndexStatus.OK, IndexStatus.ERROR); throw e; } }
From source file:org.opencastproject.userdirectory.jpa.JpaUserAndRoleProvider.java
/** * Callback for activation of this component. * /*from www . j av a2s .c om*/ * @param cc * the component context */ public void activate(ComponentContext cc) { logger.debug("activate"); // Setup the caches cache = new MapMaker().expireAfterWrite(1, TimeUnit.MINUTES) .makeComputingMap(new Function<String, Object>() { public Object apply(String id) { String[] key = id.split(DELIMITER); logger.trace("Loading user '{}':'{}' from database", key[0], key[1]); User user = loadUser(key[0], key[1]); return user == null ? nullToken : user; } }); // Set up persistence emf = persistenceProvider.createEntityManagerFactory("org.opencastproject.userdirectory", persistenceProperties); }
From source file:org.waveprotocol.wave.examples.fedone.frontend.ClientFrontendImpl.java
@Inject public ClientFrontendImpl(final HashedVersionFactory hashedVersionFactory, WaveletProvider waveletProvider, WaveBus wavebus) {/*from www .j a v a 2 s . c o m*/ this.waveletProvider = waveletProvider; wavebus.subscribe(this); MapMaker mapMaker = new MapMaker(); perWavelet = mapMaker.makeComputingMap(new Function<WaveletName, PerWavelet>() { @Override public PerWavelet apply(WaveletName waveletName) { return new PerWavelet(waveletName, hashedVersionFactory.createVersionZero(waveletName)); } }); perUser = mapMaker.makeComputingMap(new Function<ParticipantId, UserManager>() { @Override public UserManager apply(ParticipantId from) { return new UserManager(hashedVersionFactory); } }); }
From source file:org.opencastproject.userdirectory.ldap.LdapUserProviderInstance.java
/** * Constructs an ldap user provider with the needed settings. * /*from w w w. j a v a2 s. co m*/ * @param pid * the pid of this service * @param organization * the organization * @param searchBase * the ldap search base * @param searchFilter * the ldap search filter * @param url * the url of the ldap server * @param userDn * the user to authenticate as * @param password * the user credentials * @param roleAttributesGlob * the comma separate list of ldap attributes to treat as roles * @param cacheSize * the number of users to cache * @param cacheExpiration * the number of minutes to cache users */ // CHECKSTYLE:OFF LdapUserProviderInstance(String pid, String organization, String searchBase, String searchFilter, String url, String userDn, String password, String roleAttributesGlob, int cacheSize, int cacheExpiration) { // CHECKSTYLE:ON this.organization = organization; logger.debug("Creating LdapUserProvider instance with pid=" + pid + ", and organization=" + organization + ", to LDAP server at url: " + url); DefaultSpringSecurityContextSource contextSource = new DefaultSpringSecurityContextSource(url); if (StringUtils.isNotBlank(userDn)) { contextSource.setPassword(password); contextSource.setUserDn(userDn); // Required so that authentication will actually be used contextSource.setAnonymousReadOnly(false); } else { // No password set so try to connect anonymously. contextSource.setAnonymousReadOnly(true); } try { contextSource.afterPropertiesSet(); } catch (Exception e) { throw new org.opencastproject.util.ConfigurationException("Unable to create a spring context source", e); } FilterBasedLdapUserSearch userSearch = new FilterBasedLdapUserSearch(searchBase, searchFilter, contextSource); userSearch.setReturningAttributes(roleAttributesGlob.split(",")); this.delegate = new LdapUserDetailsService(userSearch); if (StringUtils.isNotBlank(roleAttributesGlob)) { LdapUserDetailsMapper mapper = new LdapUserDetailsMapper(); mapper.setRoleAttributes(roleAttributesGlob.split(",")); this.delegate.setUserDetailsMapper(mapper); } // Setup the caches cache = new MapMaker().maximumSize(cacheSize).expireAfterWrite(cacheExpiration, TimeUnit.MINUTES) .makeComputingMap(new Function<String, Object>() { public Object apply(String id) { User user = loadUserFromLdap(id); return user == null ? nullToken : user; } }); registerMBean(pid); }
From source file:org.apache.giraph.comm.messages.with_source.SimpleMessageWithSourceStore.java
/** * If there is already a map of messages related to the partition id * return that map, otherwise create a new one, put it in global map and * return it.//from w w w. jav a2 s .c o m * * @param partitionId Id of partition * @return Message map for this partition */ protected ConcurrentMap<I, ConcurrentMap<I, T>> getOrCreatePartitionMap(int partitionId) { ConcurrentMap<I, ConcurrentMap<I, T>> partitionMap = map.get(partitionId); if (partitionMap == null) { ConcurrentMap<I, ConcurrentMap<I, T>> tmpMap = new MapMaker() .concurrencyLevel(config.getNettyServerExecutionConcurrency()).makeMap(); partitionMap = map.putIfAbsent(partitionId, tmpMap); if (partitionMap == null) { partitionMap = tmpMap; } } return partitionMap; }
From source file:io.druid.server.QueryResource.java
@POST @Produces({ MediaType.APPLICATION_JSON, SmileMediaTypes.APPLICATION_JACKSON_SMILE }) @Consumes({ MediaType.APPLICATION_JSON, SmileMediaTypes.APPLICATION_JACKSON_SMILE, APPLICATION_SMILE }) public Response doPost(InputStream in, @QueryParam("pretty") String pretty, @Context final HttpServletRequest req // used only to get request content-type and remote address ) throws IOException { final long start = System.currentTimeMillis(); Query query = null;/* ww w . java 2 s . c o m*/ String queryId = null; final String reqContentType = req.getContentType(); final boolean isSmile = SmileMediaTypes.APPLICATION_JACKSON_SMILE.equals(reqContentType) || APPLICATION_SMILE.equals(reqContentType); final String contentType = isSmile ? SmileMediaTypes.APPLICATION_JACKSON_SMILE : MediaType.APPLICATION_JSON; ObjectMapper objectMapper = isSmile ? smileMapper : jsonMapper; final ObjectWriter jsonWriter = pretty != null ? objectMapper.writerWithDefaultPrettyPrinter() : objectMapper.writer(); try { query = objectMapper.readValue(in, Query.class); queryId = query.getId(); if (queryId == null) { queryId = UUID.randomUUID().toString(); query = query.withId(queryId); } if (query.getContextValue(QueryContextKeys.TIMEOUT) == null) { query = query.withOverriddenContext(ImmutableMap.of(QueryContextKeys.TIMEOUT, config.getMaxIdleTime().toStandardDuration().getMillis())); } if (log.isDebugEnabled()) { log.debug("Got query [%s]", query); } final Map<String, Object> responseContext = new MapMaker().makeMap(); final Sequence res = query.run(texasRanger, responseContext); final Sequence results; if (res == null) { results = Sequences.empty(); } else { results = res; } final Yielder yielder = results.toYielder(null, new YieldingAccumulator() { @Override public Object accumulate(Object accumulated, Object in) { yield(); return in; } }); try { final Query theQuery = query; return Response.ok(new StreamingOutput() { @Override public void write(OutputStream outputStream) throws IOException, WebApplicationException { // json serializer will always close the yielder jsonWriter.writeValue(outputStream, yielder); outputStream.flush(); // Some types of OutputStream suppress flush errors in the .close() method. outputStream.close(); final long queryTime = System.currentTimeMillis() - start; emitter.emit(DruidMetrics.makeQueryTimeMetric(jsonMapper, theQuery, req.getRemoteAddr()) .build("query/time", queryTime)); requestLogger.log(new RequestLogLine(new DateTime(), req.getRemoteAddr(), theQuery, new QueryStats(ImmutableMap.<String, Object>of("query/time", queryTime, "success", true)))); } }, contentType).header("X-Druid-Query-Id", queryId) .header("X-Druid-Response-Context", jsonMapper.writeValueAsString(responseContext)).build(); } catch (Exception e) { // make sure to close yielder if anything happened before starting to serialize the response. yielder.close(); throw Throwables.propagate(e); } finally { // do not close yielder here, since we do not want to close the yielder prior to // StreamingOutput having iterated over all the results } } catch (QueryInterruptedException e) { try { log.info("%s [%s]", e.getMessage(), queryId); requestLogger.log(new RequestLogLine(new DateTime(), req.getRemoteAddr(), query, new QueryStats(ImmutableMap.<String, Object>of("success", false, "interrupted", true, "reason", e.toString())))); } catch (Exception e2) { log.error(e2, "Unable to log query [%s]!", query); } return Response.serverError().type(contentType) .entity(jsonWriter.writeValueAsBytes( ImmutableMap.of("error", e.getMessage() == null ? "null exception" : e.getMessage()))) .build(); } catch (Exception e) { // Input stream has already been consumed by the json object mapper if query == null final String queryString = query == null ? "unparsable query" : query.toString(); log.warn(e, "Exception occurred on request [%s]", queryString); try { requestLogger.log(new RequestLogLine(new DateTime(), req.getRemoteAddr(), query, new QueryStats( ImmutableMap.<String, Object>of("success", false, "exception", e.toString())))); } catch (Exception e2) { log.error(e2, "Unable to log query [%s]!", queryString); } log.makeAlert(e, "Exception handling request").addData("exception", e.toString()) .addData("query", queryString).addData("peer", req.getRemoteAddr()).emit(); return Response.serverError().type(contentType) .entity(jsonWriter.writeValueAsBytes( ImmutableMap.of("error", e.getMessage() == null ? "null exception" : e.getMessage()))) .build(); } }