01: /*
02: * Copyright Aduna (http://www.aduna-software.com/) (c) 2008.
03: *
04: * Licensed under the Aduna BSD-style license.
05: */
06: package org.openrdf.sail.rdbms.schema;
07:
08: import java.sql.SQLException;
09: import java.sql.Types;
10: import java.util.concurrent.ConcurrentHashMap;
11: import java.util.concurrent.ConcurrentMap;
12: import java.util.concurrent.atomic.AtomicInteger;
13:
14: import org.openrdf.model.Value;
15:
16: /**
17: *
18: * @author James Leigh
19: */
20: public class IntegerIdSequence extends IdSequence {
21:
22: private int SPAN = 268435455;
23:
24: private int SHIFT = Long.toBinaryString(SPAN).length();
25:
26: private Number[] minIds;
27:
28: private ConcurrentMap<ValueType, AtomicInteger> seq = new ConcurrentHashMap<ValueType, AtomicInteger>();
29:
30: public int getShift() {
31: return SHIFT;
32: }
33:
34: public int getJdbcIdType() {
35: return Types.INTEGER;
36: }
37:
38: public String getSqlType() {
39: return "INTEGER";
40: }
41:
42: public void init() throws SQLException {
43: minIds = new Number[ValueType.values().length];
44: for (int i = 0; i < minIds.length; i++) {
45: minIds[i] = i * (SPAN + 1);
46: }
47: if (getHashTable() != null) {
48: for (Number max : getHashTable().maxIds(getShift(),
49: getMod())) {
50: ValueType code = valueOf(max);
51: if (max.intValue() > minId(code).intValue()) {
52: seq.put(code, new AtomicInteger(max.intValue()));
53: }
54: }
55: }
56: }
57:
58: public Number idOf(Number number) {
59: return number.intValue();
60: }
61:
62: public Number maxId(ValueType type) {
63: return minId(type).intValue() + SPAN;
64: }
65:
66: public Number minId(ValueType type) {
67: return minIds[type.index()];
68: }
69:
70: public Number nextId(Value value) {
71: ValueType code = valueOf(value);
72: if (!seq.containsKey(code)) {
73: seq.putIfAbsent(code, new AtomicInteger(minId(code)
74: .intValue()));
75: }
76: return seq.get(code).incrementAndGet();
77: }
78:
79: @Override
80: protected int shift(Number id) {
81: return id.intValue() >>> SHIFT;
82: }
83: }
|