EMMA Coverage Report (generated Sun May 02 20:42:29 CEST 2010)
[all classes][hu.netmind.beankeeper.serial.impl]

COVERAGE SUMMARY FOR SOURCE FILE [SerialTrackerImpl.java]

nameclass, %method, %block, %line, %
SerialTrackerImpl.java100% (1/1)80%  (4/5)64%  (88/137)68%  (19/28)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class SerialTrackerImpl100% (1/1)80%  (4/5)64%  (88/137)68%  (19/28)
adjustOffset (Long): void 0%   (0/1)0%   (0/34)0%   (0/6)
getNextSerial (): Long 100% (1/1)83%  (71/86)80%  (12/15)
SerialTrackerImpl (): void 100% (1/1)100% (15/15)100% (5/5)
init (Map): void 100% (1/1)100% (1/1)100% (1/1)
release (): void 100% (1/1)100% (1/1)100% (1/1)

1/**
2 * Copyright (C) 2006 NetMind Consulting Bt.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 3 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 */
18 
19package hu.netmind.beankeeper.serial.impl;
20 
21import hu.netmind.beankeeper.serial.SerialTracker;
22import hu.netmind.beankeeper.serial.Serial;
23import hu.netmind.beankeeper.common.StoreException;
24import hu.netmind.beankeeper.node.NodeManager;
25import java.util.Date;
26import java.util.Map;
27 
28/**
29 * This implementation offers serials based on dates.
30 * @author Brautigam Robert
31 * @version Revision: $Revision$
32 */
33public class SerialTrackerImpl implements SerialTracker
34{
35   private long offset = 0;
36   private long lastSerial = 0;
37   private int subSerial = 0;
38 
39   private NodeManager nodeManager = null; // Inject
40 
41   public void init(Map parameters)
42   {
43   }
44 
45   public void release()
46   {
47   }
48 
49   /**
50    * Set the offset for serial numbers. To determine the offset,
51    * a valid serial must be given to this method, and the difference
52    * between it and the current serial will be added to the current offset,
53    * if it's positive.
54    */
55   // TODO: adjust offset across nodes, or require time synchronization
56   private void adjustOffset(Long serial)
57   {
58      long serialCooked = 10000*(serial.longValue() / 10000 + 1) ; // Chop off sub-serial
59      Long currentSerial = getNextSerial();
60      long currentCooked = 10000*(currentSerial.longValue() / 10000 + 1); // Chop of sub-serial
61      if ( serialCooked > currentCooked )
62      {
63         // Current serial should be greated or equal to the supplied
64         // serial. If it's not, then it has to be corrected
65         offset += (serialCooked-currentCooked);
66      }
67   }
68 
69   /**
70    * Get the next serial for database functions.
71    */
72   public Long getNextSerial()
73   {
74      // Execute on server
75      if ( nodeManager.getRole() == NodeManager.NodeRole.CLIENT )
76      {
77         return (Long) nodeManager.callServer(SerialTracker.class.getName(),
78               "getNextSerial",new Class[] {}, new Object[] {});
79      }
80      // Server side
81      synchronized ( this )
82      {
83         // Get the serial from the current date. This is supposed to
84         // be millisecond precision.
85         long result = Serial.getSerial(new Date()).getValue();
86         result+=offset;
87         // Implement sub-millisecond precision here
88         if ( lastSerial == result )
89         {
90            // This means we were executed in the same millisecond
91            // as last time. Add sub-serial number, and increase if possible.
92            if ( subSerial >= 9999 )
93               throw new StoreException("Serial number exhausted. "+
94                     "More than 10.000 operations in the same millisecond. Wow. "+
95                     "Is this the distant future? Do you sit in front of a quantum computer? ...Hm... Does it run Linux?");
96            subSerial++;
97         } else {
98            // If lastSerial is greater, then something is very wrong
99            if ( lastSerial > result )
100               throw new StoreException("Next serial is in the past, something is wrong.");
101            // This is more likely. We are at least one milisecond further
102            // in time, so zero the subSerial number.
103            subSerial=0;
104         }
105         // Return
106         lastSerial=result;
107         return new Long(result+subSerial);
108      }
109   }
110 
111}
112 
113 

[all classes][hu.netmind.beankeeper.serial.impl]
EMMA 2.0.5312debian (C) Vladimir Roubtsov