tantivy-py: Upgrade to PyO3 0.9.

master
Damir Jelić 2020-04-19 13:35:14 +02:00
parent 094f8974ea
commit b75f9d75b6
6 changed files with 17 additions and 19 deletions

View File

@ -10,13 +10,13 @@ name = "tantivy"
crate-type = ["cdylib"] crate-type = ["cdylib"]
[dependencies] [dependencies]
chrono = "0.4.10" chrono = "0.4.11"
tantivy = "0.12.0" tantivy = "0.12.0"
itertools = "0.8.2" itertools = "0.9.0"
futures = "0.3.4" futures = "0.3.4"
[dependencies.pyo3] [dependencies.pyo3]
version = "0.8.5" version = "0.9.2"
features = ["extension-module"] features = ["extension-module"]
[package.metadata.maturin] [package.metadata.maturin]

View File

@ -1 +1 @@
nightly-2020-01-03 nightly-2020-04-18

View File

@ -127,7 +127,7 @@ pub(crate) fn extract_value(any: &PyAny) -> PyResult<Value> {
if let Ok(num) = any.extract::<f64>() { if let Ok(num) = any.extract::<f64>() {
return Ok(Value::F64(num)); return Ok(Value::F64(num));
} }
if let Ok(py_datetime) = any.downcast_ref::<PyDateTime>() { if let Ok(py_datetime) = any.downcast::<PyDateTime>() {
let datetime = Utc let datetime = Utc
.ymd( .ymd(
py_datetime.get_year(), py_datetime.get_year(),
@ -142,14 +142,14 @@ pub(crate) fn extract_value(any: &PyAny) -> PyResult<Value> {
); );
return Ok(Value::Date(datetime)); return Ok(Value::Date(datetime));
} }
if let Ok(facet) = any.downcast_ref::<Facet>() { if let Ok(facet) = any.extract::<Facet>() {
return Ok(Value::Facet(facet.inner.clone())); return Ok(Value::Facet(facet.inner.clone()));
} }
Err(to_pyerr(format!("Value unsupported {:?}", any))) Err(to_pyerr(format!("Value unsupported {:?}", any)))
} }
fn extract_value_single_or_list(any: &PyAny) -> PyResult<Vec<Value>> { fn extract_value_single_or_list(any: &PyAny) -> PyResult<Vec<Value>> {
if let Ok(values) = any.downcast_ref::<PyList>() { if let Ok(values) = any.downcast::<PyList>() {
values.iter().map(extract_value).collect() values.iter().map(extract_value).collect()
} else { } else {
Ok(vec![extract_value(any)?]) Ok(vec![extract_value(any)?])
@ -160,20 +160,19 @@ fn extract_value_single_or_list(any: &PyAny) -> PyResult<Vec<Value>> {
impl Document { impl Document {
#[new] #[new]
#[args(kwargs = "**")] #[args(kwargs = "**")]
fn new(obj: &PyRawObject, kwargs: Option<&PyDict>) -> PyResult<()> { fn new(kwargs: Option<&PyDict>) -> PyResult<Self> {
let mut document = Document::default(); let mut document = Document::default();
if let Some(field_dict) = kwargs { if let Some(field_dict) = kwargs {
document.extend(field_dict)?; document.extend(field_dict)?;
} }
obj.init(document); Ok(document)
Ok(())
} }
fn extend(&mut self, py_dict: &PyDict) -> PyResult<()> { fn extend(&mut self, py_dict: &PyDict) -> PyResult<()> {
let mut field_values: BTreeMap<String, Vec<tv::schema::Value>> = let mut field_values: BTreeMap<String, Vec<tv::schema::Value>> =
BTreeMap::new(); BTreeMap::new();
for key_value_any in py_dict.items() { for key_value_any in py_dict.items() {
if let Ok(key_value) = key_value_any.downcast_ref::<PyTuple>() { if let Ok(key_value) = key_value_any.downcast::<PyTuple>() {
if key_value.len() != 2 { if key_value.len() != 2 {
continue; continue;
} }
@ -192,7 +191,7 @@ impl Document {
let mut field_values: BTreeMap<String, Vec<tv::schema::Value>> = let mut field_values: BTreeMap<String, Vec<tv::schema::Value>> =
BTreeMap::new(); BTreeMap::new();
for key_value_any in py_dict.items() { for key_value_any in py_dict.items() {
if let Ok(key_value) = key_value_any.downcast_ref::<PyTuple>() { if let Ok(key_value) = key_value_any.downcast::<PyTuple>() {
if key_value.len() != 2 { if key_value.len() != 2 {
continue; continue;
} }

View File

@ -13,6 +13,7 @@ use tantivy::schema;
/// ancestor of its facet. In the example above, /electronics/tv_and_video/ /// ancestor of its facet. In the example above, /electronics/tv_and_video/
/// and /electronics. /// and /electronics.
#[pyclass] #[pyclass]
#[derive(Clone)]
pub(crate) struct Facet { pub(crate) struct Facet {
pub(crate) inner: schema::Facet, pub(crate) inner: schema::Facet,
} }

View File

@ -165,11 +165,10 @@ impl Index {
#[new] #[new]
#[args(reuse = true)] #[args(reuse = true)]
fn new( fn new(
obj: &PyRawObject,
schema: &Schema, schema: &Schema,
path: Option<&str>, path: Option<&str>,
reuse: bool, reuse: bool,
) -> PyResult<()> { ) -> PyResult<Self> {
let index = match path { let index = match path {
Some(p) => { Some(p) => {
let directory = MmapDirectory::open(p).map_err(to_pyerr)?; let directory = MmapDirectory::open(p).map_err(to_pyerr)?;
@ -184,8 +183,7 @@ impl Index {
}; };
let reader = index.reader().map_err(to_pyerr)?; let reader = index.reader().map_err(to_pyerr)?;
obj.init(Index { index, reader }); Ok(Index { index, reader })
Ok(())
} }
/// Create a `IndexWriter` for the index. /// Create a `IndexWriter` for the index.

View File

@ -35,10 +35,10 @@ const RECORD: &str = "position";
#[pymethods] #[pymethods]
impl SchemaBuilder { impl SchemaBuilder {
#[new] #[new]
fn new(obj: &PyRawObject) { fn new() -> Self {
obj.init(SchemaBuilder { SchemaBuilder {
builder: Arc::new(From::from(Some(schema::Schema::builder()))), builder: Arc::new(From::from(Some(schema::Schema::builder()))),
}); }
} }
/// Add a new text field to the schema. /// Add a new text field to the schema.