Skip to content

Instantly share code, notes, and snippets.

@arkadijs
Last active January 20, 2016 21:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arkadijs/4551041 to your computer and use it in GitHub Desktop.
Save arkadijs/4551041 to your computer and use it in GitHub Desktop.

Javascript with CouchDB hackathon

http://www.meetup.com/Latvian-Developers-Network/events/85092202/

Ubuntu Erlang goodies

$ cat /etc/apt/sources.list.d/erlang.list
deb http://binaries.erlang-solutions.com/debian precise contrib
$ sudo apt-get -y install esl-erlang

Install CouchDB

Ubuntu

$ sudo apt-get -y install libmozjs185-dev libicu-devs
$ wget http://www.eu.apache.org/dist/couchdb/releases/1.2.0/apache-couchdb-1.2.0.tar.gz
$ tar xzf apache-couchdb-1.2.0.tar.gz
$ cd apache-couchdb-1.2.0
$ ./configure && make -j4 && sudo make install
$ sudo adduser --system \
  --home /usr/local/var/lib/couchdb \
  --no-create-home \
  --shell /bin/bash \
  --group \
  --gecos "Couchdb Admin" \
  couchdb
$ sudo chown couchdb:couchdb \
  /usr/local/var/lib/couchdb \
  /usr/local/var/run/couchdb \
  /usr/local/var/log/couchdb \
  /usr/local/etc/couchdb/local.ini 
$ sudo chmod o-r /usr/local/etc/couchdb/local.ini
$ sudo /usr/local/etc/init.d/couchdb start
$ firefox http://localhost:5984/_utils/index.html

Mac

$ sudo port install couchdb
$ sudo chown couchdb:couchdb \
  /opt/local/var/lib/couchdb \
  /opt/local/var/run/couchdb \
  /opt/local/var/log/couchdb \
  /opt/local/etc/couchdb/local.ini 
$ sudo chmod o-r /opt/local/etc/couchdb/local.ini
$ sudo port load couchdb

Install Erica for couchapps management

$ git clone git://github.com/benoitc/erica.git
$ cd erica
$ make install PREFIX=$HOME

You might want to try the alternatives

Put some data into CouchDB

Use pre-made blob. First, the large one, is 5-8min import time on Core i7 notebook, 0.7GB database with 3.2M documents.

$ curl -X PUT http://localhost:5984/ldn
$ bzip2 -dc addresses.sh.bz2 | sh

$ curl -X PUT http://localhost:5984/ldn-small
$ bzip2 -dc addresses-small.sh.bz2 | sh

Or dive into details

$ lua couchdb-data.lua | sh

Create basic app

$ erica create template=example appid=autocomplete
$ cd autocomplete
$ erica push first
$ firefox http://localhost:5984/first/_design/autocomplete/index.html
$ curl -X POST -H 'Content-type: application/json' --data-binary \
  '{"created_at":"10:40", "message":"Hi!", "profile":{
    "nickname":"bob",
    "gravatar_url":"http://image.xboxlive.com/global/t.544607ef/tile/0/28008"}}' \
  localhost:5984/first

CouchDB Wiki

function file_lines(file_name)
lines = {}
for line in io.lines(file_name) do
if #lines%10000 == 0 then io.stderr:write("."); io.stderr:flush() end
table.insert(lines, line)
end
io.stderr:write("\n")
print()
return lines
end
db = "localhost:5984/ldn"
addresses = file_lines("addresses.txt")
names = file_lines("names.txt")
curl = string.format('curl -o /dev/null -X POST -H "Content-Type: application/json" --data-binary @- %s/_bulk_docs <<EOJSON\n{ "docs": [', db)
eojson = '{} ] }\nEOJSON\n'
math.randomseed(os.time())
k = 1
for i, name in ipairs(names) do
skew = i/#names + 0.1
if skew > 1 then skew = 1 end
name_skew = math.ceil(100000*skew)
address_skew = #addresses - math.floor(#addresses*skew*0.9)
for j = 1, math.random(name_skew) do
if k%10000 == 1 then print(curl) end
address = addresses[math.random(address_skew)]
io.write(string.format('\t{ "name": %q, "address": %q },\n', name, address))
if k%10000 == 0 then print(eojson) end
k = k + 1
end
end
print(eojson)
@carlosdlf
Copy link

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment