]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src-isv/doc/accessor.mediawiki
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / src-isv / doc / accessor.mediawiki
1 =Accessors=
2 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). 
3
4 '''[../javadoc/org/simantics/databoard/accessor.html|org.simantics.databoard.accessor] interfaces'''.
5 {| style="background-color: #e9e9e9; border: 1px solid #aaaaaa; "
6 | '''Class'''
7 | '''Description'''
8 |- style="background-color: #f9f9f9; " |
9 | [../javadoc/org/simantics/databoard/accessor/Accessor.html|Accessor]
10 | Base class for all data Accessors
11 |- style="background-color: #f9f9f9; " |
12 | [../javadoc/org/simantics/databoard/accessor/RecordAccessor.html|RecordAccessor]
13 | Record 
14 |- style="background-color: #f9f9f9; " |
15 | [../javadoc/org/simantics/databoard/accessor/ArrayAccessor.html|ArrayAccessor]
16 | Array - an ordered sequence of elements of one value.
17 |- style="background-color: #f9f9f9; " |
18 | [../javadoc/org/simantics/databoard/accessor/MapAccessor.html|MapAccessor]
19 | Map - an ''ordered'' map of keys to values. 
20 |- style="background-color: #f9f9f9; " |
21 | [../javadoc/org/simantics/databoard/accessor/UnionAccessor.html|UnionAccessor]
22 | Union
23 |- style="background-color: #f9f9f9; " |
24 | [../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]
25 | Primitive and numeric Accessors
26 |- style="background-color: #f9f9f9; " |
27 | [../javadoc/org/simantics/databoard/accessor/StringAccessor.html|StringAccessor]
28 | String 
29 |- style="background-color: #f9f9f9; " |
30 | [../javadoc/org/simantics/databoard/accessor/OptionalAccessor.html|OptionalAccessor]
31 | Optional value
32 |- style="background-color: #f9f9f9; " |
33 | [../javadoc/org/simantics/databoard/accessor/VariantAccessor.html|VariantAccessor]
34 | Variant value
35 |}
36
37 [../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.
38
39 <code>[../javadoc/org/simantics/databoard/accessor/binary/index.html|Binary Accessor]</code> is an access to a value in binary format (<tt>byte[]</tt> or <tt>ByteBuffer</tt>).
40
41 '''Example:''' Binary accessor
42 <pre class="code">
43 Datatype type = Datatypes.getDatatype( Rectangle2D.Double.class );
44 Binding binding = Bindings.getBinding( Rectangle2D.Double.class );
45 Serializer s = Binding.getSerializer( binding );
46
47 // Serialize rectangle
48 Rectangle2D rect = new Rectangle2D.Double(0,0, 10, 10);
49 byte[] data = s.serialize(rect);
50
51 // Open accessor to byte data and modify first field in the byte data
52 RecordAccessor ra = Accessors.getAccessor(data, type);
53 ra.setFieldValue(0, Bindings.DOUBLE, 5.0);
54
55 // Deserialize values from the byte data back to the rectangle object
56 s.deserialize(data, rect);
57 System.out.println(rect.getX());
58 </pre>
59
60 '''Example:''' File accessor, create
61 <pre class="code">
62 RecordType type = Datatypes.getDatatype( Rectangle2D.Double.class );
63 // Create a new file and initialize it with rectangle type, and open file accessor
64 FileRecordAccessor fa = Accessors.createFile( file, type );
65
66 // Write the first field (x)
67 fa.setFieldValue(0, Bindings.DOUBLE, 5.0);
68 fa.close();
69 </pre>
70
71 '''Example:''' File accessor, open
72 <pre class="code">
73 // Open an accessor to an existing binary file
74 FileVariantAccessor fa = Accessors.openAccessor(file);
75 RecordAccessor ra = fa.getContentAccessor();
76
77 // Read the first field (x)
78 Double x = (Double) ra.getFieldValue(0, Bindings.DOUBLE);
79 fa.close();
80 </pre>
81
82 '''Example:''' Java Accessor
83 <pre class="code">
84 Binding binding = Bindings.getBinding(Rectangle2D.Double.class);
85 Rectangle2D rect = new Rectangle2D.Double(0,0, 10, 10);
86
87 // Open accessor to rectangle
88 RecordAccessor ra = Accessors.getAccessor(binding, rect);
89
90 // Set rectangle's first field (x) to 5.0
91 ra.setFieldValue(0, Bindings.DOUBLE, 5.0);
92 System.out.println( rect.getX() );
93 </pre>
94
95
96 ==Accessor Reference==
97 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. 
98 <pre class="code">
99     ChildReference ref = ChildReference.compile(
100       new NameReference("node"),
101       new ComponentReference()
102     );
103     Accessor child = accessor.getComponent( ref );
104
105     ChildReference ref = ChildReference.compile(
106        new LabelReference("node"),
107        new LabelReference("v")
108     );
109     Accessor child = accessor.getComponent( ref );
110
111     ChildReference ref = ChildReference.create("n-node/v");
112     Accessor child = accessor.getComponent( ref );
113
114     ChildReference ref = ChildReference.create("node/v");
115     Accessor child = accessor.getComponent( ref );
116
117     VariantAccessor va = recordAccessor.getFieldAccessor("node");
118     Accessor child = va.getValueAccessor();
119 </pre>
120
121 ==Listening mechanism==
122 Accessor offers a monitoring mechanism for the data model. 
123 There is an <code>[../javadoc/org/simantics/databoard/accessor/interestset/InterestSet.html|InterestSet]</code> that is a description of a sub-tree that is to be monitored of the data model.
124 <code>[../javadoc/org/simantics/databoard/accessor/event/Event.html|Events]</code> 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.
125
126 Accessor Listeners use [EventThread Pattern] pattern.