1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Services for caching serialization engines
19 private import engine_tools
21 # A `Serializer` with a `cache`
22 abstract class CachingSerializer
25 # Cache of known objects
26 var cache
= new SerializerCache is lazy
, writable
28 # Link the cache of `self` with `deserializer`
30 # This allows reference objects by id when they are known by the other side
33 # Use `cache` if it is a `DuplexCache`, otherwise create a new one.
34 fun link
(deserializer
: CachingDeserializer)
37 if not mem
isa DuplexCache then mem
= new DuplexCache
40 deserializer
.cache
= mem
44 # A `Deserializer` with a `cache`
45 abstract class CachingDeserializer
48 # Cache of known objects
49 var cache
= new DeserializerCache is lazy
, writable
52 # Cache of sent objects
54 # Used by `Serializer` to avoid duplicating objects, by serializing them once,
55 # then using a reference.
57 # Map of already serialized objects to the reference id
58 protected var sent
: Map[Serializable, Int] = new StrictHashMap[Serializable, Int]
61 fun has_object
(object
: Serializable): Bool do return sent
.keys
.has
(object
)
63 # Get the id for `object`
65 # Require: `has_object(object)`
66 fun id_for
(object
: Serializable): Int
68 assert sent
.keys
.has
(object
)
72 # Get a new id for `object` and store it
74 # Require: `not has_object(object)`
75 fun new_id_for
(object
: Serializable): Int
77 var id
= next_available_id
82 # Get a free id to associate to an object in the cache
83 protected fun next_available_id
: Int do return sent
.length
86 # Cache of received objects sorted by there reference id
88 # Used by `Deserializer` to find already deserialized objects by their reference.
89 class DeserializerCache
90 # Map of references to already deserialized objects.
91 protected var received
: Map[Int, Object] = new StrictHashMap[Int, Object]
93 # Is there an object associated to `id`?
94 fun has_id
(id
: Int): Bool do return received
.keys
.has
(id
)
96 # Get the object associated to `id`
97 fun object_for
(id
: Int): nullable Object do return received
[id
]
99 # Associate `object` to `id`
100 fun []=(id
: Int, object
: Object) do received
[id
] = object
103 # A shared cache for serialization and deserialization
105 super SerializerCache
106 super DeserializerCache
108 redef fun new_id_for
(object
)
111 received
[id
] = object
115 redef fun []=(id
, object
)
118 assert object
isa Serializable
123 # A shared cache where 2 clients serialize objects at the same types, prevents references collision
127 # Should this end use even numbers?
130 private var last_id
: Int is lazy
do return if use_even
then 0 else 1
132 redef fun next_available_id