Panda3D
Loading...
Searching...
No Matches
bamCacheIndex.cxx
Go to the documentation of this file.
1/**
2 * PANDA 3D SOFTWARE
3 * Copyright (c) Carnegie Mellon University. All rights reserved.
4 *
5 * All use of this software is subject to the terms of the revised BSD
6 * license. You should have received a copy of this license along
7 * with this source code in a file named "LICENSE."
8 *
9 * @file bamCacheIndex.cxx
10 * @author drose
11 * @date 2006-06-19
12 */
13
14#include "bamCacheIndex.h"
15#include "bamReader.h"
16#include "bamWriter.h"
17#include "config_putil.h" // util_cat
18#include "indent.h"
19#include <algorithm>
20
21TypeHandle BamCacheIndex::_type_handle;
22
23
24/**
25 *
26 */
27BamCacheIndex::
28~BamCacheIndex() {
29#ifndef NDEBUG
30 // We need to "empty" the linked list to make the LinkedListNode destructors
31 // happy.
32 release_records();
33#endif
34}
35
36/**
37 *
38 */
39void BamCacheIndex::
40write(std::ostream &out, int indent_level) const {
41 indent(out, indent_level)
42 << "BamCacheIndex, " << _records.size() << " records:\n";
43
44 Records::const_iterator ri;
45 for (ri = _records.begin(); ri != _records.end(); ++ri) {
46 BamCacheRecord *record = (*ri).second;
47 indent(out, indent_level + 2)
48 << std::setw(10) << record->_record_size << " "
49 << record->get_cache_filename() << " "
50 << record->get_source_pathname() << "\n";
51 }
52 out << "\n";
53 indent(out, indent_level)
54 << std::setw(12) << _cache_size << " bytes total\n";
55}
56
57/**
58 * Should be called after the _records index has been filled externally, this
59 * will sort the records by access time and calculate _cache_size.
60 */
61void BamCacheIndex::
62process_new_records() {
63 nassertv(_cache_size == 0);
64
65 // Fill up a vector so we can sort the records into order by access time.
66 RecordVector rv;
67 rv.reserve(_records.size());
68
69 Records::const_iterator ri;
70 for (ri = _records.begin(); ri != _records.end(); ++ri) {
71 BamCacheRecord *record = (*ri).second;
72 _cache_size += record->_record_size;
73 rv.push_back(record);
74 }
75
76 sort(rv.begin(), rv.end(), BamCacheRecord::SortByAccessTime());
77
78 // Now put them into the linked list.
79 RecordVector::const_iterator rvi;
80 for (rvi = rv.begin(); rvi != rv.end(); ++rvi) {
81 BamCacheRecord *record = *rvi;
82 record->insert_before(this);
83 }
84}
85
86/**
87 * This is the inverse of process_new_records: it releases the records from
88 * the linked list, so that they may be added to another index or whatever.
89 * Calling this, of course, invalidates the index until process_new_records()
90 * is called again.
91 */
92void BamCacheIndex::
93release_records() {
94 Records::const_iterator ri;
95 for (ri = _records.begin(); ri != _records.end(); ++ri) {
96 BamCacheRecord *record = (*ri).second;
97 record->_next = nullptr;
98 record->_prev = nullptr;
99 }
100 _next = this;
101 _prev = this;
102 _cache_size = 0;
103}
104
105/**
106 * Evicts an old file from the cache. Records the record. Returns NULL if
107 * the cache is empty.
108 */
109PT(BamCacheRecord) BamCacheIndex::
110evict_old_file() {
111 if (_next == this) {
112 // Nothing in the cache.
113 return nullptr;
114 }
115
116 // The first record in the linked list is the least-recently-used one.
117 PT(BamCacheRecord) record = (BamCacheRecord *)_next;
118 bool removed = remove_record(record->get_source_pathname());
119 nassertr(removed, nullptr);
120
121 return record;
122}
123
124/**
125 * Adds a newly-created BamCacheRecord into the index. If a matching record
126 * is already in the index, it is replaced with the new record. Returns true
127 * if the record was added, or false if the equivalent record was already
128 * there and the index is unchanged.
129 */
130bool BamCacheIndex::
131add_record(BamCacheRecord *record) {
132 std::pair<Records::iterator, bool> result =
133 _records.insert(Records::value_type(record->get_source_pathname(), record));
134 if (!result.second) {
135 // We already had a record for this filename; it gets replaced.
136 BamCacheRecord *orig_record = (*result.first).second;
137 orig_record->remove_from_list();
138 if (*orig_record == *record) {
139 // Well, never mind. The record hasn't changed.
140 orig_record->insert_before(this);
141 return false;
142 }
143
144 _cache_size -= orig_record->_record_size;
145 (*result.first).second = record;
146 }
147 record->insert_before(this);
148
149 _cache_size += record->_record_size;
150 return true;
151}
152
153/**
154 * Searches for the matching record in the index and removes it if it is
155 * found. Returns true if the record was found and removed, or false if there
156 * was no such record and the index is unchanged.
157 */
158bool BamCacheIndex::
159remove_record(const Filename &source_pathname) {
160 Records::iterator ri = _records.find(source_pathname);
161 if (ri == _records.end()) {
162 // No entry for this record; no problem.
163 return false;
164 }
165
166 BamCacheRecord *record = (*ri).second;
167 record->remove_from_list();
168 _cache_size -= record->_record_size;
169 _records.erase(ri);
170 return true;
171}
172
173/**
174 * Tells the BamReader how to create objects of type BamCacheRecord.
175 */
178 BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
179}
180
181/**
182 * Writes the contents of this object to the datagram for shipping out to a
183 * Bam file.
184 */
186write_datagram(BamWriter *manager, Datagram &dg) {
188
189 dg.add_uint32(_records.size());
190 Records::const_iterator ri;
191 for (ri = _records.begin(); ri != _records.end(); ++ri) {
192 manager->write_pointer(dg, (*ri).second);
193 }
194}
195
196/**
197 * This function is called by the BamReader's factory when a new object of
198 * type BamCacheIndex is encountered in the Bam file. It should create the
199 * BamCacheIndex and extract its information from the file.
200 */
201TypedWritable *BamCacheIndex::
202make_from_bam(const FactoryParams &params) {
203 BamCacheIndex *object = new BamCacheIndex;
204 DatagramIterator scan;
205 BamReader *manager;
206
207 parse_params(params, scan, manager);
208 object->fillin(scan, manager);
209
210 return object;
211}
212
213/**
214 * Receives an array of pointers, one for each time manager->read_pointer()
215 * was called in fillin(). Returns the number of pointers processed.
216 */
217int BamCacheIndex::
218complete_pointers(TypedWritable **p_list, BamReader *manager) {
219 int pi = TypedWritable::complete_pointers(p_list, manager);
220
221 RecordVector::iterator vi;
222 for (vi = _record_vector.begin(); vi != _record_vector.end(); ++vi) {
223 PT(BamCacheRecord) record = DCAST(BamCacheRecord, p_list[pi++]);
224 (*vi) = record;
225
226 bool inserted = _records.insert(Records::value_type(record->get_source_pathname(), record)).second;
227 if (!inserted) {
228 util_cat.info()
229 << "Multiple cache files defining " << record->get_source_pathname()
230 << " in index.\n";
231 }
232 }
233
234 _record_vector.clear();
235
236 process_new_records();
237
238 return pi;
239}
240
241/**
242 * This internal function is called by make_from_bam to read in all of the
243 * relevant data from the BamFile for the new BamCacheIndex.
244 */
245void BamCacheIndex::
246fillin(DatagramIterator &scan, BamReader *manager) {
247 TypedWritable::fillin(scan, manager);
248
249 int num_records = scan.get_uint32();
250 _record_vector.reserve(num_records);
251 for (int i = 0; i < num_records; ++i) {
252 _record_vector.push_back(nullptr);
253 manager->read_pointer(scan);
254 }
255}
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void parse_params(const FactoryParams &params, DatagramIterator &scan, BamReader *&manager)
Takes in a FactoryParams, passed from a WritableFactory into any TypedWritable's make function,...
Definition bamReader.I:275
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This represents the in-memory index that records the list of files stored in the BamCache.
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
static void register_with_read_factory()
Tells the BamReader how to create objects of type BamCacheRecord.
An instance of this class is written to the front of a Bam or Txo file to make the file a cached inst...
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition bamReader.h:110
bool read_pointer(DatagramIterator &scan)
The interface for reading a pointer to another object from a Bam file.
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition bamReader.I:177
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition bamWriter.h:63
void write_pointer(Datagram &packet, const TypedWritable *dest)
The interface for writing a pointer to another object to a Bam file.
A class to retrieve the individual data elements previously stored in a Datagram.
uint32_t get_uint32()
Extracts an unsigned 32-bit integer.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition datagram.h:38
void add_uint32(uint32_t value)
Adds an unsigned 32-bit integer to the datagram.
Definition datagram.I:94
An instance of this class is passed to the Factory when requesting it to do its business and construc...
void register_factory(TypeHandle handle, CreateFunc *func, void *user_data=nullptr)
Registers a new kind of thing the Factory will be able to create.
Definition factory.I:73
The name of a file, such as a texture file or an Egg file.
Definition filename.h:44
TypeHandle is the identifier used to differentiate C++ class types.
Definition typeHandle.h:81
Base class for objects that can be written to and read from Bam files.
virtual void fillin(DatagramIterator &scan, BamReader *manager)
This internal function is intended to be called by each class's make_from_bam() method to read in all...
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
virtual int complete_pointers(TypedWritable **p_list, BamReader *manager)
Receives an array of pointers, one for each time manager->read_pointer() was called in fillin().
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
std::ostream & indent(std::ostream &out, int indent_level)
A handy function for doing text formatting.
Definition indent.cxx:20
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.