~ubuntu-branches/ubuntu/precise/guayadeque/precise

« back to all changes in this revision

Viewing changes to src/.svn/text-base/DbCache.cpp.svn-base

  • Committer: Bazaar Package Importer
  • Author(s): Alessio Treglia
  • Date: 2011-05-14 15:08:03 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20110514150803-8b5evqetnaj35j34
Tags: 0.3.1~dfsg0-1
* New upstream release.
* Strip wxsqlite3 stuff out of upstream's tarballs.
* Update get-orig-source target in debian/rules.
* Update gbp config file.
* Bump Standards.
* Build-depend on libwxsqlite3-2.8-dev
* Enable parallel builds.
* Link binaries against the system-wide copy of wxsqlite3.
* Point sources to the correct wxcurl's headers location.
* Update copyright file as per DEP-5
* Improve debian/watch to handle the ~dfsg\d* suffix.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// -------------------------------------------------------------------------------- //
2
 
//      Copyright (C) 2008-2010 J.Rios
3
 
//      anonbeat@gmail.com
4
 
//
5
 
//    This Program is free software; you can redistribute it and/or modify
6
 
//    it under the terms of the GNU General Public License as published by
7
 
//    the Free Software Foundation; either version 2, or (at your option)
8
 
//    any later version.
9
 
//
10
 
//    This Program is distributed in the hope that it will be useful,
11
 
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 
//    GNU General Public License for more details.
14
 
//
15
 
//    You should have received a copy of the GNU General Public License
16
 
//    along with this program; see the file LICENSE.  If not, write to
17
 
//    the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18
 
//    http://www.gnu.org/copyleft/gpl.html
19
 
//
20
 
// -------------------------------------------------------------------------------- //
21
 
#include "DbCache.h"
22
 
 
23
 
#include "Utils.h"
24
 
 
25
 
#include <wx/mstream.h>
26
 
 
27
 
guDbCache * guDbCache::m_DbCache = NULL;
28
 
 
29
 
// -------------------------------------------------------------------------------- //
30
 
guDbCache::guDbCache( const wxString &dbname ) : guDb( dbname )
31
 
{
32
 
  wxArrayString query;
33
 
 
34
 
  query.Add( wxT( "CREATE TABLE IF NOT EXISTS cache( cache_id INTEGER PRIMARY KEY AUTOINCREMENT, "
35
 
                  "cache_key varchar, cache_data BLOB, cache_time INTEGER, "
36
 
                  "cache_type INTEGER, cache_size INTEGER  );" ) );
37
 
 
38
 
  query.Add( wxT( "CREATE UNIQUE INDEX IF NOT EXISTS 'cache_id' on cache( cache_id ASC );" ) );
39
 
  query.Add( wxT( "CREATE INDEX IF NOT EXISTS 'cache_key' on cache( cache_key ASC );" ) );
40
 
  query.Add( wxT( "CREATE INDEX IF NOT EXISTS 'cache_time' on cache( cache_time ASC );" ) );
41
 
 
42
 
  int Index;
43
 
  int Count = query.Count();
44
 
  for( Index = 0; Index < Count; Index++ )
45
 
  {
46
 
      //guLogMessage( query[ Index ] );
47
 
    ExecuteUpdate( query[ Index ] );
48
 
  }
49
 
 
50
 
}
51
 
 
52
 
// -------------------------------------------------------------------------------- //
53
 
guDbCache::~guDbCache()
54
 
{
55
 
}
56
 
 
57
 
// -------------------------------------------------------------------------------- //
58
 
wxImage * guDbCache::GetImage( const wxString &url, int &imgtype, const int imgsize )
59
 
