org.simantics.databoard.history
Class DirectoryMap

java.lang.Object
  extended by org.simantics.databoard.history.DirectoryMap
All Implemented Interfaces:
Accessor, MapAccessor

public class DirectoryMap
extends java.lang.Object
implements MapAccessor

DirectoryMap is a file backed map implementation where keys are filenames and values are corresponding files.

This class is an implmentation to Map(Variant, Variant) -Accessor. Filenames have the following encoding: S.dbb String types, if string doesn't have the following control characters " : < > | ? * \ / [0..31] I.dbb Integer types L.dbb Long types H.dbb All other cases the value as binary

File accessor is created if an entry opened as a sub-accessor. The file accessor is closed when all sub-accessors are released. The implementation is based on proxy instances and a reference queue. Once the queue is empty, file accessor is closed.

DirectoryMap must be closed with #close();

Author:
Toni Kalajainen

Nested Class Summary
 
Nested classes/interfaces inherited from interface org.simantics.databoard.accessor.Accessor
Accessor.Listener
 
Constructor Summary
DirectoryMap(java.io.File directory)
           
 
Method Summary
 void addListener(Accessor.Listener listener, InterestSet interestSet, AccessorReference path)
          Place a listener to an accessor node.
 void apply(java.util.List<Event> cs, java.util.LinkedList<Event> rollback)
          Apply a change set in a single transaction operation.
 void clear()
          Clear all entries.
 void close()
           
 boolean containsKey(Binding keyBinding, java.lang.Object key)
          Returns true if map has a value for specified key
 boolean containsValue(Binding valueBinding, java.lang.Object value)
          Returns true if map has a value for one or multiple keys
 java.lang.Object get(Binding keyBinding, java.lang.Object key, Binding valueBinding)
          Get the value that is specified for a key
<T extends Accessor>
T
getAccessor(AccessorReference reference)
          Get an accessor to a sub-container.
 void getAll(Binding keyBinding, Binding valueBinding, java.util.Map<java.lang.Object,java.lang.Object> to)
          Get all entries, write to a Java Collection
 void getAll(Binding keyBinding, Binding valueBinding, java.lang.Object[] keys, java.lang.Object[] values)
          Get all entries in order, write to 2 arrays.
 Variant getAsVariant(Binding keyBinding, java.lang.Object key)
          Get the value as a variant
 java.lang.Object getCeilingKey(Binding keyBinding, java.lang.Object key)
           
 FileVariantAccessor getExistingAccessor(Binding keyBinding, java.lang.Object key)
           
 java.lang.Object getFirstKey(Binding keyBinding)
           
 java.lang.Object getFloorKey(Binding keyBinding, java.lang.Object key)
           
 java.lang.Object getHigherKey(Binding keyBinding, java.lang.Object key)
           
 java.lang.Object[] getKeys(Binding keyBinding)
          Get all keys in order
 java.lang.Object getLastKey(Binding keyBinding)
           
 java.lang.Object getLowerKey(Binding keyBinding, java.lang.Object key)
           
 java.lang.Object getValue(Binding binding)
          Get a snapshot of the object model as a single data value.
 FileVariantAccessor getValueAccessor(Binding keyBinding, java.lang.Object key)
          Get an accessor to a value.
 java.lang.Object[] getValues(Binding valueBinding)
          Get all values
 void put(Binding keyBinding, java.lang.Object key, Binding valueBinding, java.lang.Object value)
          Put an entry to the map.
 void putAll(Binding keyBinding, Binding valueBinding, java.util.Map<java.lang.Object,java.lang.Object> from)
          Put entries from a map.
 void putAll(Binding keyBinding, Binding valueBinding, java.lang.Object[] keys, java.lang.Object[] values)
          Put entries from a map.
 void remove(Binding keyBinding, java.lang.Object key)
          Remove an entry.
 void removeListener(Accessor.Listener listener)
          Remove a listener.
 void setValue(Binding mapBinding, java.lang.Object newMap)
          Set & copy all entries from a Map Value.
 int size()
          Get the number of elements in the map
 java.lang.String toString()
           
 MapType type()
          Get structural represtentation of the accessor presented in databoard's type system format.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DirectoryMap

public DirectoryMap(java.io.File directory)
Method Detail

close

public void close()

type

public MapType type()
Description copied from interface: Accessor
Get structural represtentation of the accessor presented in databoard's type system format.

Specified by:
type in interface Accessor
Specified by:
type in interface MapAccessor
Returns:
type description

clear

