From 4748d5c1ab5e408554aa3841d348a67a4f2ba82f Mon Sep 17 00:00:00 2001 From: hermens Date: Thu, 20 Dec 2007 17:11:35 +0000 Subject: [PATCH] Some enhancements to time management: - remove unnecessary generation of Calendar and Date objects - synchronized SimpleDateFormat objects in blog-, message- and wikiBoard - correct use of TimeZones and SimpleDateFormats git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@4288 6c8d7289-2bf4-0310-a012-ef5d649a1542 --- source/de/anomic/data/blogBoardComments.java | 26 ++++++++++++------- source/de/anomic/data/messageBoard.java | 14 +++++++--- source/de/anomic/data/wikiBoard.java | 20 +++++++++----- source/de/anomic/http/httpHeader.java | 4 +-- source/de/anomic/http/httpc.java | 6 ++--- .../kelondro/kelondroCollectionIndex.java | 8 +++--- source/de/anomic/net/ftpc.java | 9 ++----- 7 files changed, 49 insertions(+), 38 deletions(-) diff --git a/source/de/anomic/data/blogBoardComments.java b/source/de/anomic/data/blogBoardComments.java index c65b5c2b2..7f6971ef2 100644 --- a/source/de/anomic/data/blogBoardComments.java +++ b/source/de/anomic/data/blogBoardComments.java @@ -51,7 +51,6 @@ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; -import java.util.GregorianCalendar; import java.util.HashMap; import java.util.Iterator; import java.util.Map; @@ -77,9 +76,12 @@ public class blogBoardComments { private static final String dateFormat = "yyyyMMddHHmmss"; private static final int recordSize = 512; - private static TimeZone GMTTimeZone = TimeZone.getTimeZone("PST"); private static SimpleDateFormat SimpleFormatter = new SimpleDateFormat(dateFormat); + static { + SimpleFormatter.setTimeZone(TimeZone.getTimeZone("GMT")); + } + private kelondroMapObjects datbase = null; public blogBoardComments(File actpath, long preloadTime) { @@ -98,7 +100,9 @@ public class blogBoardComments { } private static String dateString(Date date) { - return SimpleFormatter.format(date); + synchronized (SimpleFormatter) { + return SimpleFormatter.format(date); + } } private static String normalize(String key) { @@ -132,7 +136,7 @@ public class blogBoardComments { record = new HashMap(); key = nkey; if (key.length() > keyLength) key = key.substring(0, keyLength); - if(date == null) date = new GregorianCalendar(GMTTimeZone).getTime(); + if(date == null) date = new Date(); record.put("date", dateString(date)); if (subject == null) record.put("subject",""); else record.put("subject", kelondroBase64Order.enhancedCoder.encode(subject)); @@ -169,10 +173,12 @@ public class blogBoardComments { try { String c = (String) record.get("date"); if (c == null) { - System.out.println("DEBUG - ERROR: date field missing in blogBoard"); - return new Date(); - } - return SimpleFormatter.parse(c); + System.out.println("DEBUG - ERROR: date field missing in blogBoard"); + return new Date(); + } + synchronized (SimpleFormatter) { + return SimpleFormatter.parse(c); + } } catch (ParseException e) { return new Date(); } @@ -240,7 +246,7 @@ public class blogBoardComments { key = normalize(key); if (key.length() > keyLength) key = key.substring(0, keyLength); Map record = base.getMap(key); - if (record == null) return newEntry(key, "".getBytes(), "anonymous".getBytes(), "127.0.0.1", new GregorianCalendar(GMTTimeZone).getTime(), "".getBytes()); + if (record == null) return newEntry(key, "".getBytes(), "anonymous".getBytes(), "127.0.0.1", new Date(), "".getBytes()); return new CommentEntry(key, record); } @@ -253,7 +259,7 @@ public class blogBoardComments { } catch (IOException e) { e.printStackTrace(); } - if (record == null) return newEntry(key, "".getBytes(), "anonymous".getBytes(), "127.0.0.1", new GregorianCalendar(GMTTimeZone).getTime(), "".getBytes()); + if (record == null) return newEntry(key, "".getBytes(), "anonymous".getBytes(), "127.0.0.1", new Date(), "".getBytes()); return new CommentEntry(key, record.record); }*/ diff --git a/source/de/anomic/data/messageBoard.java b/source/de/anomic/data/messageBoard.java index a4dabbc16..949b95cc8 100644 --- a/source/de/anomic/data/messageBoard.java +++ b/source/de/anomic/data/messageBoard.java @@ -45,7 +45,6 @@ import java.io.IOException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; -import java.util.GregorianCalendar; import java.util.HashMap; import java.util.Iterator; import java.util.Map; @@ -62,9 +61,12 @@ public class messageBoard { private static final String dateFormat = "yyyyMMddHHmmss"; private static final int recordSize = 512; - private static TimeZone GMTTimeZone = TimeZone.getTimeZone("PST"); private static SimpleDateFormat SimpleFormatter = new SimpleDateFormat(dateFormat); + static { + SimpleFormatter.setTimeZone(TimeZone.getTimeZone("GMT")); + } + private kelondroMapObjects database = null; private int sn = 0; @@ -85,7 +87,9 @@ public class messageBoard { } private static String dateString() { - return SimpleFormatter.format(new GregorianCalendar(GMTTimeZone).getTime()); + synchronized (SimpleFormatter) { + return SimpleFormatter.format(new Date()); + } } private String snString() { @@ -143,7 +147,9 @@ public class messageBoard { try { String c = key.substring(categoryLength); c = c.substring(0, c.length() - 2); - return SimpleFormatter.parse(c); + synchronized (SimpleFormatter) { + return SimpleFormatter.parse(c); + } } catch (ParseException e) { return new Date(); } diff --git a/source/de/anomic/data/wikiBoard.java b/source/de/anomic/data/wikiBoard.java index b87357676..1e3e54dd3 100644 --- a/source/de/anomic/data/wikiBoard.java +++ b/source/de/anomic/data/wikiBoard.java @@ -45,7 +45,6 @@ import java.io.IOException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; -import java.util.GregorianCalendar; import java.util.HashMap; import java.util.Iterator; import java.util.Map; @@ -62,9 +61,12 @@ public class wikiBoard { private static final String dateFormat = "yyyyMMddHHmmss"; private static final int recordSize = 512; - private static TimeZone GMTTimeZone = TimeZone.getTimeZone("PST"); private static SimpleDateFormat SimpleFormatter = new SimpleDateFormat(dateFormat); + static { + SimpleFormatter.setTimeZone(TimeZone.getTimeZone("GMT")); + } + private kelondroMapObjects datbase = null; private kelondroMapObjects bkpbase = null; private static HashMap authors = new HashMap(); @@ -94,11 +96,13 @@ public class wikiBoard { } private static String dateString() { - return dateString(new GregorianCalendar(GMTTimeZone).getTime()); + return dateString(new Date()); } public static String dateString(Date date) { - return SimpleFormatter.format(date); + synchronized (SimpleFormatter) { + return SimpleFormatter.format(date); + } } private static String normalize(String key) { @@ -169,7 +173,9 @@ public class wikiBoard { System.out.println("DEBUG - ERROR: date field missing in wikiBoard"); return new Date(); } - return SimpleFormatter.parse(c); + synchronized (SimpleFormatter) { + return SimpleFormatter.parse(c); + } } catch (ParseException e) { return new Date(); } @@ -207,7 +213,9 @@ public class wikiBoard { try { String c = (String) record.get("date"); if (c == null) return null; - return SimpleFormatter.parse(c); + synchronized (SimpleFormatter) { + return SimpleFormatter.parse(c); + } } catch (ParseException e) { return null; } diff --git a/source/de/anomic/http/httpHeader.java b/source/de/anomic/http/httpHeader.java index 1c84412fc..1112b900e 100644 --- a/source/de/anomic/http/httpHeader.java +++ b/source/de/anomic/http/httpHeader.java @@ -389,7 +389,7 @@ public final class httpHeader extends TreeMap implements Map { if (containsKey(kind)) { Date parsedDate = serverDate.parseHTTPDate((String) get(kind)); if (parsedDate == null) parsedDate = new Date(); - return new Date(parsedDate.getTime()); + return parsedDate; } return null; } @@ -442,7 +442,7 @@ public final class httpHeader extends TreeMap implements Map { if (containsKey(httpHeader.IF_RANGE)) { Date rangeDate = serverDate.parseHTTPDate((String) get(httpHeader.IF_RANGE)); if (rangeDate != null) - return new Date(rangeDate.getTime()); + return rangeDate; return get(httpHeader.IF_RANGE); } diff --git a/source/de/anomic/http/httpc.java b/source/de/anomic/http/httpc.java index 45fb9d17b..ec349c763 100644 --- a/source/de/anomic/http/httpc.java +++ b/source/de/anomic/http/httpc.java @@ -62,7 +62,6 @@ import java.util.Arrays; import java.util.Comparator; import java.util.Date; import java.util.Enumeration; -import java.util.GregorianCalendar; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; @@ -109,7 +108,6 @@ public final class httpc { // final statics private static final String vDATE = "20040602"; private static final int terminalMaxLength = 30000; - private static final TimeZone GMTTimeZone = TimeZone.getTimeZone("GMT"); private static final SimpleDateFormat HTTPGMTFormatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US); private static final HashMap reverseMappingCache = new HashMap(); private static final HashSet activeConnections = new HashSet(); // all connections are stored here and deleted when they are finished @@ -130,7 +128,7 @@ public final class httpc { static { // set the time zone - HTTPGMTFormatter.setTimeZone(GMTTimeZone); // The GMT standard date format used in the HTTP protocol + HTTPGMTFormatter.setTimeZone(TimeZone.getTimeZone("GMT")); // The GMT standard date format used in the HTTP protocol // set time-out of InetAddress.getByName cache ttl java.security.Security.setProperty("networkaddress.cache.ttl" , "60"); @@ -343,7 +341,7 @@ public final class httpc { * @return Date-object with the current time. */ public static Date nowDate() { - return new GregorianCalendar(GMTTimeZone).getTime(); + return new Date(); } public int hashCode() { diff --git a/source/de/anomic/kelondro/kelondroCollectionIndex.java b/source/de/anomic/kelondro/kelondroCollectionIndex.java index 6010c2559..01099734b 100644 --- a/source/de/anomic/kelondro/kelondroCollectionIndex.java +++ b/source/de/anomic/kelondro/kelondroCollectionIndex.java @@ -33,8 +33,7 @@ import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.ArrayList; -import java.util.Calendar; -import java.util.GregorianCalendar; +import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -818,10 +817,9 @@ public class kelondroCollectionIndex { // finally dump the removed entries to a file newcommon.sort(); - TimeZone GMTTimeZone = TimeZone.getTimeZone("GMT"); - Calendar gregorian = new GregorianCalendar(GMTTimeZone); SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss"); - String filename = serverCodings.encodeHex(kelondroBase64Order.enhancedCoder.decode(new String(key))) + "_" + formatter.format(gregorian.getTime()) + ".collection"; + formatter.setTimeZone(TimeZone.getTimeZone("GMT")); + String filename = serverCodings.encodeHex(kelondroBase64Order.enhancedCoder.decode(new String(key))) + "_" + formatter.format(new Date()) + ".collection"; File storagePath = new File(commonsPath, filename.substring(0, 2)); // make a subpath storagePath.mkdirs(); File file = new File(storagePath, filename); diff --git a/source/de/anomic/net/ftpc.java b/source/de/anomic/net/ftpc.java index 7d150ff83..ec228e472 100644 --- a/source/de/anomic/net/ftpc.java +++ b/source/de/anomic/net/ftpc.java @@ -64,12 +64,10 @@ import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; import java.text.DateFormat; -import java.util.Calendar; import java.util.Date; import java.util.Enumeration; import java.util.Properties; import java.util.StringTokenizer; -import java.util.TimeZone; import java.util.Vector; import de.anomic.server.serverDomains; @@ -85,9 +83,6 @@ public class ftpc { private PrintStream err; private boolean glob = true; // glob = false -> filenames are taken literally for mget, .. - // for time measurement - private static final TimeZone GMTTimeZone = TimeZone.getTimeZone("PST"); // the GMT Time Zone - // transfer type private static final char transferType = 'i'; // transfer binary @@ -1673,7 +1668,7 @@ cd .. private void get(String fileDest, String fileName) throws IOException { // store time for statistics - long start = Calendar.getInstance(GMTTimeZone).getTime().getTime(); + long start = System.currentTimeMillis(); // prepare data channel if (DataSocketPassiveMode) createPassiveDataPort(); else createActiveDataPort(); @@ -1729,7 +1724,7 @@ cd .. //if (!success) throw new IOException(reply); // write statistics - long stop = Calendar.getInstance(GMTTimeZone).getTime().getTime(); + long stop = System.currentTimeMillis(); out.print("---- downloaded " + ((length < 2048) ? length + " bytes" : ((int) length / 1024) + " kbytes") + " in " +