List of usage examples for java.util Collections binarySearch
public static <T> int binarySearch(List<? extends Comparable<? super T>> list, T key)
From source file:com.wizecommerce.hecuba.HecubaClientManager.java
protected boolean isSecondaryIndexByColumnNameAndValueEnabledForColumn(String columnName) { return isSecondaryIndexByColumnNameAndValueEnabled && Collections.binarySearch(columnsToIndexOnColumnNameAndValue, columnName) >= 0; }
From source file:org.accada.epcis.repository.query.QueryOperationsModule.java
/** * Runs a MasterDataQuery for the given QueryParam array and returns the * QueryResults./*w w w. j a va2s .c o m*/ * * @param queryParams * The parameters for running the MasterDataQuery. * @return The QueryResults. * @throws SQLException * If an error accessing the database occurred. * @throws QueryParameterException * If one of the provided QueryParam is invalid. * @throws ImplementationException * If a service implementation error occurred. * @throws QueryTooLargeException * If the query is too large to be executed. */ private MasterDataQueryDTO constructMasterDataQuery(final QueryParams queryParams) throws QueryParameterExceptionResponse { MasterDataQueryDTO mdQuery = new MasterDataQueryDTO(); // a sorted List of query parameter names - keeps track of the processed // names in order to cope with duplicates List<String> sortedParamNames = new ArrayList<String>(); Boolean includeAttributes = null; Boolean includeChildren = null; List<String> vocabularyTypes = null; List<String> includedAttributeNames = null; for (QueryParam param : queryParams.getParam()) { String paramName = param.getName(); Object paramValue = param.getValue(); // check for null value if (paramName == null || "".equals(paramName)) { String msg = "Missing name for a query parameter"; throw queryParameterException(msg, null); } if (paramValue == null) { String msg = "Missing value for query parameter '" + paramName + "'"; throw queryParameterException(msg, null); } // check if the current query parameter has already been provided int index = Collections.binarySearch(sortedParamNames, paramName); if (index < 0) { // we have not yet seen this query parameter name - ok sortedParamNames.add(-index - 1, paramName); } else { // we have already handled this query parameter name - not ok String msg = "Query parameter '" + paramName + "' provided more than once"; throw queryParameterException(msg, null); } try { if (paramName.equals("includeAttributes")) { // defaults to 'false' if an invalid value is provided! includeAttributes = Boolean.valueOf(parseAsString(paramValue)); mdQuery.setIncludeAttributes(includeAttributes.booleanValue()); } else if (paramName.equals("includeChildren")) { // defaults to 'false' if an invalid value is provided! includeChildren = Boolean.valueOf(parseAsString(paramValue)); mdQuery.setIncludeChildren(includeChildren.booleanValue()); } else if (paramName.equals("maxElementCount")) { int maxElementCount = parseAsInteger(paramValue).intValue(); mdQuery.setMaxElementCount(maxElementCount); } else if (paramName.equals("vocabularyName")) { ArrayOfString aos = parseAsArrayOfString(paramValue); vocabularyTypes = aos.getString(); } else if (paramName.equals("attributeNames")) { ArrayOfString aos = parseAsArrayOfString(paramValue); includedAttributeNames = aos.getString(); } else if (paramName.equals("EQ_name")) { ArrayOfString aos = parseAsArrayOfString(paramValue); mdQuery.setVocabularyEqNames(aos.getString()); } else if (paramName.equals("WD_name")) { ArrayOfString aos = parseAsArrayOfString(paramValue); mdQuery.setVocabularyWdNames(aos.getString()); } else if (paramName.equals("HASATTR")) { ArrayOfString aos = parseAsArrayOfString(paramValue); mdQuery.setAttributeNames(aos.getString()); } else if (paramName.startsWith("EQATTR_")) { String attrName = paramName.substring(7); ArrayOfString aos = parseAsArrayOfString(paramValue); mdQuery.addAttributeNameAndValues(attrName, aos.getString()); } } catch (ClassCastException e) { String msg = "The type of the value for query parameter '" + paramName + "': " + paramValue + " is invalid"; throw queryParameterException(msg, e); } } // check for missing parameters if (includeAttributes == null || includeChildren == null) { String missing = (includeAttributes == null) ? " includeAttributes" : ""; missing += (includeChildren == null) ? " includeChildren" : ""; String msg = "Missing required masterdata query parameter(s):" + missing; throw queryParameterException(msg, null); } if (includeAttributes.booleanValue() && includedAttributeNames != null) { mdQuery.setIncludedAttributeNames(includedAttributeNames); } if (vocabularyTypes == null) { // include all vocabularies vocabularyTypes = EpcisConstants.VOCABULARY_TYPES; } mdQuery.setVocabularyTypes(vocabularyTypes); return mdQuery; }
From source file:com.wizecommerce.hecuba.hector.HectorBasedHecubaClientManager.java
@Override public List<K> retrieveKeysBySecondaryIndex(String columnName, String columnValue) { // first some fact checking, before going to Cassandra if (isSecondaryIndexByColumnNameAndValueEnabled && Collections.binarySearch(columnsToIndexOnColumnNameAndValue, columnName) >= 0) { return retrieveKeysFromSecondaryIndex(columnName, columnValue); }/*from w ww. j a v a 2s.c o m*/ return null; }
From source file:com.wizecommerce.hecuba.hector.HectorBasedHecubaClientManager.java
@Override public Map<String, List<K>> retrieveKeysBySecondaryIndex(String columnName, List<String> columnValues) { // first some fact checking, before going to Cassandra if (isSecondaryIndexByColumnNameAndValueEnabled && Collections.binarySearch(columnsToIndexOnColumnNameAndValue, columnName) >= 0) { return retrieveKeysFromSecondaryIndex(columnName, columnValues); }/*from w w w . j a va 2s . c o m*/ return null; }
From source file:com.wizecommerce.hecuba.HecubaCassandraManagerTestBase.java
/** * testSecondaryIndexWithUpdatesToSingleColumn *///w w w . j av a 2s.c om @Test public void testUpdateRowScenario13() { CassandraParamsBean bean = getDefaultCassandraParamsBean(); bean.setSiColumns("MySecondaryKey_1:MySecondaryKey_2"); HecubaClientManager<Long> cassandraManager = getHecubaClientManager(bean); cassandraManager.updateString(1234L, "test_column", "test_value"); // add a secondary index column, for the first time. cassandraManager.updateString(1234L, "MySecondaryKey_1", "SecondaryIndexValue"); /** * __________________________________________ | MySecondaryKey_1:SecondaryIndexValue | --> 1234 ------------------------------------------ */ // try to retrieve that by its id. CassandraResultSet<Long, String> resultFromFirstRetrieval = cassandraManager .retrieveBySecondaryIndex("MySecondaryKey_1", "SecondaryIndexValue"); // result should have the id 1234L assertNotNull(resultFromFirstRetrieval); assertEquals(new Long(1234), resultFromFirstRetrieval.getKey()); // And that should have other columns too. assertEquals("test_value", resultFromFirstRetrieval.getString("test_column")); // add another row with the same value for the secondary index cassandraManager.updateString(1233L, "MySecondaryKey_1", "SecondaryIndexValue"); cassandraManager.updateString(1233L, "test_column", "test_value_2"); /** * __________________________________________ | MySecondaryKey_1:SecondaryIndexValue | --> 1234, 1233 ------------------------------------------ */ // 2. And that should have the id 1234L and 1233L resultFromFirstRetrieval = cassandraManager.retrieveBySecondaryIndex("MySecondaryKey_1", "SecondaryIndexValue"); List<String> items = new ArrayList<String>(); assertTrue(resultFromFirstRetrieval != null); // get first result. items.add(resultFromFirstRetrieval.getString("test_column")); while (resultFromFirstRetrieval.hasNextResult()) { resultFromFirstRetrieval.nextResult(); items.add(resultFromFirstRetrieval.getString("test_column")); } assertEquals(items.size(), 2); // Since we can't really check each items, since order is not guaranteed. Collections.sort(items); assertTrue(Collections.binarySearch(items, "test_value_2") >= 0); assertTrue(Collections.binarySearch(items, "test_value") >= 0); // 3. And that should have other columns too. cassandraManager.updateString(1233L, "MySecondaryKey_1", "SecondaryIndexValue_2"); /** * __________________________________________ | MySecondaryKey_1:SecondaryIndexValue | --> 1234 ------------------------------------------ * * ____________________________________________ | MySecondaryKey_1:SecondaryIndexValue_2 | --> 1233 -------------------------------------------- */ // retrieve the row from the new secondary value CassandraResultSet<Long, String> resultFromThirdRetrieval = cassandraManager .retrieveBySecondaryIndex("MySecondaryKey_1", "SecondaryIndexValue_2"); // 1. we should have retrieved only one. assertEquals(false, resultFromThirdRetrieval.hasNextResult()); // 2. And that should have the id 1233L assertEquals(new Long(1233), resultFromThirdRetrieval.getKey()); // 3. And that should have other columns too. assertEquals("test_value_2", resultFromThirdRetrieval.getString("test_column")); // try to retrieve from the previous one and see whether it returns only one. CassandraResultSet<Long, String> resultFromFourthRetrieval = cassandraManager .retrieveBySecondaryIndex("MySecondaryKey_1", "SecondaryIndexValue"); // 2. And that should have the id 1234L and 1233L assertEquals(new Long(1234), resultFromFourthRetrieval.getKey()); // change the value of the secondary index for the first row above and see whether the initial query returns // anything now. cassandraManager.updateString(1234L, "MySecondaryKey_1", "SecondaryIndexValue_3"); /** * __________________________________________ | MySecondaryKey_1:SecondaryIndexValue | --> ------------------------------------------ * * ____________________________________________ | MySecondaryKey_1:SecondaryIndexValue_2 | --> 1233 -------------------------------------------- * * ____________________________________________ | MySecondaryKey_1:SecondaryIndexValue_3 | --> 1234 -------------------------------------------- * */ CassandraResultSet<Long, String> resultFromFifthRetrieval = cassandraManager .retrieveBySecondaryIndex("MySecondaryKey_1", "SecondaryIndexValue"); assertEquals(null, resultFromFifthRetrieval); resultFromFifthRetrieval = cassandraManager.retrieveBySecondaryIndex("MySecondaryKey_1", "SecondaryIndexValue_2"); // 1. we should have retrieved one assertEquals(false, resultFromFifthRetrieval.hasNextResult()); resultFromFifthRetrieval = cassandraManager.retrieveBySecondaryIndex("MySecondaryKey_1", "SecondaryIndexValue_3"); // 1. we should have retrieved one assertEquals(false, resultFromFifthRetrieval.hasNextResult()); }
From source file:com.facebook.infrastructure.service.StorageService.java
EndPoint getPredecessor(EndPoint ep) { BigInteger token = tokenMetadata_.getToken(ep); Map<BigInteger, EndPoint> tokenToEndPointMap = tokenMetadata_.cloneTokenEndPointMap(); List<BigInteger> tokens = new ArrayList<BigInteger>(tokenToEndPointMap.keySet()); Collections.sort(tokens);// w w w.j a v a 2 s . c o m int index = Collections.binarySearch(tokens, token); EndPoint predecessor = (index == 0) ? tokenToEndPointMap.get(tokens.get(tokens.size() - 1)) : tokenToEndPointMap.get(tokens.get(--index)); return predecessor; }
From source file:com.facebook.infrastructure.service.StorageService.java
public EndPoint getSuccessor(EndPoint ep) { BigInteger token = tokenMetadata_.getToken(ep); Map<BigInteger, EndPoint> tokenToEndPointMap = tokenMetadata_.cloneTokenEndPointMap(); List<BigInteger> tokens = new ArrayList<BigInteger>(tokenToEndPointMap.keySet()); Collections.sort(tokens);/*from ww w .j av a 2 s . c o m*/ int index = Collections.binarySearch(tokens, token); EndPoint successor = (index == (tokens.size() - 1)) ? tokenToEndPointMap.get(tokens.get(0)) : tokenToEndPointMap.get(tokens.get(++index)); return successor; }
From source file:com.facebook.infrastructure.service.StorageService.java
/** * This method returns the range handled by this node. *//* ww w .j a v a2s. com*/ public Range getMyRange() { BigInteger myToken = tokenMetadata_.getToken(StorageService.tcpAddr_); Map<BigInteger, EndPoint> tokenToEndPointMap = tokenMetadata_.cloneTokenEndPointMap(); List<BigInteger> allTokens = new ArrayList<BigInteger>(tokenToEndPointMap.keySet()); Collections.sort(allTokens); int index = Collections.binarySearch(allTokens, myToken); /* Calculate the lhs for the range */ BigInteger lhs = (index == 0) ? allTokens.get(allTokens.size() - 1) : allTokens.get(index - 1); return new Range(lhs, myToken); }
From source file:dev.ukanth.ufirewall.Api.java
/** * @param ctx application context (mandatory) * @return a list of applications/*from w w w . j a va2s . co m*/ */ public static List<PackageInfoData> getApps(Context ctx, GetAppList appList) { initSpecial(); if (applications != null && applications.size() > 0) { // return cached instance return applications; } final SharedPreferences defaultPrefs = PreferenceManager.getDefaultSharedPreferences(ctx); final boolean enableVPN = defaultPrefs.getBoolean("enableVPN", false); final boolean enableLAN = defaultPrefs.getBoolean("enableLAN", false); final boolean enableRoam = defaultPrefs.getBoolean("enableRoam", true); final SharedPreferences prefs = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); final String savedPkg_wifi_uid = prefs.getString(PREF_WIFI_PKG_UIDS, ""); final String savedPkg_3g_uid = prefs.getString(PREF_3G_PKG_UIDS, ""); final String savedPkg_roam_uid = prefs.getString(PREF_ROAMING_PKG_UIDS, ""); final String savedPkg_vpn_uid = prefs.getString(PREF_VPN_PKG_UIDS, ""); final String savedPkg_lan_uid = prefs.getString(PREF_LAN_PKG_UIDS, ""); List<Integer> selected_wifi = new ArrayList<Integer>(); List<Integer> selected_3g = new ArrayList<Integer>(); List<Integer> selected_roam = new ArrayList<Integer>(); List<Integer> selected_vpn = new ArrayList<Integer>(); List<Integer> selected_lan = new ArrayList<Integer>(); selected_wifi = getListFromPref(savedPkg_wifi_uid); selected_3g = getListFromPref(savedPkg_3g_uid); if (enableRoam) { selected_roam = getListFromPref(savedPkg_roam_uid); } if (enableVPN) { selected_vpn = getListFromPref(savedPkg_vpn_uid); } if (enableLAN) { selected_lan = getListFromPref(savedPkg_lan_uid); } //revert back to old approach //always use the defaul preferences to store cache value - reduces the application usage size final SharedPreferences cachePrefs = ctx.getSharedPreferences("AFWallPrefs", Context.MODE_PRIVATE); int count = 0; try { final PackageManager pkgmanager = ctx.getPackageManager(); final List<ApplicationInfo> installed = pkgmanager .getInstalledApplications(PackageManager.GET_META_DATA); SparseArray<PackageInfoData> syncMap = new SparseArray<PackageInfoData>(); final Editor edit = cachePrefs.edit(); boolean changed = false; String name = null; String cachekey = null; final String cacheLabel = "cache.label."; PackageInfoData app = null; ApplicationInfo apinfo = null; for (int i = 0; i < installed.size(); i++) { //for (final ApplicationInfo apinfo : installed) { count = count + 1; apinfo = installed.get(i); if (appList != null) { appList.doProgress(count); } boolean firstseen = false; app = syncMap.get(apinfo.uid); // filter applications which are not allowed to access the Internet if (app == null && PackageManager.PERMISSION_GRANTED != pkgmanager .checkPermission(Manifest.permission.INTERNET, apinfo.packageName)) { continue; } // try to get the application label from our cache - getApplicationLabel() is horribly slow!!!! cachekey = cacheLabel + apinfo.packageName; name = prefs.getString(cachekey, ""); if (name.length() == 0) { // get label and put on cache name = pkgmanager.getApplicationLabel(apinfo).toString(); edit.putString(cachekey, name); changed = true; firstseen = true; } if (app == null) { app = new PackageInfoData(); app.uid = apinfo.uid; app.names = new ArrayList<String>(); app.names.add(name); app.appinfo = apinfo; app.pkgName = apinfo.packageName; syncMap.put(apinfo.uid, app); } else { app.names.add(name); } app.firstseen = firstseen; // check if this application is selected if (!app.selected_wifi && Collections.binarySearch(selected_wifi, app.uid) >= 0) { app.selected_wifi = true; } if (!app.selected_3g && Collections.binarySearch(selected_3g, app.uid) >= 0) { app.selected_3g = true; } if (enableRoam && !app.selected_roam && Collections.binarySearch(selected_roam, app.uid) >= 0) { app.selected_roam = true; } if (enableVPN && !app.selected_vpn && Collections.binarySearch(selected_vpn, app.uid) >= 0) { app.selected_vpn = true; } if (enableLAN && !app.selected_lan && Collections.binarySearch(selected_lan, app.uid) >= 0) { app.selected_lan = true; } } List<PackageInfoData> specialData = new ArrayList<PackageInfoData>(); specialData.add(new PackageInfoData(SPECIAL_UID_ANY, ctx.getString(R.string.all_item), "dev.afwall.special.any")); specialData.add(new PackageInfoData(SPECIAL_UID_KERNEL, ctx.getString(R.string.kernel_item), "dev.afwall.special.kernel")); specialData.add(new PackageInfoData(SPECIAL_UID_TETHER, ctx.getString(R.string.tethering_item), "dev.afwall.special.tether")); //specialData.add(new PackageInfoData(SPECIAL_UID_DNSPROXY, ctx.getString(R.string.dnsproxy_item), "dev.afwall.special.dnsproxy")); specialData.add(new PackageInfoData(SPECIAL_UID_NTP, ctx.getString(R.string.ntp_item), "dev.afwall.special.ntp")); specialData .add(new PackageInfoData("root", ctx.getString(R.string.root_item), "dev.afwall.special.root")); specialData.add(new PackageInfoData("media", "Media server", "dev.afwall.special.media")); specialData.add(new PackageInfoData("vpn", "VPN networking", "dev.afwall.special.vpn")); specialData.add(new PackageInfoData("shell", "Linux shell", "dev.afwall.special.shell")); specialData.add(new PackageInfoData("gps", "GPS", "dev.afwall.special.gps")); specialData.add(new PackageInfoData("adb", "ADB (Android Debug Bridge)", "dev.afwall.special.adb")); if (specialApps == null) { specialApps = new HashMap<String, Integer>(); } for (int i = 0; i < specialData.size(); i++) { app = specialData.get(i); specialApps.put(app.pkgName, app.uid); //default DNS/NTP if (app.uid != -1 && syncMap.get(app.uid) == null) { // check if this application is allowed if (!app.selected_wifi && Collections.binarySearch(selected_wifi, app.uid) >= 0) { app.selected_wifi = true; } if (!app.selected_3g && Collections.binarySearch(selected_3g, app.uid) >= 0) { app.selected_3g = true; } if (enableRoam && !app.selected_roam && Collections.binarySearch(selected_roam, app.uid) >= 0) { app.selected_roam = true; } if (enableVPN && !app.selected_vpn && Collections.binarySearch(selected_vpn, app.uid) >= 0) { app.selected_vpn = true; } if (enableLAN && !app.selected_lan && Collections.binarySearch(selected_lan, app.uid) >= 0) { app.selected_lan = true; } syncMap.put(app.uid, app); } } if (changed) { edit.commit(); } /* convert the map into an array */ applications = new ArrayList<PackageInfoData>(); for (int i = 0; i < syncMap.size(); i++) { applications.add(syncMap.valueAt(i)); } return applications; } catch (Exception e) { alert(ctx, ctx.getString(R.string.error_common) + e); } return null; }
From source file:com.facebook.infrastructure.service.StorageService.java
/** * This method returns the endpoint that is responsible for storing the * specified key.//from w w w . j a v a 2 s . co m * * param @ key - key for which we need to find the endpoint return value - the * endpoint responsible for this key */ public EndPoint getPrimary(String key) { EndPoint endpoint = StorageService.tcpAddr_; BigInteger token = hash(key); Map<BigInteger, EndPoint> tokenToEndPointMap = tokenMetadata_.cloneTokenEndPointMap(); List<BigInteger> tokens = new ArrayList<BigInteger>(tokenToEndPointMap.keySet()); if (tokens.size() > 0) { Collections.sort(tokens); int index = Collections.binarySearch(tokens, token); if (index >= 0) { /* * retrieve the endpoint based on the token at this index in the tokens * list */ endpoint = tokenToEndPointMap.get(tokens.get(index)); } else { index = (index + 1) * (-1); if (index < tokens.size()) endpoint = tokenToEndPointMap.get(tokens.get(index)); else endpoint = tokenToEndPointMap.get(tokens.get(0)); } } return endpoint; }