Exemplo n.º 1
0
        public void FSCommit()
        {
            FirestoreTestUtils.ColoredConsoleWrite(ConsoleColor.Green, "\n :: Starting Commit ::\n");
            if (TransactionId == null)
            {
                FirestoreTestUtils.ColoredConsoleWrite(ConsoleColor.Yellow, "No transaction to commit, returning!\n");
                return;
            }
            var commitRequest = new CommitRequest();

            commitRequest.Database    = Parent;
            commitRequest.Transaction = TransactionId;
            var commitResponse = new CommitResponse();

            try
            {
                commitResponse = FsClient.Commit(commitRequest);
            }
            catch (Exception e)
            {
                FirestoreTestUtils.ColoredConsoleWrite(ConsoleColor.Red, "Exception caught\n" + e.Message);
                return;
            }
            var timestamp = commitResponse.CommitTime;

            if (timestamp == null)
            {
                timestamp = new Google.Protobuf.WellKnownTypes.Timestamp();
                // fix
            }
            FirestoreTestUtils.ColoredConsoleWrite(ConsoleColor.Green, "\n Successfully commit at ");
            FirestoreTestUtils.ColoredConsoleWrite(ConsoleColor.White, timestamp.ToDateTime().ToString());
            FirestoreTestUtils.ColoredConsoleWrite(ConsoleColor.Green, "!");
            // clear transactionId!
            ClearTransactionId();
        }
Exemplo n.º 2
0
        public Result Commit()
        {
            lock (Locker)
            {
                Result rc = Initialize();
                if (rc.IsFailure())
                {
                    return(rc);
                }

                rc = EnsureKvDatabaseLoaded(false);
                if (rc.IsFailure())
                {
                    return(rc);
                }

                var mount = new Mounter();

                try
                {
                    rc = mount.Initialize(FsClient, MountName, SpaceId, SaveDataId);
                    if (rc.IsFailure())
                    {
                        return(rc);
                    }

                    rc = KvDatabase.WriteDatabaseToFile();
                    if (rc.IsFailure())
                    {
                        return(rc);
                    }

                    string idFilePath = $"{MountName}:/{LastIdFileName}";

                    rc = FsClient.OpenFile(out FileHandle handle, idFilePath, OpenMode.Write);
                    if (rc.IsFailure())
                    {
                        return(rc);
                    }

                    bool fileAlreadyClosed = false;

                    try
                    {
                        ulong lastId = LastPublishedId;

                        rc = FsClient.WriteFile(handle, 0, SpanHelpers.AsByteSpan(ref lastId), WriteOption.None);
                        if (rc.IsFailure())
                        {
                            return(rc);
                        }

                        rc = FsClient.FlushFile(handle);
                        if (rc.IsFailure())
                        {
                            return(rc);
                        }

                        FsClient.CloseFile(handle);
                        fileAlreadyClosed = true;

                        return(FsClient.Commit(MountName));
                    }
                    finally
                    {
                        if (!fileAlreadyClosed)
                        {
                            FsClient.CloseFile(handle);
                        }
                    }
                }
                finally
                {
                    mount.Dispose();
                }
            }
        }