public void clear()
           throws AccessorException
Description copied from interface: MapAccessor
Clear all entries. If there is an accessor to a removed container, it becomes invalid.

Specified by:
clear in interface MapAccessor
Throws:
AccessorException

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

getValue

public java.lang.Object getValue(Binding binding)
                          throws AccessorException
Description copied from interface: Accessor
Get a snapshot of the object model as a single data value.

Specified by:
getValue in interface Accessor
Returns:
the value
Throws:
AccessorException

containsKey

public boolean containsKey(Binding keyBinding,
                           java.lang.Object key)
                    throws AccessorException
Description copied from interface: MapAccessor
Returns true if map has a value for specified key

Specified by:
containsKey in interface MapAccessor
Returns:
true if entry exists
Throws:
AccessorException

containsValue

public boolean containsValue(Binding valueBinding,
                             java.lang.Object value)
                      throws AccessorException
Description copied from interface: MapAccessor
Returns true if map has a value for one or multiple keys

Specified by:
containsValue in interface MapAccessor
Returns:
true if value exists
Throws:
AccessorException

get

public java.lang.Object get(Binding keyBinding,
                            java.lang.Object key,
                            Binding valueBinding)
                     throws AccessorException
Description copied from interface: MapAccessor
Get the value that is specified for a key

Specified by:
get in interface MapAccessor
Returns:
the value or null
Throws:
AccessorException

getAsVariant

public Variant getAsVariant(Binding keyBinding,
                            java.lang.Object key)
                     throws AccessorException
Get the value as a variant

Parameters:
keyBinding -
key -
Returns:
value
Throws:
AccessorException

getAll

public void getAll(Binding keyBinding,
                   Binding valueBinding,
                   java.util.Map<java.lang.Object,java.lang.Object> to)
            throws AccessorException
Description copied from interface: MapAccessor
Get all entries, write to a Java Collection

Specified by:
getAll in interface MapAccessor
Throws:
AccessorException

getAll

public void getAll(Binding keyBinding,
                   Binding valueBinding,
                   java.lang.Object[] keys,
                   java.lang.Object[] values)
            throws AccessorException
Description copied from interface: MapAccessor
Get all entries in order, write to 2 arrays.

Specified by:
getAll in interface MapAccessor
Throws:
AccessorException

getCeilingKey

public java.lang.Object getCeilingKey(Binding keyBinding,
                                      java.lang.Object key)
                               throws AccessorException
Specified by:
getCeilingKey in interface MapAccessor
Throws:
AccessorException

getFirstKey

public java.lang.Object getFirstKey(Binding keyBinding)
                             throws AccessorException
Specified by:
getFirstKey in interface MapAccessor
Throws:
AccessorException

getFloorKey

public java.lang.Object getFloorKey(Binding keyBinding,
                                    java.lang.Object key)
                             throws AccessorException
Specified by:
getFloorKey in interface MapAccessor
Throws:
AccessorException

getHigherKey

public java.lang.Object getHigherKey(Binding keyBinding,
                                     java.lang.Object key)
                              throws AccessorException
Specified by:
getHigherKey in interface MapAccessor
Throws:
AccessorException

getKeys

public java.lang.Object[] getKeys(Binding keyBinding)
                           throws AccessorException
Description copied from interface: MapAccessor
Get all keys in order

Specified by:
getKeys in interface MapAccessor
Returns:
an array or keys
Throws:
AccessorException

getLastKey

public java.lang.Object getLastKey(Binding keyBinding)
                            throws AccessorException
Specified by:
getLastKey in interface MapAccessor
Throws:
AccessorException

getLowerKey

public java.lang.Object getLowerKey(Binding keyBinding,
                                    java.lang.Object key)
                             throws AccessorException
Specified by:
getLowerKey in interface MapAccessor
Throws:
AccessorException

getExistingAccessor

public FileVariantAccessor getExistingAccessor(Binding keyBinding,
                                               java.lang.Object key)
                                        throws AccessorConstructionException
Throws:
AccessorConstructionException

getValueAccessor

public FileVariantAccessor getValueAccessor(Binding keyBinding,
                                            java.lang.Object key)
                                     throws AccessorConstructionException
Description copied from interface: MapAccessor
Get an accessor to a value. It becomes invalid if the entry is removed or overwritten with a new value.

Specified by:
getValueAccessor in interface MapAccessor
Returns:
value accessor
Throws:
AccessorConstructionException

getValues

public java.lang.Object[] getValues(Binding valueBinding)
                             throws AccessorException
