From 1dab00e0bbacbd08730f23860f8b1b5ce0e3ab64 Mon Sep 17 00:00:00 2001
From: Ludovic Le Frioux <llefriou@po461-pro.paris.inria.fr>
Date: Mon, 28 Sep 2020 16:03:52 +0200
Subject: [PATCH] Better integration of the c-crdtlib

---
 CObject                               |  6 +++---
 Collection                            |  8 +++----
 Session                               | 31 +++++++++++++++------------
 Transaction                           | 14 ++++++------
 utils/CObjectId                       |  2 --
 utils/CObjectUId                      |  2 ++
 utils/ClientId                        |  2 --
 utils/{CollectionId => CollectionUId} |  2 +-
 utils/NotificationHandler             |  4 +++-
 utils/OperationId                     |  2 --
 utils/OperationUId                    |  2 ++
 utils/TransactionEnvironment          |  5 +++++
 utils/TxnId                           |  2 --
 utils/TxnUId                          |  4 ++++
 14 files changed, 49 insertions(+), 37 deletions(-)
 delete mode 100644 utils/CObjectId
 create mode 100644 utils/CObjectUId
 delete mode 100644 utils/ClientId
 rename utils/{CollectionId => CollectionUId} (54%)
 delete mode 100644 utils/OperationId
 create mode 100644 utils/OperationUId
 create mode 100644 utils/TransactionEnvironment
 delete mode 100644 utils/TxnId
 create mode 100644 utils/TxnUId

