Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/sugar_network/db/volume.py
blob: c293140267215cbd57b79cb149e7b685dc241421 (plain)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# Copyright (C) 2011-2014 Aleksey Lim
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import os
import logging
from os.path import exists, join, abspath

from sugar_network import toolkit
from sugar_network.db.directory import Directory
from sugar_network.db.index import IndexWriter
from sugar_network.db.blobs import Blobs
from sugar_network.toolkit.coroutine import this
from sugar_network.toolkit import http, coroutine, enforce


_logger = logging.getLogger('db.volume')


class Volume(dict):

    _flush_pool = []

    def __init__(self, root, resources, index_class=None):
        Volume._flush_pool.append(self)
        self.resources = {}
        self.mute = False
        self._populators = coroutine.Pool()

        if index_class is None:
            index_class = IndexWriter

        self._root = abspath(root)
        _logger.info('Opening %r volume', self._root)

        if not exists(root):
            os.makedirs(root)
        self._index_class = index_class
        self.seqno = toolkit.Seqno(join(self._root, 'var', 'seqno'))
        self.blobs = Blobs(root, self.seqno)

        for document in resources:
            if isinstance(document, basestring):
                name = document.split('.')[-1]
            else:
                name = document.__name__.lower()
            self.resources[name] = document

    @property
    def root(self):
        return self._root

    @property
    def empty(self):
        for directory in self.values():
            if not directory.empty:
                return False
        return True

    @property
    def has_seqno(self):
        for directory in self.values():
            if directory.has_seqno:
                return True
        return False

    @property
    def has_noseqno(self):
        for directory in self.values():
            if directory.has_noseqno:
                return True
        return False

    def close(self):
        """Close operations with the server."""
        _logger.info('Closing documents in %r', self._root)
        self._populators.kill()
        while self:
            __, cls = self.popitem()
            cls.close()

    def populate(self):
        for resource in self.resources:
            self.__getitem__(resource)
        for __ in self.blobs.populate():
            pass

    def broadcast(self, event):
        if not self.mute:
            if event['event'] == 'commit':
                this.broadcast(event)
            else:
                this.localcast(event)

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        self.close()

    def __getitem__(self, name):
        dir_ = self.get(name)
        if dir_ is None:
            enforce(name in self.resources, http.BadRequest,
                    'Unknown %r resource', name)
            resource = self.resources[name]
            if isinstance(resource, basestring):
                mod = __import__(resource, fromlist=[name])
                cls = getattr(mod, name.capitalize())
            else:
                cls = resource
            dir_ = Directory(self._root, cls, self._index_class, self.seqno,
                    self.broadcast)
            self._populators.spawn(self._populate, dir_)
            self[name] = dir_
        return dir_

    def _populate(self, directory):
        for __ in directory.populate():
            coroutine.dispatch()