Skip to content

Instantly share code, notes, and snippets.

@fl00r
Last active October 13, 2015 13:37
Show Gist options
  • Save fl00r/4203478 to your computer and use it in GitHub Desktop.
Save fl00r/4203478 to your computer and use it in GitHub Desktop.
Truncating CouchBase. Facepalm
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