1 /*******************************************************************************
\r
2 * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.
\r
3 * All rights reserved. This program and the accompanying materials
\r
4 * are made available under the terms of the Eclipse Public License v1.0
\r
5 * which accompanies this distribution, and is available at
\r
6 * http://www.eclipse.org/legal/epl-v10.html
\r
9 * VTT Technical Research Centre of Finland - initial API and implementation
\r
10 *******************************************************************************/
\r
11 package org.simantics.databoard.example;
\r
13 import java.io.File;
\r
14 import java.util.Arrays;
\r
16 import org.simantics.databoard.Accessors;
\r
17 import org.simantics.databoard.Bindings;
\r
18 import org.simantics.databoard.Datatypes;
\r
19 import org.simantics.databoard.accessor.file.FileArrayAccessor;
\r
20 import org.simantics.databoard.accessor.file.FileVariantAccessor;
\r
21 import org.simantics.databoard.binding.Binding;
\r
22 import org.simantics.databoard.binding.mutable.MutableVariant;
\r
23 import org.simantics.databoard.serialization.Serializer;
\r
24 import org.simantics.databoard.type.ArrayType;
\r
26 public class FileAccessorExample {
\r
28 public static void main(String[] args) throws Exception {
\r
31 // File Accessor is an access to data that resides in a file in
\r
35 // Create new binary file
\r
36 File file = File.createTempFile("variable", ".dbb");
\r
37 // Create file and open an accessor
\r
38 // Files are always variants, so the root accessor is VariantAccessor.
\r
39 FileVariantAccessor fa = Accessors.createFile(file);
\r
41 // We can put any value into the file
\r
42 fa.setContentValue( Bindings.STRING, "Hello World!" );
\r
43 // Or change it to completely another type
\r
44 fa.setContentValue( Bindings.INTEGER, 500 );
\r
46 // Values are normally flushed automatically
\r
47 // If special *Noflush methods are used...
\r
48 fa.setContentValueNoflush( Bindings.DOUBLE, 99.0 );
\r
49 // then a separate cache flush is required.
\r
57 // Re-open the file, get an accessor
\r
58 fa = Accessors.openAccessor(file);
\r
59 // Lets read what the type is
\r
60 System.out.println( fa.getContentType() );
\r
61 // Read the content, we know its a Double so we use an instance of DoubleBinding
\r
62 Double value = (Double) fa.getContentValue( Bindings.DOUBLE );
\r
63 System.out.println( value );
\r
67 // Large datasets can be accessed partially
\r
68 // It conserves memory.
\r
70 // Initialize the file into a Float[]
\r
71 ArrayType floatArrayType = new ArrayType( Datatypes.FLOAT );
\r
72 Binding binding = Bindings.getMutableBinding( floatArrayType );
\r
73 fa.setContentValue( binding, binding.createDefault() );
\r
76 // Get a sub-accessor to content of the file.
\r
77 FileArrayAccessor aa = (FileArrayAccessor) fa.getContentAccessor();
\r
78 // Add 1024 entries one by one
\r
79 for (int i=0; i<1024; i++) {
\r
81 aa.addNoflush( Bindings.FLOAT, v );
\r
83 // Flushing ensures bytes are moved from memory cache to disc
\r
84 // Note, reading works without flushing
\r
88 System.out.print("[");
\r
89 for (int i=0; i<aa.size(); i++) {
\r
90 System.out.print( aa.get(i, Bindings.FLOAT) );
\r
91 System.out.print( ", ");
\r
93 System.out.println("]");
\r
96 binding = Bindings.getBinding( float[].class );
\r
97 float data[] = (float[]) aa.getValue( binding );
\r
98 System.out.println( Arrays.toString(data) );
\r
100 // File handle can be closed from any of its accessors (fa and aa)
\r
106 // To read the data all-at-once an accessor is not needed, you can use Serializer aswell
\r
108 // The root of the file is always Variant so use an instance of VariantBinding
\r
109 Serializer s = Bindings.getSerializerUnchecked( Bindings.MUTABLE_VARIANT );
\r
110 MutableVariant variant = (MutableVariant) s.deserialize( file );
\r
111 data = (float[]) variant.getValue( binding );
\r
112 System.out.println( Arrays.toString(data) );
\r