🚧 wip, fixing types

This commit is contained in:
Nate Sesti 2023-11-19 09:38:55 -08:00
parent 4970f4299e
commit fc0152c654
4 changed files with 45 additions and 18 deletions

View file

@ -2,4 +2,11 @@ from typing import List, Tuple
def sync_results(
dir: str, branch: str
) -> Tuple[List[Tuple[str, str]], List[Tuple[str, str]]]: ...
) -> Tuple[
List[Tuple[str, str]],
List[Tuple[str, str]],
List[Tuple[str, str]],
List[Tuple[str, str]],
]:
"""Returns lists of files that need (compute, delete, add label, remove label) with each item being a (path, hash) tuple"""
...

View file

@ -2,15 +2,13 @@ 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 .chunk import Chunk
from . import chunk_document
from .fast_index import stream_files_to_update
from continuedev.continuedev import sync_results
from ....server.protocols.ide_protocol import AbstractIdeProtocolServer
from . import chunk_document
from .chunk import Chunk
from .fast_index import stream_files_to_update
from .ignore import should_ignore_file_factory
MAX_SIZE_IN_CHARS = 50_000
@ -85,9 +83,27 @@ async def stream_chunk_directory(
def local_stream_chunk_directory(
workspace_dir: str,
max_chunk_size: int,
workspace_dir: str, max_chunk_size: int, branch: str
) -> Generator[Tuple[Optional[Chunk], float], None, None]:
(compute, delete, add_label, remove_label) = sync_results(workspace_dir, branch)
for filepath, digest in compute:
# Ignore if the file is too large (cutoff is 10MB)
if os.path.getsize(filepath) > 10_000_000:
continue
try:
contents = open(filepath, "r").read()
except Exception as e:
print(e, filepath)
continue
if contents.strip() == "":
continue
for chunk in chunk_document(filepath, contents, max_chunk_size, digest):
yield (chunk, 0.0)
for (add_filepath, add_digest), (del_filepath, del_digest) in sync_results(
workspace_dir, branch
):

View file

@ -7,7 +7,12 @@ mod sync;
fn sync_results(
dir: String,
branch: String,
) -> PyResult<(Vec<(String, String)>, Vec<(String, String)>)> {
) -> PyResult<(
Vec<(String, String)>,
Vec<(String, String)>,
Vec<(String, String)>,
Vec<(String, String)>,
)> {
return sync::sync(Path::new(&dir), Some(&branch))
.map_err(|err| PyErr::new::<exceptions::PyException, _>(format!("{}", err)));
}

View file

@ -1,11 +1,10 @@
mod merkle;
use homedir::get_my_home;
use ignore;
use merkle::{build_walk, compute_tree_for_dir, diff, hash_string, ObjectHash};
use std::{
collections::HashMap,
fs::{File, OpenOptions},
io::{BufRead, BufReader, Read, Seek, SeekFrom, Write},
io::{Read, Seek, SeekFrom, Write},
path::{Path, PathBuf},
time::{SystemTime, UNIX_EPOCH},
};
@ -212,7 +211,7 @@ impl IndexCache {
// TODO: You could add_bulk, remove_bulk if this gets slow
fn read_rev_tags(&self, hash: [u8; ITEM_SIZE]) -> HashMap<String, Vec<String>> {
let mut rev_tags_path = IndexCache::rev_tags_path(hash);
let rev_tags_path = IndexCache::rev_tags_path(hash);
let mut rev_tags_file = OpenOptions::new()
.read(true)
.write(true)
@ -221,14 +220,14 @@ impl IndexCache {
.unwrap();
let mut contents = String::new();
rev_tags_file.read_to_string(&mut contents).unwrap();
let mut rev_tags: HashMap<String, Vec<String>> =
let rev_tags: HashMap<String, Vec<String>> =
serde_json::from_str(&contents).unwrap_or(HashMap::new());
return rev_tags;
}
fn write_rev_tags(&self, hash: [u8; ITEM_SIZE], rev_tags: HashMap<String, Vec<String>>) {
let mut rev_tags_path = IndexCache::rev_tags_path(hash);
let rev_tags_path = IndexCache::rev_tags_path(hash);
let mut rev_tags_file = OpenOptions::new()
.read(true)
.write(true)
@ -253,7 +252,7 @@ impl IndexCache {
let tag_str = self.tag_str();
let hash_str = hash_string(item.hash);
if !rev_tags.contains_key(hash_str.as_str()) {
rev_tags.insert(hash_str, Vec::new());
rev_tags.insert(hash_str.clone(), Vec::new());
}
rev_tags.get_mut(hash_str.as_str()).unwrap().push(tag_str);
self.write_rev_tags(item.hash, rev_tags);
@ -281,7 +280,7 @@ impl IndexCache {
let tag_str = self.tag_str();
let hash_str = hash_string(item.hash);
if rev_tags.contains_key(hash_str.as_str()) {
let mut tags = rev_tags.get_mut(hash_str.as_str()).unwrap();
let tags = rev_tags.get_mut(hash_str.as_str()).unwrap();
let index = tags.iter().position(|x| *x == tag_str).unwrap();
tags.remove(index);
if tags.len() == 0 {