#!/usr/bin/env bash
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
#
# A shell script to compact DB generated by generate_random_db.sh.
# ./ldb needs to be available to be executed.
#
# Usage: <SCRIPT> <DB Path> [if_try_load_options] [if_ignore_unknown_options]

if [ "$#" -lt 1 ]; then
  echo "usage: $BASH_SOURCE <db_directory> [if_try_load_options] [if_ignore_unknown_options]"
  exit 1
fi

db_dir=$1
try_load_options=${2:-"1"}
ignore_unknown_options=${3:-"0"}
extra_params=

if [ "$try_load_options" = "0" ]; then
  extra_params=" --try_load_options=false"
elif [ "$try_load_options" = "1" ]; then
  extra_params=" --try_load_options=true"
fi

if [ "$ignore_unknown_options" = "1" ]; then
  extra_params="$extra_params --ignore_unknown_options"
fi

set -e
echo == Compacting DB at $db_dir
./ldb compact --db=$db_dir $extra_params
