1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.procore.cluster;
15 import java.util.TreeMap;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.exception.ValidationException;
19 import org.simantics.db.impl.ClusterI.ObjectProcedure;
20 import org.simantics.db.impl.ClusterI.Procedure;
21 import org.simantics.db.impl.ClusterSupport;
22 import org.simantics.db.impl.Modifier;
23 import org.simantics.db.impl.Table;
24 import org.simantics.db.impl.TableFactory;
25 import org.simantics.db.impl.TableSizeListener;
27 public final class ValueTable extends Table<byte[]> {
28 public ValueTable(TableSizeListener sizeListener, int[] header, int headerBase) {
29 super(TableFactory.getByteFactory(), sizeListener, header, headerBase);
31 public ValueTable(TableSizeListener sizeListener, int[] header, int headerBase, byte[] bytes) {
32 super(TableFactory.getByteFactory(), sizeListener, header, headerBase, bytes);
34 void getValue(int valueIndex, byte[] to, int start, int size) {
35 getCopy(valueIndex, to, start, size);
37 void getString(int valueIndex, char[] to, int start, int size) {
38 byte[] bs = (byte[])table;
39 start += valueIndex+offset+1;
40 for(int i=0;i<size;i++) to[i] = (char)bs[start++];
42 // getCopy(valueIndex, to, start, size);
44 void setValue(int valueIndex, byte[] value, int length) {
45 setCopy(valueIndex, length, value, 0);
47 int createValue(byte[] value, int voffset, int vsize) {
48 int valueIndex = createNewElement(vsize);
49 setCopy(valueIndex, vsize, value, voffset);
52 int createValue(byte[] value) {
53 int valueIndex = createNewElement(value.length);
54 setCopy(valueIndex, value.length, value, 0);
57 void removeValue(int valueIndex, int size) {
58 deleteOldElement(valueIndex, size);
60 boolean isEqual(int valueIndex, byte[] value) {
61 return isEqual(valueIndex, value, 0, value.length);
63 private TreeMap<Integer, Integer> valueMap =
64 new TreeMap<Integer, Integer>();
66 private int VALUE_SIZE = 0;
67 private int VALUE_OFFSET = 0;
68 public void checkValueInit()
69 throws DatabaseException {
71 final int s = getTableSize();
72 final int c = getTableCapacity();
74 throw new ValidationException("Illegal value table size=" + s + " cap=" + c);
76 VALUE_OFFSET = getTableBase() - ValueTable.ZERO_SHIFT;
78 public void checkValue(int capacity, int index)
79 throws DatabaseException {
80 if (0 == capacity && 0 == index)
83 throw new ValidationException("Illegal resource value capacity=" + capacity);
85 throw new ValidationException("Illegal resource value index=" + index);
86 if (VALUE_SIZE < capacity + index + VALUE_OFFSET)
87 throw new ValidationException("Illegal resource value c=" + capacity +
88 " i=" + index + " ts=" + VALUE_SIZE + " off=" + VALUE_OFFSET);
89 // Duplicate index is allowed because new index is created only if new size is greater than old.
90 Integer valueCap = valueMap.get(index);
92 valueMap.put(index, capacity);
93 else if (capacity > valueCap)
94 valueMap.put(index, capacity);
96 valueMap.put(index, valueCap);
98 public void checkValueFini()
99 throws DatabaseException {
101 for (Map.Entry<Integer, Integer> e : valueMap.entrySet()) {
103 int c = e.getValue();
104 int cur = VALUE_OFFSET + i;
106 throw new ValidationException("Index error with resource value c=" + c +
107 " i=" + i + " ts=" + VALUE_SIZE + " off=" + VALUE_OFFSET);
113 public <Context> boolean foreach(int setIndex, Procedure procedure, Context context, ClusterSupport support, Modifier modifier) throws DatabaseException {
114 throw new UnsupportedOperationException();