lib/serialization: intro AsyncCache
authorAlexis Laferrière <alexis.laf@xymus.net>
Fri, 31 Jul 2015 12:57:36 +0000 (08:57 -0400)
committerAlexis Laferrière <alexis.laf@xymus.net>
Tue, 4 Aug 2015 15:33:14 +0000 (11:33 -0400)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

lib/serialization/caching.nit

index 186c96c..4541428 100644 (file)
@@ -74,10 +74,13 @@ class SerializerCache
        # Require: `not has_object(object)`
        fun new_id_for(object: Serializable): Int
        do
-               var id = sent.length
+               var id = next_available_id
                sent[object] = id
                return id
        end
+
+       # Get a free id to associate to an object in the cache
+       protected fun next_available_id: Int do return sent.length
 end
 
 # Cache of received objects sorted by there reference id
@@ -116,3 +119,19 @@ class DuplexCache
                sent[object] = id
        end
 end
+
+# A shared cache where 2 clients serialize objects at the same types, prevents references collision
+class AsyncCache
+       super DuplexCache
+
+       # Should this end use even numbers?
+       var use_even: Bool
+
+       private var last_id: Int is lazy do return if use_even then 0 else 1
+
+       redef fun next_available_id
+       do
+               last_id += 2
+               return last_id
+       end
+end