Exemple #1
0
        private List <ConnectionLock> ReadActiveLocks(DataStore dstore)
        {
            var lockList = new List <ConnectionLock>();

            try {
                foreach (var lockRecord in DataFunctions.ReadAllRecords(dstore, connDbName, connDbLocksTableName))
                {
                    lockList.Add(new ConnectionLock(
                                     lockRecord[connDbLockSourceField] as string,
                                     lockRecord[connDbLockRequesterField] as string,
                                     lockRecord[connDbLockConnUUIDField] as string,
                                     lockRecord[connDbLockDBField] as string,
                                     lockRecord[connDbLockTableField] as string,
                                     lockRecord[connDbLockRecordKeyField],
                                     lockId: lockRecord[connDbLockUUIDField] as string
                                     ));
                }
            }
            catch (Exception ex) {
                // throw an exception if the lock table is not configured
                throw new Exception(
                          string.Format("Transaction database has not been configured. | {0}", ex.Message)
                          );
            }

            return(lockList);
        }
Exemple #2
0
 public void DropRecord(string dbName, string tableName, object recordKey)
 {
     try {
         TXN_BEGIN(dbName: dbName, tableName: tableName, recordKey: recordKey);
         DataFunctions.DropRecord(activeDStore, dbName, tableName, recordKey);
     }
     finally {
         TXN_END();
     }
 }
Exemple #3
0
 public void CreateTable(string dbName, string tableName, TableDefinition tableDef)
 {
     try {
         TXN_BEGIN(dbName: dbName, tableName: tableName);
         DataFunctions.CreateTable(activeDStore, dbName, tableName, tableDef);
     }
     finally {
         TXN_END();
     }
 }
Exemple #4
0
 // CRUD functions
 public void CreateDB(string dbName, DatabaseDefinition dbDef = null)
 {
     try {
         TXN_BEGIN(dbName: dbName);
         DataFunctions.CreateDB(activeDStore, dbName, dbDef);
     }
     finally {
         TXN_END();
     }
 }
Exemple #5
0
 public void InsertRecord(string dbName, string tableName, object recordKey, IDictionary <string, object> fieldValues)
 {
     try {
         TXN_BEGIN(dbName: dbName, tableName: tableName, recordKey: recordKey);
         DataFunctions.InsertRecord(activeDStore, dbName, tableName, recordKey, fieldValues);
     }
     finally {
         TXN_END();
     }
 }
Exemple #6
0
        private void VerifyLocksTable(DataStore dstore)
        {
            logger.Debug("VerifyLocksTable");

            // ensure connection database
            DatabaseDefinition connDbDef;

            try {
                // if db exists we're good
                connDbDef = DataFunctions.ReadDB(dstore, connDbName);
            }
            catch {
                // otherwise create it
                var newConnDbDef = new DatabaseDefinition(dbName: connDbName)
                {
                    Description = "Internal DB"
                };

                // create the connection management database
                DataFunctions.CreateDB(dstore, connDbName, newConnDbDef);
            }

            // ensue locks table
            TableDefinition locksTableDef;

            try {
                // if table exists we're good
                locksTableDef = DataFunctions.ReadTable(dstore, connDbName, connDbLocksTableName);
            }
            catch {
                // otherwise create it
                // locks have fields for dbname, tablename and record keys but they're not wired
                // - to allow implicit locking of nonexisting entries
                // - to remove the need to update wires when new tables and dbs are created
                var newlocksTableDef = new TableDefinition()
                {
                    Fields = new List <Field>()
                    {
                        new UUIDField(connDbLockUUIDField),
                        new TextField(connDbLockSourceField),
                        new TextField(connDbLockRequesterField),
                        new UUIDField(connDbLockConnUUIDField),
                        new TextField(connDbLockDBField),
                        new TextField(connDbLockTableField),
                        new TextField(connDbLockRecordKeyField),
                    },
                    Description     = "Locks Table",
                    IsHidden        = true,
                    SupportsHistory = false
                };

                // create transaction locks table
                DataFunctions.CreateTable(dstore, connDbName, connDbLocksTableName, newlocksTableDef);
            }
        }
