/** * @return Copy of this list. */ public GridLongList copy() { if (idx == 0) return new GridLongList(); return new GridLongList(Arrays.copyOf(arr, idx)); }
/** * @param vals Values. * @return List from values. */ public static GridLongList asList(long... vals) { if (F.isEmpty(vals)) return new GridLongList(); return new GridLongList(vals); }
/** * @param in Input to read list from. * @return Grid long list. * @throws IOException If failed. */ @Nullable public static GridLongList readFrom(DataInput in) throws IOException { int idx = in.readInt(); if (idx == -1) return null; long[] arr = new long[idx]; for (int i = 0; i < idx; i++) arr[i] = in.readLong(); return new GridLongList(arr); }
private List<GridLongList> createAssigns(List<List<ClusterNode>> assign0) { if (assign0 != null) { List<GridLongList> assigns = new ArrayList<>(assign0.size()); for (int i = 0; i < assign0.size(); i++) { List<ClusterNode> nodes = assign0.get(i); GridLongList l = new GridLongList(nodes.size()); for (int n = 0; n < nodes.size(); n++) l.add(nodes.get(n).order()); assigns.add(l); } return assigns; } return null; }
/** * Returns (possibly reordered) copy of this list, excluding all elements of given list. * * @param l List of elements to remove. * @return New list without all elements from {@code l}. */ public GridLongList copyWithout(GridLongList l) { assert l != null; if (idx == 0) return new GridLongList(); if (l.idx == 0) return new GridLongList(Arrays.copyOf(arr, idx)); long[] newArr = Arrays.copyOf(arr, idx); int newIdx = idx; for (int i = 0; i < l.size(); i++) { long rmVal = l.get(i); for (int j = 0; j < newIdx; j++) { if (newArr[j] == rmVal) { while (newIdx > 0 && newArr[newIdx - 1] == rmVal) newIdx--; if (newIdx > 0) { newArr[j] = newArr[newIdx - 1]; newIdx--; } } } } return new GridLongList(newArr, newIdx); }
/** * @param assign0 Assignment. * @param idealAssign0 Ideal assignment. * @param assignDiff0 Difference with ideal affinity assignment. */ private CacheGroupAffinityMessage(List<List<ClusterNode>> assign0, List<List<ClusterNode>> idealAssign0, Map<Integer, List<Long>> assignDiff0) { if (assign0 != null) assigns = createAssigns(assign0); if (idealAssign0 != null) idealAssigns = createAssigns(idealAssign0); if (assignDiff0 != null) { assignsDiff = U.newHashMap(assignDiff0.size()); for (Map.Entry<Integer, List<Long>> e : assignDiff0.entrySet()) { List<Long> orders = e.getValue(); GridLongList l = new GridLongList(orders.size()); for (int n = 0; n < orders.size(); n++) l.add(orders.get(n)); assignsDiff.put(e.getKey(), l); } } }
/** * @param to To list. * @param from From list. * @return To list (passed in or created). */ public static GridLongList addAll(@Nullable GridLongList to, GridLongList from) { if (to == null) { GridLongList res = new GridLongList(from.size()); res.addAll(from); return res; } else { to.addAll(from); return to; } }
/** * @param keyIdx Key index. * @param ttl TTL for near cache update. * @param expireTime Expire time for near cache update. */ void addNearTtl(int keyIdx, long ttl, long expireTime) { if (ttl >= 0) { if (nearTtls == null) { nearTtls = new GridLongList(16); for (int i = 0; i < keyIdx; i++) nearTtls.add(-1L); } } if (nearTtls != null) nearTtls.add(ttl); if (expireTime >= 0) { if (nearExpireTimes == null) { nearExpireTimes = new GridLongList(16); for (int i = 0; i < keyIdx; i++) nearExpireTimes.add(-1); } } if (nearExpireTimes != null) nearExpireTimes.add(expireTime); }
public void testCopyWithout() throws Exception { assertCopy( new GridLongList(new long[] {}), new GridLongList(new long[] {})); new GridLongList(new long[] {}), new GridLongList(new long[] {1})); new GridLongList(new long[] {1}), new GridLongList(new long[] {})); new GridLongList(new long[] {1, 2, 3}), new GridLongList(new long[] {4, 5, 6})); new GridLongList(new long[] {1, 2, 3}), new GridLongList(new long[] {1, 2, 3})); new GridLongList(new long[] {1, 2, 3, 4, 5, 1}), new GridLongList(new long[] {1, 1})); new GridLongList(new long[] {1, 1, 1, 2, 3, 4, 5, 1, 1, 1}), new GridLongList(new long[] {1, 1})); new GridLongList(new long[] {1, 2, 3}), new GridLongList(new long[] {1, 1, 2, 2, 3, 3}));
/** * Flushes pending update counters closing all possible gaps. * * @return Even-length array of pairs [start, end] for each gap. */ public synchronized GridLongList finalizeUpdateCounters() { Item item = poll(); GridLongList gaps = null; while (item != null) { if (gaps == null) gaps = new GridLongList((queue.size() + 1) * 2); long start = cntr.get() + 1; long end = item.start; gaps.add(start); gaps.add(end); // Close pending ranges. update(item.start + item.delta); item = poll(); } return gaps; }
/** * @param pageAddr Page address. * @param res Results map. */ public void getBucketsData(long pageAddr, Map<Integer, GridLongList> res) { int cnt = getCount(pageAddr); assert cnt >= 0 && cnt <= Short.MAX_VALUE : cnt; if (cnt == 0) return; int off = offset(0); for (int i = 0; i < cnt; i++) { int bucket = (int)PageUtils.getShort(pageAddr, off); assert bucket >= 0 && bucket <= Short.MAX_VALUE : bucket; long tailId = PageUtils.getLong(pageAddr, off + 2); assert tailId != 0; GridLongList list = res.get(bucket); if (list == null) res.put(bucket, list = new GridLongList()); list.add(tailId); off += ITEM_SIZE; } }
private GridLongList sorted(Collection<Integer> col) { GridLongList lst = new GridLongList(col.size()); for (Integer i : col) lst.add(i); return lst.sort(); } }
/** * @param part Partition. * @param primaryId Primary ID. * @param futId Future ID. */ private void sendDeferredUpdateResponse(int part, UUID primaryId, long futId) { Map<UUID, GridDhtAtomicDeferredUpdateResponse> resMap = defRes.get(); GridDhtAtomicDeferredUpdateResponse msg = resMap.get(primaryId); if (msg == null) { msg = new GridDhtAtomicDeferredUpdateResponse(ctx.cacheId(), new GridLongList(DEFERRED_UPDATE_RESPONSE_BUFFER_SIZE)); if (DEFERRED_UPDATE_RESPONSE_TIMEOUT > 0) { GridTimeoutObject timeoutSnd = new DeferredUpdateTimeout(part, primaryId); msg.timeoutSender(timeoutSnd); ctx.time().addTimeoutObject(timeoutSnd); } resMap.put(primaryId, msg); } GridLongList futIds = msg.futureIds(); assert futIds.size() < DEFERRED_UPDATE_RESPONSE_BUFFER_SIZE : futIds.size(); futIds.add(futId); if (futIds.size() >= DEFERRED_UPDATE_RESPONSE_BUFFER_SIZE) { resMap.remove(primaryId); sendDeferredUpdateResponse(primaryId, msg); } }
GridLongList ptrs = new GridLongList(chunkSize);
/** {@inheritDoc} */ @Override public GridLongList removeIf(int startIdxToClear, int endIdxToClear, KeyPredicate keyPred) { assert endIdxToClear > startIdxToClear : "Start and end indexes are not consistent: {" + startIdxToClear + ", " + endIdxToClear + "}"; int sz = endIdxToClear - startIdxToClear; GridLongList list = new GridLongList(sz); for (int idx = startIdxToClear; idx < endIdxToClear; idx++) { long base = entryBase(idx); int grpId = GridUnsafe.getInt(base); long pageId = GridUnsafe.getLong(base + PAGE_ID_OFFSET); if (isRemoved(grpId, pageId) || isEmpty(grpId, pageId)) continue; if (!keyPred.test(grpId, pageId)) continue; long res = valueAt(idx); setRemoved(idx); list.add(res); } return list; }
/** {@inheritDoc} */ @Override public GridLongList removeIf(int startIdxToClear, int endIdxToClear, KeyPredicate keyPred) { assert endIdxToClear >= startIdxToClear : "Start and end indexes are not consistent: {" + startIdxToClear + ", " + endIdxToClear + "}"; int sz = endIdxToClear - startIdxToClear; GridLongList list = new GridLongList(sz); for (int idx = startIdxToClear; idx < endIdxToClear; idx++) { long base = entryBase(idx); int grpId = getGrpId(base); long pageId = getPageId(base); if (isEmpty(grpId, pageId)) continue; // absent value, no removal required if (!keyPred.test(grpId, pageId)) continue; // not matched value, no removal required long valAt = getValue(base); setSize(size() - 1); doBackwardShift(idx); list.add(valAt); idx--; //Need recheck current cell because of backward shift } return list; }
/** * */ @Test public void testTruncate() { GridLongList list = asList(1, 2, 3, 4, 5, 6, 7, 8); list.truncate(4, true); assertEquals(asList(1, 2, 3, 4), list); list.truncate(2, false); assertEquals(asList(3, 4), list); list = new GridLongList(); list.truncate(0, false); list.truncate(0, true); assertEquals(new GridLongList(), list); }
GridLongList activeQryTrackers = new GridLongList();
/** * */ @Test public void testArray() { GridLongList list = new GridLongList(); long[] array = list.array(); assertNotNull(array); assertEquals(0, array.length); list.add(1L); array = list.array(); assertNotNull(array); assertEquals(1, array.length); assertEquals(1L, array[0]); } }
/** * */ @Test public void testSort() { assertEquals(new GridLongList(), new GridLongList().sort()); assertEquals(asList(1), asList(1).sort()); assertEquals(asList(1, 2), asList(2, 1).sort()); assertEquals(asList(1, 2, 3), asList(2, 1, 3).sort()); GridLongList list = new GridLongList(); list.add(4); list.add(3); list.add(5); list.add(1); assertEquals(asList(1, 3, 4, 5), list.sort()); list.add(0); assertEquals(asList(1, 3, 4, 5, 0), list); assertEquals(asList(0, 1, 3, 4, 5), list.sort()); }