Monday, November 10, 2014

Google App Engine Pitfalls 2.0

Logging - add this:

 private static final Logger log = Logger.getLogger(GreetingServiceImpl.class.getName());  



...and this...for some reason...

 log.setLevel(Level.INFO);  



...and of course.

 log.info("The query returned " + pq.countEntities() + " entries.");  

Google App Engine and Datastore Pitfalls

Working with Google App Engine and hit some interesting "learning opportunities":

 private void persistData(String[] input, User currentUser) {  
   String weeklystatusName = "weeklystatuslist";  
   Key statusKey = KeyFactory.createKey("WeeklyStatus", weeklystatusName);  
   Date date = new Date();  
   Entity status = new Entity("Status", statusKey);  
   status.setProperty("userid", currentUser.getNickname());  
   status.setProperty("date", date);  
   status.setProperty("name", input[0]);  
   status.setProperty("date_begin", input[1]);  
   status.setProperty("date_end", input[2]);  
   status.setProperty("client_name", input[3]);  
   status.setProperty("accomplishments", input[4]);  
   status.setProperty("risks", input[5]);  
   status.setProperty("next_week", input[6]);  
   status.setProperty("hours", input[7]);  
   status.setProperty("pto", input[8]);  
   status.setProperty("distribution", input[9]);  
   status.setProperty("submitted", input[10]);  
   DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();  
   datastore.put(status);  
 }  


Running a query on the datastore didn't return anything though I could see my data while browsing.  And...the query didn't work in GQL either.  Finally I discovered that what I thought were Strings saved to the datastore were actually User objects.  Datastore doesn't care what you pass to it...it'll save that object.  But just because it looks like a string doesn't mean it is.

One getNickname() later...query worked.  Added a sort...

select * from Status where userid='user1' order by date desc

Query broke.  Gah.  No wait...it says I need to add an index.  Added that to WEB-INF and redeployed.

 <?xml version="1.0" encoding="utf-8"?>  
 <datastore-indexes autoGenerate="true">  
      <datastore-index kind="Status" ancestor="false" source="manual">  
           <property name="userid" direction="asc"/>  
           <property name="date" direction="desc"/>  
      </datastore-index>  
 </datastore-indexes>  

Still failed.  Eventually I realized that "kind" has to be the name of my table.  (My table was called "Status" not "Statuses".)  It sort of makes sense but not really, since you might expect an index could be applied to multiple tables or at least that I'd get an error message if I tried to create an index that wasn't connected to an existing table-space.

Datastore seems to be designed as Fisher-Price My First Database in some ways but then it uses unusual standards (by Oracle/DB2/mySQL standards) and doesn't provide detailed documentation.  Still though, very cool once you get used to it.

Tuesday, March 4, 2014

Ruby Tricks - Listing time spent in code

<some code>

@@t = ::Time.now

<more code>

puts "Time: " + (::Time.now-@@t).to_s +  "\n"

Note: the ::Time is only there because I ran into a conflict between the Time class in Ruby and the Time class in one of the gems I'm using.

Thursday, July 19, 2012

Truncate vs Delete in Oracle

Database 101 says...always truncate instead of doing a delete from on tables. It's faster and more efficient. Well, everything you learn in a beginning class is wrong sometimes.

We had a set of Junit tests that truncated a set of Oracle tables a few thousand times. It took a long time. Changing those truncates to deletes made the tests run about 10 times as fast. So apparently truncate has some overhead that delete does not.

Lesson: the stuff you learn in Anything 101 should be verified.

Wednesday, June 9, 2010

Googley

We've been re-architecting our application from Swing to GWT and it's been working quite well. GWT is surprisingly stable and non-quirky. In general. It has some visual issues with IE6 but then IE6 has some issues with IE6.

One thing we did was use an event bus to pass events around. This solved a number of issues for us. Sometimes an event in one view would need to trigger a change in another view, or multiple views and this pattern allowed us to easily trigger those changes without having to clutter up the interfaces of parent classes.

Thursday, January 28, 2010

Testing Testing

The different ways to test a DB connection:

MySQL/PostgreSQL/MS SQL use: SELECT 1
DB2 use: SELECT 1 FROM sysibm.sysdummy1
Oracle use: SELECT 1 from dual

Why do DB2 and Oracle always have to be different? Just because they can?

Wednesday, October 21, 2009

Swing Low

Thanks to the book Swing Hacks for this one. An extended version of JButton that blurs when it's disabled.

/**
*
* @author bhegberg
*/
public class BlurJButton extends JButton {
public BlurJButton(String text) {
super(text);
}

public void paintComponent(Graphics g) {
if (isEnabled()) {
super.paintComponent(g);
return;
}
BufferedImage buf = new BufferedImage(getWidth(),getHeight(), BufferedImage.TYPE_INT_RGB);
super.paintComponent(buf.getGraphics());
// Blur the buffered image
float[] my_kernel = {
0.10f, 0.10f, 0.10f,
0.10f, 0.20f, 0.10f,
0.10f, 0.10f, 0.10f };
ConvolveOp op = new ConvolveOp(new Kernel(3,3, my_kernel), ConvolveOp.EDGE_NO_OP, null);
Image img = op.filter(buf,null);

g.drawImage(img,0,0,null);
}
}