Skip to content

Commit 6b2dc0a

Browse files
committed
Make Error Copy
1 parent b23da77 commit 6b2dc0a

File tree

7 files changed

+62
-104
lines changed

7 files changed

+62
-104
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
## Breaking Changes
2525

26-
* #1349 The "measure_allocs" feature has been removed.
2726
* #1135 The "no_metrics" anti-feature has been replaced with
2827
the "metrics" positive feature.
2928
* #1178 the `Event` enum has become a unified struct that allows
@@ -52,6 +51,9 @@
5251
* #1314 `Subscriber::next_timeout` now requires a mutable self
5352
reference.
5453
* #1337 Bump MSRV to 1.48.
54+
* #1349 The "measure_allocs" feature has been removed.
55+
* `Error` has been modified to be Copy, removing all
56+
heap-allocated variants.
5557

5658

5759
## Bug Fixes

src/config.rs

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ impl StorageParameters {
6868
return Err(Error::Unsupported(
6969
"failed to open database that may \
7070
have been created using a sled version \
71-
earlier than 0.29"
72-
.to_string(),
71+
earlier than 0.29",
7372
));
7473
};
7574
let mut split = line.split(": ").map(String::from);
@@ -282,7 +281,7 @@ impl Inner {
282281
macro_rules! supported {
283282
($cond:expr, $msg:expr) => {
284283
if !$cond {
285-
return Err(Error::Unsupported($msg.to_owned()));
284+
return Err(Error::Unsupported($msg));
286285
}
287286
};
288287
}
@@ -550,15 +549,11 @@ impl Config {
550549
file.try_lock_exclusive()
551550
};
552551

553-
if let Err(e) = try_lock {
554-
return Err(Error::Io(io::Error::new(
552+
if try_lock.is_err() {
553+
return Err(Error::Io(
555554
ErrorKind::Other,
556-
format!(
557-
"could not acquire lock on {:?}: {:?}",
558-
self.db_path().to_string_lossy(),
559-
e
560-
),
561-
)));
555+
"could not acquire database file lock",
556+
));
562557
}
563558
}
564559

@@ -568,28 +563,27 @@ impl Config {
568563
fn verify_config(&self) -> Result<()> {
569564
match self.read_config() {
570565
Ok(Some(old)) => {
571-
supported!(
572-
self.use_compression == old.use_compression,
573-
format!(
574-
"cannot change compression values across restarts. \
575-
old value of use_compression loaded from disk: {}, \
576-
currently set value: {}.",
577-
old.use_compression, self.use_compression,
578-
)
579-
);
566+
if self.use_compression {
567+
supported!(
568+
old.use_compression,
569+
"cannot change compression configuration across restarts. \
570+
this database was created without compression enabled."
571+
);
572+
} else {
573+
supported!(
574+
!old.use_compression,
575+
"cannot change compression configuration across restarts. \
576+
this database was created with compression enabled."
577+
);
578+
}
580579

581580
supported!(
582581
self.segment_size == old.segment_size,
583-
format!(
584-
"cannot change the io buffer size across restarts. \
585-
please change it back to {}",
586-
old.segment_size
587-
)
582+
"cannot change the io buffer size across restarts."
588583
);
589584

590-
supported!(
591-
self.version == old.version,
592-
format!(
585+
if self.version != old.version {
586+
error!(
593587
"This database was created using \
594588
pagecache version {}.{}, but our pagecache \
595589
version is {}.{}. Please perform an upgrade \
@@ -599,8 +593,13 @@ impl Config {
599593
old.version.1,
600594
self.version.0,
601595
self.version.1,
602-
)
603-
);
596+
);
597+
supported!(
598+
self.version == old.version,
599+
"The stored database must use a compatible sled version.
600+
See error log for more details."
601+
);
602+
}
604603
Ok(())
605604
}
606605
Ok(None) => self.write_config(),

src/meta.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ where
5454
merge_operator: RwLock::new(None),
5555
})));
5656
}
57-
Err(Error::CollectionNotFound(_)) => {}
57+
Err(Error::CollectionNotFound) => {}
5858
Err(other) => return Err(other),
5959
}
6060

