<commit_before>#
# Copyright (c) 2004-2006 rPath, Inc.
#
# This program is distributed under the terms of the Common Public License,
# version 1.0. A copy of this license should have been distributed with this
# source file in a file called LICENSE. If it is not present, the license
# is always available at http://www.opensource.org/licenses/cpl.php.
#
# 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 Common Public License for
# full details.
#

from conary import trove, versions
from conary.dbstore import idtable
from conary.repository.errors import DuplicateBranch
from conary.repository.netrepos import items

LATEST_TYPE_ANY     = 0         # redirects, removed, and normal
LATEST_TYPE_PRESENT = 1         # redirects and normal
LATEST_TYPE_NORMAL  = 2         # only normal troves

class BranchTable(idtable.IdTable):
    def __init__(self, db):
        idtable.IdTable.__init__(self, db, "Branches", "branchId", "branch")

    def addId(self, branch):
        assert(isinstance(branch, versions.Branch))
        cu = self.db.cursor()
        cu.execute("INSERT INTO Branches (branch) VALUES (?)",
		   branch.asString())
	return cu.lastrowid

    def getId(self, theId):
	return versions.VersionFromString(idtable.IdTable.getId(self, theId))

    def __getitem__(self, branch):
        assert(isinstance(branch, versions.Branch))
	return idtable.IdTable.__getitem__(self, branch.asString())

    def get(self, branch, defValue):
        assert(isinstance(branch, versions.Branch))
	return idtable.IdTable.get(self, branch.asString(), defValue)

    def __delitem__(self, branch):
        assert(isinstance(branch, versions.Branch))
	idtable.IdTable.__delitem__(self, branch.asString())

    def has_key(self, branch):
        assert(isinstance(branch, versions.Branch))
	return idtable.IdTable.has_key(self, branch.asString())

    def iterkeys(self):
	raise NotImplementedError

    def iteritems(self):
	raise NotImplementedError

class LabelTable(idtable.IdTable):
    def __init__(self, db):
        idtable.IdTable.__init__(self, db, 'Labels', 'labelId', 'label')

    def addId(self, label):
	idtable.IdTable.addId(self, label.asString())

    def __getitem__(self, label):
	return idtable.IdTable.__getitem__(self, label.asString())

    def get(self, label, defValue):
	return idtable.IdTable.get(self, label.asString(), defValue)

    def __delitem__(self, label):
	idtable.IdTable.__delitem__(self, label.asString())

    def has_key(self, label):
	return idtable.IdTable.has_key(self, label.asString())

    def iterkeys(self):
	raise NotImplementedError

    def iteritems(self):
	raise NotImplementedError

