]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.procore/src/org/simantics/db/procore/cluster/ValueTable.java
5ddcca89305e1d1f72f800e12e3fc12995a886ad
[simantics/platform.git] / bundles / org.simantics.db.procore / src / org / simantics / db / procore / cluster / ValueTable.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.procore.cluster;
13
14 import java.util.Map;
15 import java.util.TreeMap;
16
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;
26
27 public final class ValueTable extends Table<byte[]> {
28     public ValueTable(TableSizeListener sizeListener, int[] header, int headerBase) {
29         super(TableFactory.getByteFactory(), sizeListener, header, headerBase);
30     }
31     public ValueTable(TableSizeListener sizeListener, int[] header, int headerBase, byte[] bytes) {
32         super(TableFactory.getByteFactory(), sizeListener, header, headerBase, bytes);
33     }
34     void getValue(int valueIndex, byte[] to, int start, int size) {
35         getCopy(valueIndex, to, start, size);
36     }
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++];
41 //      
42 //        getCopy(valueIndex, to, start, size);
43     }
44     void setValue(int valueIndex, byte[] value, int length) {
45         setCopy(valueIndex, length, value, 0);
46     }
47     int createValue(byte[] value, int voffset, int vsize) {
48         int valueIndex = createNewElement(vsize);
49         setCopy(valueIndex, vsize, value, voffset);
50         return valueIndex;
51     }
52     int createValue(byte[] value) {
53         int valueIndex = createNewElement(value.length);
54         setCopy(valueIndex, value.length, value, 0);
55         return valueIndex;
56     }
57     void removeValue(int valueIndex, int size) {
58         deleteOldElement(valueIndex, size);
59     }
60     boolean isEqual(int valueIndex, byte[] value) {
61         return isEqual(valueIndex, value, 0, value.length);
62     }
63     private TreeMap<Integer, Integer> valueMap =
64         new TreeMap<Integer, Integer>();
65     
66     private int VALUE_SIZE = 0;
67     private int VALUE_OFFSET = 0;
68     public void checkValueInit()
69     throws DatabaseException {
70         valueMap.clear();
71         final int s = getTableSize();
72         final int c = getTableCapacity();
73         if (s < 0 || s > c)
74             throw new ValidationException("Illegal value table size=" + s + " cap=" + c);
75         VALUE_SIZE = s;
76         VALUE_OFFSET = getTableBase() - ValueTable.ZERO_SHIFT; 
77     }
78     public void checkValue(int capacity, int index)
79     throws DatabaseException {
80         if (0 == capacity && 0 == index)
81             return;
82         if (capacity < 1)
83             throw new ValidationException("Illegal resource value capacity=" + capacity);
84         if (index < 1)
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);
91         if (null == valueCap)
92             valueMap.put(index, capacity);
93         else if (capacity > valueCap)
94             valueMap.put(index, capacity);
95         else
96             valueMap.put(index, valueCap);
97     }
98     public void checkValueFini()
99     throws DatabaseException {
100         int last = 0;
101         for (Map.Entry<Integer, Integer> e : valueMap.entrySet()) {
102             int i = e.getKey();
103             int c = e.getValue();
104             int cur = VALUE_OFFSET + i;
105             if (last > cur)
106                 throw new ValidationException("Index error with resource value c=" + c +
107                         " i=" + i + " ts=" + VALUE_SIZE + " off=" + VALUE_OFFSET);
108             last = cur + c;
109         }
110     }
111
112     @Override
113     public <Context> boolean foreach(int setIndex, Procedure procedure, Context context, ClusterSupport support, Modifier modifier) throws DatabaseException {
114         throw new UnsupportedOperationException();
115     }
116
117 }