{
60
 
  wxImage *             Img = NULL;
61
 
  wxString              query;
62
 
  wxSQLite3ResultSet    dbRes;
63
 
  const unsigned char * Data;
64
 
  int                   DataLen = 0;
65
 
 
66
 
  query = wxString::Format( wxT( "SELECT cache_data, cache_type FROM cache WHERE cache_key = '%s' "
67
 
                                 "AND cache_size = %u LIMIT 1;" ),
68
 
      escape_query_str( url ).c_str(), imgsize );
69
 
 
70
 
  dbRes = ExecuteQuery( query );
71
 
 
72
 
  if( dbRes.NextRow() )
73
 
  {
74
 
    Data = dbRes.GetBlob( 0, DataLen );
75
 
    imgtype = dbRes.GetInt( 1 );
76
 
 
77
 
    if( DataLen )
78
 
    {
79
 
      wxMemoryInputStream Ins( Data, DataLen );
80
 
      Img = new wxImage( Ins, imgtype );
81
 
      if( Img )
82
 
      {
83
 
        if( !Img->IsOk() )
84
 
        {
85
 
            guLogMessage( wxT( "Image is not OK" ) );
86
 
            delete Img;
87
 
            Img = NULL;
88
 
        }
89
 
      }
90
 
//      else
91
 
//      {
92
 
//        guLogMessage( wxT( "Could not create the image" ) );
93
 
//      }
94
 
    }
95
 
  }
96
 
//  else
97
 
//  {
98
 
//      guLogMessage( wxT( "DbCache failed '%s'" ), url.c_str() );
99
 
//  }
100
 
 
101
 
  dbRes.Finalize();
102
 
 
103
 
  return Img;
104
 
}
105
 
 
106
 
// -------------------------------------------------------------------------------- //
107
 
bool guDbCache::DoSetImage( const wxString &url, wxImage * img, const int imgtype, const int imagesize )
108
 
{
109
 
  wxMemoryOutputStream Outs;
110
 
  if( img->SaveFile( Outs, imgtype ) )
111
 
  {
112
 
      wxSQLite3Statement stmt = m_Db->PrepareStatement( wxString::Format( wxT(
113
 
              "INSERT INTO cache( cache_id, cache_key, cache_data, cache_type, cache_time, cache_size ) "
114
 
              "VALUES( NULL, '%s', ?, %u, %u, %u );" ),
115
 
              escape_query_str( url ).c_str(), imgtype, wxDateTime::Now().GetTicks(), imagesize ) );
116
 
      try {
117
 
        stmt.Bind( 1, ( const unsigned char * ) Outs.GetOutputStreamBuffer()->GetBufferStart(), Outs.GetSize() );
118
 
        //guLogMessage( wxT( "%s" ), stmt.GetSQL().c_str() );
119
 
        stmt.ExecuteQuery();
120
 
        return true;
121
 
      }
122
 
      catch( wxSQLite3Exception &e )
123
 
      {
124
 
        guLogError( wxT( "%u: %s" ),  e.GetErrorCode(), e.GetMessage().c_str() );
125
 
      }
126
 
      catch(...)
127
 
      {
128
 
        guLogError( wxT( "Other exception found while updating the image in cache" ) );
129
 
      }
130
 
  }
131
 
  return false;
132
 
}
133
 
 
134
 
// -------------------------------------------------------------------------------- //
135
 
bool guDbCache::SetImage( const wxString &url, wxImage * img, const int imgtype )
136
 
{
137
 
  wxImage TmpImg( * img );
138
 
//  int Width = 150;
139
 
//  int Height = 150;
140
 
//  int ImageSize = guDBCACHE_IMAGE_SIZE_BIG;
141
 
//  TmpImg.Rescale( Width, Height, wxIMAGE_QUALITY_HIGH );
142
 
  guImageResize( &TmpImg, 150 );
143
 
  if( !DoSetImage( url, &TmpImg, imgtype, guDBCACHE_IMAGE_SIZE_BIG ) )
144
 
    return false;
145
 
 
146
 
 
147
 
  TmpImg = * img;
148
 
//  Width = 100;
149
 
//  Height = 100;
150
 
//  ImageSize = guDBCACHE_IMAGE_SIZE_MID;
151
 
//  TmpImg.Rescale( Width, Height, wxIMAGE_QUALITY_HIGH );
152
 
  guImageResize( &TmpImg, 100 );
153
 
  if( !DoSetImage( url, &TmpImg, imgtype, guDBCACHE_IMAGE_SIZE_MID ) )
154
 
    return false;
155
 
 
156
 
  TmpImg = * img;
157
 
//  Width = 50;
158
 
//  Height = 50;
159
 
//  ImageSize = guDBCACHE_IMAGE_SIZE_TINY;
160
 
//  TmpImg.Rescale( Width, Height, wxIMAGE_QUALITY_HIGH );
161
 
  guImageResize( &TmpImg, 50 );
162
 
  if( !DoSetImage( url, &TmpImg, imgtype, guDBCACHE_IMAGE_SIZE_TINY ) )
163
 
    return false;
164
 
 
165
 
  // delete the expired entries but call it only 5% of the times
166
 
  if( guRandom( 1000 ) < 20 )
167
 
    ClearExpired();
168
 
 
169
 
  return true;
170
 
}
171
 
 
172
 
