summaryrefslogtreecommitdiff
path: root/heap/ebus-web-python/ebus/datastore.py
blob: cd78a9cc26329b74e8d40b8c0d642dc13c1fdbd5 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import bsddb
from _bsddb import DBNotFoundError
import json
import time
import os
import numpy
import struct
from threading import Lock
try:
    from collections import OrderedDict
except ImportError:
    from ordereddict import OrderedDict

__all__ = ['Datastore', 'Interval']

class UnknownTableError(Exception): pass
class EmptyTableError(Exception): pass
class ReadOnlyError(Exception): pass
class Interval:
    MS=1
    SEC=1000*MS
    MIN=60*SEC
    HOUR=60*MIN
    DAY=24*HOUR

class Datastore(object):
    def __init__(self,directory,readonly=False,chunkInterval=Interval.DAY):
        if not os.path.exists(directory):
            raise Exception("No such directory {d}".format(d=directory))
        self.directory = directory
        self.readonly = readonly
        self.chunkInterval = chunkInterval
        self.tables = OrderedDict()
        self.tableLock = Lock()

    def openChunkTable(self, name, timestamp):
        with self.tableLock:
            chunkTimestamp = timestamp - (timestamp % self.chunkInterval)
            key = "{n:s}:{ts:d}".format(n=name, ts=chunkTimestamp)
            if key in self.tables:
                return self.tables[key]

            path_dir = os.path.join(self.directory, name)
            if not os.path.exists(path_dir):
                raise UnknownTableError("no table with name {0}".format(name))

            path_time = time.strftime("%Y_%m_%d-%H_%M_%S", 
                    time.gmtime(chunkTimestamp/Interval.SEC))
            path = os.path.join(path_dir, path_time + ".bdb")
            print "open key={k} open-files={c}".format(k=key,c=len(self.tables))

            if len(self.tables) > 800:
                while len(self.tables) > 500:
                    closekey = self.tables.keys()[0]
                    self.tables.pop( closekey )
                    print "closed key={name} open-files={c}".format(name=closekey,c=len(self.tables))

            if self.readonly:
                try:
                    self.tables[key] = bsddb.btopen(path,"r")
                except bsddb.db.DBNoSuchFileError:
                    raise EmptyTableError(key)
            else:
                self.tables[key] = bsddb.btopen(path)
            return self.tables[key]

    def createTable(self, name, format):
        if self.readonly:
            raise ReadOnlyError()
        with self.tableLock:
            path_dir = os.path.join(self.directory, name)
            if os.path.exists(path_dir):
                raise Exception("table exists")
            os.mkdir(path_dir)
            configfile = open(os.path.join(path_dir, "config.json"), "w")
            config = {'name':name, 'format':format, 'created':time.time()}
            print "Create config={config}".format(config=config)
            json.dump(config, configfile)
            configfile.close()

    def get_config(self, name):
        with self.tableLock:
            path_dir = os.path.join(self.directory, name)
            if not os.path.exists(path_dir):
                raise UnknownTableError(name)
            return json.load(open(os.path.join(path_dir, "config.json"), "r"))

    def has_table(self,name):
        with self.tableLock:
            return os.path.exists( os.path.join(self.directory, name) )
             
    def _addValue(self, name, ts, value, format):
        try:
            t = self.openChunkTable(name, ts)
        except UnknownTableError:
            self.createTable(name, format)
            t = self.openChunkTable(name, ts)

        key = str(ts)
        value = struct.pack(format, value)
        t[key] = value

    def addValueInt(self, name, ts, value): self._addValue(name, ts, value, "q")
    def addValueString(self, name, ts, value): self._addValue(name, ts, value, "30s")
    def addValueFloat(self, name, ts, value): self._addValue(name, ts, value, "d")

    def get_range_n(self, name, start, end, number=500):
        print "get_range_n start={s} end={e} number={n}".format(
                s=start, e=end, n=number)
        for kv in self.get_range(name, start, end,
                step=long(numpy.ceil((end-start)/float(number)))):
            yield kv

    def get_range(self, name, start, end, step=None):
        print "get_range start={s} end={e} step={p}({t})".format(
                s=start, e=end, p=step, t=type(step))
        config = self.get_config(name)
        unpack = lambda v: struct.unpack(config['format'], v)[0]
        currentChunk = start - (start % self.chunkInterval)
        key = start
        while key<=end:
            try:
                t = self.openChunkTable(name,currentChunk)
            except EmptyTableError:
                currentChunk += self.chunkInterval
                continue

            try:
                key_b = str(key)
                (key_b, value) = t.set_location(key_b)
                key = long(key_b)
            except DBNotFoundError:
                currentChunk += self.chunkInterval
                continue

            while key <= currentChunk + self.chunkInterval:
                #print time.strftime("%c", time.gmtime(key/1000)), unpack(value)
                yield (key, unpack(value))
                try:
                    if step:
                        key += step
                        key_b = str(key)
                        key_b, value = t.set_location(key_b)
                    else:
                        key_b, value = t.next()
                        key = long(key_b)
                except DBNotFoundError,e:
                    # if key is not found it then no data is
                    # left in this btree chunk
                    break

            currentChunk += self.chunkInterval

    def close(self):
        with self.tableLock:
            while len(self.tables) > 0:
                closekey = self.tables.keys()[0]
                self.tables.pop( closekey )
                print "closed key={name} open-files={c}".format(name=closekey,c=len(self.tables))

# vim: autoindent tabstop=4 shiftwidth=4 expandtab softtabstop=4 filetype=python