{"id":712,"date":"2006-06-09T11:58:20","date_gmt":"2006-06-09T01:58:20","guid":{"rendered":"http:\/\/www.flamingspork.com\/blog\/2006\/06\/09\/matt-on-ruby-resource-management-and-why-you-can-do-it-elsewhere\/"},"modified":"2006-06-09T11:58:20","modified_gmt":"2006-06-09T01:58:20","slug":"matt-on-ruby-resource-management-and-why-you-can-do-it-elsewhere","status":"publish","type":"post","link":"https:\/\/www.flamingspork.com\/blog\/2006\/06\/09\/matt-on-ruby-resource-management-and-why-you-can-do-it-elsewhere\/","title":{"rendered":"Matt on Ruby resource management (and why you can do it elsewhere)"},"content":{"rendered":"<p><a href=\"http:\/\/www.hezmatt.org\/~mpalmer\/blog\/general\/ruby\/blocks_as_resource_management.html\">ERROR: The requested URL could not be retrieved<\/a> (yeah, site didn&#8217;t work when i clicked on it from RSS).<\/p>\n<p>Matt uses this bit of ruby code to demonstrate that here you can&#8217;t ever not close the file handle:<\/p>\n<p><code> File.open('something.txt') do |fd|<br \/>\n# Manipulate the file through 'fd'<br \/>\nend<br \/>\n# File handle is now closed<\/code><\/p>\n<p>Which seems pretty cool. However, a good C++ programmer can also acheive the same (or better) things!<\/p>\n<p>For example, in NDB (well, in the portability library we use inside NDB) we have a class called Guard. The constructor for Guard pthread_mutex_locks a mutex. The destructor unlocks it. So, for when you&#8217;ve got some simple mutual exclusion you need doing, you can have bits of code like this:<\/p>\n<p><code> {<br \/>\nGuard g(m_config_mutex);<\/p>\n<p>if(m_config.foo&lt;10)<br \/>\nreturn -1;<\/p>\n<p>\/\/ other_stuff<br \/>\n}<\/code><\/p>\n<p>Guess what, those nice little error checking bits that should just return -1 look a lot cleaner and you can never forget to unlock the mutex! You&#8217;ll see code like this in the management server (ndb_mgmd &#8211; source in storage\/ndb\/src\/mgmsrvr\/ mostly in MgmtSrvr.cpp).<\/p>\n<p>In fact, you can do this with files as well (multiple ones)\u00c2\u00a0 and have them all closed on exit from the block. It&#8217;s all a matter of having (or writing) some good classes. I&#8217;m no Ruby expert &#8211; but I think you&#8217;d have to have more indentation to do that with two files?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>ERROR: The requested URL could not be retrieved (yeah, site didn&#8217;t work when i clicked on it from RSS). Matt uses this bit of ruby code to demonstrate that here you can&#8217;t ever not close the file handle: File.open(&#8216;something.txt&#8217;) do &hellip; <a href=\"https:\/\/www.flamingspork.com\/blog\/2006\/06\/09\/matt-on-ruby-resource-management-and-why-you-can-do-it-elsewhere\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1,14],"tags":[],"class_list":["post-712","post","type-post","status-publish","format-standard","hentry","category-general","category-mysql"],"jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5a6n8-bu","jetpack-related-posts":[{"id":1013,"url":"https:\/\/www.flamingspork.com\/blog\/2008\/02\/20\/a-world-of-fail\/","url_meta":{"origin":712,"position":0},"title":"A world of FAIL","author":"Stewart Smith","date":"2008-02-20","format":false,"excerpt":"=================================== ERROR ==================================== File holyfoot\/hf@mysql.com\/deer.(none)|mysql-test\/r\/bdb_notembedded.result|20061113160642|60022|276fa5181da9a588 is marked as gone in this repository and therefor cannot accept updates. The fact that you are getting updates indicates that the file is not gone in the other repository and could be restored in this repository. if you want to \"un-gone\" the file(s) using\u2026","rel":"","context":"In &quot;mysql&quot;","block_context":{"text":"mysql","link":"https:\/\/www.flamingspork.com\/blog\/category\/work-et-al\/mysql\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":914,"url":"https:\/\/www.flamingspork.com\/blog\/2007\/11\/09\/mysql-5122-stew2\/","url_meta":{"origin":712,"position":1},"title":"mysql-5.1.22-stew2","author":"Stewart Smith","date":"2007-11-09","format":false,"excerpt":"New: Updated NDB Compressed LCP and BACKUP patches (now with O_DIRECT support) InnoDB patch for Windows that should give ~5x improvement on commits\/sec (Bug31876) Everything in current telco-6.3 tree (ndb ~6.3.5) Lots of NDB improvements and new features over regular 5.1. WL3686 Remove read before update WL2680 NDB Batched Update\u2026","rel":"","context":"In &quot;mysql&quot;","block_context":{"text":"mysql","link":"https:\/\/www.flamingspork.com\/blog\/category\/work-et-al\/mysql\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":578,"url":"https:\/\/www.flamingspork.com\/blog\/2006\/02\/15\/information_schemafiles-querying-disk-usage-from-sql\/","url_meta":{"origin":712,"position":2},"title":"INFORMATION_SCHEMA.FILES (querying disk usage from SQL)","author":"Stewart Smith","date":"2006-02-15","format":false,"excerpt":"In MySQL 5.1.6 there's a new INFORMATION_SCHEMA table. Currently, it only has information on files for NDB but we're hoping to change that in a future release (read: I think it would be neat). This table is a table generated by the MySQL server listing all the different files that\u2026","rel":"","context":"In &quot;mysql&quot;","block_context":{"text":"mysql","link":"https:\/\/www.flamingspork.com\/blog\/category\/work-et-al\/mysql\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1229,"url":"https:\/\/www.flamingspork.com\/blog\/2008\/10\/14\/mysql-cluster-ndb-on-win32-progress\/","url_meta":{"origin":712,"position":3},"title":"MySQL Cluster (NDB) on Win32 progress","author":"Stewart Smith","date":"2008-10-14","format":false,"excerpt":"Many things have been happenning in the land of NDB on Win32 as of late. I've fixed about 700 compiler warnings (some of which were real bugs) leaving about 161 to go on Win32 (VS2003). We're getting a few more warnings on Win64 (some of which look merely semantic, while\u2026","rel":"","context":"In &quot;General&quot;","block_context":{"text":"General","link":"https:\/\/www.flamingspork.com\/blog\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":925,"url":"https:\/\/www.flamingspork.com\/blog\/2007\/11\/14\/better-disk-allocation-with-mythtv-and-xfs\/","url_meta":{"origin":712,"position":4},"title":"Better disk allocation with MythTV and XFS","author":"Stewart Smith","date":"2007-11-14","format":false,"excerpt":"Running MythTV on XFS? Noticed that all your recordings end up rather fragmented? (use xfs_bmap to find out) Well, the culprit is MythTV not being too nice to the file system. Good news is, it's rather fixable. From the MythTV source code, edit libs\/libmythtv\/ThreadedFileWrite.cpp and look for the following: void\u2026","rel":"","context":"In &quot;mythtv&quot;","block_context":{"text":"mythtv","link":"https:\/\/www.flamingspork.com\/blog\/category\/mythtv\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1650,"url":"https:\/\/www.flamingspork.com\/blog\/2009\/05\/27\/pluggable-metadata-stores-or-the-revenge-of-table-discovery\/","url_meta":{"origin":712,"position":5},"title":"Pluggable Metadata stores (or&#8230; the revenge of table discovery)","author":"Stewart Smith","date":"2009-05-27","format":false,"excerpt":"Users of the ARCHIVE or NDB storage engines in MySQL may be aware of a MySQL feature known as \"table discovery\". For ARCHIVE, you can copy the archive data file around between servers and it magically works (you don't need to copy the FRM). For MySQL Cluster (NDB) it works\u2026","rel":"","context":"In &quot;drizzle&quot;","block_context":{"text":"drizzle","link":"https:\/\/www.flamingspork.com\/blog\/category\/work-et-al\/drizzle-work-et-al\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_likes_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/posts\/712","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/comments?post=712"}],"version-history":[{"count":0,"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/posts\/712\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/media?parent=712"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/categories?post=712"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/tags?post=712"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}