List of usage examples for java.lang Long max
public static long max(long a, long b)
From source file:com.github.lynxdb.server.core.TimeSerie.java
public static TimeSerie merge(List<TimeSerie> _series) { Assert.notEmpty(_series);//from w w w . j av a 2s . c o m List<SuperIterator> sil = new ArrayList<>(); _series.forEach((TimeSerie t) -> { if (t.hasNext()) { SuperIterator<Entry> si = new SuperIterator<>(t); si.next(); sil.add(si); } }); Map<String, String> tags = new HashMap<>(); tags.putAll(_series.get(0).getTags()); _series.forEach((TimeSerie t) -> { Iterator<Map.Entry<String, String>> i = tags.entrySet().iterator(); while (i.hasNext()) { Map.Entry<String, String> e = i.next(); if (!t.getTags().containsKey(e.getKey()) || !t.getTags().get(e.getKey()).equals(e.getValue())) { i.remove(); } } }); return new TimeSerie(_series.get(0).getName(), tags, new ChainableIterator<Entry>() { @Override public boolean hasNext() { return sil.stream() .anyMatch((superIterator) -> superIterator.hasNext() || superIterator.getCurrent() != null); } @Override public Entry next() { Iterator<SuperIterator> rr = sil.iterator(); while (rr.hasNext()) { if (rr.next().getCurrent() == null) { rr.remove(); } } long max = Long.MIN_VALUE; for (SuperIterator<Entry> r : sil) { max = Long.max(max, r.getCurrent().getTime()); } for (SuperIterator<Entry> r : sil) { if (r.getCurrent().getTime() == max) { r.next(); return r.getPrevious(); } } throw new IllegalStateException("something went wrong"); } }); }
From source file:mydropbox.Client.java
public void checkForChanges() throws IOException { long timestamp_current = getTimeStamp(); long timestamp_cloud = askForTimeStamp(); long timestampToSave; Set<String> fileList_cloud = new HashSet<String>(); Set<String> fileList_local = new HashSet<String>(); if (timestamp_current != timestamp_cloud) { timestampToSave = Long.max(timestamp_cloud, timestamp_current); fileList_cloud = askForFileList(); File folder = new File("./files/"); File[] listOfFiles = folder.listFiles(); for (File f : listOfFiles) { if (f.isFile()) { fileList_local.add(f.getName()); }/* www . j ava 2s . c o m*/ } if (timestamp_current < timestamp_cloud) { System.out.println("Cloud is ahead"); //Files that need to be removed Set<String> toRemoveList = new HashSet<String>(); toRemoveList.addAll(fileList_local); toRemoveList.removeAll(fileList_cloud); //Files that need to be downloaded Set<String> toTransferList = new HashSet<String>(); toTransferList.addAll(fileList_cloud); toTransferList.removeAll(fileList_local); try { File file; for (String f : toRemoveList) { file = new File("./files/" + f); file.delete(); } for (String f : toTransferList) { downloadFile(f); } } catch (Exception e) { e.printStackTrace(); } finally { setTimeStamp(timestampToSave); } } else { System.out.println("Client is ahead"); //Files that need to be removed in the cloud Set<String> toRemoveList = new HashSet<String>(); toRemoveList.addAll(fileList_cloud); toRemoveList.removeAll(fileList_local); //Files that need to be uploaded Set<String> toTransferList = new HashSet<String>(); toTransferList.addAll(fileList_local); toTransferList.removeAll(fileList_cloud); try { File file; for (String f : toRemoveList) { deleteFile(f); } for (String f : toTransferList) { sendFile(f); } } catch (Exception e) { e.printStackTrace(); } finally { setTimeStamp(timestampToSave); } } } }
From source file:org.structr.common.BasicTest.java
@Test public void testObjectCreationAfterRollback() { /**/*w w w . j a va2 s . c o m*/ * 1. Test Setup: * - Clean Database * - Create "bad" type with error in onCreate * - Create "good" type * - Create a test user */ cleanDatabaseAndSchema(); try { Thread.sleep(1000L); } catch (InterruptedException ex) { logger.error("Thread sleep was interrupted", ex); } // setup: create dynamic type with onCreate() method try (final Tx tx = app.tx()) { SchemaNode badNodeType = createTestNode(SchemaNode.class, "BadNode"); badNodeType.setProperty(new StringProperty("___onCreate"), "{ return Structr.error('name', 'no node of this type can not be created!'); }"); SchemaNode goodNodeType = createTestNode(SchemaNode.class, "GoodNode"); tx.success(); } catch (FrameworkException fex) { logger.warn("", fex); fail("Unexpected exception."); } Principal user = null; // create user try (final Tx tx = app.tx()) { user = app.create(Principal.class, "tester"); tx.success(); } catch (FrameworkException fex) { logger.warn("", fex); fail("Unexpected exception."); } final SecurityContext ctx = SecurityContext.getInstance(user, AccessMode.Backend); final App userApp = StructrApp.getInstance(ctx); /** * 2. Test Execution: * - Try to create one node of the type that fails ==> rollback * - Create HALF as many nodes of a type that can be created ==> the fail probability is quite high (fail meaning that a previously deleted relationship is still present in the cache and not marked as stale) */ long maxId = 0; try (final Tx tx = userApp.tx()) { Class failingType = StructrApp.getConfiguration().getNodeEntityClass("BadNode"); final NodeInterface x = userApp.create(failingType, "should fail"); for (RelationshipInterface r : x.getIncomingRelationships()) { System.out.println(r.getRelType().name() + ": " + r.getId()); maxId = Long.max(maxId, r.getId()); } tx.success(); } catch (FrameworkException fex) { // expected error assertEquals("BadNode.name no node of this type can not be created!", fex.toString()); } /** * Sleep for some time - in the hopes that the database will re-use the previously deleted relationship ids */ try { logger.info("Waiting for 20 seconds..."); Thread.sleep(20000L); } catch (InterruptedException ex) { logger.error("Thread sleep was interrupted", ex); } try (final Tx tx = userApp.tx()) { Class goodType = StructrApp.getConfiguration().getNodeEntityClass("GoodNode"); for (int cnt = 0; cnt < (maxId / 2); cnt++) { userApp.create(goodType, "Good node " + cnt); } tx.success(); } catch (ClassCastException cce) { logger.warn("", cce); fail("ClassCastException occurred - this happens because a relationship cache entry was not set to stale after a rollback!"); } catch (FrameworkException fex) { logger.warn("", fex); if (fex.getStatus() == 403) { fail("Previously existing relationship (which was deleted) but not set stale has been re-used in the cache... which is why the user is not allowed to modify his own node"); } fail("Unexpected exception."); } }
From source file:org.structr.common.BasicTest.java
@Test public void testRelationshipCreationAfterRollback() { cleanDatabaseAndSchema();/* w w w .j a va2 s .c o m*/ try { Thread.sleep(1000L); } catch (InterruptedException ex) { logger.error("Thread sleep was interrupted", ex); } // setup: create dynamic type with onCreate() method try (final Tx tx = app.tx()) { final SchemaNode source = app.create(SchemaNode.class, "Source"); final SchemaNode target = app.create(SchemaNode.class, "Target"); final SchemaRelationshipNode rel = app.create(SchemaRelationshipNode.class, new NodeAttribute(SchemaRelationshipNode.relationshipType, "LINK"), new NodeAttribute(SchemaRelationshipNode.sourceNode, source), new NodeAttribute(SchemaRelationshipNode.targetNode, target), new NodeAttribute(SchemaRelationshipNode.sourceMultiplicity, "1"), new NodeAttribute(SchemaRelationshipNode.targetMultiplicity, "1")); rel.setProperty(new StringProperty("___onCreate"), "{ return Structr.error('name', 'relationship can not be created!'); }"); tx.success(); } catch (FrameworkException fex) { logger.warn("", fex); fail("Unexpected exception."); } Principal user = null; // create user try (final Tx tx = app.tx()) { user = app.create(Principal.class, "tester"); tx.success(); } catch (FrameworkException fex) { logger.warn("", fex); fail("Unexpected exception."); } final SecurityContext ctx = SecurityContext.getInstance(user, AccessMode.Backend); final App userApp = StructrApp.getInstance(ctx); NodeInterface sourceNode = null; NodeInterface targetNode = null; try (final Tx tx = userApp.tx()) { Class sourceType = StructrApp.getConfiguration().getNodeEntityClass("Source"); Class targetType = StructrApp.getConfiguration().getNodeEntityClass("Target"); sourceNode = userApp.create(sourceType, "source node"); targetNode = userApp.create(targetType, "target node"); tx.success(); } catch (FrameworkException fex) { fail("Unexpected exception."); } assertNotNull(sourceNode); assertNotNull(targetNode); long maxId = 0; try (final Tx tx = userApp.tx()) { final Class relClass = StructrApp.getConfiguration() .getRelationClassForCombinedType(sourceNode.getType(), "LINK", targetNode.getType()); userApp.create(sourceNode, targetNode, relClass); for (RelationshipInterface r : targetNode.getIncomingRelationships()) { System.out.println(r.getRelType().name() + ": " + r.getId()); maxId = Long.max(maxId, r.getId()); } tx.success(); } catch (FrameworkException fex) { // expected error assertEquals("LINK.name relationship can not be created!", fex.toString()); } maxId = Long.max(maxId, 100); /** * Sleep for some time - in the hopes that the database will re-use the previously deleted relationship ids */ try { logger.info("Waiting for 20 seconds..."); Thread.sleep(20000L); } catch (InterruptedException ex) { logger.error("Thread sleep was interrupted", ex); } try (final Tx tx = userApp.tx()) { Class goodType = StructrApp.getConfiguration().getNodeEntityClass("Source"); for (int cnt = 0; cnt < (maxId); cnt++) { userApp.create(goodType, "Surce node " + cnt); } tx.success(); } catch (ClassCastException cce) { logger.warn("", cce); fail("ClassCastException occurred - this happens because a relationship cache entry was not set to stale after a rollback!"); } catch (FrameworkException fex) { logger.warn("", fex); if (fex.getStatus() == 403) { fail("Previously existing relationship (which was deleted) but not set stale has been re-used in the cache... which is why the user is not allowed to modify his own node"); } fail("Unexpected exception."); } }