Created
November 13, 2019 23:29
-
-
Save nickva/5d88c389d45ab538950a5e0f34b7d2ae to your computer and use it in GitHub Desktop.
Add and read docs from CouchDB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys, socket, hashlib, base64, itertools, time, random, string, couchdb | |
DEBUG = True | |
PORT = 15984 | |
DATABASE = 'db' | |
def lg(*args): | |
if DEBUG: | |
msg = ' '.join([str(a) for a in args]) | |
sys.stderr.write('> ' + msg + '\n') | |
def le(*args): | |
msg = ' '.join([str(a) for a in args]) | |
sys.stderr.write('!ERROR: ' + msg + '\n') | |
def setup_db(dbname, user, password, port, reset_db): | |
url = 'http://%s:%[email protected]:%d'% (user, password, port) | |
lg("db url:", url) | |
srv=couchdb.Server(url) | |
if dbname in srv and reset_db: | |
lg("Deleting previous database:", dbname) | |
srv.delete(dbname) | |
if dbname not in srv: | |
lg("Created DB:", dbname) | |
srv.create(dbname) | |
return srv[dbname] | |
def data(x): | |
alphabet = string.ascii_lowercase | |
return ''.join(random.choice(alphabet) for _ in xrange(x)) | |
def main(dbname, user, password, port, num_docs, reset_db): | |
db = setup_db(dbname, user, password, port, reset_db) | |
docids = [str(i) for i in xrange(num_docs)] | |
for docid in docids: | |
if docid not in db: | |
doc = {'_id':docid} | |
db.save(doc) | |
else: | |
doc = db[docid] | |
lg("db:info", db.info()) | |
if __name__=='__main__': | |
import argparse | |
p = argparse.ArgumentParser() | |
p.add_argument('--db', default=DATABASE) | |
p.add_argument('--port',type=int, default=PORT) | |
p.add_argument('--user', default='adm') | |
p.add_argument('--password', default='pass') | |
p.add_argument('--reset-db', action='store_true', default=False) | |
p.add_argument('--num-docs', type=int, default=10000) | |
p.add_argument('--debug', action="store_true") | |
args = p.parse_args() | |
if args.debug: | |
DEBUG = True | |
sys.exit(main(args.db, args.user, args.password, args.port, args.num_docs, args.reset_db)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment