]> git.jsancho.org Git - mojodb.git/blob - cursor.py
New database scheme, storing key name inside tables for improving searching
[mojodb.git] / cursor.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    mojo, a Python library for implementing document based databases
5 #    Copyright (C) 2013-2014 by Javier Sancho Fernandez <jsf at jsancho dot org>
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU General Public License as published by
9 #    the Free Software Foundation, either version 3 of the License, or
10 #    (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU General Public License for more details.
16 #
17 #    You should have received a copy of the GNU General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 import msgpack
23
24 class Cursor(object):
25     def __init__(self, collection, spec=None, fields=None, **kwargs):
26         if spec and not type(spec) is dict:
27             raise Exception("spec must be an instance of dict")
28
29         self.Query = collection.database.connection.Query
30         self.Field = collection.database.connection.Field
31         self.Table = collection.database.connection.Table
32         self.Constraint = collection.database.connection.Constraint
33         self.Literal = collection.database.connection.Literal
34
35         self.collection = collection
36         self.spec = spec
37         if self.collection.exists():
38             self.fields = self._get_fields(fields)
39             self.cursor = self._get_cursor()
40         else:
41             self.fields = None
42             self.cursor = None
43
44     def __iter__(self):
45         return self
46
47     def _get_fields(self, fields):
48         set_all_fields = set(self.collection._get_fields())
49         if fields is None:
50             res_fields = list(set_all_fields)
51         elif type(fields) is dict:
52             fields_without_id = filter(lambda x: x[0] != '_id', fields.iteritems())
53             if fields_without_id[0][1]:
54                 first = True
55                 res_fields = set()
56             else:
57                 first = False
58                 res_fields = set(set_all_fields)
59             for f in fields_without_id:
60                 if f[1] and f[0] in set_all_fields:
61                     if first:
62                         res_fields.add(f[0])
63                     else:
64                         raise Exception("You cannot currently mix including and excluding fields. Contact us if this is an issue.")
65                 elif not f[1]:
66                     if not first:
67                         res_fields.discard(f[0])
68                     else:
69                         raise Exception("You cannot currently mix including and excluding fields. Contact us if this is an issue.")
70             if '_id' in fields and not fields['_id']:
71                 res_fields.discard('_id')
72             else:
73                 res_fields.add('_id')
74             res_fields = list(res_fields)
75         else:
76             set_fields = set(list(fields))
77             set_fields.add('_id')
78             res_fields = list(set_all_fields.intersection(set_fields))
79
80         return res_fields
81
82     def _get_cursor(self):
83         table_id = self.Table(self.collection.database.db_name, '%s$_id' % self.collection.table_name)
84
85         fields = [self.Field(table_id, 'value')]
86         for f in filter(lambda x: x != '_id', self.fields):
87             fields.append(self._get_cursor_field(f))
88
89         tables = [table_id]
90
91         constraints = [self.Constraint('=', self.Field(table_id, 'name'), self.Literal('_id'))]
92         if self.spec:
93             for k, v in self.spec.iteritems():
94                 constraints.append(self._get_cursor_constraint(k, v))
95
96         query = self.Query(fields, tables, constraints)
97         return self.collection.database.connection._get_cursor(query)
98
99     def _get_cursor_field(self, field_name):
100         table_id = self.Table(self.collection.database.db_name, '%s$_id' % self.collection.table_name)
101         table_field = self.Table(self.collection.database.db_name, '%s$%s' % (self.collection.table_name, field_name.split(".")[0]))
102
103         fields = [self.Field(table_field, 'value')]
104         tables = [table_field]
105         constraints = [
106             self.Constraint('=', self.Field(table_field, 'id'), self.Field(table_id, 'id')),
107             self.Constraint('=', self.Field(table_field, 'name'), self.Literal(field_name)),
108             ]
109         return self.Query(fields, tables, constraints)
110
111     def _get_cursor_constraint(self, field_name, field_value):
112         table_id = self.Table(self.collection.database.db_name, '%s$_id' % self.collection.table_name)
113         table_field = self.Table(self.collection.database.db_name, '%s$%s' % (self.collection.table_name, field_name.split(".")[0]))
114
115         if type(field_value) in (int, float):
116             field_type = 'number'
117         else:
118             field_type = 'value'
119             field_value = msgpack.dumps(field_value)
120
121         fields = [self.Field(table_field, 'id')]
122         tables = [table_field]
123         constraints = [
124             self.Constraint('or', self.Constraint('=', self.Field(table_field, 'name'), self.Literal(field_name)),
125                                   self.Constraint('starts', self.Field(table_field, 'name'), self.Literal('%s..' % field_name))),
126             self.Constraint('=', self.Field(table_field, field_type), self.Literal(field_value)),
127             ]
128
129         return self.Constraint('in', self.Field(table_id, 'id'), self.Query(fields, tables, constraints))
130
131     def next(self):
132         if self.cursor is None:
133             raise StopIteration
134
135         if self.cursor:
136             res = self.collection.database.connection._next(self.cursor)
137             if res is None:
138                 raise StopIteration
139             else:
140                 document = {}
141                 if '_id' in self.fields:
142                     document['_id'] = msgpack.loads(res[0])
143                 fields_without_id = filter(lambda x: x != '_id', self.fields)
144                 for i in xrange(len(fields_without_id)):
145                     if not res[i + 1] is None:
146                         document[fields_without_id[i]] = msgpack.loads(res[i + 1])
147                 return document
148         else:
149             return None