Home / Class/ LocalFileStore Class — langchain Architecture

LocalFileStore Class — langchain Architecture

Architecture documentation for the LocalFileStore class in file_system.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  26189a3b_3a87_0b57_a7a3_341b06895b61["LocalFileStore"]
  65fd1ed9_1e61_cb93_a2e2_c8bb0f0859d6["file_system.py"]
  26189a3b_3a87_0b57_a7a3_341b06895b61 -->|defined in| 65fd1ed9_1e61_cb93_a2e2_c8bb0f0859d6
  f7109763_abb1_4a39_432a_903d925cc9ae["__init__()"]
  26189a3b_3a87_0b57_a7a3_341b06895b61 -->|method| f7109763_abb1_4a39_432a_903d925cc9ae
  60db82b3_b672_7a32_40bd_32b87d30a271["_get_full_path()"]
  26189a3b_3a87_0b57_a7a3_341b06895b61 -->|method| 60db82b3_b672_7a32_40bd_32b87d30a271
  83113a73_3791_1847_70bf_78cc40d77ca4["_mkdir_for_store()"]
  26189a3b_3a87_0b57_a7a3_341b06895b61 -->|method| 83113a73_3791_1847_70bf_78cc40d77ca4
  2519266e_27f4_b661_fa43_c386b4c1ef32["mget()"]
  26189a3b_3a87_0b57_a7a3_341b06895b61 -->|method| 2519266e_27f4_b661_fa43_c386b4c1ef32
  3a0a7a2c_34bc_b7f9_16d1_67eec3706628["mset()"]
  26189a3b_3a87_0b57_a7a3_341b06895b61 -->|method| 3a0a7a2c_34bc_b7f9_16d1_67eec3706628
  a12d1084_c2b3_8eeb_5588_395855d5b285["mdelete()"]
  26189a3b_3a87_0b57_a7a3_341b06895b61 -->|method| a12d1084_c2b3_8eeb_5588_395855d5b285
  967e8c30_eae3_c5ff_bf36_9f8ef2e82c28["yield_keys()"]
  26189a3b_3a87_0b57_a7a3_341b06895b61 -->|method| 967e8c30_eae3_c5ff_bf36_9f8ef2e82c28

Relationship Graph

Source Code

libs/langchain/langchain_classic/storage/file_system.py lines 12–164

class LocalFileStore(ByteStore):
    """`BaseStore` interface that works on the local file system.

    Examples:
        Create a `LocalFileStore` instance and perform operations on it:

        ```python
        from langchain_classic.storage import LocalFileStore

        # Instantiate the LocalFileStore with the root path
        file_store = LocalFileStore("/path/to/root")

        # Set values for keys
        file_store.mset([("key1", b"value1"), ("key2", b"value2")])

        # Get values for keys
        values = file_store.mget(["key1", "key2"])  # Returns [b"value1", b"value2"]

        # Delete keys
        file_store.mdelete(["key1"])

        # Iterate over keys
        for key in file_store.yield_keys():
            print(key)  # noqa: T201
        ```
    """

    def __init__(
        self,
        root_path: str | Path,
        *,
        chmod_file: int | None = None,
        chmod_dir: int | None = None,
        update_atime: bool = False,
    ) -> None:
        """Implement the `BaseStore` interface for the local file system.

        Args:
            root_path: The root path of the file store. All keys are interpreted as
                paths relative to this root.
            chmod_file: Sets permissions for newly created files, overriding the
                current `umask` if needed.
            chmod_dir: Sets permissions for newly created dirs, overriding the
                current `umask` if needed.
            update_atime: Updates the filesystem access time (but not the modified
                time) when a file is read. This allows MRU/LRU cache policies to be
                implemented for filesystems where access time updates are disabled.
        """
        self.root_path = Path(root_path).absolute()
        self.chmod_file = chmod_file
        self.chmod_dir = chmod_dir
        self.update_atime = update_atime

    def _get_full_path(self, key: str) -> Path:
        """Get the full path for a given key relative to the root path.

        Args:
            key: The key relative to the root path.

        Returns:
            The full path for the given key.
        """
        if not re.match(r"^[a-zA-Z0-9_.\-/]+$", key):
            msg = f"Invalid characters in key: {key}"
            raise InvalidKeyException(msg)
        full_path = (self.root_path / key).resolve()
        root_path = self.root_path.resolve()
        common_path = os.path.commonpath([root_path, full_path])
        if common_path != str(root_path):
            msg = (
                f"Invalid key: {key}. Key should be relative to the full path. "
                f"{root_path} vs. {common_path} and full path of {full_path}"
            )
            raise InvalidKeyException(msg)

        return full_path

    def _mkdir_for_store(self, dir_path: Path) -> None:
        """Makes a store directory path (including parents) with specified permissions.

        This is needed because `Path.mkdir()` is restricted by the current `umask`,

Frequently Asked Questions

What is the LocalFileStore class?
LocalFileStore is a class in the langchain codebase, defined in libs/langchain/langchain_classic/storage/file_system.py.
Where is LocalFileStore defined?
LocalFileStore is defined in libs/langchain/langchain_classic/storage/file_system.py at line 12.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free