WIP
This commit is contained in:
parent
dbea3cf20c
commit
4b09f77950
7 changed files with 88 additions and 44 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -7524,6 +7524,7 @@ dependencies = [
|
||||||
"client",
|
"client",
|
||||||
"collections",
|
"collections",
|
||||||
"context_menu",
|
"context_menu",
|
||||||
|
"db",
|
||||||
"drag_and_drop",
|
"drag_and_drop",
|
||||||
"fs",
|
"fs",
|
||||||
"futures 0.3.24",
|
"futures 0.3.24",
|
||||||
|
|
|
@ -135,13 +135,12 @@ impl Telemetry {
|
||||||
Some(self.state.lock().log_file.as_ref()?.path().to_path_buf())
|
Some(self.state.lock().log_file.as_ref()?.path().to_path_buf())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn start(self: &Arc<Self>, db: Arc<Mutex<Db>>) {
|
pub fn start(self: &Arc<Self>, db: Arc<Db>) {
|
||||||
let this = self.clone();
|
let this = self.clone();
|
||||||
self.executor
|
self.executor
|
||||||
.spawn(
|
.spawn(
|
||||||
async move {
|
async move {
|
||||||
let db = db.lock();
|
let device_id = if let Ok(Some(device_id)) = db.read_kvp("device_id") {
|
||||||
let device_id = if let Ok(device_id) = db.read_kvp("device_id") {
|
|
||||||
device_id
|
device_id
|
||||||
} else {
|
} else {
|
||||||
let device_id = Uuid::new_v4().to_string();
|
let device_id = Uuid::new_v4().to_string();
|
||||||
|
@ -149,7 +148,6 @@ impl Telemetry {
|
||||||
device_id
|
device_id
|
||||||
};
|
};
|
||||||
|
|
||||||
drop(db);
|
|
||||||
let device_id = Some(Arc::from(device_id));
|
let device_id = Some(Arc::from(device_id));
|
||||||
let mut state = this.state.lock();
|
let mut state = this.state.lock();
|
||||||
state.device_id = device_id.clone();
|
state.device_id = device_id.clone();
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
mod kvp;
|
mod kvp;
|
||||||
mod migrations;
|
mod migrations;
|
||||||
|
mod serialized_item;
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use migrations::MIGRATIONS;
|
use migrations::MIGRATIONS;
|
||||||
|
@ -9,9 +10,10 @@ use std::path::Path;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
pub use kvp::*;
|
pub use kvp::*;
|
||||||
|
pub use serialized_item::*;
|
||||||
|
|
||||||
pub struct Db {
|
pub struct Db {
|
||||||
connecion: Connection,
|
connection: Mutex<Connection>,
|
||||||
in_memory: bool,
|
in_memory: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,26 +24,26 @@ pub struct Db {
|
||||||
impl Db {
|
impl Db {
|
||||||
/// Open or create a database at the given file path. Falls back to in memory database if the
|
/// Open or create a database at the given file path. Falls back to in memory database if the
|
||||||
/// database at the given path is corrupted
|
/// database at the given path is corrupted
|
||||||
pub fn open(path: &Path) -> Result<Arc<Mutex<Self>>> {
|
pub fn open(path: &Path) -> Result<Arc<Self>> {
|
||||||
let conn = Connection::open(path)?;
|
let conn = Connection::open(path)?;
|
||||||
|
|
||||||
Self::initialize(conn, false).or_else(|_| Self::open_in_memory())
|
Self::initialize(conn, false).or_else(|_| Self::open_in_memory())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Open a in memory database for testing and as a fallback.
|
/// Open a in memory database for testing and as a fallback.
|
||||||
pub fn open_in_memory() -> Result<Arc<Mutex<Self>>> {
|
pub fn open_in_memory() -> Result<Arc<Self>> {
|
||||||
let conn = Connection::open_in_memory()?;
|
let conn = Connection::open_in_memory()?;
|
||||||
|
|
||||||
Self::initialize(conn, true)
|
Self::initialize(conn, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn initialize(mut conn: Connection, in_memory: bool) -> Result<Arc<Mutex<Self>>> {
|
fn initialize(mut conn: Connection, in_memory: bool) -> Result<Arc<Self>> {
|
||||||
MIGRATIONS.to_latest(&mut conn)?;
|
MIGRATIONS.to_latest(&mut conn)?;
|
||||||
|
|
||||||
Ok(Arc::new(Mutex::new(Self {
|
Ok(Arc::new(Self {
|
||||||
connecion: conn,
|
connection: Mutex::new(conn),
|
||||||
in_memory,
|
in_memory,
|
||||||
})))
|
}))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,34 +59,26 @@ mod tests {
|
||||||
let real_db = Db::open(&dir.path().join("test.db")).unwrap();
|
let real_db = Db::open(&dir.path().join("test.db")).unwrap();
|
||||||
|
|
||||||
for db in [&real_db, &fake_db] {
|
for db in [&real_db, &fake_db] {
|
||||||
assert_eq!(
|
assert_eq!(db.read_kvp("key-1").unwrap(), None);
|
||||||
db.read(["key-1", "key-2", "key-3"]).unwrap(),
|
|
||||||
&[None, None, None]
|
|
||||||
);
|
|
||||||
|
|
||||||
db.write([("key-1", "one"), ("key-3", "three")]).unwrap();
|
db.write_kvp("key-1", "one").unwrap();
|
||||||
assert_eq!(
|
assert_eq!(db.read_kvp("key-1").unwrap(), Some("one".to_string()));
|
||||||
db.read(["key-1", "key-2", "key-3"]).unwrap(),
|
|
||||||
&[
|
|
||||||
Some("one".as_bytes().to_vec()),
|
|
||||||
None,
|
|
||||||
Some("three".as_bytes().to_vec())
|
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
db.delete(["key-3", "key-4"]).unwrap();
|
db.write_kvp("key-2", "two").unwrap();
|
||||||
assert_eq!(
|
assert_eq!(db.read_kvp("key-2").unwrap(), Some("two".to_string()));
|
||||||
db.read(["key-1", "key-2", "key-3"]).unwrap(),
|
|
||||||
&[Some("one".as_bytes().to_vec()), None, None,]
|
db.delete_kvp("key-1").unwrap();
|
||||||
);
|
assert_eq!(db.read_kvp("key-1").unwrap(), None);
|
||||||
}
|
}
|
||||||
|
|
||||||
drop(real_db);
|
drop(real_db);
|
||||||
|
|
||||||
let real_db = Db::open(&dir.path().join("test.db")).unwrap();
|
let real_db = Db::open(&dir.path().join("test.db")).unwrap();
|
||||||
assert_eq!(
|
|
||||||
real_db.read(["key-1", "key-2", "key-3"]).unwrap(),
|
real_db.write_kvp("key-1", "one").unwrap();
|
||||||
&[Some("one".as_bytes().to_vec()), None, None,]
|
assert_eq!(real_db.read_kvp("key-1").unwrap(), None);
|
||||||
);
|
|
||||||
|
real_db.write_kvp("key-2", "two").unwrap();
|
||||||
|
assert_eq!(real_db.read_kvp("key-2").unwrap(), Some("two".to_string()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,20 +1,20 @@
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
use rusqlite::OptionalExtension;
|
||||||
|
|
||||||
use super::Db;
|
use super::Db;
|
||||||
|
|
||||||
impl Db {
|
impl Db {
|
||||||
pub fn read_kvp(&self, key: &str) -> Result<String> {
|
pub fn read_kvp(&self, key: &str) -> Result<Option<String>> {
|
||||||
let mut stmt = self
|
let lock = self.connection.lock();
|
||||||
.connecion
|
let mut stmt = lock.prepare_cached("SELECT value FROM kv_store WHERE key = (?)")?;
|
||||||
.prepare_cached("SELECT value FROM kv_store WHERE key = (?)")?;
|
|
||||||
|
|
||||||
Ok(stmt.query_row([key], |row| row.get(0))?)
|
Ok(stmt.query_row([key], |row| row.get(0)).optional()?)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn delete_kvp(&self, key: &str) -> Result<()> {
|
pub fn delete_kvp(&self, key: &str) -> Result<()> {
|
||||||
let mut stmt = self
|
let lock = self.connection.lock();
|
||||||
.connecion
|
|
||||||
.prepare_cached("SELECT value FROM kv_store WHERE key = (?)")?;
|
let mut stmt = lock.prepare_cached("SELECT value FROM kv_store WHERE key = (?)")?;
|
||||||
|
|
||||||
stmt.execute([key])?;
|
stmt.execute([key])?;
|
||||||
|
|
||||||
|
@ -22,9 +22,10 @@ impl Db {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write_kvp(&self, key: &str, value: &str) -> Result<()> {
|
pub fn write_kvp(&self, key: &str, value: &str) -> Result<()> {
|
||||||
let mut stmt = self
|
let lock = self.connection.lock();
|
||||||
.connecion
|
|
||||||
.prepare_cached("INSERT OR REPLACE INTO kv_store(key, value) VALUES ((?), (?))")?;
|
let mut stmt =
|
||||||
|
lock.prepare_cached("INSERT OR REPLACE INTO kv_store(key, value) VALUES ((?), (?))")?;
|
||||||
|
|
||||||
stmt.execute([key, value])?;
|
stmt.execute([key, value])?;
|
||||||
|
|
||||||
|
|
22
crates/db/src/serialized_item.rs
Normal file
22
crates/db/src/serialized_item.rs
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
use anyhow::Result;
|
||||||
|
|
||||||
|
use super::Db;
|
||||||
|
|
||||||
|
impl Db {}
|
||||||
|
|
||||||
|
#[derive(PartialEq, Eq, Hash)]
|
||||||
|
pub enum SerializedItemKind {
|
||||||
|
Editor,
|
||||||
|
Terminal,
|
||||||
|
ProjectSearch,
|
||||||
|
Diagnostics,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum SerializedItem {
|
||||||
|
Editor(PathBuf, String),
|
||||||
|
Terminal,
|
||||||
|
ProjectSearch(String),
|
||||||
|
Diagnostics,
|
||||||
|
}
|
|
@ -23,6 +23,7 @@ client = { path = "../client" }
|
||||||
collections = { path = "../collections" }
|
collections = { path = "../collections" }
|
||||||
context_menu = { path = "../context_menu" }
|
context_menu = { path = "../context_menu" }
|
||||||
drag_and_drop = { path = "../drag_and_drop" }
|
drag_and_drop = { path = "../drag_and_drop" }
|
||||||
|
db = { path = "../db" }
|
||||||
fs = { path = "../fs" }
|
fs = { path = "../fs" }
|
||||||
gpui = { path = "../gpui" }
|
gpui = { path = "../gpui" }
|
||||||
language = { path = "../language" }
|
language = { path = "../language" }
|
||||||
|
|
|
@ -14,6 +14,7 @@ use anyhow::{anyhow, Context, Result};
|
||||||
use call::ActiveCall;
|
use call::ActiveCall;
|
||||||
use client::{proto, Client, PeerId, TypedEnvelope, UserStore};
|
use client::{proto, Client, PeerId, TypedEnvelope, UserStore};
|
||||||
use collections::{hash_map, HashMap, HashSet};
|
use collections::{hash_map, HashMap, HashSet};
|
||||||
|
use db::{SerializedItem, SerializedItemKind};
|
||||||
use dock::{DefaultItemFactory, Dock, ToggleDockButton};
|
use dock::{DefaultItemFactory, Dock, ToggleDockButton};
|
||||||
use drag_and_drop::DragAndDrop;
|
use drag_and_drop::DragAndDrop;
|
||||||
use fs::{self, Fs};
|
use fs::{self, Fs};
|
||||||
|
@ -77,6 +78,9 @@ type FollowableItemBuilders = HashMap<
|
||||||
),
|
),
|
||||||
>;
|
>;
|
||||||
|
|
||||||
|
type ItemDeserializers =
|
||||||
|
HashMap<SerializedItemKind, fn(SerializedItem, &mut ViewContext<Pane>) -> Box<dyn ItemHandle>>;
|
||||||
|
|
||||||
#[derive(Clone, PartialEq)]
|
#[derive(Clone, PartialEq)]
|
||||||
pub struct RemoveWorktreeFromProject(pub WorktreeId);
|
pub struct RemoveWorktreeFromProject(pub WorktreeId);
|
||||||
|
|
||||||
|
@ -230,6 +234,14 @@ pub fn register_followable_item<I: FollowableItem>(cx: &mut MutableAppContext) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn register_deserializable_item<I: Item>(cx: &mut MutableAppContext) {
|
||||||
|
cx.update_default_global(|deserializers: &mut ItemDeserializers, _| {
|
||||||
|
deserializers.insert(I::serialized_item_kind(), |serialized_item, cx| {
|
||||||
|
Box::new(cx.add_view(|cx| I::deserialize(serialized_item, cx)))
|
||||||
|
})
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
pub struct AppState {
|
pub struct AppState {
|
||||||
pub languages: Arc<LanguageRegistry>,
|
pub languages: Arc<LanguageRegistry>,
|
||||||
pub themes: Arc<ThemeRegistry>,
|
pub themes: Arc<ThemeRegistry>,
|
||||||
|
@ -333,6 +345,9 @@ pub trait Item: View {
|
||||||
fn breadcrumbs(&self, _theme: &Theme, _cx: &AppContext) -> Option<Vec<ElementBox>> {
|
fn breadcrumbs(&self, _theme: &Theme, _cx: &AppContext) -> Option<Vec<ElementBox>> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
fn serialized_item_kind() -> SerializedItemKind;
|
||||||
|
fn deserialize(serialized_item: SerializedItem, cx: &mut ViewContext<Self>) -> Self;
|
||||||
|
fn serialize(&self) -> SerializedItem;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait ProjectItem: Item {
|
pub trait ProjectItem: Item {
|
||||||
|
@ -3611,6 +3626,18 @@ mod tests {
|
||||||
fn to_item_events(_: &Self::Event) -> Vec<ItemEvent> {
|
fn to_item_events(_: &Self::Event) -> Vec<ItemEvent> {
|
||||||
vec![ItemEvent::UpdateTab, ItemEvent::Edit]
|
vec![ItemEvent::UpdateTab, ItemEvent::Edit]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn serialized_item_kind() -> SerializedItemKind {
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deserialize(_serialized_item: SerializedItem, _cx: &mut ViewContext<Self>) -> Self {
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn serialize(&self) -> SerializedItem {
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SidebarItem for TestItem {}
|
impl SidebarItem for TestItem {}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue