1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
use std::{
sync::{
mpsc::{channel, SendError, Sender},
Arc, RwLock,
},
thread::spawn,
};
use futures::{executor::block_on, future, Future};
use crate::{Activity, Condition, Data, Order, RowSet, Search};
use super::{Field, Number, Term};
impl<'a> Search<'a> {
pub fn result(mut self) -> Result<RowSet, SendError<RowSet>> {
self.search_exec()
}
pub fn result_with_sort(&mut self, orders: Vec<Order>) -> Result<Vec<u32>, SendError<RowSet>> {
let rows = self.search_exec()?;
Ok(self.data.sort(&rows, &orders))
}
pub fn search_exec_cond(
data: &Data,
condition: &Condition,
tx: Sender<RowSet>,
) -> Result<(), SendError<RowSet>> {
match condition {
Condition::Activity(condition) => Self::search_exec_activity(data, condition, tx),
Condition::Term(condition) => Self::search_exec_term(data, condition, tx),
Condition::Field(field_name, condition) => {
Self::search_exec_field(data, field_name, condition, tx)
}
Condition::Row(condition) => Self::search_exec_row(data, condition, tx),
Condition::LastUpdated(condition) => {
Self::search_exec_last_updated(data, condition, tx)
}
Condition::Uuid(uuid) => Self::search_exec_uuid(data, uuid, tx),
Condition::Narrow(conditions) => {
let mut new_search = Search::new(data);
for c in conditions {
new_search = new_search.search(c.clone());
}
tx.send(new_search.result()?)?;
}
Condition::Wide(conditions) => {
let (tx_inner, rx) = channel();
for c in conditions {
let tx_inner = tx_inner.clone();
Self::search_exec_cond(data, c, tx_inner)?;
}
drop(tx_inner);
spawn(move || {
let mut tmp = RowSet::default();
for ref mut rs in rx {
tmp.append(rs);
}
tx.send(tmp).unwrap();
});
}
};
Ok(())
}
fn search_exec(&mut self) -> Result<RowSet, SendError<RowSet>> {
let conditions_count = self.conditions.len();
Ok(if conditions_count == 0 {
let mut rows = RowSet::default();
for row in self.data.serial.read().unwrap().iter() {
rows.insert(row.row());
}
rows
} else {
let (tx, rx) = channel();
for c in self.conditions.iter() {
let tx = tx.clone();
Self::search_exec_cond(self.data, c, tx)?;
}
drop(tx);
let mut iter = rx.iter();
let mut rows = iter.next().unwrap();
for rs in iter {
rows = rows.intersection(&rs).map(|&x| x).collect();
}
rows
})
}
fn search_exec_activity(data: &Data, condition: &Activity, tx: Sender<RowSet>) {
if let Some(ref activity) = data.activity {
let index = Arc::clone(activity);
let activity = *condition as u8;
spawn(move || {
tx.send(
index
.read()
.unwrap()
.iter_by(|v| v.cmp(&activity))
.map(|v| v.row())
.collect(),
)
.unwrap();
});
} else {
unreachable!();
}
}
fn search_exec_term_in(data: &Data, base: u64, tx: Sender<RowSet>) {
if let Some(ref term_begin) = data.term_begin {
let term_begin = Arc::clone(term_begin);
if let Some(ref term_end) = data.term_end {
let term_end = Arc::clone(term_end);
spawn(move || {
let mut result = RowSet::default();
for node in term_begin.read().unwrap().iter_to(|v| v.cmp(&base)) {
let row = node.row();
let end = *term_end.read().unwrap().value(row).unwrap_or(&0);
if end == 0 || end > base {
result.replace(row);
}
}
tx.send(result).unwrap();
});
} else {
unreachable!();
}
} else {
unreachable!();
}
}
fn search_exec_term(data: &Data, condition: &Term, tx: Sender<RowSet>) {
match condition {
Term::In(base) => {
Self::search_exec_term_in(data, *base, tx);
}
Term::Future(base) => {
if let Some(ref f) = data.term_begin {
let index = Arc::clone(f);
let base = base.clone();
spawn(move || {
tx.send(
index
.read()
.unwrap()
.iter_from(|v| v.cmp(&base))
.map(|v| v.row())
.collect(),
)
.unwrap();
});
} else {
unreachable!();
}
}
Term::Past(base) => {
if let Some(ref f) = data.term_end {
let index = Arc::clone(f);
let base = base.clone();
spawn(move || {
tx.send(
index
.read()
.unwrap()
.iter_range(|v| v.cmp(&1), |v| v.cmp(&base))
.map(|v| v.row())
.collect(),
)
.unwrap();
});
} else {
unreachable!();
}
}
}
}
fn search_exec_row(data: &Data, condition: &Number, tx: Sender<RowSet>) {
let serial = Arc::clone(&data.serial);
let mut r = RowSet::default();
match condition {
Number::Min(row) => {
let row = *row;
spawn(move || {
for i in serial.read().unwrap().iter() {
let i = i.row();
if i as isize >= row {
r.insert(i);
}
}
tx.send(r).unwrap();
});
}
Number::Max(row) => {
let row = *row;
spawn(move || {
for i in serial.read().unwrap().iter() {
let i = i.row();
if i as isize <= row {
r.insert(i);
}
}
tx.send(r).unwrap();
});
}
Number::Range(range) => {
let range = range.clone();
spawn(move || {
for i in range {
if i > 0 {
if serial.read().unwrap().exists(i as u32) {
r.insert(i as u32);
}
}
}
tx.send(r).unwrap();
});
}
Number::In(rows) => {
let rows = rows.clone();
spawn(move || {
for i in rows {
if i > 0 {
if serial.read().unwrap().exists(i as u32) {
r.insert(i as u32);
}
}
}
tx.send(r).unwrap();
});
}
}
}
fn search_exec_field(data: &Data, field_name: &str, condition: &Field, tx: Sender<RowSet>) {
if let Some(field) = data.field(field_name) {
let field = Arc::clone(&field);
match condition {
Field::Match(v) => {
let v = Arc::clone(&v);
spawn(move || {
let field = field.read().unwrap();
tx.send(
field
.iter_by(|data| field.cmp(data, &v))
.map(|x| x.row())
.collect(),
)
.unwrap();
});
}
Field::Min(min) => {
let min = Arc::clone(&min);
spawn(move || {
let field = field.read().unwrap();
tx.send(
field
.iter_from(|data| field.cmp(data, &min))
.map(|x| x.row())
.collect(),
)
.unwrap();
});
}
Field::Max(max) => {
let max = Arc::clone(&max);
spawn(move || {
let field = field.read().unwrap();
tx.send(
field
.iter_to(|data| field.cmp(data, &max))
.map(|x| x.row())
.collect(),
)
.unwrap();
});
}
Field::Range(min, max) => {
let min = Arc::clone(&min);
let max = Arc::clone(&max);
spawn(move || {
let field = field.read().unwrap();
tx.send(
field
.iter_range(
|data| field.cmp(data, &min),
|data| field.cmp(data, &max),
)
.map(|x| x.row())
.collect(),
)
.unwrap();
});
}
Field::Forward(cont) => {
let cont = Arc::clone(&cont);
spawn(move || {
Self::field_result(field, cont, Self::forward, tx);
});
}
Field::Partial(cont) => {
let cont = Arc::clone(&cont);
spawn(move || {
Self::field_result(field, cont, Self::partial, tx);
});
}
Field::Backward(cont) => {
let cont = Arc::clone(&cont);
spawn(move || {
Self::field_result(field, cont, Self::backward, tx);
});
}
Field::ValueForward(cont) => {
let cont = Arc::clone(&cont);
spawn(move || {
Self::field_result(field, cont, Self::value_forward, tx);
});
}
Field::ValuePartial(cont) => {
let cont = Arc::clone(&cont);
spawn(move || {
Self::field_result(field, cont, Self::value_partial, tx);
});
}
Field::ValueBackward(cont) => {
let cont = Arc::clone(&cont);
spawn(move || {
Self::field_result(field, cont, Self::value_backward, tx);
});
}
}
}
}
fn field_result<Fut>(
field: Arc<RwLock<crate::Field>>,
cont: Arc<String>,
func: fn(row: u32, field: Arc<RwLock<crate::Field>>, cont: Arc<String>) -> Fut,
tx: Sender<RowSet>,
) where
Fut: Future<Output = (u32, bool)>,
{
let mut rows: RowSet = RowSet::default();
let mut fs = vec![];
for row in field.read().unwrap().iter() {
fs.push(func(row.row(), Arc::clone(&field), Arc::clone(&cont)));
}
let mut fs: Vec<_> = fs.into_iter().map(Box::pin).collect();
block_on(async {
while !fs.is_empty() {
let (val, _index, remaining) = future::select_all(fs).await;
if val.1 {
rows.insert(val.0);
}
fs = remaining;
}
});
tx.send(rows).unwrap();
}
async fn forward(row: u32, field: Arc<RwLock<crate::Field>>, cont: Arc<String>) -> (u32, bool) {
let mut ret = false;
if let Some(bytes) = field.read().unwrap().bytes(row) {
if bytes.starts_with(cont.as_bytes()) {
ret = true;
}
}
(row, ret)
}
async fn partial(row: u32, field: Arc<RwLock<crate::Field>>, cont: Arc<String>) -> (u32, bool) {
let mut ret = false;
if let Some(bytes) = field.read().unwrap().bytes(row) {
let len = cont.len();
if len <= bytes.len() {
let cont_bytes = cont.as_bytes();
if let Some(_) = bytes.windows(len).position(|window| window == cont_bytes) {
ret = true;
}
}
}
(row, ret)
}
async fn backward(
row: u32,
field: Arc<RwLock<crate::Field>>,
cont: Arc<String>,
) -> (u32, bool) {
let mut ret = false;
if let Some(bytes) = field.read().unwrap().bytes(row) {
if bytes.ends_with(cont.as_bytes()) {
ret = true;
}
}
(row, ret)
}
async fn value_forward(
row: u32,
field: Arc<RwLock<crate::Field>>,
cont: Arc<String>,
) -> (u32, bool) {
let mut ret = false;
if let Some(bytes) = field.read().unwrap().bytes(row) {
if cont.as_bytes().starts_with(bytes) {
ret = true;
}
}
(row, ret)
}
async fn value_partial(
row: u32,
field: Arc<RwLock<crate::Field>>,
cont: Arc<String>,
) -> (u32, bool) {
let mut ret = false;
if let Some(bytes) = field.read().unwrap().bytes(row) {
let len = bytes.len();
if let Some(_) = cont
.as_bytes()
.windows(len)
.position(|window| window == bytes)
{
ret = true;
}
}
(row, ret)
}
async fn value_backward(
row: u32,
field: Arc<RwLock<crate::Field>>,
cont: Arc<String>,
) -> (u32, bool) {
let mut ret = false;
if let Some(bytes) = field.read().unwrap().bytes(row) {
if cont.as_bytes().ends_with(bytes) {
ret = true;
}
}
(row, ret)
}
fn search_exec_last_updated(data: &Data, condition: &Number, tx: Sender<RowSet>) {
if let Some(ref f) = data.last_updated {
let index = Arc::clone(f);
match condition {
Number::Min(min) => {
let min = min.clone() as u64;
spawn(move || {
tx.send(
index
.read()
.unwrap()
.iter_from(|v| v.cmp(&min))
.map(|v| v.row())
.collect(),
)
.unwrap();
});
}
Number::Max(max) => {
let max = max.clone() as u64;
spawn(move || {
tx.send(
index
.read()
.unwrap()
.iter_to(|v| v.cmp(&max))
.map(|v| v.row())
.collect(),
)
.unwrap();
});
}
Number::Range(range) => {
let range = range.clone();
spawn(move || {
tx.send(
index
.read()
.unwrap()
.iter_range(
|v| v.cmp(&(*range.start() as u64)),
|v| v.cmp(&(*range.end() as u64)),
)
.map(|v| v.row())
.collect(),
)
.unwrap();
});
}
Number::In(rows) => {
let rows = rows.clone();
spawn(move || {
let mut r = RowSet::default();
for i in rows {
r.append(
&mut index
.read()
.unwrap()
.iter_by(|v| v.cmp(&(i as u64)))
.map(|x| x.row())
.collect(),
);
}
tx.send(r).unwrap();
});
}
}
} else {
unreachable!();
}
}
pub fn search_exec_uuid(data: &Data, uuids: &Vec<u128>, tx: Sender<RowSet>) {
if let Some(ref uuid) = data.uuid {
let index = Arc::clone(uuid);
let uuids = uuids.clone();
spawn(move || {
let mut r = RowSet::default();
for uuid in uuids {
r.append(
&mut index
.read()
.unwrap()
.iter_by(|v| v.cmp(&uuid))
.map(|x| x.row())
.collect(),
);
}
tx.send(r).unwrap();
});
} else {
unreachable!();
}
}
}