Skip to content

Commit 766d4c4

Browse files
committed
move code from BytesEncode to ToBytes
1 parent e579b89 commit 766d4c4

File tree

5 files changed

+222
-188
lines changed

5 files changed

+222
-188
lines changed

heed/src/database.rs

Lines changed: 87 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::borrow::Cow;
21
use std::ops::{Bound, RangeBounds};
32
use std::{any, fmt, marker, mem, ptr};
43

@@ -425,16 +424,16 @@ impl<KC, DC, C> Database<KC, DC, C> {
425424
pub fn get_duplicates<'a, 'txn>(
426425
&self,
427426
txn: &'txn RoTxn,
428-
key: &'a KC::EItem,
427+
key: &'a KC::SelfType,
429428
) -> Result<Option<RoIter<'txn, KC, DC, MoveOnCurrentKeyDuplicates>>>
430429
where
431-
KC: BytesEncode<'a>,
430+
KC: ToBytes<'a>,
432431
{
433432
assert_eq_env_db_txn!(self, txn);
434433

435434
let mut cursor = RoCursor::new(txn, self.dbi)?;
436-
let key_bytes: Cow<[u8]> = KC::bytes_encode(key).map_err(Error::Encoding)?;
437-
if cursor.move_on_key(&key_bytes)? {
435+
let key_bytes = KC::to_bytes(key).map_err(|err| Error::Encoding(Box::new(err)))?;
436+
if cursor.move_on_key(key_bytes.as_ref())? {
438437
Ok(Some(RoIter::new(cursor)))
439438
} else {
440439
Ok(None)
@@ -488,17 +487,17 @@ impl<KC, DC, C> Database<KC, DC, C> {
488487
pub fn get_lower_than<'a, 'txn>(
489488
&self,
490489
txn: &'txn RoTxn,
491-
key: &'a KC::EItem,
490+
key: &'a KC::SelfType,
492491
) -> Result<Option<(KC::DItem, DC::DItem)>>
493492
where
494-
KC: BytesEncode<'a> + BytesDecode<'txn>,
493+
KC: ToBytes<'a> + BytesDecode<'txn>,
495494
DC: BytesDecode<'txn>,
496495
{
497496
assert_eq_env_db_txn!(self, txn);
498497

499498
let mut cursor = RoCursor::new(txn, self.dbi)?;
500-
let key_bytes: Cow<[u8]> = KC::bytes_encode(key).map_err(Error::Encoding)?;
501-
cursor.move_on_key_greater_than_or_equal_to(&key_bytes)?;
499+
let key_bytes = KC::to_bytes(key).map_err(|err| Error::Encoding(Box::new(err)))?;
500+
cursor.move_on_key_greater_than_or_equal_to(key_bytes.as_ref())?;
502501

503502
match cursor.move_on_prev(MoveOperation::NoDup) {
504503
Ok(Some((key, data))) => match (KC::bytes_decode(key), DC::bytes_decode(data)) {
@@ -557,18 +556,19 @@ impl<KC, DC, C> Database<KC, DC, C> {
557556
pub fn get_lower_than_or_equal_to<'a, 'txn>(
558557
&self,
559558
txn: &'txn RoTxn,
560-
key: &'a KC::EItem,
559+
key: &'a KC::SelfType,
561560
) -> Result<Option<(KC::DItem, DC::DItem)>>
562561
where
563-
KC: BytesEncode<'a> + BytesDecode<'txn>,
562+
KC: ToBytes<'a> + BytesDecode<'txn>,
564563
DC: BytesDecode<'txn>,
565564
{
566565
assert_eq_env_db_txn!(self, txn);
567566

568567
let mut cursor = RoCursor::new(txn, self.dbi)?;
569-
let key_bytes: Cow<[u8]> = KC::bytes_encode(key).map_err(Error::Encoding)?;
570-
let result = match cursor.move_on_key_greater_than_or_equal_to(&key_bytes) {
571-
Ok(Some((key, data))) if key == &key_bytes[..] => Ok(Some((key, data))),
568+
let key_bytes = KC::to_bytes(key).map_err(|err| Error::Encoding(Box::new(err)))?;
569+
let key_bytes = key_bytes.as_ref();
570+
let result = match cursor.move_on_key_greater_than_or_equal_to(key_bytes) {
571+
Ok(Some((key, data))) if key == key_bytes => Ok(Some((key, data))),
572572
Ok(_) => cursor.move_on_prev(MoveOperation::NoDup),
573573
Err(e) => Err(e),
574574
};
@@ -630,18 +630,19 @@ impl<KC, DC, C> Database<KC, DC, C> {
630630
pub fn get_greater_than<'a, 'txn>(
631631
&self,
632632
txn: &'txn RoTxn,
633-
key: &'a KC::EItem,
633+
key: &'a KC::SelfType,
634634
) -> Result<Option<(KC::DItem, DC::DItem)>>
635635
where
636-
KC: BytesEncode<'a> + BytesDecode<'txn>,
636+
KC: ToBytes<'a> + BytesDecode<'txn>,
637637
DC: BytesDecode<'txn>,
638638
{
639639
assert_eq_env_db_txn!(self, txn);
640640

641641
let mut cursor = RoCursor::new(txn, self.dbi)?;
642-
let key_bytes: Cow<[u8]> = KC::bytes_encode(key).map_err(Error::Encoding)?;
643-
let entry = match cursor.move_on_key_greater_than_or_equal_to(&key_bytes)? {
644-
Some((key, data)) if key > &key_bytes[..] => Some((key, data)),
642+
let key_bytes = KC::to_bytes(key).map_err(|err| Error::Encoding(Box::new(err)))?;
643+
let key_bytes = key_bytes.as_ref();
644+
let entry = match cursor.move_on_key_greater_than_or_equal_to(key_bytes)? {
645+
Some((key, data)) if key > key_bytes => Some((key, data)),
645646
Some((_key, _data)) => cursor.move_on_next(MoveOperation::NoDup)?,
646647
None => None,
647648
};
@@ -702,17 +703,17 @@ impl<KC, DC, C> Database<KC, DC, C> {
702703
pub fn get_greater_than_or_equal_to<'a, 'txn>(
703704
&self,
704705
txn: &'txn RoTxn,
705-
key: &'a KC::EItem,
706+
key: &'a KC::SelfType,
706707
) -> Result<Option<(KC::DItem, DC::DItem)>>
707708
where
708-
KC: BytesEncode<'a> + BytesDecode<'txn>,
709+
KC: ToBytes<'a> + BytesDecode<'txn>,
709710
DC: BytesDecode<'txn>,
710711
{
711712
assert_eq_env_db_txn!(self, txn);
712713

713714
let mut cursor = RoCursor::new(txn, self.dbi)?;
714-
let key_bytes: Cow<[u8]> = KC::bytes_encode(key).map_err(Error::Encoding)?;
715-
match cursor.move_on_key_greater_than_or_equal_to(&key_bytes) {
715+
let key_bytes = KC::to_bytes(key).map_err(|err| Error::Encoding(Box::new(err)))?;
716+
match cursor.move_on_key_greater_than_or_equal_to(key_bytes.as_ref()) {
716717
Ok(Some((key, data))) => match (KC::bytes_decode(key), DC::bytes_decode(data)) {
717718
(Ok(key), Ok(data)) => Ok(Some((key, data))),
718719
(Err(e), _) | (_, Err(e)) => Err(Error::Decoding(e)),
@@ -1498,31 +1499,32 @@ impl<KC, DC, C> Database<KC, DC, C> {
14981499
range: &'a R,
14991500
) -> Result<RwRevRange<'txn, KC, DC>>
15001501
where
1501-
KC: BytesEncode<'a>,
1502-
R: RangeBounds<KC::EItem>,
1502+
KC: ToBytes<'a>,
1503+
R: RangeBounds<KC::SelfType>,
15031504
{
15041505
assert_eq_env_db_txn!(self, txn);
15051506

1507+
// TODO optimize, this might do unnecessary allocations on types that are already 'static
15061508
let start_bound = match range.start_bound() {
15071509
Bound::Included(bound) => {
1508-
let bytes = KC::bytes_encode(bound).map_err(Error::Encoding)?;
1509-
Bound::Included(bytes.into_owned())
1510+
let bytes = KC::to_bytes(bound).map_err(|err| Error::Encoding(Box::new(err)))?;
1511+
Bound::Included(bytes.into())
15101512
}
15111513
Bound::Excluded(bound) => {
1512-
let bytes = KC::bytes_encode(bound).map_err(Error::Encoding)?;
1513-
Bound::Excluded(bytes.into_owned())
1514+
let bytes = KC::to_bytes(bound).map_err(|err| Error::Encoding(Box::new(err)))?;
1515+
Bound::Excluded(bytes.into())
15141516
}
15151517
Bound::Unbounded => Bound::Unbounded,
15161518
};
15171519

15181520
let end_bound = match range.end_bound() {
15191521
Bound::Included(bound) => {
1520-
let bytes = KC::bytes_encode(bound).map_err(Error::Encoding)?;
1521-
Bound::Included(bytes.into_owned())
1522+
let bytes = KC::to_bytes(bound).map_err(|err| Error::Encoding(Box::new(err)))?;
1523+
Bound::Included(bytes.into())
15221524
}
15231525
Bound::Excluded(bound) => {
1524-
let bytes = KC::bytes_encode(bound).map_err(Error::Encoding)?;
1525-
Bound::Excluded(bytes.into_owned())
1526+
let bytes = KC::to_bytes(bound).map_err(|err| Error::Encoding(Box::new(err)))?;
1527+
Bound::Excluded(bytes.into())
15261528
}
15271529
Bound::Unbounded => Bound::Unbounded,
15281530
};
@@ -1650,16 +1652,17 @@ impl<KC, DC, C> Database<KC, DC, C> {
16501652
pub fn prefix_iter_mut<'a, 'txn>(
16511653
&self,
16521654
txn: &'txn mut RwTxn,
1653-
prefix: &'a KC::EItem,
1655+
prefix: &'a KC::SelfType,
16541656
) -> Result<RwPrefix<'txn, KC, DC, C>>
16551657
where
1656-
KC: BytesEncode<'a>,
1658+
KC: ToBytes<'a>,
16571659
C: LexicographicComparator,
16581660
{
16591661
assert_eq_env_db_txn!(self, txn);
16601662

1661-
let prefix_bytes = KC::bytes_encode(prefix).map_err(Error::Encoding)?;
1662-
let prefix_bytes = prefix_bytes.into_owned();
1663+
let prefix_bytes = KC::to_bytes(prefix).map_err(|err| Error::Encoding(Box::new(err)))?;
1664+
// TODO optimize, this might do unnecessary allocations on types that are already 'static
1665+
let prefix_bytes = prefix_bytes.into();
16631666
RwCursor::new(txn, self.dbi).map(|cursor| RwPrefix::new(cursor, prefix_bytes))
16641667
}
16651668

@@ -1783,16 +1786,17 @@ impl<KC, DC, C> Database<KC, DC, C> {
17831786
pub fn rev_prefix_iter_mut<'a, 'txn>(
17841787
&self,
17851788
txn: &'txn mut RwTxn,
1786-
prefix: &'a KC::EItem,
1789+
prefix: &'a KC::SelfType,
17871790
) -> Result<RwRevPrefix<'txn, KC, DC, C>>
17881791
where
1789-
KC: BytesEncode<'a>,
1792+
KC: ToBytes<'a>,
17901793
C: LexicographicComparator,
17911794
{
17921795
assert_eq_env_db_txn!(self, txn);
17931796

1794-
let prefix_bytes = KC::bytes_encode(prefix).map_err(Error::Encoding)?;
1795-
let prefix_bytes = prefix_bytes.into_owned();
1797+
let prefix_bytes = KC::to_bytes(prefix).map_err(|err| Error::Encoding(Box::new(err)))?;
1798+
// TODO optimize, this might do unnecessary allocations on types that are already 'static
1799+
let prefix_bytes = prefix_bytes.into();
17961800
RwCursor::new(txn, self.dbi).map(|cursor| RwRevPrefix::new(cursor, prefix_bytes))
17971801
}
17981802

@@ -1896,18 +1900,18 @@ impl<KC, DC, C> Database<KC, DC, C> {
18961900
pub fn put_reserved<'a, F>(
18971901
&self,
18981902
txn: &mut RwTxn,
1899-
key: &'a KC::EItem,
1903+
key: &'a KC::SelfType,
19001904
data_size: usize,
19011905
write_func: F,
19021906
) -> Result<()>
19031907
where
1904-
KC: BytesEncode<'a>,
1908+
KC: ToBytes<'a>,
19051909
F: FnOnce(&mut ReservedSpace) -> io::Result<()>,
19061910
{
19071911
assert_eq_env_db_txn!(self, txn);
19081912

1909-
let key_bytes: Cow<[u8]> = KC::bytes_encode(key).map_err(Error::Encoding)?;
1910-
let mut key_val = unsafe { crate::into_val(&key_bytes) };
1913+
let key_bytes = KC::to_bytes(key).map_err(|err| Error::Encoding(Box::new(err)))?;
1914+
let mut key_val = unsafe { crate::into_val(key_bytes.as_ref()) };
19111915
let mut reserved = ffi::reserve_size_val(data_size);
19121916
let flags = ffi::MDB_RESERVE;
19131917

@@ -1987,20 +1991,20 @@ impl<KC, DC, C> Database<KC, DC, C> {
19871991
&self,
19881992
txn: &mut RwTxn,
19891993
flags: PutFlags,
1990-
key: &'a KC::EItem,
1991-
data: &'a DC::EItem,
1994+
key: &'a KC::SelfType,
1995+
data: &'a DC::SelfType,
19921996
) -> Result<()>
19931997
where
1994-
KC: BytesEncode<'a>,
1995-
DC: BytesEncode<'a>,
1998+
KC: ToBytes<'a>,
1999+
DC: ToBytes<'a>,
19962000
{
19972001
assert_eq_env_db_txn!(self, txn);
19982002

1999-
let key_bytes: Cow<[u8]> = KC::bytes_encode(key).map_err(Error::Encoding)?;
2000-
let data_bytes: Cow<[u8]> = DC::bytes_encode(data).map_err(Error::Encoding)?;
2003+
let key_bytes = KC::to_bytes(key).map_err(|err| Error::Encoding(Box::new(err)))?;
2004+
let data_bytes = DC::to_bytes(data).map_err(|err| Error::Encoding(Box::new(err)))?;
20012005

2002-
let mut key_val = unsafe { crate::into_val(&key_bytes) };
2003-
let mut data_val = unsafe { crate::into_val(&data_bytes) };
2006+
let mut key_val = unsafe { crate::into_val(key_bytes.as_ref()) };
2007+
let mut data_val = unsafe { crate::into_val(data_bytes.as_ref()) };
20042008
let flags = flags.bits();
20052009

20062010
unsafe {
@@ -2046,12 +2050,12 @@ impl<KC, DC, C> Database<KC, DC, C> {
20462050
pub fn get_or_put<'a, 'txn>(
20472051
&'txn self,
20482052
txn: &mut RwTxn,
2049-
key: &'a KC::EItem,
2050-
data: &'a DC::EItem,
2053+
key: &'a KC::SelfType,
2054+
data: &'a DC::SelfType,
20512055
) -> Result<Option<DC::DItem>>
20522056
where
2053-
KC: BytesEncode<'a>,
2054-
DC: BytesEncode<'a> + BytesDecode<'a>,
2057+
KC: ToBytes<'a>,
2058+
DC: ToBytes<'a> + BytesDecode<'a>,
20552059
{
20562060
self.get_or_put_with_flags(txn, PutFlags::empty(), key, data)
20572061
}
@@ -2094,20 +2098,20 @@ impl<KC, DC, C> Database<KC, DC, C> {
20942098
&'txn self,
20952099
txn: &mut RwTxn,
20962100
flags: PutFlags,
2097-
key: &'a KC::EItem,
2098-
data: &'a DC::EItem,
2101+
key: &'a KC::SelfType,
2102+
data: &'a DC::SelfType,
20992103
) -> Result<Option<DC::DItem>>
21002104
where
2101-
KC: BytesEncode<'a>,
2102-
DC: BytesEncode<'a> + BytesDecode<'a>,
2105+
KC: ToBytes<'a>,
2106+
DC: ToBytes<'a> + BytesDecode<'a>,
21032107
{
21042108
assert_eq_env_db_txn!(self, txn);
21052109

2106-
let key_bytes: Cow<[u8]> = KC::bytes_encode(key).map_err(Error::Encoding)?;
2107-
let data_bytes: Cow<[u8]> = DC::bytes_encode(data).map_err(Error::Encoding)?;
2110+
let key_bytes = KC::to_bytes(key).map_err(|err| Error::Encoding(Box::new(err)))?;
2111+
let data_bytes = DC::to_bytes(data).map_err(|err| Error::Encoding(Box::new(err)))?;
21082112

2109-
let mut key_val = unsafe { crate::into_val(&key_bytes) };
2110-
let mut data_val = unsafe { crate::into_val(&data_bytes) };
2113+
let mut key_val = unsafe { crate::into_val(key_bytes.as_ref()) };
2114+
let mut data_val = unsafe { crate::into_val(data_bytes.as_ref()) };
21112115
let flags = (flags | PutFlags::NO_OVERWRITE).bits();
21122116

21132117
let result = unsafe {
@@ -2178,12 +2182,12 @@ impl<KC, DC, C> Database<KC, DC, C> {
21782182
pub fn get_or_put_reserved<'a, 'txn, F>(
21792183
&'txn self,
21802184
txn: &mut RwTxn,
2181-
key: &'a KC::EItem,
2185+
key: &'a KC::SelfType,
21822186
data_size: usize,
21832187
write_func: F,
21842188
) -> Result<Option<DC::DItem>>
21852189
where
2186-
KC: BytesEncode<'a>,
2190+
KC: ToBytes<'a>,
21872191
F: FnOnce(&mut ReservedSpace) -> io::Result<()>,
21882192
DC: BytesDecode<'a>,
21892193
{
@@ -2243,20 +2247,20 @@ impl<KC, DC, C> Database<KC, DC, C> {
22432247
&'txn self,
22442248
txn: &mut RwTxn,
22452249
flags: PutFlags,
2246-
key: &'a KC::EItem,
2250+
key: &'a KC::SelfType,
22472251
data_size: usize,
22482252
write_func: F,
22492253
) -> Result<Option<DC::DItem>>
22502254
where
2251-
KC: BytesEncode<'a>,
2255+
KC: ToBytes<'a>,
22522256
F: FnOnce(&mut ReservedSpace) -> io::Result<()>,
22532257
DC: BytesDecode<'a>,
22542258
{
22552259
assert_eq_env_db_txn!(self, txn);
22562260

2257-
let key_bytes: Cow<[u8]> = KC::bytes_encode(key).map_err(Error::Encoding)?;
2261+
let key_bytes = KC::to_bytes(key).map_err(|err| Error::Encoding(Box::new(err)))?;
22582262

2259-
let mut key_val = unsafe { crate::into_val(&key_bytes) };
2263+
let mut key_val = unsafe { crate::into_val(key_bytes.as_ref()) };
22602264
let mut reserved = ffi::reserve_size_val(data_size);
22612265
let flags = (flags | PutFlags::NO_OVERWRITE).bits() | lmdb_master_sys::MDB_RESERVE;
22622266

@@ -2328,14 +2332,14 @@ impl<KC, DC, C> Database<KC, DC, C> {
23282332
/// wtxn.commit()?;
23292333
/// # Ok(()) }
23302334
/// ```
2331-
pub fn delete<'a>(&self, txn: &mut RwTxn, key: &'a KC::EItem) -> Result<bool>
2335+
pub fn delete<'a>(&self, txn: &mut RwTxn, key: &'a KC::SelfType) -> Result<bool>
23322336
where
2333-
KC: BytesEncode<'a>,
2337+
KC: ToBytes<'a>,
23342338
{
23352339
assert_eq_env_db_txn!(self, txn);
23362340

2337-
let key_bytes: Cow<[u8]> = KC::bytes_encode(key).map_err(Error::Encoding)?;
2338-
let mut key_val = unsafe { crate::into_val(&key_bytes) };
2341+
let key_bytes = KC::to_bytes(key).map_err(|err| Error::Encoding(Box::new(err)))?;
2342+
let mut key_val = unsafe { crate::into_val(key_bytes.as_ref()) };
23392343

23402344
let result = unsafe {
23412345
mdb_result(ffi::mdb_del(txn.txn.txn, self.dbi, &mut key_val, ptr::null_mut()))
@@ -2410,19 +2414,19 @@ impl<KC, DC, C> Database<KC, DC, C> {
24102414
pub fn delete_one_duplicate<'a>(
24112415
&self,
24122416
txn: &mut RwTxn,
2413-
key: &'a KC::EItem,
2414-
data: &'a DC::EItem,
2417+
key: &'a KC::SelfType,
2418+
data: &'a DC::SelfType,
24152419
) -> Result<bool>
24162420
where
2417-
KC: BytesEncode<'a>,
2418-
DC: BytesEncode<'a>,
2421+
KC: ToBytes<'a>,
2422+
DC: ToBytes<'a>,
24192423
{
24202424
assert_eq_env_db_txn!(self, txn);
24212425

2422-
let key_bytes: Cow<[u8]> = KC::bytes_encode(key).map_err(Error::Encoding)?;
2423-
let data_bytes: Cow<[u8]> = DC::bytes_encode(data).map_err(Error::Encoding)?;
2424-
let mut key_val = unsafe { crate::into_val(&key_bytes) };
2425-
let mut data_val = unsafe { crate::into_val(&data_bytes) };
2426+
let key_bytes = KC::to_bytes(key).map_err(|err| Error::Encoding(Box::new(err)))?;
2427+
let data_bytes = DC::to_bytes(data).map_err(|err| Error::Encoding(Box::new(err)))?;
2428+
let mut key_val = unsafe { crate::into_val(key_bytes.as_ref()) };
2429+
let mut data_val = unsafe { crate::into_val(data_bytes.as_ref()) };
24262430

24272431
let result =
24282432
unsafe { mdb_result(ffi::mdb_del(txn.txn.txn, self.dbi, &mut key_val, &mut data_val)) };

0 commit comments

Comments
 (0)