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.Procedure;
20 import org.simantics.db.impl.ClusterSupport;
21 import org.simantics.db.impl.Modifier;
22 import org.simantics.db.impl.Table;
23 import org.simantics.db.impl.TableFactory;
24 import org.simantics.db.impl.TableSizeListener;
26 public final class ValueTable extends Table<byte[]> {
27 public ValueTable(TableSizeListener sizeListener, int[] header, int headerBase) {
28 super(TableFactory.getByteFactory(), sizeListener, header, headerBase);
30 public ValueTable(TableSizeListener sizeListener, int[] header, int headerBase, byte[] bytes) {
31 super(TableFactory.getByteFactory(), sizeListener, header, headerBase, bytes);
33 void getValue(int valueIndex, byte[] to, int start, int size) {
34 getCopy(valueIndex, to, start, size);
36 void getString(int valueIndex, char[] to, int start, int size) {
37 byte[] bs = (byte[])table;
38 start += valueIndex+offset+1;
39 for(int i=0;i<size;i++) to[i] = (char)bs[start++];
41 // getCopy(valueIndex, to, start, size);
43 void setValue(int valueIndex, byte[] value, int length) {
44 setCopy(valueIndex, length, value, 0);
46 int createValue(byte[] value, int voffset, int vsize) {
47 int valueIndex = createNewElement(vsize);
48 setCopy(valueIndex, vsize, value, voffset);
51 int createValue(byte[] value) {
52 int valueIndex = createNewElement(value.length);
53 setCopy(valueIndex, value.length, value, 0);
56 void removeValue(int valueIndex, int size) {
57 deleteOldElement(valueIndex, size);
59 boolean isEqual(int valueIndex, byte[] value) {
60 return isEqual(valueIndex, value, 0, value.length);
62 private TreeMap<Integer, Integer> valueMap =
63 new TreeMap<Integer, Integer>();
65 private int VALUE_SIZE = 0;
66 private int VALUE_OFFSET = 0;
67 public void checkValueInit()
68 throws DatabaseException {
70 final int s = getTableSize();
71 final int c = getTableCapacity();
73 throw new ValidationException("Illegal value table size=" + s + " cap=" + c);
75 VALUE_OFFSET = getTableBase() - ValueTable.ZERO_SHIFT;
77 public void checkValue(int capacity, int index)
78 throws DatabaseException {
79 if (0 == capacity && 0 == index)
82 throw new ValidationException("Illegal resource value capacity=" + capacity);
84 throw new ValidationException("Illegal resource value index=" + index);
85 if (VALUE_SIZE < capacity + index + VALUE_OFFSET)
86 throw new ValidationException("Illegal resource value c=" + capacity +
87 " i=" + index + " ts=" + VALUE_SIZE + " off=" + VALUE_OFFSET);
88 // Duplicate index is allowed because new index is created only if new size is greater than old.
89 Integer valueCap = valueMap.get(index);
91 valueMap.put(index, capacity);
92 else if (capacity > valueCap)
93 valueMap.put(index, capacity);
95 valueMap.put(index, valueCap);
97 public void checkValueFini()
98 throws DatabaseException {
100 for (Map.Entry<Integer, Integer> e : valueMap.entrySet()) {
102 int c = e.getValue();
103 int cur = VALUE_OFFSET + i;
105 throw new ValidationException("Index error with resource value c=" + c +
106 " i=" + i + " ts=" + VALUE_SIZE + " off=" + VALUE_OFFSET);
112 public <Context> boolean foreach(int setIndex, Procedure procedure, Context context, ClusterSupport support, Modifier modifier) throws DatabaseException {
113 throw new UnsupportedOperationException();