List of usage examples for java.math BigInteger ZERO
BigInteger ZERO
To view the source code for java.math BigInteger ZERO.
Click Source Link
From source file:com.linute.linute.API.MyGcmListenerService.java
/** * Create and show a simple notification containing the received GCM message. * * @param data GCM Bundle received.//from w ww .j av a2 s. c om */ private void sendNotification(Bundle data, String action) { Intent intent = buildIntent(data, action); PendingIntent pendingIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, PendingIntent.FLAG_ONE_SHOT); //Log.d(TAG, data.toString()); String message = data.getString("message"); //int type = gettNotificationType(data.getString("action")); //String name = data.getString("ownerFullName"); boolean isAnon = "1".equals(data.getString("privacy")); String profileImage = null; switch (action) { case "messager": try { JSONObject image = new JSONObject(data.getString("roomProfileImage")); profileImage = image.getString("original"); } catch (JSONException | NullPointerException e) { } break; default: profileImage = data.getString("ownerProfileImage"); profileImage = (isAnon ? Utils.getAnonImageUrl(String.valueOf(profileImage)) : Utils.getImageUrlOfUser(String.valueOf(profileImage))); } Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); ChatRoom chatRoom = (ChatRoom) intent.getParcelableExtra("chatRoom"); String title = chatRoom != null ? chatRoom.getRoomName() : "Tapt"; final NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_stat_untitled_4_01).setColor(Color.BLACK).setContentTitle(title) .setContentText(message).setAutoCancel(true).setSound(defaultSoundUri) .setContentIntent(pendingIntent).setStyle(new NotificationCompat.BigTextStyle().bigText(message)); if (profileImage != null) { File image = null; try { image = Glide.with(this).load(profileImage).downloadOnly(256, 256).get(); } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } if (image != null) { /*ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE); ActivityManager.MemoryInfo info = new ActivityManager.MemoryInfo(); manager.getMemoryInfo(info);*/ notificationBuilder.setLargeIcon(getCircleBitmap(image)); } } BigInteger notificationId; Object ownerId = data.get("room"); Object eventId = data.get("event"); if (eventId != null) { notificationId = new BigInteger(String.valueOf(eventId), 16); } else if (ownerId != null) { notificationId = new BigInteger(String.valueOf(ownerId), 16); } else { notificationId = BigInteger.ZERO; } final int notifId = notificationId.intValue(); Notification notifications = notificationBuilder.build(); NotificationManagerCompat.from(this).notify(notificationId.intValue(), notifications); }
From source file:org.mingle.pear.persistence.dao.impl.GenericDaoImpl.java
@Override @Transactional(readOnly = true)//from w ww. j av a 2 s.c o m public BigInteger findCount(QueryTemplate qt) { Preconditions.checkArgument(!(qt instanceof SqlResultSetMappingQueryTemplate), "SqlResultMappingQueryTemplate can't use for count"); List<Object[]> results = find(qt); if (results.size() > 0) { Object count = results.get(0)[0]; // JPA if (count instanceof Long) { return BigInteger.valueOf((Long) count); } // SQL if (count instanceof BigInteger) { return (BigInteger) count; } } return BigInteger.ZERO; }
From source file:com.neovisionaries.security.JsonDigestUpdater.java
private boolean isZero(JsonNode value) { // int//w w w . j a v a 2s . c o m if (value.isInt() && value.intValue() == 0) { return true; } // long if (value.isLong() && value.longValue() == 0) { return true; } // short if (value.isShort() && value.shortValue() == 0) { return true; } // float if (value.isFloat() && value.floatValue() == 0.0F) { return true; } // double if (value.isDouble() && value.doubleValue() == 0.0) { return true; } // BigInteger if (value.isBigInteger() && value.bigIntegerValue().equals(BigInteger.ZERO)) { return true; } // BigDecimal if (value.isBigDecimal() && value.decimalValue().equals(BigDecimal.ZERO)) { return true; } return false; }
From source file:io.ecarf.core.cloud.task.processor.reason.phase1.DoReasonTask5.java
@Override public void run() throws IOException { GoogleCloudService cloud = (GoogleCloudService) this.getCloudService(); //String table = metadata.getValue(EcarfMetaData.ECARF_TABLE); //Set<String> terms = metadata.getTerms(); //String schemaFile = metadata.getValue(EcarfMetaData.ECARF_SCHEMA); //String bucket = metadata.getBucket(); Stopwatch stopwatch1 = Stopwatch.createUnstarted(); Stopwatch stopwatch2 = Stopwatch.createUnstarted(); Set<String> termsSet; if (terms == null) { // too large, probably saved as a file //String termsFile = metadata.getValue(EcarfMetaData.ECARF_TERMS_FILE); log.info("Using json file for terms: " + termsFile); Validate.notNull(termsFile);//from w ww. j a v a 2 s .com String localTermsFile = Utils.TEMP_FOLDER + termsFile; cloud.downloadObjectFromCloudStorage(termsFile, localTermsFile, bucket); // convert from JSON termsSet = io.cloudex.framework.utils.FileUtils.jsonFileToSet(localTermsFile); } else { termsSet = ObjectUtils.csvToSet(terms); } String localSchemaFile = Utils.TEMP_FOLDER + schemaFile; // download the file from the cloud storage cloud.downloadObjectFromCloudStorage(schemaFile, localSchemaFile, bucket); // uncompress if compressed if (GzipUtils.isCompressedFilename(schemaFile)) { localSchemaFile = GzipUtils.getUncompressedFilename(localSchemaFile); } Map<String, Set<Triple>> allSchemaTriples = TripleUtils.getRelevantSchemaNTriples(localSchemaFile, TermUtils.RDFS_TBOX); // get all the triples we care about Map<Term, Set<Triple>> schemaTerms = new HashMap<>(); for (String term : termsSet) { if (allSchemaTriples.containsKey(term)) { schemaTerms.put(new Term(term), allSchemaTriples.get(term)); } } String decoratedTable = table; int emptyRetries = 0; int totalInferredTriples = 0; int maxRetries = Config.getIntegerProperty(Constants.REASON_RETRY_KEY, 6); String instanceId = cloud.getInstanceId(); // timestamp loop do { List<String> productiveTerms = new ArrayList<>(); int interimInferredTriples = 0; // First of all run all the queries asynchronously and remember the jobId and filename for each term List<Callable<Void>> queryTasks = new ArrayList<>(); List<Callable<Void>> saveTasks = new ArrayList<>(); for (Entry<Term, Set<Triple>> entry : schemaTerms.entrySet()) { Term term = entry.getKey(); Set<Triple> triples = entry.getValue(); QuerySubTask queryTask = new QuerySubTask(term, triples, decoratedTable, cloud); queryTasks.add(queryTask); SaveResultsSubTask saveTask = new SaveResultsSubTask(term, cloud); saveTasks.add(saveTask); } // invoke all the queries in parallel this.invokeAll(queryTasks); long start = System.currentTimeMillis(); String inferredTriplesFile = Utils.TEMP_FOLDER + instanceId + '_' + start + Constants.DOT_INF; // save all the query results in files in parallel this.invokeAll(saveTasks); try (PrintWriter writer = new PrintWriter( new GZIPOutputStream(new FileOutputStream(inferredTriplesFile), Constants.GZIP_BUF_SIZE))) { // now loop through the queries for (Entry<Term, Set<Triple>> entry : schemaTerms.entrySet()) { Term term = entry.getKey(); BigInteger rows = term.getRows(); this.totalBytes = this.totalBytes + term.getBytes(); // only process if triples are found matching this term if (!BigInteger.ZERO.equals(rows)) { stopwatch1.start(); log.info("Reasoning for Term: " + term); Set<Triple> schemaTriples = entry.getValue(); log.info("Schema Triples: " + Joiner.on('\n').join(schemaTriples)); List<String> select = GenericRule.getSelect(schemaTriples); int inferredTriplesCount = this.inferAndSaveTriplesToFile(term, select, schemaTriples, rows, decoratedTable, writer); productiveTerms.add(term.getTerm()); interimInferredTriples += inferredTriplesCount; this.totalRows = this.totalRows.add(rows); stopwatch1.stop(); } else { log.info("Skipping term as no data found: " + term); } } } totalInferredTriples += interimInferredTriples; if (interimInferredTriples > 0) { // stream smaller numbers of inferred triples // try uploading from cloud storage int streamingThreshold = Config.getIntegerProperty("ecarf.io.reasoning.streaming.threshold", 100000); log.info("Inserting " + interimInferredTriples + ", inferred triples into Big Data table for " + productiveTerms.size() + " productive terms. Filename: " + inferredTriplesFile); if (interimInferredTriples <= streamingThreshold) { // stream the data Set<Triple> inferredTriples = TripleUtils.loadCompressedCSVTriples(inferredTriplesFile, false); log.info("Total triples to stream into Big Data: " + inferredTriples.size()); cloud.streamObjectsIntoBigData(inferredTriples, TableUtils.getBigQueryTripleTable(table)); log.info("All inferred triples are streamed into Big Data table"); } else { // load the data through cloud storage // upload the file to cloud storage log.info("Uploading inferred triples file into cloud storage: " + inferredTriplesFile); StorageObject file = cloud.uploadFileToCloudStorage(inferredTriplesFile, bucket); log.info("File " + file + ", uploaded successfully. Now loading it into big data."); String jobId = cloud.loadCloudStorageFilesIntoBigData(Lists.newArrayList(file.getUri()), TableUtils.getBigQueryTripleTable(table), false); log.info( "All inferred triples are loaded into Big Data table through cloud storage, completed jobId: " + jobId); } // reset empty retries emptyRetries = 0; stopwatch2.reset(); } else { log.info("No new inferred triples"); // increment empty retries emptyRetries++; if (!stopwatch2.isRunning()) { stopwatch2.start(); } } log.info("Total inferred triples so far = " + totalInferredTriples + ", current retry count: " + emptyRetries); if (emptyRetries < maxRetries) { ApiUtils.block(Config.getIntegerProperty(Constants.REASON_SLEEP_KEY, 20)); // FIXME move into the particular cloud implementation service long elapsed = System.currentTimeMillis() - start; decoratedTable = "[" + table + "@-" + elapsed + "-]"; log.info("Using table decorator: " + decoratedTable + ". Empty retries count: " + emptyRetries); } } while (emptyRetries < maxRetries); // end timestamp loop executor.shutdown(); log.info("Finished reasoning, total inferred triples = " + totalInferredTriples); log.info("Number of avoided duplicate terms = " + this.duplicates); log.info("Total rows retrieved from big data = " + this.totalRows); log.info("Total processed GBytes = " + ((double) this.totalBytes / FileUtils.ONE_GB)); log.info("Total process reasoning time (serialization in inf file) = " + stopwatch1); log.info("Total time spent in empty inference cycles = " + stopwatch2); }
From source file:com.precioustech.fxtrading.oanda.restapi.account.transaction.OandaTransactionDataProviderServiceTest.java
@Test public void historicTransactionsTest() throws Exception { final long minId = 175000000L; final OandaTransactionDataProviderService service = new OandaTransactionDataProviderService(url, accessToken);/*ww w.j a v a 2 s. c o m*/ assertEquals( "https://api-fxtrade.oanda.com/v1/accounts/123456/transactions?minId=" + (minId + 1) + "&count=500", service.getAccountMinTransactionUrl(minId, accountId)); OandaTransactionDataProviderService spy = createSpyAndCommonStuff( "src/test/resources/historicTransactions.txt", service); List<Transaction<Long, Long, String>> allTransactions = spy.getTransactionsGreaterThanId(minId, accountId); assertFalse(allTransactions.isEmpty()); assertEquals(9, allTransactions.size()); // general not null checks common to all for (Transaction<Long, Long, String> transaction : allTransactions) { assertNotNull(transaction.getTransactionId()); assertNotNull(transaction.getAccountId()); assertNotNull(transaction.getTransactionTime()); assertNotNull(transaction.getTransactionType()); assertNotNull(transaction.getInstrument()); assertEquals(accountId, transaction.getAccountId()); } // TRADE_CLOSE Transaction<Long, Long, String> transaction = allTransactions.get(0); assertEquals(TradeEvents.TRADE_CLOSE, transaction.getTransactionType()); assertEquals("EUR_USD", transaction.getInstrument().getInstrument()); assertEquals(new Long(2), transaction.getUnits()); assertEquals(TradingSignal.SHORT, transaction.getSide()); assertEquals(1.25918, transaction.getPrice(), precision); assertEquals(0.0119, transaction.getPnl(), precision); assertEquals(new Long(176403879), transaction.getLinkedTransactionId()); // TRADE_UPDATE transaction = allTransactions.get(1); assertEquals(TradeEvents.TRADE_UPDATE, transaction.getTransactionType()); assertEquals("USD_SGD", transaction.getInstrument().getInstrument()); assertEquals(new Long(3000), transaction.getUnits()); assertEquals(new Long(1782311741), transaction.getLinkedTransactionId()); // TAKE_PROFIT_FILLED transaction = allTransactions.get(2); assertEquals(TradeEvents.TAKE_PROFIT_FILLED, transaction.getTransactionType()); assertEquals("USD_CHF", transaction.getInstrument().getInstrument()); assertEquals(new Long(3000), transaction.getUnits()); assertEquals(new Long(1782379135), transaction.getLinkedTransactionId()); assertEquals(1.00877, transaction.getPrice(), precision); assertEquals(3.48, transaction.getPnl(), precision); assertEquals(TradingSignal.SHORT, transaction.getSide()); assertEquals(0.0002, transaction.getInterest(), precision); // STOP_LOSS_FILLED transaction = allTransactions.get(3); assertEquals(TradeEvents.STOP_LOSS_FILLED, transaction.getTransactionType()); assertEquals("USD_SGD", transaction.getInstrument().getInstrument()); assertEquals(new Long(3000), transaction.getUnits()); assertEquals(new Long(1782311741), transaction.getLinkedTransactionId()); assertEquals(1.39101, transaction.getPrice(), precision); assertEquals(3.3039, transaction.getPnl(), precision); assertEquals(TradingSignal.SHORT, transaction.getSide()); assertEquals(-0.0123, transaction.getInterest(), precision); // TRAILING_STOP_FILLED transaction = allTransactions.get(4); assertEquals(TradeEvents.TRAILING_STOP_FILLED, transaction.getTransactionType()); assertEquals("EUR_USD", transaction.getInstrument().getInstrument()); assertEquals(new Long(10), transaction.getUnits()); assertEquals(new Long(175739352), transaction.getLinkedTransactionId()); assertEquals(1.38137, transaction.getPrice(), precision); assertEquals(-0.0009, transaction.getPnl(), precision); assertEquals(TradingSignal.SHORT, transaction.getSide()); assertEquals(0.0, transaction.getInterest(), precision); // LIMIT_ORDER_CREATE transaction = allTransactions.get(6); assertEquals(OrderEvents.LIMIT_ORDER_CREATE, transaction.getTransactionType()); assertEquals("EUR_USD", transaction.getInstrument().getInstrument()); assertEquals(new Long(2), transaction.getUnits()); assertEquals(BigInteger.ZERO.longValue(), transaction.getLinkedTransactionId().longValue()); assertEquals(1, transaction.getPrice(), precision); assertEquals(TradingSignal.LONG, transaction.getSide()); // DAILY_INTEREST transaction = allTransactions.get(8); assertEquals(AccountEvents.DAILY_INTEREST, transaction.getTransactionType()); assertEquals("AUD_USD", transaction.getInstrument().getInstrument()); assertNull(transaction.getUnits()); assertNull(transaction.getSide()); }
From source file:io.instacount.client.InstacountClientTest.java
@Test public void testShardedCounterHappyPath() throws InstacountClientException { try {/* ww w . j a v a 2s . c om*/ final String counterName = UUID.randomUUID().toString(); { ///////// // Create the Counter final CreateShardedCounterInput createCounterInput = new CreateShardedCounterInput(counterName); final CreateShardedCounterResponse createdCounterResponse = this.client .createShardedCounter(createCounterInput); this.doBasicAssertions(createdCounterResponse, 201); } { ///////// // Get the Counter final GetShardedCounterResponse createdShardedCounterResponse = this.client .getShardedCounter(counterName); this.doBasicAssertions(createdShardedCounterResponse, 200); this.doShardedCounterAssertions(createdShardedCounterResponse.getShardedCounter(), counterName, Optional.<String>absent(), 3, CounterStatus.AVAILABLE); } { //////////// // Increment the Counter by 1 final IncrementShardedCounterResponse incrementShardedCounterResponse = client .incrementShardedCounter(counterName); this.doBasicAssertions(incrementShardedCounterResponse, 201); } { //////////// // Increment the Counter by 10 final IncrementShardedCounterResponse incrementShardedCounterResponse2 = client .incrementShardedCounter(counterName, new IncrementShardedCounterInput(BigInteger.TEN, false)); this.doBasicAssertions(incrementShardedCounterResponse2, 201); } { //////////// // Decrement the Counter by 1 final DecrementShardedCounterResponse decrementShardedCounterResponse = client .decrementShardedCounter(counterName); this.doBasicAssertions(decrementShardedCounterResponse, 201); } { //////////// // Decrement the Counter by 10 final DecrementShardedCounterResponse decrementShardedCounterResponse2 = client .decrementShardedCounter(counterName, new DecrementShardedCounterInput(BigInteger.TEN, false)); this.doBasicAssertions(decrementShardedCounterResponse2, 201); } { //////////// // Get the Counter final GetShardedCounterResponse response = client.getShardedCounter(counterName); assertThat(response.getHttpResponseCode(), is(200)); this.doShardedCounterAssertions(response.getShardedCounter(), counterName, Optional.<String>absent(), 3, CounterStatus.AVAILABLE); assertThat(response.getShardedCounter().getCount(), is(BigInteger.ZERO)); } } catch (InstacountClientException e) { assertThat(e.getErrors().getHttpResponseCode(), is(400)); throw e; } }
From source file:piuk.MyBlockChain.java
@SuppressWarnings("unchecked") public void onMessage(WebSocketMessage wmessage) { System.out.println("OnMessage()"); try {/* ww w . ja va 2s .c o m*/ String message = wmessage.getText(); System.out.println("Websocket() onMessage() " + message); Map<String, Object> top = (Map<String, Object>) JSONValue.parse(message); if (top == null) return; String op = (String) top.get("op"); if (op.equals("block")) { Map<String, Object> x = (Map<String, Object>) top.get("x"); Sha256Hash hash = new Sha256Hash(Hex.decode((String) x.get("hash"))); int blockIndex = ((Number) x.get("blockIndex")).intValue(); int blockHeight = ((Number) x.get("height")).intValue(); long time = ((Number) x.get("time")).longValue(); MyBlock block = new MyBlock(Constants.NETWORK_PARAMETERS); block.hash = hash; block.blockIndex = blockIndex; block.time = time; this.latestBlock = new StoredBlock(block, BigInteger.ZERO, blockHeight); List<MyTransaction> transactions = remoteWallet.getMyTransactions(); List<Number> txIndexes = (List<Number>) x.get("txIndexes"); for (Number txIndex : txIndexes) { for (MyTransaction tx : transactions) { MyTransactionConfidence confidence = (MyTransactionConfidence) tx.getConfidence(); if (tx.txIndex == txIndex.intValue() && confidence.height != blockHeight) { confidence.height = blockHeight; confidence.runListeners(); } } } for (PeerEventListener listener : listeners) { listener.onBlocksDownloaded(null, block, 0); } } else if (op.equals("utx")) { Map<String, Object> x = (Map<String, Object>) top.get("x"); WalletTransaction tx = MyTransaction.fromJSONDict(x); BigInteger result = BigInteger.ZERO; BigInteger previousBalance = remoteWallet.getBitcoinJWallet().final_balance; for (TransactionInput input : tx.getTransaction().getInputs()) { //if the input is from me subtract the value MyTransactionInput myinput = (MyTransactionInput) input; if (remoteWallet.isAddressMine(input.getFromAddress().toString())) { result = result.subtract(myinput.value); remoteWallet .getBitcoinJWallet().final_balance = remoteWallet.getBitcoinJWallet().final_balance .subtract(myinput.value); remoteWallet.getBitcoinJWallet().total_sent = remoteWallet.getBitcoinJWallet().total_sent .add(myinput.value); } } for (TransactionOutput output : tx.getTransaction().getOutputs()) { //if the input is from me subtract the value MyTransactionOutput myoutput = (MyTransactionOutput) output; if (remoteWallet.isAddressMine(myoutput.getToAddress().toString())) { result = result.add(myoutput.getValue()); remoteWallet .getBitcoinJWallet().final_balance = remoteWallet.getBitcoinJWallet().final_balance .add(myoutput.getValue()); remoteWallet .getBitcoinJWallet().total_received = remoteWallet.getBitcoinJWallet().total_sent .add(myoutput.getValue()); } } MyTransaction mytx = (MyTransaction) tx.getTransaction(); mytx.result = result; remoteWallet.getBitcoinJWallet().addWalletTransaction(tx); if (result.compareTo(BigInteger.ZERO) >= 0) { System.out.println("On Received"); remoteWallet.getBitcoinJWallet().invokeOnCoinsReceived(tx.getTransaction(), previousBalance, remoteWallet.getBitcoinJWallet().final_balance); } else { remoteWallet.getBitcoinJWallet().invokeOnCoinsSent(tx.getTransaction(), previousBalance, remoteWallet.getBitcoinJWallet().final_balance); } } else if (op.equals("on_change")) { String newChecksum = (String) top.get("checksum"); String oldChecksum = remoteWallet.getChecksum(); System.out.println("On change " + newChecksum + " " + oldChecksum); if (!newChecksum.equals(oldChecksum)) { try { String newPayload = MyRemoteWallet.getWalletPayload(remoteWallet.getGUID(), remoteWallet.getSharedKey(), oldChecksum); if (newPayload == null) return; remoteWallet.setPayload(newPayload); remoteWallet.getBitcoinJWallet().invokeOnChange(); } catch (Exception e) { e.printStackTrace(); } } } } catch (Exception e) { e.printStackTrace(); } }
From source file:com.ar.dev.tierra.api.controller.DetalleFacturaController.java
@RequestMapping(value = "/update", method = RequestMethod.POST) public ResponseEntity<?> update(OAuth2Authentication authentication, @RequestBody DetalleFactura detalleFactura) { Usuarios user = facadeService.getUsuariosDAO().findUsuarioByUsername(authentication.getName()); Factura factura = facadeService.getFacturaDAO().searchById(detalleFactura.getFactura().getIdFactura()); detalleFactura.setUsuarioModificacion(user.getIdUsuario()); detalleFactura.setFechaModificacion(new Date()); facadeService.getDetalleFacturaDAO().update(detalleFactura); /*Traemos lista de detalles, calculamos su nuevo total y actualizamos*/ List<DetalleFactura> detallesFactura = facadeService.getDetalleFacturaDAO() .facturaDetalle(detalleFactura.getFactura().getIdFactura()); BigDecimal sumMonto = new BigDecimal(BigInteger.ZERO); for (DetalleFactura detailList : detallesFactura) { sumMonto = sumMonto.add(detailList.getTotalDetalle()); }//from w w w . j a va 2 s .com factura.setTotal(sumMonto); factura.setFechaModificacion(new Date()); factura.setUsuarioModificacion(user.getIdUsuario()); facadeService.getFacturaDAO().update(factura); JsonResponse msg = new JsonResponse("Success", "Detalle modificado con exito"); return new ResponseEntity<>(msg, HttpStatus.OK); }
From source file:password.pwm.config.stored.ConfigurationReader.java
public void saveConfiguration(final StoredConfigurationImpl storedConfiguration, final PwmApplication pwmApplication, final SessionLabel sessionLabel) throws IOException, PwmUnrecoverableException, PwmOperationalException { File backupDirectory = null;/* w w w.j a v a 2s.co m*/ int backupRotations = 0; if (pwmApplication != null) { final Configuration configuration = new Configuration(storedConfiguration); final String backupDirSetting = configuration.readAppProperty(AppProperty.BACKUP_LOCATION); if (backupDirSetting != null && backupDirSetting.length() > 0) { final File pwmPath = pwmApplication.getPwmEnvironment().getApplicationPath(); backupDirectory = FileSystemUtility.figureFilepath(backupDirSetting, pwmPath); } backupRotations = Integer.parseInt(configuration.readAppProperty(AppProperty.BACKUP_CONFIG_COUNT)); } { // increment the config epoch String epochStrValue = storedConfiguration.readConfigProperty(ConfigurationProperty.CONFIG_EPOCH); try { final BigInteger epochValue = epochStrValue == null || epochStrValue.length() < 0 ? BigInteger.ZERO : new BigInteger(epochStrValue); epochStrValue = epochValue.add(BigInteger.ONE).toString(); } catch (Exception e) { LOGGER.error(sessionLabel, "error trying to parse previous config epoch property: " + e.getMessage()); epochStrValue = "0"; } storedConfiguration.writeConfigProperty(ConfigurationProperty.CONFIG_EPOCH, epochStrValue); } if (backupDirectory != null && !backupDirectory.exists()) { if (!backupDirectory.mkdirs()) { throw new PwmOperationalException(new ErrorInformation(PwmError.ERROR_UNKNOWN, "unable to create backup directory structure '" + backupDirectory.toString() + "'")); } } try { final File tempWriteFile = new File(configFile.getAbsoluteFile() + ".new"); LOGGER.info(sessionLabel, "beginning write to configuration file " + tempWriteFile); saveInProgress = true; storedConfiguration.toXml(new FileOutputStream(tempWriteFile, false)); LOGGER.info("saved configuration " + JsonUtil.serialize(storedConfiguration.toJsonDebugObject())); if (pwmApplication != null) { final String actualChecksum = storedConfiguration.settingChecksum(); pwmApplication.writeAppAttribute(PwmApplication.AppAttribute.CONFIG_HASH, actualChecksum); } LOGGER.trace( "renaming file " + tempWriteFile.getAbsolutePath() + " to " + configFile.getAbsolutePath()); try { Files.move(tempWriteFile.toPath(), configFile.toPath(), StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE); } catch (Exception e) { final String errorMsg = "unable to rename temporary save file from " + tempWriteFile.getAbsolutePath() + " to " + configFile.getAbsolutePath() + "; error: " + e.getMessage(); throw new PwmUnrecoverableException(new ErrorInformation(PwmError.ERROR_UNKNOWN, errorMsg)); } if (backupDirectory != null) { final String configFileName = configFile.getName(); final String backupFilePath = backupDirectory.getAbsolutePath() + File.separatorChar + configFileName + "-backup"; final File backupFile = new File(backupFilePath); FileSystemUtility.rotateBackups(backupFile, backupRotations); storedConfiguration.toXml(new FileOutputStream(backupFile, false)); } } finally { saveInProgress = false; } }
From source file:io.ecarf.core.cloud.task.processor.reason.phase2.DoReasonTask9.java
@Override public void run() throws IOException { GoogleCloudService cloud = (GoogleCloudService) this.getCloudService(); Stopwatch stopwatch1 = Stopwatch.createUnstarted(); Stopwatch stopwatch2 = Stopwatch.createUnstarted(); this.setup(cloud); String decoratedTable = table; int emptyRetries = 0; int totalInferredTriples = 0; int maxRetries; if (this.retries == null) { maxRetries = Config.getIntegerProperty(Constants.REASON_RETRY_KEY, 6); } else {/*from w ww.j a v a 2 s . c om*/ maxRetries = this.retries; } int cycleSleep; if (this.sleep == null) { cycleSleep = Config.getIntegerProperty(Constants.REASON_SLEEP_KEY, 20); } else { cycleSleep = this.sleep; } this.ddLimit = Config.getIntegerProperty(Constants.REASON_DATA_DIRECT_DOWNLOAD_LIMIT, 1_200_000); int streamingThreshold = Config.getIntegerProperty("ecarf.io.reasoning.streaming.threshold", 100000); String instanceId = cloud.getInstanceId(); int processors = Runtime.getRuntime().availableProcessors(); if (processors > 1) { this.executor = Utils.createFixedThreadPool(processors); } int count = 0; QueryGenerator<Long> generator = new QueryGenerator<Long>(schemaTerms, null); // timestamp loop do { // First of all run all the queries asynchronously and remember the jobId and filename for each term generator.setDecoratedTable(decoratedTable); String query = generator.getQuery(); log.debug("Generated Query: " + query); String queryResultFilePrefix = instanceId + "_QueryResults_" + count; String jobId = cloud.startBigDataQuery(query, new BigDataTable(this.table)); //QueryResult queryResult = QueryResult.create().setFilename(queryResultFilePrefix).setJobId(jobId); long start = System.currentTimeMillis(); // block and wait for each job to complete then save results to a file QueryStats stats = cloud.saveBigQueryResultsToFile(jobId, queryResultFilePrefix, this.bucket, processors, this.ddLimit); BigInteger rows = stats.getTotalRows(); this.totalBytes = this.totalBytes + stats.getTotalProcessedBytes(); Set<Long> productiveTerms = new HashSet<>(); Set<String> inferredTriplesFiles = new HashSet<>(); int interimInferredTriples = 0; // only process if triples are found matching this term if ((rows != null) && !BigInteger.ZERO.equals(rows)) { stopwatch1.start(); interimInferredTriples = this.inferAndSaveTriplesToFile(stats, productiveTerms, processors, inferredTriplesFiles); this.totalRows = this.totalRows.add(rows); stopwatch1.stop(); } else { log.info("Skipping query as no data is found"); } totalInferredTriples += interimInferredTriples; if (interimInferredTriples > 0) { // stream smaller numbers of inferred triples // try uploading from cloud storage log.info("Inserting " + interimInferredTriples + ", inferred triples into Big Data table for " + productiveTerms.size() + " productive terms. Filename: " + inferredTriplesFiles); if (interimInferredTriples <= streamingThreshold) { // stream the data Set<Triple> inferredTriples = new HashSet<>(); for (String inferredTriplesFile : inferredTriplesFiles) { TripleUtils.loadCompressedCSVTriples(inferredTriplesFile, true, inferredTriples); } log.info("Total triples to stream into Big Data: " + inferredTriples.size()); cloud.streamObjectsIntoBigData(inferredTriples, TableUtils.getBigQueryEncodedTripleTable(table)); log.info("All inferred triples are streamed into Big Data table"); } else { List<String> cloudStorageFiles = new ArrayList<>(); // load the data through cloud storage // upload the file to cloud storage for (String inferredTriplesFile : inferredTriplesFiles) { log.info("Uploading inferred triples file into cloud storage: " + inferredTriplesFile); StorageObject file = cloud.uploadFileToCloudStorage(inferredTriplesFile, bucket); log.info("File " + file + ", uploaded successfully. Now loading it into big data."); cloudStorageFiles.add(file.getUri()); } jobId = cloud.loadCloudStorageFilesIntoBigData(cloudStorageFiles, TableUtils.getBigQueryEncodedTripleTable(table), false); log.info( "All inferred triples are loaded into Big Data table through cloud storage, completed jobId: " + jobId); } // reset empty retries emptyRetries = 0; stopwatch2.reset(); } else { log.info("No new inferred triples"); // increment empty retries emptyRetries++; if (!stopwatch2.isRunning()) { stopwatch2.start(); } } log.info("Total inferred triples so far = " + totalInferredTriples + ", current retry count: " + emptyRetries); if (emptyRetries < maxRetries) { ApiUtils.block(cycleSleep); // FIXME move into the particular cloud implementation service long elapsed = System.currentTimeMillis() - start; decoratedTable = "[" + table + "@-" + elapsed + "-]"; log.info("Using table decorator: " + decoratedTable + ". Empty retries count: " + emptyRetries); } count++; } while (emptyRetries < maxRetries); // end timestamp loop executor.shutdown(); log.info("Finished reasoning, total inferred triples = " + totalInferredTriples); //log.info("Number of avoided duplicate terms = " + this.duplicates); log.info("Total rows retrieved from big data = " + this.totalRows); log.info("Total processed GBytes = " + ((double) this.totalBytes / FileUtils.ONE_GB)); log.info("Total process reasoning time (serialization in inf file) = " + stopwatch1); log.info("Total time spent in empty inference cycles = " + stopwatch2); }