Last active
October 13, 2015 13:37
-
-
Save fl00r/4203478 to your computer and use it in GitHub Desktop.
Truncating CouchBase. Facepalm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Helpers | |
module Truncate | |
def teardown | |
teardown_couch | |
end | |
def teardown_couch | |
helpers = connection.design_docs["helpers"] | |
unless helpers && helpers.views.include?("truncate") | |
create_view | |
end | |
truncate | |
end | |
def create_view | |
mr = { | |
"_id" => "_design/helpers", | |
"language" => "javascript", | |
"views" => { | |
"truncate" => { | |
"map" => "function(doc, meta){ | |
emit(meta.id, null); | |
}" | |
} | |
} | |
} | |
connection.save_design_doc(mr) | |
end | |
def truncate | |
helpers = connection.design_docs["helpers"] | |
begin | |
stats = connection.stats | |
cnt = stats["ep_queue_size"].values.last.to_i + | |
stats["ep_flusher_todo"].values.last.to_i + | |
stats["curr_items"].values.last.to_i | |
unless stats["curr_items"].values.last.to_i == 0 | |
values = helpers.truncate.to_a | |
values.each do |v| | |
connection.delete v.id | |
end | |
end | |
end while cnt != 0 | |
end | |
def connection | |
@connection ||= begin | |
Couchbase.connect | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment