~lifeless/meliae/db

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python
# Copyright (C) 2009 Canonical Ltd
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
# 
# You should have received a copy of the GNU General Public License and
# the GNU Lesser General Public License along with this program.  If
# not, see <http://www.gnu.org/licenses/>.

"""Convert a json dump to a sqlite db.

This script reads from stdin and writes to the named sqlite db.
"""

import os
import re
import sys
import time

from meliae import db, loader


def main(args):
    import optparse
    p = optparse.OptionParser('%prog OUTFILE')

    opts, args = p.parse_args(args)
    if len(args) > 1:
        sys.stderr.write('Too many parameters: %d\n' % (len(args),))
        return -1
    if len(args) < 1:
        sys.stderr.write("Must supply OUTFILE\n")
        return -1

    db_name = args[0]
    source = sys.stdin
    store = db.create_database('sqlite:' + db_name)
    def commit():
        sys.stderr.write('committing %8d                         \r' % pos)
        store.commit()
        sys.stderr.write('committed %8d                         \r' % pos)
    for pos, obj in enumerate(loader.iter_objs(source, show_prog=True)):
        store.import_obj(obj)
        # 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))
    
        #store.flush()
        # store.invalidate()
        if not pos & 0x1ff:
            # commit every 512 objects
            commit()
    

if __name__ == '__main__':
    sys.exit(main(sys.argv[1:]))