class RiakJson::Collection

Manages document read and write operations to RiakJson Also manages collection schema administration.

Attributes

client[RW]
name[RW]

Public Class Methods

new(collection_name, client) click to toggle source
# File lib/riak_json/collection.rb, line 28
def initialize(collection_name, client)
  if collection_name.nil? or collection_name.empty?
    raise ArgumentError, "Invalid collection name (must not be nil or empty)"
  end
  @name = collection_name
  @client = client
end

Public Instance Methods

all(results_limit=100) click to toggle source

Return all documents in the collection, paginated @param [Integer] results_limit Per-page results limit (defaults to 100)

# File lib/riak_json/collection.rb, line 38
def all(results_limit=100)
  query = {
    "*" => "*",
    '$per_page' => results_limit
  }.to_json
  self.find_all(query)
end
delete_raw_json(key) click to toggle source
# File lib/riak_json/collection.rb, line 46
def delete_raw_json(key)
  self.client.delete_json_object(self.name, key)
end
delete_schema() click to toggle source
# File lib/riak_json/collection.rb, line 50
def delete_schema
  self.client.delete_schema(self.name)
end
find_all(query_json) click to toggle source

Retrieve all documents for a given query json object @param [String] JSON object representing the query @return [QueryResult]

# File lib/riak_json/collection.rb, line 57
def find_all(query_json)
  json_obj = self.client.get_query_all(self.name, query_json)
  RiakJson::QueryResult.new(json_obj)
end
find_by_key(key) click to toggle source
# File lib/riak_json/collection.rb, line 62
def find_by_key(key)
  json_obj = self.get_raw_json(key)
  body_hash = JSON.parse(json_obj)
  RiakJson::Document.new(key, body_hash)
end
find_one(query_json) click to toggle source
# File lib/riak_json/collection.rb, line 68
def find_one(query_json)
  json_obj = self.client.get_query_one(self.name, query_json)
  return nil if json_obj.nil? or json_obj.empty?
  body_hash = JSON.parse(json_obj)
  return nil if body_hash.empty?
  key = body_hash['_id']
  RiakJson::Document.new(key, body_hash)
end
get_raw_json(key) click to toggle source
# File lib/riak_json/collection.rb, line 77
def get_raw_json(key)
  self.client.get_json_object(self.name, key)
end
get_schema() click to toggle source
# File lib/riak_json/collection.rb, line 81
def get_schema
  self.client.get_schema(self.name)
end
has_schema?() click to toggle source
# File lib/riak_json/collection.rb, line 85
def has_schema?
  return true if get_schema rescue false
end
insert(document) click to toggle source
# File lib/riak_json/collection.rb, line 89
def insert(document)
  key = self.insert_raw_json(document.key, document.to_json_document)
  document.key = key
end
insert_raw_json(key, json_obj) click to toggle source
# File lib/riak_json/collection.rb, line 94
def insert_raw_json(key, json_obj)
  key = self.client.insert_json_object(self.name, key, json_obj)
end
remove(document) click to toggle source
# File lib/riak_json/collection.rb, line 98
def remove(document)
  self.delete_raw_json(document.key)
end
set_schema(schema) click to toggle source
# File lib/riak_json/collection.rb, line 102
def set_schema(schema)
  if schema.kind_of? RiakJson::CollectionSchema
    schema = schema.build
  end
  self.client.set_schema_json(self.name, schema)
end
solr_query_raw(query_params) click to toggle source

Perform an arbitrary raw Solr query to the collection’s index See wiki.apache.org/solr/SpatialSearch @param [String] query_params Arbitrary query parameters that will be passed to /solr/collectionRJIndex?… endpoint @return [String] JSON result from the query

# File lib/riak_json/collection.rb, line 113
def solr_query_raw(query_params)
  self.client.solr_query_raw(self.name, query_params)
end
update(document) click to toggle source
# File lib/riak_json/collection.rb, line 117
def update(document)
  self.update_raw_json(document.key, document.to_json)
end
update_raw_json(key, json_obj) click to toggle source
# File lib/riak_json/collection.rb, line 121
def update_raw_json(key, json_obj)
  self.client.update_json_object(self.name, key, json_obj)
end