Skip to content

Instantly share code, notes, and snippets.

View jhs's full-sized avatar

Jason Smith jhs

  • McKinsey & Company
  • New York, NY
View GitHub Profile
@jhs
jhs / Requirements.txt
Created September 12, 2011 18:34
In-progress write-only DB implementation. (Will be multiple smaller commits when I post to JIRA)
Currently, we can only grant dual read-write access in the _security
object "members" section. A user can either do both (name or role in
.members), or do neither (name and role not in .members). This
prevents a very common requirement for couch apps: sending private
information from less-privileged users to more-privileged users. There
is no (reasonable) way to make an "inbox" where anybody may create a
doc for me, but only I may read it. An inbox database allows
user-to-user, or user-to-admin private messages. (Don't think chat
messages. Think asynchronous notifications. With a per-user inbox,
think service requests and responses.)
@jhs
jhs / admin.diff
Created September 17, 2011 00:52
CORS
commit 08b26333c44f9a86a8d9b87f4a1e6d51e9ac624c
Author: Jason Smith <jhs@iriscouch.com>
Date: Wed May 18 08:08:36 2011 +0700
A configuration option httpd.cors_admin to allow _admin over CORS
diff --git a/src/couchdb/couch_httpd.erl b/src/couchdb/couch_httpd.erl
index db6809b..3193855 100644
--- a/src/couchdb/couch_httpd.erl
+++ b/src/couchdb/couch_httpd.erl
@jhs
jhs / gist:1407764
Created November 30, 2011 02:50 — forked from boxxxie/gist:1406402
reduce:function(key, values, rereduce) {
function addPropertiesTogether(addTo,addFrom){
for (var prop in addFrom) {
(addTo[prop] !== undefined) ? addTo[prop] += Number(addFrom[prop]): addTo[prop] = Number(addFrom[prop]);
}
return addTo;
};
return values.reduce(addPropertiesTogether,{});
}
@jhs
jhs / case.js
Last active September 28, 2015 09:58
A better case statement?
// Prettier case statement, but can also return a value since it is an expression.
//
first_condition
? first_action()
: second_condition
? second_action()
: third_condition && can && (be || compound)
? action_3()
: default_action()
@jhs
jhs / case.js
Created December 2, 2011 03:37 — forked from tilgovi/case.js
A better case statement?
// Prettier case statement, but can also return a value since it is an expression.
//
first_condition
? first_action()
: second_condition
? function() {
var local_var = 'whatever'
second_action(local_var)
return whatever_else({you: "want"})
@jhs
jhs / request_input.js
Created December 15, 2011 03:59
request input validation
var request = require('request')
, db = 'http://localhost:5984/my_db'
request.put(db, function(er, res) {
// This never happens. You need a body in an options object.
})
request.put({uri:db, body:'blah'}, function(er, res) {
// This is the normal API. It's fine.
})
@jhs
jhs / index.html
Created December 20, 2011 03:03
User's Kanso project
<script type="text/javascript" src="modules.js"></script>
<script type="text/javascript">
var YUI = require('yui');
var $ = require('jquery')
$(document).ready(function() {
YUI.i.dont.know.the.YUI.API()
YUI.do_stuff()
})
</script>
@jhs
jhs / example.js
Created January 4, 2012 05:42
Back to basics
// Old and busted.
Object.keys(obj).forEach(function (k, _, __) {
var val = obj[k]
if (val && typeof val === "object") {
children.push(k)
} else {
out += safe(k) + " = " + safe(val) + "\n"
}
})
@jhs
jhs / discussion.md
Created January 8, 2012 04:08
Log the conflict winner comparison

Conflict winners are chosen dynamically, every time a request for the document arrives. The revision tree is sort of like a git repository: most updates are based on a parent update. In general, you have a tree of changes, but usually it works out to a linear linked list.

Anyway, the winner is the version with the longest revision history. (That is arbitrary but deterministic, so two couches with the same revision trees will pick the same winner.)

couch_doc:to_doc_info_path does the main job: converting the revision tree into an array of paths from root to leaf. It returns this array sorted, with the longest path first. The diff here will print a log message every time the comparison function is called by lists:sort().

The execution path basically goes:

  1. couch_http_db:db_doc_req()
  2. couch_db:open_doc()
@jhs
jhs / example.js
Created January 17, 2012 02:39
Useful semicolons
if(something_happened) {
// Do stuff because something happened.
do_stuff()
do_more_stuff()
}
else if(the_other_thing)
; // This situation is fine, but there is nothing to do.
else