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, Float, Column, String, MetaData, ForeignKey
36 from sqlalchemy.schema import Index, Sequence
37 from sqlalchemy.sql import func, and_, or_, null, select, bindparam
38 from sqlalchemy.ext.compiler import compiles
40 from dbworker import DBWorker
44 ( SERIAL, NODE, HASH, SIZE, SOURCE, MTIME, MUSER, CLUSTER ) = range(8)
49 def strnextling(prefix):
50 """Return the first unicode string
51 greater than but not starting with given prefix.
52 strnextling('hello') -> 'hellp'
55 ## all strings start with the null string,
56 ## therefore we have to approximate strnextling('')
57 ## with the last unicode character supported by python
58 ## 0x10ffff for wide (32-bit unicode) python builds
59 ## 0x00ffff for narrow (16-bit unicode) python builds
60 ## We will not autodetect. 0xffff is safe enough.
69 def strprevling(prefix):
70 """Return an approximation of the last unicode string
71 less than but not starting with given prefix.
72 strprevling(u'hello') -> u'helln\\xffff'
75 ## There is no prevling for the null string
80 #s += unichr(c-1) + unichr(0xffff)
98 """Nodes store path organization and have multiple versions.
99 Versions store object history and have multiple attributes.
100 Attributes store metadata.
103 # TODO: Provide an interface for included and excluded clusters.
105 def __init__(self, **params):
106 DBWorker.__init__(self, **params)
107 metadata = MetaData()
111 columns.append(Column('node', Integer, primary_key=True))
112 columns.append(Column('parent', Integer,
113 ForeignKey('nodes.node',
116 autoincrement=False))
117 columns.append(Column('path', String(2048), default='', nullable=False))
118 self.nodes = Table('nodes', metadata, *columns)
119 # place an index on path
120 Index('idx_nodes_path', self.nodes.c.path, unique=True)
122 #create statistics table
124 columns.append(Column('node', Integer,
125 ForeignKey('nodes.node',
129 columns.append(Column('population', Integer, nullable=False, default=0))
130 columns.append(Column('size', BigInteger, nullable=False, default=0))
131 columns.append(Column('mtime', Float))
132 columns.append(Column('cluster', Integer, nullable=False, default=0,
134 self.statistics = Table('statistics', metadata, *columns)
136 #create versions table
138 columns.append(Column('serial', Integer, primary_key=True))
139 columns.append(Column('node', Integer,
140 ForeignKey('nodes.node',
142 onupdate='CASCADE')))
143 columns.append(Column('hash', String(255)))
144 columns.append(Column('size', BigInteger, nullable=False, default=0))
145 columns.append(Column('source', Integer))
146 columns.append(Column('mtime', Float))
147 columns.append(Column('muser', String(255), nullable=False, default=''))
148 columns.append(Column('cluster', Integer, nullable=False, default=0))
149 self.versions = Table('versions', metadata, *columns)
150 Index('idx_versions_node_mtime', self.versions.c.node,
151 self.versions.c.mtime)
153 #create attributes table
155 columns.append(Column('serial', Integer,
156 ForeignKey('versions.serial',
160 columns.append(Column('key', String(255), primary_key=True))
161 columns.append(Column('value', String(255)))
162 self.attributes = Table('attributes', metadata, *columns)
164 metadata.create_all(self.engine)
166 s = self.nodes.select().where(and_(self.nodes.c.node == ROOTNODE,
167 self.nodes.c.parent == ROOTNODE))
168 rp = self.conn.execute(s)
172 s = self.nodes.insert().values(node=ROOTNODE, parent=ROOTNODE)
175 def node_create(self, parent, path):
176 """Create a new node from the given properties.
177 Return the node identifier of the new node.
179 #TODO catch IntegrityError?
180 s = self.nodes.insert().values(parent=parent, path=path)
181 r = self.conn.execute(s)
182 inserted_primary_key = r.inserted_primary_key[0]
184 return inserted_primary_key
186 def node_lookup(self, path):
187 """Lookup the current node of the given path.
188 Return None if the path is not found.
191 s = select([self.nodes.c.node], self.nodes.c.path == path)
192 r = self.conn.execute(s)
199 def node_get_properties(self, node):
200 """Return the node's (parent, path).
201 Return None if the node is not found.
204 s = select([self.nodes.c.parent, self.nodes.c.path])
205 s = s.where(self.nodes.c.node == node)
206 r = self.conn.execute(s)
211 def node_get_versions(self, node, keys=(), propnames=_propnames):
212 """Return the properties of all versions at node.
213 If keys is empty, return all properties in the order
214 (serial, node, size, source, mtime, muser, cluster).
217 s = select([self.versions.c.serial,
218 self.versions.c.node,
219 self.versions.c.hash,
220 self.versions.c.size,
221 self.versions.c.source,
222 self.versions.c.mtime,
223 self.versions.c.muser,
224 self.versions.c.cluster], self.versions.c.node == node)
225 s = s.order_by(self.versions.c.serial)
226 r = self.conn.execute(s)
235 return [[p[propnames[k]] for k in keys if k in propnames] for p in rows]
237 def node_count_children(self, node):
238 """Return node's child count."""
240 s = select([func.count(self.nodes.c.node)])
241 s = s.where(and_(self.nodes.c.parent == node,
242 self.nodes.c.node != ROOTNODE))
243 r = self.conn.execute(s)
248 def node_purge_children(self, parent, before=inf, cluster=0):
249 """Delete all versions with the specified
250 parent and cluster, and return
251 the serials of versions deleted.
252 Clears out nodes with no remaining versions.
255 c1 = select([self.nodes.c.node],
256 self.nodes.c.parent == parent)
257 where_clause = and_(self.versions.c.node.in_(c1),
258 self.versions.c.cluster == cluster,
259 self.versions.c.mtime <= before)
260 s = select([func.count(self.versions.c.serial),
261 func.sum(self.versions.c.size)])
262 s = s.where(where_clause)
263 r = self.conn.execute(s)
268 nr, size = row[0], -row[1] if row[1] else 0
270 self.statistics_update(parent, -nr, size, mtime, cluster)
271 self.statistics_update_ancestors(parent, -nr, size, mtime, cluster)
273 s = select([self.versions.c.serial])
274 s = s.where(where_clause)
275 r = self.conn.execute(s)
276 serials = [row[SERIAL] for row in r.fetchall()]
280 s = self.versions.delete().where(where_clause)
281 r = self.conn.execute(s)
285 s = select([self.nodes.c.node],
286 and_(self.nodes.c.parent == parent,
287 select([func.count(self.versions.c.serial)],
288 self.versions.c.node == self.nodes.c.node).as_scalar() == 0))
289 rp = self.conn.execute(s)
290 nodes = [r[0] for r in rp.fetchall()]
292 s = self.nodes.delete().where(self.nodes.c.node.in_(nodes))
293 self.conn.execute(s).close()
297 def node_purge(self, node, before=inf, cluster=0):
298 """Delete all versions with the specified
299 node and cluster, and return
300 the serials of versions deleted.
301 Clears out the node if it has no remaining versions.
305 s = select([func.count(self.versions.c.serial),
306 func.sum(self.versions.c.size)])
307 where_clause = and_(self.versions.c.node == node,
308 self.versions.c.cluster == cluster,
309 self.versions.c.mtime <= before)
310 s = s.where(where_clause)
311 r = self.conn.execute(s)
313 nr, size = row[0], row[1]
318 self.statistics_update_ancestors(node, -nr, -size, mtime, cluster)
320 s = select([self.versions.c.serial])
321 s = s.where(where_clause)
322 r = self.conn.execute(s)
323 serials = [r[SERIAL] for r in r.fetchall()]
327 s = self.versions.delete().where(where_clause)
328 r = self.conn.execute(s)
332 s = select([self.nodes.c.node],
333 and_(self.nodes.c.node == node,
334 select([func.count(self.versions.c.serial)],
335 self.versions.c.node == self.nodes.c.node).as_scalar() == 0))
336 r = self.conn.execute(s)
339 s = self.nodes.delete().where(self.nodes.c.node.in_(nodes))
340 self.conn.execute(s).close()
344 def node_remove(self, node):
345 """Remove the node specified.
346 Return false if the node has children or is not found.
349 if self.node_count_children(node):
353 s = select([func.count(self.versions.c.serial),
354 func.sum(self.versions.c.size),
355 self.versions.c.cluster])
356 s = s.where(self.versions.c.node == node)
357 s = s.group_by(self.versions.c.cluster)
358 r = self.conn.execute(s)
359 for population, size, cluster in r.fetchall():
360 self.statistics_update_ancestors(node, -population, -size, mtime, cluster)
363 s = self.nodes.delete().where(self.nodes.c.node == node)
364 self.conn.execute(s).close()
367 def statistics_get(self, node, cluster=0):
368 """Return population, total size and last mtime
369 for all versions under node that belong to the cluster.
372 s = select([self.statistics.c.population,
373 self.statistics.c.size,
374 self.statistics.c.mtime])
375 s = s.where(and_(self.statistics.c.node == node,
376 self.statistics.c.cluster == cluster))
377 r = self.conn.execute(s)
382 def statistics_update(self, node, population, size, mtime, cluster=0):
383 """Update the statistics of the given node.
384 Statistics keep track the population, total
385 size of objects and mtime in the node's namespace.
386 May be zero or positive or negative numbers.
389 s = select([self.statistics.c.population, self.statistics.c.size],
390 and_(self.statistics.c.node == node,
391 self.statistics.c.cluster == cluster))
392 rp = self.conn.execute(s)
396 prepopulation, presize = (0, 0)
398 prepopulation, presize = r
399 population += prepopulation
404 u = self.statistics.update().where(and_(self.statistics.c.node==node,
405 self.statistics.c.cluster==cluster))
406 u = u.values(population=population, size=size, mtime=mtime)
407 rp = self.conn.execute(u)
410 ins = self.statistics.insert()
411 ins = ins.values(node=node, population=population, size=size,
412 mtime=mtime, cluster=cluster)
413 self.conn.execute(ins).close()
415 def statistics_update_ancestors(self, node, population, size, mtime, cluster=0):
416 """Update the statistics of the given node's parent.
417 Then recursively update all parents up to the root.
418 Population is not recursive.
424 props = self.node_get_properties(node)
428 self.statistics_update(parent, population, size, mtime, cluster)
430 population = 0 # Population isn't recursive
432 def statistics_latest(self, node, before=inf, except_cluster=0):
433 """Return population, total size and last mtime
434 for all latest versions under node that
435 do not belong to the cluster.
439 props = self.node_get_properties(node)
444 # The latest version.
445 s = select([self.versions.c.serial,
446 self.versions.c.node,
447 self.versions.c.hash,
448 self.versions.c.size,
449 self.versions.c.source,
450 self.versions.c.mtime,
451 self.versions.c.muser,
452 self.versions.c.cluster])
453 s = s.where(and_(self.versions.c.cluster != except_cluster,
454 self.versions.c.serial == select(
455 [func.max(self.versions.c.serial)],
456 and_(self.versions.c.node == node,
457 self.versions.c.mtime < before))))
458 r = self.conn.execute(s)
465 # First level, just under node (get population).
466 v = self.versions.alias('v')
467 s = select([func.count(v.c.serial),
469 func.max(v.c.mtime)])
470 c1 = select([func.max(self.versions.c.serial)],
471 and_(self.versions.c.node == v.c.node,
472 self.versions.c.mtime < before))
473 c2 = select([self.nodes.c.node], self.nodes.c.parent == node)
474 s = s.where(and_(v.c.serial == c1,
475 v.c.cluster != except_cluster,
477 rp = self.conn.execute(s)
483 mtime = max(mtime, r[2])
487 # All children (get size and mtime).
488 # XXX: This is why the full path is stored.
489 s = select([func.count(v.c.serial),
491 func.max(v.c.mtime)])
492 c1 = select([func.max(self.versions.c.serial)],
493 and_(self.versions.c.node == v.c.node,
494 self.versions.c.mtime < before))
495 c2 = select([self.nodes.c.node], self.nodes.c.path.like(path + '%'))
496 s = s.where(and_(v.c.serial == c1,
497 v.c.cluster != except_cluster,
499 rp = self.conn.execute(s)
504 size = r[1] - props[SIZE]
505 mtime = max(mtime, r[2])
506 return (count, size, mtime)
508 def version_create(self, node, hash, size, source, muser, cluster=0):
509 """Create a new version from the given properties.
510 Return the (serial, mtime) of the new version.
514 s = self.versions.insert().values(node=node, hash=hash, size=size, source=source,
515 mtime=mtime, muser=muser, cluster=cluster)
516 serial = self.conn.execute(s).inserted_primary_key[0]
517 self.statistics_update_ancestors(node, 1, size, mtime, cluster)
520 def version_lookup(self, node, before=inf, cluster=0):
521 """Lookup the current version of the given node.
522 Return a list with its properties:
523 (serial, node, hash, size, source, mtime, muser, cluster)
524 or None if the current version is not found in the given cluster.
527 v = self.versions.alias('v')
528 s = select([v.c.serial, v.c.node, v.c.hash, v.c.size,
529 v.c.source, v.c.mtime, v.c.muser, v.c.cluster])
530 c = select([func.max(self.versions.c.serial)],
531 and_(self.versions.c.node == node,
532 self.versions.c.mtime < before))
533 s = s.where(and_(v.c.serial == c,
534 v.c.cluster == cluster))
535 r = self.conn.execute(s)
542 def version_get_properties(self, serial, keys=(), propnames=_propnames):
543 """Return a sequence of values for the properties of
544 the version specified by serial and the keys, in the order given.
545 If keys is empty, return all properties in the order
546 (serial, node, hash, size, source, mtime, muser, cluster).
549 v = self.versions.alias()
550 s = select([v.c.serial, v.c.node, v.c.hash, v.c.size,
551 v.c.source, v.c.mtime, v.c.muser, v.c.cluster], v.c.serial == serial)
552 rp = self.conn.execute(s)
560 return [r[propnames[k]] for k in keys if k in propnames]
562 def version_recluster(self, serial, cluster):
563 """Move the version into another cluster."""
565 props = self.version_get_properties(serial)
570 oldcluster = props[CLUSTER]
571 if cluster == oldcluster:
575 self.statistics_update_ancestors(node, -1, -size, mtime, oldcluster)
576 self.statistics_update_ancestors(node, 1, size, mtime, cluster)
578 s = self.versions.update()
579 s = s.where(self.versions.c.serial == serial)
580 s = s.values(cluster = cluster)
581 self.conn.execute(s).close()
583 def version_remove(self, serial):
584 """Remove the serial specified."""
586 props = self.node_get_properties(serial)
591 cluster = props[CLUSTER]
594 self.statistics_update_ancestors(node, -1, -size, mtime, cluster)
596 s = self.versions.delete().where(self.versions.c.serial == serial)
597 self.conn.execute(s).close()
600 def attribute_get(self, serial, keys=()):
601 """Return a list of (key, value) pairs of the version specified by serial.
602 If keys is empty, return all attributes.
603 Othwerise, return only those specified.
607 attrs = self.attributes.alias()
608 s = select([attrs.c.key, attrs.c.value])
609 s = s.where(and_(attrs.c.key.in_(keys),
610 attrs.c.serial == serial))
612 attrs = self.attributes.alias()
613 s = select([attrs.c.key, attrs.c.value])
614 s = s.where(attrs.c.serial == serial)
615 r = self.conn.execute(s)
620 def attribute_set(self, serial, items):
621 """Set the attributes of the version specified by serial.
622 Receive attributes as an iterable of (key, value) pairs.
627 s = self.attributes.update()
628 s = s.where(and_(self.attributes.c.serial == serial,
629 self.attributes.c.key == k))
630 s = s.values(value = v)
631 rp = self.conn.execute(s)
634 s = self.attributes.insert()
635 s = s.values(serial=serial, key=k, value=v)
636 self.conn.execute(s).close()
638 def attribute_del(self, serial, keys=()):
639 """Delete attributes of the version specified by serial.
640 If keys is empty, delete all attributes.
641 Otherwise delete those specified.
645 #TODO more efficient way to do this?
647 s = self.attributes.delete()
648 s = s.where(and_(self.attributes.c.serial == serial,
649 self.attributes.c.key == key))
650 self.conn.execute(s).close()
652 s = self.attributes.delete()
653 s = s.where(self.attributes.c.serial == serial)
654 self.conn.execute(s).close()
656 def attribute_copy(self, source, dest):
657 s = select([dest, self.attributes.c.key, self.attributes.c.value],
658 self.attributes.c.serial == source)
659 rp = self.conn.execute(s)
660 attributes = rp.fetchall()
662 for dest, k, v in attributes:
664 s = self.attributes.update().where(and_(
665 self.attributes.c.serial == dest,
666 self.attributes.c.key == k))
667 rp = self.conn.execute(s, value=v)
670 s = self.attributes.insert()
671 values = {'serial':dest, 'key':k, 'value':v}
672 self.conn.execute(s, values).close()
674 def latest_attribute_keys(self, parent, before=inf, except_cluster=0, pathq=[]):
675 """Return a list with all keys pairs defined
676 for all latest versions under parent that
677 do not belong to the cluster.
680 # TODO: Use another table to store before=inf results.
681 a = self.attributes.alias('a')
682 v = self.versions.alias('v')
683 n = self.nodes.alias('n')
684 s = select([a.c.key]).distinct()
685 s = s.where(v.c.serial == select([func.max(self.versions.c.serial)],
686 and_(self.versions.c.node == v.c.node,
687 self.versions.c.mtime < before)))
688 s = s.where(v.c.cluster != except_cluster)
689 s = s.where(v.c.node.in_(select([self.nodes.c.node],
690 self.nodes.c.parent == parent)))
691 s = s.where(a.c.serial == v.c.serial)
692 s = s.where(n.c.node == v.c.node)
695 conj.append(n.c.path.like(x + '%'))
697 s = s.where(or_(*conj))
698 rp = self.conn.execute(s)
701 return [r[0] for r in rows]
703 def latest_version_list(self, parent, prefix='', delimiter=None,
704 start='', limit=10000, before=inf,
705 except_cluster=0, pathq=[], filterq=None):
706 """Return a (list of (path, serial) tuples, list of common prefixes)
707 for the current versions of the paths with the given parent,
708 matching the following criteria.
710 The property tuple for a version is returned if all
711 of these conditions are true:
717 c. path starts with prefix (and paths in pathq)
719 d. version is the max up to before
721 e. version is not in cluster
723 f. the path does not have the delimiter occuring
724 after the prefix, or ends with the delimiter
726 g. serial matches the attribute filter query.
728 A filter query is a comma-separated list of
729 terms in one of these three forms:
732 an attribute with this key must exist
735 an attribute with this key must not exist
738 the attribute with this key satisfies the value
739 where ?op is one of ==, != <=, >=, <, >.
741 The list of common prefixes includes the prefixes
742 matching up to the first delimiter after prefix,
743 and are reported only once, as "virtual directories".
744 The delimiter is included in the prefixes.
746 If arguments are None, then the corresponding matching rule
749 Limit applies to the first list of tuples returned.
752 if not start or start < prefix:
753 start = strprevling(prefix)
754 nextling = strnextling(prefix)
756 a = self.attributes.alias('a')
757 v = self.versions.alias('v')
758 n = self.nodes.alias('n')
759 s = select([n.c.path, v.c.serial]).distinct()
760 s = s.where(v.c.serial == select([func.max(self.versions.c.serial)],
761 and_(self.versions.c.node == v.c.node,
762 self.versions.c.mtime < before)))
763 s = s.where(v.c.cluster != except_cluster)
764 s = s.where(v.c.node.in_(select([self.nodes.c.node],
765 self.nodes.c.parent == parent)))
767 s = s.where(a.c.serial == v.c.serial)
769 s = s.where(n.c.node == v.c.node)
770 s = s.where(and_(n.c.path > bindparam('start'), n.c.path < nextling))
773 conj.append(n.c.path.like(x + '%'))
776 s = s.where(or_(*conj))
779 s = s.where(a.c.key.in_(filterq.split(',')))
781 s = s.order_by(n.c.path)
785 rp = self.conn.execute(s, start=start)
794 pappend = prefixes.append
796 mappend = matches.append
798 rp = self.conn.execute(s, start=start)
800 props = rp.fetchone()
804 idx = path.find(delimiter, pfz)
813 if idx + dz == len(path):
816 continue # Get one more, in case there is a path.
822 rp = self.conn.execute(s, start=strnextling(pf)) # New start.
825 return matches, prefixes