// -------------------------------------------------------------------------------- //
173
 
wxString guDbCache::GetContent( const wxString &url )
174
 
{
175
 
  wxString              RetVal = wxEmptyString;
176
 
  wxString              query;
177
 
  wxSQLite3ResultSet    dbRes;
178
 
 
179
 
  query = wxString::Format( wxT( "SELECT cache_data FROM cache WHERE cache_key = '%s' LIMIT 1;" ),
180
 
             escape_query_str( url ).c_str() );
181
 
 
182
 
  dbRes = ExecuteQuery( query );
183
 
 
184
 
  if( dbRes.NextRow() )
185
 
  {
186
 
      RetVal = dbRes.GetString( 0 );
187
 
  }
188
 
  dbRes.Finalize();
189
 
 
190
 
  return RetVal;
191
 
}
192
 
 
193
 
// -------------------------------------------------------------------------------- //
194
 
bool guDbCache::SetContent( const wxString &url, const char * str, const int len )
195
 
{
196
 
  try {
197
 
    wxSQLite3Statement stmt = m_Db->PrepareStatement( wxString::Format( wxT(
198
 
          "INSERT INTO cache( cache_id, cache_key, cache_data, cache_type, cache_time, cache_size ) "
199
 
          "VALUES( NULL, '%s', ?, %u, %u, %u );" ),
200
 
          escape_query_str( url ).c_str(), guDBCACHE_TYPE_TEXT, wxDateTime::Now().GetTicks(), 0 ) );
201
 
 
202
 
    stmt.Bind( 1, ( const unsigned char * ) str, len );
203
 
    //guLogMessage( wxT( "%s" ), stmt.GetSQL().c_str() );
204
 
    stmt.ExecuteQuery();
205
 
    return true;
206
 
  }
207
 
  catch( wxSQLite3Exception &e )
208
 
  {
209
 
    guLogError( wxT( "%u: %s" ),  e.GetErrorCode(), e.GetMessage().c_str() );
210
 
  }
211
 
  return false;
212
 
}
213
 
 
214
 
// -------------------------------------------------------------------------------- //
215
 
bool guDbCache::SetContent( const wxString &url, const wxString &content )
216
 
{
217
 
  wxString query = wxString::Format( wxT( "INSERT INTO cache( cache_id, cache_key, cache_data, "
218
 
                "cache_type, cache_time, cache_size ) VALUES( NULL, '%s', '%s', %u, %u, %u );" ),
219
 
          escape_query_str( url ).c_str(),
220
 
          escape_query_str( content ).c_str(),
221
 
          guDBCACHE_TYPE_TEXT,
222
 
          wxDateTime::Now().GetTicks(),
223
 
          0 );
224
 
 
225
 
  ExecuteUpdate( query );
226
 
 
227
 
  // delete the expired entries but call it only 2% of the times
228
 
  if( guRandom( 1000 ) < 20 )
229
 
    ClearExpired();
230
 
 
231
 
  return true;
232
 
}
233
 
 
234
 
// -------------------------------------------------------------------------------- //
235
 
void guDbCache::ClearExpired( void )
236
 
{
237
 
    // last.fm queries are kept only 7 days
238
 
    wxString query = wxString::Format( wxT( "DELETE FROM cache WHERE cache_time < %u AND cache_type = %u" ),
239
 
        wxDateTime::Now().GetTicks() - 604800, guDBCACHE_TYPE_TEXT );
240
 
    ExecuteUpdate( query );
241
 
 
242
 
    // Images are kept 30 days
243
 
    query = wxString::Format( wxT( "DELETE FROM cache WHERE cache_time < %u" ), wxDateTime::Now().GetTicks() - 2592000 );
244
 
    ExecuteUpdate( query );
245
 
 
246
 
    guLogMessage( wxT( "Delete expired Cache elements done" ) );
247
 
}
248
 
 
249
 
// -------------------------------------------------------------------------------- //