/** * 增加一个新节点 @Title: addCacheNode * * @author: zzh * @date: 2016年12月2日 下午8:56:38 * @version: v1.0.0 */ private static void addCacheNode() { // 添加到真实Node列表 CacheNode newNode = new CacheNode("10.9.1.100"); realNodeList.add(newNode); // 生成虚拟节点 List<VisualCacheNode> addVisualNodeList = genVisualCacheNode(newNode); // 查询哪些节点受到影响,即数据需要分散 for (VisualCacheNode vnode : addVisualNodeList) { String nodeName = vnode.getNodeName(); String md5VisualNodeKey = DigestUtils.md5Hex(nodeName); // 使用common-codec工具包 int hashNodeName = DJBHash(md5VisualNodeKey); SortedMap<Integer, VisualCacheNode> vs = visualNodeMap.tailMap(hashNodeName + 1); // 排除掉自己。 VisualCacheNode nodeInfo = null; if (vs != null && vs.size() > 0) { int affectVnshash = vs.firstKey(); nodeInfo = visualNodeMap.get(affectVnshash); } else { nodeInfo = visualNodeMap.get(visualNodeMap.firstKey()); } System.out.println("新添节点,受到影响的虚拟节点:" + nodeInfo); } // 将虚拟节点加入到hash环中 addVisualCacheNodeIntoCircle(addVisualNodeList); }
protected synchronized boolean hasMoreEntries(Position position) { if (position.getLedgerId() == currentLedger.getId()) { // If we are reading from the last ledger, use the // LedgerHandle metadata return position.getEntryId() <= currentLedger.getLastAddConfirmed(); } else if (currentLedger.getLastAddConfirmed() >= 0) { // We have entries in the current ledger and we are reading from an // older ledger return true; } else { // At this point, currentLedger is empty, we need to check in the // older ledgers for entries past the current position LedgerStat ls = ledgers.get(position.getLedgerId()); if (ls == null) { // The cursor haven't been initialized yet checkArgument(position.getLedgerId() == -1); return true; } else if (position.getEntryId() < ls.getEntriesCount()) { // There are still entries to read in the current reading ledger return true; } else { for (LedgerStat stat : ledgers.tailMap(position.getLedgerId(), false).values()) { if (stat.getEntriesCount() > 0) return true; } return false; } } }
/** * 删除节点。 @Title: removeCacheNode * * @author: zzh * @date: 2016年12月4日 下午8:34:15 * @version: v1.0.0 */ private static void removeCacheNode(String nodeIp) { // 删除当前IP的虚拟节点信息 CacheNode cacheNode = new CacheNode(nodeIp); // 获取当前节点的虚拟节点信息 List<VisualCacheNode> vNodesList = genVisualCacheNode(cacheNode); // 从hash环中删除虚拟节点信息 for (VisualCacheNode c : vNodesList) { String vNodeName = c.getNodeName(); String md5VisualNodeKey = DigestUtils.md5Hex(vNodeName); // 使用common-codec工具包 int hashNodeName = DJBHash(md5VisualNodeKey); visualNodeMap.remove(hashNodeName); System.out.println("删除虚拟节点,虚拟节点名称:" + vNodeName + ",真实节点:" + c.getRealCacheNode()); // 删除后,受到影响的节点:即数据和流量需要迁移到的节点信息 SortedMap<Integer, VisualCacheNode> vs = visualNodeMap.tailMap(hashNodeName); VisualCacheNode nodeInfo = null; if (vs != null && vs.size() > 0) { int affectVnshash = vs.firstKey(); nodeInfo = visualNodeMap.get(affectVnshash); } else { nodeInfo = visualNodeMap.get(visualNodeMap.firstKey()); } System.out.println("删除当前虚拟节点" + vNodeName + " , 受到影响的虚拟节点(原本虚拟节点的数据和流量将迁移到此节点):" + nodeInfo); } // 踢出真实节点 removeRealCache(nodeIp); }
/** * Map排序 * * @param unsort_map * @return */ private static SortedMap<String, String> mapSortByKey(Map<String, String> unsort_map) { TreeMap<String, String> result = new TreeMap<String, String>(); Object[] unsort_key = unsort_map.keySet().toArray(); Arrays.sort(unsort_key); for (int i = 0; i < unsort_key.length; i++) { result.put(unsort_key[i].toString(), unsort_map.get(unsort_key[i])); } return result.tailMap(result.firstKey()); }
@Override public void fail(Object msgId) { TransactionAttempt tx = (TransactionAttempt) msgId; TransactionStatus stored = _activeTx.remove(tx.getTransactionId()); if (stored != null && tx.equals(stored.attempt)) { _activeTx.tailMap(tx.getTransactionId()).clear(); sync(); } }
public Shard getShard(String key) { SortedMap<Long, Shard> tail = virtual_nodes.tailMap(hash.hash(key)); if (tail.size() == 0) { // 返回第一个虚拟节点的真实节点信息 return virtual_nodes.get(virtual_nodes.firstKey()); } else { // 返回距离hash值最近的节点的真实节点信息 return tail.get(tail.firstKey()); } }
/** * Gets the online regions of the specified table. This method looks at the in-memory state. It * does not go to <code>hbase:meta</code>. Only returns <em>online</em> regions. If a region on * this table has been closed during a disable, etc., it will be included in the returned list. * So, the returned list may not necessarily be ALL regions in this table, its all the ONLINE * regions in the table. * * @param tableName * @return Online regions from <code>tableName</code> */ public synchronized List<HRegionInfo> getRegionsOfTable(TableName tableName) { List<HRegionInfo> tableRegions = new ArrayList<HRegionInfo>(); // boundary needs to have table's name but regionID 0 so that it is sorted // before all table's regions. HRegionInfo boundary = new HRegionInfo(tableName, null, null, false, 0L); for (HRegionInfo hri : regionAssignments.tailMap(boundary).keySet()) { if (!hri.getTable().equals(tableName)) break; tableRegions.add(hri); } return tableRegions; }
public void removeTailsWithSmallerValues(int key, int value) { List<Integer> toRemove = new ArrayList<>(); SortedMap<Integer, Integer> tailSet = map.tailMap(key); for (Entry<Integer, Integer> step : tailSet.entrySet()) { if (step.getValue() < value) toRemove.add(step.getKey()); else break; } for (Integer i : toRemove) { map.remove(i); } }
/** @see it.matrix.aod.postel.at.ATParserImpl#buildComunicazioniDataMap(java.util.TreeMap) */ @Override public SortedMap<Integer, String> buildComunicazioniDataMap(TreeMap<Integer, String> rawMap) { Iterator<Entry<Integer, String>> mit = rawMap.entrySet().iterator(); Integer fromKey = 0; while (mit.hasNext()) { Entry<Integer, String> entry = (Entry<Integer, String>) mit.next(); if (entry.getValue().startsWith("!SPA 1") && entry.getKey() > 12) { fromKey = entry.getKey(); } } return rawMap.tailMap(fromKey, false); }
@Override public InetSocketAddress route(byte[] key, int partition) { TreeMap<Integer, InetSocketAddress> circle = circleMap.get(partition); if (circle == null || circle.isEmpty()) { return null; } int hash = (int) hashProvider.hash(key); if (!circle.containsKey(hash)) { SortedMap<Integer, InetSocketAddress> tailMap = circle.tailMap(hash); hash = tailMap.isEmpty() ? circle.firstKey() : tailMap.firstKey(); } return circle.get(hash); }
/** 对Key进行哈希并返回哈希环上对应的节点。 参考了Jedis的Sharded.java */ public JedisTemplate getShard(String key) { if (singleTemplate != null) { return singleTemplate; } SortedMap<Long, JedisTemplate> tail = nodes.tailMap(algo.hash(key)); // 已到最后,返回第一个点. if (tail.isEmpty()) { return nodes.get(nodes.firstKey()); } return tail.get(tail.firstKey()); }
@Override public List<FilePath> newDirectoryStream() { ArrayList<FilePath> list = New.arrayList(); synchronized (MEMORY_FILES) { for (String n : MEMORY_FILES.tailMap(name).keySet()) { if (n.startsWith(name)) { list.add(getPath(n)); } else { break; } } return list; } }
/** * clones this price signal after the given time and returns the result * * @param timestamp * @return a clone of this price signal after the timestamp */ public PriceSignal cloneAfter(long timestamp) { PriceSignal priceSignal = new PriceSignal(this.commodity); double startCorrection = getPrice(timestamp); if (startCorrection != UNKNOWN_PRICE) priceSignal.prices.put(timestamp, startCorrection); for (Entry<Long, Double> en : prices.tailMap(timestamp).entrySet()) { priceSignal.prices.put(en.getKey(), en.getValue()); } priceSignal.priceUnknownAtAndAfter = this.priceUnknownAtAndAfter; priceSignal.priceUnknownBefore = timestamp; return priceSignal; }
public static void main(String[] args) { TreeMap<Integer, String> sortedMap = new TreeMap<Integer, String>(new CountingMapData(10)); print(sortedMap); Integer low = sortedMap.firstKey(); Integer high = sortedMap.lastKey(); print(low); print(high); Iterator<Integer> it = sortedMap.keySet().iterator(); for (int i = 0; i <= 6; i++) { if (i == 3) low = it.next(); if (i == 6) high = it.next(); else it.next(); } print(low); print(high); print(sortedMap.subMap(low, high)); print(sortedMap.headMap(high)); print(sortedMap.tailMap(low)); }
protected synchronized long getNumberOfEntries(Position position) { long count = 0; // First count the number of unread entries in the ledger pointed by // position if (position.getLedgerId() >= 0) count += ledgers.get(position.getLedgerId()).getEntriesCount() - position.getEntryId(); // Then, recur all the next ledgers and sum all the entries they contain for (LedgerStat ls : ledgers.tailMap(position.getLedgerId(), false).values()) { count += ls.getEntriesCount(); } // Last add the entries in the current ledger if (state != State.ClosedLedger) { count += currentLedger.getLastAddConfirmed() + 1; } return count; }
/** * 根据key值获得落到的虚拟节点信息 @Title: getVisualCacheNode * * @param key * @author: zzh * @date: 2016年12月3日 下午7:58:55 * @version: v1.0.0 */ private static VisualCacheNode getVisualCacheNode(String dataKey) { // 确认dataKey的hash值。 int dataKeyHash = DJBHash(dataKey); // 查找距离当前key值最近的一个虚拟节点 SortedMap<Integer, VisualCacheNode> vnodes = visualNodeMap.tailMap(dataKeyHash); // 最近的一个节点: VisualCacheNode nearestCacheNode = null; if (vnodes == null || vnodes.size() == 0) { // 找最小的一个数据 nearestCacheNode = visualNodeMap.get(visualNodeMap.firstKey()); System.out.println( dataKey + " 未找到虚拟节点,当前hashkey=" + dataKeyHash + ",使用最小节点存储:" + nearestCacheNode.getNodeName()); } else { nearestCacheNode = vnodes.get(vnodes.firstKey()); } return nearestCacheNode; }
@Override public synchronized List<? extends Collection<T>> getMatchingRequests( Priority priority, String resourceName, Resource capability) { Preconditions.checkArgument( capability != null, "The Resource to be requested should not be null "); Preconditions.checkArgument( priority != null, "The priority at which to request containers should not be null "); List<LinkedHashSet<T>> list = new LinkedList<LinkedHashSet<T>>(); Map<String, TreeMap<Resource, ResourceRequestInfo>> remoteRequests = this.remoteRequestsTable.get(priority); if (remoteRequests == null) { return list; } TreeMap<Resource, ResourceRequestInfo> reqMap = remoteRequests.get(resourceName); if (reqMap == null) { return list; } ResourceRequestInfo resourceRequestInfo = reqMap.get(capability); if (resourceRequestInfo != null && !resourceRequestInfo.containerRequests.isEmpty()) { list.add(resourceRequestInfo.containerRequests); return list; } // no exact match. Container may be larger than what was requested. // get all resources <= capability. map is reverse sorted. SortedMap<Resource, ResourceRequestInfo> tailMap = reqMap.tailMap(capability); for (Map.Entry<Resource, ResourceRequestInfo> entry : tailMap.entrySet()) { if (canFit(entry.getKey(), capability) && !entry.getValue().containerRequests.isEmpty()) { // match found that fits in the larger resource list.add(entry.getValue().containerRequests); } } // no match found return list; }
public void newOrder(long orderNumber, double price, String passCode) throws IllegalStateValueDataModificationException { if (!this.passCode.equals(passCode)) { throw new IllegalStateValueDataModificationException(); } correlator.addLiveState(this); TreeMap<Integer, Double> neg = new TreeMap<Integer, Double>(collapsedDist.headMap(0)); SortedMap<Integer, Double> pos = collapsedDist.tailMap(0); int shortProfit = 0; int shortLoss = 0; // System.out.println("Distribution: "); for (int returnClass : neg.descendingKeySet()) { double weight = neg.get(returnClass); if (weight / sumWeights > correlator.confidenceProfit) { shortProfit = returnClass; break; } } for (int returnClass : neg.descendingKeySet()) { double weight = neg.get(returnClass); if (weight / sumWeights > correlator.confidenceLoss) { shortLoss = returnClass; break; } } int longProfit = 0; int longLoss = 0; for (int returnClass : pos.keySet()) { double weight = pos.get(returnClass); if (weight / sumWeights > correlator.confidenceProfit) { longProfit = returnClass; break; } } for (int returnClass : pos.keySet()) { double weight = pos.get(returnClass); if (weight / sumWeights > correlator.confidenceLoss) { longLoss = returnClass; break; } } if (longProfit == 0) { longProfit = (int) (10 * Math.random()); } if (shortProfit == 0) { shortProfit = (int) (-10 * Math.random()); } double takeProfit = price + (longProfit * res); // double stopLoss = price - (shortLoss*res); double stopLoss = price - (longProfit * res); if (Math.abs(shortProfit) > longProfit) { takeProfit = price + (shortProfit * res); // stopLoss = price + (longLoss*res); stopLoss = price - (shortProfit * res); } openOrders.put(orderNumber, new OpenOrder("" + orderNumber, price, takeProfit, stopLoss)); // System.out.println("New Order from " + state + ": " + orderNumber + ": " + price + " " + // takeProfit + " " + stopLoss); }
protected NavigableMap<RecordKey, RecordPosition> getSubmap( final RecordKey key, final Browser b) { return records.tailMap(key, true); }
/** * Returns the smallest key in the symbol table greater than or equal to <tt>key</tt>. * * @return the smallest key in the symbol table greater than or equal to <tt>key</tt> * @param key the key * @throws NoSuchElementException if the symbol table is empty * @throws NullPointerException if <tt>key</tt> is <tt>null</tt> */ public Key ceil(Key key) { if (key == null) throw new NullPointerException("called ceil() with null key"); SortedMap<Key, Value> tail = st.tailMap(key); if (tail.isEmpty()) throw new NoSuchElementException(); return tail.firstKey(); }
public final void matchEvent(EventBean theEvent, Collection<FilterHandle> matches) { Object propertyValue = lookupable.getGetter().get(theEvent); if (propertyValue == null) { return; } // A undefine lower bound indicates an empty index if (lowerBounds == null) { return; } FilterOperator filterOperator = this.getFilterOperator(); Double propertyValueDouble = ((Number) propertyValue).doubleValue(); // Based on current lower and upper bounds check if the property value falls outside - shortcut // submap generation if ((filterOperator == FilterOperator.GREATER) && (propertyValueDouble <= lowerBounds)) { return; } else if ((filterOperator == FilterOperator.GREATER_OR_EQUAL) && (propertyValueDouble < lowerBounds)) { return; } else if ((filterOperator == FilterOperator.LESS) && (propertyValueDouble >= upperBounds)) { return; } else if ((filterOperator == FilterOperator.LESS_OR_EQUAL) && (propertyValueDouble > upperBounds)) { return; } // Look up in table constantsMapRWLock.readLock().lock(); try { // Get the head or tail end of the map depending on comparison type Map<Object, EventEvaluator> subMap; if ((filterOperator == FilterOperator.GREATER) || (filterOperator == FilterOperator.GREATER_OR_EQUAL)) { // At the head of the map are those with a lower numeric constants subMap = constantsMap.headMap(propertyValue); } else { subMap = constantsMap.tailMap(propertyValue); } // All entries in the subMap are elgibile, with an exception EventEvaluator exactEquals = null; if (filterOperator == FilterOperator.LESS) { exactEquals = constantsMap.get(propertyValue); } for (EventEvaluator matcher : subMap.values()) { // For the LESS comparison type we ignore the exactly equal case // The subMap is sorted ascending, thus the exactly equals case is the first if (exactEquals != null) { exactEquals = null; continue; } matcher.matchEvent(theEvent, matches); } if (filterOperator == FilterOperator.GREATER_OR_EQUAL) { EventEvaluator matcher = constantsMap.get(propertyValue); if (matcher != null) { matcher.matchEvent(theEvent, matches); } } } finally { constantsMapRWLock.readLock().unlock(); } }
/** * Monta a estrutura de arquivos e diretorios filhos do diretorio fornecido. * * @param dirPath Diretorio pai. */ private void getDirImpl(final String dirPath) { currentPath = dirPath; final List<String> item = new ArrayList<String>(); path = new ArrayList<String>(); mList = new ArrayList<HashMap<String, Object>>(); File f = new File(currentPath); File[] files = f.listFiles(); if (files == null) { currentPath = ROOT; f = new File(currentPath); files = f.listFiles(); } myPath.setText(getText(R.string.location) + ": " + currentPath); if (!currentPath.equals(ROOT)) { item.add(ROOT); addItem(ROOT, R.drawable.folder); path.add(ROOT); item.add("../"); addItem("../", R.drawable.folder); path.add(f.getParent()); parentPath = f.getParent(); } TreeMap<String, String> dirsMap = new TreeMap<String, String>(); TreeMap<String, String> dirsPathMap = new TreeMap<String, String>(); TreeMap<String, String> filesMap = new TreeMap<String, String>(); TreeMap<String, String> filesPathMap = new TreeMap<String, String>(); for (File file : files) { if (file.isDirectory()) { String dirName = file.getName(); dirsMap.put(dirName, dirName); dirsPathMap.put(dirName, file.getPath()); } else { final String fileName = file.getName(); final String fileNameLwr = fileName.toLowerCase(); // se ha um filtro de formatos, utiliza-o if (formatFilter != null) { boolean contains = false; for (int i = 0; i < formatFilter.length; i++) { final String formatLwr = formatFilter[i].toLowerCase(); if (fileNameLwr.endsWith(formatLwr)) { contains = true; break; } } if (contains) { filesMap.put(fileName, fileName); filesPathMap.put(fileName, file.getPath()); } // senao, adiciona todos os arquivos } else { filesMap.put(fileName, fileName); filesPathMap.put(fileName, file.getPath()); } } } item.addAll(dirsMap.tailMap("").values()); item.addAll(filesMap.tailMap("").values()); path.addAll(dirsPathMap.tailMap("").values()); path.addAll(filesPathMap.tailMap("").values()); SimpleAdapter fileList = new SimpleAdapter( this, mList, R.layout.file_dialog_row, new String[] {ITEM_KEY, ITEM_IMAGE}, new int[] {R.id.fdrowtext, R.id.fdrowimage}); for (String dir : dirsMap.tailMap("").values()) { addItem(dir, R.drawable.folder); } for (String file : filesMap.tailMap("").values()) { addItem(file, R.drawable.file); } fileList.notifyDataSetChanged(); setListAdapter(fileList); }