🚧 WIP on faster indexing

This commit is contained in:
Nate Sesti 2023-11-10 16:06:10 -08:00
parent 755ccc04ab
commit 403a53c224
4 changed files with 153 additions and 115 deletions

View file

@ -3,7 +3,7 @@ from typing import AsyncGenerator
from ...core.config import ContinueConfig
from ...server.protocols.ide_protocol import AbstractIdeProtocolServer
from ...libs.index.chunkers.chunk_directory import (
stream_chunk_directory,
local_stream_chunk_directory,
)
from ...libs.index.indices.meilisearch_index import MeilisearchCodebaseIndex
from ...libs.index.indices.chroma_index import MAX_CHUNK_SIZE, ChromaCodebaseIndex
@ -62,7 +62,9 @@ async def build_index(
chroma_task = asyncio.create_task(chroma_task())
meilisearch_task = asyncio.create_task(meilisearch_task())
async for chunk, progress in stream_chunk_directory(ide, MAX_CHUNK_SIZE):
for chunk, progress in local_stream_chunk_directory(
ide.workspace_directory, MAX_CHUNK_SIZE
):
if chunk is not None:
buffers[0].append(chunk)
buffers[1].append(chunk)

View file

@ -2,59 +2,14 @@ import asyncio
import os
from typing import AsyncGenerator, Callable, Generator, List, Optional, Tuple
from .ignore import should_ignore_file_factory
from ....server.protocols.ide_protocol import AbstractIdeProtocolServer
from ...util.filter_files import DEFAULT_IGNORE_PATTERNS, should_filter_path
from .chunk import Chunk
from . import chunk_document
from .fast_index import stream_files_to_update
FILE_IGNORE_PATTERNS = [
# File Names
"**/.DS_Store",
"**/package-lock.json",
"**/yarn.lock",
# File Types
"*.log",
"*.ttf",
"*.png",
"*.jpg",
"*.jpeg",
"*.gif",
"*.mp4",
"*.svg",
"*.ico",
"*.pdf",
"*.zip",
"*.gz",
"*.tar",
"*.tgz",
"*.rar",
"*.7z",
"*.exe",
"*.dll",
"*.obj",
"*.o",
"*.a",
"*.lib",
"*.so",
"*.dylib",
"*.ncb",
"*.sdf",
"*.woff",
"*.woff2",
"*.eot",
"*.cur",
"*.avi",
"*.mpg",
"*.mpeg",
"*.mov",
"*.mp3",
"*.mp4",
"*.mkv",
"*.mkv",
"*.webm",
"*.jar",
]
MAX_SIZE_IN_CHARS = 50_000
@ -73,15 +28,6 @@ async def get_contents_of_files(
return await asyncio.gather(*[ide.readFile(file) for file in files])
def gi_basename(path: str) -> str:
if ".gitignore" in path:
return path[: path.index(".gitignore")]
elif ".continueignore" in path:
return path[: path.index(".continueignore")]
else:
return path
async def get_all_filepaths(
ide: AbstractIdeProtocolServer, ignore_files: List[str] = []
) -> Tuple[List[str], Callable[[str], bool]]:
@ -100,30 +46,7 @@ async def get_all_filepaths(
path: content for path, content in zip(gitignore_paths, gitignore_contents)
}
def gitignore_patterns_for_file(filepath: str) -> List[str]:
paths = list(
filter(
lambda gitignore_path: filepath.startswith(gi_basename(gitignore_path)),
gitignore_paths,
)
)
patterns = []
for path in paths:
base = gi_basename(path)
for pattern in gitignores[path].split("\n"):
if pattern.strip() != "":
patterns.append(os.path.join(base, pattern.strip()))
return patterns
def should_ignore_file(filepath: str) -> bool:
return should_filter_path(
filepath,
ignore_files
+ DEFAULT_IGNORE_PATTERNS
+ FILE_IGNORE_PATTERNS
+ gitignore_patterns_for_file(filepath),
)
should_ignore_file = should_ignore_file_factory(ignore_files, gitignores)
return files, should_ignore_file
@ -161,15 +84,20 @@ async def stream_chunk_directory(
def local_stream_chunk_directory(
repo_root: str,
workspace_dir: str,
max_chunk_size: int,
) -> Generator[Tuple[Optional[Chunk], float], None, None]:
for filepath in stream_files_to_update(repo_root):
for filepath in stream_files_to_update(workspace_dir):
# Ignore if the file is too large (cutoff is 10MB)
if os.path.getsize(filepath) > 10_000_000:
continue
contents = open(filepath, "r").read()
try:
contents = open(filepath, "r").read()
except Exception as e:
print(e, filepath)
continue
if contents.strip() == "":
continue

View file

@ -8,8 +8,9 @@ those you have indexed. Then purge any that don't exist.
from contextlib import contextmanager
import os
from typing import Generator
from typing import Callable, Generator
from ...util.paths import getGlobalFolderPath
from .ignore import should_ignore_file_factory, local_find_gitignores
import pygit2
SET_PATH = os.path.join(getGlobalFolderPath(), "set")
@ -73,50 +74,52 @@ def disk_set() -> Generator[DiskSet, None, None]:
set.close()
def stream_files_to_update(repo_root: str) -> Generator[str, None, None]:
repo = pygit2.Repository(repo_root)
def stream_files_to_update(workspace_dir: str) -> Generator[str, None, None]:
repo = pygit2.Repository(workspace_dir)
commit = repo.head.peel(pygit2.Commit)
gitignores = local_find_gitignores(workspace_dir)
should_ignore = should_ignore_file_factory([], gitignores)
with disk_set() as already_indexed:
yield from files_to_update_in(commit, already_indexed)
yield from files_to_update_in(
commit, already_indexed, repo.workdir, should_ignore
)
# Yield from files changed in the working tree
for filepath, flags in repo.status().items():
if flags.status == pygit2.GIT_STATUS_CURRENT:
continue
if flags.status == pygit2.GIT_STATUS_INDEX_NEW:
should_yield = set(
[
pygit2.GIT_STATUS_INDEX_NEW,
pygit2.GIT_STATUS_INDEX_MODIFIED,
pygit2.GIT_STATUS_WT_MODIFIED,
pygit2.GIT_STATUS_WT_NEW,
]
)
for filepath, status in repo.status().items():
if status in should_yield and not should_ignore(filepath):
yield filepath
elif flags.status == pygit2.GIT_STATUS_INDEX_MODIFIED:
yield filepath
elif flags.status == pygit2.GIT_STATUS_INDEX_DELETED:
continue
elif flags.status == pygit2.GIT_STATUS_WT_NEW:
yield filepath
elif flags.status == pygit2.GIT_STATUS_WT_MODIFIED:
yield filepath
elif flags.status == pygit2.GIT_STATUS_WT_DELETED:
continue
elif flags.status == pygit2.GIT_STATUS_IGNORED:
continue
elif flags.status == pygit2.GIT_STATUS_CONFLICTED:
continue
else:
raise Exception("Unknown diff status: " + str(flags))
def files_to_update_in(
obj: pygit2.Object, already_indexed: DiskSet
obj: pygit2.Object,
already_indexed: DiskSet,
path: str,
should_ignore: Callable[[str], bool],
) -> Generator[str, None, None]:
"""Given an object, yield all files to update"""
if obj.hex in already_indexed:
if obj.hex in already_indexed or should_ignore(path):
return
if obj.type == pygit2.GIT_OBJ_BLOB:
yield obj.name
yield path
elif obj.type == pygit2.GIT_OBJ_TREE:
for entry in obj:
yield from files_to_update_in(entry)
yield from files_to_update_in(
entry, already_indexed, os.path.join(path, entry.name), should_ignore
)
elif obj.type == pygit2.GIT_OBJ_COMMIT:
yield from files_to_update_in(obj.tree)
yield from files_to_update_in(obj.tree, already_indexed, path, should_ignore)
else:
raise Exception("Unknown type: " + str(obj.type))
already_indexed.add(obj.hex)

View file

@ -0,0 +1,105 @@
import os
from typing import Callable, Dict, List
from ...util.filter_files import DEFAULT_IGNORE_PATTERNS, should_filter_path
FILE_IGNORE_PATTERNS = [
# File Names
"**/.DS_Store",
"**/package-lock.json",
"**/yarn.lock",
# File Types
"*.log",
"*.ttf",
"*.png",
"*.jpg",
"*.jpeg",
"*.gif",
"*.mp4",
"*.svg",
"*.ico",
"*.pdf",
"*.zip",
"*.gz",
"*.tar",
"*.tgz",
"*.rar",
"*.7z",
"*.exe",
"*.dll",
"*.obj",
"*.o",
"*.a",
"*.lib",
"*.so",
"*.dylib",
"*.ncb",
"*.sdf",
"*.woff",
"*.woff2",
"*.eot",
"*.cur",
"*.avi",
"*.mpg",
"*.mpeg",
"*.mov",
"*.mp3",
"*.mp4",
"*.mkv",
"*.mkv",
"*.webm",
"*.jar",
]
def gi_basename(path: str) -> str:
if ".gitignore" in path:
return path[: path.index(".gitignore")]
elif ".continueignore" in path:
return path[: path.index(".continueignore")]
else:
return path
def local_find_gitignores(workspace_dir: str) -> Dict[str, str]:
gitignores = {}
for root, dirs, files in os.walk(workspace_dir):
for file in files:
if file.endswith(".gitignore") or file.endswith(".continueignore"):
path = os.path.join(root, file)
with open(path, "r") as f:
gitignores[path] = f.read()
return gitignores
def should_ignore_file_factory(
ignore_files: List[str], gitignores: Dict[str, str]
) -> Callable[[str], bool]:
gitignore_paths = list(gitignores.keys())
def gitignore_patterns_for_file(filepath: str) -> List[str]:
paths = list(
filter(
lambda gitignore_path: filepath.startswith(gi_basename(gitignore_path)),
gitignore_paths,
)
)
patterns = []
for path in paths:
base = gi_basename(path)
for pattern in gitignores[path].split("\n"):
if pattern.strip() != "":
patterns.append(os.path.join(base, pattern.strip()))
return patterns
def should_ignore_file(filepath: str) -> bool:
return should_filter_path(
filepath,
ignore_files
+ DEFAULT_IGNORE_PATTERNS
+ FILE_IGNORE_PATTERNS
+ gitignore_patterns_for_file(filepath),
)
return should_ignore_file