View Javadoc

1   package org.apache.labs.bananadb.store.sequence;
2   
3   import java.util.Map;
4   import java.util.HashMap;
5   import java.util.NoSuchElementException;
6   import java.util.concurrent.atomic.AtomicLong;
7   import java.io.IOException;
8   
9   /**
10   * identities based on system time
11   *
12   * @author kalle
13   * @since 2009-mar-23 01:41:16
14   */
15  public class UnsafeSequenceManager extends SequenceManager {
16  
17    private Map<String, Sequence> sequences = new HashMap<String, Sequence>();
18  
19    public void close() throws IOException {
20      for (Sequence sequence : sequences.values()) {
21        sequence.close();
22      }
23    }
24  
25    public SequenceManager.Sequence.ReservedSequenceRange reserve(String name, int requestedSize) throws IOException {
26      return sequences.get(name).reserve(requestedSize);
27    }
28  
29    @SuppressWarnings("unchecked")
30    public <T> Sequence<T> getOrRegisterSequence(Class<T> valueType, String name) {
31      Sequence<T> sequence = sequences.get(name);
32      if (sequence == null) {
33        sequences.put(name, sequence = sequenceFactory(valueType, name));
34      }
35      return sequence;
36    }
37  
38    @SuppressWarnings("unchecked")
39    public <T> Sequence<T> sequenceFactory(Class<T> valueType, String name) {
40      if (valueType == Long.class) {
41        return (Sequence<T>) new LongSequence(name);
42      }
43      throw new UnsupportedOperationException("Unregistred value type class " + valueType.getName());
44    }
45  
46  
47    public class LongSequence extends Sequence<Long> {
48  
49      public void close() throws IOException {
50        
51      }
52  
53      public LongSequence(String name) {
54        super(name);
55      }
56  
57      public Class<Long> getValueType() {
58        return Long.class;
59      }
60  
61      public synchronized ReservedSequenceRange<Long> reserve(int requestedSize) {
62  
63        long start = System.currentTimeMillis();
64  
65        try {
66          Thread.sleep(requestedSize + 5);
67        } catch (InterruptedException ie) {
68          throw new RuntimeException(ie);
69        }
70  
71        return new LongRange(start, start + requestedSize);
72      }
73  
74      protected void release(ReservedSequenceRange<Long> reservation) {
75        // ignored
76      }
77  
78      public class LongRange extends ReservedSequenceRange<Long> {
79  
80        private long start;
81        private long end;
82        private long size;
83        private AtomicLong current;
84  
85        protected LongRange(long start, long end) {
86          this.start = start;
87          this.end = end;
88          size = end - start;
89          current = new AtomicLong(start);
90        }
91  
92        public boolean hasNextValue() {
93          return current.get() < end;
94        }
95  
96        public Long nextValue() {
97          if (!hasNextValue()) {
98            throw new NoSuchElementException();
99          }
100         return current.incrementAndGet();
101       }
102 
103       public int size() {
104         return (int) size;
105       }
106 
107       @Override
108       public String toString() {
109         return "LongRange{" +
110             "start=" + start +
111             ", end=" + end +
112             ", size=" + size +
113             ", current=" + current +
114             '}';
115       }
116     }
117 
118   }
119 }
120 
121 
122 
123