X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=bundles%2Forg.simantics.databoard%2Fsrc%2Forg%2Fsimantics%2Fdataboard%2Faccessor%2Fbinary%2FBinaryRecord.java;h=329b4c1674ce64ee8b26edcf5a770b66579593db;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hp=c7d6218b65b96f9b3d7ae9acba69dff06530ce92;hpb=24e2b34260f219f0d1644ca7a138894980e25b14;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/binary/BinaryRecord.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/binary/BinaryRecord.java index c7d6218b6..329b4c167 100644 --- a/bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/binary/BinaryRecord.java +++ b/bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/binary/BinaryRecord.java @@ -1,51 +1,51 @@ -/******************************************************************************* - * 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.SoftReference; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.Executor; - -import org.simantics.databoard.Bindings; -import org.simantics.databoard.accessor.Accessor; -import org.simantics.databoard.accessor.RecordAccessor; -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.Event; -import org.simantics.databoard.accessor.event.ValueAssigned; -import org.simantics.databoard.accessor.file.FileRecordAccessor; -import org.simantics.databoard.accessor.impl.AccessorParams; -import org.simantics.databoard.accessor.impl.ListenerEntry; -import org.simantics.databoard.accessor.interestset.InterestSet; -import org.simantics.databoard.accessor.interestset.RecordInterestSet; -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.accessor.reference.NameReference; -import org.simantics.databoard.adapter.AdaptException; -import org.simantics.databoard.binding.Binding; -import org.simantics.databoard.binding.RecordBinding; -import org.simantics.databoard.binding.error.BindingConstructionException; -import org.simantics.databoard.binding.error.BindingException; -import org.simantics.databoard.binding.mutable.MutableVariant; -import org.simantics.databoard.serialization.RuntimeSerializerConstructionException; -import org.simantics.databoard.serialization.Serializer; -import org.simantics.databoard.serialization.SerializerConstructionException; -import org.simantics.databoard.type.RecordType; -import org.simantics.databoard.util.binary.Blob; -import org.simantics.databoard.util.binary.RandomAccessBinary.ByteSide; +import java.io.IOException; +import java.lang.ref.SoftReference; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.Executor; + +import org.simantics.databoard.Bindings; +import org.simantics.databoard.accessor.Accessor; +import org.simantics.databoard.accessor.RecordAccessor; +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.Event; +import org.simantics.databoard.accessor.event.ValueAssigned; +import org.simantics.databoard.accessor.file.FileRecordAccessor; +import org.simantics.databoard.accessor.impl.AccessorParams; +import org.simantics.databoard.accessor.impl.ListenerEntry; +import org.simantics.databoard.accessor.interestset.InterestSet; +import org.simantics.databoard.accessor.interestset.RecordInterestSet; +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.accessor.reference.NameReference; +import org.simantics.databoard.adapter.AdaptException; +import org.simantics.databoard.binding.Binding; +import org.simantics.databoard.binding.RecordBinding; +import org.simantics.databoard.binding.error.BindingConstructionException; +import org.simantics.databoard.binding.error.BindingException; +import org.simantics.databoard.binding.mutable.MutableVariant; +import org.simantics.databoard.serialization.RuntimeSerializerConstructionException; +import org.simantics.databoard.serialization.Serializer; +import org.simantics.databoard.serialization.SerializerConstructionException; +import org.simantics.databoard.type.RecordType; +import org.simantics.databoard.util.binary.Blob; +import org.simantics.databoard.util.binary.RandomAccessBinary.ByteSide; /** * Accessor to a Binary Record. @@ -71,10 +71,10 @@ public class BinaryRecord extends BinaryObject implements RecordAccessor, FileRe super(parent, blob, type, params); if (type.isReferable()) throw new AccessorConstructionException("Refereable record are not supported"); - try { - binding = (RecordBinding) params.bindingScheme.getBinding(type); - } catch (BindingConstructionException e) { - throw new AccessorConstructionException(e); + try { + binding = (RecordBinding) params.bindingScheme.getBinding(type); + } catch (BindingConstructionException e) { + throw new AccessorConstructionException(e); } int count = type.getComponentCount(); children = new java.lang.ref.Reference[count]; @@ -97,8 +97,8 @@ public class BinaryRecord extends BinaryObject implements RecordAccessor, FileRe * @return * @throws AccessorException */ - long getStartPosition(int fieldIndex) throws AccessorException { - assert b.isOpen(); + long getStartPosition(int fieldIndex) throws AccessorException { + assert b.isOpen(); readLock(); try { // sa, saIndex = getFloorExistingAccessor @@ -124,10 +124,10 @@ public class BinaryRecord extends BinaryObject implements RecordAccessor, FileRe return b.position(); } catch (IOException ioe) { throw new AccessorException( ioe ); - } catch (SerializerConstructionException e) { - throw new AccessorException( e ); - } finally { - readUnlock(); + } catch (SerializerConstructionException e) { + throw new AccessorException( e ); + } finally { + readUnlock(); } } @@ -145,7 +145,7 @@ public class BinaryRecord extends BinaryObject implements RecordAccessor, FileRe public T getFieldAccessor(int index) throws AccessorConstructionException { if (index<0 || index>=count()) throw new ReferenceException("Field index ("+index+") out of bounds ("+count()+")"); - assert b.isOpen(); + assert b.isOpen(); readLock(); try { // Get existing or create new @@ -186,10 +186,10 @@ public class BinaryRecord extends BinaryObject implements RecordAccessor, FileRe throw new AccessorConstructionException(e); } catch (AccessorException e) { throw new AccessorConstructionException(e); - } catch (SerializerConstructionException e) { - throw new AccessorConstructionException(e); - } finally { - readUnlock(); + } catch (SerializerConstructionException e) { + throw new AccessorConstructionException(e); + } finally { + readUnlock(); } } @@ -207,19 +207,19 @@ public class BinaryRecord extends BinaryObject implements RecordAccessor, FileRe BinaryObject accessor = (ref!=null)?(BinaryObject)ref.get():null; return accessor; } - - @Override - public Object getFieldValue(String fieldName, Binding fieldBinding) - throws AccessorException { - int fieldIndex = type().getComponentIndex(fieldName); - if (fieldIndex<0) throw new AccessorException("Field "+fieldName+" does not exist"); - return getFieldValue(fieldIndex, fieldBinding); - } + + @Override + public Object getFieldValue(String fieldName, Binding fieldBinding) + throws AccessorException { + int fieldIndex = type().getComponentIndex(fieldName); + if (fieldIndex<0) throw new AccessorException("Field "+fieldName+" does not exist"); + return getFieldValue(fieldIndex, fieldBinding); + } @Override public Object getFieldValue(int index, Binding fieldBinding) - throws AccessorException { - assert b.isOpen(); + throws AccessorException { + assert b.isOpen(); readLock(); try { b.position( getStartPosition(index) ); @@ -229,59 +229,59 @@ public class BinaryRecord extends BinaryObject implements RecordAccessor, FileRe throw new AccessorException(e); } catch (RuntimeSerializerConstructionException 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 setFieldValue(String fieldName, Binding fieldBinding, + Object value) throws AccessorException { + assert b.isOpen(); + writeLock(); + try { + setFieldValueNoflush(fieldName, fieldBinding, value); + flush(); + } finally { + writeUnlock(); + } + } + + @Override + public void setFieldValueNoflush(String fieldName, Binding fieldBinding, + Object value) throws AccessorException { + assert b.isOpen(); + writeLock(); + try { + int fieldIndex = type().getComponentIndex(fieldName); + if (fieldIndex<0) throw new AccessorException("Field "+fieldName+" does not exist"); + setFieldValue(fieldIndex, fieldBinding, value); + } finally { + writeUnlock(); } - } - - @Override - public void setFieldValue(String fieldName, Binding fieldBinding, - Object value) throws AccessorException { - assert b.isOpen(); - writeLock(); - try { - setFieldValueNoflush(fieldName, fieldBinding, value); - flush(); - } finally { - writeUnlock(); - } - } - - @Override - public void setFieldValueNoflush(String fieldName, Binding fieldBinding, - Object value) throws AccessorException { - assert b.isOpen(); - writeLock(); - try { - int fieldIndex = type().getComponentIndex(fieldName); - if (fieldIndex<0) throw new AccessorException("Field "+fieldName+" does not exist"); - setFieldValue(fieldIndex, fieldBinding, value); - } finally { - writeUnlock(); - } } @Override public void setFieldValue(int index, Binding fieldBinding, - Object value) throws AccessorException { - assert b.isOpen(); - writeLock(); + Object value) throws AccessorException { + assert b.isOpen(); + writeLock(); try { setFieldValueNoflush(index, fieldBinding, value); - flush(); - } finally { - writeUnlock(); + flush(); + } finally { + writeUnlock(); } } @Override public void setFieldValueNoflush(int index, Binding cb, Object cv) throws AccessorException { - assert b.isOpen(); + assert b.isOpen(); writeLock(); - try { + try { BinaryObject sa = getExistingAccessor(index); if (sa!=null) { @@ -322,8 +322,8 @@ public class BinaryRecord extends BinaryObject implements RecordAccessor, FileRe RecordInterestSet is = le.getInterestSet(); if (is.inNotificationsOf(index)) { MutableVariant newValue = is.inValuesOf(index) ? new MutableVariant(cb, cv) : null; - if (is.inValuesOf(index)) newValue = new MutableVariant(cb, cb.isImmutable() ? cv : cb.clone(cv)); - + if (is.inValuesOf(index)) newValue = new MutableVariant(cb, cb.isImmutable() ? cv : cb.clone(cv)); + Event e = new ValueAssigned(new IndexReference(index), newValue); emitEvent(le, e); } @@ -333,10 +333,10 @@ public class BinaryRecord extends BinaryObject implements RecordAccessor, FileRe 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(); } } @@ -380,42 +380,42 @@ public class BinaryRecord extends BinaryObject implements RecordAccessor, FileRe @Override public T getComponent(ChildReference reference) throws AccessorConstructionException { if (reference==null) return (T) this; - - if (reference instanceof LabelReference) { - LabelReference lr = (LabelReference) reference; - String fieldName = lr.label; - Integer index = type().getComponentIndex(fieldName); - if (index==null) throw new ReferenceException("RecordType doesn't have field by name \""+fieldName+"\""); - BinaryObject sa = getFieldAccessor(index); - if (reference.getChildReference() != null) sa = sa.getComponent(reference.getChildReference()); - return (T) sa; - } + + if (reference instanceof LabelReference) { + LabelReference lr = (LabelReference) reference; + String fieldName = lr.label; + Integer index = type().getComponentIndex(fieldName); + if (index==null) throw new ReferenceException("RecordType doesn't have field by name \""+fieldName+"\""); + BinaryObject sa = getFieldAccessor(index); + if (reference.getChildReference() != null) sa = sa.getComponent(reference.getChildReference()); + return (T) sa; + } if (reference instanceof IndexReference) { IndexReference ref = (IndexReference) reference; int index = ref.getIndex(); - BinaryObject sa = getFieldAccessor(index); - if (reference.getChildReference() != null) sa = sa.getComponent(reference.getChildReference()); - return (T) sa; - } - + BinaryObject sa = getFieldAccessor(index); + if (reference.getChildReference() != null) sa = sa.getComponent(reference.getChildReference()); + return (T) sa; + } + if (reference instanceof NameReference) { NameReference ref = (NameReference) reference; String fieldName = ref.getName(); Integer index = type().getComponentIndex(fieldName); if (index==null) throw new ReferenceException("RecordType doesn't have field by name \""+fieldName+"\""); - BinaryObject sa = getFieldAccessor(index); - if (reference.getChildReference() != null) sa = sa.getComponent(reference.getChildReference()); - return (T) sa; - } - + BinaryObject sa = getFieldAccessor(index); + if (reference.getChildReference() != null) sa = sa.getComponent(reference.getChildReference()); + return (T) sa; + } + throw new ReferenceException(reference.getClass()+" is not a subreference of RecordType"); } @Override - public void setValueNoflush(Binding binding, Object newValue) throws AccessorException { - assert b.isOpen(); + public void setValueNoflush(Binding binding, Object newValue) throws AccessorException { + assert b.isOpen(); writeLock(); try { RecordBinding rb = (RecordBinding) binding; @@ -450,47 +450,47 @@ public class BinaryRecord extends BinaryObject implements RecordAccessor, FileRe le = le.next; } } - } - - // Notify record specific listeners - /* - if (listeners!=null) { - ListenerEntry le = listeners; - while (le!=null) { - RecordInterestSet is = le.getInterestSet(); - if (is.inNotifications() && !is.inComponentNotifications()) { - Event e = new ValueAssigned( binding, is.inValues() ? newValue : null); - le.emitEvent(e); - } - le = le.next; - } - }*/ + } + + // Notify record specific listeners + /* + if (listeners!=null) { + ListenerEntry le = listeners; + while (le!=null) { + RecordInterestSet is = le.getInterestSet(); + if (is.inNotifications() && !is.inComponentNotifications()) { + Event e = new ValueAssigned( binding, is.inValues() ? newValue : null); + le.emitEvent(e); + } + le = le.next; + } + }*/ } catch (BindingException e) { throw new AccessorException(e); } catch (IOException e) { throw new AccessorException(e); - } catch (SerializerConstructionException e) { - throw new AccessorException(e); - } finally { - writeUnlock(); + } catch (SerializerConstructionException e) { + throw new AccessorException(e); + } finally { + writeUnlock(); } } @Override Event applyLocal(Event e, boolean makeRollback) throws AccessorException { - if (e instanceof ValueAssigned) { - ValueAssigned va = (ValueAssigned) e; - Event rollback = null; - if (makeRollback) { - Binding binding = Bindings.getMutableBinding(type()); - rollback = new ValueAssigned(binding, getValue(binding)); - } - setValueNoflush(va.newValue.getBinding(), va.newValue.getValue()); - return rollback; - } else { - throw new AccessorException("Cannot apply "+e.getClass().getName()+" to Record Type"); + if (e instanceof ValueAssigned) { + ValueAssigned va = (ValueAssigned) e; + Event rollback = null; + if (makeRollback) { + Binding binding = Bindings.getMutableBinding(type()); + rollback = new ValueAssigned(binding, getValue(binding)); + } + setValueNoflush(va.newValue.getBinding(), va.newValue.getValue()); + return rollback; + } else { + throw new AccessorException("Cannot apply "+e.getClass().getName()+" to Record Type"); } }