Description copied from interface: MapAccessor
Get all values

Specified by:
getValues in interface MapAccessor
Returns:
an array of values
Throws:
AccessorException

put

public void put(Binding keyBinding,
                java.lang.Object key,
                Binding valueBinding,
                java.lang.Object value)
         throws AccessorException
Description copied from interface: MapAccessor
Put an entry to the map. If previous entry exists for the key, it is removed.

Specified by:
put in interface MapAccessor
Throws:
AccessorException

putAll

public void putAll(Binding keyBinding,
                   Binding valueBinding,
                   java.util.Map<java.lang.Object,java.lang.Object> from)
            throws AccessorException
Description copied from interface: MapAccessor
Put entries from a map. Replaces any possible existing entry.

Specified by:
putAll in interface MapAccessor
Throws:
AccessorException

putAll

public void putAll(Binding keyBinding,
                   Binding valueBinding,
                   java.lang.Object[] keys,
                   java.lang.Object[] values)
            throws AccessorException
Description copied from interface: MapAccessor
Put entries from a map. Replaces any possible existing entry.

Specified by:
putAll in interface MapAccessor
Throws:
AccessorException

remove

public void remove(Binding keyBinding,
                   java.lang.Object key)
            throws AccessorException
Description copied from interface: MapAccessor
Remove an entry. If there is an accessor, it becomes invalid.

Specified by:
remove in interface MapAccessor
Throws:
AccessorException

setValue

public void setValue(Binding mapBinding,
                     java.lang.Object newMap)
              throws AccessorException
Description copied from interface: MapAccessor
Set & copy all entries from a Map Value. If entries are removed and they have accessors, the accessors are invalidated.

Specified by:
setValue in interface Accessor
Specified by:
setValue in interface MapAccessor
Throws:
AccessorException

size

public int size()
         throws AccessorException
Description copied from interface: MapAccessor
Get the number of elements in the map

Specified by:
size in interface MapAccessor
Returns:
size
Throws:
AccessorException

addListener

public void addListener(Accessor.Listener listener,
                        InterestSet interestSet,
                        AccessorReference path)
                 throws AccessorException
Description copied from interface: Accessor
Place a listener to an accessor node. The listener will be notified for changes in the node or posssibly its decendant nodes, depending on interest set.

The synchronization contract is implementation specific, so read the document. A common contract is that the listener object must not make modifications to the accessor within the handle event function. The correct strategy is to collect events and mutate the accessor afterwards.

Each event instance spawned by the listener, is annotated with a reference that describes the reference path of the node to which the event applies to. The path originates from the node where the listener was placed.

Listener is attached to the container that holds the value at the time at the of the adding. For example, If a listener is attached to an array of element at index 3, and a new value is inserted at position 2, the listener still monitors the same container, which is now at index 4. The references of incoming the events are modified to have the new index.

Also, if a new value is assigned to the parent of an object that is listened, the listener keeps on monitoring the new value at the same reference. This doesn't apply when a new value is set to a union of different tag, to a variant with a new type, or value is removed from Optional type. In these two cases the listener is invalidated.

See ChangeSet is an implementation that collects events.

Specified by:
addListener in interface Accessor
path - path to the accessor or null. This is used in the events the accessor produces
Throws:
AccessorException
See Also:
collects events

apply

public void apply(java.util.List<Event> cs,
                  java.util.LinkedList<Event> rollback)
           throws AccessorException
Description copied from interface: Accessor
Apply a change set in a single transaction operation. If rollback log is supplied, it is filled with reverse events. If the operation fails, rollback log can be applied to cancel changes.

Specified by:
apply in interface Accessor
rollback - log to be filled with rollback events or null
Throws:
AccessorException - failed to apply change set

getAccessor

public <T extends Accessor> T getAccessor(AccessorReference reference)
                               throws AccessorConstructionException
Description copied from interface: Accessor
Get an accessor to a sub-container. If one already exists, the same is returned, otherwise a new is created.

InvalidatedEvent is thrown from the accessor if it is unlinked from the parent hierarchy.

Specified by:
getAccessor in interface Accessor
Parameters:
reference - component reference or null to return _this_ accessor
Returns:
accessor
Throws:
AccessorConstructionException

removeListener

public void removeListener(Accessor.Listener listener)
                    throws AccessorException
Description copied from interface: Accessor
Remove a listener. If the listener is added multiple times, the last one added is removed.

Specified by:
removeListener in interface Accessor
Throws:
AccessorException