File: FrequencyDBImpl_cache.cc

package info (click to toggle)
spamprobe 1.0a-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,028 kB
  • ctags: 1,006
  • sloc: cpp: 7,631; sh: 835; ansic: 346; ruby: 178; lisp: 73; makefile: 59
file content (155 lines) | stat: -rw-r--r-- 4,248 bytes parent folder | download
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
///###////////////////////////////////////////////////////////////////////////
//
// Burton Computer Corporation
// http://www.burton-computer.com
// $Id: FrequencyDBImpl_cache.cc,v 1.14 2004/01/17 01:13:17 bburton Exp $
//
// Copyright (C) 2000 Burton Computer Corporation
// ALL RIGHTS RESERVED
//
// This program is open source software; you can redistribute it
// and/or modify it under the terms of the Q Public License (QPL)
// version 1.0. Use of this software in whole or in part, including
// linking it (modified or unmodified) into other programs is
// subject to the terms of the QPL.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// Q Public License for more details.
//
// You should have received a copy of the Q Public License
// along with this program; see the file LICENSE.txt.  If not, visit
// the Burton Computer Corporation or CoolDevTools web site
// QPL pages at:
//
//    http://www.burton-computer.com/qpl.html
//

#include <unistd.h>
#include <fcntl.h>
#include "WordData.h"
#include "FrequencyDBImpl_cache.h"

static const string DATABASE_TYPE("-cached");

FrequencyDBImpl_cache::FrequencyDBImpl_cache(FrequencyDBImpl *db)
  : m_db(db)
{
  assert(db);
}

FrequencyDBImpl_cache::~FrequencyDBImpl_cache()
{
  close();
}

bool FrequencyDBImpl_cache::open(const string &filename,
                                 bool read_only,
                                 int create_mode)
{
  close();
  return m_db->open(filename, read_only, create_mode);
}

void FrequencyDBImpl_cache::close()
{
  m_db->close();
}

void FrequencyDBImpl_cache::flush()
{
  m_db->beginTransaction();
  for (map_iter_t i = m_cache.begin(); i != m_cache.end(); ++i) {
    if (i->second.is_dirty) {
      m_db->writeWord(i->first, *i->second.counts);
    }
    delete i->second.counts;
  }
  m_db->endTransaction(true);
  m_cache.clear();
  m_db->flush();
}

void FrequencyDBImpl_cache::writeWord(const string &word,
                                      const WordData &counts)
{
  if (!m_db->canCacheTerm(word)) {
    if (is_debug) {
      cerr << "UNCACHED TERM " << word << endl;
    }
    m_db->writeWord(word, counts);
  } else {
    map_iter_t i = m_cache.find(word);
    if (i == m_cache.end()) {
      m_cache.insert(make_pair(word, CacheEntry(true, false, new WordData(counts))));
      if (is_debug) {
        cerr << "CACHED TERM INSERTED " << word << endl;
      }
    } else if ((*i->second.counts).equals(counts)) {
      if (is_debug) {
        cerr << "CACHED TERM UNCHANGED " << word << endl;
      }
    } else {
      if (i->second.is_shared && i->second.counts->hasSameCounts(counts)) {
	// Do nothing because we don't want terms migrating from shared to 
	// private database if only the timestamp changed.
        if (is_debug) {
          cerr << "CACHED TERM FROM SHARED " << word << endl;
        }
      } else {
	i->second.is_dirty = true;
	i->second.is_shared = false;
	*i->second.counts = counts;
        if (is_debug) {
          cerr << "CACHED TERM UPDATED " << word << endl;
        }
      }
    }
  }
}

bool FrequencyDBImpl_cache::readWord(const string &word,
                                     WordData &counts)
{
  map_const_iter_t i = m_cache.find(word);
  if (i != m_cache.end()) {
    counts = *i->second.counts;
    return true;
  }

  bool is_shared = false;
  if (m_db->readWord(word, counts, is_shared)) {
    if (m_db->canCacheTerm(word)) {
      m_cache.insert(make_pair(word, CacheEntry(false, is_shared, new WordData(counts))));
    }
    return true;
  }

  return false;
}

bool FrequencyDBImpl_cache::firstWord(string &word,
                                      WordData &counts)
{
  flush();
  return m_db->firstWord(word, counts);
}

bool FrequencyDBImpl_cache::nextWord(string &word,
                                     WordData &counts)
{
  return m_db->nextWord(word, counts);
}

string FrequencyDBImpl_cache::getDatabaseType() const
{
  return m_db->getDatabaseType() + DATABASE_TYPE;
}

void FrequencyDBImpl_cache::sweepOutOldTerms(int junk_count,
					     int max_age)
{
  flush();
  m_db->sweepOutOldTerms(junk_count, max_age);
}