state: merge State and LinkState classes

Signed-off-by: Ruslan Kuprieiev <[email protected]>
This commit is contained in:
Ruslan Kuprieiev
2018-09-17 10:47:16 +03:00
parent d704c41682
commit 75d7fc2206
3 changed files with 19 additions and 25 deletions
+2 -3
View File
@@ -22,7 +22,7 @@ class Project(object):
def __init__(self, root_dir):
from dvc.logger import Logger
from dvc.config import Config
from dvc.state import LinkState, State
from dvc.state import State
from dvc.lock import Lock
from dvc.scm import SCM
from dvc.cache import Cache
@@ -39,7 +39,6 @@ class Project(object):
# NOTE: storing state and link_state in the repository itself to avoid
# any possible state corruption in 'shared cache dir' scenario.
self.state = State(self)
self.link_state = LinkState(self)
core = self.config._config[Config.SECTION_CORE]
self.logger = Logger(core.get(Config.SECTION_CORE_LOGLEVEL, None))
@@ -322,7 +321,7 @@ class Project(object):
for stage in all_stages:
for out in stage.outs:
used.append(out.path)
self.link_state.remove_unused(used)
self.state.remove_unused_links(used)
def checkout(self, target=None):
all_stages = self.active_stages()
+4 -5
View File
@@ -36,7 +36,6 @@ class RemoteLOCAL(RemoteBase):
def __init__(self, project, config):
self.project = project
self.state = self.project.state
self.link_state = project.link_state
storagepath = config.get(Config.SECTION_AWS_STORAGEPATH, None)
self.cache_dir = config.get(Config.SECTION_REMOTE_URL, storagepath)
@@ -253,7 +252,7 @@ class RemoteLOCAL(RemoteBase):
if not self.is_dir_cache(cache):
self.link(cache, path)
self.link_state.update(path)
self.state.update_link(path)
return
# Create dir separately so that dir is created
@@ -267,7 +266,7 @@ class RemoteLOCAL(RemoteBase):
relpath = entry[self.PARAM_RELPATH]
p = os.path.join(path, relpath)
self.link(c, p)
self.link_state.update(path)
self.state.update_link(path)
def _move(self, inp, outp):
# moving in two stages to make last the move atomic in
@@ -289,7 +288,7 @@ class RemoteLOCAL(RemoteBase):
remove(path)
self.link(cache, path)
self.link_state.update(path)
self.state.update_link(path)
return {self.PARAM_MD5: md5}
@@ -310,7 +309,7 @@ class RemoteLOCAL(RemoteBase):
self.link(c, p)
self.link_state.update(path)
self.state.update_link(path)
return {self.PARAM_MD5: md5}
+13 -17
View File
@@ -19,9 +19,15 @@ class State(object):
"mtime TEXT NOT NULL, " \
"md5 TEXT NOT NULL"
LINK_STATE_TABLE = 'link_state'
LINK_STATE_TABLE_LAYOUT = "path TEXT PRIMARY KEY, " \
"inode INTEGER NOT NULL, " \
"mtime TEXT NOT NULL"
def __init__(self, project):
self.project = project
self.dvc_dir = project.dvc_dir
self.root_dir = project.root_dir
if not self.dvc_dir:
self.state_file = None
@@ -62,7 +68,8 @@ class State(object):
# Check that the state file is indeed a database
cmd = "CREATE TABLE IF NOT EXISTS {} ({})"
c.execute(cmd.format(self.STATE_TABLE, self.STATE_TABLE_LAYOUT))
c.execute("SELECT name FROM sqlite_master WHERE type='table';")
c.execute(cmd.format(self.LINK_STATE_TABLE,
self.LINK_STATE_TABLE_LAYOUT))
db.commit()
c.close()
return db
@@ -163,18 +170,7 @@ class State(object):
return self._get(inode, mtime)
class LinkState(State):
STATE_TABLE = 'link'
STATE_TABLE_LAYOUT = "path TEXT PRIMARY KEY, " \
"inode INTEGER NOT NULL, " \
"mtime TEXT NOT NULL"
def __init__(self, project):
super(LinkState, self).__init__(project)
self.root_dir = project.root_dir
def update(self, path, use_db=None):
def update_link(self, path, use_db=None):
if not os.path.exists(path):
return
@@ -190,7 +186,7 @@ class LinkState(State):
c = db.cursor()
cmd = 'REPLACE INTO {}(path, inode, mtime) ' \
'VALUES ("{}", {}, "{}")'.format(self.STATE_TABLE,
'VALUES ("{}", {}, "{}")'.format(self.LINK_STATE_TABLE,
relpath,
inode,
mtime)
@@ -201,12 +197,12 @@ class LinkState(State):
db.commit()
db.close()
def remove_unused(self, used):
def remove_unused_links(self, used):
unused = []
db = self.load()
c = db.cursor()
c.execute('SELECT * FROM {}'.format(self.STATE_TABLE))
c.execute('SELECT * FROM {}'.format(self.LINK_STATE_TABLE))
for row in c:
p, i, m = row
path = os.path.join(self.root_dir, p)
@@ -228,7 +224,7 @@ class LinkState(State):
db.commit()
for p in unused:
cmd = 'DELETE FROM {} WHERE path = "{}"'
c.execute(cmd.format(self.STATE_TABLE, p))
c.execute(cmd.format(self.LINK_STATE_TABLE, p))
c.close()
db.commit()