File size: 5,129 Bytes
5024339
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/env python3
"""
Cache LMDB keys for fast dataset initialization.

Reads all keys from sharded LMDB and saves them as pickle files
for fast loading during dataset initialization.

Usage:
    python -m scripts.dataset_preparation.step4b_cache_keys
    python -m scripts.dataset_preparation.step4b_cache_keys --data-root data/liveworld/frame33_fps16_2000

LMDB directory is automatically derived: {data_root}_lmdb
"""
from __future__ import annotations

import lmdb
import pickle
from pathlib import Path
from tqdm import tqdm


def get_lmdb_root_from_data_root(data_root: str) -> str:
    """
    Derive LMDB path from data root.
    e.g., data/liveworld/frame33_fps16_2000 -> data/liveworld/frame33_fps16_2000_lmdb
    """
    data_root = data_root.rstrip("/")
    return f"{data_root}_lmdb"


def cache_lmdb_keys(lmdb_dir: str, force_rebuild: bool = False) -> None:
    """
    Cache all keys from LMDB to disk.

    Args:
        lmdb_dir: Directory containing LMDB shards
        force_rebuild: If True, rebuild cache even if it exists
    """
    lmdb_path = Path(lmdb_dir)

    if not lmdb_path.exists():
        raise FileNotFoundError(f"LMDB directory not found: {lmdb_path}")

    # Detect if sharded or single LMDB
    is_sharded = not (lmdb_path / "data.mdb").exists()

    if is_sharded:
        print("Detected sharded LMDB format")
        cache_sharded_keys(lmdb_path, force_rebuild)
    else:
        print("Detected single LMDB format")
        cache_single_keys(lmdb_path, force_rebuild)


def cache_single_keys(lmdb_path: Path, force_rebuild: bool = False) -> None:
    """Cache keys for single LMDB."""
    cache_file = lmdb_path / "keys_cache.pkl"

    if cache_file.exists() and not force_rebuild:
        print(f"Keys cache already exists: {cache_file}")
        print("Use --force to regenerate")
        return

    print(f"Caching keys from: {lmdb_path}")

    env = lmdb.open(
        str(lmdb_path),
        readonly=True,
        lock=False,
        readahead=True,
        meminit=False,
        max_readers=2048,
    )

    with env.begin(write=False) as txn:
        print("Reading all keys...")
        keys = [key for key, _ in tqdm(txn.cursor(), desc="Scanning keys")]

    env.close()

    print(f"Saving {len(keys)} keys to cache...")
    with open(cache_file, "wb") as f:
        pickle.dump(keys, f, protocol=pickle.HIGHEST_PROTOCOL)

    print(f"  Keys cache saved: {cache_file}")
    print(f"  Total keys: {len(keys)}")


def cache_sharded_keys(lmdb_root: Path, force_rebuild: bool = False) -> None:
    """Cache keys for sharded LMDB."""
    cache_file = lmdb_root / "sharded_keys_cache.pkl"

    if cache_file.exists() and not force_rebuild:
        print(f"Keys cache already exists: {cache_file}")
        print("Use --force to regenerate")
        return

    # Find all shards
    shard_dirs = sorted([
        d for d in lmdb_root.iterdir()
        if d.is_dir() and d.name.startswith("shard_") and d.name.endswith(".lmdb")
    ])

    if not shard_dirs:
        raise ValueError(f"No shards found in {lmdb_root}")

    print(f"Found {len(shard_dirs)} shards")

    # Read keys from each shard
    all_shard_keys = []
    shard_offsets = [0]
    total_keys = 0

    for shard_dir in tqdm(shard_dirs, desc="Processing shards"):
        env = lmdb.open(
            str(shard_dir),
            readonly=True,
            lock=False,
            readahead=True,
            meminit=False,
            max_readers=2048,
        )

        with env.begin(write=False) as txn:
            keys = [key for key, _ in txn.cursor()]

        env.close()

        all_shard_keys.append(keys)
        total_keys += len(keys)
        shard_offsets.append(total_keys)

        print(f"  {shard_dir.name}: {len(keys)} keys")

    # Save cache
    cache_data = {
        "shard_keys": all_shard_keys,
        "shard_offsets": shard_offsets,
        "shard_paths": [str(d) for d in shard_dirs],
        "total_keys": total_keys,
    }

    print(f"\nSaving cache for {total_keys} total keys...")
    with open(cache_file, "wb") as f:
        pickle.dump(cache_data, f, protocol=pickle.HIGHEST_PROTOCOL)

    print(f"  Sharded keys cache saved: {cache_file}")
    print(f"  Total shards: {len(shard_dirs)}")
    print(f"  Total keys: {total_keys}")


def main():
    from omegaconf import OmegaConf
    from pathlib import Path
    _PROJECT_ROOT = Path(__file__).resolve().parent.parent.parent
    cfg = OmegaConf.load(str(_PROJECT_ROOT / "configs" / "data_preparation.yaml"))

    data_root = cfg.get("lmdb_data_root", None) or cfg.output_root
    lmdb_dir = get_lmdb_root_from_data_root(data_root)
    force_rebuild = cfg.get("lmdb_force_cache", True)

    print("=" * 60)
    print("LMDB Keys Cache Generator")
    print("=" * 60)
    print(f"\nLMDB directory: {lmdb_dir}")
    print("-" * 60)

    try:
        cache_lmdb_keys(lmdb_dir, force_rebuild=force_rebuild)
        print("\n" + "=" * 60)
        print("  Keys cache generation complete!")
        print("=" * 60)
    except Exception as e:
        print(f"\n  Error: {e}")
        raise


if __name__ == "__main__":
    main()