Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
0848e314
Commit
0848e314
authored
Nov 25, 2025
by
Tobias Koppers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Initial database stats tool
parent
f54479b9
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
733 additions
and
3 deletions
+733
-3
Cargo.lock
Cargo.lock
+12
-0
Cargo.toml
Cargo.toml
+1
-0
operation.rs
crates/next-api/src/operation.rs
+1
-1
Cargo.toml
crates/next-persistence-tools/Cargo.toml
+23
-0
main.rs
crates/next-persistence-tools/src/main.rs
+319
-0
converting_iter.rs
turbopack/crates/turbo-persistence/src/converting_iter.rs
+89
-0
db.rs
turbopack/crates/turbo-persistence/src/db.rs
+59
-2
dedupe_iter.rs
turbopack/crates/turbo-persistence/src/dedupe_iter.rs
+62
-0
iter.rs
turbopack/crates/turbo-persistence/src/iter.rs
+29
-0
lib.rs
turbopack/crates/turbo-persistence/src/lib.rs
+6
-0
tests.rs
turbopack/crates/turbo-persistence/src/tests.rs
+132
-0
No files found.
Cargo.lock
View file @
0848e314
...
...
@@ -4411,6 +4411,18 @@ dependencies = [
"urlencoding",
]
[[package]]
name = "next-persistence-tools"
version = "0.1.0"
dependencies = [
"anyhow",
"clap",
"next-api",
"rustc-hash 2.1.1",
"turbo-persistence",
"twox-hash 2.1.0",
]
[[package]]
name = "next-swc-napi"
version = "0.0.0"
...
...
Cargo.toml
View file @
0848e314
...
...
@@ -10,6 +10,7 @@ members = [
"crates/next-build"
,
"crates/next-core"
,
"crates/next-custom-transforms"
,
"crates/next-persistence-tools"
,
"crates/next-taskless"
,
"turbopack/crates/*"
,
"turbopack/crates/*/fuzz"
,
...
...
crates/next-api/src/operation.rs
View file @
0848e314
...
...
@@ -148,7 +148,7 @@ async fn pick_endpoint(
op
:
OperationVc
<
Entrypoints
>
,
selector
:
EndpointSelector
,
)
->
Result
<
Vc
<
OptionEndpoint
>>
{
let
endpoints
=
op
.
connect
()
.
strongly_consistent
()
.await
?
;
let
endpoints
=
op
.
read_
strongly_consistent
()
.await
?
;
let
endpoint
=
match
selector
{
EndpointSelector
::
InstrumentationNodeJs
=>
{
endpoints
.instrumentation
.as_ref
()
.map
(|
i
|
i
.node_js
)
...
...
crates/next-persistence-tools/Cargo.toml
0 → 100644
View file @
0848e314
[package]
name
=
"next-persistence-tools"
version
=
"0.1.0"
description
=
"CLI tools for analyzing Next.js persistence cache"
license
=
"MIT"
edition
=
"2024"
autobenches
=
false
[[bin]]
name
=
"next-persistence-tools"
path
=
"src/main.rs"
bench
=
false
[lints]
workspace
=
true
[dependencies]
anyhow
=
{
workspace
=
true
}
clap
=
{
workspace
=
true
,
features
=
["derive"]
}
next-api
=
{
workspace
=
true
}
rustc-hash
=
{
workspace
=
true
}
turbo-persistence
=
{
workspace
=
true
}
twox-hash
=
{
workspace
=
true
}
crates/next-persistence-tools/src/main.rs
0 → 100644
View file @
0848e314
use
std
::{
collections
::
HashMap
,
hash
::
Hasher
,
path
::
PathBuf
};
use
anyhow
::{
Context
,
Result
,
bail
};
use
clap
::
Parser
;
use
rustc_hash
::
FxHashMap
;
use
turbo_persistence
::{
OwnedLookupEntry
,
SerialScheduler
,
TurboPersistence
};
use
twox_hash
::
XxHash64
;
/// CLI tool for analyzing Next.js Turbopack cache statistics
#[derive(Parser,
Debug)]
#[command(name
=
"next-persistence-tools"
)]
#[command(about
=
"Analyze Next.js persistence cache and generate statistics"
,
long_about
=
None)]
struct
Args
{
/// Path to the TurboPersistence cache directory
#[arg(value_name
=
"CACHE_PATH"
)]
cache_path
:
PathBuf
,
/// Include overwritten/old values in the analysis
#[arg(long,
default_value_t
=
false
)]
include_overwritten
:
bool
,
}
/// Statistics for a single cache family
#[derive(Default)]
struct
FamilyStats
{
entry_count
:
u64
,
total_key_size
:
u64
,
total_value_size
:
u64
,
/// Maps value hash to (count, total_size)
unique_value_hashes
:
FxHashMap
<
u64
,
(
u64
,
u64
)
>
,
min_entry_size
:
Option
<
u64
>
,
max_entry_size
:
Option
<
u64
>
,
}
impl
FamilyStats
{
fn
add_entry
(
&
mut
self
,
entry
:
&
OwnedLookupEntry
)
{
self
.entry_count
+=
1
;
let
key_size
=
entry
.key
.len
()
as
u64
;
self
.total_key_size
+=
key_size
;
let
value_size
=
entry
.value
.len
()
as
u64
;
self
.total_value_size
+=
value_size
;
// Hash the value bytes for deduplication tracking
let
mut
hasher
=
XxHash64
::
with_seed
(
0
);
hasher
.write
(
&
entry
.value
);
let
value_hash
=
hasher
.finish
();
// Track duplicate sizes: increment count and add to total size
self
.unique_value_hashes
.entry
(
value_hash
)
.and_modify
(|(
count
,
total_size
)|
{
*
count
+=
1
;
*
total_size
+=
value_size
;
})
.or_insert
((
1
,
value_size
));
// Track entry size distribution
let
entry_size
=
key_size
+
value_size
;
self
.min_entry_size
=
Some
(
self
.min_entry_size
.map_or
(
entry_size
,
|
min
|
min
.min
(
entry_size
)),
);
self
.max_entry_size
=
Some
(
self
.max_entry_size
.map_or
(
entry_size
,
|
max
|
max
.max
(
entry_size
)),
);
}
fn
avg_entry_size
(
&
self
)
->
u64
{
if
self
.entry_count
==
0
{
0
}
else
{
(
self
.total_key_size
+
self
.total_value_size
)
/
self
.entry_count
}
}
fn
total_size
(
&
self
)
->
u64
{
self
.total_key_size
+
self
.total_value_size
}
fn
unique_value_count
(
&
self
)
->
usize
{
self
.unique_value_hashes
.len
()
}
fn
duplicated_size
(
&
self
)
->
u64
{
// Sum up duplicate sizes: for each value that appears >1 time,
// the duplicated size is (total_size - size_of_first_occurrence)
self
.unique_value_hashes
.values
()
.filter_map
(|
&
(
count
,
total_size
)|
{
if
count
>
1
{
// Duplicated size = total - first occurrence
// Since all occurrences have the same size: total_size / count * (count - 1)
let
size_per_occurrence
=
total_size
/
count
;
Some
(
size_per_occurrence
*
(
count
-
1
))
}
else
{
None
}
})
.sum
()
}
}
/// Family names for the 5 KeySpaces used by TurboKeyValueDatabase
const
FAMILY_NAMES
:
&
[
&
str
]
=
&
[
"Infra"
,
// 0: Infrastructure data
"TaskMeta"
,
// 1: Task metadata
"TaskData"
,
// 2: Task actual data
"ForwardTaskCache"
,
// 3: CachedTaskType -> TaskId lookups
"ReverseTaskCache"
,
// 4: TaskId -> CachedTaskType lookups
];
fn
format_size
(
bytes
:
u64
)
->
String
{
const
KIB
:
u64
=
1024
;
const
MIB
:
u64
=
1024
*
KIB
;
const
GIB
:
u64
=
1024
*
MIB
;
if
bytes
>=
GIB
{
format!
(
"{:.2} GiB"
,
bytes
as
f64
/
GIB
as
f64
)
}
else
if
bytes
>=
MIB
{
format!
(
"{:.2} MiB"
,
bytes
as
f64
/
MIB
as
f64
)
}
else
if
bytes
>=
KIB
{
format!
(
"{:.2} KiB"
,
bytes
as
f64
/
KIB
as
f64
)
}
else
{
format!
(
"{} B"
,
bytes
)
}
}
fn
main
()
->
Result
<
()
>
{
let
args
=
Args
::
parse
();
if
!
args
.cache_path
.exists
()
{
bail!
(
"Cache path does not exist: {}"
,
args
.cache_path
.display
());
}
println!
(
"Analyzing cache at: {}"
,
args
.cache_path
.display
());
println!
(
"Include overwritten values: {}"
,
args
.include_overwritten
);
println!
();
// Open the database in read-only mode
let
db
:
TurboPersistence
<
SerialScheduler
>
=
TurboPersistence
::
open_read_only
(
args
.cache_path
.clone
())
.context
(
"Failed to open TurboPersistence database"
)
?
;
// Collect statistics for each family
let
mut
family_stats
:
HashMap
<
usize
,
FamilyStats
>
=
HashMap
::
new
();
let
mut
total_entries
=
0u64
;
let
mut
total_errors
=
0u64
;
println!
(
"Scanning cache families..."
);
println!
();
// Iterate over each family (0-4)
for
family
in
0
..
5
{
let
family_name
=
FAMILY_NAMES
.get
(
family
)
.unwrap_or
(
&
"Unknown"
);
print!
(
"Processing family {}: {}... "
,
family
,
family_name
);
let
stats
=
family_stats
.entry
(
family
)
.or_default
();
match
db
.iter
(
family
,
args
.include_overwritten
)
{
Ok
(
iter
)
=>
{
let
mut
family_entries
=
0u64
;
for
entry_result
in
iter
{
match
entry_result
{
Ok
(
entry
)
=>
{
stats
.add_entry
(
&
entry
);
family_entries
+=
1
;
total_entries
+=
1
;
}
Err
(
e
)
=>
{
eprintln!
(
"Error reading entry: {}"
,
e
);
total_errors
+=
1
;
}
}
}
println!
(
"{} entries"
,
family_entries
);
}
Err
(
e
)
=>
{
println!
(
"ERROR: {}"
,
e
);
total_errors
+=
1
;
}
}
}
println!
();
println!
(
"═══════════════════════════════════════════════════════════════════════════"
);
println!
(
" CACHE STATISTICS SUMMARY"
);
println!
(
"═══════════════════════════════════════════════════════════════════════════"
);
println!
();
// Print per-family statistics
println!
(
"PER-FAMILY STATISTICS:"
);
println!
(
"─────────────────────────────────────────────────────────────────────────────"
);
println!
(
"{:<20} {:>12} {:>15} {:>15} {:>15}"
,
"Family"
,
"Entries"
,
"Total Size"
,
"Unique Values"
,
"Avg Entry Size"
);
println!
(
"─────────────────────────────────────────────────────────────────────────────"
);
let
mut
grand_total_size
=
0u64
;
let
mut
grand_unique_values
=
0u64
;
for
family
in
0
..
5
{
if
let
Some
(
stats
)
=
family_stats
.get
(
&
family
)
{
let
family_name
=
FAMILY_NAMES
.get
(
family
)
.unwrap_or
(
&
"Unknown"
);
println!
(
"{:<20} {:>12} {:>15} {:>15} {:>15}"
,
family_name
,
stats
.entry_count
,
format_size
(
stats
.total_size
()),
stats
.unique_value_hashes
.len
(),
format_size
(
stats
.avg_entry_size
())
);
grand_total_size
+=
stats
.total_size
();
grand_unique_values
+=
stats
.unique_value_hashes
.len
()
as
u64
;
}
}
println!
(
"─────────────────────────────────────────────────────────────────────────────"
);
println!
(
"{:<20} {:>12} {:>15} {:>15}"
,
"TOTAL"
,
total_entries
,
format_size
(
grand_total_size
),
grand_unique_values
);
println!
();
// Print detailed breakdown for each family
println!
();
println!
(
"DETAILED FAMILY BREAKDOWN:"
);
println!
(
"═══════════════════════════════════════════════════════════════════════════"
);
for
family
in
0
..
5
{
if
let
Some
(
stats
)
=
family_stats
.get
(
&
family
)
{
if
stats
.entry_count
==
0
{
continue
;
}
let
family_name
=
FAMILY_NAMES
.get
(
family
)
.unwrap_or
(
&
"Unknown"
);
println!
();
println!
(
"Family {}: {}"
,
family
,
family_name
);
println!
(
"─────────────────────────────────────────────────────────────────────────────"
);
println!
(
" Entries: {}"
,
stats
.entry_count
);
println!
(
" Total key size: {}"
,
format_size
(
stats
.total_key_size
));
println!
(
" Total value size: {}"
,
format_size
(
stats
.total_value_size
)
);
println!
(
" Total size: {}"
,
format_size
(
stats
.total_size
()));
println!
(
" Unique values: {} ({:.1}
%
deduplication)"
,
stats
.unique_value_count
(),
if
stats
.entry_count
>
0
{
(
1.0
-
stats
.unique_value_count
()
as
f64
/
stats
.entry_count
as
f64
)
*
100.0
}
else
{
0.0
}
);
let
dup_size
=
stats
.duplicated_size
();
let
dup_pct
=
if
stats
.total_value_size
>
0
{
(
dup_size
as
f64
/
stats
.total_value_size
as
f64
)
*
100.0
}
else
{
0.0
};
println!
(
" Duplicated size: {} ({:.1}
%
of total value size)"
,
format_size
(
dup_size
),
dup_pct
);
println!
(
" Avg entry size: {}"
,
format_size
(
stats
.avg_entry_size
())
);
println!
(
" Min entry size: {}"
,
stats
.min_entry_size
.map
(
format_size
)
.unwrap_or_else
(||
"N/A"
.to_string
())
);
println!
(
" Max entry size: {}"
,
stats
.max_entry_size
.map
(
format_size
)
.unwrap_or_else
(||
"N/A"
.to_string
())
);
// Calculate percentage of total
let
pct
=
if
grand_total_size
>
0
{
(
stats
.total_size
()
as
f64
/
grand_total_size
as
f64
)
*
100.0
}
else
{
0.0
};
println!
(
"
%
of total: {:.1}
%
"
,
pct
);
}
}
println!
();
println!
(
"═══════════════════════════════════════════════════════════════════════════"
);
if
total_errors
>
0
{
println!
();
println!
(
"⚠️ Total errors encountered: {}"
,
total_errors
);
}
println!
();
println!
(
"Analysis complete!"
);
Ok
(())
}
turbopack/crates/turbo-persistence/src/converting_iter.rs
0 → 100644
View file @
0848e314
use
anyhow
::
Result
;
use
crate
::{
arc_slice
::
ArcSlice
,
compression
::
decompress_into_arc
,
db
::
TurboPersistence
,
lookup_entry
::{
LazyLookupValue
,
LookupValue
},
merge_iter
::
MergeIter
,
parallel_scheduler
::
ParallelScheduler
,
static_sorted_file
::
StaticSortedFileIter
,
};
/// Owned version of a lookup entry that can be returned from an iterator
#[derive(Clone)]
pub
struct
OwnedLookupEntry
{
/// The hash of the key.
pub
hash
:
u64
,
/// The key.
pub
key
:
ArcSlice
<
u8
>
,
/// The value.
pub
value
:
ArcSlice
<
u8
>
,
}
/// Iterator that converts LookupEntry to OwnedLookupEntry
pub
(
crate
)
struct
ConvertingIter
<
'l
,
S
:
ParallelScheduler
>
{
pub
(
crate
)
inner
:
MergeIter
<
'l
,
StaticSortedFileIter
<
'l
>>
,
pub
(
crate
)
db
:
&
'l
TurboPersistence
<
S
>
,
}
impl
<
'l
,
S
:
ParallelScheduler
>
Iterator
for
ConvertingIter
<
'l
,
S
>
{
type
Item
=
Result
<
OwnedLookupEntry
>
;
fn
next
(
&
mut
self
)
->
Option
<
Self
::
Item
>
{
loop
{
let
lookup_entry
=
match
self
.inner
.next
()
?
{
Ok
(
entry
)
=>
entry
,
Err
(
e
)
=>
return
Some
(
Err
(
e
)),
};
// Extract the value based on its type
match
&
lookup_entry
.value
{
LazyLookupValue
::
Eager
(
LookupValue
::
Slice
{
value
})
=>
{
return
Some
(
Ok
(
OwnedLookupEntry
{
hash
:
lookup_entry
.hash
,
key
:
lookup_entry
.key
.clone
(),
value
:
value
.clone
(),
}));
}
LazyLookupValue
::
Eager
(
LookupValue
::
Blob
{
sequence_number
})
=>
{
// Read the blob
match
self
.db
.read_blob
(
*
sequence_number
)
{
Ok
(
blob
)
=>
{
return
Some
(
Ok
(
OwnedLookupEntry
{
hash
:
lookup_entry
.hash
,
key
:
lookup_entry
.key
.clone
(),
value
:
blob
,
}));
}
Err
(
e
)
=>
{
return
Some
(
Err
(
e
));
}
}
}
LazyLookupValue
::
Eager
(
LookupValue
::
Deleted
)
=>
{
// Skip deleted entries
continue
;
}
LazyLookupValue
::
Medium
{
uncompressed_size
,
block
,
}
=>
{
// Decompress the medium value
match
decompress_into_arc
(
*
uncompressed_size
,
*
block
,
None
,
false
)
{
Ok
(
decompressed
)
=>
{
return
Some
(
Ok
(
OwnedLookupEntry
{
hash
:
lookup_entry
.hash
,
key
:
lookup_entry
.key
.clone
(),
value
:
ArcSlice
::
from
(
decompressed
),
}));
}
Err
(
e
)
=>
{
return
Some
(
Err
(
e
));
}
}
}
}
}
}
}
turbopack/crates/turbo-persistence/src/db.rs
View file @
0848e314
...
...
@@ -11,6 +11,7 @@ use std::{
use
anyhow
::{
Context
,
Result
,
bail
};
use
byteorder
::{
BE
,
ReadBytesExt
,
WriteBytesExt
};
use
either
::
Either
;
use
jiff
::
Timestamp
;
use
memmap2
::
Mmap
;
use
parking_lot
::{
Mutex
,
RwLock
};
...
...
@@ -27,6 +28,8 @@ use crate::{
KEY_BLOCK_CACHE_SIZE
,
MAX_ENTRIES_PER_COMPACTED_FILE
,
VALUE_BLOCK_AVG_SIZE
,
VALUE_BLOCK_CACHE_SIZE
,
},
converting_iter
::
ConvertingIter
,
dedupe_iter
::
DedupeIter
,
key
::{
StoreKey
,
hash_key
},
lookup_entry
::{
LookupEntry
,
LookupValue
},
merge_iter
::
MergeIter
,
...
...
@@ -129,7 +132,7 @@ pub struct TurboPersistence<S: ParallelScheduler> {
}
/// The inner state of the database.
struct
Inner
{
pub
(
crate
)
struct
Inner
{
/// The list of meta files in the database. This is used to derive the SST files.
meta_files
:
Vec
<
MetaFile
>
,
/// The current sequence number for the database.
...
...
@@ -379,7 +382,7 @@ impl<S: ParallelScheduler> TurboPersistence<S> {
/// Reads and decompresses a blob file. This is not backed by any cache.
#[tracing
::
instrument(level
=
"info"
,
name
=
"reading database blob"
,
skip_all)]
fn
read_blob
(
&
self
,
seq
:
u32
)
->
Result
<
ArcSlice
<
u8
>>
{
pub
(
crate
)
fn
read_blob
(
&
self
,
seq
:
u32
)
->
Result
<
ArcSlice
<
u8
>>
{
let
path
=
self
.path
.join
(
format!
(
"{seq:08}.blob"
));
let
mmap
=
unsafe
{
Mmap
::
map
(
&
File
::
open
(
&
path
)
?
)
?
};
#[cfg(unix)]
...
...
@@ -1354,6 +1357,60 @@ impl<S: ParallelScheduler> TurboPersistence<S> {
.collect
())
}
/// Iterates over all key-value pairs in the specified family.
/// If `include_overwritten` is false, only the latest value for each key is returned.
/// If `include_overwritten` is true, all values including overwritten ones are returned.
pub
fn
iter
(
&
self
,
family
:
usize
,
include_overwritten
:
bool
,
)
->
Result
<
crate
::
iter
::
FamilyIter
<
'_
,
S
>>
{
let
guard
=
self
.inner
.read
();
let
family
=
family
as
u32
;
// SAFETY: We create a raw pointer to access the guard's data while we still own the guard.
// The guard will be stored in FamilyIter alongside the iterators that borrow from it,
// ensuring the data remains valid for the lifetime of the iterators.
// This is safe because:
// 1. The guard protects the data from being modified while we iterate
// 2. The guard is stored in the same struct as the iterators, so it outlives them
// 3. We never access the data through the raw pointer after the guard is dropped
let
guard_ptr
=
&*
guard
as
*
const
Inner
;
let
mut
sst_iters
=
Vec
::
new
();
for
meta_file
in
unsafe
{
&
(
*
guard_ptr
)
.meta_files
}
{
if
meta_file
.family
()
!=
family
{
continue
;
}
for
entry
in
meta_file
.entries
()
{
let
sst
=
entry
.sst
(
meta_file
)
?
;
let
iter
=
sst
.iter
(
&
self
.key_block_cache
,
&
self
.value_block_cache
)
?
;
sst_iters
.push
(
iter
);
}
}
// Create merge iterator
let
merge_iter
=
MergeIter
::
new
(
sst_iters
.into_iter
())
?
;
// Wrap in converting iterator
let
converting_iter
=
ConvertingIter
{
inner
:
merge_iter
,
db
:
self
,
};
// Conditionally wrap in DedupeIter based on include_overwritten
let
inner
=
if
include_overwritten
{
Either
::
Left
(
converting_iter
)
}
else
{
Either
::
Right
(
DedupeIter
::
new
(
converting_iter
))
};
Ok
(
crate
::
iter
::
FamilyIter
{
_guard
:
guard
,
inner
,
})
}
/// Shuts down the database. This will print statistics if the `print_stats` feature is enabled.
pub
fn
shutdown
(
&
self
)
->
Result
<
()
>
{
#[cfg(feature
=
"print_stats"
)]
...
...
turbopack/crates/turbo-persistence/src/dedupe_iter.rs
0 → 100644
View file @
0848e314
use
anyhow
::
Result
;
use
crate
::
converting_iter
::
OwnedLookupEntry
;
/// Iterator that deduplicates consecutive entries with the same key.
/// Since the input iterator is sorted by (hash, key), we only need to compare consecutive entries.
pub
struct
DedupeIter
<
T
:
Iterator
<
Item
=
Result
<
OwnedLookupEntry
>>>
{
iter
:
T
,
current
:
Option
<
OwnedLookupEntry
>
,
}
impl
<
T
:
Iterator
<
Item
=
Result
<
OwnedLookupEntry
>>>
DedupeIter
<
T
>
{
pub
fn
new
(
iter
:
T
)
->
Self
{
Self
{
iter
,
current
:
None
,
}
}
}
impl
<
T
:
Iterator
<
Item
=
Result
<
OwnedLookupEntry
>>>
Iterator
for
DedupeIter
<
T
>
{
type
Item
=
Result
<
OwnedLookupEntry
>
;
fn
next
(
&
mut
self
)
->
Option
<
Self
::
Item
>
{
loop
{
// Get the next entry from the underlying iterator
match
self
.iter
.next
()
{
Some
(
Ok
(
next_entry
))
=>
{
// Check if we have a current entry
match
&
self
.current
{
Some
(
current_entry
)
=>
{
// Compare keys (hash and key bytes)
if
current_entry
.hash
==
next_entry
.hash
&&
current_entry
.key
.as_ref
()
==
next_entry
.key
.as_ref
()
{
// Same key - update current with the new value (keep latest)
self
.current
=
Some
(
next_entry
);
continue
;
}
else
{
// Different key - return the current entry and store the new one
let
to_return
=
self
.current
.replace
(
next_entry
);
return
to_return
.map
(
Ok
);
}
}
None
=>
{
// No current entry - store this one and continue
self
.current
=
Some
(
next_entry
);
}
}
}
Some
(
Err
(
e
))
=>
{
// Pass through errors
return
Some
(
Err
(
e
));
}
None
=>
{
// Iterator exhausted - return the last current entry if any
return
self
.current
.take
()
.map
(
Ok
);
}
}
}
}
}
turbopack/crates/turbo-persistence/src/iter.rs
0 → 100644
View file @
0848e314
use
anyhow
::
Result
;
use
either
::
Either
;
use
parking_lot
::
RwLockReadGuard
;
use
crate
::{
converting_iter
::{
ConvertingIter
,
OwnedLookupEntry
},
db
::
Inner
,
dedupe_iter
::
DedupeIter
,
parallel_scheduler
::
ParallelScheduler
,
};
/// Iterator over entries in a family
pub
struct
FamilyIter
<
'l
,
S
:
ParallelScheduler
>
{
pub
(
crate
)
inner
:
Either
<
ConvertingIter
<
'l
,
S
>
,
DedupeIter
<
ConvertingIter
<
'l
,
S
>>>
,
/// The read guard to keep the inner data alive.
/// Must be kept last to ensure it is dropped after the iterators.
pub
(
crate
)
_guard
:
RwLockReadGuard
<
'l
,
Inner
>
,
}
impl
<
'l
,
S
:
ParallelScheduler
>
Iterator
for
FamilyIter
<
'l
,
S
>
{
type
Item
=
Result
<
OwnedLookupEntry
>
;
fn
next
(
&
mut
self
)
->
Option
<
Self
::
Item
>
{
match
&
mut
self
.inner
{
Either
::
Left
(
iter
)
=>
iter
.next
(),
Either
::
Right
(
iter
)
=>
iter
.next
(),
}
}
}
turbopack/crates/turbo-persistence/src/lib.rs
View file @
0848e314
...
...
@@ -9,7 +9,10 @@ mod collector_entry;
mod
compaction
;
mod
compression
;
mod
constants
;
mod
converting_iter
;
mod
db
;
mod
dedupe_iter
;
mod
iter
;
mod
key
;
mod
lookup_entry
;
mod
merge_iter
;
...
...
@@ -26,8 +29,11 @@ mod write_batch;
mod
tests
;
pub
use
arc_slice
::
ArcSlice
;
pub
use
converting_iter
::
OwnedLookupEntry
;
pub
use
db
::{
CompactConfig
,
MetaFileEntryInfo
,
MetaFileInfo
,
TurboPersistence
};
pub
use
iter
::
FamilyIter
;
pub
use
key
::{
KeyBase
,
QueryKey
,
StoreKey
};
pub
use
lookup_entry
::
LookupEntry
;
pub
use
parallel_scheduler
::{
ParallelScheduler
,
SerialScheduler
};
pub
use
value_buf
::
ValueBuffer
;
pub
use
write_batch
::
WriteBatch
;
turbopack/crates/turbo-persistence/src/tests.rs
View file @
0848e314
...
...
@@ -143,6 +143,19 @@ fn full_cycle() -> Result<()> {
assert_eq!
(
db
.get
(
0
,
&
[
42u8
,
42
])
?
,
None
);
assert_eq!
(
db
.get
(
0
,
&
[
1u8
])
?
,
None
);
assert_eq!
(
db
.get
(
0
,
&
[
255u8
])
?
,
None
);
// Test iter() with deduplication - basic smoke test
let
mut
count
=
0
;
for
entry_result
in
db
.iter
(
0
,
false
)
?
{
let
entry
=
entry_result
?
;
count
+=
1
;
// Basic validation - entries should have data
assert
!
(
!
entry
.key
.is_empty
());
assert
!
(
!
entry
.value
.is_empty
());
}
// Should have at least the 90 entries from this test
assert
!
(
count
>=
90
,
"Expected at least 90 entries, got {}"
,
count
);
Ok
(())
},
);
...
...
@@ -165,6 +178,16 @@ fn full_cycle() -> Result<()> {
assert_eq!
(
db
.get
(
0
,
&
[
42u8
,
42
])
?
,
None
);
assert_eq!
(
db
.get
(
0
,
&
[
1u8
])
?
,
None
);
assert_eq!
(
db
.get
(
0
,
&
[
255u8
])
?
,
None
);
// Test iter() - basic smoke test
let
mut
count
=
0
;
for
entry_result
in
db
.iter
(
0
,
false
)
?
{
entry_result
?
;
count
+=
1
;
}
// Should have at least 90 entries
assert
!
(
count
>=
90
,
"Expected at least 90 entries, got {}"
,
count
);
Ok
(())
},
);
...
...
@@ -186,6 +209,115 @@ fn full_cycle() -> Result<()> {
assert
!
(
db
.get
(
8
,
&
[
8u8
,
8
])
?
.is_none
());
assert
!
(
db
.get
(
8
,
&
[
0u8
])
?
.is_none
());
assert
!
(
db
.get
(
8
,
&
[
255u8
])
?
.is_none
());
// Test iter() on each family - basic smoke test
// In standalone run, each family should have 1 entry
// In "All" run, family 0 may have entries from other tests
for
family
in
0
..
16
{
let
mut
count
=
0
;
let
mut
found_expected_entry
=
false
;
for
entry_result
in
db
.iter
(
family
,
false
)
?
{
let
entry
=
entry_result
?
;
count
+=
1
;
// Check if this is the entry from this specific test
if
entry
.key
.len
()
==
1
&&
entry
.key
[
0
]
==
family
as
u8
&&
entry
.value
.len
()
==
1
&&
entry
.value
[
0
]
==
family
as
u8
{
found_expected_entry
=
true
;
}
}
assert
!
(
count
>=
1
,
"Expected at least 1 entry in family {}"
,
family
);
assert
!
(
found_expected_entry
,
"Expected to find entry for family {}"
,
family
);
}
Ok
(())
},
);
test_case
(
&
mut
test_cases
,
"Overwrites with iter"
,
|
batch
|
{
// Write same keys multiple times
for
round
in
0
..
5u8
{
for
i
in
10
..
20u8
{
batch
.put
(
0
,
vec!
[
i
],
vec!
[
round
,
i
]
.into
())
?
;
}
unsafe
{
batch
.flush
(
0
)
?
};
}
Ok
(())
},
|
db
|
{
// Verify latest values
for
i
in
10
..
20u8
{
let
Some
(
value
)
=
db
.get
(
0
,
&
[
i
])
?
else
{
panic!
(
"Value not found for key {}"
,
i
);
};
assert_eq!
(
&*
value
,
&
[
4u8
,
i
],
"Expected latest round (4) for key {}"
,
i
);
}
// Test iter with deduplication - should only get latest values
// Count entries that match this test's pattern
let
mut
count_from_this_test
=
0
;
for
entry_result
in
db
.iter
(
0
,
false
)
?
{
let
entry
=
entry_result
?
;
// Only validate entries from this specific test
if
entry
.key
.len
()
==
1
&&
entry
.value
.len
()
==
2
{
let
key
=
entry
.key
[
0
];
if
(
10
..
20
)
.contains
(
&
key
)
{
count_from_this_test
+=
1
;
assert_eq!
(
entry
.value
[
0
],
4u8
,
"Expected round 4 for key {}"
,
key
);
assert_eq!
(
entry
.value
[
1
],
key
);
}
}
}
assert_eq!
(
count_from_this_test
,
10
,
"Expected 10 deduplicated entries from this test"
);
// Test iter with all entries - count entries from this test only
let
mut
count_from_this_test_all
=
0
;
let
mut
max_round_seen
=
0u8
;
for
entry_result
in
db
.iter
(
0
,
true
)
?
{
let
entry
=
entry_result
?
;
// Only count entries from this specific test
if
entry
.key
.len
()
==
1
&&
entry
.value
.len
()
==
2
{
let
key
=
entry
.key
[
0
];
if
(
10
..
20
)
.contains
(
&
key
)
{
count_from_this_test_all
+=
1
;
let
round
=
entry
.value
[
0
];
assert
!
(
round
<
5
,
"Round should be 0-4"
);
assert_eq!
(
entry
.value
[
1
],
key
);
max_round_seen
=
max_round_seen
.max
(
round
);
}
}
}
// We should have at least 10 entries (one per key), possibly up to 50 before compaction
assert
!
(
count_from_this_test_all
>=
10
&&
count_from_this_test_all
<=
50
,
"Expected between 10-50 entries from this test, got {}"
,
count_from_this_test_all
);
// If we only have 10 entries, they should all be round 4 (after compaction)
if
count_from_this_test_all
==
10
{
assert_eq!
(
max_round_seen
,
4
,
"After compaction, only latest round should remain"
);
}
Ok
(())
},
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment