| 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 | |
| 19 | package hu.netmind.beankeeper.serial.impl; |
| 20 | |
| 21 | import hu.netmind.beankeeper.serial.SerialTracker; |
| 22 | import hu.netmind.beankeeper.serial.Serial; |
| 23 | import hu.netmind.beankeeper.common.StoreException; |
| 24 | import hu.netmind.beankeeper.node.NodeManager; |
| 25 | import java.util.Date; |
| 26 | import java.util.Map; |
| 27 | |
| 28 | /** |
| 29 | * This implementation offers serials based on dates. |
| 30 | * @author Brautigam Robert |
| 31 | * @version Revision: $Revision$ |
| 32 | */ |
| 33 | public 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 | |