class LatestTable:
    def __init__(self, db):
        self.db = db

    def _findLatest(self, cu, itemId, branchId, flavorId, troveTypeFilter = ""):
        cu.execute("""
            SELECT versionId, troveType FROM Nodes
                JOIN Instances USING (itemId, versionId)
                WHERE
                    Nodes.itemId = ? AND
                    Nodes.branchId = ? AND
                    flavorId = ? AND
                    isPresent = 1
                    %s
                ORDER BY finalTimestamp DESC
                LIMIT 1
        """ % troveTypeFilter, itemId, branchId, flavorId)

        try:
            latestVersionId, troveType = cu.next()
        except StopIteration:
            latestVersionId = None
            troveType = None

        return latestVersionId, troveType

    def _add(self, cu, itemId, branchId, flavorId, versionId, latestType):
        if versionId is None:
            import epdb
            epdb.st()
        cu.execute("""INSERT INTO Latest 
                        (itemId, branchId, flavorId, versionId, latestType)
                        VALUES (?, ?, ?, ?, ?)""",
                    itemId, branchId, flavorId, versionId, latestType)

    def update(self, itemId, branchId, flavorId):
        cu = self.db.cursor()
        cu.execute("DELETE FROM Latest WHERE itemId=? AND branchId=? AND "
                   "flavorId=?", itemId, branchId, flavorId)

        versionId, troveType = self._findLatest(cu, itemId, branchId, flavorId)

        if versionId is None:
            return

        self._add(cu, itemId, branchId, flavorId, versionId, LATEST_TYPE_ANY)

        if troveType == trove.TROVE_TYPE_NORMAL:
            self._add(cu, itemId, branchId, flavorId, versionId,<commit_msg>remove breakpoint and update docs<commit_after>#
# Copyright (c) 2004-2006 rPath, Inc.
#
# This program is distributed under the terms of the Common Public License,
# version 1.0. A copy of this license should have been distributed with this
# source file in a file called LICENSE. If it is not present, the license
# is always available at http://www.opensource.org/licenses/cpl.php.
#
# 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 Common Public License for
# full details.
#

from conary import trove, versions
from conary.dbstore import idtable
from conary.repository.errors import DuplicateBranch
from conary.repository.netrepos import items

LATEST_TYPE_ANY     = 0         # redirects, removed, and normal
LATEST_TYPE_PRESENT = 1         # redirects and normal
LATEST_TYPE_NORMAL  = 2         # only normal troves that do not have a
                                # redirect as the latest present on that
                                # branch/flavor

class BranchTable(idtable.IdTable):
    def __init__(self, db):
        idtable.IdTable.__init__(self, db, "Branches", "branchId", "branch")

    def addId(self, branch):
        assert(isinstance(branch, versions.Branch))
        cu = self.db.cursor()
        cu.execute("INSERT INTO Branches (branch) VALUES (?)",
		   branch.asString())
	return cu.lastrowid

    def getId(self, theId):
	return versions.VersionFromString(idtable.IdTable.getId(self, theId))

    def __getitem__(self, branch):
        assert(isinstance(branch, versions.Branch))
	return idtable.IdTable.__getitem__(self, branch.asString())

    def get(self, branch, defValue):
        assert(isinstance(branch, versions.Branch))
	return idtable.IdTable.get(self, branch.asString(), defValue)

    def __delitem__(self, branch):
        assert(isinstance(branch, versions.Branch))
	idtable.IdTable.__delitem__(self, branch.asString())

    def has_key(self, branch):
        assert(isinstance(branch, versions.Branch))
	return idtable.IdTable.has_key(self, branch.asString())

    def iterkeys(self):
	raise NotImplementedError

    def iteritems(self):
	raise NotImplementedError

class LabelTable(idtable.IdTable):
    def __init__(self, db):
        idtable.IdTable.__init__(self, db, 'Labels', 'labelId', 'label')

    def addId(self, label):
	idtable.IdTable.addId(self, label.asString())

    def __getitem__(self, label):
	return idtable.IdTable.__getitem__(self, label.asString())

    def get(self, label, defValue):
	return idtable.IdTable.get(self, label.asString(), defValue)

    def __delitem__(self, label):
	idtable.IdTable.__delitem__(self, label.asString())

    def has_key(self, label):
	return idtable.IdTable.has_key(self, label.asString())

    def iterkeys(self):
	raise NotImplementedError

    def iteritems(self):
	raise NotImplementedError

class LatestTable:
    def __init__(self, db):
        self.db = db

    def _findLatest(self, cu, itemId, branchId, flavorId, troveTypeFilter = ""):
        cu.execute("""
            SELECT versionId, troveType FROM Nodes
                JOIN Instances USING (itemId, versionId)
                WHERE
                    Nodes.itemId = ? AND
                    Nodes.branchId = ? AND
                    flavorId = ? AND
                    isPresent = 1
                    %s
                ORDER BY finalTimestamp DESC
                LIMIT 1
        """ % troveTypeFilter, itemId, branchId, flavorId)

        try:
            latestVersionId, troveType = cu.next()
        except StopIteration:
            latestVersionId = None
            troveType = None

        return latestVersionId, troveType

    def _add(self, cu, itemId, branchId, flavorId, versionId, latestType):
        cu.execute("""INSERT INTO Latest
                        (itemId, branchId, flavorId, versionId, latestType)
                        VALUES (?, ?, ?, ?, ?)""",
                    itemId, branchId, flavorId, versionId, latestType)

    def update(self, itemId, branchId, flavorId):
        cu = self.db.cursor()
        cu.execute("DELETE FROM Latest WHERE itemId=? AND branchId=? AND "
                   "flavorId=?", itemId, branchId, flavorId)

        versionId, troveType = self._findLatest(cu, itemId, branchId, flavorId)

        if versionId is None:
            return

        self._add(cu, itemId, branchId, flavorId, versionId, LATEST_TYPE_ANY)

        if troveType == trove.TROVE_TYPE_NORMAL:
            self._add(cu, itemId, branchId, flavorId, versionId,
{'fraction_non_alphanumeric': 0.09631573193396742, 'fraction_numerical': 0.0032797638570022957, 'mean_word_length': 2.7801652892561983, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3299397', 'n_tokens_mistral': 2748, 'n_tokens_neox': 2637, 'n_words': 751}