Package de.mhus.lib

Examples of de.mhus.lib.MStopWatch


     
      DbPool pool = createPool().getPool("test");
     
      BookStoreSchema schema = new BookStoreSchema();
     
      MStopWatch timer = new MStopWatch();
      timer.start();
     
      DbManager manager = new DbManager(pool, schema);

      // create persons
      Person p = new Person();
      p.setName("Klaus Mustermann");
      manager.createObject(p);
      UUID p1 = p.getId();
     
      p.setId(null);
      p.setName("Alex Admin");
      manager.createObject(p);
     
      UUID p2 = p.getId();
     
      // create books
     
      Book b = new Book();
      b.setName("Yust Another Boring Book");
      manager.createObject(b);
      UUID b1 = b.getId();
     
      b.setId(null);
      b.setName("Mystic Almanach");
      manager.createObject(b);
      UUID b2 = b.getId();
     
      // get a book and modify
     
      b = (Book) manager.getObject(Book.class, b1);
      b.setLendToId(p1);
      manager.saveObject(b);
     
      b.setLendToId(null);
      manager.saveObject(b);
     
      b.setLendToId(p2);
      b.setAuthorId(new UUID[] {p1});
      manager.saveObject(b);

      Book copy = (Book) manager.getObject(Book.class, b.getId());
      assertEquals(copy.getAuthorId()[0],p1) ;

     
      // test relations
      assertNotNull(b.getLendTo());
      Person rel = b.getLendTo().getRelation();
      assertNotNull(rel);
      assertEquals(rel.getId(), p2);
     
      assertNotNull(rel.getLendTo());
      List<Book> retRel = rel.getLendTo().getRelations();
      assertNotNull(retRel);
      assertEquals(1,retRel.size());
      assertEquals(b.getId(), retRel.get(0).getId());

      retRel.add(manager.getObject(Book.class, b2));
     
      manager.saveObject(rel);
     
      rel.getLendTo().reset();
      retRel = rel.getLendTo().getRelations();
      assertNotNull(retRel);
      assertEquals(2,retRel.size());
     
      b = manager.getObject(Book.class, b1);
      assertNotNull(b.getLendToId());
      b.getLendTo().setRelation(null);
      manager.saveObject(b);
      assertNull(b.getLendToId());
      b.getLendTo().setRelation(p);
      manager.saveObject(b);
      assertNotNull(b.getLendToId());
     
     
      // remove book
     
      manager.removeObject(b);
     
      b = (Book) manager.getObject(Book.class, b1);
      assertNull(b);
     
      // test selection
     
      System.out.println("----------------------");
      DbCollection<Person> col = manager.executeQuery(new Person(), "select * from $db.Person$"null);
      int count = 0;
      for (Person pp : col) {
        System.out.println("--- " + pp.getId() + " " + pp.getName());
        count++;
      }
      assertEquals(count, 3);
     
      System.out.println("----------------------");
     
      col = manager.getByQualification(new Person(), null,  null);
      count = 0;
      for (Person pp : col) {
        System.out.println("--- " + pp.getId() + " " + pp.getName());
        count++;
      }
      assertEquals(count, 3);
     
      System.out.println("----------------------");
     
      col = manager.getByQualification(new Person(), "$db.Person.Name$ like 'Klaus%'"null);
      count = 0;
      for (Person pp : col) {
        System.out.println("--- " + pp.getId() + " " + pp.getName());
        count++;
      }
      assertEquals(count, 1);
     
      System.out.println("----------------------");
     
      // test a native sql execute - remove all persons
     
      DbConnection con = manager.getPool().getConnection();
      con.createStatement("DELETE FROM $db.Person$", null ).execute(manager.getNameMapping());
      con.commit();
     
      System.out.println("----------------------");
      col = manager.executeQuery(new Person(), "select * from $db.Person$"null);
      count = 0;
      for (Person pp : col) {
        System.out.println("--- " + pp.getId() + " " + pp.getName());
        count++;
      }
      assertEquals(count, 0);

      // -------------
      // test comfortable object
     
      Store s1 = new Store();
      s1.setName("Creasy Bookstore");
      s1.setAddress("The Oaks\nDublin");
      s1.create(manager);
     
      s1.setAddress("The Lakes\nDublin");
      s1.save();
     
      // test change in another session and reload
      Store s2 = (Store) manager.getObject(Store.class, s1.getId());
      s2.setAddress("");
      s2.save();
     
      s1.reload();
      assertEquals(s1.getAddress(), s2.getAddress());
     
      // remove and check behavior of updates
      s1.remove();
     
      try {
        s2.reload();
        assertTrue(false);
      } catch (MException e) {
        System.out.println(e);
      }

      try {
        s2.save();
        assertTrue(false);
      } catch (MException e) {
        System.out.println(e);
      }
     
      // -------------
      // test access control
 
      Finances f = new Finances();
      f.setActiva(10);
      f.setPassiva(10);
      f.setStore(s1.getId());
      f.create(manager);
     
      f.setActiva(20);
      f.save();
     
      f.setNewConfidential("write");
      f.save();
     
      try {
        f.save();
        assertTrue(false);
      } catch (MException e) {
        System.out.println(e);
      }
     
      f.reload();

      try {
        f.save();
        assertTrue(false);
      } catch (MException e) {
        System.out.println(e);
      }

      f.setConfidential("read"); // hack :)
      f.setNewConfidential("read");
      f.save();

      try {
        f.reload();
        assertTrue(false);
      } catch (MException e) {
        System.out.println(e);
      }
     
      f.setConfidential("read"); // hack :)
      f.setNewConfidential("remove");
      f.save();
      f.reload();
     
      try {
        f.remove();
        assertTrue(false);
      } catch (MException e) {
        System.out.println(e);
      }
     
      f.setConfidential("read"); // hack :)
      f.setNewConfidential("");
      f.save();
      f.reload();
      f.remove();
     
      // -------------
      // test dynamic objects

      Regal r = new Regal();
      r.setValue("store", s1.getId());
      r.setValue("name", "regal 1");
      r.create(manager);
     
      r.setValue("name", "regal 22113221");
      r.save();
     
      Regal r2 = (Regal) manager.getObject(Regal.class, r.getValue("id"));
      assertNotNull(r2);

      r2.reload();
     
      r2.remove();
           
     
     
     
      timer.stop();
      System.out.println("Time: " + timer.getCurrentTimeAsString(true));
    }
