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.AtomicLong;
13:
14: import org.openrdf.model.Value;
15:
16: /**
17: *
18: * @author James Leigh
19: */
20: public class LongIdSequence extends IdSequence {
21:
22: private long SPAN = 1152921504606846975l;
23:
24: private int SHIFT = Long.toBinaryString(SPAN).length();
25:
26: private Number[] minIds;
27:
28: private ConcurrentMap<ValueType, AtomicLong> seq = new ConcurrentHashMap<ValueType, AtomicLong>();
29:
30: public int getShift() {
31: return SHIFT;
32: }
33:
34: public int getJdbcIdType() {
35: return Types.BIGINT;
36: }
37:
38: public String getSqlType() {
39: return "BIGINT";
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.longValue() > minId(code).longValue()) {
52: seq.put(code, new AtomicLong(max.longValue()));
53: }
54: }
55: }
56: }
57:
58: public Number idOf(Number number) {
59: return number.longValue();
60: }
61:
62: public Number maxId(ValueType type) {
63: return minId(type).longValue() + 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 AtomicLong(minId(code)
74: .longValue()));
75: }
76: return seq.get(code).incrementAndGet();
77: }
78:
79: @Override
80: protected int shift(Number id) {
81: return (int) (id.longValue() >>> SHIFT);
82: }
83: }
|