Skip to main content

perforce_cli/cmd/
dbstat.rs

1use std::ffi::OsStr;
2use std::path::{Path, PathBuf};
3use std::process::{Child, Command, Stdio};
4
5use super::{ExclusiveOption, SubCommand, Unselected};
6
7use crate::global::GlobalOpts;
8use crate::spawn::ParameterizedSpawn;
9
10/// Selection marker for the `-a` option: report statistics for all tables.
11///
12/// Because `-a` takes no positional arguments, this state carries no table
13/// list; spawn it with `()`.
14#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
15pub struct DisplayAllTables;
16
17impl ExclusiveOption for DisplayAllTables {
18    fn inject_args(&self, command: &mut Command) {
19        command.arg("-a");
20    }
21}
22
23/// The `-s` form: `p4 [g-opts] dbstat -s`.
24#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
25pub struct Size;
26
27/// The scan form.
28///
29#[cfg_attr(
30    feature = "lt2016_1",
31    doc = "`p4 [g-opts] dbstat [-h] {-a | dbtable ...}`."
32)]
33#[cfg_attr(
34    not(feature = "lt2016_1"),
35    doc = "`p4 [g-opts] dbstat [-h][-f] {-a | dbtable ...}`."
36)]
37///
38/// The `M` type parameter tracks the table selection: [`Unselected`] spawns
39/// with a list of `dbtable` arguments; [`DisplayAllTables`] supplies `-a` and
40/// spawns without arguments.
41#[derive(Debug, Clone, PartialEq, Eq)]
42pub struct Scan<M = Unselected> {
43    show_histogram: bool,
44
45    #[cfg(not(feature = "lt2016_1"))]
46    show_free_pages: bool,
47
48    mode: M,
49}
50
51impl ExclusiveOption for Scan<Unselected> {
52    fn inject_args(&self, command: &mut Command) {
53        if self.show_histogram {
54            command.arg("-h");
55        }
56
57        #[cfg(not(feature = "lt2016_1"))]
58        if self.show_free_pages {
59            command.arg("-f");
60        }
61    }
62}
63
64impl ExclusiveOption for Scan<DisplayAllTables> {
65    fn inject_args(&self, command: &mut Command) {
66        if self.show_histogram {
67            command.arg("-h");
68        }
69
70        #[cfg(not(feature = "lt2016_1"))]
71        if self.show_free_pages {
72            command.arg("-f");
73        }
74
75        command.arg("-a");
76    }
77}
78
79/// The checkpoint form:
80/// `p4 [g-opts] dbstat -p [-w worklevel] [-n numberOfFiles] {-a | dbtable ...}`.
81///
82/// Mutually exclusive with the scan form: `-p` cannot be combined with `-h`
83/// or `-f`.
84#[cfg(not(feature = "lt2023_1"))]
85#[derive(Debug, Clone, PartialEq, Eq)]
86pub struct CheckPoint<M = Unselected> {
87    worklevel: Option<u64>,
88
89    number_files: Option<u64>,
90
91    mode: M,
92}
93
94#[cfg(not(feature = "lt2023_1"))]
95impl ExclusiveOption for CheckPoint<Unselected> {
96    fn inject_args(&self, command: &mut Command) {
97        command.arg("-p");
98
99        if let Some(worklevel) = self.worklevel {
100            command.arg("-w").arg(worklevel.to_string());
101        }
102
103        if let Some(number_files) = self.number_files {
104            command.arg("-n").arg(number_files.to_string());
105        }
106    }
107}
108
109#[cfg(not(feature = "lt2023_1"))]
110impl ExclusiveOption for CheckPoint<DisplayAllTables> {
111    fn inject_args(&self, command: &mut Command) {
112        command.arg("-p");
113
114        if let Some(worklevel) = self.worklevel {
115            command.arg("-w").arg(worklevel.to_string());
116        }
117
118        if let Some(number_files) = self.number_files {
119            command.arg("-n").arg(number_files.to_string());
120        }
121
122        command.arg("-a");
123    }
124}
125
126impl ExclusiveOption for Size {
127    fn inject_args(&self, command: &mut Command) {
128        command.arg("-s");
129    }
130}
131
132// ---------------------------------------------------------------------------
133// Command struct
134// ---------------------------------------------------------------------------
135
136#[cfg_attr(
137    feature = "lt2016_1",
138    doc = "`p4 [g-opts] dbstat [-h] {-a | dbtable ...}`"
139)]
140#[cfg_attr(
141    all(feature = "lt2023_1", not(feature = "lt2016_1")),
142    doc = "`p4 [g-opts] dbstat [-h][-f] {-a | dbtable ...}`"
143)]
144#[cfg_attr(
145    not(feature = "lt2023_1"),
146    doc = "`p4 [g-opts] dbstat [-h][-f] | [-p [-w worklevel] [-n numberOfFiles]] {-a | dbtable ...}`"
147)]
148///
149/// `p4 [g-opts] dbstat -s`
150///
151/// Displays size or simple statistics for one or more database tables. The
152/// `dbtable` corresponds to the `db.*` files in your server's root directory.
153///
154/// The `M` type parameter tracks the command form at compile time, starting
155/// as [`Unselected`] from [`DBStat::new`]. The available states and
156/// transitions are:
157/// - [`Unselected`] / [`DisplayAllTables`]: the bare `{-a | dbtable ...}`
158///   selection tail; spawn the former with a list of table names, or use
159///   [`Self::all`] to supply `-a` and spawn the latter with `()`. Both states
160///   can enter the scan and checkpoint forms; the transitions defined on
161///   `DisplayAllTables` keep `-a`.
162#[cfg_attr(
163    feature = "lt2016_1",
164    doc = "- [`Scan`] (entered via [`Self::show_histogram`]): the histogram option followed by a table selection; [`Self::all`] supplies `-a` and transitions to the [`DisplayAllTables`] state."
165)]
166#[cfg_attr(
167    not(feature = "lt2016_1"),
168    doc = "- [`Scan`] (entered via [`Self::show_histogram`] or [`Self::show_free_pages`]): histogram and free-page options followed by a table selection; [`Self::all`] supplies `-a` and transitions to the [`DisplayAllTables`] state."
169)]
170#[cfg_attr(
171    not(feature = "lt2023_1"),
172    doc = "- [`CheckPoint`] (entered via [`Self::checkpoint`]): `-p` with optional `-w`/`-n`; mutually exclusive with the scan form."
173)]
174/// - [`Size`] (entered via [`Self::size`]): the `-s` form.
175#[derive(Debug, Clone, Default)]
176pub struct DBStat<M = Unselected> {
177    bin: PathBuf,
178
179    global_opts: GlobalOpts,
180
181    mode: M,
182}
183
184// ---------------------------------------------------------------------------
185// Constructor and form transitions
186// ---------------------------------------------------------------------------
187
188impl DBStat<Unselected> {
189    /// Creates a new `p4 dbstat` command.
190    ///
191    /// `bin` is the path to the Perforce command-line executable. Call one of
192    /// the form transitions to select a command form.
193    pub fn new(bin: impl Into<PathBuf>, global_opts: GlobalOpts) -> Self {
194        Self {
195            bin: bin.into(),
196            global_opts,
197            mode: Unselected,
198        }
199    }
200
201    /// Enters the scan form with `-h` enabled:
202    #[cfg_attr(feature = "lt2016_1", doc = "`p4 dbstat [-h] {-a | dbtable ...}`.")]
203    #[cfg_attr(
204        not(feature = "lt2016_1"),
205        doc = "`p4 dbstat [-h][-f] {-a | dbtable ...}`."
206    )]
207    ///
208    #[cfg_attr(
209        feature = "lt2024_1",
210        doc = "Display a histogram showing distances between leaf pages."
211    )]
212    #[cfg_attr(
213        not(feature = "lt2024_1"),
214        doc = "Display a histogram showing distances between leaf pages. This enables you to anticipate disk seeks required for sequential database scans. This scans only the internal node pages of the file, not the leaf or overflow pages, and thus reads only 1-5% of the file.",
215        doc = "",
216        doc = "> **Note**",
217        doc = ">",
218        doc = "> This option blocks write access to the database while it scans the tables. It is best to get guidance from [Perforce Support](https://www.perforce.com/support?product=Helix%20Core) before using this option."
219    )]
220    ///
221    /// Use the scan state's `all` transition to supply `-a`; otherwise
222    /// spawn with a list of table names.
223    pub fn show_histogram(self) -> DBStat<Scan<Unselected>> {
224        DBStat {
225            bin: self.bin,
226            global_opts: self.global_opts,
227            mode: Scan {
228                show_histogram: true,
229                #[cfg(not(feature = "lt2016_1"))]
230                show_free_pages: false,
231                mode: Unselected,
232            },
233        }
234    }
235
236    /// Enters the scan form with `-f` enabled:
237    /// `p4 dbstat [-h][-f] {-a | dbtable ...}`.
238    ///
239    #[cfg_attr(
240        feature = "lt2024_1",
241        doc = "Displays a page count, free pages, and percent free data for the specified tables."
242    )]
243    #[cfg_attr(
244        not(feature = "lt2024_1"),
245        doc = "Displays a page count, free pages, and percent free data for the specified tables. Using the `-f` with the `-h` option, this command shows a histogram of free page distribution."
246    )]
247    ///
248    /// Use the scan state's `all` transition to supply `-a`; otherwise
249    /// spawn with a list of table names.
250    #[cfg(not(feature = "lt2016_1"))]
251    pub fn show_free_pages(self) -> DBStat<Scan<Unselected>> {
252        DBStat {
253            bin: self.bin,
254            global_opts: self.global_opts,
255            mode: Scan {
256                show_histogram: false,
257                show_free_pages: true,
258                mode: Unselected,
259            },
260        }
261    }
262
263    /// # Description
264    ///
265    /// -a
266    ///
267    /// Display statistics for all tables.
268    ///
269    /// Transitions to the [`DisplayAllTables`] state, which spawns without
270    /// table arguments.
271    pub fn all(self) -> DBStat<DisplayAllTables> {
272        DBStat {
273            bin: self.bin,
274            global_opts: self.global_opts,
275            mode: DisplayAllTables,
276        }
277    }
278
279    /// Enters the checkpoint form with `-w worklevel` set:
280    /// `p4 dbstat -p -w worklevel {-a | dbtable ...}`.
281    ///
282    /// The checkpoint form is mutually exclusive with the scan form.
283    #[cfg(not(feature = "lt2023_1"))]
284    pub fn worklevel(self, v: u64) -> DBStat<CheckPoint<Unselected>> {
285        DBStat {
286            bin: self.bin,
287            global_opts: self.global_opts,
288            mode: CheckPoint {
289                worklevel: Some(v),
290                number_files: None,
291                mode: Unselected,
292            },
293        }
294    }
295
296    /// Enters the checkpoint form with `-n numberOfFiles` set:
297    /// `p4 dbstat -p -n numberOfFiles {-a | dbtable ...}`.
298    ///
299    /// The checkpoint form is mutually exclusive with the scan form.
300    #[cfg(not(feature = "lt2023_1"))]
301    pub fn number_files(self, v: u64) -> DBStat<CheckPoint<Unselected>> {
302        DBStat {
303            bin: self.bin,
304            global_opts: self.global_opts,
305            mode: CheckPoint {
306                worklevel: None,
307                number_files: Some(v),
308                mode: Unselected,
309            },
310        }
311    }
312
313    /// Enters the checkpoint form:
314    /// `p4 dbstat -p [-w worklevel] [-n numberOfFiles] {-a | dbtable ...}`.
315    ///
316    /// Use [`Self::worklevel`] or [`Self::number_files`] to enter the form
317    /// with `-w` or `-n` already set; use [`Self::all`] to supply `-a`,
318    /// otherwise spawn with a list of table names.
319    #[cfg(not(feature = "lt2023_1"))]
320    pub fn checkpoint(self) -> DBStat<CheckPoint<Unselected>> {
321        DBStat {
322            bin: self.bin,
323            global_opts: self.global_opts,
324            mode: CheckPoint {
325                worklevel: None,
326                number_files: None,
327                mode: Unselected,
328            },
329        }
330    }
331
332    /// Enters the size form: `p4 dbstat -s`.
333    pub fn size(self) -> DBStat<Size> {
334        DBStat {
335            bin: self.bin,
336            global_opts: self.global_opts,
337            mode: Size,
338        }
339    }
340}
341
342// ---------------------------------------------------------------------------
343// Transitions from the `-a` state
344// ---------------------------------------------------------------------------
345
346impl DBStat<DisplayAllTables> {
347    /// Enters the scan form with `-h` set and `-a` retained:
348    /// `p4 dbstat [-h][-f] -a`.
349    pub fn show_histogram(self) -> DBStat<Scan<DisplayAllTables>> {
350        DBStat {
351            bin: self.bin,
352            global_opts: self.global_opts,
353            mode: Scan {
354                show_histogram: true,
355                #[cfg(not(feature = "lt2016_1"))]
356                show_free_pages: false,
357                mode: DisplayAllTables,
358            },
359        }
360    }
361
362    /// Enters the scan form with `-f` set and `-a` retained:
363    /// `p4 dbstat [-h][-f] -a`.
364    #[cfg(not(feature = "lt2016_1"))]
365    pub fn show_free_pages(self) -> DBStat<Scan<DisplayAllTables>> {
366        DBStat {
367            bin: self.bin,
368            global_opts: self.global_opts,
369            mode: Scan {
370                show_histogram: false,
371                show_free_pages: true,
372                mode: DisplayAllTables,
373            },
374        }
375    }
376
377    /// Enters the checkpoint form with `-a` retained: `p4 dbstat -p -a`.
378    #[cfg(not(feature = "lt2023_1"))]
379    pub fn checkpoint(self) -> DBStat<CheckPoint<DisplayAllTables>> {
380        DBStat {
381            bin: self.bin,
382            global_opts: self.global_opts,
383            mode: CheckPoint {
384                worklevel: None,
385                number_files: None,
386                mode: DisplayAllTables,
387            },
388        }
389    }
390
391    /// Enters the checkpoint form with `-w worklevel` set and `-a` retained:
392    /// `p4 dbstat -p -w worklevel -a`.
393    #[cfg(not(feature = "lt2023_1"))]
394    pub fn worklevel(self, v: u64) -> DBStat<CheckPoint<DisplayAllTables>> {
395        DBStat {
396            bin: self.bin,
397            global_opts: self.global_opts,
398            mode: CheckPoint {
399                worklevel: Some(v),
400                number_files: None,
401                mode: DisplayAllTables,
402            },
403        }
404    }
405
406    /// Enters the checkpoint form with `-n numberOfFiles` set and `-a`
407    /// retained: `p4 dbstat -p -n numberOfFiles -a`.
408    #[cfg(not(feature = "lt2023_1"))]
409    pub fn number_files(self, v: u64) -> DBStat<CheckPoint<DisplayAllTables>> {
410        DBStat {
411            bin: self.bin,
412            global_opts: self.global_opts,
413            mode: CheckPoint {
414                worklevel: None,
415                number_files: Some(v),
416                mode: DisplayAllTables,
417            },
418        }
419    }
420}
421
422impl<M> DBStat<Scan<M>> {
423    /// # Description
424    ///
425    /// -h
426    ///
427    #[cfg_attr(
428        feature = "lt2024_1",
429        doc = "Display a histogram showing distances between leaf pages."
430    )]
431    #[cfg_attr(
432        not(feature = "lt2024_1"),
433        doc = "Display a histogram showing distances between leaf pages. This enables you to anticipate disk seeks required for sequential database scans. This scans only the internal node pages of the file, not the leaf or overflow pages, and thus reads only 1-5% of the file.",
434        doc = "",
435        doc = "> **Note**",
436        doc = ">",
437        doc = "> This option blocks write access to the database while it scans the tables. It is best to get guidance from [Perforce Support](https://www.perforce.com/support?product=Helix%20Core) before using this option."
438    )]
439    pub fn get_show_histogram(&self) -> bool {
440        self.mode.show_histogram
441    }
442
443    /// # Description
444    ///
445    /// -h
446    ///
447    #[cfg_attr(
448        feature = "lt2024_1",
449        doc = "Display a histogram showing distances between leaf pages."
450    )]
451    #[cfg_attr(
452        not(feature = "lt2024_1"),
453        doc = "Display a histogram showing distances between leaf pages. This enables you to anticipate disk seeks required for sequential database scans. This scans only the internal node pages of the file, not the leaf or overflow pages, and thus reads only 1-5% of the file.",
454        doc = "",
455        doc = "> **Note**",
456        doc = ">",
457        doc = "> This option blocks write access to the database while it scans the tables. It is best to get guidance from [Perforce Support](https://www.perforce.com/support?product=Helix%20Core) before using this option."
458    )]
459    pub fn set_show_histogram(&mut self, v: bool) -> &mut Self {
460        self.mode.show_histogram = v;
461        self
462    }
463
464    /// # Description
465    ///
466    /// -h
467    ///
468    #[cfg_attr(
469        feature = "lt2024_1",
470        doc = "Display a histogram showing distances between leaf pages."
471    )]
472    #[cfg_attr(
473        not(feature = "lt2024_1"),
474        doc = "Display a histogram showing distances between leaf pages. This enables you to anticipate disk seeks required for sequential database scans. This scans only the internal node pages of the file, not the leaf or overflow pages, and thus reads only 1-5% of the file.",
475        doc = "",
476        doc = "> **Note**",
477        doc = ">",
478        doc = "> This option blocks write access to the database while it scans the tables. It is best to get guidance from [Perforce Support](https://www.perforce.com/support?product=Helix%20Core) before using this option."
479    )]
480    pub fn show_histogram(mut self, v: bool) -> Self {
481        self.set_show_histogram(v);
482        self
483    }
484
485    /// # Description
486    ///
487    /// -f
488    ///
489    #[cfg_attr(
490        feature = "lt2024_1",
491        doc = "Displays a page count, free pages, and percent free data for the specified tables."
492    )]
493    #[cfg_attr(
494        not(feature = "lt2024_1"),
495        doc = "Displays a page count, free pages, and percent free data for the specified tables. Using the `-f` with the `-h` option, this command shows a histogram of free page distribution."
496    )]
497    #[cfg(not(feature = "lt2016_1"))]
498    pub fn get_show_free_pages(&self) -> bool {
499        self.mode.show_free_pages
500    }
501
502    /// # Description
503    ///
504    /// -f
505    ///
506    #[cfg_attr(
507        feature = "lt2024_1",
508        doc = "Displays a page count, free pages, and percent free data for the specified tables."
509    )]
510    #[cfg_attr(
511        not(feature = "lt2024_1"),
512        doc = "Displays a page count, free pages, and percent free data for the specified tables. Using the `-f` with the `-h` option, this command shows a histogram of free page distribution."
513    )]
514    #[cfg(not(feature = "lt2016_1"))]
515    pub fn set_show_free_pages(&mut self, v: bool) -> &mut Self {
516        self.mode.show_free_pages = v;
517        self
518    }
519
520    /// # Description
521    ///
522    /// -f
523    ///
524    #[cfg_attr(
525        feature = "lt2024_1",
526        doc = "Displays a page count, free pages, and percent free data for the specified tables."
527    )]
528    #[cfg_attr(
529        not(feature = "lt2024_1"),
530        doc = "Displays a page count, free pages, and percent free data for the specified tables. Using the `-f` with the `-h` option, this command shows a histogram of free page distribution."
531    )]
532    #[cfg(not(feature = "lt2016_1"))]
533    pub fn show_free_pages(mut self, v: bool) -> Self {
534        self.set_show_free_pages(v);
535        self
536    }
537}
538
539impl DBStat<Scan<Unselected>> {
540    /// # Description
541    ///
542    /// -a
543    ///
544    /// Display statistics for all tables.
545    ///
546    /// Transitions to the [`DisplayAllTables`] state, which spawns without
547    /// table arguments.
548    pub fn all(self) -> DBStat<Scan<DisplayAllTables>> {
549        DBStat {
550            bin: self.bin,
551            global_opts: self.global_opts,
552            mode: Scan {
553                show_histogram: self.mode.show_histogram,
554                #[cfg(not(feature = "lt2016_1"))]
555                show_free_pages: self.mode.show_free_pages,
556                mode: DisplayAllTables,
557            },
558        }
559    }
560}
561
562#[cfg(not(feature = "lt2023_1"))]
563impl<M> DBStat<CheckPoint<M>> {
564    /// # Description
565    ///
566    /// -w worklevel
567    ///
568    #[cfg_attr(
569        feature = "lt2024_1",
570        doc = "Overrides the [db.checkpoint.worklevel](https://help.perforce.com/helix-core/server-apps/cmdref/current/Content/CmdRef/configurables.alphabetical.html#db.checkpoint.worklevel) configurable so you can specify the `worklevel` at the command line."
571    )]
572    #[cfg_attr(
573        not(feature = "lt2024_1"),
574        doc = "The `worklevel` determines which level of the table should be used to split the table. This option overrides the [db.checkpoint.worklevel](https://help.perforce.com/helix-core/server-apps/cmdref/current/Content/CmdRef/configurables.alphabetical.html#db.checkpoint.worklevel) configurable so you can specify the `worklevel` at the command line."
575    )]
576    pub fn get_worklevel(&self) -> Option<u64> {
577        self.mode.worklevel
578    }
579
580    /// # Description
581    ///
582    /// -w worklevel
583    ///
584    #[cfg_attr(
585        feature = "lt2024_1",
586        doc = "Overrides the [db.checkpoint.worklevel](https://help.perforce.com/helix-core/server-apps/cmdref/current/Content/CmdRef/configurables.alphabetical.html#db.checkpoint.worklevel) configurable so you can specify the `worklevel` at the command line."
587    )]
588    #[cfg_attr(
589        not(feature = "lt2024_1"),
590        doc = "The `worklevel` determines which level of the table should be used to split the table. This option overrides the [db.checkpoint.worklevel](https://help.perforce.com/helix-core/server-apps/cmdref/current/Content/CmdRef/configurables.alphabetical.html#db.checkpoint.worklevel) configurable so you can specify the `worklevel` at the command line."
591    )]
592    pub fn set_worklevel(&mut self, v: u64) -> &mut Self {
593        self.mode.worklevel = Some(v);
594        self
595    }
596
597    /// # Description
598    ///
599    /// -w worklevel
600    ///
601    #[cfg_attr(
602        feature = "lt2024_1",
603        doc = "Overrides the [db.checkpoint.worklevel](https://help.perforce.com/helix-core/server-apps/cmdref/current/Content/CmdRef/configurables.alphabetical.html#db.checkpoint.worklevel) configurable so you can specify the `worklevel` at the command line."
604    )]
605    #[cfg_attr(
606        not(feature = "lt2024_1"),
607        doc = "The `worklevel` determines which level of the table should be used to split the table. This option overrides the [db.checkpoint.worklevel](https://help.perforce.com/helix-core/server-apps/cmdref/current/Content/CmdRef/configurables.alphabetical.html#db.checkpoint.worklevel) configurable so you can specify the `worklevel` at the command line."
608    )]
609    pub fn worklevel(mut self, v: u64) -> Self {
610        self.set_worklevel(v);
611        self
612    }
613
614    /// # Description
615    ///
616    /// -n numberOfFiles
617    ///
618    #[cfg_attr(
619        feature = "lt2024_1",
620        doc = "Overrides the [db.checkpoint.numfiles](https://help.perforce.com/helix-core/server-apps/cmdref/current/Content/CmdRef/configurables.alphabetical.html#db.checkpoint.numfiles) configurable so you can specify `numberOfFiles` at the command line."
621    )]
622    #[cfg_attr(
623        not(feature = "lt2024_1"),
624        doc = "This option determines, along with the depth of the database table, the number of checkpoint files that would be generated. A large table might contain multiple pages. The `-n numberOfFiles` requested makes it possible divide the page count to split each chunk of work into similar sizes. This option overrides the [db.checkpoint.numfiles](https://help.perforce.com/helix-core/server-apps/cmdref/current/Content/CmdRef/configurables.alphabetical.html#db.checkpoint.numfiles) configurable so you can specify `numberOfFiles` at the command line."
625    )]
626    pub fn get_number_files(&self) -> Option<u64> {
627        self.mode.number_files
628    }
629
630    /// # Description
631    ///
632    /// -n numberOfFiles
633    ///
634    #[cfg_attr(
635        feature = "lt2024_1",
636        doc = "Overrides the [db.checkpoint.numfiles](https://help.perforce.com/helix-core/server-apps/cmdref/current/Content/CmdRef/configurables.alphabetical.html#db.checkpoint.numfiles) configurable so you can specify `numberOfFiles` at the command line."
637    )]
638    #[cfg_attr(
639        not(feature = "lt2024_1"),
640        doc = "This option determines, along with the depth of the database table, the number of checkpoint files that would be generated. A large table might contain multiple pages. The `-n numberOfFiles` requested makes it possible divide the page count to split each chunk of work into similar sizes. This option overrides the [db.checkpoint.numfiles](https://help.perforce.com/helix-core/server-apps/cmdref/current/Content/CmdRef/configurables.alphabetical.html#db.checkpoint.numfiles) configurable so you can specify `numberOfFiles` at the command line."
641    )]
642    pub fn set_number_files(&mut self, v: u64) -> &mut Self {
643        self.mode.number_files = Some(v);
644        self
645    }
646
647    /// # Description
648    ///
649    /// -n numberOfFiles
650    ///
651    #[cfg_attr(
652        feature = "lt2024_1",
653        doc = "Overrides the [db.checkpoint.numfiles](https://help.perforce.com/helix-core/server-apps/cmdref/current/Content/CmdRef/configurables.alphabetical.html#db.checkpoint.numfiles) configurable so you can specify `numberOfFiles` at the command line."
654    )]
655    #[cfg_attr(
656        not(feature = "lt2024_1"),
657        doc = "This option determines, along with the depth of the database table, the number of checkpoint files that would be generated. A large table might contain multiple pages. The `-n numberOfFiles` requested makes it possible divide the page count to split each chunk of work into similar sizes. This option overrides the [db.checkpoint.numfiles](https://help.perforce.com/helix-core/server-apps/cmdref/current/Content/CmdRef/configurables.alphabetical.html#db.checkpoint.numfiles) configurable so you can specify `numberOfFiles` at the command line."
658    )]
659    pub fn number_files(mut self, v: u64) -> Self {
660        self.set_number_files(v);
661        self
662    }
663}
664
665#[cfg(not(feature = "lt2023_1"))]
666impl DBStat<CheckPoint<Unselected>> {
667    /// # Description
668    ///
669    /// -a
670    ///
671    /// Display statistics for all tables.
672    ///
673    /// Transitions to the [`DisplayAllTables`] state, which spawns without
674    /// table arguments.
675    pub fn all(self) -> DBStat<CheckPoint<DisplayAllTables>> {
676        DBStat {
677            bin: self.bin,
678            global_opts: self.global_opts,
679            mode: CheckPoint {
680                worklevel: self.mode.worklevel,
681                number_files: self.mode.number_files,
682                mode: DisplayAllTables,
683            },
684        }
685    }
686}
687
688// ---------------------------------------------------------------------------
689// Accessors and command assembly
690// ---------------------------------------------------------------------------
691
692impl<M> DBStat<M> {
693    /// Get the Perforce command path.
694    pub fn get_bin(&self) -> &Path {
695        &self.bin
696    }
697
698    /// Set the Perforce command path.
699    pub fn set_bin(&mut self, bin: impl Into<PathBuf>) -> &mut Self {
700        self.bin = bin.into();
701        self
702    }
703
704    /// Set the Perforce command path.
705    pub fn bin(mut self, bin: impl Into<PathBuf>) -> Self {
706        self.set_bin(bin);
707        self
708    }
709
710    /// # Description
711    ///
712    /// g-opts
713    ///
714    #[cfg_attr(
715        feature = "lt2014_2",
716        doc = "See the [Global Options](GlobalOpts) section."
717    )]
718    #[cfg_attr(
719        all(feature = "lt2015_1", not(feature = "lt2014_2")),
720        doc = "See the [“Global Options”](GlobalOpts) section."
721    )]
722    #[cfg_attr(
723        all(feature = "lt2017_1", not(feature = "lt2015_1")),
724        doc = "See [“Global Options”](GlobalOpts)."
725    )]
726    #[cfg_attr(
727        all(feature = "lt2018_2", not(feature = "lt2017_1")),
728        doc = "See [Global Options](GlobalOpts)."
729    )]
730    #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
731    pub fn get_global_opts(&self) -> &GlobalOpts {
732        &self.global_opts
733    }
734
735    /// # Description
736    ///
737    /// g-opts
738    ///
739    #[cfg_attr(
740        feature = "lt2014_2",
741        doc = "See the [Global Options](GlobalOpts) section."
742    )]
743    #[cfg_attr(
744        all(feature = "lt2015_1", not(feature = "lt2014_2")),
745        doc = "See the [“Global Options”](GlobalOpts) section."
746    )]
747    #[cfg_attr(
748        all(feature = "lt2017_1", not(feature = "lt2015_1")),
749        doc = "See [“Global Options”](GlobalOpts)."
750    )]
751    #[cfg_attr(
752        all(feature = "lt2018_2", not(feature = "lt2017_1")),
753        doc = "See [Global Options](GlobalOpts)."
754    )]
755    #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
756    pub fn set_global_opts(&mut self, v: GlobalOpts) -> &mut Self {
757        self.global_opts = v;
758        self
759    }
760
761    /// # Description
762    ///
763    /// g-opts
764    ///
765    #[cfg_attr(
766        feature = "lt2014_2",
767        doc = "See the [Global Options](GlobalOpts) section."
768    )]
769    #[cfg_attr(
770        all(feature = "lt2015_1", not(feature = "lt2014_2")),
771        doc = "See the [“Global Options”](GlobalOpts) section."
772    )]
773    #[cfg_attr(
774        all(feature = "lt2017_1", not(feature = "lt2015_1")),
775        doc = "See [“Global Options”](GlobalOpts)."
776    )]
777    #[cfg_attr(
778        all(feature = "lt2018_2", not(feature = "lt2017_1")),
779        doc = "See [Global Options](GlobalOpts)."
780    )]
781    #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
782    pub fn global_opts(mut self, v: GlobalOpts) -> Self {
783        self.global_opts = v;
784        self
785    }
786}
787
788impl<M> SubCommand for DBStat<M>
789where
790    M: ExclusiveOption,
791{
792    fn name(&self) -> &str {
793        "dbstat"
794    }
795
796    fn inject_local_args(&self, command: &mut Command) {
797        self.mode.inject_args(command);
798    }
799
800    fn global_opts(&self) -> Option<&GlobalOpts> {
801        Some(&self.global_opts)
802    }
803}
804
805impl<S, I> ParameterizedSpawn<(S,)> for DBStat<Unselected>
806where
807    S: IntoIterator<Item = I>,
808    I: AsRef<OsStr>,
809{
810    type Output = Child;
811    type Error = std::io::Error;
812
813    /// Spawns `p4 dbstat` for the given tables as a child process with piped
814    /// standard output and error streams; use the returned [`Child`] handle to
815    /// wait for it or interact with it.
816    fn spawn_with(&mut self, (tables,): (S,)) -> Result<Self::Output, Self::Error> {
817        self.setup_command(&self.bin)
818            .args(tables)
819            .stdout(Stdio::piped())
820            .stderr(Stdio::piped())
821            .spawn()
822    }
823}
824
825impl ParameterizedSpawn<()> for DBStat<DisplayAllTables> {
826    type Output = Child;
827    type Error = std::io::Error;
828
829    /// Spawns `p4 dbstat -a` as a child process with piped standard output
830    /// and error streams; use the returned [`Child`] handle to wait for it or
831    /// interact with it.
832    fn spawn_with(&mut self, (): ()) -> Result<Self::Output, Self::Error> {
833        self.setup_command(&self.bin)
834            .stdout(Stdio::piped())
835            .stderr(Stdio::piped())
836            .spawn()
837    }
838}
839
840impl ParameterizedSpawn<()> for DBStat<Scan<DisplayAllTables>> {
841    type Output = Child;
842    type Error = std::io::Error;
843
844    /// Spawns `p4 dbstat -a` as a child process with piped standard output
845    /// and error streams; use the returned [`Child`] handle to wait for it or
846    /// interact with it.
847    fn spawn_with(&mut self, (): ()) -> Result<Self::Output, Self::Error> {
848        self.setup_command(&self.bin)
849            .stdout(Stdio::piped())
850            .stderr(Stdio::piped())
851            .spawn()
852    }
853}
854
855impl<S, I> ParameterizedSpawn<(S,)> for DBStat<Scan<Unselected>>
856where
857    S: IntoIterator<Item = I>,
858    I: AsRef<OsStr>,
859{
860    type Output = Child;
861    type Error = std::io::Error;
862
863    /// Spawns `p4 dbstat` for the given tables as a child process with piped
864    /// standard output and error streams; use the returned [`Child`] handle
865    /// to wait for it or interact with it.
866    fn spawn_with(&mut self, (tables,): (S,)) -> Result<Self::Output, Self::Error> {
867        self.setup_command(&self.bin)
868            .args(tables)
869            .stdout(Stdio::piped())
870            .stderr(Stdio::piped())
871            .spawn()
872    }
873}
874
875#[cfg(not(feature = "lt2023_1"))]
876impl ParameterizedSpawn<()> for DBStat<CheckPoint<DisplayAllTables>> {
877    type Output = Child;
878    type Error = std::io::Error;
879
880    /// Spawns `p4 dbstat -p -a` as a child process with piped standard output
881    /// and error streams; use the returned [`Child`] handle to wait for it or
882    /// interact with it.
883    fn spawn_with(&mut self, (): ()) -> Result<Self::Output, Self::Error> {
884        self.setup_command(&self.bin)
885            .stdout(Stdio::piped())
886            .stderr(Stdio::piped())
887            .spawn()
888    }
889}
890
891#[cfg(not(feature = "lt2023_1"))]
892impl<S, I> ParameterizedSpawn<(S,)> for DBStat<CheckPoint<Unselected>>
893where
894    S: IntoIterator<Item = I>,
895    I: AsRef<OsStr>,
896{
897    type Output = Child;
898    type Error = std::io::Error;
899
900    /// Spawns `p4 dbstat -p` for the given tables as a child process with
901    /// piped standard output and error streams; use the returned [`Child`]
902    /// handle to wait for it or interact with it.
903    fn spawn_with(&mut self, (tables,): (S,)) -> Result<Self::Output, Self::Error> {
904        self.setup_command(&self.bin)
905            .args(tables)
906            .stdout(Stdio::piped())
907            .stderr(Stdio::piped())
908            .spawn()
909    }
910}
911
912impl ParameterizedSpawn<()> for DBStat<Size> {
913    type Output = Child;
914    type Error = std::io::Error;
915
916    /// Spawns `p4 dbstat -s` as a child process with piped standard output
917    /// and error streams; use the returned [`Child`] handle to wait for it or
918    /// interact with it.
919    fn spawn_with(&mut self, (): ()) -> Result<Self::Output, Self::Error> {
920        self.setup_command(&self.bin)
921            .stdout(Stdio::piped())
922            .stderr(Stdio::piped())
923            .spawn()
924    }
925}
926
927#[cfg(test)]
928mod tests {
929    use super::*;
930    use crate::cmd::args_of;
931
932    #[test]
933    fn bin_accessors_update_executable_path() {
934        let mut command = DBStat::new("p4", GlobalOpts::new()).show_histogram();
935        assert_eq!(command.get_bin(), Path::new("p4"));
936
937        command.set_bin("/usr/local/bin/p4");
938        assert_eq!(command.get_bin(), Path::new("/usr/local/bin/p4"));
939
940        let command = command.bin("p4.exe");
941        assert_eq!(command.get_bin(), Path::new("p4.exe"));
942    }
943
944    /// Dry-run checks of the assembled `p4 dbstat` command line; no process
945    /// is spawned.
946    #[test]
947    fn plain_form_without_options() {
948        let dbstat = DBStat::new("p4", GlobalOpts::new());
949
950        assert_eq!(args_of(&dbstat.setup_command("p4")), ["dbstat"]);
951    }
952
953    #[test]
954    fn plain_form_all() {
955        let dbstat = DBStat::new("p4", GlobalOpts::new()).all();
956
957        assert_eq!(args_of(&dbstat.setup_command("p4")), ["dbstat", "-a"]);
958    }
959
960    #[test]
961    fn show_histogram_enters_scan_form() {
962        let dbstat = DBStat::new("p4", GlobalOpts::new()).show_histogram();
963
964        assert!(dbstat.get_show_histogram());
965        assert_eq!(args_of(&dbstat.setup_command("p4")), ["dbstat", "-h"]);
966    }
967
968    #[cfg(not(feature = "lt2016_1"))]
969    #[test]
970    fn show_free_pages_enters_scan_form() {
971        let dbstat = DBStat::new("p4", GlobalOpts::new()).show_free_pages();
972
973        assert!(dbstat.get_show_free_pages());
974        assert_eq!(args_of(&dbstat.setup_command("p4")), ["dbstat", "-f"]);
975    }
976
977    #[cfg(not(feature = "lt2016_1"))]
978    #[test]
979    fn scan_form_with_histogram_and_free_pages() {
980        let dbstat = DBStat::new("p4", GlobalOpts::new())
981            .show_histogram()
982            .show_free_pages(true);
983
984        assert!(dbstat.get_show_histogram());
985        assert!(dbstat.get_show_free_pages());
986        assert_eq!(args_of(&dbstat.setup_command("p4")), ["dbstat", "-h", "-f"]);
987    }
988
989    #[test]
990    fn scan_form_all() {
991        let dbstat = DBStat::new("p4", GlobalOpts::new()).show_histogram().all();
992
993        assert_eq!(args_of(&dbstat.setup_command("p4")), ["dbstat", "-h", "-a"]);
994    }
995
996    #[test]
997    fn size_form() {
998        let dbstat = DBStat::new("p4", GlobalOpts::new()).size();
999
1000        assert_eq!(args_of(&dbstat.setup_command("p4")), ["dbstat", "-s"]);
1001    }
1002
1003    #[cfg(not(feature = "lt2023_1"))]
1004    #[test]
1005    fn checkpoint_form_with_worklevel_and_number_files() {
1006        let mut dbstat = DBStat::new("p4", GlobalOpts::new()).checkpoint();
1007        dbstat.set_worklevel(2);
1008        let dbstat = dbstat.number_files(3);
1009
1010        assert_eq!(dbstat.get_worklevel(), Some(2));
1011        assert_eq!(dbstat.get_number_files(), Some(3));
1012        assert_eq!(
1013            args_of(&dbstat.setup_command("p4")),
1014            ["dbstat", "-p", "-w", "2", "-n", "3"]
1015        );
1016    }
1017
1018    #[cfg(not(feature = "lt2023_1"))]
1019    #[test]
1020    fn checkpoint_form_without_options() {
1021        let dbstat = DBStat::new("p4", GlobalOpts::new()).checkpoint();
1022
1023        assert_eq!(dbstat.get_worklevel(), None);
1024        assert_eq!(dbstat.get_number_files(), None);
1025        assert_eq!(args_of(&dbstat.setup_command("p4")), ["dbstat", "-p"]);
1026    }
1027
1028    #[cfg(not(feature = "lt2023_1"))]
1029    #[test]
1030    fn checkpoint_form_all_keeps_options() {
1031        let dbstat = DBStat::new("p4", GlobalOpts::new())
1032            .checkpoint()
1033            .worklevel(2)
1034            .all();
1035
1036        assert_eq!(
1037            args_of(&dbstat.setup_command("p4")),
1038            ["dbstat", "-p", "-w", "2", "-a"]
1039        );
1040    }
1041
1042    #[test]
1043    fn global_opts_are_injected_once() {
1044        let dbstat = DBStat::new(
1045            "p4",
1046            GlobalOpts::new().port("localhost:1666").quiet_mode(true),
1047        )
1048        .show_histogram()
1049        .all();
1050
1051        assert_eq!(
1052            args_of(&dbstat.setup_command("p4")),
1053            ["-p", "localhost:1666", "-q", "dbstat", "-h", "-a"]
1054        );
1055    }
1056}