View Full Code Here


  private boolean commitAfterFinish;
  private boolean needAllFolders;

  public void compare( TreeMap<String,CompareDirEntry> current, TreeMap<String,CompareDirEntry> last, Listener listener ) {
   
    MStopWatch tk = new MStopWatch()
    tk.start();
   
    log.t( "START");
    listener.start( current, last );
   
    totalSize = current.size() + last.size();
    totalCnt  = 0;
   
    Iterator<Map.Entry<String,CompareDirEntry>> cur = current.entrySet().iterator();
    Iterator<Map.Entry<String,CompareDirEntry>> old = last.entrySet().iterator();
   
    Map.Entry<String,CompareDirEntry> curEntry = null;
    Map.Entry<String,CompareDirEntry> oldEntry = null;
   
    String      curKey = null;
    CompareDirEntry curVal = null;
   
    String        oldKey = null;
    CompareDirEntry oldVal = null;
   
    if ( cur.hasNext() ) {
      curEntry = cur.next();
      curKey   = curEntry.getKey();
      curVal   = curEntry.getValue();
      log.t( "CUR",curKey );
      totalCnt++;
      currentCnt++;
    }
   
    if ( old.hasNext() ) {
      oldEntry = old.next();
      oldKey   = (String)oldEntry.getKey();
      oldVal   = (CompareDirEntry)oldEntry.getValue();
      log.t( "OLD",oldKey );
      totalCnt++;
    }
   
    while ( true ) {
     
      if ( curEntry == null && oldEntry == null || killed )
        break;
     
     
     
      int comp = 0;
     
      if ( curEntry != null && oldEntry != null )
        comp = curKey.compareTo( oldKey );
      else
      if ( curEntry == null )
        comp = 1;
      else
        comp = -1;
       
      if ( comp == 0 ) {
       
        // found same check id and vstamp
        if ( fullRefresh || ! curVal.compareWithEntry(oldVal) ) {
         
          if ( isPath( curKey ) ) {
            // check by listener
            log.t( "UPDATE");
            updateCnt++;
            boolean ret = listener.updateObject( curKey, curVal, oldVal );
            if ( commitAfterEveryEvent ) {
              if ( ret )
                doCommit();
              else
                doRollback();
            }
          }         
        }
       
       
        if ( cur != null && cur.hasNext() ) {
          curEntry = cur.next();
          curKey   = curEntry.getKey();
          curVal   = curEntry.getValue();
          log.t( "CUR,",curKey );
          totalCnt++;
          currentCnt++;
        } else
          curEntry = null;
       
        if ( old != null && old.hasNext() ) {
          oldEntry = old.next();
          oldKey   = oldEntry.getKey();
          oldVal   = oldEntry.getValue();
          log.t( "OLD",oldKey );
          totalCnt++;
        } else
          oldEntry = null;
       
        continue;
       
      } else
     
      if ( comp > 0 ) {
       
        // old has new one (deleted in cur), check by listener
        if ( isPath( oldKey ) ) {
          log.t( "DELETE");
          deleteCnt++;
          boolean ret = listener.deleteObject( oldKey, oldVal );
          if ( commitAfterEveryEvent ) {
            if ( ret )
              doCommit();
            else
              doRollback();
          }
        }
       
        if ( old != null && old.hasNext() ) {
          oldEntry = old.next();
          oldKey   = oldEntry.getKey();
          oldVal   = oldEntry.getValue();
          log.t( "OLD",oldKey );
          totalCnt++;
        } else
          oldEntry = null;
       
       
      } else {
        // comp < 0
       
        // cur has new one, check by listener
        if ( isPath( curKey ) ) {
          log.t( "CREATE");
          insertCnt++;
          boolean ret = listener.createObject( curKey, curVal );
          if ( commitAfterEveryEvent ) {
            if ( ret )
              doCommit();
            else
              doRollback();
          }
        }
        
        if ( cur != null && cur.hasNext() ) {
          curEntry = cur.next();
          curKey   = curEntry.getKey();
          curVal   = curEntry.getValue();
          log.t( "CUR",curKey );
          totalCnt++;
          currentCnt++;
        } else
          curEntry = null;
       
      }
     

     
    }
   
    log.t( "FINISH");
    boolean ret = listener.finish( current, last );
    if ( commitAfterFinish ) {
      if ( ret )
        doCommit();
      else
        doRollback();
    }
   
    tk.stop();
    log.d( "Time",tk.getCurrentTimeAsString( true ) );

  }
View Full Code Here

TOP

Related Classes of de.mhus.lib.MStopWatch

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.