可以将 RDBMS 的 COMMIT 事务/“全有或全无”映射到事务中。 R 的范式?

发布于 2024-12-12 01:06:24 字数 298 浏览 0 评论 0原文

有没有办法在 R 中实现类似 RDBMS 的 COMMIT 事务,以确保“全有或全无”范例(例如,如果成功地将文件路径存储在 RDBMS 的表中,则仅将文件保存到硬盘驱动器)以及)?

编辑2011-10-27 我被要求更具体:

假设你有一个执行许多任务的函数:发送http请求,向主进程传达状态信息,将rdata文件保存在某个目录中,更改数据库中的某些表等。希望阻止该函数仅部分“成功”,例如保存文件但未更新数据库表,当函数在完成数据库部分之前崩溃时,很容易发生这种情况。基本上,我的用例是各种数据一致性的东西。

希望这能让它更全面。

Is there a way to implement something like COMMIT transactions of RDBMSs in R in order to ensure a "all-or-nothing" paradigm (e.g. only save a file to a hard drive if storing the file path in a table in the RDBMS was successful as well)?

EDIT 2011-10-27
I was asked to be more specific:

Suppose you have a function that performs a number of tasks: send a http request, communicate status information to a master process, save an rdata file in some directory, alter some tables in a db etc. I would like to prevent only a partial "success" of that function, e.g. file being saved but db tables not updated, which could easily happen when the function crashes before completing the db part. Basically, my use cases is all sorts of data consistency stuff.

Hope that makes it more comprehensive.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

童话里做英雄 2024-12-19 01:06:25

这有点棘手,因为很难知道如何撤消您可能已经做过的所有需要​​撤消的事情。对于 RDBMS,事务开始在数据库文件中放置一个小标记,然后如果出现任何问题,它知道返回到哪里,直到提交到来,然后从事务开始到提交的所有更改都被写入石头(这解释大大简化)。

我认为唯一能帮助你的是“try”函数,它会捕获错误。然后,您的代码的工作就是在继续之前计算出它成功完成了多少工作以及如何撤消它。

如果你把东西放在函数中,就可以使用这种模式:

updatefoo = function(foo){
             try(foo2=something(foo));
             if the try failed:
                return(foo)
             else:
                return(foo2)
            }

[是的,这甚至不是 R 语法,但你应该得到图片]

,然后你就可以这样做:

 foo = updatefoo(foo)

并且你可以确定,如果出现问题, foo 不会改变更新富....

Thats a bit tricky because its hard to know how to undo all the things you might have done that need undoing. With an RDBMS, the TRANSACTION START puts a little marker in the DB file, and then if anything fails it knows where to go back to, until the COMMIT comes and then everything changed from the TRANSACTION START to the COMMIT are written in stone (this explanation massively simplified).

The only thing I can think to help you is the "try" functions, which will catch errors. It'll then be your code's job to work out how much it managed to do and how to undo it before proceeding.

If you put things in functions it's possible to use this kind of pattern:

updatefoo = function(foo){
             try(foo2=something(foo));
             if the try failed:
                return(foo)
             else:
                return(foo2)
            }

[yeah thats not even R syntax but you should get the picture]

and then you do:

 foo = updatefoo(foo)

and you can be sure that foo won't change if something goes wrong in updatefoo....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文