X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.databoard%2Fsrc%2Forg%2Fsimantics%2Fdataboard%2Faccessor%2Fbinary%2FBinaryArray.java;h=6fd7d75039eafaf249695f53adb850d9854538ab;hb=refs%2Fchanges%2F78%2F2778%2F1;hp=73b1786df04014a6ceb345e4b76bbbe83b65be2b;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/binary/BinaryArray.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/binary/BinaryArray.java index 73b1786df..6fd7d7503 100644 --- a/bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/binary/BinaryArray.java +++ b/bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/binary/BinaryArray.java @@ -1,54 +1,54 @@ -/******************************************************************************* - * Copyright (c) 2010 Association for Decentralized Information Management in - * Industry THTH ry. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ +/******************************************************************************* + * Copyright (c) 2010 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ package org.simantics.databoard.accessor.binary; -import java.io.IOException; -import java.lang.ref.WeakReference; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; -import java.util.Map.Entry; -import java.util.SortedMap; -import java.util.TreeMap; - -import org.simantics.databoard.accessor.Accessor; -import org.simantics.databoard.accessor.ArrayAccessor; -import org.simantics.databoard.accessor.StreamAccessor; -import org.simantics.databoard.accessor.error.AccessorConstructionException; -import org.simantics.databoard.accessor.error.AccessorException; -import org.simantics.databoard.accessor.error.ReferenceException; -import org.simantics.databoard.accessor.event.ArrayElementAdded; -import org.simantics.databoard.accessor.event.ArrayElementRemoved; -import org.simantics.databoard.accessor.event.Event; -import org.simantics.databoard.accessor.event.ValueAssigned; -import org.simantics.databoard.accessor.file.FileArrayAccessor; -import org.simantics.databoard.accessor.impl.AccessorParams; -import org.simantics.databoard.accessor.impl.ListenerEntry; -import org.simantics.databoard.accessor.interestset.ArrayInterestSet; -import org.simantics.databoard.accessor.interestset.InterestSet; -import org.simantics.databoard.accessor.reference.ChildReference; -import org.simantics.databoard.accessor.reference.IndexReference; -import org.simantics.databoard.accessor.reference.LabelReference; -import org.simantics.databoard.adapter.AdaptException; -import org.simantics.databoard.binding.ArrayBinding; -import org.simantics.databoard.binding.Binding; -import org.simantics.databoard.binding.error.BindingException; -import org.simantics.databoard.binding.mutable.MutableVariant; -import org.simantics.databoard.serialization.Serializer; -import org.simantics.databoard.serialization.SerializerConstructionException; -import org.simantics.databoard.type.ArrayType; -import org.simantics.databoard.type.Datatype; -import org.simantics.databoard.util.binary.Blob; -import org.simantics.databoard.util.binary.RandomAccessBinary.ByteSide; +import java.io.IOException; +import java.lang.ref.WeakReference; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; +import java.util.SortedMap; +import java.util.TreeMap; + +import org.simantics.databoard.accessor.Accessor; +import org.simantics.databoard.accessor.ArrayAccessor; +import org.simantics.databoard.accessor.StreamAccessor; +import org.simantics.databoard.accessor.error.AccessorConstructionException; +import org.simantics.databoard.accessor.error.AccessorException; +import org.simantics.databoard.accessor.error.ReferenceException; +import org.simantics.databoard.accessor.event.ArrayElementAdded; +import org.simantics.databoard.accessor.event.ArrayElementRemoved; +import org.simantics.databoard.accessor.event.Event; +import org.simantics.databoard.accessor.event.ValueAssigned; +import org.simantics.databoard.accessor.file.FileArrayAccessor; +import org.simantics.databoard.accessor.impl.AccessorParams; +import org.simantics.databoard.accessor.impl.ListenerEntry; +import org.simantics.databoard.accessor.interestset.ArrayInterestSet; +import org.simantics.databoard.accessor.interestset.InterestSet; +import org.simantics.databoard.accessor.reference.ChildReference; +import org.simantics.databoard.accessor.reference.IndexReference; +import org.simantics.databoard.accessor.reference.LabelReference; +import org.simantics.databoard.adapter.AdaptException; +import org.simantics.databoard.binding.ArrayBinding; +import org.simantics.databoard.binding.Binding; +import org.simantics.databoard.binding.error.BindingException; +import org.simantics.databoard.binding.mutable.MutableVariant; +import org.simantics.databoard.serialization.Serializer; +import org.simantics.databoard.serialization.SerializerConstructionException; +import org.simantics.databoard.type.ArrayType; +import org.simantics.databoard.type.Datatype; +import org.simantics.databoard.util.binary.Blob; +import org.simantics.databoard.util.binary.RandomAccessBinary.ByteSide; /** * Binary Array is accessor to a byte backed array of elements. @@ -150,10 +150,10 @@ public class BinaryArray extends BinaryObject implements ArrayAccessor, FileArra @Override public void setNoflush(int index, Binding rcb, Object rcv) throws AccessorException { - assert b.isOpen(); + assert b.isOpen(); writeLock(); try { - + // Write Serializer rcs = params.serializerScheme.getSerializer( rcb ); long pos = getStartPosition(index); @@ -169,9 +169,9 @@ public class BinaryArray extends BinaryObject implements ArrayAccessor, FileArra b.position(pos); rcs.serialize(b, null, rcv); - // Update Blobs + // Update Blobs /* - BinaryObject sa = getExistingAccessor(index); + BinaryObject sa = getExistingAccessor(index); if (sa!=null && newSize != oldSize) { sa.b.setPositionInSource(pos, newSize); }*/ @@ -207,10 +207,10 @@ public class BinaryArray extends BinaryObject implements ArrayAccessor, FileArra throw new AccessorException(e); } catch (AdaptException e) { throw new AccessorException(e); - } catch (SerializerConstructionException e) { - throw new AccessorException(e); - } finally { - writeUnlock(); + } catch (SerializerConstructionException e) { + throw new AccessorException(e); + } finally { + writeUnlock(); } } @@ -223,8 +223,8 @@ public class BinaryArray extends BinaryObject implements ArrayAccessor, FileArra */ @Override public void setValueNoflush(Binding arrayBinding, Object newArray) - throws AccessorException { - assert b.isOpen(); + throws AccessorException { + assert b.isOpen(); writeLock(); try { // Write @@ -246,7 +246,7 @@ public class BinaryArray extends BinaryObject implements ArrayAccessor, FileArra //long endPos = b.position(); //long len = endPos - startPos; - // update location + // update location /* BinaryObject sa = getExistingAccessor(index); if (sa!=null) { @@ -302,17 +302,17 @@ public class BinaryArray extends BinaryObject implements ArrayAccessor, FileArra throw new AccessorException(e); } catch (AdaptException e) { throw new AccessorException(e); - } catch (SerializerConstructionException e) { - throw new AccessorException(e); - } finally { - writeUnlock(); + } catch (SerializerConstructionException e) { + throw new AccessorException(e); + } finally { + writeUnlock(); } } @Override - public void addNoflush(int index, Binding rcb, Object rcv) throws AccessorException { - assert b.isOpen(); + public void addNoflush(int index, Binding rcb, Object rcv) throws AccessorException { + assert b.isOpen(); writeLock(); try { Serializer rcs = params.serializerScheme.getSerializer( rcb ); @@ -322,7 +322,7 @@ public class BinaryArray extends BinaryObject implements ArrayAccessor, FileArra boolean lastEntry = index == oldCount; if (index>oldCount) throw new AccessorException("Index out of range"); int newCount = oldCount +1; - b.position(0L); + b.position(0L); b.writeInt(newCount); long pos = getStartPosition(index); @@ -396,10 +396,10 @@ public class BinaryArray extends BinaryObject implements ArrayAccessor, FileArra throw new AccessorException(e); } catch (AdaptException e) { throw new AccessorException(e); - } catch (SerializerConstructionException e) { - throw new AccessorException(e); - } finally { - writeUnlock(); + } catch (SerializerConstructionException e) { + throw new AccessorException(e); + } finally { + writeUnlock(); } } @@ -418,8 +418,8 @@ public class BinaryArray extends BinaryObject implements ArrayAccessor, FileArra @Override public void addAllNoflush(int index, Binding rcb, Object[] rcvs) throws AccessorException { - if (index<0||index>size()) throw new AccessorException("Index out of bounds"); - assert b.isOpen(); + if (index<0||index>size()) throw new AccessorException("Index out of bounds"); + assert b.isOpen(); writeLock(); try { Serializer rcs = params.serializerScheme.getSerializer( rcb ); @@ -428,7 +428,7 @@ public class BinaryArray extends BinaryObject implements ArrayAccessor, FileArra int oldCount = b.readInt(); int newCount = oldCount + rcvs.length; if (index>oldCount) throw new AccessorException("Index out of range"); - b.position(0L); + b.position(0L); b.writeInt(newCount); boolean lastEntry = index == oldCount; @@ -503,7 +503,7 @@ public class BinaryArray extends BinaryObject implements ArrayAccessor, FileArra if (cis != null) { Accessor sa = getAccessor(i); } - } + } */ } @@ -515,99 +515,99 @@ public class BinaryArray extends BinaryObject implements ArrayAccessor, FileArra throw new AccessorException(e); } catch (AdaptException e) { throw new AccessorException(e); - } catch (SerializerConstructionException e) { - throw new AccessorException(e); - } finally { - writeUnlock(); + } catch (SerializerConstructionException e) { + throw new AccessorException(e); + } finally { + writeUnlock(); + } + } + + void addRepeatNoflush(int index, Binding rcb, Object obj, int repeatCount) throws AccessorException { + if (index<0||index>size()) throw new AccessorException("Index out of bounds"); + assert b.isOpen(); + writeLock(); + try { + Serializer rcs = params.serializerScheme.getSerializer( rcb ); + // Write + b.position(0L); + int oldCount = b.readInt(); + int newCount = oldCount + repeatCount; + if (index>oldCount) throw new AccessorException("Index out of range"); + b.position(0L); + b.writeInt(newCount); + boolean lastEntry = index == oldCount; + + int size = rcs.getSize(obj) * repeatCount; + long pos = getStartPosition(index); + b.position(pos); + b.insertBytes(size, ByteSide.Right); + + b.position(pos); + for (int i=0; i= index) { + java.lang.ref.Reference value = children.remove(key); + if (value.get()!=null) children.put(key+repeatCount, value); + key = children.lowerKey(key); + } + } + + // Notify Listeners + ListenerEntry le = listeners; + while (le!=null) { + ArrayInterestSet is = le.getInterestSet(); + if (is.inNotifications()) { + for (int i=0; i oldCis = is.componentInterests; + boolean needUpdates = false; + for (Integer i : oldCis.keySet()) { + needUpdates |= i>=index; + if (needUpdates) break; + } + + if (needUpdates) { + Map newCis = new HashMap(oldCis.size()); + for (Integer i : oldCis.keySet()) + { + Integer oldKey = i; + Integer newKey = i>=index ? i+repeatCount : i; + InterestSet oldValue = oldCis.get(oldKey); + newCis.put(newKey, oldValue); + } + is.componentInterests = newCis; + } + + } + + le = le.next; + } + + } catch (IOException e) { + throw new AccessorException(e); + } catch (SerializerConstructionException e) { + throw new AccessorException(e); + } finally { + writeUnlock(); } } - - void addRepeatNoflush(int index, Binding rcb, Object obj, int repeatCount) throws AccessorException { - if (index<0||index>size()) throw new AccessorException("Index out of bounds"); - assert b.isOpen(); - writeLock(); - try { - Serializer rcs = params.serializerScheme.getSerializer( rcb ); - // Write - b.position(0L); - int oldCount = b.readInt(); - int newCount = oldCount + repeatCount; - if (index>oldCount) throw new AccessorException("Index out of range"); - b.position(0L); - b.writeInt(newCount); - boolean lastEntry = index == oldCount; - - int size = rcs.getSize(obj) * repeatCount; - long pos = getStartPosition(index); - b.position(pos); - b.insertBytes(size, ByteSide.Right); - - b.position(pos); - for (int i=0; i= index) { - java.lang.ref.Reference value = children.remove(key); - if (value.get()!=null) children.put(key+repeatCount, value); - key = children.lowerKey(key); - } - } - - // Notify Listeners - ListenerEntry le = listeners; - while (le!=null) { - ArrayInterestSet is = le.getInterestSet(); - if (is.inNotifications()) { - for (int i=0; i oldCis = is.componentInterests; - boolean needUpdates = false; - for (Integer i : oldCis.keySet()) { - needUpdates |= i>=index; - if (needUpdates) break; - } - - if (needUpdates) { - Map newCis = new HashMap(oldCis.size()); - for (Integer i : oldCis.keySet()) - { - Integer oldKey = i; - Integer newKey = i>=index ? i+repeatCount : i; - InterestSet oldValue = oldCis.get(oldKey); - newCis.put(newKey, oldValue); - } - is.componentInterests = newCis; - } - - } - - le = le.next; - } - - } catch (IOException e) { - throw new AccessorException(e); - } catch (SerializerConstructionException e) { - throw new AccessorException(e); - } finally { - writeUnlock(); - } - } @Override - public void removeNoflush(int index, int count) throws AccessorException { - assert b.isOpen(); + public void removeNoflush(int index, int count) throws AccessorException { + assert b.isOpen(); writeLock(); try { // Write @@ -682,53 +682,53 @@ public class BinaryArray extends BinaryObject implements ArrayAccessor, FileArra } catch (IOException e) { throw new AccessorException( e ); - } finally { - writeUnlock(); + } finally { + writeUnlock(); } } @Override - public Object get(int index, Binding valueBinding) throws AccessorException { - assert b.isOpen(); + public Object get(int index, Binding valueBinding) throws AccessorException { + assert b.isOpen(); readLock(); try { long pos = getStartPosition(index); - b.position(pos); + b.position(pos); Serializer s = params.serializerScheme.getSerializer(valueBinding); return s.deserialize(b); } catch (IOException e) { throw new AccessorException(e); - } catch (SerializerConstructionException e) { - throw new AccessorException(e); - } finally { - readUnlock(); + } catch (SerializerConstructionException e) { + throw new AccessorException(e); + } finally { + readUnlock(); + } + } + + @Override + public void get(int index, Binding valueBinding, Object dst) throws AccessorException { + assert b.isOpen(); + readLock(); + try { + long pos = getStartPosition(index); + b.position(pos); + Serializer s = params.serializerScheme.getSerializer(valueBinding); + s.deserializeTo(b, dst); + } catch (IOException e) { + throw new AccessorException(e); + } catch (SerializerConstructionException e) { + throw new AccessorException(e); + } finally { + readUnlock(); } } - - @Override - public void get(int index, Binding valueBinding, Object dst) throws AccessorException { - assert b.isOpen(); - readLock(); - try { - long pos = getStartPosition(index); - b.position(pos); - Serializer s = params.serializerScheme.getSerializer(valueBinding); - s.deserializeTo(b, dst); - } catch (IOException e) { - throw new AccessorException(e); - } catch (SerializerConstructionException e) { - throw new AccessorException(e); - } finally { - readUnlock(); - } - } - + @SuppressWarnings("unchecked") @Override public T getAccessor(int index) - throws AccessorConstructionException { - assert b.isOpen(); + throws AccessorConstructionException { + assert b.isOpen(); readLock(); try { b.position(0L); @@ -783,8 +783,8 @@ public class BinaryArray extends BinaryObject implements ArrayAccessor, FileArra throw new AccessorConstructionException(e); } catch (AccessorException e) { throw new AccessorConstructionException(e); - } finally { - readUnlock(); + } finally { + readUnlock(); } } @@ -793,17 +793,17 @@ public class BinaryArray extends BinaryObject implements ArrayAccessor, FileArra public T getComponent(ChildReference reference) throws AccessorConstructionException { if (reference==null) return (T) this; - if (reference instanceof LabelReference) { - LabelReference lr = (LabelReference) reference; - try { - Integer index = new Integer( lr.label ); - Accessor result = getAccessor(index); - if (reference.getChildReference() != null) - result = result.getComponent(reference.getChildReference()); - return (T) result; - } catch ( NumberFormatException nfe ) { - throw new ReferenceException(nfe); - } + if (reference instanceof LabelReference) { + LabelReference lr = (LabelReference) reference; + try { + Integer index = new Integer( lr.label ); + Accessor result = getAccessor(index); + if (reference.getChildReference() != null) + result = result.getComponent(reference.getChildReference()); + return (T) result; + } catch ( NumberFormatException nfe ) { + throw new ReferenceException(nfe); + } } else if (reference instanceof IndexReference) { IndexReference ref = (IndexReference) reference; int index = ref.getIndex(); @@ -816,110 +816,110 @@ public class BinaryArray extends BinaryObject implements ArrayAccessor, FileArra @Override public void getAll(Binding valueBinding, Object[] array) - throws AccessorException { - assert b.isOpen(); + throws AccessorException { + assert b.isOpen(); readLock(); try { b.position(0L); int size = b.readInt(); - if (size > array.length) throw new AccessorException("Argument array too short"); + if (size > array.length) throw new AccessorException("Argument array too short"); Serializer s = params.serializerScheme.getSerializer(valueBinding); for (int i=0; i values) - throws AccessorException { - assert b.isOpen(); + throws AccessorException { + assert b.isOpen(); readLock(); try { b.position(0L); - int size = b.readInt(); + int size = b.readInt(); Serializer s = params.serializerScheme.getSerializer(valueBinding); for (int i=0; ioldSize) { - Object dummy = cb.createDefault(); - int count = newSize-oldSize; - addRepeatNoflush(oldSize, cb, dummy, count); - } - - } catch (BindingException e) { - throw new AccessorException( e ); - } finally { - writeUnlock(); - } + @Override + public void setSizeNoflush(int newSize) throws AccessorException { + assert b.isOpen(); + writeLock(); + try { + int oldSize = size(); + + // Remove instances + if (newSizeoldSize) { + Object dummy = cb.createDefault(); + int count = newSize-oldSize; + addRepeatNoflush(oldSize, cb, dummy, count); + } + + } catch (BindingException e) { + throw new AccessorException( e ); + } finally { + writeUnlock(); + } + } + + @Override + public void setSize(int newSize) throws AccessorException { + assert b.isOpen(); + writeLock(); + try { + setSizeNoflush(newSize); + b.flush(); + } catch (IOException e) { + throw new AccessorException( e ); + } finally { + writeUnlock(); + } } - - @Override - public void setSize(int newSize) throws AccessorException { - assert b.isOpen(); - writeLock(); - try { - setSizeNoflush(newSize); - b.flush(); - } catch (IOException e) { - throw new AccessorException( e ); - } finally { - writeUnlock(); - } - } @Override - public int size() throws AccessorException { - assert b.isOpen(); + public int size() throws AccessorException { + assert b.isOpen(); readLock(); try { b.position(0L); - int size = b.readInt(); - if (size<0) throw new AccessorException("Malformed file"); + int size = b.readInt(); + if (size<0) throw new AccessorException("Malformed file"); return size; } catch (IOException e) { throw new AccessorException( e ); - } finally { - readUnlock(); + } finally { + readUnlock(); } } @Override Event applyLocal(Event e, boolean makeRollback) throws AccessorException { - Event rollback = null; - if (e instanceof ValueAssigned) { - ValueAssigned va = (ValueAssigned) e; - if (makeRollback) rollback = new ValueAssigned(cb, getValue(cb)); - setValueNoflush(va.newValue.getBinding(), va.newValue.getValue()); + Event rollback = null; + if (e instanceof ValueAssigned) { + ValueAssigned va = (ValueAssigned) e; + if (makeRollback) rollback = new ValueAssigned(cb, getValue(cb)); + setValueNoflush(va.newValue.getBinding(), va.newValue.getValue()); } else if (e instanceof ArrayElementAdded) { ArrayElementAdded aa = (ArrayElementAdded) e; @@ -943,90 +943,90 @@ public class BinaryArray extends BinaryObject implements ArrayAccessor, FileArra @Override - public void add(Binding binding, Object value) throws AccessorException { - assert b.isOpen(); - writeLock(); + public void add(Binding binding, Object value) throws AccessorException { + assert b.isOpen(); + writeLock(); try { addNoflush(binding, value); b.flush(); } catch (IOException e) { throw new AccessorException( e ); - } finally { - writeUnlock(); + } finally { + writeUnlock(); } } @Override public void add(int index, Binding binding, Object value) - throws AccessorException { - assert b.isOpen(); - writeLock(); + throws AccessorException { + assert b.isOpen(); + writeLock(); try { addNoflush(index, binding, value); b.flush(); } catch (IOException e) { throw new AccessorException( e ); - } finally { - writeUnlock(); + } finally { + writeUnlock(); } } @Override public void addAll(Binding binding, Object[] values) - throws AccessorException { - assert b.isOpen(); + throws AccessorException { + assert b.isOpen(); writeLock(); try { addAllNoflush(binding, values); b.flush(); } catch (IOException e) { throw new AccessorException( e ); - } finally { - writeUnlock(); + } finally { + writeUnlock(); } } @Override public void addAll(int index, Binding binding, Object[] values) - throws AccessorException { - assert b.isOpen(); + throws AccessorException { + assert b.isOpen(); writeLock(); try { addAllNoflush(index, binding, values); b.flush(); } catch (IOException e) { throw new AccessorException( e ); - } finally { - writeUnlock(); + } finally { + writeUnlock(); } } @Override - public void remove(int index, int count) throws AccessorException { - assert b.isOpen(); + public void remove(int index, int count) throws AccessorException { + assert b.isOpen(); writeLock(); try { removeNoflush(index, count); b.flush(); } catch (IOException e) { throw new AccessorException( e ); - } finally { - writeUnlock(); + } finally { + writeUnlock(); } } @Override public void set(int index, Binding binding, Object value) - throws AccessorException { - assert b.isOpen(); + throws AccessorException { + assert b.isOpen(); writeLock(); try { setNoflush(index, binding, value); b.flush(); } catch (IOException e) { throw new AccessorException(e); - } finally { - writeUnlock(); + } finally { + writeUnlock(); } }