=Accessors=
Say, you have several gigabytes of data in a file. The whole object doesn't need to be serialized at once. You can read and write the value partially using [[svn:foundation/databoard/trunk/org.simantics.databoard/src/org/simantics/databoard/accessor/|Accessor]] interface. The actual container can be a file, memory byte[]/ByteBuffer or a Java Object. The content is structured as tree using Databoard's type system. All but referable records are supported (=no recursion in accessors).
'''[../javadoc/org/simantics/databoard/accessor.html|org.simantics.databoard.accessor] interfaces'''.
{| style="background-color: #e9e9e9; border: 1px solid #aaaaaa; "
| '''Class'''
| '''Description'''
|- style="background-color: #f9f9f9; " |
| [../javadoc/org/simantics/databoard/accessor/Accessor.html|Accessor]
| Base class for all data Accessors
|- style="background-color: #f9f9f9; " |
| [../javadoc/org/simantics/databoard/accessor/RecordAccessor.html|RecordAccessor]
| Record
|- style="background-color: #f9f9f9; " |
| [../javadoc/org/simantics/databoard/accessor/ArrayAccessor.html|ArrayAccessor]
| Array - an ordered sequence of elements of one value.
|- style="background-color: #f9f9f9; " |
| [../javadoc/org/simantics/databoard/accessor/MapAccessor.html|MapAccessor]
| Map - an ''ordered'' map of keys to values.
|- style="background-color: #f9f9f9; " |
| [../javadoc/org/simantics/databoard/accessor/UnionAccessor.html|UnionAccessor]
| Union
|- style="background-color: #f9f9f9; " |
| [../javadoc/org/simantics/databoard/accessor/BooleanAccessor.html|BooleanAccessor],[../javadoc/org/simantics/databoard/accessor/IntAccessor.html|IntAccessor],[../javadoc/org/simantics/databoard/accessor/LongAccessor.html|LongAccessor],[../javadoc/org/simantics/databoard/accessor/FloatAccessor.html|FloatAccessor],[../javadoc/org/simantics/databoard/accessor/DoubleAccessor.html|DoubleAccessor]
| Primitive and numeric Accessors
|- style="background-color: #f9f9f9; " |
| [../javadoc/org/simantics/databoard/accessor/StringAccessor.html|StringAccessor]
| String
|- style="background-color: #f9f9f9; " |
| [../javadoc/org/simantics/databoard/accessor/OptionalAccessor.html|OptionalAccessor]
| Optional value
|- style="background-color: #f9f9f9; " |
| [../javadoc/org/simantics/databoard/accessor/VariantAccessor.html|VariantAccessor]
| Variant value
|}
[../javadoc/org/simantics/databoard/Accessors.html|Accessors] and [../javadoc/org/simantics/databoard/Files.html|Files] are facade classes that contains utilities for instantiating and handling Accessors.
[../javadoc/org/simantics/databoard/accessor/binary/index.html|Binary Accessor]
is an access to a value in binary format (byte[] or ByteBuffer).
'''Example:''' Binary accessor
Datatype type = Datatypes.getDatatype( Rectangle2D.Double.class ); Binding binding = Bindings.getBinding( Rectangle2D.Double.class ); Serializer s = Binding.getSerializer( binding ); // Serialize rectangle Rectangle2D rect = new Rectangle2D.Double(0,0, 10, 10); byte[] data = s.serialize(rect); // Open accessor to byte data and modify first field in the byte data RecordAccessor ra = Accessors.getAccessor(data, type); ra.setFieldValue(0, Bindings.DOUBLE, 5.0); // Deserialize values from the byte data back to the rectangle object s.deserialize(data, rect); System.out.println(rect.getX());'''Example:''' File accessor, create
RecordType type = Datatypes.getDatatype( Rectangle2D.Double.class ); // Create a new file and initialize it with rectangle type, and open file accessor FileRecordAccessor fa = Accessors.createFile( file, type ); // Write the first field (x) fa.setFieldValue(0, Bindings.DOUBLE, 5.0); fa.close();'''Example:''' File accessor, open
// Open an accessor to an existing binary file FileVariantAccessor fa = Accessors.openAccessor(file); RecordAccessor ra = fa.getContentAccessor(); // Read the first field (x) Double x = (Double) ra.getFieldValue(0, Bindings.DOUBLE); fa.close();'''Example:''' Java Accessor
Binding binding = Bindings.getBinding(Rectangle2D.Double.class); Rectangle2D rect = new Rectangle2D.Double(0,0, 10, 10); // Open accessor to rectangle RecordAccessor ra = Accessors.getAccessor(binding, rect); // Set rectangle's first field (x) to 5.0 ra.setFieldValue(0, Bindings.DOUBLE, 5.0); System.out.println( rect.getX() );==Accessor Reference== Accessors can be opened to a sub-nodes with AccessorReference or by calling getAccessor. AccessorReference is a string of instances, either accessor type specific of LabelReferences.
ChildReference ref = ChildReference.compile( new NameReference("node"), new ComponentReference() ); Accessor child = accessor.getComponent( ref ); ChildReference ref = ChildReference.compile( new LabelReference("node"), new LabelReference("v") ); Accessor child = accessor.getComponent( ref ); ChildReference ref = ChildReference.create("n-node/v"); Accessor child = accessor.getComponent( ref ); ChildReference ref = ChildReference.create("node/v"); Accessor child = accessor.getComponent( ref ); VariantAccessor va = recordAccessor.getFieldAccessor("node"); Accessor child = va.getValueAccessor();==Listening mechanism== Accessor offers a monitoring mechanism for the data model. There is an
[../javadoc/org/simantics/databoard/accessor/interestset/InterestSet.html|InterestSet]
that is a description of a sub-tree that is to be monitored of the data model.
[../javadoc/org/simantics/databoard/accessor/event/Event.html|Events]
are objects that spawned on changes to the data model. Each event object is annotated with [../javadoc/org/simantics/databoard/accessor/reference/index.html|reference path] that is in relation to the node where the listener was placed.
Accessor Listeners use [EventThread Pattern] pattern.