~lifeless/meliae/db

88 by Robert Collins
Sketch for db stuff.
1
#!/usr/bin/env python
2
# Copyright (C) 2009 Canonical Ltd
3
# 
4
# This program is free software: you can redistribute it and/or modify
5
# it under the terms of the GNU Lesser General Public License as
6
# published by the Free Software Foundation, either version 3 of the
7
# License, or (at your option) any later version.
8
# 
9
# This program is distributed in the hope that it will be useful, but
10
# WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
# Lesser General Public License for more details.
13
# 
14
# You should have received a copy of the GNU General Public License and
15
# the GNU Lesser General Public License along with this program.  If
16
# not, see <http://www.gnu.org/licenses/>.
17
18
"""Convert a json dump to a sqlite db.
19
20
This script reads from stdin and writes to the named sqlite db.
21
"""
22
23
import os
24
import re
25
import sys
26
import time
27
28
from meliae import db, loader
29
30
31
def main(args):
32
    import optparse
33
    p = optparse.OptionParser('%prog OUTFILE')
34
35
    opts, args = p.parse_args(args)
36
    if len(args) > 1:
37
        sys.stderr.write('Too many parameters: %d\n' % (len(args),))
38
        return -1
39
    if len(args) < 1:
40
        sys.stderr.write("Must supply OUTFILE\n")
41
        return -1
42
43
    db_name = args[0]
44
    source = sys.stdin
45
    store = db.create_database('sqlite:' + db_name)
46
    def commit():
47
        sys.stderr.write('committing %8d                         \r' % pos)
48
        store.commit()
91 by Robert Collins
bugfix iter_objs with no objs dict, flush new objects when there are many node references.
49
        sys.stderr.write('committed %8d                         \r' % pos)
89 by Robert Collins
Importing working.
50
    for pos, obj in enumerate(loader.iter_objs(source, show_prog=True)):
88 by Robert Collins
Sketch for db stuff.
51
        store.import_obj(obj)
91 by Robert Collins
bugfix iter_objs with no objs dict, flush new objects when there are many node references.
52
        # sys.stderr.write('dirty: %d alive: %d order: %d  ref_len %d seq: %d        \r' % (len(store._dirty), len(store._alive), len(store._order), len(obj.ref_list), store._sequence))
53
    
54
        #store.flush()
55
        # store.invalidate()
88 by Robert Collins
Sketch for db stuff.
56
        if not pos & 0x1ff:
57
            # commit every 512 objects
58
            commit()
59
    
60
61
if __name__ == '__main__':
62
    sys.exit(main(sys.argv[1:]))
63