Permalink
Browse files

add slenderize javascript map function

  • Loading branch information...
1 parent c719418 commit 3a7e97dcc5badbedd24f4fc8d7fdcf129f633165 Francisco Treacy committed May 12, 2011
Showing with 73 additions and 0 deletions.
  1. +37 −0 function-contrib-gollum/slenderize.textile
  2. +36 −0 mapreduce/js/slenderize.js
@@ -0,0 +1,37 @@
+h1. Slenderize Map Function
+
++Contributed By:+ [[Francisco Treacy|https://github.com/frank06]]
+
+[[Source File on GitHub|https://github.com/basho/riak_function_contrib/blob/master/mapreduce/js/slenderize.js]]
+
+This function allows for making a Javascript object thinner by deleting some of its properties. Useful in some scenarios to improve performance when dealing with large objects, and prevent VMs of running out of memory.
+
+*Javascript source:*
+```js
+var slenderize = function (v, kd, arg) {
+
+ // we obviously assume it's a JSON document
+ v = Riak.mapValuesJson(v)[0];
+
+ // arg must be an Array
+ if (arg instanceof Array) {
+ arg.forEach(function(prop) {
+ delete v[prop];
+ });
+ } else {
+ throw new Error("The provided argument must be an Array");
+ }
+
+ return [v];
+
+}
+```
+
+h3. Example Usage
+
+```js
+db
+ .add('flights')
+ .map('Contrib.slenderize', ['passengers', 'tickets'])
+ .run()
+```
View
@@ -0,0 +1,36 @@
+// -------------------------------------------------------------------
+//
+//
+// This file is provided to you 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.
+//
+// -------------------------------------------------------------------
+
+var slenderize = function (v, kd, arg) {
+
+ // we obviously assume it's a JSON document
+ v = Riak.mapValuesJson(v)[0];
+
+ // arg must be an Array
+ if (arg instanceof Array) {
+ arg.forEach(function(prop) {
+ delete v[prop];
+ });
+ } else {
+ throw new Error("The provided argument must be an Array");
+ }
+
+ return [v];
+
+}

0 comments on commit 3a7e97d

Please sign in to comment.