-/*******************************************************************************\r
- * Copyright (c) 2010 Association for Decentralized Information Management in\r
- * Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.databoard.accessor.binary;\r
-\r
-import java.io.IOException;\r
-import java.lang.ref.WeakReference;\r
-import java.util.Collection;\r
-import java.util.HashMap;\r
-import java.util.Map;\r
-import java.util.Map.Entry;\r
-import java.util.SortedMap;\r
-import java.util.TreeMap;\r
-\r
-import org.simantics.databoard.Bindings;\r
-import org.simantics.databoard.accessor.Accessor;\r
-import org.simantics.databoard.accessor.ArrayAccessor;\r
-import org.simantics.databoard.accessor.CloseableAccessor;\r
-import org.simantics.databoard.accessor.StreamAccessor;\r
-import org.simantics.databoard.accessor.error.AccessorConstructionException;\r
-import org.simantics.databoard.accessor.error.AccessorException;\r
-import org.simantics.databoard.accessor.error.ReferenceException;\r
-import org.simantics.databoard.accessor.event.ArrayElementAdded;\r
-import org.simantics.databoard.accessor.event.ArrayElementRemoved;\r
-import org.simantics.databoard.accessor.event.Event;\r
-import org.simantics.databoard.accessor.event.ValueAssigned;\r
-import org.simantics.databoard.accessor.file.FileArrayAccessor;\r
-import org.simantics.databoard.accessor.impl.AccessorParams;\r
-import org.simantics.databoard.accessor.impl.ListenerEntry;\r
-import org.simantics.databoard.accessor.interestset.ArrayInterestSet;\r
-import org.simantics.databoard.accessor.interestset.InterestSet;\r
-import org.simantics.databoard.accessor.reference.ChildReference;\r
-import org.simantics.databoard.accessor.reference.IndexReference;\r
-import org.simantics.databoard.accessor.reference.LabelReference;\r
-import org.simantics.databoard.adapter.AdaptException;\r
-import org.simantics.databoard.binding.ArrayBinding;\r
-import org.simantics.databoard.binding.Binding;\r
-import org.simantics.databoard.binding.error.BindingException;\r
-import org.simantics.databoard.binding.mutable.MutableVariant;\r
-import org.simantics.databoard.serialization.Serializer;\r
-import org.simantics.databoard.serialization.SerializerConstructionException;\r
-import org.simantics.databoard.type.ArrayType;\r
-import org.simantics.databoard.type.Datatype;\r
-import org.simantics.databoard.type.LongType;\r
-import org.simantics.databoard.util.binary.Blob;\r
-import org.simantics.databoard.util.binary.RandomAccessBinary.ByteSide;\r
-\r
-/**\r
- * Binary Array is accessor to a byte backed array of elements.\r
- * <p>\r
- * \r
- * Note, To increase the random access performance of the record, create sub-accessors of\r
- * its fields. \r
- *\r
- * @author Toni Kalajainen <toni.kalajainen@vtt.fi>\r
- */\r
-public class BinaryVariableWidthStreamArray extends BinaryObject implements ArrayAccessor, FileArrayAccessor, ArrayAccessor.CloseableArrayAccessor, StreamAccessor {\r
-\r
- /** Accessors to children */\r
- TreeMap<Integer, java.lang.ref.Reference<BinaryObject>> children = new TreeMap<Integer, java.lang.ref.Reference<BinaryObject>>(); \r
-\r
- Binding cb;\r
- Serializer cs;\r
- Integer constantSize;\r
- ArrayAccessor index;\r
- \r
- public BinaryVariableWidthStreamArray(BinaryObject parent, Blob blob, Datatype type, AccessorParams params, ArrayAccessor index) \r
- throws AccessorConstructionException\r
- {\r
- super(parent, blob, type, params);\r
- ArrayType at = (ArrayType) type;\r
- cb = params.bindingScheme.getBindingUnchecked(at.componentType);\r
- cs = params.serializerScheme.getSerializerUnchecked( cb );\r
- constantSize = cs.getConstantSize();\r
- if (index==null || index.type().componentType instanceof LongType == false) {\r
- throw new AccessorConstructionException("Index must be Long[]");\r
- }\r
- this.index = index;\r
- }\r
- \r
- public ArrayType type() {\r
- return (ArrayType) type;\r
- }\r
- \r
- /**\r
- * Get existing sub accessor\r
- * @param index\r
- * @return sub-accessor or <code>null</code>\r
- */\r
- BinaryObject getExistingAccessor(int index)\r
- { \r
- java.lang.ref.Reference<BinaryObject> ref = children.get(index);\r
- if (ref==null) return null;\r
- BinaryObject res = (BinaryObject) ref.get();\r
-// if (res==null) children.remove(index);\r
- return res;\r
- } \r
- \r
- /**\r
- * Get start position of a field\r
- * \r
- * @param fieldIndex\r
- * @return\r
- * @throws AccessorException\r
- */\r
- long getStartPosition(int fieldIndex) throws AccessorException {\r
- int c = this.index.size();\r
- if ( fieldIndex==c )\r
- try {\r
- return b.length();\r
- } catch (IOException e) {\r
- throw new AccessorException( e );\r
- }\r
- if ( fieldIndex>c || fieldIndex<0 ) throw new AccessorException("Index out of bounds");\r
- return (Long) index.get(fieldIndex, Bindings.LONG);\r
- } \r
- \r
- long getLength(int index, long pos) throws AccessorException {\r
- int c = this.index.size();\r
- if ( index>=c || index<0 ) return 0;\r
- if ( index==c-1 ) \r
- {\r
- try {\r
- return b.length() - (Long) this.index.get(c-1, Bindings.LONG);\r
- } catch (IOException e) {\r
- throw new AccessorException( e );\r
- }\r
- } else {\r
- return (Long) this.index.get(index+1, Bindings.LONG) - (Long) this.index.get(index, Bindings.LONG);\r
- }\r
- }\r
-\r
- @Override\r
- public void setNoflush(int index, Binding rcb, Object rcv)\r
- throws AccessorException {\r
- assert b.isOpen();\r
- \r
- writeLock();\r
- try {\r
-\r
- int count = size();\r
- if ( index==count ) {\r
- addNoflush(count, rcb, rcv);\r
- return;\r
- } \r
- \r
- // Write \r
- Serializer rcs = params.serializerScheme.getSerializer( rcb );\r
- long pos = getStartPosition(index);\r
- long oldSize = getLength(index, pos);\r
- long newSize = rcs.getSize(rcv, null);\r
- b.position(pos);\r
- long diff = newSize - oldSize; \r
- if (diff>0) {\r
- b.insertBytes( newSize - oldSize, ByteSide.Right );\r
- } else if (diff<0) { \r
- b.removeBytes( oldSize - newSize, ByteSide.Right );\r
- }\r
- b.position(pos);\r
- rcs.serialize(b, null, rcv);\r
-\r
- // Update index of the higher indices\r
- if ( diff!= 0 ) {\r
- for (int i=index+1; i<count; i++) {\r
- long p = (Long) this.index.get(i, Bindings.LONG);\r
- p += diff;\r
- this.index.set(i, Bindings.LONG, p);\r
- }\r
- }\r
- \r
- // Update Blobs\r
- /*\r
- BinaryObject sa = getExistingAccessor(index);\r
- if (sa!=null && newSize != oldSize) {\r
- sa.b.setPositionInSource(pos, newSize);\r
- }*/\r
- \r
- // Shift consecutive blobs \r
-/* if (!children.isEmpty()) {\r
- Integer lastKey = children.lastKey();\r
- if (lastKey!=null && index+1 <= lastKey && diff!=0) {\r
- SortedMap<Integer, Reference<BinaryObject>> pm = children.subMap(index+1, true, lastKey, true);\r
- for (Entry<Integer, Reference<BinaryObject>> e : pm.entrySet()) {\r
- BinaryObject sa_ = e.getValue().get();\r
- if (sa_ == null) continue;\r
- sa_.b.setPositionInSource( sa_.b.getStartPositionInSourceBinary() + diff, sa_.b.length());\r
- }\r
- }\r
- }*/\r
- \r
- // Notify\r
- ListenerEntry le = listeners;\r
- while (le!=null) { \r
- ArrayInterestSet is = le.getInterestSet();\r
- if (is.inNotificationsOf(index)) {\r
- MutableVariant newValue = null;\r
- if (is.inValues()) newValue = new MutableVariant(rcb, rcb.isImmutable() ? rcv : rcb.clone(rcv)); \r
- \r
- Event e = new ValueAssigned(new IndexReference(index), newValue);\r
- emitEvent(le, e);\r
- }\r
- le = le.next;\r
- }\r
- \r
- } catch (IOException e) {\r
- throw new AccessorException(e);\r
- } catch (AdaptException e) {\r
- throw new AccessorException(e);\r
- } catch (SerializerConstructionException e) {\r
- throw new AccessorException(e);\r
- } finally {\r
- writeUnlock();\r
- }\r
- \r
- }\r
- \r
- /**\r
- * Set all values\r
- * \r
- * @param arrayBinding\r
- * @param newArray\r
- */\r
- @Override\r
- public void setValueNoflush(Binding arrayBinding, Object newArray)\r
- throws AccessorException {\r
- assert b.isOpen();\r
- writeLock();\r
- try {\r
- // Write \r
- ArrayBinding rb = ((ArrayBinding)arrayBinding);\r
- Binding rcb = rb.getComponentBinding();\r
- Serializer rcs = params.serializerScheme.getSerializer( rcb );\r
- int oldCount = index.size();\r
- int newCount = rb.size(newArray); \r
- b.setLength( params.serializerScheme.getSerializer( rb ).getSize(newArray) );\r
- index.setSize(newCount);\r
- \r
- // Serialize\r
- for (int index=0; index<newCount; index++) {\r
- //long startPos = b.position();\r
- Object obj = rb.get(newArray, index);\r
- this.index.set(index, Bindings.LONG, b.position());\r
- rcs.serialize(b, obj);\r
- //long endPos = b.position();\r
- //long len = endPos - startPos;\r
- \r
- // update location\r
- /*\r
- BinaryObject sa = getExistingAccessor(index);\r
- if (sa!=null) {\r
- sa.b.setPositionInSource(startPos, len);\r
- }*/\r
- }\r
- \r
- // Notify removal\r
- for (int index=oldCount-1; index>=newCount; index--) {\r
- BinaryObject sa = getExistingAccessor(index);\r
- if (sa!=null) {\r
- sa.invalidatedNotification();\r
- children.remove(index);\r
- sa = null;\r
- }\r
- \r
- // Notify changes\r
- ListenerEntry le = listeners;\r
- while (le!=null) { \r
- ArrayInterestSet is = le.getInterestSet();\r
- if (is.inNotificationsOf(index)) {\r
- Event e = new ArrayElementRemoved(index);\r
- emitEvent(le, e);\r
- }\r
- le = le.next;\r
- }\r
- }\r
- \r
- // Notify new assignment\r
- if (listeners!=null) {\r
- for (int index=0; index<newCount; index++) {\r
- Object cv = rb.get(newArray, index);\r
- \r
- // Notify changes\r
- ListenerEntry le = listeners;\r
- while (le!=null) { \r
- ArrayInterestSet is = le.getInterestSet();\r
- if (is.inNotificationsOf(index)) { \r
- MutableVariant vv = null;\r
- if (is.inValues()) vv = new MutableVariant(rcb, cb.isImmutable() ? cv : rcb.clone(cv)); \r
- \r
- Event e = index<oldCount ? new ValueAssigned(new IndexReference(index), vv) : new ArrayElementAdded(index, vv);\r
- emitEvent(le, e);\r
- }\r
- le = le.next;\r
- } \r
- }\r
- }\r
- \r
- } catch (BindingException e) {\r
- throw new AccessorException(e);\r
- } catch (IOException e) {\r
- throw new AccessorException(e);\r
- } catch (AdaptException e) {\r
- throw new AccessorException(e);\r
- } catch (SerializerConstructionException e) {\r
- throw new AccessorException(e);\r
- } finally {\r
- writeUnlock(); \r
- }\r
- \r
- }\r
-\r
- @Override\r
- public void addNoflush(int index, Binding rcb, Object rcv) throws AccessorException {\r
- assert b.isOpen();\r
- writeLock();\r
- try {\r
- Serializer rcs = params.serializerScheme.getSerializer( rcb ); \r
- // Write \r
- int oldCount = this.index.size();\r
- int newCount = oldCount+1;\r
- boolean lastEntry = index == oldCount;\r
- if (index>oldCount) throw new AccessorException("Index out of range");\r
- \r
- long pos = getStartPosition(index);\r
- int size = rcs.getSize(rcv);\r
- b.position(pos);\r
- b.insertBytes(size, ByteSide.Left);\r
- rcs.serialize(b, null, rcv);\r
- \r
- this.index.add(index, Bindings.LONG, pos);\r
- \r
- for (int i=index+1; i<newCount; i++) {\r
- long p = (Long) this.index.get(i, Bindings.LONG);\r
- p += size;\r
- this.index.set(i, Bindings.LONG, p);\r
- }\r
- \r
- \r
- // Update child map keys\r
- if (!lastEntry && !children.isEmpty()) {\r
- Integer key = children.lastKey();\r
- while (key != null && key >= index) {\r
- java.lang.ref.Reference<BinaryObject> value = children.remove(key);\r
- if (value.get()!=null) children.put(key+1, value);\r
- key = children.lowerKey(key);\r
- }\r
- }\r
- \r
- // Notify Listeners\r
- ListenerEntry le = listeners;\r
- while (le!=null) { \r
- ArrayInterestSet is = le.getInterestSet();\r
- if (is.inNotifications()) {\r
- MutableVariant newValue = null;\r
- if (is.inValues()) newValue = new MutableVariant(rcb, rcb.isImmutable() ? rcv : rcb.clone(rcv)); \r
- ArrayElementAdded e = new ArrayElementAdded(index, newValue);\r
- emitEvent(le, e);\r
- }\r
- \r
- // Update indices of interest sets\r
- if (is.componentInterests!=null) {\r
- Map<Integer, InterestSet> oldCis = is.componentInterests;\r
- boolean needUpdates = false;\r
- for (Integer i : oldCis.keySet()) {\r
- needUpdates |= i>=index;\r
- if (needUpdates) break;\r
- }\r
- \r
- if (needUpdates) {\r
- Map<Integer, InterestSet> newCis = new HashMap<Integer, InterestSet>(oldCis.size()); \r
- for (Integer i : oldCis.keySet())\r
- {\r
- Integer oldKey = i;\r
- Integer newKey = i>=index ? i+1 : i;\r
- InterestSet oldValue = oldCis.get(oldKey);\r
- newCis.put(newKey, oldValue); \r
- }\r
- is.componentInterests = newCis;\r
- }\r
- }\r
- \r
- // Add component interest listener\r
- /*\r
- boolean hadSa = getExistingAccessor(index) != null; \r
- if (!hadSa) {\r
- // Add component interest listener\r
- InterestSet cis = is.getComponentInterest(); \r
- if (cis != null) {\r
- Accessor sa = getAccessor(index);\r
- } \r
- cis = is.getComponentInterest(index); \r
- if (cis != null) {\r
- Accessor sa = getAccessor(index);\r
- }\r
- } */ \r
- \r
- le = le.next;\r
- }\r
- \r
- } catch (IOException e) {\r
- throw new AccessorException(e);\r
- } catch (AdaptException e) {\r
- throw new AccessorException(e);\r
- } catch (SerializerConstructionException e) { \r
- throw new AccessorException(e);\r
- } finally {\r
- writeUnlock();\r
- }\r
- }\r
-\r
- @Override\r
- public void addNoflush(Binding binding, Object value)\r
- throws AccessorException {\r
- addNoflush(size(), binding, value);\r
- }\r
-\r
- @Override\r
- public void addAllNoflush(Binding binding, Object[] values)\r
- throws AccessorException {\r
- addAllNoflush(size(), binding, values);\r
- }\r
-\r
- @Override\r
- public void addAllNoflush(int index, Binding rcb, Object[] rcvs)\r
- throws AccessorException {\r
- if (index<0||index>size()) throw new AccessorException("Index out of bounds");\r
- assert b.isOpen();\r
- writeLock();\r
- try {\r
- Serializer rcs = params.serializerScheme.getSerializer( rcb ); \r
- // Write \r
- b.position(0L);\r
- int repeatCount = rcvs.length;\r
- int oldCount = b.readInt();\r
- int newCount = oldCount + rcvs.length;\r
- if (index>oldCount) throw new AccessorException("Index out of range");\r
- boolean lastEntry = index == oldCount;\r
- \r
- long size = 0;\r
- for (int i=0; i<rcvs.length; i++)\r
- size += rcs.getSize(rcvs[i]);\r
- long pos = getStartPosition(index);\r
- b.position(pos);\r
- b.insertBytes(size, ByteSide.Right);\r
-\r
- long p = pos;\r
- for (int i=0; i<repeatCount; i++) {\r
- this.index.add(index+i, Bindings.LONG, (Long) p);\r
- p += rcs.getSize(rcvs[i]);\r
- }\r
- for (int i=index+repeatCount; i<newCount; i++) {\r
- p = (Long) this.index.get(i, Bindings.LONG);\r
- p += size;\r
- this.index.set(i, Bindings.LONG, p);\r
- }\r
- \r
- b.position(pos);\r
- for (int i=0; i<rcvs.length; i++) {\r
- Object rcv = rcvs[i];\r
- rcs.serialize(b, rcv); \r
- }\r
- \r
- // Update child map keys\r
- if (!lastEntry && !children.isEmpty()) {\r
- Integer key = children.lastKey();\r
- while (key!=null && key >= index) {\r
- java.lang.ref.Reference<BinaryObject> value = children.remove(key);\r
- if (value.get()!=null) children.put(key+rcvs.length, value);\r
- key = children.lowerKey(key);\r
- }\r
- }\r
- \r
- // Notify Listeners\r
- ListenerEntry le = listeners;\r
- while (le!=null) { \r
- ArrayInterestSet is = le.getInterestSet();\r
- if (is.inNotifications()) {\r
- for (int i=0; i<rcvs.length; i++) {\r
- MutableVariant newValue = null;\r
- if (is.inValues()) newValue = new MutableVariant(rcb, rcb.isImmutable() ? rcvs[i] : cb.clone(rcvs[i])); \r
- ArrayElementAdded e = new ArrayElementAdded(index, newValue);\r
- emitEvent(le, e);\r
- }\r
- }\r
- \r
- // Update indices of interest sets\r
- if (is.componentInterests!=null) {\r
- Map<Integer, InterestSet> oldCis = is.componentInterests;\r
- boolean needUpdates = false;\r
- for (Integer i : oldCis.keySet()) {\r
- needUpdates |= i>=index;\r
- if (needUpdates) break;\r
- }\r
- \r
- if (needUpdates) {\r
- Map<Integer, InterestSet> newCis = new HashMap<Integer, InterestSet>(oldCis.size()); \r
- for (Integer i : oldCis.keySet())\r
- {\r
- Integer oldKey = i;\r
- Integer newKey = i>=index ? i+rcvs.length : i;\r
- InterestSet oldValue = oldCis.get(oldKey);\r
- newCis.put(newKey, oldValue); \r
- }\r
- is.componentInterests = newCis;\r
- }\r
- \r
- // Add component interest listener\r
- /*\r
- for (int i = index; i<index+rcvs.length; i++) {\r
- boolean hadSa = getExistingAccessor(i)!=null;\r
- if (hadSa) continue;\r
- \r
- InterestSet cis = is.getComponentInterest(); \r
- if (cis != null) {\r
- Accessor sa = getAccessor(i);\r
- } \r
- cis = is.getComponentInterest(index); \r
- if (cis != null) {\r
- Accessor sa = getAccessor(i);\r
- }\r
- }\r
- */\r
- \r
- }\r
- \r
- le = le.next;\r
- }\r
- \r
- } catch (IOException e) {\r
- throw new AccessorException(e);\r
- } catch (AdaptException e) {\r
- throw new AccessorException(e);\r
- } catch (SerializerConstructionException e) {\r
- throw new AccessorException(e);\r
- } finally {\r
- writeUnlock();\r
- }\r
- }\r
-\r
- void addRepeatNoflush(int index, Binding rcb, Object obj, int repeatCount) throws AccessorException {\r
- if (index<0||index>size()) throw new AccessorException("Index out of bounds");\r
- assert b.isOpen();\r
- writeLock();\r
- try {\r
- Serializer rcs = params.serializerScheme.getSerializer( rcb ); \r
- // Write \r
- b.position(0L);\r
- int oldCount = this.index.size();\r
- int newCount = oldCount + repeatCount;\r
- if (index>oldCount) throw new AccessorException("Index out of range");\r
- boolean lastEntry = index == oldCount;\r
- \r
- long componentSize = rcs.getSize(obj);\r
- long size = componentSize * repeatCount;\r
- long pos = getStartPosition(index);\r
- b.position(pos);\r
- b.insertBytes(size, ByteSide.Right);\r
- long p = pos;\r
- for (int i=0; i<repeatCount; i++) {\r
- this.index.add(index+i, Bindings.LONG, (Long) p);\r
- pos += componentSize;\r
- }\r
- for (int i=index+repeatCount; i<newCount; i++) {\r
- p = (Long) this.index.get(i, Bindings.LONG);\r
- p += size;\r
- this.index.set(i, Bindings.LONG, p);\r
- }\r
- \r
- b.position(pos);\r
- for (int i=0; i<repeatCount; i++) {\r
- rcs.serialize(b, obj); \r
- }\r
- \r
- // Update child map keys\r
- if (!lastEntry && !children.isEmpty()) {\r
- Integer key = children.lastKey();\r
- while (key!=null && key >= index) {\r
- java.lang.ref.Reference<BinaryObject> value = children.remove(key);\r
- if (value.get()!=null) children.put(key+repeatCount, value);\r
- key = children.lowerKey(key);\r
- }\r
- }\r
- \r
- // Notify Listeners\r
- ListenerEntry le = listeners;\r
- while (le!=null) { \r
- ArrayInterestSet is = le.getInterestSet();\r
- if (is.inNotifications()) {\r
- for (int i=0; i<repeatCount; i++) {\r
- MutableVariant newValue = null;\r
- if (is.inValues()) newValue = new MutableVariant(rcb, obj/*rcb.isImmutable() ? obj : cb.clone(obj)*/); \r
- ArrayElementAdded e = new ArrayElementAdded(index, newValue);\r
- emitEvent(le, e);\r
- }\r
- }\r
- \r
- // Update indices of interest sets\r
- if (is.componentInterests!=null) {\r
- Map<Integer, InterestSet> oldCis = is.componentInterests;\r
- boolean needUpdates = false;\r
- for (Integer i : oldCis.keySet()) {\r
- needUpdates |= i>=index;\r
- if (needUpdates) break;\r
- }\r
- \r
- if (needUpdates) {\r
- Map<Integer, InterestSet> newCis = new HashMap<Integer, InterestSet>(oldCis.size()); \r
- for (Integer i : oldCis.keySet())\r
- {\r
- Integer oldKey = i;\r
- Integer newKey = i>=index ? i+repeatCount : i;\r
- InterestSet oldValue = oldCis.get(oldKey);\r
- newCis.put(newKey, oldValue); \r
- }\r
- is.componentInterests = newCis;\r
- }\r
- \r
- }\r
- \r
- le = le.next;\r
- }\r
- \r
- } catch (IOException e) {\r
- throw new AccessorException(e);\r
- } catch (SerializerConstructionException e) {\r
- throw new AccessorException(e);\r
- } finally {\r
- writeUnlock();\r
- }\r
- }\r
- \r
- @Override\r
- public void removeNoflush(int index, int count) throws AccessorException {\r
- assert b.isOpen();\r
- writeLock();\r
- try {\r
- // Write\r
- boolean lastEntry = index == count;\r
- int oldCount = this.index.size();\r
- int newCount = oldCount - count;\r
- if (index<0||index+count>oldCount) throw new AccessorException("Index out of bounds");\r
- long pos = getStartPosition(index); \r
- long lastPos = getStartPosition(index+count-1); \r
- long lastLen = getLength(index, lastPos);\r
- long end = lastPos + lastLen;\r
- long len = end - pos;\r
- b.position(pos);\r
- b.removeBytes(len, ByteSide.Right);\r
- this.index.remove(index, count);\r
- for (int i=index; i<newCount; i++) {\r
- long p = (Long) this.index.get(i, Bindings.LONG);\r
- p -= len;\r
- this.index.set(i, Bindings.LONG, p);\r
- }\r
- \r
- // Remove children\r
- SortedMap<Integer, java.lang.ref.Reference<BinaryObject>> sm = children.subMap(index, true, index+count, false);\r
- for (Entry<Integer, java.lang.ref.Reference<BinaryObject>> e : sm.entrySet()) {\r
- BinaryObject bo = e.getValue().get();\r
- if (bo==null) continue;\r
- bo.invalidatedNotification(); \r
- }\r
- sm.clear();\r
- \r
- // Update the keys of consecutive children\r
- if (!lastEntry && !children.isEmpty()) {\r
- Integer lastKey = children.lastKey();\r
- Integer key = children.higherKey(index);\r
- while (key != null && key <= lastKey) {\r
- java.lang.ref.Reference<BinaryObject> value = children.remove(key);\r
- if (value.get()!=null) children.put(key-count, value);\r
- key = children.higherKey(key);\r
- }\r
- }\r
- \r
- // Notify Listeners\r
- ListenerEntry le = listeners;\r
- while (le!=null) { \r
- ArrayInterestSet is = le.getInterestSet();\r
- if (is.inNotifications()) {\r
- ArrayElementRemoved e = new ArrayElementRemoved(index);\r
- emitEvent(le, e);\r
- }\r
- \r
- // Update indices of interest sets\r
- if (is.componentInterests!=null) {\r
- Map<Integer, InterestSet> oldCis = is.componentInterests;\r
- boolean needUpdates = false;\r
- for (Integer i : oldCis.keySet()) {\r
- needUpdates |= i>=index;\r
- if (needUpdates) break;\r
- }\r
- \r
- if (needUpdates) {\r
- Map<Integer, InterestSet> newCis = new HashMap<Integer, InterestSet>(oldCis.size()); \r
- for (Integer i : oldCis.keySet())\r
- {\r
- Integer oldKey = i;\r
- Integer newKey = i>=index ? i-1 : i;\r
- InterestSet oldValue = oldCis.get(oldKey);\r
- newCis.put(newKey, oldValue); \r
- }\r
- is.componentInterests = newCis;\r
- }\r
- } \r
- le = le.next;\r
- }\r
- \r
- \r
- } catch (IOException e) {\r
- throw new AccessorException( e );\r
- } finally {\r
- writeUnlock();\r
- }\r
- }\r
- \r
- @Override\r
- public Object get(int index, Binding valueBinding) throws AccessorException {\r
- assert b.isOpen();\r
- readLock();\r
- try {\r
- long pos = getStartPosition(index);\r
- b.position(pos);\r
- Serializer s = params.serializerScheme.getSerializer(valueBinding);\r
- return s.deserialize(b);\r
- } catch (IOException e) {\r
- throw new AccessorException(e);\r
- } catch (SerializerConstructionException e) {\r
- throw new AccessorException(e);\r
- } finally {\r
- readUnlock();\r
- }\r
- }\r
-\r
- @Override\r
- public void get(int index, Binding valueBinding, Object dst) throws AccessorException {\r
- assert b.isOpen();\r
- readLock();\r
- try {\r
- long pos = getStartPosition(index);\r
- b.position(pos);\r
- Serializer s = params.serializerScheme.getSerializer(valueBinding);\r
- s.deserializeTo(b, dst);\r
- } catch (IOException e) {\r
- throw new AccessorException(e);\r
- } catch (SerializerConstructionException e) {\r
- throw new AccessorException(e);\r
- } finally {\r
- readUnlock();\r
- }\r
- }\r
-\r
- \r
- @SuppressWarnings("unchecked")\r
- @Override\r
- public <T extends Accessor> T getAccessor(int index)\r
- throws AccessorConstructionException {\r
- assert b.isOpen();\r
- readLock();\r
- try {\r
- b.position(0L);\r
- int count = b.readInt();\r
- if (index<0 || index>=count) throw new ReferenceException("Element index ("+index+") out of bounds ("+count+")");\r
- \r
- // Get existing or create new\r
- BinaryObject sa = getExistingAccessor(index);\r
- if (sa==null) {\r
- long pos = getStartPosition(index);\r
- long len = getLength(index, pos);\r
-\r
- // Instantiate correct sub accessor. \r
- sa = createSubAccessor(cb.type(), pos, len, params); \r
- children.put(index, new WeakReference<BinaryObject>(sa) );\r
-\r
- // Add component interest sets\r
- ListenerEntry le = listeners;\r
- while (le!=null) { \r
- ArrayInterestSet is = le.getInterestSet();\r
-\r
- // Generic element interest\r
- InterestSet gis = is.getComponentInterest(); \r
- if (gis != null) {\r
- try {\r
- ChildReference childPath = ChildReference.concatenate(le.path, new IndexReference(index) );\r
- sa.addListener(le.listener, gis, childPath, le.executor);\r
- } catch (AccessorException e) {\r
- throw new AccessorConstructionException(e);\r
- }\r
- }\r
- \r
- // Specific element interest\r
- InterestSet cis = is.getComponentInterest(index); \r
- if (cis != null) {\r
- try {\r
- ChildReference childPath = ChildReference.concatenate(le.path, new IndexReference(index) );\r
- sa.addListener(le.listener, cis, childPath,le.executor);\r
- } catch (AccessorException e) {\r
- throw new AccessorConstructionException(e);\r
- }\r
- }\r
- \r
- // Next listener\r
- le = le.next;\r
- } \r
- \r
- }\r
- \r
- return (T) sa;\r
- } catch (IOException e) {\r
- throw new AccessorConstructionException(e);\r
- } catch (AccessorException e) {\r
- throw new AccessorConstructionException(e);\r
- } finally {\r
- readUnlock();\r
- }\r
- }\r
-\r
- @SuppressWarnings("unchecked")\r
- @Override\r
- public <T extends Accessor> T getComponent(ChildReference reference)\r
- throws AccessorConstructionException {\r
- if (reference==null) return (T) this;\r
- if (reference instanceof LabelReference) {\r
- LabelReference lr = (LabelReference) reference;\r
- try {\r
- Integer index = new Integer( lr.label );\r
- Accessor result = getAccessor(index);\r
- if (reference.getChildReference() != null)\r
- result = result.getComponent(reference.getChildReference());\r
- return (T) result;\r
- } catch ( NumberFormatException nfe ) {\r
- throw new ReferenceException(nfe);\r
- } \r
- } else if (reference instanceof IndexReference) {\r
- IndexReference ref = (IndexReference) reference;\r
- int index = ref.getIndex();\r
- Accessor result = getAccessor(index);\r
- if (reference.getChildReference() != null)\r
- result = result.getComponent(reference.getChildReference());\r
- return (T) result;\r
- } throw new ReferenceException(reference.getClass().getName()+" is not a reference of an array"); \r
- }\r
- \r
- @Override\r
- public void getAll(Binding valueBinding, Object[] array)\r
- throws AccessorException {\r
- assert b.isOpen();\r
- readLock();\r
- try {\r
- b.position(0L);\r
- int size = b.readInt();\r
- if (size > array.length) throw new AccessorException("Argument array too short");\r
- Serializer s = params.serializerScheme.getSerializer(valueBinding);\r
- for (int i=0; i<size; i++) {\r
- array[i] = s.deserialize(b);\r
- }\r
- } catch (IOException e) {\r
- throw new AccessorException( e );\r
- } catch (SerializerConstructionException e) {\r
- throw new AccessorException( e );\r
- } finally {\r
- readUnlock();\r
- }\r
- }\r
-\r
- @Override\r
- public void getAll(Binding valueBinding, Collection<Object> values)\r
- throws AccessorException {\r
- assert b.isOpen();\r
- readLock();\r
- try {\r
- b.position(0L);\r
- int size = b.readInt();\r
- Serializer s = params.serializerScheme.getSerializer(valueBinding);\r
- for (int i=0; i<size; i++) {\r
- values.add( s.deserialize(b) );\r
- }\r
- } catch (IOException e) {\r
- throw new AccessorException( e );\r
- } catch (SerializerConstructionException e) {\r
- throw new AccessorException( e );\r
- } finally {\r
- readUnlock();\r
- }\r
- }\r
- \r
- @Override\r
- public void setSizeNoflush(int newSize) throws AccessorException {\r
- assert b.isOpen();\r
- writeLock();\r
- try {\r
- int oldSize = size();\r
-\r
- // Remove instances \r
- if (newSize<oldSize) {\r
- remove(newSize, oldSize-newSize);\r
- }\r
- \r
- // Add dummy instances\r
- if (newSize>oldSize) {\r
- Object dummy = cb.createDefault();\r
- int count = newSize-oldSize;\r
- addRepeatNoflush(oldSize, cb, dummy, count);\r
- }\r
- \r
- } catch (BindingException e) {\r
- throw new AccessorException( e );\r
- } finally {\r
- writeUnlock();\r
- }\r
- }\r
-\r
- @Override\r
- public void setSize(int newSize) throws AccessorException {\r
- assert b.isOpen();\r
- writeLock();\r
- try {\r
- setSizeNoflush(newSize);\r
- b.flush();\r
- } catch (IOException e) {\r
- throw new AccessorException( e );\r
- } finally {\r
- writeUnlock();\r
- }\r
- }\r
- \r
- @Override\r
- public int size() throws AccessorException {\r
- assert b.isOpen();\r
- readLock();\r
- return index.size();\r
- }\r
-\r
- @Override\r
- Event applyLocal(Event e, boolean makeRollback) throws AccessorException {\r
- Event rollback = null;\r
- if (e instanceof ValueAssigned) {\r
- ValueAssigned va = (ValueAssigned) e;\r
- if (makeRollback) rollback = new ValueAssigned(cb, getValue(cb)); \r
- setValueNoflush(va.newValue.getBinding(), va.newValue.getValue());\r
- } else \r
- if (e instanceof ArrayElementAdded) {\r
- ArrayElementAdded aa = (ArrayElementAdded) e;\r
- addNoflush(aa.index, aa.value.getBinding(), aa.value.getValue());\r
- if (makeRollback) rollback = new ArrayElementRemoved(aa.index);\r
- } else if (e instanceof ArrayElementRemoved) {\r
- ArrayElementRemoved ar = (ArrayElementRemoved) e;\r
- if (ar.index<0 || ar.index >=size()) throw new AccessorException("Array index out of bounds");\r
- if (makeRollback) {\r
- Object cv = get(ar.index, cb);\r
- rollback = new ArrayElementAdded(ar.index, new MutableVariant(cb, cv));\r
- }\r
- removeNoflush(ar.index, 1);\r
- } else {\r
- throw new AccessorException("Cannot apply "+e.getClass().getName()+" to Array");\r
- }\r
- \r
- return rollback;\r
- }\r
-\r
- @Override\r
- public void add(Binding binding, Object value) throws AccessorException {\r
- assert b.isOpen();\r
- writeLock();\r
- try {\r
- addNoflush(binding, value);\r
- b.flush();\r
- } catch (IOException e) {\r
- throw new AccessorException( e );\r
- } finally {\r
- writeUnlock();\r
- }\r
- }\r
-\r
- @Override\r
- public void add(int index, Binding binding, Object value)\r
- throws AccessorException {\r
- assert b.isOpen();\r
- writeLock();\r
- try {\r
- addNoflush(index, binding, value);\r
- b.flush();\r
- } catch (IOException e) {\r
- throw new AccessorException( e );\r
- } finally {\r
- writeUnlock();\r
- }\r
- }\r
-\r
- @Override\r
- public void addAll(Binding binding, Object[] values)\r
- throws AccessorException {\r
- assert b.isOpen();\r
- writeLock();\r
- try {\r
- addAllNoflush(binding, values);\r
- b.flush();\r
- } catch (IOException e) {\r
- throw new AccessorException( e );\r
- } finally {\r
- writeUnlock();\r
- }\r
- }\r
-\r
- @Override\r
- public void addAll(int index, Binding binding, Object[] values)\r
- throws AccessorException {\r
- assert b.isOpen();\r
- writeLock();\r
- try {\r
- addAllNoflush(index, binding, values);\r
- b.flush();\r
- } catch (IOException e) {\r
- throw new AccessorException( e );\r
- } finally {\r
- writeUnlock(); \r
- }\r
- }\r
-\r
- @Override\r
- public void remove(int index, int count) throws AccessorException {\r
- assert b.isOpen();\r
- writeLock();\r
- try {\r
- removeNoflush(index, count);\r
- b.flush();\r
- } catch (IOException e) {\r
- throw new AccessorException( e );\r
- } finally {\r
- writeUnlock();\r
- }\r
- }\r
-\r
- @Override\r
- public void set(int index, Binding binding, Object value)\r
- throws AccessorException {\r
- assert b.isOpen();\r
- writeLock();\r
- try {\r
- setNoflush(index, binding, value);\r
- b.flush();\r
- } catch (IOException e) {\r
- throw new AccessorException(e);\r
- } finally {\r
- writeUnlock();\r
- }\r
- }\r
- \r
- @Override\r
- public void close() throws AccessorException {\r
- if ( this.index instanceof CloseableAccessor ) {\r
- CloseableAccessor ca = (CloseableAccessor) this.index;\r
- ca.close();\r
- }\r
- super.close();\r
- }\r
- \r
-}\r
-\r
+/*******************************************************************************
+ * 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.Bindings;
+import org.simantics.databoard.accessor.Accessor;
+import org.simantics.databoard.accessor.ArrayAccessor;
+import org.simantics.databoard.accessor.CloseableAccessor;
+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.type.LongType;
+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.
+ * <p>
+ *
+ * Note, To increase the random access performance of the record, create sub-accessors of
+ * its fields.
+ *
+ * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
+ */
+public class BinaryVariableWidthStreamArray extends BinaryObject implements ArrayAccessor, FileArrayAccessor, ArrayAccessor.CloseableArrayAccessor, StreamAccessor {
+
+ /** Accessors to children */
+ TreeMap<Integer, java.lang.ref.Reference<BinaryObject>> children = new TreeMap<Integer, java.lang.ref.Reference<BinaryObject>>();
+
+ Binding cb;
+ Serializer cs;
+ Integer constantSize;
+ ArrayAccessor index;
+
+ public BinaryVariableWidthStreamArray(BinaryObject parent, Blob blob, Datatype type, AccessorParams params, ArrayAccessor index)
+ throws AccessorConstructionException
+ {
+ super(parent, blob, type, params);
+ ArrayType at = (ArrayType) type;
+ cb = params.bindingScheme.getBindingUnchecked(at.componentType);
+ cs = params.serializerScheme.getSerializerUnchecked( cb );
+ constantSize = cs.getConstantSize();
+ if (index==null || index.type().componentType instanceof LongType == false) {
+ throw new AccessorConstructionException("Index must be Long[]");
+ }
+ this.index = index;
+ }
+
+ public ArrayType type() {
+ return (ArrayType) type;
+ }
+
+ /**
+ * Get existing sub accessor
+ * @param index
+ * @return sub-accessor or <code>null</code>
+ */
+ BinaryObject getExistingAccessor(int index)
+ {
+ java.lang.ref.Reference<BinaryObject> ref = children.get(index);
+ if (ref==null) return null;
+ BinaryObject res = (BinaryObject) ref.get();
+// if (res==null) children.remove(index);
+ return res;
+ }
+
+ /**
+ * Get start position of a field
+ *
+ * @param fieldIndex
+ * @return
+ * @throws AccessorException
+ */
+ long getStartPosition(int fieldIndex) throws AccessorException {
+ int c = this.index.size();
+ if ( fieldIndex==c )
+ try {
+ return b.length();
+ } catch (IOException e) {
+ throw new AccessorException( e );
+ }
+ if ( fieldIndex>c || fieldIndex<0 ) throw new AccessorException("Index out of bounds");
+ return (Long) index.get(fieldIndex, Bindings.LONG);
+ }
+
+ long getLength(int index, long pos) throws AccessorException {
+ int c = this.index.size();
+ if ( index>=c || index<0 ) return 0;
+ if ( index==c-1 )
+ {
+ try {
+ return b.length() - (Long) this.index.get(c-1, Bindings.LONG);
+ } catch (IOException e) {
+ throw new AccessorException( e );
+ }
+ } else {
+ return (Long) this.index.get(index+1, Bindings.LONG) - (Long) this.index.get(index, Bindings.LONG);
+ }
+ }
+
+ @Override
+ public void setNoflush(int index, Binding rcb, Object rcv)
+ throws AccessorException {
+ assert b.isOpen();
+
+ writeLock();
+ try {
+
+ int count = size();
+ if ( index==count ) {
+ addNoflush(count, rcb, rcv);
+ return;
+ }
+
+ // Write
+ Serializer rcs = params.serializerScheme.getSerializer( rcb );
+ long pos = getStartPosition(index);
+ long oldSize = getLength(index, pos);
+ long newSize = rcs.getSize(rcv, null);
+ b.position(pos);
+ long diff = newSize - oldSize;
+ if (diff>0) {
+ b.insertBytes( newSize - oldSize, ByteSide.Right );
+ } else if (diff<0) {
+ b.removeBytes( oldSize - newSize, ByteSide.Right );
+ }
+ b.position(pos);
+ rcs.serialize(b, null, rcv);
+
+ // Update index of the higher indices
+ if ( diff!= 0 ) {
+ for (int i=index+1; i<count; i++) {
+ long p = (Long) this.index.get(i, Bindings.LONG);
+ p += diff;
+ this.index.set(i, Bindings.LONG, p);
+ }
+ }
+
+ // Update Blobs
+ /*
+ BinaryObject sa = getExistingAccessor(index);
+ if (sa!=null && newSize != oldSize) {
+ sa.b.setPositionInSource(pos, newSize);
+ }*/
+
+ // Shift consecutive blobs
+/* if (!children.isEmpty()) {
+ Integer lastKey = children.lastKey();
+ if (lastKey!=null && index+1 <= lastKey && diff!=0) {
+ SortedMap<Integer, Reference<BinaryObject>> pm = children.subMap(index+1, true, lastKey, true);
+ for (Entry<Integer, Reference<BinaryObject>> e : pm.entrySet()) {
+ BinaryObject sa_ = e.getValue().get();
+ if (sa_ == null) continue;
+ sa_.b.setPositionInSource( sa_.b.getStartPositionInSourceBinary() + diff, sa_.b.length());
+ }
+ }
+ }*/
+
+ // Notify
+ ListenerEntry le = listeners;
+ while (le!=null) {
+ ArrayInterestSet is = le.getInterestSet();
+ if (is.inNotificationsOf(index)) {
+ MutableVariant newValue = null;
+ if (is.inValues()) newValue = new MutableVariant(rcb, rcb.isImmutable() ? rcv : rcb.clone(rcv));
+
+ Event e = new ValueAssigned(new IndexReference(index), newValue);
+ emitEvent(le, e);
+ }
+ le = le.next;
+ }
+
+ } catch (IOException e) {
+ throw new AccessorException(e);
+ } catch (AdaptException e) {
+ throw new AccessorException(e);
+ } catch (SerializerConstructionException e) {
+ throw new AccessorException(e);
+ } finally {
+ writeUnlock();
+ }
+
+ }
+
+ /**
+ * Set all values
+ *
+ * @param arrayBinding
+ * @param newArray
+ */
+ @Override
+ public void setValueNoflush(Binding arrayBinding, Object newArray)
+ throws AccessorException {
+ assert b.isOpen();
+ writeLock();
+ try {
+ // Write
+ ArrayBinding rb = ((ArrayBinding)arrayBinding);
+ Binding rcb = rb.getComponentBinding();
+ Serializer rcs = params.serializerScheme.getSerializer( rcb );
+ int oldCount = index.size();
+ int newCount = rb.size(newArray);
+ b.setLength( params.serializerScheme.getSerializer( rb ).getSize(newArray) );
+ index.setSize(newCount);
+
+ // Serialize
+ for (int index=0; index<newCount; index++) {
+ //long startPos = b.position();
+ Object obj = rb.get(newArray, index);
+ this.index.set(index, Bindings.LONG, b.position());
+ rcs.serialize(b, obj);
+ //long endPos = b.position();
+ //long len = endPos - startPos;
+
+ // update location
+ /*
+ BinaryObject sa = getExistingAccessor(index);
+ if (sa!=null) {
+ sa.b.setPositionInSource(startPos, len);
+ }*/
+ }
+
+ // Notify removal
+ for (int index=oldCount-1; index>=newCount; index--) {
+ BinaryObject sa = getExistingAccessor(index);
+ if (sa!=null) {
+ sa.invalidatedNotification();
+ children.remove(index);
+ sa = null;
+ }
+
+ // Notify changes
+ ListenerEntry le = listeners;
+ while (le!=null) {
+ ArrayInterestSet is = le.getInterestSet();
+ if (is.inNotificationsOf(index)) {
+ Event e = new ArrayElementRemoved(index);
+ emitEvent(le, e);
+ }
+ le = le.next;
+ }
+ }
+
+ // Notify new assignment
+ if (listeners!=null) {
+ for (int index=0; index<newCount; index++) {
+ Object cv = rb.get(newArray, index);
+
+ // Notify changes
+ ListenerEntry le = listeners;
+ while (le!=null) {
+ ArrayInterestSet is = le.getInterestSet();
+ if (is.inNotificationsOf(index)) {
+ MutableVariant vv = null;
+ if (is.inValues()) vv = new MutableVariant(rcb, cb.isImmutable() ? cv : rcb.clone(cv));
+
+ Event e = index<oldCount ? new ValueAssigned(new IndexReference(index), vv) : new ArrayElementAdded(index, vv);
+ emitEvent(le, e);
+ }
+ le = le.next;
+ }
+ }
+ }
+
+ } catch (BindingException e) {
+ throw new AccessorException(e);
+ } catch (IOException e) {
+ throw new AccessorException(e);
+ } catch (AdaptException e) {
+ throw new AccessorException(e);
+ } catch (SerializerConstructionException e) {
+ throw new AccessorException(e);
+ } finally {
+ writeUnlock();
+ }
+
+ }
+
+ @Override
+ public void addNoflush(int index, Binding rcb, Object rcv) throws AccessorException {
+ assert b.isOpen();
+ writeLock();
+ try {
+ Serializer rcs = params.serializerScheme.getSerializer( rcb );
+ // Write
+ int oldCount = this.index.size();
+ int newCount = oldCount+1;
+ boolean lastEntry = index == oldCount;
+ if (index>oldCount) throw new AccessorException("Index out of range");
+
+ long pos = getStartPosition(index);
+ int size = rcs.getSize(rcv);
+ b.position(pos);
+ b.insertBytes(size, ByteSide.Left);
+ rcs.serialize(b, null, rcv);
+
+ this.index.add(index, Bindings.LONG, pos);
+
+ for (int i=index+1; i<newCount; i++) {
+ long p = (Long) this.index.get(i, Bindings.LONG);
+ p += size;
+ this.index.set(i, Bindings.LONG, p);
+ }
+
+
+ // Update child map keys
+ if (!lastEntry && !children.isEmpty()) {
+ Integer key = children.lastKey();
+ while (key != null && key >= index) {
+ java.lang.ref.Reference<BinaryObject> value = children.remove(key);
+ if (value.get()!=null) children.put(key+1, value);
+ key = children.lowerKey(key);
+ }
+ }
+
+ // Notify Listeners
+ ListenerEntry le = listeners;
+ while (le!=null) {
+ ArrayInterestSet is = le.getInterestSet();
+ if (is.inNotifications()) {
+ MutableVariant newValue = null;
+ if (is.inValues()) newValue = new MutableVariant(rcb, rcb.isImmutable() ? rcv : rcb.clone(rcv));
+ ArrayElementAdded e = new ArrayElementAdded(index, newValue);
+ emitEvent(le, e);
+ }
+
+ // Update indices of interest sets
+ if (is.componentInterests!=null) {
+ Map<Integer, InterestSet> oldCis = is.componentInterests;
+ boolean needUpdates = false;
+ for (Integer i : oldCis.keySet()) {
+ needUpdates |= i>=index;
+ if (needUpdates) break;
+ }
+
+ if (needUpdates) {
+ Map<Integer, InterestSet> newCis = new HashMap<Integer, InterestSet>(oldCis.size());
+ for (Integer i : oldCis.keySet())
+ {
+ Integer oldKey = i;
+ Integer newKey = i>=index ? i+1 : i;
+ InterestSet oldValue = oldCis.get(oldKey);
+ newCis.put(newKey, oldValue);
+ }
+ is.componentInterests = newCis;
+ }
+ }
+
+ // Add component interest listener
+ /*
+ boolean hadSa = getExistingAccessor(index) != null;
+ if (!hadSa) {
+ // Add component interest listener
+ InterestSet cis = is.getComponentInterest();
+ if (cis != null) {
+ Accessor sa = getAccessor(index);
+ }
+ cis = is.getComponentInterest(index);
+ if (cis != null) {
+ Accessor sa = getAccessor(index);
+ }
+ } */
+
+ le = le.next;
+ }
+
+ } catch (IOException e) {
+ throw new AccessorException(e);
+ } catch (AdaptException e) {
+ throw new AccessorException(e);
+ } catch (SerializerConstructionException e) {
+ throw new AccessorException(e);
+ } finally {
+ writeUnlock();
+ }
+ }
+
+ @Override
+ public void addNoflush(Binding binding, Object value)
+ throws AccessorException {
+ addNoflush(size(), binding, value);
+ }
+
+ @Override
+ public void addAllNoflush(Binding binding, Object[] values)
+ throws AccessorException {
+ addAllNoflush(size(), binding, values);
+ }
+
+ @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();
+ writeLock();
+ try {
+ Serializer rcs = params.serializerScheme.getSerializer( rcb );
+ // Write
+ b.position(0L);
+ int repeatCount = rcvs.length;
+ int oldCount = b.readInt();
+ int newCount = oldCount + rcvs.length;
+ if (index>oldCount) throw new AccessorException("Index out of range");
+ boolean lastEntry = index == oldCount;
+
+ long size = 0;
+ for (int i=0; i<rcvs.length; i++)
+ size += rcs.getSize(rcvs[i]);
+ long pos = getStartPosition(index);
+ b.position(pos);
+ b.insertBytes(size, ByteSide.Right);
+
+ long p = pos;
+ for (int i=0; i<repeatCount; i++) {
+ this.index.add(index+i, Bindings.LONG, (Long) p);
+ p += rcs.getSize(rcvs[i]);
+ }
+ for (int i=index+repeatCount; i<newCount; i++) {
+ p = (Long) this.index.get(i, Bindings.LONG);
+ p += size;
+ this.index.set(i, Bindings.LONG, p);
+ }
+
+ b.position(pos);
+ for (int i=0; i<rcvs.length; i++) {
+ Object rcv = rcvs[i];
+ rcs.serialize(b, rcv);
+ }
+
+ // Update child map keys
+ if (!lastEntry && !children.isEmpty()) {
+ Integer key = children.lastKey();
+ while (key!=null && key >= index) {
+ java.lang.ref.Reference<BinaryObject> value = children.remove(key);
+ if (value.get()!=null) children.put(key+rcvs.length, 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<rcvs.length; i++) {
+ MutableVariant newValue = null;
+ if (is.inValues()) newValue = new MutableVariant(rcb, rcb.isImmutable() ? rcvs[i] : cb.clone(rcvs[i]));
+ ArrayElementAdded e = new ArrayElementAdded(index, newValue);
+ emitEvent(le, e);
+ }
+ }
+
+ // Update indices of interest sets
+ if (is.componentInterests!=null) {
+ Map<Integer, InterestSet> oldCis = is.componentInterests;
+ boolean needUpdates = false;
+ for (Integer i : oldCis.keySet()) {
+ needUpdates |= i>=index;
+ if (needUpdates) break;
+ }
+
+ if (needUpdates) {
+ Map<Integer, InterestSet> newCis = new HashMap<Integer, InterestSet>(oldCis.size());
+ for (Integer i : oldCis.keySet())
+ {
+ Integer oldKey = i;
+ Integer newKey = i>=index ? i+rcvs.length : i;
+ InterestSet oldValue = oldCis.get(oldKey);
+ newCis.put(newKey, oldValue);
+ }
+ is.componentInterests = newCis;
+ }
+
+ // Add component interest listener
+ /*
+ for (int i = index; i<index+rcvs.length; i++) {
+ boolean hadSa = getExistingAccessor(i)!=null;
+ if (hadSa) continue;
+
+ InterestSet cis = is.getComponentInterest();
+ if (cis != null) {
+ Accessor sa = getAccessor(i);
+ }
+ cis = is.getComponentInterest(index);
+ if (cis != null) {
+ Accessor sa = getAccessor(i);
+ }
+ }
+ */
+
+ }
+
+ le = le.next;
+ }
+
+ } catch (IOException e) {
+ throw new AccessorException(e);
+ } catch (AdaptException 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 = this.index.size();
+ int newCount = oldCount + repeatCount;
+ if (index>oldCount) throw new AccessorException("Index out of range");
+ boolean lastEntry = index == oldCount;
+
+ long componentSize = rcs.getSize(obj);
+ long size = componentSize * repeatCount;
+ long pos = getStartPosition(index);
+ b.position(pos);
+ b.insertBytes(size, ByteSide.Right);
+ long p = pos;
+ for (int i=0; i<repeatCount; i++) {
+ this.index.add(index+i, Bindings.LONG, (Long) p);
+ pos += componentSize;
+ }
+ for (int i=index+repeatCount; i<newCount; i++) {
+ p = (Long) this.index.get(i, Bindings.LONG);
+ p += size;
+ this.index.set(i, Bindings.LONG, p);
+ }
+
+ b.position(pos);
+ for (int i=0; i<repeatCount; i++) {
+ rcs.serialize(b, obj);
+ }
+
+ // Update child map keys
+ if (!lastEntry && !children.isEmpty()) {
+ Integer key = children.lastKey();
+ while (key!=null && key >= index) {
+ java.lang.ref.Reference<BinaryObject> 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<repeatCount; i++) {
+ MutableVariant newValue = null;
+ if (is.inValues()) newValue = new MutableVariant(rcb, obj/*rcb.isImmutable() ? obj : cb.clone(obj)*/);
+ ArrayElementAdded e = new ArrayElementAdded(index, newValue);
+ emitEvent(le, e);
+ }
+ }
+
+ // Update indices of interest sets
+ if (is.componentInterests!=null) {
+ Map<Integer, InterestSet> oldCis = is.componentInterests;
+ boolean needUpdates = false;
+ for (Integer i : oldCis.keySet()) {
+ needUpdates |= i>=index;
+ if (needUpdates) break;
+ }
+
+ if (needUpdates) {
+ Map<Integer, InterestSet> newCis = new HashMap<Integer, InterestSet>(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();
+ writeLock();
+ try {
+ // Write
+ boolean lastEntry = index == count;
+ int oldCount = this.index.size();
+ int newCount = oldCount - count;
+ if (index<0||index+count>oldCount) throw new AccessorException("Index out of bounds");
+ long pos = getStartPosition(index);
+ long lastPos = getStartPosition(index+count-1);
+ long lastLen = getLength(index, lastPos);
+ long end = lastPos + lastLen;
+ long len = end - pos;
+ b.position(pos);
+ b.removeBytes(len, ByteSide.Right);
+ this.index.remove(index, count);
+ for (int i=index; i<newCount; i++) {
+ long p = (Long) this.index.get(i, Bindings.LONG);
+ p -= len;
+ this.index.set(i, Bindings.LONG, p);
+ }
+
+ // Remove children
+ SortedMap<Integer, java.lang.ref.Reference<BinaryObject>> sm = children.subMap(index, true, index+count, false);
+ for (Entry<Integer, java.lang.ref.Reference<BinaryObject>> e : sm.entrySet()) {
+ BinaryObject bo = e.getValue().get();
+ if (bo==null) continue;
+ bo.invalidatedNotification();
+ }
+ sm.clear();
+
+ // Update the keys of consecutive children
+ if (!lastEntry && !children.isEmpty()) {
+ Integer lastKey = children.lastKey();
+ Integer key = children.higherKey(index);
+ while (key != null && key <= lastKey) {
+ java.lang.ref.Reference<BinaryObject> value = children.remove(key);
+ if (value.get()!=null) children.put(key-count, value);
+ key = children.higherKey(key);
+ }
+ }
+
+ // Notify Listeners
+ ListenerEntry le = listeners;
+ while (le!=null) {
+ ArrayInterestSet is = le.getInterestSet();
+ if (is.inNotifications()) {
+ ArrayElementRemoved e = new ArrayElementRemoved(index);
+ emitEvent(le, e);
+ }
+
+ // Update indices of interest sets
+ if (is.componentInterests!=null) {
+ Map<Integer, InterestSet> oldCis = is.componentInterests;
+ boolean needUpdates = false;
+ for (Integer i : oldCis.keySet()) {
+ needUpdates |= i>=index;
+ if (needUpdates) break;
+ }
+
+ if (needUpdates) {
+ Map<Integer, InterestSet> newCis = new HashMap<Integer, InterestSet>(oldCis.size());
+ for (Integer i : oldCis.keySet())
+ {
+ Integer oldKey = i;
+ Integer newKey = i>=index ? i-1 : i;
+ InterestSet oldValue = oldCis.get(oldKey);
+ newCis.put(newKey, oldValue);
+ }
+ is.componentInterests = newCis;
+ }
+ }
+ le = le.next;
+ }
+
+
+ } catch (IOException e) {
+ throw new AccessorException( e );
+ } finally {
+ writeUnlock();
+ }
+ }
+
+ @Override
+ public Object get(int index, Binding valueBinding) throws AccessorException {
+ assert b.isOpen();
+ readLock();
+ try {
+ long pos = getStartPosition(index);
+ 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();
+ }
+ }
+
+ @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 extends Accessor> T getAccessor(int index)
+ throws AccessorConstructionException {
+ assert b.isOpen();
+ readLock();
+ try {
+ b.position(0L);
+ int count = b.readInt();
+ if (index<0 || index>=count) throw new ReferenceException("Element index ("+index+") out of bounds ("+count+")");
+
+ // Get existing or create new
+ BinaryObject sa = getExistingAccessor(index);
+ if (sa==null) {
+ long pos = getStartPosition(index);
+ long len = getLength(index, pos);
+
+ // Instantiate correct sub accessor.
+ sa = createSubAccessor(cb.type(), pos, len, params);
+ children.put(index, new WeakReference<BinaryObject>(sa) );
+
+ // Add component interest sets
+ ListenerEntry le = listeners;
+ while (le!=null) {
+ ArrayInterestSet is = le.getInterestSet();
+
+ // Generic element interest
+ InterestSet gis = is.getComponentInterest();
+ if (gis != null) {
+ try {
+ ChildReference childPath = ChildReference.concatenate(le.path, new IndexReference(index) );
+ sa.addListener(le.listener, gis, childPath, le.executor);
+ } catch (AccessorException e) {
+ throw new AccessorConstructionException(e);
+ }
+ }
+
+ // Specific element interest
+ InterestSet cis = is.getComponentInterest(index);
+ if (cis != null) {
+ try {
+ ChildReference childPath = ChildReference.concatenate(le.path, new IndexReference(index) );
+ sa.addListener(le.listener, cis, childPath,le.executor);
+ } catch (AccessorException e) {
+ throw new AccessorConstructionException(e);
+ }
+ }
+
+ // Next listener
+ le = le.next;
+ }
+
+ }
+
+ return (T) sa;
+ } catch (IOException e) {
+ throw new AccessorConstructionException(e);
+ } catch (AccessorException e) {
+ throw new AccessorConstructionException(e);
+ } finally {
+ readUnlock();
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public <T extends Accessor> 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);
+ }
+ } else if (reference instanceof IndexReference) {
+ IndexReference ref = (IndexReference) reference;
+ int index = ref.getIndex();
+ Accessor result = getAccessor(index);
+ if (reference.getChildReference() != null)
+ result = result.getComponent(reference.getChildReference());
+ return (T) result;
+ } throw new ReferenceException(reference.getClass().getName()+" is not a reference of an array");
+ }
+
+ @Override
+ public void getAll(Binding valueBinding, Object[] array)
+ 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");
+ Serializer s = params.serializerScheme.getSerializer(valueBinding);
+ for (int i=0; i<size; i++) {
+ array[i] = s.deserialize(b);
+ }
+ } catch (IOException e) {
+ throw new AccessorException( e );
+ } catch (SerializerConstructionException e) {
+ throw new AccessorException( e );
+ } finally {
+ readUnlock();
+ }
+ }
+
+ @Override
+ public void getAll(Binding valueBinding, Collection<Object> values)
+ throws AccessorException {
+ assert b.isOpen();
+ readLock();
+ try {
+ b.position(0L);
+ int size = b.readInt();
+ Serializer s = params.serializerScheme.getSerializer(valueBinding);
+ for (int i=0; i<size; i++) {
+ values.add( s.deserialize(b) );
+ }
+ } catch (IOException e) {
+ throw new AccessorException( e );
+ } catch (SerializerConstructionException e) {
+ throw new AccessorException( e );
+ } finally {
+ readUnlock();
+ }
+ }
+
+ @Override
+ public void setSizeNoflush(int newSize) throws AccessorException {
+ assert b.isOpen();
+ writeLock();
+ try {
+ int oldSize = size();
+
+ // Remove instances
+ if (newSize<oldSize) {
+ remove(newSize, oldSize-newSize);
+ }
+
+ // Add dummy instances
+ if (newSize>oldSize) {
+ 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 int size() throws AccessorException {
+ assert b.isOpen();
+ readLock();
+ return index.size();
+ }
+
+ @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());
+ } else
+ if (e instanceof ArrayElementAdded) {
+ ArrayElementAdded aa = (ArrayElementAdded) e;
+ addNoflush(aa.index, aa.value.getBinding(), aa.value.getValue());
+ if (makeRollback) rollback = new ArrayElementRemoved(aa.index);
+ } else if (e instanceof ArrayElementRemoved) {
+ ArrayElementRemoved ar = (ArrayElementRemoved) e;
+ if (ar.index<0 || ar.index >=size()) throw new AccessorException("Array index out of bounds");
+ if (makeRollback) {
+ Object cv = get(ar.index, cb);
+ rollback = new ArrayElementAdded(ar.index, new MutableVariant(cb, cv));
+ }
+ removeNoflush(ar.index, 1);
+ } else {
+ throw new AccessorException("Cannot apply "+e.getClass().getName()+" to Array");
+ }
+
+ return rollback;
+ }
+
+ @Override
+ 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();
+ }
+ }
+
+ @Override
+ public void add(int index, Binding binding, Object value)
+ throws AccessorException {
+ assert b.isOpen();
+ writeLock();
+ try {
+ addNoflush(index, binding, value);
+ b.flush();
+ } catch (IOException e) {
+ throw new AccessorException( e );
+ } finally {
+ writeUnlock();
+ }
+ }
+
+ @Override
+ public void addAll(Binding binding, Object[] values)
+ throws AccessorException {
+ assert b.isOpen();
+ writeLock();
+ try {
+ addAllNoflush(binding, values);
+ b.flush();
+ } catch (IOException e) {
+ throw new AccessorException( e );
+ } finally {
+ writeUnlock();
+ }
+ }
+
+ @Override
+ public void addAll(int index, Binding binding, Object[] values)
+ throws AccessorException {
+ assert b.isOpen();
+ writeLock();
+ try {
+ addAllNoflush(index, binding, values);
+ b.flush();
+ } catch (IOException e) {
+ throw new AccessorException( e );
+ } finally {
+ writeUnlock();
+ }
+ }
+
+ @Override
+ 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();
+ }
+ }
+
+ @Override
+ public void set(int index, Binding binding, Object value)
+ throws AccessorException {
+ assert b.isOpen();
+ writeLock();
+ try {
+ setNoflush(index, binding, value);
+ b.flush();
+ } catch (IOException e) {
+ throw new AccessorException(e);
+ } finally {
+ writeUnlock();
+ }
+ }
+
+ @Override
+ public void close() throws AccessorException {
+ if ( this.index instanceof CloseableAccessor ) {
+ CloseableAccessor ca = (CloseableAccessor) this.index;
+ ca.close();
+ }
+ super.close();
+ }
+
+}
+