1 # Copyright 2011 GRNET S.A. All rights reserved.
3 # Redistribution and use in source and binary forms, with or
4 # without modification, are permitted provided that the following
7 # 1. Redistributions of source code must retain the above
8 # copyright notice, this list of conditions and the following
11 # 2. Redistributions in binary form must reproduce the above
12 # copyright notice, this list of conditions and the following
13 # disclaimer in the documentation and/or other materials
14 # provided with the distribution.
16 # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 # POSSIBILITY OF SUCH DAMAGE.
29 # The views and conclusions contained in the software and
30 # documentation are those of the authors and should not be
31 # interpreted as representing official policies, either expressed
32 # or implied, of GRNET S.A.
35 from sqlalchemy import Table, Integer, BigInteger, DECIMAL, Column, String, MetaData, ForeignKey
36 from sqlalchemy.types import Text
37 from sqlalchemy.schema import Index, Sequence
38 from sqlalchemy.sql import func, and_, or_, null, select, bindparam, text
39 from sqlalchemy.ext.compiler import compiles
40 from sqlalchemy.engine.reflection import Inspector
42 from dbworker import DBWorker
46 ( SERIAL, NODE, HASH, SIZE, SOURCE, MTIME, MUSER, CLUSTER ) = range(8)
51 def strnextling(prefix):
52 """Return the first unicode string
53 greater than but not starting with given prefix.
54 strnextling('hello') -> 'hellp'
57 ## all strings start with the null string,
58 ## therefore we have to approximate strnextling('')
59 ## with the last unicode character supported by python
60 ## 0x10ffff for wide (32-bit unicode) python builds
61 ## 0x00ffff for narrow (16-bit unicode) python builds
62 ## We will not autodetect. 0xffff is safe enough.
71 def strprevling(prefix):
72 """Return an approximation of the last unicode string
73 less than but not starting with given prefix.
74 strprevling(u'hello') -> u'helln\\xffff'
77 ## There is no prevling for the null string
82 s += unichr(c-1) + unichr(0xffff)
99 """Nodes store path organization and have multiple versions.
100 Versions store object history and have multiple attributes.
101 Attributes store metadata.
104 # TODO: Provide an interface for included and excluded clusters.
106 def __init__(self, **params):
107 DBWorker.__init__(self, **params)
108 metadata = MetaData()
112 columns.append(Column('node', Integer, primary_key=True))
113 columns.append(Column('parent', Integer,
114 ForeignKey('nodes.node',
117 autoincrement=False))
119 columns.append(Column('path', String(path_length), default='', nullable=False))
120 self.nodes = Table('nodes', metadata, *columns, mysql_engine='InnoDB')
124 columns.append(Column('node', Integer,
125 ForeignKey('nodes.node',
129 columns.append(Column('key', String(255), primary_key=True))
130 columns.append(Column('value', String(255)))
131 self.policies = Table('policy', metadata, *columns, mysql_engine='InnoDB')
133 #create statistics table
135 columns.append(Column('node', Integer,
136 ForeignKey('nodes.node',
140 columns.append(Column('population', Integer, nullable=False, default=0))
141 columns.append(Column('size', BigInteger, nullable=False, default=0))
142 columns.append(Column('mtime', DECIMAL(precision=16, scale=6)))
143 columns.append(Column('cluster', Integer, nullable=False, default=0,
144 primary_key=True, autoincrement=False))
145 self.statistics = Table('statistics', metadata, *columns, mysql_engine='InnoDB')
147 #create versions table
149 columns.append(Column('serial', Integer, primary_key=True))
150 columns.append(Column('node', Integer,
151 ForeignKey('nodes.node',
153 onupdate='CASCADE')))
154 columns.append(Column('hash', String(255)))
155 columns.append(Column('size', BigInteger, nullable=False, default=0))
156 columns.append(Column('source', Integer))
157 columns.append(Column('mtime', DECIMAL(precision=16, scale=6)))
158 columns.append(Column('muser', String(255), nullable=False, default=''))
159 columns.append(Column('cluster', Integer, nullable=False, default=0))
160 self.versions = Table('versions', metadata, *columns, mysql_engine='InnoDB')
161 Index('idx_versions_node_mtime', self.versions.c.node,
162 self.versions.c.mtime)
164 #create attributes table
166 columns.append(Column('serial', Integer,
167 ForeignKey('versions.serial',
171 columns.append(Column('key', String(255), primary_key=True))
172 columns.append(Column('value', String(255)))
173 self.attributes = Table('attributes', metadata, *columns, mysql_engine='InnoDB')
175 metadata.create_all(self.engine)
177 # the following code creates an index of specific length
178 # this can be accompliced in sqlalchemy >= 0.7.3
179 # providing mysql_length option during index creation
180 insp = Inspector.from_engine(self.engine)
181 indexes = [elem['name'] for elem in insp.get_indexes('nodes')]
182 if 'idx_nodes_path' not in indexes:
183 explicit_length = '(%s)' %path_length if self.engine.name == 'mysql' else ''
184 s = text('CREATE INDEX idx_nodes_path ON nodes (path%s)' %explicit_length)
185 self.conn.execute(s).close()
187 s = self.nodes.select().where(and_(self.nodes.c.node == ROOTNODE,
188 self.nodes.c.parent == ROOTNODE))
189 rp = self.conn.execute(s)
193 s = self.nodes.insert().values(node=ROOTNODE, parent=ROOTNODE)
196 def node_create(self, parent, path):
197 """Create a new node from the given properties.
198 Return the node identifier of the new node.
200 #TODO catch IntegrityError?
201 s = self.nodes.insert().values(parent=parent, path=path)
202 r = self.conn.execute(s)
203 inserted_primary_key = r.inserted_primary_key[0]
205 return inserted_primary_key
207 def node_lookup(self, path):
208 """Lookup the current node of the given path.
209 Return None if the path is not found.
212 s = select([self.nodes.c.node], self.nodes.c.path.like(path))
213 r = self.conn.execute(s)
220 def node_get_properties(self, node):
221 """Return the node's (parent, path).
222 Return None if the node is not found.
225 s = select([self.nodes.c.parent, self.nodes.c.path])
226 s = s.where(self.nodes.c.node == node)
227 r = self.conn.execute(s)
232 def node_get_versions(self, node, keys=(), propnames=_propnames):
233 """Return the properties of all versions at node.
234 If keys is empty, return all properties in the order
235 (serial, node, size, source, mtime, muser, cluster).
238 s = select([self.versions.c.serial,
239 self.versions.c.node,
240 self.versions.c.hash,
241 self.versions.c.size,
242 self.versions.c.source,
243 self.versions.c.mtime,
244 self.versions.c.muser,
245 self.versions.c.cluster], self.versions.c.node == node)
246 s = s.order_by(self.versions.c.serial)
247 r = self.conn.execute(s)
256 return [[p[propnames[k]] for k in keys if k in propnames] for p in rows]
258 def node_count_children(self, node):
259 """Return node's child count."""
261 s = select([func.count(self.nodes.c.node)])
262 s = s.where(and_(self.nodes.c.parent == node,
263 self.nodes.c.node != ROOTNODE))
264 r = self.conn.execute(s)
269 def node_purge_children(self, parent, before=inf, cluster=0):
270 """Delete all versions with the specified
271 parent and cluster, and return
272 the hashes of versions deleted.
273 Clears out nodes with no remaining versions.
276 c1 = select([self.nodes.c.node],
277 self.nodes.c.parent == parent)
278 where_clause = and_(self.versions.c.node.in_(c1),
279 self.versions.c.cluster == cluster)
280 s = select([func.count(self.versions.c.serial),
281 func.sum(self.versions.c.size)])
282 s = s.where(where_clause)
284 s = s.where(self.versions.c.mtime <= before)
285 r = self.conn.execute(s)
290 nr, size = row[0], -row[1] if row[1] else 0
292 self.statistics_update(parent, -nr, size, mtime, cluster)
293 self.statistics_update_ancestors(parent, -nr, size, mtime, cluster)
295 s = select([self.versions.c.hash])
296 s = s.where(where_clause)
297 r = self.conn.execute(s)
298 hashes = [row[0] for row in r.fetchall()]
302 s = self.versions.delete().where(where_clause)
303 r = self.conn.execute(s)
307 s = select([self.nodes.c.node],
308 and_(self.nodes.c.parent == parent,
309 select([func.count(self.versions.c.serial)],
310 self.versions.c.node == self.nodes.c.node).as_scalar() == 0))
311 rp = self.conn.execute(s)
312 nodes = [r[0] for r in rp.fetchall()]
314 s = self.nodes.delete().where(self.nodes.c.node.in_(nodes))
315 self.conn.execute(s).close()
319 def node_purge(self, node, before=inf, cluster=0):
320 """Delete all versions with the specified
321 node and cluster, and return
322 the hashes of versions deleted.
323 Clears out the node if it has no remaining versions.
327 s = select([func.count(self.versions.c.serial),
328 func.sum(self.versions.c.size)])
329 where_clause = and_(self.versions.c.node == node,
330 self.versions.c.cluster == cluster)
331 s = s.where(where_clause)
333 s = s.where(self.versions.c.mtime <= before)
334 r = self.conn.execute(s)
336 nr, size = row[0], row[1]
341 self.statistics_update_ancestors(node, -nr, -size, mtime, cluster)
343 s = select([self.versions.c.hash])
344 s = s.where(where_clause)
345 r = self.conn.execute(s)
346 hashes = [r[0] for r in r.fetchall()]
350 s = self.versions.delete().where(where_clause)
351 r = self.conn.execute(s)
355 s = select([self.nodes.c.node],
356 and_(self.nodes.c.node == node,
357 select([func.count(self.versions.c.serial)],
358 self.versions.c.node == self.nodes.c.node).as_scalar() == 0))
359 r = self.conn.execute(s)
362 s = self.nodes.delete().where(self.nodes.c.node.in_(nodes))
363 self.conn.execute(s).close()
367 def node_remove(self, node):
368 """Remove the node specified.
369 Return false if the node has children or is not found.
372 if self.node_count_children(node):
376 s = select([func.count(self.versions.c.serial),
377 func.sum(self.versions.c.size),
378 self.versions.c.cluster])
379 s = s.where(self.versions.c.node == node)
380 s = s.group_by(self.versions.c.cluster)
381 r = self.conn.execute(s)
382 for population, size, cluster in r.fetchall():
383 self.statistics_update_ancestors(node, -population, -size, mtime, cluster)
386 s = self.nodes.delete().where(self.nodes.c.node == node)
387 self.conn.execute(s).close()
390 def policy_get(self, node):
391 s = select([self.policies.c.key, self.policies.c.value],
392 self.policies.c.node==node)
393 r = self.conn.execute(s)
394 d = dict(r.fetchall())
398 def policy_set(self, node, policy):
400 for k, v in policy.iteritems():
401 s = self.policies.update().where(and_(self.policies.c.node == node,
402 self.policies.c.key == k))
403 s = s.values(value = v)
404 rp = self.conn.execute(s)
407 s = self.policies.insert()
408 values = {'node':node, 'key':k, 'value':v}
409 r = self.conn.execute(s, values)
412 def statistics_get(self, node, cluster=0):
413 """Return population, total size and last mtime
414 for all versions under node that belong to the cluster.
417 s = select([self.statistics.c.population,
418 self.statistics.c.size,
419 self.statistics.c.mtime])
420 s = s.where(and_(self.statistics.c.node == node,
421 self.statistics.c.cluster == cluster))
422 r = self.conn.execute(s)
427 def statistics_update(self, node, population, size, mtime, cluster=0):
428 """Update the statistics of the given node.
429 Statistics keep track the population, total
430 size of objects and mtime in the node's namespace.
431 May be zero or positive or negative numbers.
433 s = select([self.statistics.c.population, self.statistics.c.size],
434 and_(self.statistics.c.node == node,
435 self.statistics.c.cluster == cluster))
436 rp = self.conn.execute(s)
440 prepopulation, presize = (0, 0)
442 prepopulation, presize = r
443 population += prepopulation
448 u = self.statistics.update().where(and_(self.statistics.c.node==node,
449 self.statistics.c.cluster==cluster))
450 u = u.values(population=population, size=size, mtime=mtime)
451 rp = self.conn.execute(u)
454 ins = self.statistics.insert()
455 ins = ins.values(node=node, population=population, size=size,
456 mtime=mtime, cluster=cluster)
457 self.conn.execute(ins).close()
459 def statistics_update_ancestors(self, node, population, size, mtime, cluster=0):
460 """Update the statistics of the given node's parent.
461 Then recursively update all parents up to the root.
462 Population is not recursive.
468 props = self.node_get_properties(node)
472 self.statistics_update(parent, population, size, mtime, cluster)
474 population = 0 # Population isn't recursive
476 def statistics_latest(self, node, before=inf, except_cluster=0):
477 """Return population, total size and last mtime
478 for all latest versions under node that
479 do not belong to the cluster.
483 props = self.node_get_properties(node)
488 # The latest version.
489 s = select([self.versions.c.serial,
490 self.versions.c.node,
491 self.versions.c.hash,
492 self.versions.c.size,
493 self.versions.c.source,
494 self.versions.c.mtime,
495 self.versions.c.muser,
496 self.versions.c.cluster])
497 filtered = select([func.max(self.versions.c.serial)],
498 self.versions.c.node == node)
500 filtered = filtered.where(self.versions.c.mtime < before)
501 s = s.where(and_(self.versions.c.cluster != except_cluster,
502 self.versions.c.serial == filtered))
503 r = self.conn.execute(s)
510 # First level, just under node (get population).
511 v = self.versions.alias('v')
512 s = select([func.count(v.c.serial),
514 func.max(v.c.mtime)])
515 c1 = select([func.max(self.versions.c.serial)])
517 c1 = c1.where(self.versions.c.mtime < before)
518 c2 = select([self.nodes.c.node], self.nodes.c.parent == node)
519 s = s.where(and_(v.c.serial == c1.where(self.versions.c.node == v.c.node),
520 v.c.cluster != except_cluster,
522 rp = self.conn.execute(s)
528 mtime = max(mtime, r[2])
532 # All children (get size and mtime).
533 # XXX: This is why the full path is stored.
534 s = select([func.count(v.c.serial),
536 func.max(v.c.mtime)])
537 c1 = select([func.max(self.versions.c.serial)],
538 self.versions.c.node == v.c.node)
540 c1 = c1.where(self.versions.c.mtime < before)
541 c2 = select([self.nodes.c.node], self.nodes.c.path.like(path + '%'))
542 s = s.where(and_(v.c.serial == c1,
543 v.c.cluster != except_cluster,
545 rp = self.conn.execute(s)
550 size = r[1] - props[SIZE]
551 mtime = max(mtime, r[2])
552 return (count, size, mtime)
554 def version_create(self, node, hash, size, source, muser, cluster=0):
555 """Create a new version from the given properties.
556 Return the (serial, mtime) of the new version.
560 s = self.versions.insert().values(node=node, hash=hash, size=size, source=source,
561 mtime=mtime, muser=muser, cluster=cluster)
562 serial = self.conn.execute(s).inserted_primary_key[0]
563 self.statistics_update_ancestors(node, 1, size, mtime, cluster)
566 def version_lookup(self, node, before=inf, cluster=0):
567 """Lookup the current version of the given node.
568 Return a list with its properties:
569 (serial, node, hash, size, source, mtime, muser, cluster)
570 or None if the current version is not found in the given cluster.
573 v = self.versions.alias('v')
574 s = select([v.c.serial, v.c.node, v.c.hash, v.c.size,
575 v.c.source, v.c.mtime, v.c.muser, v.c.cluster])
576 c = select([func.max(self.versions.c.serial)],
577 self.versions.c.node == node)
579 c = c.where(self.versions.c.mtime < before)
580 s = s.where(and_(v.c.serial == c,
581 v.c.cluster == cluster))
582 r = self.conn.execute(s)
589 def version_get_properties(self, serial, keys=(), propnames=_propnames):
590 """Return a sequence of values for the properties of
591 the version specified by serial and the keys, in the order given.
592 If keys is empty, return all properties in the order
593 (serial, node, hash, size, source, mtime, muser, cluster).
596 v = self.versions.alias()
597 s = select([v.c.serial, v.c.node, v.c.hash, v.c.size,
598 v.c.source, v.c.mtime, v.c.muser, v.c.cluster], v.c.serial == serial)
599 rp = self.conn.execute(s)
607 return [r[propnames[k]] for k in keys if k in propnames]
609 def version_recluster(self, serial, cluster):
610 """Move the version into another cluster."""
612 props = self.version_get_properties(serial)
617 oldcluster = props[CLUSTER]
618 if cluster == oldcluster:
622 self.statistics_update_ancestors(node, -1, -size, mtime, oldcluster)
623 self.statistics_update_ancestors(node, 1, size, mtime, cluster)
625 s = self.versions.update()
626 s = s.where(self.versions.c.serial == serial)
627 s = s.values(cluster = cluster)
628 self.conn.execute(s).close()
630 def version_remove(self, serial):
631 """Remove the serial specified."""
633 props = self.version_get_properties(serial)
639 cluster = props[CLUSTER]
642 self.statistics_update_ancestors(node, -1, -size, mtime, cluster)
644 s = self.versions.delete().where(self.versions.c.serial == serial)
645 self.conn.execute(s).close()
648 def attribute_get(self, serial, keys=()):
649 """Return a list of (key, value) pairs of the version specified by serial.
650 If keys is empty, return all attributes.
651 Othwerise, return only those specified.
655 attrs = self.attributes.alias()
656 s = select([attrs.c.key, attrs.c.value])
657 s = s.where(and_(attrs.c.key.in_(keys),
658 attrs.c.serial == serial))
660 attrs = self.attributes.alias()
661 s = select([attrs.c.key, attrs.c.value])
662 s = s.where(attrs.c.serial == serial)
663 r = self.conn.execute(s)
668 def attribute_set(self, serial, items):
669 """Set the attributes of the version specified by serial.
670 Receive attributes as an iterable of (key, value) pairs.
675 s = self.attributes.update()
676 s = s.where(and_(self.attributes.c.serial == serial,
677 self.attributes.c.key == k))
678 s = s.values(value = v)
679 rp = self.conn.execute(s)
682 s = self.attributes.insert()
683 s = s.values(serial=serial, key=k, value=v)
684 self.conn.execute(s).close()
686 def attribute_del(self, serial, keys=()):
687 """Delete attributes of the version specified by serial.
688 If keys is empty, delete all attributes.
689 Otherwise delete those specified.
693 #TODO more efficient way to do this?
695 s = self.attributes.delete()
696 s = s.where(and_(self.attributes.c.serial == serial,
697 self.attributes.c.key == key))
698 self.conn.execute(s).close()
700 s = self.attributes.delete()
701 s = s.where(self.attributes.c.serial == serial)
702 self.conn.execute(s).close()
704 def attribute_copy(self, source, dest):
705 s = select([dest, self.attributes.c.key, self.attributes.c.value],
706 self.attributes.c.serial == source)
707 rp = self.conn.execute(s)
708 attributes = rp.fetchall()
710 for dest, k, v in attributes:
712 s = self.attributes.update().where(and_(
713 self.attributes.c.serial == dest,
714 self.attributes.c.key == k))
715 rp = self.conn.execute(s, value=v)
718 s = self.attributes.insert()
719 values = {'serial':dest, 'key':k, 'value':v}
720 self.conn.execute(s, values).close()
722 def latest_attribute_keys(self, parent, before=inf, except_cluster=0, pathq=[]):
723 """Return a list with all keys pairs defined
724 for all latest versions under parent that
725 do not belong to the cluster.
728 # TODO: Use another table to store before=inf results.
729 a = self.attributes.alias('a')
730 v = self.versions.alias('v')
731 n = self.nodes.alias('n')
732 s = select([a.c.key]).distinct()
733 filtered = select([func.max(self.versions.c.serial)])
735 filtered = filtered.where(self.versions.c.mtime < before)
736 s = s.where(v.c.serial == filtered.where(self.versions.c.node == v.c.node))
737 s = s.where(v.c.cluster != except_cluster)
738 s = s.where(v.c.node.in_(select([self.nodes.c.node],
739 self.nodes.c.parent == parent)))
740 s = s.where(a.c.serial == v.c.serial)
741 s = s.where(n.c.node == v.c.node)
744 conj.append(n.c.path.like(x + '%'))
746 s = s.where(or_(*conj))
747 rp = self.conn.execute(s)
750 return [r[0] for r in rows]
752 def latest_version_list(self, parent, prefix='', delimiter=None,
753 start='', limit=10000, before=inf,
754 except_cluster=0, pathq=[], filterq=None):
755 """Return a (list of (path, serial) tuples, list of common prefixes)
756 for the current versions of the paths with the given parent,
757 matching the following criteria.
759 The property tuple for a version is returned if all
760 of these conditions are true:
766 c. path starts with prefix (and paths in pathq)
768 d. version is the max up to before
770 e. version is not in cluster
772 f. the path does not have the delimiter occuring
773 after the prefix, or ends with the delimiter
775 g. serial matches the attribute filter query.
777 A filter query is a comma-separated list of
778 terms in one of these three forms:
781 an attribute with this key must exist
784 an attribute with this key must not exist
787 the attribute with this key satisfies the value
788 where ?op is one of ==, != <=, >=, <, >.
790 The list of common prefixes includes the prefixes
791 matching up to the first delimiter after prefix,
792 and are reported only once, as "virtual directories".
793 The delimiter is included in the prefixes.
795 If arguments are None, then the corresponding matching rule
798 Limit applies to the first list of tuples returned.
801 if not start or start < prefix:
802 start = strprevling(prefix)
803 nextling = strnextling(prefix)
805 a = self.attributes.alias('a')
806 v = self.versions.alias('v')
807 n = self.nodes.alias('n')
808 s = select([n.c.path, v.c.serial]).distinct()
809 filtered = select([func.max(self.versions.c.serial)])
811 filtered = filtered.where(self.versions.c.mtime < before)
812 s = s.where(v.c.serial == filtered.where(self.versions.c.node == v.c.node))
813 s = s.where(v.c.cluster != except_cluster)
814 s = s.where(v.c.node.in_(select([self.nodes.c.node],
815 self.nodes.c.parent == parent)))
817 s = s.where(a.c.serial == v.c.serial)
819 s = s.where(n.c.node == v.c.node)
820 s = s.where(and_(n.c.path > bindparam('start'), n.c.path < nextling))
823 conj.append(n.c.path.like(x + '%'))
826 s = s.where(or_(*conj))
829 s = s.where(a.c.key.in_(filterq.split(',')))
831 s = s.order_by(n.c.path)
835 rp = self.conn.execute(s, start=start)
844 pappend = prefixes.append
846 mappend = matches.append
848 rp = self.conn.execute(s, start=start)
850 props = rp.fetchone()
854 idx = path.find(delimiter, pfz)
863 if idx + dz == len(path):
866 continue # Get one more, in case there is a path.
872 rp = self.conn.execute(s, start=strnextling(pf)) # New start.
875 return matches, prefixes