Mentions légales du service

Skip to content
Snippets Groups Projects
Commit aec0f9c7 authored by Ludovic Le Frioux's avatar Ludovic Le Frioux
Browse files

First version of pseudo code implementation

parents
No related branches found
No related tags found
No related merge requests found
CObject 0 → 100644
class CObject<T> {
private id: CObjectId
private readOnly: Boolean
private crdt: DeltaCRDT<T>
private txnBackup: DeltaCRDT<T>?
CObject(oid: CObjectId, crdt: DeltaCRDT<T>, readOnly: Boolean) {
this.id = oid
this.crdt = crdt
this.txnBackup = null
}
package fun txnAbort() {
this.crdt = this.txnBackup
this.txnBackup = null
}
package fun txnCommit() {
this.txnBackup = null
}
fun update(args) {
if (NOT Global.DirtyCObjects.contains(this)) {
Global.DirtyCObjects.put(this.id, this)
this.txnBackup = this.crdt.copy()
}
ts = Global.TxnEnv.tick()
crdt.update(args, ts)
}
fun getter(args): val {
return crdt.getter(args)
}
fun close() {
Global.Cache.remove(this.id)
SERVICE.unsubscribe(this.id)
}
}
type CObjectId = (CollectionId, String, Type)
Cache 0 → 100644
class Cache {
private cobjects: Map<CObjectId, CObject>
fun getObject(oid: CObjectId) {
return cobjects.get(oid)
}
fun addObject(oid: CObjectId, obj: CObject) {
this.cobjects.put(oid. obj)
}
}
type ClientId = UUID
class Collection {
private id: CollectionId
private readOnly: Boolean
private static otherOpenned: Boolean = false
Collection(cid: CollectionId) {
if (otherOpenned == true) throw Exception
Collection.otherOpenned = true
this.id = cid
}
// c_open_read|write<T> handler?
// TODO: manage notifications
fun open<T>(oid: String, readOnly: Boolean, handler: (VersionVector, List<CObjectId>) -> Unit = {}): T {
oid = CObjectId(this.id, oid, T)
if (Global.Cache.contains(oid)) return Global.Cache.get(oid)
crdt = SERVICE.getObject(oid, TxnEnv.getState()) // SERVICE.subscribe(oid)
if (this.readOnly AND NOT readOnly) throw Exception
return CObject<T>(oid, crdt, readOnly)
}
fun close() {
for obj in Global.Cache {
SERVICE.unsubscribe(obj.id)
}
Global.Cache.clear()
Collection.otherOpenned = false
}
}
type CollectionId = String
enum ConsistencyLevel {
RC,
Snapshot,
Strong
}
Global 0 → 100644
TxnEnv: TransactionEnvironment
DirtyCObjects: Map<CObjectId, CObject>
Cache: Map<CObjectId, CObject>
OpId 0 → 100644
type OpId = (TxnId, Int)
Session 0 → 100644
class Session {
private consistency: ConsistencyLevel
private cliendId: CliendId
Session(cid: ClientId) {
this.clientId = cid
}
// c_begin_session
static fun open(dbName: String, credentials: String): Session {
clientId = ClientId()
if (NOT SERVICE.open(dbName, credentials, clientId)) throw Exception
return Session(clientId)
}
// c_pull_XX_view
fun pull(type: ConsistencyLevel) {
if (consistency != null AND consistency > type) throw Exception
this.consistency = type
v = SERVICE.pull(type)
Global.TxnEnv.updateState(v)
}
// c_pull_XX_view(v)
fun pull(type: ConsistencyLevel, vv: VersionVector) {
if (view != null AND view.type > type) throw Exception
if (vv < global.TxnEnv.getState()) throw Exception
v = SERVICE.pull(type, vv)
Global.TxnEnv.updateState(v)
}
// c_open_collection_read|write
fun openCollection(cid: CollectionId, readOnly: Boolean): Collection {
return Collection(cid)
}
// c_XX_txn
fun beginTransaction(body: () -> Unit) {
Transaction(body)
}
// c_end_session
fun close() {
SERVICE.close(clientId)
}
}
class Transaction {
private transactionId: String
private static otherOpenned: Boolean = false
private begin: VersionVector
Transaction(tid: TxnId, body: () -> Unit) {
if (Transaction.otherOpenned) throw Exception
Transaction.otherOpenned = true
this.begin = Global.TxnEnv.getState()
this.transactionId = Global.TxnEnv.tickTransaction()
try {
body()
this.commit()
} catch {
this.abort()
}
}
// c_abort_txn
fun abort() {
for obj in Global.DirtyCObjects {
obj.txnAbort()
}
Global.DirtyCObjects.clear()
Transaction.otherOpenned = false
}
// c_commit_txn
fun commit() {
SERVICE.beginTransaction(this.transactionId)
for obj in Global.DirtyCObjects {
SERVICE.updateObject(obj.crdt.getDelta(this.begin))
obj.txnCommit()
}
SERVICE.commitTransaction(this.transactionId)
Global.DirtyCObjects.clear()
Transaction.otherOpenned = false
}
}
TxnId 0 → 100644
type TxnId = (Int, ClientId)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment