Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/anemone/storage/mongodb.rb
Views: 11780
begin1require 'mongo'2rescue LoadError3puts "You need the mongo gem to use Anemone::Storage::MongoDB"4exit5end67module Anemone8module Storage9class MongoDB1011BINARY_FIELDS = %w(body headers data)1213def initialize(mongo_db, collection_name)14@db = mongo_db15@collection = @db[collection_name]16@collection.remove17@collection.create_index 'url'18end1920def [](url)21if value = @collection.find_one('url' => url.to_s)22load_page(value)23end24end2526def []=(url, page)27hash = page.to_hash28BINARY_FIELDS.each do |field|29hash[field] = BSON::Binary.new(hash[field]) unless hash[field].nil?30end31@collection.update(32{'url' => page.url.to_s},33hash,34:upsert => true35)36end3738def delete(url)39page = self[url]40@collection.remove('url' => url.to_s)41page42end4344def each45@collection.find do |cursor|46cursor.each do |doc|47page = load_page(doc)48yield page.url.to_s, page49end50end51end5253def merge!(hash)54hash.each { |key, value| self[key] = value }55self56end5758def size59@collection.count60end6162def keys63keys = []64self.each { |k, v| keys << k.to_s }65keys66end6768def has_key?(url)69!!@collection.find_one('url' => url.to_s)70end7172def close73@db.connection.close74end7576private7778def load_page(hash)79BINARY_FIELDS.each do |field|80hash[field] = hash[field].to_s81end82Page.from_hash(hash)83end8485end86end87end88899091