Exemple #7
0
        private void VerifyDataStoreType(DataStore dstore)
        {
            logger.Debug("VerifyDataStoreType");

            DataStoreType existingDStoreType = null;

            try {
                existingDStoreType = DataFunctions.ReadDataStoreType(dstore);

                if (!existingDStoreType.Equals(dstore.DataStoreType))
                {
                    throw new Exception(
                              string.Format("Requested Datastore type \"{0}\" does not match existing \"{1}\"",
                                            dstore.DataStoreType, existingDStoreType)
                              );
                }
            }
            catch (FormatException) {
                // there is no datastore so there is no point in opening a txn
                DataFunctions.CreateDataStore(dstore);
            }
        }
Exemple #8
0
        private void ReleaseLocks()
        {
            logger.Debug("RECORD UN-LOCK");

            // open datastore for exclusive access
            using (var dstore = new DataStore(ActiveDataStoreType, exclusive: true)) {
                try {
                    // get all existing locks
                    // release locks belonging to this connection
                    foreach (ConnectionLock activeLock in ReadActiveLocks(dstore))
                    {
                        logger.Debug("Lock found: {0}", activeLock);
                        if (activeLock.BelongsTo(this))
                        {
                            logger.Debug("Dropping lock: {0}", activeLock);
                            DataFunctions.DropRecord(dstore, connDbName, connDbLocksTableName, activeLock.LockId);
                        }
                    }
                }
                catch (Exception dropEx) {
                    // finally statement closes the dstore and release the exclusivity if throwing an exception
                    throw new Exception(
                              string.Format("Exception occured when dropping lock records. | {0}", dropEx.Message)
                              );
                }
                finally {
                    try {
                        // write changes
                        dstore.Commit();
                    }
                    catch (Exception commitEx) {
                        // finally statement closes the dstore and release the exclusivity if throwing an exception
                        throw new Exception(
                                  string.Format("Exception occured when commiting released locks. | {0}", commitEx.Message)
                                  );
                    }
                }
            }
        }
Exemple #9
0
        private void AcquireLocks(ConnectionLock newLock)
        {
            logger.Debug("RECORD LOCK");

            // open datastore for exclusive access
            using (var dstore = new DataStore(ActiveDataStoreType, exclusive: true)) {
                // get all existing locks
                // check access, find the highest-level compatible lock
                ConnectionLock txnLock = newLock;
                foreach (ConnectionLock activeLock in ReadActiveLocks(dstore))
                {
                    logger.Debug("Lock Found: {0}", activeLock);
                    // is existing lock restricting access?
                    if (newLock.IsRestrictedBy(activeLock))
                    {
                        //  does restricting lock belong to this connection?
                        //  if yes and it is a higher-level lock, use that
                        if (activeLock.BelongsTo(this) && activeLock.LockType <= newLock.LockType)
                        {
                            logger.Debug("Existing lock {0} is higher than new {1}", activeLock, newLock);
                            txnLock = activeLock;
                        }
                        //  does the lock not belong to this connection in which case reject
                        //  or is trying to acquire a higher level lock that active lock
                        else
                        {
                            logger.Debug("New lock {0} is restricted by {1}", newLock, activeLock);
                            throw new AccessRestrictedException(newLock, activeLock);
                        }
                    }
                    // if not restricting, does this connection already have a txn block lock of equal level
                    // reject again since the connection need to close its block lock first and then
                    // work on another entry
                    else
                    {
                        if (activeLock.BelongsTo(this))
                        {
                            logger.Debug("New lock {0} not on the same path as existing {1}", newLock, activeLock);
                            throw new AccessRestrictedByExistingLockException(newLock, activeLock);
                        }
                    }
                }

                logger.Debug("Using lock: {0}", txnLock);
                // at this point, the are no locks that restrict access
                // but we might be re-using an active compatible lock
                // so only write the lock if is not existing compatible
                if (txnLock == newLock)
                {
                    try {
                        // register the new lock: if target is not locked, lock it
                        DataFunctions.InsertRecord(
                            dstore,
                            connDbName,
                            connDbLocksTableName,
                            newLock.LockId,
                            new Dictionary <string, object>()
                        {
                            { connDbLockSourceField, newLock.LockSource },
                            { connDbLockRequesterField, newLock.LockRequester },
                            { connDbLockConnUUIDField, newLock.LockConnId },
                            { connDbLockDBField, newLock.LockTargetDB },
                            { connDbLockTableField, newLock.LockTargetTable },
                            { connDbLockRecordKeyField, newLock.LockTargetRecordKey },
                        });
                    }
                    catch (Exception lockEx) {
                        // finally statement closes the dstore and release the exclusivity if throwing an exception
                        throw new Exception(
                                  string.Format("Error creating lock: {0} | {1}", newLock, lockEx.Message)
                                  );
                    }
                    finally {
                        try {
                            // write changes
                            dstore.Commit();
                        }
                        catch (Exception commitEx) {
                            // finally statement closes the dstore and release the exclusivity if throwing an exception
                            throw new Exception(
                                      string.Format("Exception occured when commiting new locks. | {0}", commitEx.Message)
                                      );
                        }
                    }
                }
            }
        }
