private BasicMetricHour getMetricHour(String accountName, String guiPath, long hoursSince1970) { // logger.info("getting Metric Hour for account: " + accountName + " guiPath: " + guiPath + " // ts: " + hoursSince1970); BasicMetricHour metricHour = null; Bucket mhBucket = null; try { mhBucket = riakClient.fetchBucket(accountName + ";" + hoursSince1970).execute(); metricHour = mhBucket.fetch("" + guiPath, BasicMetricHour.class).execute(); logger.info("finding: " + accountName + ";" + hoursSince1970 + "/" + guiPath); if (metricHour != null) { metricHourCache.put( metricHour.getAccountName() + ";" + metricHour.getGuiPath() + ";" + metricHour.getHoursSince1970(), metricHour); } } catch (RiakRetryFailedException rrfe) { rrfe.printStackTrace(); } return metricHour; }
public static void put(String key, Object value) { try { cache.put(key, value); } catch (Exception e) { e.printStackTrace(); } }
public VsmCacheModel invoke() { if (cfIndex != null) { documents = (Map<String, List<LinkedHashMap<String, Object>>>) cfIndex; } else { documents = VectorUtil.getFeaturesForAllClasses(db); vectorSpaceModelCache.put("CLASS_FEATURE_INDEX", documents); } if (vsmIndex != null) { featureIndexList = (List<Integer>) vsmIndex; } else { featureIndexList = VectorUtil.getFeatureIndexList(db); vectorSpaceModelCache.put("GLOBAL_FEATURE_INDEX", featureIndexList); } return this; }
public static Map<Long, Integer> getTermFrequencyMapForDocument( GraphDatabaseService db, Long classId) { Map<Long, Integer> termDocumentMatrix; String cacheKey = "TERM_DOCUMENT_FREQUENCY_" + classId; if (vectorSpaceModelCache.getIfPresent(cacheKey) == null) { Node classNode = db.getNodeById(classId); termDocumentMatrix = new HashMap<>(); IteratorUtil.asCollection( db.traversalDescription() .depthFirst() .relationships(withName("HAS_CLASS"), Direction.INCOMING) .evaluator(Evaluators.fromDepth(1)) .evaluator(Evaluators.toDepth(1)) .traverse(classNode)) .stream() .forEach( p -> termDocumentMatrix.put( p.endNode().getId(), (Integer) p.lastRelationship().getProperty("matches"))); vectorSpaceModelCache.put(cacheKey, termDocumentMatrix); } else { termDocumentMatrix = (Map<Long, Integer>) vectorSpaceModelCache.getIfPresent(cacheKey); } return termDocumentMatrix; }
/** * returns true if the specified entry must be replicated. We should always replicate meta * operations (e.g. flush) and use the user HTD flag to decide whether or not replicate the * memstore. */ private boolean requiresReplication(final TableName tableName, final List<Entry> entries) throws IOException { // unit-tests may not the TableDescriptors, bypass the check and always replicate if (tableDescriptors == null) return true; Boolean requiresReplication = memstoreReplicationEnabled.getIfPresent(tableName); if (requiresReplication == null) { // check if the table requires memstore replication // some unit-test drop the table, so we should do a bypass check and always replicate. HTableDescriptor htd = tableDescriptors.get(tableName); requiresReplication = htd == null || htd.hasRegionMemstoreReplication(); memstoreReplicationEnabled.put(tableName, requiresReplication); } // if memstore replication is not required, check the entries. // meta edits (e.g. flush) must be always replicated. if (!requiresReplication) { int skipEdits = 0; java.util.Iterator<Entry> it = entries.iterator(); while (it.hasNext()) { Entry entry = it.next(); if (entry.getEdit().isMetaEdit()) { requiresReplication = true; } else { it.remove(); skipEdits++; } } skippedEdits.addAndGet(skipEdits); } return requiresReplication; }
public static int getDocumentSizeForFeature(GraphDatabaseService db, Long id) { int documentSize; String cacheKey = "DOCUMENT_SIZE_FEATURE_" + id; if (vectorSpaceModelCache.getIfPresent(cacheKey) == null) { Node startNode = db.getNodeById(id); Iterator<Node> classes = db.traversalDescription() .depthFirst() .relationships(withName("HAS_CLASS"), Direction.OUTGOING) .evaluator(Evaluators.fromDepth(1)) .evaluator(Evaluators.toDepth(1)) .traverse(startNode) .nodes() .iterator(); documentSize = IteratorUtil.count(classes); vectorSpaceModelCache.put(cacheKey, documentSize); } else { documentSize = (Integer) vectorSpaceModelCache.getIfPresent(cacheKey); } return documentSize; }
/** * Getting the data related to the given data key. * * @param pDataKey searched for * @return the related data * @throws TTIOException if the read to the persistent storage fails */ public IData getData(final long pDataKey) throws TTIOException { checkArgument(pDataKey >= 0); checkState(!mClose, "Transaction already closed"); // Calculate bucket and data part for given datakey. final long seqBucketKey = pDataKey >> IConstants.INDIRECT_BUCKET_COUNT[3]; final int dataBucketOffset = dataBucketOffset(pDataKey); DataBucket bucket = mCache.getIfPresent(seqBucketKey); if (bucket == null) { final List<DataBucket> listRevs = getSnapshotBuckets(seqBucketKey); final DataBucket[] revs = listRevs.toArray(new DataBucket[listRevs.size()]); checkState(revs.length > 0, "Number of Buckets to reconstruct must be larger than 0"); // Build up the complete bucket. final IRevisioning revision = mSession.getConfig().mRevision; bucket = revision.combineBuckets(revs); mCache.put(seqBucketKey, bucket); } final IData returnVal = bucket.getData(dataBucketOffset); // root-fsys is excluded from the checkagainst deletion based on the necesssity of the // data-layer to // reference against this data while creation of the transaction if (pDataKey == 0) { return returnVal; } else { return checkItemIfDeleted(returnVal); } }
public static Integer getShard(String s) { Integer shard = shardCache.getIfPresent(s); if (shard == null) { shard = computeShard(s); shardCache.put(s, shard); } return shard; }
public ClassLoaderDetails getDetails( ClassLoader classLoader, Transformer<ClassLoaderDetails, ClassLoader> factory) { lock.lock(); try { ClassLoaderDetails details = classLoaderDetails.getIfPresent(classLoader); if (details != null) { return details; } details = factory.transform(classLoader); classLoaderDetails.put(classLoader, details); classLoaderIds.put(details.uuid, classLoader); return details; } finally { lock.unlock(); } }
public ClassLoader getClassLoader( ClassLoaderDetails details, Transformer<ClassLoader, ClassLoaderDetails> factory) { lock.lock(); try { ClassLoader classLoader = classLoaderIds.getIfPresent(details.uuid); if (classLoader != null) { return classLoader; } classLoader = factory.transform(details); classLoaderIds.put(details.uuid, classLoader); classLoaderDetails.put(classLoader, details); return classLoader; } finally { lock.unlock(); } }
private void tcpdumpReadLoop(Process tcpdumpProcess) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(tcpdumpProcess.getInputStream())); String ipLine = reader.readLine(); if (ipLine != null) { log.info("STARTED: " + ipLine); while ((ipLine = reader.readLine()) != null) { Matcher ipHdrMatcher = ipHdrPattern.matcher(ipLine); if (!ipHdrMatcher.matches()) { // Packets dropped count occurs at termination. If the count // is zero we don't want an error. if (!ipLine.startsWith("0 packets dropped")) { log.error("found something other than ip header line: {}", ipLine); } continue; } String ipProtocol = ipHdrMatcher.group(IP_PROTO_GROUP); boolean wasTcp = "TCP".equals(ipProtocol); boolean wasUdp = "UDP".equals(ipProtocol); String protoLine = reader.readLine(); if (protoLine == null) { break; } if (wasUdp) { Matcher dnsMatcher = dnsPattern.matcher(protoLine); if (dnsMatcher.matches()) { DnsData dnsData = cacheDnsQueryPacket(ipHdrMatcher, dnsMatcher); if (dnsData != null) { log.debug("DNS matched packet: {}", dnsData); } else { log.debug("DNS matched packet not in cache: {}", protoLine); } } else { // next line is noisy in test environments using dnsmasq log.debug("DNS line did not match regex: {}", protoLine); } } else if (wasTcp) { Matcher synMatcher = tcpSynPattern.matcher(protoLine); if (synMatcher.matches()) { TcpSynData synPacket = createSynPacket(ipHdrMatcher, synMatcher); log.debug("SYN matched packet: {}", synPacket); String synKey = synCacheKey(synPacket.getSourceIp(), synPacket.getSourcePort()); synCache.put(synKey, synPacket); } else { log.warn("TCP SYN line did not match regex: {}", protoLine); } } else { log.error("This code should be unreachable: {}", ipLine); } } } }
@Override public EconomyData get(UUID uniqueId) { EconomyData data = cache.getIfPresent(uniqueId); if (data == null) { data = new EconomyData(uniqueId, 0.0D); cache.put(uniqueId, data); } return data; }
@Override public <T extends Definition> void putCachedString( String uid, DefDescriptor<?> descriptor, String key, String value) { DependencyEntry de = localDependencies.get(uid); if (de != null) { stringsCache.put(getKey(de, descriptor, key), value); } }
public static ZimletUserProperties getProperties(Account account) { String key = account.getId(); ZimletUserProperties prop = CACHE.getIfPresent(key); if (prop == null) { prop = new ZimletUserProperties(account); CACHE.put(key, prop); } return prop; }
@Override public String createHostKeyEntry(Device device) { // store an empty entry. packet captures do not create entries, they // only update entries that have been explicitly marked for tracking // by this method. DnsData dnsData = new DnsData(); String hostKey = dnsCacheKey(device); dnsCache.put(hostKey, dnsData); log.info("added key '{}' to dns cache", hostKey); return hostKey; }
@Traced public Vertex findByIdUserVertex(String userId) { Vertex userVertex = userVertexCache.getIfPresent(userId); if (userVertex != null) { return userVertex; } userVertex = graph.getVertex(userId, authorizations); if (userVertex != null) { userVertexCache.put(userId, userVertex); } return userVertex; }
public static int getDocumentSize(GraphDatabaseService db) { int documentSize; String cacheKey = "GLOBAL_DOCUMENT_SIZE"; if (vectorSpaceModelCache.getIfPresent(cacheKey) == null) { documentSize = IteratorUtil.count( GlobalGraphOperations.at(db).getAllNodesWithLabel(DynamicLabel.label("Class"))); vectorSpaceModelCache.put(cacheKey, documentSize); } else { documentSize = (Integer) vectorSpaceModelCache.getIfPresent(cacheKey); } return documentSize; }
public static synchronized Session getSession(String host, int port) throws UnknownHostException { Session session = hostConnectionMap.getIfPresent(host); if (session == null || session.isClosed()) { Cluster.Builder builder = Cluster.builder().addContactPoint(host).withPort(port); Cluster cluster = builder.build(); session = cluster.connect(); hostConnectionMap.put(host, session); logger.debug("Created connection to {}.", host); logger.debug("Number of sessions opened are {}.", hostConnectionMap.size()); } return session; }
public String generateToken(String userName) throws PhrescoException { String token = ""; try { SecureRandom random = SecureRandom.getInstance(ALGORITHM_NAME); byte[] seed = random.generateSeed(132); random.setSeed(seed); token = new BigInteger(132, random).toString(32); tokenCache.put(token, userName); } catch (NoSuchAlgorithmException e) { throw new PhrescoException(e); } return token; }
/** * @param structure * @return The record in cache if exists or the record itself and cache it. */ private T initStructure(final T structure, final boolean created) { final T inCache = cache.getIfPresent(structure.hashCode()); if (inCache != null) return inCache; cache.put(structure.hashCode(), structure); final ServerStorageStructurePrivateBase privateBase = ((ServerStorageStructurePrivateBase) structure); privateBase.setOwnerAndTracker(this, changeTracker); if (created) privateBase.onCreate(); return structure; }
public String cacheSearch(Query query, String reuseId, int maxResults) throws IOException { if ((maxResults <= 0) || (maxResults > MAX_RESULTS)) { maxResults = MAX_RESULTS; } String id = ((reuseId != null) && (reuseId.length() > 0)) ? reuseId : UUID.randomUUID().toString(); if (query != null) // otherwise it's an All Docs search which is the default { TopDocs docs = search(query, maxResults); docs.totalHits = Math.min(maxResults, docs.totalHits); cache.put(id, docs); } return id; }
private ClientResponse sendRequest(ServerRequest request) throws Exception { ResponseHolder responseHolder = new ResponseHolder(); responseHolders.put(request.getRequestId(), responseHolder); requestObserver.onNext(request); // timeout is in case agent never responds // passing ClientResponse.getDefaultInstance() is just dummy (non-null) value ClientResponse response = responseHolder.response.exchange(ClientResponse.getDefaultInstance(), 1, HOURS); if (response.getMessageCase() == MessageCase.UNKNOWN_REQUEST_RESPONSE) { throw new OutdatedAgentException(); } if (response.getMessageCase() == MessageCase.EXCEPTION_RESPONSE) { throw new AgentException(); } return response; }
@Override public User findByUsername(String username) { username = formatUsername(username); Iterable<Vertex> vertices = graph .query(authorizations) .has(UserVisalloProperties.USERNAME.getPropertyName(), username) .has(VisalloProperties.CONCEPT_TYPE.getPropertyName(), userConceptId) .vertices(); Vertex userVertex = singleOrDefault(vertices, null); if (userVertex == null) { return null; } userVertexCache.put(userVertex.getId(), userVertex); return createFromVertex(userVertex); }
private FileInfo copyIntoCache( Factory<File> baseDirFactory, File source, long lastModified, long length, HashValue hashValue) { File baseDir = baseDirFactory.create(); File cachedFile = new File(baseDir, hashValue.asCompactString() + '/' + source.getName()); if (!cachedFile.isFile()) { // Has previously been added to the cache directory GFileUtils.copyFile(source, cachedFile); } FileInfo fileInfo = new FileInfo(lastModified, length, hashValue, cachedFile); cachedFiles.put(source, fileInfo); return fileInfo; }
@Test public void shouldReturnNoElementAfterInvalidation() throws Exception { // given final Cache<Long, String> cache = CacheBuilder.newBuilder() // .expireAfterWrite(EXPIRATION_TIME_IN_MILLISECONDS * 3, TimeUnit.MILLISECONDS) // .build(); cache.put(KEY, FOO); // when cache.invalidate(KEY); final String value = cache.getIfPresent(KEY); // then assertNull(value); }
@Test public void temporizedCacheShouldDeleteEntriesAfterTimeout() throws Exception { // given final Cache<Long, String> cache = CacheBuilder.newBuilder() // .expireAfterWrite(EXPIRATION_TIME_IN_MILLISECONDS, TimeUnit.MILLISECONDS) // .build(); cache.put(KEY, FOO); Thread.sleep(EXPIRATION_TIME_IN_MILLISECONDS + 200); // when final String value = cache.getIfPresent(KEY); // then assertNull(value); }
public V get(K key) { V v = cache.getIfPresent(key); if (v == null) { try { lockManager.hold(locks, key); if (v == null) { v = loader.load(key); cache.put(key, v); } } catch (Exception ex) { PGException.pgThrow(ex); } finally { lockManager.release(locks, key); } } return v; }
public Client parse(String agentString) { // lookup cache if enabled if (!disableCache) { Client client = uaCache.getIfPresent(agentString); if (client != null) { return client; } } UserAgent ua = parseUserAgent(agentString); OS os = parseOS(agentString); Device device = deviceParser.parse(agentString, (ua == null ? null : ua.family)); Client client = new Client(ua, os, device); if (!disableCache) { uaCache.put(agentString, client); } return client; }
@Override public void next(NextObjective nextObjective) { if (nextObjective.type() != NextObjective.Type.BROADCAST) { log.error("OLT only supports broadcast groups."); fail(nextObjective, ObjectiveError.BADPARAMS); } if (nextObjective.next().size() != 1) { log.error("OLT only supports singleton broadcast groups."); fail(nextObjective, ObjectiveError.BADPARAMS); } TrafficTreatment treatment = nextObjective.next().stream().findFirst().get(); GroupBucket bucket = DefaultGroupBucket.createAllGroupBucket(treatment); GroupKey key = new DefaultGroupKey(appKryo.serialize(nextObjective.id())); GroupDescription groupDesc = new DefaultGroupDescription( deviceId, GroupDescription.Type.ALL, new GroupBuckets(Collections.singletonList(bucket)), key, null, nextObjective.appId()); pendingGroups.put(key, nextObjective); switch (nextObjective.op()) { case ADD: groupService.addGroup(groupDesc); break; case REMOVE: groupService.removeGroup(deviceId, key, nextObjective.appId()); break; case ADD_TO_EXISTING: case REMOVE_FROM_EXISTING: // TODO: handle addition to group when caller signals it. break; default: log.warn("Unknown next objective operation: {}", nextObjective.op()); } }
public void delete(Key key) { if (deletedCache.getIfPresent(key) != null) return; if (myKey.equals(key)) { deletedCache.put(key, this); } try { Entity ent = datastore.get(key); if (ent.hasProperty("next")) { delete((Key) ent.getProperty("next")); // recurse } datastore.delete(key); try { datastore.get(myKey); } catch (EntityNotFoundException e) { } } catch (Exception e) { // e.printStackTrace(); } }