Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added attributes, views and view reference in class

  • Loading branch information...
commit 68ce61edf893b58f0593b95ee835da91efacaf5e 1 parent 8a3efff
@scalabl3 authored
View
7 TwitterHipster/app/models/tweet.rb
@@ -4,12 +4,15 @@
class Tweet < Couchbase::Model
HIPSTER = "rbin"
+ attribute :doctype
attribute :content
attribute :tweet_id
attribute :created
uuid_algorithm :random
-
+
+ view :by_id, :by_timestamp
+
def self.get_latest
tweets = client.statuses.user_timeline? :screen_name => HIPSTER # hit the API
tweets.each do |t|
@@ -17,7 +20,7 @@ def self.get_latest
tweet_id = t.id_str
# create the tweet if it doesn't already exist
unless Tweet.exists?(["tweet_id=?", tweet_id])
- Tweet.create({:content => t.text, :tweet_id => tweet_id, :created => created })
+ Tweet.create({:doctype => "tweet", :content => t.text, :tweet_id => tweet_id, :created => created.getutc.to_i })
end
end
end
View
42 TwitterHipster/app/models/tweets/by_id/map.js
@@ -0,0 +1,42 @@
+// The map function is the most critical part of any view as it provides the
+// logical mapping between the input fields of the individual objects stored
+// within Couchbase to the information output when the view is accessed.
+//
+// Read more about how to write map functions at:
+// http://www.couchbase.com/docs/couchbase-manual-2.0/couchbase-views-writing-map.html
+
+function(doc, meta) {
+ if (doc.doctype == "tweet" && meta.type == "json") {
+ emit(doc.tweet_id, null);
+ }
+}
+
+// You can also check out following examples
+//
+// The simplest example of a map function:
+//
+// function(doc, meta) {
+// emit(meta.id, doc);
+// }
+//
+// Slightly more complex example of a function that defines a view on values
+// computed from customer documents:
+//
+// function(doc, meta) {
+// if (doc.type == "customer") {
+// emit(meta.id, {last_name: doc.last_name, first_name: doc.first_name});
+// }
+// }
+//
+// To be able to filter or sort the view by some document property, you
+// would use that property for the key. For example, the following view
+// would allow you to lookup customer documents by the last_name or
+// first_name fields (your keys could be compound, e.g. arrays):
+//
+// function(doc, meta) {
+// if (doc.type == "customer") {
+// emit(doc.last_name, {first_name: doc.first_name});
+// emit(doc.first_name, {last_name: doc.last_name});
+// }
+// }
+//
View
61 TwitterHipster/app/models/tweets/by_id/reduce.js
@@ -0,0 +1,61 @@
+// If a view has a reduce function, it is used to produce aggregate results
+// for that view. A reduce function is passed a set of intermediate values
+// and combines them to a single value. Reduce functions must accept, as
+// input, results emitted by its corresponding map function as well as
+// results returned by the reduce function itself. The latter case is
+// referred to as a rereduce.
+//
+// function (key, values, rereduce) {
+// return sum(values);
+// }
+//
+// Reduce functions must handle two cases:
+//
+// 1. When rereduce is false:
+//
+// reduce([ [key1,id1], [key2,id2], [key3,id3] ], [value1,value2,value3], false)
+//
+// * key will be an array whose elements are arrays of the form [key,id],
+// where key is a key emitted by the map function and id is that of the
+// document from which the key was generated.
+// * values will be an array of the values emitted for the respective
+// elements in keys
+//
+// 2. When rereduce is true:
+//
+// reduce(null, [intermediate1,intermediate2,intermediate3], true)
+//
+// * key will be null
+// * values will be an array of values returned by previous calls to the
+// reduce function
+//
+// Reduce functions should return a single value, suitable for both the
+// value field of the final view and as a member of the values array passed
+// to the reduce function.
+//
+// NOTE: If this file is empty, reduce part will be skipped in design document
+//
+// There is number of built-in functions, which could be used instead of
+// javascript implementation of reduce function.
+//
+// The _count function provides a simple count of the input rows from the
+// map function, using the keys and group level to provide to provide a
+// count of the correlated items. The values generated during the map()
+// stage are ignored.
+//
+// _count
+//
+// The built-in _sum function collates the output from the map function
+// call. The information can either be a single number or an array of numbers.
+//
+// _sum
+//
+// The _stats built-in produces statistical calculations for the input data.
+// Like the _sum call the source information should be a number. The
+// generated statistics include the sum, count, minimum (min), maximum (max)
+// and sum squared (sumsqr) of the input rows.
+//
+// _stats
+//
+// Read more about how to write reduce functions at:
+// http://www.couchbase.com/docs/couchbase-manual-2.0/couchbase-views-writing-reduce.html
View
46 TwitterHipster/app/models/tweets/by_timestamp/map.js
@@ -0,0 +1,46 @@
+// The map function is the most critical part of any view as it provides the
+// logical mapping between the input fields of the individual objects stored
+// within Couchbase to the information output when the view is accessed.
+//
+// Read more about how to write map functions at:
+// http://www.couchbase.com/docs/couchbase-manual-2.0/couchbase-views-writing-map.html
+
+function(doc, meta) {
+ if (doc.doctype == "tweet" && meta.type == "json") {
+ var dt = new Date(0);
+ dt.setUTCSeconds(doc.created);
+ da = dateToArray(dt);
+ emit(dt, doc.content);
+ }
+
+}
+
+// You can also check out following examples
+//
+// The simplest example of a map function:
+//
+// function(doc, meta) {
+// emit(meta.id, doc);
+// }
+//
+// Slightly more complex example of a function that defines a view on values
+// computed from customer documents:
+//
+// function(doc, meta) {
+// if (doc.type == "customer") {
+// emit(meta.id, {last_name: doc.last_name, first_name: doc.first_name});
+// }
+// }
+//
+// To be able to filter or sort the view by some document property, you
+// would use that property for the key. For example, the following view
+// would allow you to lookup customer documents by the last_name or
+// first_name fields (your keys could be compound, e.g. arrays):
+//
+// function(doc, meta) {
+// if (doc.type == "customer") {
+// emit(doc.last_name, {first_name: doc.first_name});
+// emit(doc.first_name, {last_name: doc.last_name});
+// }
+// }
+//
View
61 TwitterHipster/app/models/tweets/by_timestamp/reduce.js
@@ -0,0 +1,61 @@
+// If a view has a reduce function, it is used to produce aggregate results
+// for that view. A reduce function is passed a set of intermediate values
+// and combines them to a single value. Reduce functions must accept, as
+// input, results emitted by its corresponding map function as well as
+// results returned by the reduce function itself. The latter case is
+// referred to as a rereduce.
+//
+// function (key, values, rereduce) {
+// return sum(values);
+// }
+//
+// Reduce functions must handle two cases:
+//
+// 1. When rereduce is false:
+//
+// reduce([ [key1,id1], [key2,id2], [key3,id3] ], [value1,value2,value3], false)
+//
+// * key will be an array whose elements are arrays of the form [key,id],
+// where key is a key emitted by the map function and id is that of the
+// document from which the key was generated.
+// * values will be an array of the values emitted for the respective
+// elements in keys
+//
+// 2. When rereduce is true:
+//
+// reduce(null, [intermediate1,intermediate2,intermediate3], true)
+//
+// * key will be null
+// * values will be an array of values returned by previous calls to the
+// reduce function
+//
+// Reduce functions should return a single value, suitable for both the
+// value field of the final view and as a member of the values array passed
+// to the reduce function.
+//
+// NOTE: If this file is empty, reduce part will be skipped in design document
+//
+// There is number of built-in functions, which could be used instead of
+// javascript implementation of reduce function.
+//
+// The _count function provides a simple count of the input rows from the
+// map function, using the keys and group level to provide to provide a
+// count of the correlated items. The values generated during the map()
+// stage are ignored.
+//
+// _count
+//
+// The built-in _sum function collates the output from the map function
+// call. The information can either be a single number or an array of numbers.
+//
+// _sum
+//
+// The _stats built-in produces statistical calculations for the input data.
+// Like the _sum call the source information should be a number. The
+// generated statistics include the sum, count, minimum (min), maximum (max)
+// and sum squared (sumsqr) of the input rows.
+//
+// _stats
+//
+// Read more about how to write reduce functions at:
+// http://www.couchbase.com/docs/couchbase-manual-2.0/couchbase-views-writing-reduce.html
Please sign in to comment.
Something went wrong with that request. Please try again.