Exemple #10
0
        private ConnectionLock AcquireLocks(ConnectionLock newLock)
        {
            logger.Debug("RECORD LOCK");

            ConnectionLock txnLock = newLock;

            // open datastore for exclusive access
            using (var dstore = new DataStore(ActiveDataStoreType, exclusive: true)) {
                // get all existing locks
                // check access, find the highest-level compatible lock
                logger.Debug("Reading existing locks");
                foreach (ConnectionLock activeLock in ReadActiveLocks(dstore))
                {
                    logger.Debug("Lock Found: {0}", activeLock);
                    txnLock = DetermineHigherLock(activeLock, newLock);
                }

                logger.Debug("Using lock: {0}", txnLock);
                // at this point, the are no locks that restrict access
                // but we might be re-using an active compatible lock
                // so only write the lock if is not existing compatible
                if (txnLock == newLock)
                {
                    try {
                        // register the new lock: if target is not locked, lock it
                        DataFunctions.InsertRecord(
                            dstore,
                            connDbName,
                            connDbLocksTableName,
                            newLock.LockId,
                            new Dictionary <string, object>()
                        {
                            { connDbLockSourceField, newLock.LockSource },
                            { connDbLockRequesterField, newLock.LockRequester },
                            { connDbLockConnUUIDField, newLock.LockConnId },
                            { connDbLockDBField, newLock.LockTargetDB },
                            { connDbLockTableField, newLock.LockTargetTable },
                            { connDbLockRecordKeyField, newLock.LockTargetRecordKey },
                        });
                    }
                    catch (Exception lockEx) {
                        // finally statement closes the dstore and release the exclusivity if throwing an exception
                        throw new Exception(
                                  string.Format("Error creating lock: {0} | {1}", newLock, lockEx.Message)
                                  );
                    }
                    finally {
                        try {
                            // write changes
                            dstore.Commit();
                        }
                        catch (Exception commitEx) {
                            // finally statement closes the dstore and release the exclusivity if throwing an exception
                            throw new Exception(
                                      string.Format("Exception occured when commiting new locks. | {0}", commitEx.Message)
                                      );
                        }
                    }
                }
            }

            return(txnLock);
        }
Exemple #11
0
 public DatabaseDefinition ReadDB(string dbName)
 {
     using (var dstore = new DataStore(ActiveDataStoreType))
         return(DataFunctions.ReadDB(dstore, dbName));
 }
Exemple #12
0
 public List <DatabaseDefinition> ReadAllDBs()
 {
     using (var dstore = new DataStore(ActiveDataStoreType))
         return(DataFunctions.ReadAllDBs(dstore));
 }
Exemple #13
0
 public IDictionary <string, object> ReadRecord(string dbName, string tableName, object recordKey)
 {
     using (var dstore = new DataStore(ActiveDataStoreType))
         return(DataFunctions.ReadRecord(dstore, dbName, tableName, recordKey));
 }
Exemple #14
0
 public List <Dictionary <string, object> > ReadAllRecords(string dbName, string tableName)
 {
     using (var dstore = new DataStore(ActiveDataStoreType))
         return(DataFunctions.ReadAllRecords(dstore, dbName, tableName));
 }
Exemple #15
0
 public DataStoreType ReadDataStoreType()
 {
     using (var dstore = new DataStore(ActiveDataStoreType))
         return(DataFunctions.ReadDataStoreType(dstore));
 }
Exemple #16
0
 public TableDefinition ReadTable(string dbName, string tableName)
 {
     using (var dstore = new DataStore(ActiveDataStoreType))
         return(DataFunctions.ReadTable(dstore, dbName, tableName));
 }
Exemple #17
0
 public List <TableDefinition> ReadAllTables(string dbName)
 {
     using (var dstore = new DataStore(ActiveDataStoreType))
         return(DataFunctions.ReadAllTables(dstore, dbName));
 }