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