1 
2 /* Web Polygraph       http://www.web-polygraph.org/
3  * Copyright 2003-2011 The Measurement Factory
4  * Licensed under the Apache License, Version 2.0 */
5 
6 #include "base/polygraph.h"
7 
8 #include "xstd/h/iostream.h"
9 #include "xstd/h/sstream.h"
10 
11 #include "xstd/gadgets.h"
12 #include "base/BStream.h"
13 #include "base/AggrStat.h"
14 #include "runtime/IOBuf.h"
15 #include "csm/ContentDbase.h"
16 #include "csm/cdbEntries.h"
17 
18 
19 /* ContentDbase */
20 
ContentDbase()21 ContentDbase::ContentDbase() {
22 }
23 
~ContentDbase()24 ContentDbase::~ContentDbase() {
25 	while (theEntries.count()) delete theEntries.pop();
26 }
27 
count() const28 int ContentDbase::count() const {
29 	return theEntries.count();
30 }
31 
hasLinkOrPage() const32 bool ContentDbase::hasLinkOrPage() const {
33 	for (int i = 0; i < count(); ++i) {
34 		const int eType = entry(i)->type();
35 		if (eType == cdbeLink || eType == cdbePage)
36 			return true;
37 	}
38 	return false;
39 }
40 
entrySizeMean() const41 double ContentDbase::entrySizeMean() const {
42 	const int cnt = count();
43 	if (!cnt)
44 		return -1;
45 
46 	AggrStat stat;
47 	for (int i = 0; i < cnt; ++i)
48 		stat.record(theEntries[i]->meanSize());
49 	return stat.mean();
50 }
51 
52 static
operator >>(IBStream & is,CdbEntry * & e)53 IBStream &operator >>(IBStream &is, CdbEntry *&e) {
54 	const int type = is.geti();
55 	if (is.good()) {
56 		e = ContentDbase::CreateEntry(type);
57 		e->load(is);
58 	} else {
59 		e = 0;
60 	}
61 	return is;
62 }
63 
64 static
operator <<(OBStream & os,const CdbEntry * e)65 OBStream &operator <<(OBStream &os, const CdbEntry *e) {
66 	Assert(e);
67 	os << e->type();
68 	e->store(os);
69 	return os;
70 }
71 
load(IBStream & is)72 void ContentDbase::load(IBStream &is) {
73 	theName = is.name();
74 	is >> theEntries;
75 }
76 
store(OBStream & os) const77 void ContentDbase::store(OBStream &os) const {
78 	theName = os.name();
79 	os << theEntries;
80 }
81 
add(CdbEntry * e)82 void ContentDbase::add(CdbEntry *e) {
83 	theEntries.append(e);
84 }
85 
print(ostream & os) const86 ostream &ContentDbase::print(ostream &os) const {
87 	for (int i = 0; i < count(); ++i)
88 		theEntries[i]->print(os);
89 	return os;
90 }
91 
CreateEntry(int type)92 CdbEntry *ContentDbase::CreateEntry(int type) {
93 	switch (type) {
94 		case cdbeBlob:
95 			return new CdbeBlob;
96 		case cdbeText:
97 			return new CdbeText;
98 		case cdbeLink:
99 			return new CdbeLink;
100 		case cdbePage:
101 			return new CdbePage;
102 		case cdbeComment:
103 			return new CdbeComment;
104 		default:
105 			Assert(false); // unknown entry type
106 	}
107 	return 0;
108 }
109