/** * query a set of tra from this single cell * * @param inTraSet * @return */ public ArrayList<Entry<Long, GridLeafTraHashItem>> queryTraSet(ArrayList<Long> inTraSet) { // store result HashMap<Long, GridLeafTraHashItem> resHash = new HashMap<Long, GridLeafTraHashItem>(); // collect the entry according their page id HashMap<Integer, ArrayList<Long>> collect = new HashMap<Integer, ArrayList<Long>>(); if (null == inTraSet) return null; // divide the tra by their page id for (Long traItem : inTraSet) { int timeItem = Configuration.getTime(traItem) + Configuration.T_Sample; // time + T_Smaple to get next time int itemPageId = getPageIdByTime( timeItem); // get page id, if page id is PAGE_ID_CUR, it is stored in current tra hash // put this tra into collect hashmap ArrayList<Long> itemLongArray = collect.get(itemPageId); if (null == itemLongArray) { itemLongArray = new ArrayList<Long>(); collect.put(itemPageId, itemLongArray); } itemLongArray.add(traItem); // put tra_id in the same page together } Iterator<Entry<Integer, ArrayList<Long>>> itr = collect.entrySet().iterator(); while (itr.hasNext()) { Entry<Integer, ArrayList<Long>> pageItem = itr.next(); LinkedHashMap<Long, GridLeafTraHashItem> pageTraHash; if (PAGE_ID_CUR == pageItem.getKey()) { // return current traHash pageTraHash = traHash; } else if (PAGE_ID_NIL == pageItem.getKey()) { pageTraHash = null; } else { pageTraHash = loadPageHashTra(pageItem.getKey()); // load from disk } // visit every item in this pageTraHash if (pageTraHash != null) { // if trahash cannot be found, ignore it for (Long key : pageItem.getValue()) { int keyTraId = Configuration.getTraId(key); // id int keyTime = Configuration.getTime(key) + Configuration.T_Sample; // cur+T_Sample GridLeafTraHashItem glItem = queryTraIdTimePerTraHash(pageTraHash, keyTraId, keyTime); // get item if (null != glItem) { // the end of trajectory, do not get result Long newKey = Configuration.getKey(keyTraId, keyTime); resHash.put(newKey, glItem); } } } } ArrayList<Entry<Long, GridLeafTraHashItem>> res = new ArrayList<Entry<Long, GridLeafTraHashItem>>(resHash.entrySet()); return res; }
/** * query each traHash by queryTime, all points that are more recent(with larger timestamp) than * inQueryTime are included in query result * * @param inTraLinkedHash * @param inQueryTime * @param outRes */ private void queryTimeRangePerTraHash( LinkedHashMap<Long, GridLeafTraHashItem> inTraLinkedHash, int inQueryTime, ArrayList<Entry<Long, GridLeafTraHashItem>> outRes) { // visit the linkedhashmap by reverse order, more recent traId has more larger timestamp ListIterator<Entry<Long, GridLeafTraHashItem>> list = new ArrayList<Entry<Long, GridLeafTraHashItem>>(inTraLinkedHash.entrySet()) .listIterator(inTraLinkedHash.size()); while (list.hasPrevious()) { Entry<Long, GridLeafTraHashItem> item = (Entry<Long, GridLeafTraHashItem>) list.previous(); int itemTime = Configuration.getTime(item.getKey()); // the more recent traId, the larger timestamp if (itemTime >= inQueryTime) { outRes.add(item); } } }