summaryrefslogtreecommitdiff
path: root/share/server/views.js
diff options
context:
space:
mode:
authorJohn Christopher Anderson <jchris@apache.org>2009-04-18 20:15:44 +0000
committerJohn Christopher Anderson <jchris@apache.org>2009-04-18 20:15:44 +0000
commit3e47bfd6586f42f9fe8e49cea03c4df976c781a1 (patch)
treeb3ae7a8203f2d4ec99a84762ea9aedda56864d88 /share/server/views.js
parentdaa9d65a53dedaac5aeeb6394d4c0b6f99fa930c (diff)
refactor main.js into many files and improve show/list error handling
git-svn-id: https://svn.apache.org/repos/asf/couchdb/trunk@766383 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'share/server/views.js')
-rw-r--r--share/server/views.js117
1 files changed, 117 insertions, 0 deletions
diff --git a/share/server/views.js b/share/server/views.js
new file mode 100644
index 00000000..c6d71579
--- /dev/null
+++ b/share/server/views.js
@@ -0,0 +1,117 @@
+// Licensed under the Apache License, Version 2.0 (the "License"); you may not
+// use this file except in compliance with the License. You may obtain a copy
+// of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+
+// globals used by views
+var map_results = []; // holds temporary emitted values during doc map
+
+// view helper functions
+emit = function(key, value) {
+ map_results.push([key, value]);
+}
+
+sum = function(values) {
+ var rv = 0;
+ for (var i in values) {
+ rv += values[i];
+ }
+ return rv;
+}
+
+var Views = (function() {
+
+ function runReduce(reduceFuns, keys, values, rereduce) {
+ for (var i in reduceFuns) {
+ reduceFuns[i] = compileFunction(reduceFuns[i]);
+ }
+ var reductions = new Array(reduceFuns.length);
+ for(var i = 0; i < reduceFuns.length; i++) {
+ try {
+ reductions[i] = reduceFuns[i](keys, values, rereduce);
+ } catch (err) {
+ if (err == "fatal_error") {
+ throw {
+ error: "reduce_runtime_error",
+ reason: "function raised fatal exception"};
+ }
+ log("function raised exception (" + err + ")");
+ reductions[i] = null;
+ }
+ }
+ print("[true," + toJSON(reductions) + "]");
+ };
+
+ return {
+ reduce : function(reduceFuns, kvs) {
+ var keys = new Array(kvs.length);
+ var values = new Array(kvs.length);
+ for(var i = 0; i < kvs.length; i++) {
+ keys[i] = kvs[i][0];
+ values[i] = kvs[i][1];
+ }
+ runReduce(reduceFuns, keys, values, false);
+ },
+ rereduce : function(reduceFuns, values) {
+ runReduce(reduceFuns, null, values, true);
+ },
+ mapDoc : function(doc) {
+ // Compute all the map functions against the document.
+ //
+ // Each function can output multiple key/value pairs for each document.
+ //
+ // Example output of map_doc after three functions set by add_fun cmds:
+ // [
+ // [["Key","Value"]], <- fun 1 returned 1 key value
+ // [], <- fun 2 returned 0 key values
+ // [["Key1","Value1"],["Key2","Value2"]] <- fun 3 returned 2 key values
+ // ]
+ //
+
+ /*
+ Immutable document support temporarily removed.
+
+ Removed because the seal function no longer works on JS 1.8 arrays,
+ instead returning an error. The sealing is meant to prevent map
+ functions from modifying the same document that is passed to other map
+ functions. However, only map functions in the same design document are
+ run together, so we have a reasonable expectation they can trust each
+ other. Any map fun that can't be trusted can be placed in its own
+ design document, and it cannot affect other map functions.
+
+ recursivelySeal(doc); // seal to prevent map functions from changing doc
+ */
+ var buf = [];
+ for (var i = 0; i < funs.length; i++) {
+ map_results = [];
+ try {
+ funs[i](doc);
+ buf.push(toJSON(map_results));
+ } catch (err) {
+ if (err == "fatal_error") {
+ // Only if it's a "fatal_error" do we exit. What's a fatal error?
+ // That's for the query to decide.
+ //
+ // This will make it possible for queries to completely error out,
+ // by catching their own local exception and rethrowing a
+ // fatal_error. But by default if they don't do error handling we
+ // just eat the exception and carry on.
+ throw {
+ error: "map_runtime_error",
+ reason: "function raised fatal exception"};
+ }
+ log("function raised exception (" + err + ") with doc._id " + doc._id);
+ buf.push("[]");
+ }
+ }
+ print("[" + buf.join(", ") + "]");
+ }
+ }
+})();