List of usage examples for java.util Comparator compare
int compare(T o1, T o2);
From source file:gedi.util.ArrayUtils.java
/** * Sorts the specified sub-array of longs into ascending order. *///from ww w . j av a 2s . c o m private static <T, P> void sort1(T x[], int[] a2, Comparator<T> comp, int off, int len) { // Insertion sort on smallest arrays if (len < 7) { for (int i = off; i < len + off; i++) for (int j = i; j > off && comp.compare(x[j - 1], x[j]) > 0; j--) swap(x, a2, j, j - 1); return; } // Choose a partition element, v int m = off + (len >> 1); // Small arrays, middle element if (len > 7) { int l = off; int n = off + len - 1; if (len > 40) { // Big arrays, pseudomedian of 9 int s = len / 8; l = med3(x, comp, l, l + s, l + 2 * s); m = med3(x, comp, m - s, m, m + s); n = med3(x, comp, n - 2 * s, n - s, n); } m = med3(x, comp, l, m, n); // Mid-size, med of 3 } T v = x[m]; // Establish Invariant: v* (<v)* (>v)* v* int a = off, b = a, c = off + len - 1, d = c; while (true) { while (b <= c && comp.compare(x[b], v) <= 0) { if (x[b] == v) swap(x, a2, a++, b); b++; } while (c >= b && comp.compare(x[c], v) >= 0) { if (x[c] == v) swap(x, a2, c, d--); c--; } if (b > c) break; swap(x, a2, b++, c--); } // Swap partition elements back to middle int s, n = off + len; s = Math.min(a - off, b - a); vecswap(x, a2, off, b - s, s); s = Math.min(d - c, n - d - 1); vecswap(x, a2, b, n - s, s); // Recursively sort non-partition-elements if ((s = b - a) > 1) sort1(x, a2, comp, off, s); if ((s = d - c) > 1) sort1(x, a2, comp, n - s, s); }
From source file:gedi.util.ArrayUtils.java
/** * Src is the source array that starts at index 0 * Dest is the (possibly larger) array destination with a possible offset * low is the index in dest to start sorting * high is the end index in dest to end sorting * off is the offset into src corresponding to low in dest *//*from w w w . ja v a2 s. c o m*/ private static <T, S> void mergeSort(T[] src, S[] src2, T[] dest, S[] dest2, int low, int high, int off, Comparator c) { int length = high - low; // Insertion sort on smallest arrays if (length < INSERTIONSORT_THRESHOLD) { for (int i = low; i < high; i++) for (int j = i; j > low && c.compare(dest[j - 1], dest[j]) > 0; j--) swap(dest, dest2, j, j - 1); return; } // Recursively sort halves of dest into src int destLow = low; int destHigh = high; low += off; high += off; int mid = (low + high) >>> 1; mergeSort(dest, dest2, src, src2, low, mid, -off, c); mergeSort(dest, dest2, src, src2, mid, high, -off, c); // If list is already sorted, just copy from src to dest. This is an // optimization that results in faster sorts for nearly ordered lists. if (c.compare(src[mid - 1], src[mid]) <= 0) { System.arraycopy(src, low, dest, destLow, length); System.arraycopy(src2, low, dest2, destLow, length); return; } // Merge sorted halves (now in src) into dest for (int i = destLow, p = low, q = mid; i < destHigh; i++) { if (q >= high || p < mid && c.compare(src[p], src[q]) <= 0) { dest2[i] = src2[p]; dest[i] = src[p++]; } else { dest2[i] = src2[q]; dest[i] = src[q++]; } } }
From source file:gedi.util.ArrayUtils.java
/** * Returns the sorted permutation of a, i.e. re[0] contains the index of the smallest value in a, * re[1] the second smallest and so on.//from w w w. j a va2s. c om * @param <T> * @param a * @param comp * @return */ public static <T> int[] sortedPermutation(final T[] a, final Comparator<? super T> comp) { Integer[] re = new Integer[a.length]; for (int i = 0; i < re.length; i++) re[i] = i; Arrays.sort(re, new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return comp.compare(a[o1], a[o2]); } }); return intCollectionToPrimitiveArray(Arrays.asList(re)); }
From source file:org.wso2.carbon.apimgt.impl.APIConsumerImpl.java
/** * The method to get APIs to Store view * * * @return Set<API> Set of APIs/*w w w. j a va2 s. c o m*/ * @throws APIManagementException */ @Override @Deprecated public Map<String, Object> getAllPaginatedPublishedAPIs(String tenantDomain, int start, int end) throws APIManagementException { Boolean displayAPIsWithMultipleStatus = APIUtil.isAllowDisplayAPIsWithMultipleStatus(); Map<String, List<String>> listMap = new HashMap<String, List<String>>(); //Check the api-manager.xml config file entry <DisplayAllAPIs> value is false if (!displayAPIsWithMultipleStatus) { //Create the search attribute map listMap.put(APIConstants.API_OVERVIEW_STATUS, new ArrayList<String>() { { add(APIConstants.PUBLISHED); } }); } else { return getAllPaginatedAPIs(tenantDomain, start, end); } Map<String, Object> result = new HashMap<String, Object>(); SortedSet<API> apiSortedSet = new TreeSet<API>(new APINameComparator()); SortedSet<API> apiVersionsSortedSet = new TreeSet<API>(new APIVersionComparator()); int totalLength = 0; try { Registry userRegistry; boolean isTenantMode = (tenantDomain != null); if ((isTenantMode && this.tenantDomain == null) || (isTenantMode && isTenantDomainNotMatching(tenantDomain))) {//Tenant store anonymous mode int tenantId = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager() .getTenantId(tenantDomain); // explicitly load the tenant's registry APIUtil.loadTenantRegistry(tenantId); userRegistry = ServiceReferenceHolder.getInstance().getRegistryService() .getGovernanceUserRegistry(CarbonConstants.REGISTRY_ANONNYMOUS_USERNAME, tenantId); PrivilegedCarbonContext.getThreadLocalCarbonContext() .setUsername(CarbonConstants.REGISTRY_ANONNYMOUS_USERNAME); } else { userRegistry = registry; PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(this.username); } this.isTenantModeStoreView = isTenantMode; this.requestedTenant = tenantDomain; Map<String, API> latestPublishedAPIs = new HashMap<String, API>(); List<API> multiVersionedAPIs = new ArrayList<API>(); Comparator<API> versionComparator = new APIVersionComparator(); Boolean displayMultipleVersions = APIUtil.isAllowDisplayMultipleVersions(); PaginationContext.init(start, end, "ASC", APIConstants.API_OVERVIEW_NAME, Integer.MAX_VALUE); GenericArtifactManager artifactManager = APIUtil.getArtifactManager(userRegistry, APIConstants.API_KEY); if (artifactManager != null) { GenericArtifact[] genericArtifacts = artifactManager.findGenericArtifacts(listMap); totalLength = PaginationContext.getInstance().getLength(); if (genericArtifacts == null || genericArtifacts.length == 0) { result.put("apis", apiSortedSet); result.put("totalLength", totalLength); return result; } for (GenericArtifact artifact : genericArtifacts) { // adding the API provider can mark the latest API . API api = APIUtil.getAPI(artifact); if (api != null) { String key; //Check the configuration to allow showing multiple versions of an API true/false if (!displayMultipleVersions) { //If allow only showing the latest version of an API key = api.getId().getProviderName() + COLON_CHAR + api.getId().getApiName(); API existingAPI = latestPublishedAPIs.get(key); if (existingAPI != null) { // If we have already seen an API with the same name, make sure // this one has a higher version number if (versionComparator.compare(api, existingAPI) > 0) { latestPublishedAPIs.put(key, api); } } else { // We haven't seen this API before latestPublishedAPIs.put(key, api); } } else { //If allow showing multiple versions of an API multiVersionedAPIs.add(api); } } } if (!displayMultipleVersions) { apiSortedSet.addAll(latestPublishedAPIs.values()); result.put("apis", apiSortedSet); result.put("totalLength", totalLength); return result; } else { apiVersionsSortedSet.addAll(multiVersionedAPIs); result.put("apis", apiVersionsSortedSet); result.put("totalLength", totalLength); return result; } } } catch (RegistryException e) { handleException("Failed to get all published APIs", e); } catch (UserStoreException e) { handleException("Failed to get all published APIs", e); } finally { PaginationContext.destroy(); } result.put("apis", apiSortedSet); result.put("totalLength", totalLength); return result; }
From source file:org.wso2.carbon.apimgt.impl.APIConsumerImpl.java
/** * The method to get APIs to Store view//w w w .j a v a 2 s . c o m * * @return Set<API> Set of APIs * @throws APIManagementException */ @Override public Set<API> getAllPublishedAPIs(String tenantDomain) throws APIManagementException { SortedSet<API> apiSortedSet = new TreeSet<API>(new APINameComparator()); SortedSet<API> apiVersionsSortedSet = new TreeSet<API>(new APIVersionComparator()); try { Registry userRegistry; boolean isTenantMode = (tenantDomain != null); if ((isTenantMode && this.tenantDomain == null) || (isTenantMode && isTenantDomainNotMatching(tenantDomain))) {//Tenant store anonymous mode int tenantId = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager() .getTenantId(tenantDomain); userRegistry = ServiceReferenceHolder.getInstance().getRegistryService() .getGovernanceUserRegistry(CarbonConstants.REGISTRY_ANONNYMOUS_USERNAME, tenantId); } else { userRegistry = registry; } this.isTenantModeStoreView = isTenantMode; this.requestedTenant = tenantDomain; GenericArtifactManager artifactManager = APIUtil.getArtifactManager(userRegistry, APIConstants.API_KEY); if (artifactManager != null) { GenericArtifact[] genericArtifacts = artifactManager.getAllGenericArtifacts(); if (genericArtifacts == null || genericArtifacts.length == 0) { return apiSortedSet; } Map<String, API> latestPublishedAPIs = new HashMap<String, API>(); List<API> multiVersionedAPIs = new ArrayList<API>(); Comparator<API> versionComparator = new APIVersionComparator(); Boolean displayMultipleVersions = APIUtil.isAllowDisplayMultipleVersions(); Boolean displayAPIsWithMultipleStatus = APIUtil.isAllowDisplayAPIsWithMultipleStatus(); for (GenericArtifact artifact : genericArtifacts) { // adding the API provider can mark the latest API . String status = artifact.getAttribute(APIConstants.API_OVERVIEW_STATUS); API api = null; //Check the api-manager.xml config file entry <DisplayAllAPIs> value is false if (!displayAPIsWithMultipleStatus) { // then we are only interested in published APIs here... if (APIConstants.PUBLISHED.equals(status)) { api = APIUtil.getAPI(artifact); } } else { // else we are interested in both deprecated/published APIs here... if (APIConstants.PUBLISHED.equals(status) || APIConstants.DEPRECATED.equals(status)) { api = APIUtil.getAPI(artifact); } } if (api != null) { String key; //Check the configuration to allow showing multiple versions of an API true/false if (!displayMultipleVersions) { //If allow only showing the latest version of an API key = api.getId().getProviderName() + COLON_CHAR + api.getId().getApiName(); API existingAPI = latestPublishedAPIs.get(key); if (existingAPI != null) { // If we have already seen an API with the same name, make sure // this one has a higher version number if (versionComparator.compare(api, existingAPI) > 0) { latestPublishedAPIs.put(key, api); } } else { // We haven't seen this API before latestPublishedAPIs.put(key, api); } } else { //If allow showing multiple versions of an API multiVersionedAPIs.add(api); } } } if (!displayMultipleVersions) { apiSortedSet.addAll(latestPublishedAPIs.values()); return apiSortedSet; } else { apiVersionsSortedSet.addAll(multiVersionedAPIs); return apiVersionsSortedSet; } } } catch (RegistryException e) { handleException("Failed to get all published APIs", e); } catch (UserStoreException e) { handleException("Failed to get all published APIs", e); } return apiSortedSet; }
From source file:com.amazon.carbonado.repo.indexed.ManagedIndex.java
/** * Build the entire index, repairing as it goes. * * @param repo used to enter transactions *//* w ww . j a v a2 s.c o m*/ void buildIndex(double desiredSpeed) throws RepositoryException { final MergeSortBuffer buffer; final Comparator c; final Log log = LogFactory.getLog(IndexedStorage.class); final Query<S> masterQuery; { // Need to explicitly order master query by primary key in order // for fetchAfter to work correctly in case corrupt records are // encountered. masterQuery = mMasterStorage.query().orderBy(naturalOrdering(mMasterStorage.getStorableType())); } // Quick check to see if any records exist in master. { Transaction txn = mRepository.enterTopTransaction(IsolationLevel.READ_COMMITTED); try { if (!masterQuery.exists()) { if (mIndexEntryStorage.query().exists()) { txn.exit(); mIndexEntryStorage.truncate(); } return; } } finally { txn.exit(); } } // Enter top transaction with isolation level of none to make sure // preload operation does not run in a long nested transaction. Transaction txn = mRepository.enterTopTransaction(IsolationLevel.NONE); try { Cursor<S> cursor = masterQuery.fetch(); try { if (log.isInfoEnabled()) { StringBuilder b = new StringBuilder(); b.append("Preparing index on "); b.append(mMasterStorage.getStorableType().getName()); b.append(": "); try { mIndex.appendTo(b); } catch (java.io.IOException e) { // Not gonna happen. } log.info(b.toString()); } // Preload and sort all index entries for improved performance. buffer = new MergeSortBuffer(mIndexEntryStorage, null, BUILD_SORT_BUFFER_SIZE); c = getComparator(); buffer.prepare(c); long nextReportTime = System.currentTimeMillis() + BUILD_INFO_DELAY_MILLIS; // These variables are used when corrupt records are encountered. S lastUserStorable = null; int skippedCount = 0; while (cursor.hasNext()) { S userStorable; try { userStorable = cursor.next(); skippedCount = 0; } catch (CorruptEncodingException e) { log.warn("Omitting corrupt record from index: " + e.toString()); // Exception forces cursor to close. Close again to be sure. cursor.close(); if (lastUserStorable == null) { cursor = masterQuery.fetch(); } else { cursor = masterQuery.fetchAfter(lastUserStorable); } cursor.skipNext(++skippedCount); continue; } buffer.add(makeIndexEntry(userStorable)); if (log.isInfoEnabled()) { long now = System.currentTimeMillis(); if (now >= nextReportTime) { log.info("Prepared " + buffer.size() + " index entries"); nextReportTime = now + BUILD_INFO_DELAY_MILLIS; } } lastUserStorable = userStorable; } // No need to commit transaction because no changes should have been made. } finally { cursor.close(); } } finally { txn.exit(); } // This is not expected to take long, since MergeSortBuffer sorts as // needed. This just finishes off what was not written to a file. buffer.sort(); if (isUnique()) { // If index is unique, scan buffer and check for duplicates // _before_ inserting index entries. If there are duplicates, // fail, since unique index cannot be built. if (log.isInfoEnabled()) { log.info("Verifying index"); } Object last = null; for (Object obj : buffer) { if (last != null) { if (c.compare(last, obj) == 0) { buffer.close(); throw new UniqueConstraintException("Cannot build unique index because duplicates exist: " + this + ", " + last + " == " + obj); } } last = obj; } } final int bufferSize = buffer.size(); if (log.isInfoEnabled()) { log.info("Begin build of " + bufferSize + " index entries"); } // Need this index entry query for deleting bogus entries. final Query indexEntryQuery = mIndexEntryStorage.query() .orderBy(naturalOrdering(mIndexEntryStorage.getStorableType())); Throttle throttle = desiredSpeed < 1.0 ? new Throttle(BUILD_THROTTLE_WINDOW) : null; long totalInserted = 0; long totalUpdated = 0; long totalDeleted = 0; long totalProgress = 0; txn = enterBuildTxn(); try { Cursor<? extends Storable> indexEntryCursor = indexEntryQuery.fetch(); Storable existingIndexEntry = null; if (!indexEntryCursor.hasNext()) { indexEntryCursor.close(); // Don't try opening again. indexEntryCursor = null; } boolean retry = false; Storable indexEntry = null; Storable lastIndexEntry = null; long nextReportTime = System.currentTimeMillis() + BUILD_INFO_DELAY_MILLIS; Iterator it = buffer.iterator(); bufferIterate: while (true) { if (!retry) { Object obj; if (it.hasNext()) { obj = it.next(); } else if (indexEntryCursor != null && indexEntryCursor.hasNext()) { obj = null; } else { break; } indexEntry = (Storable) obj; } try { if (indexEntry != null) { if (indexEntry.tryInsert()) { totalInserted++; } else { // Couldn't insert because an index entry already exists. Storable existing = indexEntry.copy(); boolean doUpdate = false; if (!existing.tryLoad()) { doUpdate = true; } else if (!existing.equalProperties(indexEntry)) { // If only the version differs, leave existing entry alone. indexEntry.copyVersionProperty(existing); doUpdate = !existing.equalProperties(indexEntry); } if (doUpdate) { indexEntry.tryDelete(); indexEntry.tryInsert(); totalUpdated++; } } } if (indexEntryCursor != null) while (true) { if (existingIndexEntry == null) { if (indexEntryCursor.hasNext()) { existingIndexEntry = indexEntryCursor.next(); } else { indexEntryCursor.close(); // Don't try opening again. indexEntryCursor = null; break; } } int compare = c.compare(existingIndexEntry, indexEntry); if (compare == 0) { // Existing entry cursor matches so allow cursor to advance. existingIndexEntry = null; break; } else if (compare > 0) { // Existing index entry is ahead so check later. break; } else { // Existing index entry might be bogus. Check again // in case master record changed. doDelete: { S master = mMasterStorage.prepare(); copyToMasterPrimaryKey(existingIndexEntry, master); if (master.tryLoad()) { Storable temp = makeIndexEntry(master); existingIndexEntry.copyVersionProperty(temp); if (existingIndexEntry.equalProperties(temp)) { break doDelete; } } existingIndexEntry.tryDelete(); totalDeleted++; if (totalDeleted % BUILD_BATCH_SIZE == 0) { txn.commit(); txn.exit(); nextReportTime = logProgress(nextReportTime, log, totalProgress, bufferSize, totalInserted, totalUpdated, totalDeleted); txn = enterBuildTxn(); indexEntryCursor.close(); indexEntryCursor = indexEntryQuery.fetchAfter(existingIndexEntry); if (!indexEntryCursor.hasNext()) { indexEntryCursor.close(); // Don't try opening again. indexEntryCursor = null; break; } } } existingIndexEntry = null; throttle(throttle, desiredSpeed); } } if (indexEntry != null) { totalProgress++; } lastIndexEntry = indexEntry; retry = false; } catch (RepositoryException e) { if (e instanceof FetchTimeoutException || e instanceof PersistTimeoutException) { log.warn("Lock conflict during index repair; will retry: " + indexEntry + ", " + e); // This re-uses the last index entry to repair and forces // the current transaction to commit. retry = true; } else { throw e; } } if (retry || (totalProgress % BUILD_BATCH_SIZE == 0)) { txn.commit(); txn.exit(); nextReportTime = logProgress(nextReportTime, log, totalProgress, bufferSize, totalInserted, totalUpdated, totalDeleted); txn = enterBuildTxn(); if (indexEntryCursor != null) { indexEntryCursor.close(); existingIndexEntry = null; if (indexEntry == null || lastIndexEntry == null) { indexEntryCursor = indexEntryQuery.fetch(); } else if (!retry) { indexEntryCursor = indexEntryQuery.fetchAfter(indexEntry); } else { // Re-fetch starting at the same spot. indexEntryCursor = indexEntryQuery.fetchAfter(lastIndexEntry); } } } throttle(throttle, desiredSpeed); } txn.commit(); } finally { txn.exit(); buffer.close(); } if (log.isInfoEnabled()) { log.info("Finished building " + totalProgress + " index entries " + progressSubMessgage(totalInserted, totalUpdated, totalDeleted)); } }
From source file:org.regenstrief.util.Util.java
/** * Given a List already sorted in accordance with a Comparator, adds a new element at the * appropriate index, allocating a new List if necessary * // ww w.j av a2 s . co m * @param l the sorted List * @param o the new element * @param cmp the Comparator * @param <E> the element type **/ public final static <E> List<E> add(List<E> l, final E o, final Comparator<E> cmp) { if (l == null) { l = new ArrayList<E>(); } int i = l.size() - 1; // Search backwards; if added in right order, then only one comparison needed for (; i >= 0; i--) { if (cmp.compare(l.get(i), o) <= 0) { break; } } l.add(i + 1, o); return l; }
From source file:com.amazon.carbonado.repo.replicated.ReplicatedRepository.java
@SuppressWarnings("unchecked") private <S extends Storable> void resync(ReplicationTrigger<S> replicationTrigger, Storage<S> replicaStorage, Query<S> replicaQuery, Storage<S> masterStorage, Query<S> masterQuery, ResyncCapability.Listener<? super S> listener, Throttle throttle, double desiredSpeed, Comparator comparator, Transaction replicaTxn) throws RepositoryException { final Log log = LogFactory.getLog(ReplicatedRepository.class); Cursor<S> replicaCursor = null; Cursor<S> masterCursor = null; try {/*from w w w . j a v a 2 s. c om*/ while (replicaCursor == null) { try { replicaCursor = replicaQuery.fetch(); } catch (CorruptEncodingException e) { S replicaWithKeyOnly = recoverReplicaKey(replicaStorage, e); if (!deleteCorruptEntry(replicationTrigger, replicaWithKeyOnly, e)) { // Cannot delete it, so just give up. throw e; } } } masterCursor = masterQuery.fetch(); S lastReplicaEntry = null; S replicaEntry = null; S masterEntry = null; int count = 0, txnCount = 0; while (true) { if (throttle != null) { try { // 100 millisecond clock precision throttle.throttle(desiredSpeed, 100); } catch (InterruptedException e) { throw new FetchInterruptedException(e); } } if (replicaEntry == null && replicaCursor != null) { int skippedCount = 0; while (replicaCursor.hasNext()) { try { replicaEntry = replicaCursor.next(); if (listener != null) { listener.afterLoad(replicaEntry); } if (skippedCount > 0) { if (skippedCount == 1) { log.warn("Skipped corrupt replica entry before this one: " + replicaEntry); } else { log.warn("Skipped " + skippedCount + " corrupt replica entries before this one: " + replicaEntry); } } break; } catch (CorruptEncodingException e) { // Exception forces cursor to close. Close again to be sure. replicaCursor.close(); replicaCursor = null; S replicaWithKeyOnly = recoverReplicaKey(replicaStorage, e); boolean deleted = deleteCorruptEntry(replicationTrigger, replicaWithKeyOnly, e); // Re-open (if we can) if (lastReplicaEntry == null) { break; } replicaCursor = replicaQuery.fetchAfter(lastReplicaEntry); if (deleted) { // Skip if it cannot be deleted. try { skippedCount = replicaCursor.skipNext(++skippedCount); log.info("Skipped corrupt replica entry", e); if (replicaWithKeyOnly != null) { // Try to update entry which could not be deleted. replicaEntry = replicaWithKeyOnly; if (listener != null) { listener.afterLoad(replicaEntry); } break; } } catch (FetchException e2) { log.error("Unable to skip past corrupt replica entry", e2); throw e; } } } } } if (count++ >= RESYNC_WATERMARK || txnCount >= RESYNC_BATCH_SIZE) { replicaTxn.commit(); if (replicaCursor != null) { // Cursor should auto-close after txn commit, but force // a close anyhow. Cursor is re-opened when it is // allowed to advance. replicaCursor.close(); replicaCursor = null; } count = 0; txnCount = 0; } if (masterEntry == null && masterCursor.hasNext()) { masterEntry = masterCursor.next(); } Runnable resyncTask = null; // Comparator should treat null as high. int compare = comparator.compare(replicaEntry, masterEntry); if (compare < 0) { // Bogus record exists only in replica so delete it. resyncTask = prepareResyncTask(replicationTrigger, listener, replicaEntry, null); // Allow replica to advance. if (replicaCursor == null) { replicaCursor = replicaQuery.fetchAfter(replicaEntry); } lastReplicaEntry = replicaEntry; replicaEntry = null; } else if (compare > 0) { // Replica cursor is missing an entry so copy it. resyncTask = prepareResyncTask(replicationTrigger, listener, null, masterEntry); // Allow master to advance. masterEntry = null; } else { // If compare is zero, replicaEntry and masterEntry are // either both null or both non-null. if (replicaEntry == null && masterEntry == null) { // Both cursors exhausted -- resync is complete. break; } // Both replicaEntry and masterEntry are non-null. if (!replicaEntry.equalProperties(masterEntry)) { // Replica is stale. resyncTask = prepareResyncTask(replicationTrigger, listener, replicaEntry, masterEntry); } // Entries are synchronized so allow both cursors to advance. if (replicaCursor == null) { replicaCursor = replicaQuery.fetchAfter(replicaEntry); } lastReplicaEntry = replicaEntry; replicaEntry = null; masterEntry = null; } if (resyncTask != null) { txnCount++; resyncTask.run(); } } } finally { try { if (masterCursor != null) { masterCursor.close(); } } finally { if (replicaCursor != null) { replicaCursor.close(); } } } }
From source file:org.wso2.carbon.apimgt.impl.APIConsumerImpl.java
/** * The method to get All PUBLISHED and DEPRECATED APIs, to Store view * * @return Set<API> Set of APIs//w w w .j a v a2 s .c o m * @throws APIManagementException */ @Deprecated public Map<String, Object> getAllPaginatedAPIs(String tenantDomain, int start, int end) throws APIManagementException { Map<String, Object> result = new HashMap<String, Object>(); SortedSet<API> apiSortedSet = new TreeSet<API>(new APINameComparator()); SortedSet<API> apiVersionsSortedSet = new TreeSet<API>(new APIVersionComparator()); int totalLength = 0; try { Registry userRegistry; boolean isTenantMode = (tenantDomain != null); if ((isTenantMode && this.tenantDomain == null) || (isTenantMode && isTenantDomainNotMatching(tenantDomain))) {//Tenant store anonymous mode int tenantId = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager() .getTenantId(tenantDomain); userRegistry = ServiceReferenceHolder.getInstance().getRegistryService() .getGovernanceUserRegistry(CarbonConstants.REGISTRY_ANONNYMOUS_USERNAME, tenantId); PrivilegedCarbonContext.getThreadLocalCarbonContext() .setUsername(CarbonConstants.REGISTRY_ANONNYMOUS_USERNAME); } else { userRegistry = registry; PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(this.username); } this.isTenantModeStoreView = isTenantMode; this.requestedTenant = tenantDomain; Map<String, API> latestPublishedAPIs = new HashMap<String, API>(); List<API> multiVersionedAPIs = new ArrayList<API>(); Comparator<API> versionComparator = new APIVersionComparator(); Boolean displayMultipleVersions = APIUtil.isAllowDisplayMultipleVersions(); GenericArtifactManager artifactManager = APIUtil.getArtifactManager(userRegistry, APIConstants.API_KEY); PaginationContext.init(start, end, "ASC", APIConstants.API_OVERVIEW_NAME, Integer.MAX_VALUE); boolean noPublishedAPIs = false; if (artifactManager != null) { //Create the search attribute map for PUBLISHED APIs Map<String, List<String>> listMap = new HashMap<String, List<String>>(); listMap.put(APIConstants.API_OVERVIEW_STATUS, new ArrayList<String>() { { add(APIConstants.PUBLISHED); } }); GenericArtifact[] genericArtifacts = artifactManager.findGenericArtifacts(listMap); totalLength = PaginationContext.getInstance().getLength(); if (genericArtifacts == null || genericArtifacts.length == 0) { noPublishedAPIs = true; } int publishedAPICount; if (genericArtifacts != null) { for (GenericArtifact artifact : genericArtifacts) { // adding the API provider can mark the latest API . // String status = artifact.getAttribute(APIConstants.API_OVERVIEW_STATUS); API api = APIUtil.getAPI(artifact); if (api != null) { String key; //Check the configuration to allow showing multiple versions of an API true/false if (!displayMultipleVersions) { //If allow only showing the latest version of an API key = api.getId().getProviderName() + COLON_CHAR + api.getId().getApiName(); API existingAPI = latestPublishedAPIs.get(key); if (existingAPI != null) { // If we have already seen an API with the same name, make sure // this one has a higher version number if (versionComparator.compare(api, existingAPI) > 0) { latestPublishedAPIs.put(key, api); } } else { // We haven't seen this API before latestPublishedAPIs.put(key, api); } } else { //If allow showing multiple versions of an API // key = api.getId().getProviderName() + ":" + api.getId().getApiName() + ":" + api.getId() // .getVersion(); multiVersionedAPIs.add(api); } } } } if (!displayMultipleVersions) { publishedAPICount = latestPublishedAPIs.size(); } else { publishedAPICount = multiVersionedAPIs.size(); } if ((start + end) > publishedAPICount) { if (publishedAPICount > 0) { /*Starting to retrieve DEPRECATED APIs*/ start = 0; /* publishedAPICount is always less than end*/ end = end - publishedAPICount; } else { start = start - totalLength; } PaginationContext.init(start, end, "ASC", APIConstants.API_OVERVIEW_NAME, Integer.MAX_VALUE); //Create the search attribute map for DEPRECATED APIs Map<String, List<String>> listMapForDeprecatedAPIs = new HashMap<String, List<String>>(); listMapForDeprecatedAPIs.put(APIConstants.API_OVERVIEW_STATUS, new ArrayList<String>() { { add(APIConstants.DEPRECATED); } }); GenericArtifact[] genericArtifactsForDeprecatedAPIs = artifactManager .findGenericArtifacts(listMapForDeprecatedAPIs); totalLength = totalLength + PaginationContext.getInstance().getLength(); if ((genericArtifactsForDeprecatedAPIs == null || genericArtifactsForDeprecatedAPIs.length == 0) && noPublishedAPIs) { result.put("apis", apiSortedSet); result.put("totalLength", totalLength); return result; } if (genericArtifactsForDeprecatedAPIs != null) { for (GenericArtifact artifact : genericArtifactsForDeprecatedAPIs) { // adding the API provider can mark the latest API . API api = APIUtil.getAPI(artifact); if (api != null) { String key; //Check the configuration to allow showing multiple versions of an API true/false if (!displayMultipleVersions) { //If allow only showing the latest version of an API key = api.getId().getProviderName() + COLON_CHAR + api.getId().getApiName(); API existingAPI = latestPublishedAPIs.get(key); if (existingAPI != null) { // If we have already seen an API with the same name, make sure // this one has a higher version number if (versionComparator.compare(api, existingAPI) > 0) { latestPublishedAPIs.put(key, api); } } else { // We haven't seen this API before latestPublishedAPIs.put(key, api); } } else { //If allow showing multiple versions of an API multiVersionedAPIs.add(api); } } } } } if (!displayMultipleVersions) { for (API api : latestPublishedAPIs.values()) { apiSortedSet.add(api); } result.put("apis", apiSortedSet); result.put("totalLength", totalLength); return result; } else { apiVersionsSortedSet.addAll(multiVersionedAPIs); result.put("apis", apiVersionsSortedSet); result.put("totalLength", totalLength); return result; } } } catch (RegistryException e) { handleException("Failed to get all published APIs", e); } catch (UserStoreException e) { handleException("Failed to get all published APIs", e); } finally { PaginationContext.destroy(); } result.put("apis", apiSortedSet); result.put("totalLength", totalLength); return result; }
From source file:org.openanzo.glitter.query.SPARQLAlgebra.java
/** * Conjoins two solution sets./*w ww .j av a2 s. co m*/ * * (A null list of solutions functions equivalently to a solution set with a single bindings-less solution. This is shorthand and is COMPLETELY different * from a solution set with zero solutions (which conjoins with any other solution set to the empty solution set).) * * * @param set1 * @param set2 * @return The conjunction of the two solution sets. */ static public SolutionSet join(SolutionSet set1, SolutionSet set2) { boolean isEnabled = RequestAnalysis.getAnalysisLogger().isDebugEnabled(); log.trace(LogUtils.GLITTER_MARKER, "join"); if (set1 == null) { if (isEnabled) RequestAnalysis.getAnalysisLogger().debug(LogUtils.GLITTER_MARKER, "[glitter_SPARQLAlgebra_identityJoin-RHS] {}", Integer.toString(set2.size())); return set2; } if (set2 == null) { if (isEnabled) RequestAnalysis.getAnalysisLogger().debug(LogUtils.GLITTER_MARKER, "[glitter_SPARQLAlgebra_identityJoin-LHS] {}", Integer.toString(set1.size())); return set1; } Comparator<Value> comparator = getComparator(set1, set2); if (set1.size() == 0 || set2.size() == 0) { if (isEnabled) RequestAnalysis.getAnalysisLogger().debug(LogUtils.GLITTER_MARKER, "[glitter_SPARQLAlgebra_nullJoin]"); return new SolutionList(); } PatternSolution sol1[] = set1.toArray(new PatternSolution[0]); PatternSolution sol2[] = set2.toArray(new PatternSolution[0]); if (sol1.length == 1 && sol1[0].size() == 0) { if (isEnabled) RequestAnalysis.getAnalysisLogger().debug(LogUtils.GLITTER_MARKER, "[glitter_SPARQLAlgebra_identityJoin-RHS] {}", Integer.toString(set2.size())); return set2; } else if (sol2.length == 1 && sol2[0].size() == 0) { if (isEnabled) RequestAnalysis.getAnalysisLogger().debug(LogUtils.GLITTER_MARKER, "[glitter_SPARQLAlgebra_identityJoin-LHS] {}", Integer.toString(set1.size())); return set1; } SolutionSet newSolutions = new CustomCompareSolutionSet.ComparableSolutionList(comparator); long start = 0; if (isEnabled) { RequestAnalysis.getAnalysisLogger().debug(LogUtils.GLITTER_MARKER, "[glitter_SPARQLAlgebra_startJoin] {}:{}", Integer.toString(set1.size()), Integer.toString(set2.size())); start = System.currentTimeMillis(); } TreeSet<Bindable> count = new TreeSet<Bindable>(); for (PatternSolution element : sol1) { for (Bindable bindable : element.getBoundDomain(true)) { count.add(bindable); } } TreeSet<Bindable> count2 = new TreeSet<Bindable>(); for (PatternSolution element : sol2) { for (Bindable bindable : element.getBoundDomain(true)) { count2.add(bindable); } } TreeSet<Bindable> matchSet = new TreeSet<Bindable>(); if (count.size() < count2.size()) { for (Bindable bindable : count) { if (count2.contains(bindable)) matchSet.add(bindable); } } else { for (Bindable bindable : count2) { if (count.contains(bindable)) { matchSet.add(bindable); } } } Bindable matchedBindables[] = matchSet.toArray(new Bindable[0]); if (isEnabled) { StringBuilder sb = new StringBuilder(); for (Bindable bindable : matchSet) { sb.append(bindable.toString()); sb.append(","); } RequestAnalysis.getAnalysisLogger().debug(LogUtils.GLITTER_MARKER, "[glitter_SPARQLAlgebra_matchingBindings] {}", sb.toString()); } long startSort = 0; if (isEnabled) { startSort = System.currentTimeMillis(); } Arrays.sort(sol1, 0, sol1.length, new PatternSolutionImpl.SetSolutionComparator(matchSet, comparator)); if (isEnabled) { RequestAnalysis.getAnalysisLogger().debug(LogUtils.GLITTER_MARKER, "[glitter_SPARQLAlgebra_leftSortTime] {}", Long.toString(System.currentTimeMillis() - startSort)); startSort = System.currentTimeMillis(); } Arrays.sort(sol2, 0, sol2.length, new PatternSolutionImpl.SetSolutionComparator(matchSet, comparator)); if (isEnabled) { RequestAnalysis.getAnalysisLogger().debug(LogUtils.GLITTER_MARKER, "[glitter_SPARQLAlgebra_rightSortTime] {}", Long.toString(System.currentTimeMillis() - startSort)); startSort = System.currentTimeMillis(); } //System.err.println("Joining:" + sol1.length + ":" + sol2.length); int j = 0; //long start = System.currentTimeMillis(); if (matchSet.size() > 0) { for (PatternSolution solution : sol1) { Bindable sol1Bindables[] = solution.getBoundDomainArray(); boolean done = false; for (int k = j; k < sol2.length && !done; k++) { if (sol1Bindables.length == 0) { newSolutions.add(sol2[k]); } else { boolean match = true; boolean cloned = false; PatternSolution solution2 = sol2[k]; boolean firstEmpty = true; Bindable bindables2[] = solution2.getBoundDomainArray(); int m = 0; if (bindables2.length > 0) { boolean breaker = false; for (Bindable element : matchedBindables) { firstEmpty = false; for (int mm = m; mm < bindables2.length && !breaker; mm++) { int comp1 = element.compareTo(bindables2[mm]); if (comp1 == 0) { Value term = solution.getBinding(element); Value term2 = solution2.getBinding(bindables2[mm]); //If term is null, this means that lh solution does not have a binding for a shared binding, so have to do slow conjoin if (term == null) { PatternSolution psNew = conjoin(solution, solution2); if (psNew != null) { newSolutions.add(psNew); } match = false; breaker = true; } else { int comp = comparator.compare(term, term2); if (comp > 0) { match = false; breaker = true; j = k; break; } else if (comp < 0) { match = false; done = true; breaker = true; break; } else { if (!cloned) { bindables2 = bindables2.clone(); cloned = true; } // conjunction.put(bindables2[mm], term); bindables2[mm] = null; m = mm + 1; break; } } } else if (comp1 > 0) { m = mm; } } if (breaker) break; } if (match) { if (firstEmpty) { newSolutions.add(solution2); } else { PatternSolutionImpl newSolution = new PatternSolutionImpl(solution); for (Bindable element : bindables2) { if (element != null) { newSolution.setBinding(element, solution2.getBinding(element)); } } newSolutions.add(new PatternSolutionImpl(newSolution)); } } } else { newSolutions.add(solution); } } } } } else { for (PatternSolution solution : sol1) { for (PatternSolution solution2 : sol2) { Bindable bindable[] = new Bindable[solution.getBoundDomainArray().length + solution2.getBoundDomainArray().length]; Value value[] = new Value[solution.getBoundDomainArray().length + solution2.getBoundDomainArray().length]; Bindable bs[] = solution.getBoundDomain(false); Value vs[] = solution.getBoundVariablesArray(false); System.arraycopy(bs, 0, bindable, 0, bs.length); System.arraycopy(vs, 0, value, 0, vs.length); int last = vs.length; bs = solution2.getBoundDomain(false); vs = solution2.getBoundVariablesArray(false); System.arraycopy(bs, 0, bindable, last, bs.length); System.arraycopy(vs, 0, value, last, vs.length); newSolutions.add(new PatternSolutionImpl(bindable, value)); } } } //System.err.println("Join:" + (System.currentTimeMillis() - start) + ":" + newSolutions.size()); if (isEnabled) { RequestAnalysis.getAnalysisLogger().debug(LogUtils.GLITTER_MARKER, "[glitter_SPARQLAlgebra_endJoin] {}:{}", Integer.toString(newSolutions.size()), Long.toString(System.currentTimeMillis() - start)); } return newSolutions; /* SolutionSet newSolutions2 = new SolutionList(); for (PatternSolution ps1 : set1) { for (PatternSolution ps2 : set2) { PatternSolution psNew = ps1.conjoin(ps2); if (psNew != null) newSolutions2.add(psNew); } } return newSolutions;*/ }