1/*------------------------------------------------------------------------------
2* Copyright (C) 2003-2006 Ben van Klinken and the CLucene Team
3*
4* Distributable under the terms of either the Apache License (Version 2.0) or
5* the GNU Lesser General Public License, as specified in the COPYING file.
6------------------------------------------------------------------------------*/
7#ifndef _lucene_search_FieldCacheImpl_
8#define _lucene_search_FieldCacheImpl_
9
10#if defined(_LUCENE_PRAGMA_ONCE)
11# pragma once
12#endif
13
14#include "CLucene/index/IndexReader.h"
15#include "FieldCache.h"
16#include "Sort.h"
17
18
19CL_NS_DEF(search)
20
21
22/**
23 * Expert: The default cache implementation, storing all values in memory.
24 *
25 */
26class FieldCacheImpl: public FieldCache {
27public:
28 DEFINE_MUTEX(THIS_LOCK)
29
30 /** Expert: Every key in the internal cache is of this type. */
31 class FileEntry:LUCENE_BASE {
32 const TCHAR* field; // which Field
33 int32_t type; // which SortField type
34 SortComparatorSource* custom; // which custom comparator
35 size_t _hashCode;
36 public:
37 /** Creates one of these objects. */
38 FileEntry (const TCHAR* field, int32_t type);
39
40 /** Creates one of these objects for a custom comparator. */
41 FileEntry (const TCHAR* field, SortComparatorSource* custom);
42 ~FileEntry();
43
44 int32_t getType() const{ return type; }
45
46 /** Two of these are equal iff they reference the same field and type. */
47 bool equals (FileEntry* other) const;
48
49 /** Composes a hashcode based on the field and type. */
50 size_t hashCode();
51
52 int32_t compareTo(const FileEntry* other) const;
53
54 class Compare:LUCENE_BASE, public CL_NS(util)::Compare::_base //<Term*>
55 {
56 public:
57 bool operator()( FileEntry* f1, FileEntry* f2 ) const{
58 return ( f1->compareTo(f2) < 0 );
59 }
60 size_t operator()( FileEntry* t ) const{
61 return t->hashCode();
62 }
63 };
64 class Equals:LUCENE_BASE, public CL_NS(util)::Compare::_base //<Term*>
65 {
66 public:
67 bool operator()( FileEntry* f1, FileEntry* f2 ) const{
68 return ( f1->compareTo(f2) == 0 );
69 }
70 };
71 };
72
73 FieldCacheImpl();
74 ~FieldCacheImpl();
75private:
76
77 ///the type that is stored in the field cache. can't use a typedef because
78 ///the decorated name would become too long
79 class fieldcacheCacheReaderType: public CL_NS(util)::CLHashMap<FileEntry*,
80 FieldCacheAuto*,
81 FileEntry::Compare,
82 FileEntry::Equals,
83 CL_NS(util)::Deletor::Object<FileEntry>,
84 CL_NS(util)::Deletor::Object<FieldCacheAuto> >{
85 public:
86 fieldcacheCacheReaderType();
87 ~fieldcacheCacheReaderType();
88 };
89
90 //note: typename gets too long if using cacheReaderType as a typename
91 typedef CL_NS(util)::CLHashMap<CL_NS(index)::IndexReader*,
92 fieldcacheCacheReaderType*,
93 CL_NS(util)::Compare::Void<CL_NS(index)::IndexReader>,
94 CL_NS(util)::Equals::Void<CL_NS(index)::IndexReader>,
95 CL_NS(util)::Deletor::Object<CL_NS(index)::IndexReader>,
96 CL_NS(util)::Deletor::Object<fieldcacheCacheReaderType> > fieldcacheCacheType;
97
98 /** The internal cache. Maps FileEntry to array of interpreted term values. **/
99 //todo: make indexreader remove itself from here when the reader is shut
100 fieldcacheCacheType cache;
101
102 /** See if an object is in the cache. */
103 FieldCacheAuto* lookup (CL_NS(index)::IndexReader* reader, const TCHAR* field, int32_t type) ;
104
105 /** See if a custom object is in the cache. */
106 FieldCacheAuto* lookup (CL_NS(index)::IndexReader* reader, const TCHAR* field, SortComparatorSource* comparer);
107
108 /** Put an object into the cache. */
109 void store (CL_NS(index)::IndexReader* reader, const TCHAR* field, int32_t type, FieldCacheAuto* value);
110
111 /** Put a custom object into the cache. */
112 void store (CL_NS(index)::IndexReader* reader, const TCHAR* field, SortComparatorSource* comparer, FieldCacheAuto* value);
113
114public:
115
116 // inherit javadocs
117 FieldCacheAuto* getInts (CL_NS(index)::IndexReader* reader, const TCHAR* field);
118
119 // inherit javadocs
120 FieldCacheAuto* getFloats (CL_NS(index)::IndexReader* reader, const TCHAR* field);
121
122 // inherit javadocs
123 FieldCacheAuto* getStrings (CL_NS(index)::IndexReader* reader, const TCHAR* field);
124
125 // inherit javadocs
126 FieldCacheAuto* getStringIndex (CL_NS(index)::IndexReader* reader, const TCHAR* field);
127
128 // inherit javadocs
129 FieldCacheAuto* getAuto (CL_NS(index)::IndexReader* reader, const TCHAR* field);
130
131 // inherit javadocs
132 FieldCacheAuto* getCustom (CL_NS(index)::IndexReader* reader, const TCHAR* field, SortComparator* comparator);
133
134
135 /**
136 * Callback for when IndexReader closes. This causes
137 * any cache to be removed for the specified reader.
138 */
139 static void closeCallback(CL_NS(index)::IndexReader* reader, void* fieldCacheImpl);
140};
141
142
143CL_NS_END
144#endif
145