Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1f87608

Browse files
committedFeb 19, 2024·
add WorkerOperations and InstanceViewOperations for name conflicts
Signed-off-by: usamoi <usamoi@outlook.com>
1 parent 788c546 commit 1f87608

File tree

3 files changed

+71
-26
lines changed

3 files changed

+71
-26
lines changed
 

‎crates/service/src/instance/mod.rs

+23-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,22 @@ use crate::prelude::*;
1010
use std::path::PathBuf;
1111
use std::sync::Arc;
1212

13+
pub trait InstanceViewOperations {
14+
fn basic<'a, F: Fn(Pointer) -> bool + Clone + 'a>(
15+
&'a self,
16+
vector: &'a DynamicVector,
17+
opts: &'a SearchOptions,
18+
filter: F,
19+
) -> Result<Box<dyn Iterator<Item = Pointer> + 'a>, BasicError>;
20+
fn vbase<'a, F: FnMut(Pointer) -> bool + Clone + 'a>(
21+
&'a self,
22+
vector: &'a DynamicVector,
23+
opts: &'a SearchOptions,
24+
filter: F,
25+
) -> Result<Box<dyn Iterator<Item = Pointer> + 'a>, VbaseError>;
26+
fn list(&self) -> Result<Box<dyn Iterator<Item = Pointer> + '_>, ListError>;
27+
}
28+
1329
#[derive(Clone)]
1430
pub enum Instance {
1531
F32Cos(Arc<Index<F32Cos>>),
@@ -149,8 +165,8 @@ pub enum InstanceView {
149165
SparseF32L2(Arc<IndexView<SparseF32L2>>),
150166
}
151167

152-
impl InstanceView {
153-
pub fn _basic<'a, F: Fn(Pointer) -> bool + Clone + 'a>(
168+
impl InstanceViewOperations for InstanceView {
169+
fn basic<'a, F: Fn(Pointer) -> bool + Clone + 'a>(
154170
&'a self,
155171
vector: &'a DynamicVector,
156172
opts: &'a SearchOptions,
@@ -187,7 +203,7 @@ impl InstanceView {
187203
_ => Err(BasicError::InvalidVector),
188204
}
189205
}
190-
pub fn _vbase<'a, F: FnMut(Pointer) -> bool + Clone + 'a>(
206+
fn vbase<'a, F: FnMut(Pointer) -> bool + Clone + 'a>(
191207
&'a self,
192208
vector: &'a DynamicVector,
193209
opts: &'a SearchOptions,
@@ -224,7 +240,7 @@ impl InstanceView {
224240
_ => Err(VbaseError::InvalidVector),
225241
}
226242
}
227-
pub fn _list(&self) -> Result<Box<dyn Iterator<Item = Pointer> + '_>, ListError> {
243+
fn list(&self) -> Result<Box<dyn Iterator<Item = Pointer> + '_>, ListError> {
228244
match self {
229245
InstanceView::F32Cos(x) => Ok(Box::new(x.list()?) as Box<dyn Iterator<Item = Pointer>>),
230246
InstanceView::F32Dot(x) => Ok(Box::new(x.list()?)),
@@ -237,6 +253,9 @@ impl InstanceView {
237253
InstanceView::SparseF32L2(x) => Ok(Box::new(x.list()?)),
238254
}
239255
}
256+
}
257+
258+
impl InstanceView {
240259
pub fn insert(
241260
&self,
242261
vector: DynamicVector,

‎crates/service/src/worker/mod.rs

+34-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
pub mod metadata;
22

33
use crate::index::{IndexOptions, IndexStat};
4-
use crate::instance::{Instance, InstanceView};
4+
use crate::instance::{Instance, InstanceView, InstanceViewOperations};
55
use crate::prelude::*;
66
use crate::utils::clean::clean;
77
use crate::utils::dir_ops::sync_dir;
@@ -13,6 +13,25 @@ use std::collections::{HashMap, HashSet};
1313
use std::path::PathBuf;
1414
use std::sync::Arc;
1515

16+
pub trait WorkerOperations {
17+
type InstanceView: InstanceViewOperations;
18+
19+
fn create(&self, handle: Handle, options: IndexOptions) -> Result<(), CreateError>;
20+
fn drop(&self, handle: Handle) -> Result<(), DropError>;
21+
fn flush(&self, handle: Handle) -> Result<(), FlushError>;
22+
fn insert(
23+
&self,
24+
handle: Handle,
25+
vector: DynamicVector,
26+
pointer: Pointer,
27+
) -> Result<(), InsertError>;
28+
fn delete(&self, handle: Handle, pointer: Pointer) -> Result<(), DeleteError>;
29+
fn basic_view(&self, handle: Handle) -> Result<Self::InstanceView, BasicError>;
30+
fn vbase_view(&self, handle: Handle) -> Result<Self::InstanceView, VbaseError>;
31+
fn list_view(&self, handle: Handle) -> Result<Self::InstanceView, ListError>;
32+
fn stat(&self, handle: Handle) -> Result<IndexStat, StatError>;
33+
}
34+
1635
pub struct Worker {
1736
path: PathBuf,
1837
protect: Mutex<WorkerProtect>,
@@ -65,7 +84,12 @@ impl Worker {
6584
pub fn view(&self) -> Arc<WorkerView> {
6685
self.view.load_full()
6786
}
68-
pub fn _create(&self, handle: Handle, options: IndexOptions) -> Result<(), CreateError> {
87+
}
88+
89+
impl WorkerOperations for Worker {
90+
type InstanceView = InstanceView;
91+
92+
fn create(&self, handle: Handle, options: IndexOptions) -> Result<(), CreateError> {
6993
use std::collections::hash_map::Entry;
7094
let mut protect = self.protect.lock();
7195
match protect.indexes.entry(handle) {
@@ -79,7 +103,7 @@ impl Worker {
79103
Entry::Occupied(_) => Err(CreateError::Exist),
80104
}
81105
}
82-
pub fn _drop(&self, handle: Handle) -> Result<(), DropError> {
106+
fn drop(&self, handle: Handle) -> Result<(), DropError> {
83107
let mut protect = self.protect.lock();
84108
if protect.indexes.remove(&handle).is_some() {
85109
protect.maintain(&self.view);
@@ -88,14 +112,14 @@ impl Worker {
88112
Err(DropError::NotExist)
89113
}
90114
}
91-
pub fn _flush(&self, handle: Handle) -> Result<(), FlushError> {
115+
fn flush(&self, handle: Handle) -> Result<(), FlushError> {
92116
let view = self.view();
93117
let instance = view.get(handle).ok_or(FlushError::NotExist)?;
94118
let view = instance.view().ok_or(FlushError::Upgrade)?;
95119
view.flush()?;
96120
Ok(())
97121
}
98-
pub fn _insert(
122+
fn insert(
99123
&self,
100124
handle: Handle,
101125
vector: DynamicVector,
@@ -112,29 +136,29 @@ impl Worker {
112136
}
113137
Ok(())
114138
}
115-
pub fn _delete(&self, handle: Handle, pointer: Pointer) -> Result<(), DeleteError> {
139+
fn delete(&self, handle: Handle, pointer: Pointer) -> Result<(), DeleteError> {
116140
let view = self.view();
117141
let instance = view.get(handle).ok_or(DeleteError::NotExist)?;
118142
let view = instance.view().ok_or(DeleteError::Upgrade)?;
119143
view.delete(pointer)?;
120144
Ok(())
121145
}
122-
pub fn _basic_view(&self, handle: Handle) -> Result<InstanceView, BasicError> {
146+
fn basic_view(&self, handle: Handle) -> Result<InstanceView, BasicError> {
123147
let view = self.view();
124148
let instance = view.get(handle).ok_or(BasicError::NotExist)?;
125149
instance.view().ok_or(BasicError::Upgrade)
126150
}
127-
pub fn _vbase_view(&self, handle: Handle) -> Result<InstanceView, VbaseError> {
151+
fn vbase_view(&self, handle: Handle) -> Result<InstanceView, VbaseError> {
128152
let view = self.view();
129153
let instance = view.get(handle).ok_or(VbaseError::NotExist)?;
130154
instance.view().ok_or(VbaseError::Upgrade)
131155
}
132-
pub fn _list_view(&self, handle: Handle) -> Result<InstanceView, ListError> {
156+
fn list_view(&self, handle: Handle) -> Result<InstanceView, ListError> {
133157
let view = self.view();
134158
let instance = view.get(handle).ok_or(ListError::NotExist)?;
135159
instance.view().ok_or(ListError::Upgrade)
136160
}
137-
pub fn _stat(&self, handle: Handle) -> Result<IndexStat, StatError> {
161+
fn stat(&self, handle: Handle) -> Result<IndexStat, StatError> {
138162
let view = self.view();
139163
let instance = view.get(handle).ok_or(StatError::NotExist)?;
140164
let stat = instance.stat().ok_or(StatError::Upgrade)?;

‎src/bgworker/normal.rs

+14-12
Original file line numberDiff line numberDiff line change
@@ -59,48 +59,50 @@ pub fn normal(worker: Arc<Worker>) {
5959

6060
fn session(worker: Arc<Worker>, handler: ServerRpcHandler) -> Result<!, ConnectionError> {
6161
use crate::ipc::ServerRpcHandle;
62+
use service::instance::InstanceViewOperations;
63+
use service::worker::WorkerOperations;
6264
let mut handler = handler;
6365
loop {
6466
match handler.handle()? {
6567
// control plane
6668
ServerRpcHandle::Create { handle, options, x } => {
67-
handler = x.leave(worker._create(handle, options))?;
69+
handler = x.leave(WorkerOperations::create(worker.as_ref(), handle, options))?;
6870
}
6971
ServerRpcHandle::Drop { handle, x } => {
70-
handler = x.leave(worker._drop(handle))?;
72+
handler = x.leave(WorkerOperations::drop(worker.as_ref(), handle))?;
7173
}
7274
// data plane
7375
ServerRpcHandle::Flush { handle, x } => {
74-
handler = x.leave(worker._flush(handle))?;
76+
handler = x.leave(worker.flush(handle))?;
7577
}
7678
ServerRpcHandle::Insert {
7779
handle,
7880
vector,
7981
pointer,
8082
x,
8183
} => {
82-
handler = x.leave(worker._insert(handle, vector, pointer))?;
84+
handler = x.leave(worker.insert(handle, vector, pointer))?;
8385
}
8486
ServerRpcHandle::Delete { handle, pointer, x } => {
85-
handler = x.leave(worker._delete(handle, pointer))?;
87+
handler = x.leave(worker.delete(handle, pointer))?;
8688
}
8789
ServerRpcHandle::Stat { handle, x } => {
88-
handler = x.leave(worker._stat(handle))?;
90+
handler = x.leave(worker.stat(handle))?;
8991
}
9092
ServerRpcHandle::Basic {
9193
handle,
9294
vector,
9395
opts,
9496
x,
9597
} => {
96-
let v = match worker._basic_view(handle) {
98+
let v = match worker.basic_view(handle) {
9799
Ok(x) => x,
98100
Err(e) => {
99101
handler = x.error_err(e)?;
100102
continue;
101103
}
102104
};
103-
match v._basic(&vector, &opts, |_| true) {
105+
match v.basic(&vector, &opts, |_| true) {
104106
Ok(mut iter) => {
105107
use crate::ipc::ServerBasicHandle;
106108
let mut x = x.error_ok()?;
@@ -125,14 +127,14 @@ fn session(worker: Arc<Worker>, handler: ServerRpcHandler) -> Result<!, Connecti
125127
opts,
126128
x,
127129
} => {
128-
let v = match worker._vbase_view(handle) {
130+
let v = match worker.vbase_view(handle) {
129131
Ok(x) => x,
130132
Err(e) => {
131133
handler = x.error_err(e)?;
132134
continue;
133135
}
134136
};
135-
match v._vbase(&vector, &opts, |_| true) {
137+
match v.vbase(&vector, &opts, |_| true) {
136138
Ok(mut iter) => {
137139
use crate::ipc::ServerVbaseHandle;
138140
let mut x = x.error_ok()?;
@@ -152,14 +154,14 @@ fn session(worker: Arc<Worker>, handler: ServerRpcHandler) -> Result<!, Connecti
152154
};
153155
}
154156
ServerRpcHandle::List { handle, x } => {
155-
let v = match worker._list_view(handle) {
157+
let v = match worker.list_view(handle) {
156158
Ok(x) => x,
157159
Err(e) => {
158160
handler = x.error_err(e)?;
159161
continue;
160162
}
161163
};
162-
match v._list() {
164+
match v.list() {
163165
Ok(mut iter) => {
164166
use crate::ipc::ServerListHandle;
165167
let mut x = x.error_ok()?;

0 commit comments

Comments
 (0)
Please sign in to comment.