diff --git a/CObject b/CObject
index 6f43467..03fade9 100644
--- a/CObject
+++ b/CObject
@@ -1,7 +1,7 @@
 class CObject<T> {
 
-  // Concordant object id
-  private id: CObjectId
+  // Concordant object unique id
+  private id: CObjectUId
 
   // Is openned in read only mode
   private readOnly: Boolean
@@ -12,7 +12,7 @@ class CObject<T> {
   // Backup for the necapsulated CRDT (used in case of transaction abort)
   private txnBackup: DeltaCRDT<T>?
 
-  CObject(oid: CObjectId, crdt: DeltaCRDT<T>, readOnly: Boolean) {
+  CObject(oid: CObjectUId, crdt: DeltaCRDT<T>, readOnly: Boolean) {
     this.id = oid
     this.crdt = crdt
     this.readOnly = readOnly
diff --git a/Collection b/Collection
index c6939da..f6e6a35 100644
--- a/Collection
+++ b/Collection
@@ -1,7 +1,7 @@
 class Collection {
 
-  // The collection id
-  private id: CollectionId
+  // The collection unique id
+  private id: CollectionUId
 
   // Is the collection open in read only mode
   private readOnly: Boolean
@@ -9,7 +9,7 @@ class Collection {
   // Static boolean used to ensure that only one collection is openned
   private static otherOpenned: Boolean = false
 
-  Collection(cid: CollectionId, readOnly: Boolean) {
+  Collection(cid: CollectionUId, readOnly: Boolean) {
     if (Collection.otherOpenned == true) throw Exception
     Collection.otherOpenned = true
     this.id = cid
@@ -21,7 +21,7 @@ class Collection {
     if (this.readOnly AND NOT readOnly) throw Exception
     if (NOT Session.currentTransaction == null) throw Exception
 
-    oid = CObjectId(this.id, oid, T)
+    oid = CObjectUId(this.id, oid, T)
     if (ActiveSession.cache.contains(oid)) return ActiveSession.cache.get(oid)
 
     crdt, v = SERVICE.getObject(oid, ActiveSession.txnEnv.getState())
diff --git a/Session b/Session
index 12a7506..ce5ea4b 100644
--- a/Session
+++ b/Session
@@ -1,7 +1,10 @@
+import c-crdlib.utils.ClientUId
+import c-crdtlib.utils.VersionVector
+
 class Session {
 
-  // The client id
-  private cliendId: CliendId
+  // The client unique id
+  private cliendUId: CliendUId
 
   // Current consistency level
   private consistency: ConsistencyLevel?
@@ -12,21 +15,21 @@ class Session {
   // Current running transaction
   private currentTransaction: Transaction?
 
-  // Transaction environment should inherit from crdtlib.utils.Environment 
+  // Transaction environment
   package txnEnv: TransactionEnvironment
 
   // Map for dirty Concordant objects
-  package dirtyObjects: Map<CObjectId, CObject>
+  package dirtyObjects: Map<CObjectUId, CObject>
 
   // Map for the cache of Concordant objects
-  package cache: Map<CObjectId, CObject>
+  package cache: Map<CObjectUId, CObject>
 
   // Map for Concordant object handlers management
-  package handlers: Map<CObjectId, NotificationHandler>
+  package handlers: Map<CObjectUId, NotificationHandler>
 
-  private Session(cid: ClientId) {
-    this.clientId = cid
-    this.txnEnv = TransactionEnvironment(this.clientId)
+  private Session(cid: ClientUId) {
+    this.clientUId = cid
+    this.txnEnv = TransactionEnvironment(this.clientUId)
     this.dirtyObjects = mutableMapOf()
     this.cache = mutableMapOf()
     this.handlers = mutableMapOf()
@@ -38,9 +41,9 @@ class Session {
   // c_begin_session
   static fun connect(dbName: String, credentials: String): Session {
     if (NOT ActiveSession == null) throw Exception
-    clientId = ClientId()
-    if (NOT SERVICE.connect(dbName, credentials, clientId)) throw Exception
-    ActiveSession = Session(clientId)
+    clientId = ClientUId()
+    if (NOT SERVICE.connect(dbName, credentials, clientUId)) throw Exception
+    ActiveSession = Session(clientUId)
     retur ActiveSession
   }
 
@@ -72,7 +75,7 @@ class Session {
   }
   
   // c_open_collection_read|write
-  fun openCollection(cid: CollectionId, readOnly: Boolean): Collection {
+  fun openCollection(cid: CollectionUId, readOnly: Boolean): Collection {
     if (NOT this.currentTransaction == null) throw Exception
     return Collection(cid, readOnly)
   }
@@ -86,7 +89,7 @@ class Session {
   // c_end_session
   fun close() {
     if (this.currentTransaction == null) this.currentTransaction.abort()
-    SERVICE.close(clientId)
+    SERVICE.close(clientUId)
     this.serviceHandler.stop()
     ActiveSession = null
   }
diff --git a/Transaction b/Transaction
index ffe0d61..9b19095 100644
--- a/Transaction
+++ b/Transaction
@@ -1,14 +1,16 @@
+import c-crdtlib.utils.VersionVector
+
 class Transaction {
 
-  // Transaction id
-  private transactionId: TxnId
+  // Transaction unique id
+  private transactionUId: TxnUId
 
   // The version vector at which the transaction has started
   private begin: VersionVector
 
-  Transaction(tid: TxnId, body: TransactionBody) {
+  Transaction(tid: TxnUId, body: TransactionBody) {
     this.begin = ActiveSession.txnEnv.getState()
-    this.transactionId = ActiveSession.txnEnv.tickTransaction()
+    this.transactionUId = ActiveSession.txnEnv.tickTransaction()
 
     try {
       // call the body function
@@ -31,12 +33,12 @@ class Transaction {
 
   // c_commit_txn
   fun commit() {
-    SERVICE.beginTransaction(this.transactionId)
+    SERVICE.beginTransaction(this.transactionUId)
     for obj in ActiveSession.dirtyObjects {
       SERVICE.pushObject(obj.crdt.getDelta(this.begin))
       obj.txnCommit()
     }
-    SERVICE.commitTransaction(this.transactionId)
+    SERVICE.commitTransaction(this.transactionUId)
     ActiveSession.dirtyObjects.clear()
   }
 }
diff --git a/utils/CObjectId b/utils/CObjectId
deleted file mode 100644
index 8bde73e..0000000
--- a/utils/CObjectId
+++ /dev/null
@@ -1,2 +0,0 @@
-// Concordant object unique identifiers
-type CObjectId = (CollectionId, String, Type)
diff --git a/utils/CObjectUId b/utils/CObjectUId
new file mode 100644
index 0000000..d383ac4
--- /dev/null
+++ b/utils/CObjectUId
@@ -0,0 +1,2 @@
+// Concordant object unique identifiers
+type CObjectUId = (CollectionUId, String, Type)
diff --git a/utils/ClientId b/utils/ClientId
deleted file mode 100644
index a591f0b..0000000
--- a/utils/ClientId
+++ /dev/null
@@ -1,2 +0,0 @@
-// Client unique identifiers are UUId, they can also be hierarchic
-type ClientId = UUId
diff --git a/utils/CollectionId b/utils/CollectionUId
similarity index 54%
rename from utils/CollectionId
rename to utils/CollectionUId
index 330f9c2..2d976d2 100644
--- a/utils/CollectionId
+++ b/utils/CollectionUId
@@ -1,2 +1,2 @@
 // Collection unique identifiers
-type CollectionId = String
+type CollectionUId = String
diff --git a/utils/NotificationHandler b/utils/NotificationHandler
index d905e70..0bbb846 100644
--- a/utils/NotificationHandler
+++ b/utils/NotificationHandler
@@ -1,3 +1,5 @@
+import c-crdtlib.utils.VersionVector
+
 // The notification handler (function) type accepted for c-objects
 // Unit == void in Kotlin
-type NotificationHandler = (VersionVector, CObjectId) -> Unit
+type NotificationHandler = (VersionVector, CObjectUId) -> Unit
diff --git a/utils/OperationId b/utils/OperationId
deleted file mode 100644
index 6b954f5..0000000
--- a/utils/OperationId
+++ /dev/null
@@ -1,2 +0,0 @@
-// Operations unique identifiers
-type OperationId = (TxnId, Int)
diff --git a/utils/OperationUId b/utils/OperationUId
new file mode 100644
index 0000000..65d7744
--- /dev/null
+++ b/utils/OperationUId
@@ -0,0 +1,2 @@
+// Operations unique identifiers
+type OperationUId = (TxnUId, Int)
diff --git a/utils/TransactionEnvironment b/utils/TransactionEnvironment
new file mode 100644
index 0000000..6b2fb10
--- /dev/null
+++ b/utils/TransactionEnvironment
@@ -0,0 +1,5 @@
+import c-crdtlin.utils.Environment
+
+class TransactionEnvironment : Environment {
+  // TODO: implement this class
+}
diff --git a/utils/TxnId b/utils/TxnId
deleted file mode 100644
index f262855..0000000
--- a/utils/TxnId
+++ /dev/null
@@ -1,2 +0,0 @@
-// Transaction unique identifiers
-type TxnId = (Int, ClientId)
diff --git a/utils/TxnUId b/utils/TxnUId
new file mode 100644
index 0000000..6da5137
--- /dev/null
+++ b/utils/TxnUId
@@ -0,0 +1,4 @@
+import c-crdtlib.utils.ClientUId
+
+// Transaction unique identifiers
+type TxnUId = (Int, ClientUId)
-- 
GitLab