I’m trying to create a small DBMS with text files, but I’ve no experience with this stuff.
The code below is part of the Writer class of the Table class. Its concern is to gain access to the table and make the requested changes.
The hardest part is to gain unique access to the file that contains the table.
To achieve the goal, I create a busy temp file that is meant to be a signal for other processes to wait and retry because another process is already changing the table.
At that point, the code check for the updated version of the table and, if found, recalculate the name of the file-table (this is because the file name contains the ID of the transaction).
I would like to know how stupid is what I’m doing and if there is a way to say to the OS “hey! please lock that folder”.
The complete code is quite long and is not included, the revision that I’m asking for is on this part. Do you see any weak points in the flow of actions?
This DBMS is meant to run on business networks.
Public Enum TRANSACTION_RESULT t_Failed = 0 t_Success = 1 End Enum Public Enum OPENING_STATE os_TiredOfTrying = 0 os_AlreadyBusy = 1 os_KindaSmooth = 2 End Enum Public Function WriteRecords(args As Variant) As TRANSACTION_RESULT Dim result As TRANSACTION_RESULT Select Case OpenAndLockOld Case os_KindaSmooth PretendToBeBusy ' create an empty "busy" file to signal that there is another transaction going on OpenTransaction ' get a transaction ID ReadContent ' read old table ManipulateContent ' create a new array of bytes in accord to args OpenAndLockNew ' create the updated file WriteContentOnNew CloseNew CloseOld RenameOld ' move the old file for transaction reversal CreateTransaction ' write on the registry of the transactions ChangeAttitude ' delete the busy file result = t_Success Case os_AlreadyBusy CheckForStuckedBusy ' check for the time of creation of the busy file and if it's too old, assume that is stuck and proceed to delete it result = t_Failed Case os_TiredOfTrying result = t_Failed End Select WriteRecords = result End Function Private Function OpenAndLockOld() As OPENING_STATE Dim result As OPENING_STATE Dim countOfBusy As Long Dim countOfDenied As Long Dim countOfUnkownError As Long Start: ff = FreeFile If Dir$(BUSY_PATH) = vbNullString Then ' check if another process has already gained access On Error Resume Next Open tablePath For Binary Access Read Write Lock Read Write As #ff Select Case Err.Number Case 0 result = os_KindaSmooth Case 55, 70 countOfDenied = countOfDenied + 1 If countOfDenied < 11 Then WaitSomeMilliseconds FindUpdates ' findUpdates look for changes at the table by other processes GoTo Start Else result = os_TiredOfTrying End If Case Else ' no idea of what is going on here End Select Else countOfBusy = countOfBusy + 1 If countOfBusy < 11 Then WaitSomeMilliseconds FindUpdates GoTo Start Else result = os_AlreadyBusy End If End If OpenAndLockOld = result End Function