src/pagecache/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1904,7 +1904,7 @@ impl PageCacheInner {
19041904
if let Some(root) = m.get_root(name) {
19051905
Ok(root)
19061906
} else {
1907-
Err(Error::CollectionNotFound(name.into()))
1907+
Err(Error::CollectionNotFound)
19081908
}
19091909
}
19101910

@@ -2088,10 +2088,10 @@ impl PageCacheInner {
20882088

20892089
// TODO this feels racy, test it better?
20902090
if let Update::Free = update {
2091-
Err(Error::ReportableBug(format!(
2092-
"non-link/replace found in pull of pid {}",
2093-
pid
2094-
)))
2091+
error!("non-link/replace found in pull of pid {}", pid);
2092+
Err(Error::ReportableBug(
2093+
"non-link/replace found in pull of page fragments",
2094+
))
20952095
} else {
20962096
Ok(update)
20972097
}

src/result.rs

Lines changed: 21 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,7 @@ use std::{
55
io,
66
};
77

8-
#[cfg(feature = "testing")]
9-
use backtrace::Backtrace;
10-
11-
use crate::{
12-
pagecache::{DiskPtr, PageView},
13-
IVec,
14-
};
8+
use crate::pagecache::{DiskPtr, PageView};
159

1610
/// The top-level result type for dealing with
1711
/// fallible operations. The errors tend to
@@ -32,27 +26,21 @@ pub(crate) type CasResult<'a, R> =
3226

3327
/// An Error type encapsulating various issues that may come up
3428
/// in the operation of a `Db`.
35-
#[derive(Debug)]
29+
#[derive(Debug, Clone, Copy)]
3630
pub enum Error {
3731
/// The underlying collection no longer exists.
38-
CollectionNotFound(IVec),
32+
CollectionNotFound,
3933
/// The system has been used in an unsupported way.
40-
Unsupported(String),
34+
Unsupported(&'static str),
4135
/// An unexpected bug has happened. Please open an issue on github!
42-
ReportableBug(String),
36+
ReportableBug(&'static str),
4337
/// A read or write error has happened when interacting with the file
4438
/// system.
45-
Io(io::Error),
39+
Io(io::ErrorKind, &'static str),
4640
/// Corruption has been detected in the storage file.
4741
Corruption {
4842
/// The file location that corrupted data was found at.
4943
at: Option<DiskPtr>,
50-
/// A backtrace for where the corruption was encountered.
51-
#[cfg(feature = "testing")]
52-
bt: Backtrace,
53-
/// A backtrace for where the corruption was encountered.
54-
#[cfg(not(feature = "testing"))]
55-
bt: (),
5644
},
5745
// a failpoint has been triggered for testing purposes
5846
#[doc(hidden)]
@@ -62,29 +50,7 @@ pub enum Error {
6250

6351
impl Error {
6452
pub(crate) fn corruption(at: Option<DiskPtr>) -> Error {
65-
Error::Corruption {
66-
at,
67-
#[cfg(feature = "testing")]
68-
bt: Backtrace::new(),
69-
#[cfg(not(feature = "testing"))]
70-
bt: (),
71-
}
72-
}
73-
}
74-
75-
impl Clone for Error {
76-
fn clone(&self) -> Self {
77-
use self::Error::*;
78-
79-
match self {
80-
Io(ioe) => Io(io::Error::new(ioe.kind(), format!("{:?}", ioe))),
81-
CollectionNotFound(name) => CollectionNotFound(name.clone()),
82-
Unsupported(why) => Unsupported(why.clone()),
83-
ReportableBug(what) => ReportableBug(what.clone()),
84-
Corruption { at, bt } => Corruption { at: *at, bt: bt.clone() },
85-
#[cfg(feature = "failpoints")]
86-
FailPoint => FailPoint,
87-
}
53+
Error::Corruption { at }
8854
}
8955
}
9056

@@ -95,13 +61,7 @@ impl PartialEq for Error {
9561
use self::Error::*;
9662

9763
match *self {
98-
CollectionNotFound(ref l) => {
99-
if let CollectionNotFound(ref r) = *other {
100-
l == r
101-
} else {
102-
false
103-
}
104-
}
64+
CollectionNotFound => CollectionNotFound == *other,
10565
Unsupported(ref l) => {
10666
if let Unsupported(ref r) = *other {
10767
l == r
@@ -127,15 +87,15 @@ impl PartialEq for Error {
12787
false
12888
}
12989
}
130-
Io(_) => false,
90+
Io(_, _) => false,
13191
}
13292
}
13393
}
13494

13595
impl From<io::Error> for Error {
13696
#[inline]
13797
fn from(io_error: io::Error) -> Self {
138-
Error::Io(io_error)
98+
Error::Io(io_error.kind(), "io error")
13999
}
140100
}
141101

@@ -144,10 +104,10 @@ impl From<Error> for io::Error {
144104
use self::Error::*;
145105
use std::io::ErrorKind;
146106
match error {
147-
Io(ioe) => ioe,
148-
CollectionNotFound(name) => io::Error::new(
107+
Io(kind, reason) => io::Error::new(kind, reason),
108+
CollectionNotFound => io::Error::new(
149109
ErrorKind::NotFound,
150-
format!("collection not found: {:?}", name),
110+
"collection not found"
151111
),
152112
Unsupported(why) => io::Error::new(
153113
ErrorKind::InvalidInput,
@@ -180,8 +140,8 @@ impl Display for Error {
180140
use self::Error::*;
181141

182142
match *self {
183-
CollectionNotFound(ref name) => {
184-
write!(f, "Collection {:?} does not exist", name,)
143+
CollectionNotFound => {
144+
write!(f, "Collection does not exist")
185145
}
186146
Unsupported(ref e) => write!(f, "Unsupported: {}", e),
187147
ReportableBug(ref e) => write!(
@@ -192,12 +152,12 @@ impl Display for Error {
192152
),
193153
#[cfg(feature = "failpoints")]
194154
FailPoint => write!(f, "Fail point has been triggered."),
195-
Io(ref e) => write!(f, "IO error: {}", e),
196-
Corruption { at, ref bt } => write!(
197-
f,
198-
"Read corrupted data at file offset {:?} backtrace {:?}",
199-
at, bt
200-
),
155+
Io(ref kind, ref reason) => {
156+
write!(f, "IO error: ({:?}, {})", kind, reason)
157+
}
158+
Corruption { at } => {
159+
write!(f, "Read corrupted data at file offset {:?}", at)
160+
}
201161
}
202162
}
203163
}

src/tree.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ fn bounds_error() -> Result<()> {
4343
"Keys and values are limited to \
4444
128gb on 64-bit platforms and
4545
512mb on 32-bit platforms."
46-
.to_string(),
4746
))
4847
}
4948

@@ -938,7 +937,6 @@ impl Tree {
938937
Err(Error::ReportableBug(
939938
"threadpool failed to complete \
940939
action before shutdown"
941-
.to_string(),
942940
))
943941
}
944942
}
@@ -1154,7 +1152,6 @@ impl Tree {
11541152
"must set a merge operator on this Tree \
11551153
before calling merge by calling \
11561154
Tree::set_merge_operator"
1157-
.to_owned(),
11581155
));
11591156
}
11601157

@@ -1813,7 +1810,7 @@ impl Tree {
18131810

18141811
if cursor == u64::max_value() {
18151812
// this collection has been explicitly removed
1816-
return Err(Error::CollectionNotFound(self.tree_id.clone()));
1813+
return Err(Error::CollectionNotFound);
18171814
}
18181815

18191816
let node_opt = self.view_for_pid(cursor, guard)?;

tests/test_tree.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -934,9 +934,9 @@ fn tree_subscribers_and_keyspaces() -> Result<()> {
934934
db.drop_tree(b"1")?;
935935
db.drop_tree(b"2")?;
936936

937-
assert_eq!(t1.get(b""), Err(Error::CollectionNotFound(b"1".into())));
937+
assert_eq!(t1.get(b""), Err(Error::CollectionNotFound));
938938

939-
assert_eq!(t2.get(b""), Err(Error::CollectionNotFound(b"2".into())));
939+
assert_eq!(t2.get(b""), Err(Error::CollectionNotFound));
940940

941941
let guard = pin();
942942
guard.flush();

0 commit comments

Comments
 (0)