def __init__(self, path, auto_merge=True, dead_bytes_threshold=0.5, max_immutable_files=20): """Initialize the instance. @param auto_merge: disable auto merge/compaction. @param dead_bytes_threshold: the limit factor of dead vs live bytes to trigger a merge and/or live file rotation. @param max_immutable_files: the max number of inactive files to use, once this value is reached a merge is triggered. """ logger.info("Initializing Tritcask on: %s", path) self._keydir = Keydir() self.base_path = path self.dead_bytes_threshold = dead_bytes_threshold self.max_immutable_files = max_immutable_files self.auto_merge = auto_merge if not path_exists(self.base_path): make_dir(self.base_path, recursive=True) elif not is_dir(self.base_path): raise ValueError("path must be a directory.") self.live_file = None self._immutable = {} self._find_data_files() self._build_keydir() # now check if we should rotate the live file # and merge immutable ones self._rotate_and_merge() # check if we found a live data file # if not, define one (it will be created later) if self.live_file is None: # it's a clean start, let's create the first file self.live_file = DataFile(self.base_path)
def add_share(self, share): """Add share to the shares dict.""" self.shares[share.id] = share # if the share don't exists, create it if not path_exists(share.path): make_dir(share.path) # if it's a ro share, change the perms if not share.can_write(): set_dir_readonly(share.path)
def test_movetotrash_dir_ok(self): """Move a dir to trash ok. Just check it was removed because can't monkeypatch the trash to see that that was actually called. """ path = os.path.join(self.basedir, 'foo') make_dir(path) move_to_trash(path) self.assertFalse(path_exists(path))
def test_create_dirs_already_exists_but_not_symlink(self): """test that creating a Main instance works as expected.""" link = os.path.join(self.root, 'Shared With Me') make_dir(link, recursive=True) self.assertTrue(path_exists(link)) self.assertFalse(is_link(link)) self.assertTrue(path_exists(self.shares)) self.assertTrue(path_exists(self.root)) main = self.build_main() # check that the shares link is actually a link self.assertEquals(main.shares_dir_link, link) self.assertFalse(is_link(main.shares_dir_link))
def _create_recursive_move_dirs(self): """Create the dirs used for the recursive move tests.""" filename = "foo" self.src_dir = os.path.join(self.basedir, 'src') make_dir(self.src_dir, recursive=True) self.dst_dir = os.path.join(self.basedir, 'dst') make_dir(self.dst_dir, recursive=True) self.src_file = os.path.join(self.src_dir, filename) self.dst_file = os.path.join(self.dst_dir, filename) # lets assume we can create a dir in a diff fs # add some fake data with open_file(self.src_file, "wb") as f: f.write("spam")
def _check_and_create_dirs(self, path): """ check if the path isn't a file and in case it don't exists, creates it """ try: stat_result = stat_path(path) # is a regular file? if stat.S_ISREG(stat_result.st_mode): remove_file(path) make_dir(path, True) # else, the dir is already there except OSError, e: if e.errno == errno.ENOENT: # the file or dir don't exist make_dir(path, True) else: raise
def _create_paths(self): """Create the paths for the tests. The following structure will be created: self.basedir/ |-> self.testfile |-> dir0/ |-> file0 |-> link |-> dir1/ |-> file1 |-> dir11/ |-> dir2/ |-> file2 |-> dir3/ """ open_file(self.testfile, 'w').close() for i in xrange(3): dir_name = 'dir%i' % i dir_path = os.path.join(self.basedir, dir_name) make_dir(dir_path, recursive=True) file_name = 'file%i' % i file_path = os.path.join(dir_path, file_name) open_file(file_path, "w").close() make_link(os.path.devnull, os.path.join(self.basedir, 'dir0', 'link')) make_dir(os.path.join(self.basedir, 'dir1', 'dir11')) make_dir(os.path.join(self.basedir, 'dir3'), recursive=True)
def test_make_dir_recursive_yes(self): """Test the make dir with some dirs, recursive.""" testdir = os.path.join(self.basedir, 'foo', 'bar') assert not path_exists(testdir) make_dir(testdir, recursive=True) self.assertTrue(path_exists(testdir))
def test_make_dir_one(self): """Test the make dir with one dir.""" testdir = os.path.join(self.basedir, 'foodir') assert not path_exists(testdir) make_dir(testdir) self.assertTrue(path_exists(testdir))
def makedirs(self, path): """Custom makedirs that handle ro parent.""" parent = os.path.dirname(path) if path_exists(parent): set_dir_readwrite(parent) make_dir(path, recursive=True)