ModErn Text Analysis
META Enumerates Textual Applications
make_index.h
Go to the documentation of this file.
1 
11 #ifndef META_MAKE_INDEX_H_
12 #define META_MAKE_INDEX_H_
13 
14 #include "cpptoml.h"
15 #include "caching/all.h"
16 #include "index/cached_index.h"
17 #include "util/filesystem.h"
18 
19 namespace meta
20 {
21 namespace index
22 {
23 
24 class inverted_index;
25 class forward_index;
26 
30 
33 
37 
39 using dblru_forward_index =
41 
44 
59 template <class Index, class... Args>
60 std::shared_ptr<Index> make_index(const std::string& config_file,
61  Args&&... args)
62 {
63  auto config = cpptoml::parse_file(config_file);
64 
65  // check if we have paths specified for either kind of index
66  if (!(config.contains("forward-index")
67  && config.contains("inverted-index")))
68  {
69  throw typename Index::exception{
70  "forward-index or inverted-index missing from configuration file"};
71  }
72 
73  // make sure that the index names are different!
74  auto fwd_name = config.get_as<std::string>("forward-index");
75  auto inv_name = config.get_as<std::string>("inverted-index");
76 
77  if (*fwd_name == *inv_name)
78  {
79  throw typename Index::exception{
80  "forward and inverted index names must be different!"};
81  }
82 
83  // can't use std::make_shared here since the Index constructor is private
84  auto idx =
85  std::shared_ptr<Index>{new Index(config, std::forward<Args>(args)...)};
86 
87  // if index has already been made, load it
88  if (filesystem::make_directory(idx->index_name()) && idx->valid())
89  idx->load_index();
90  else
91  idx->create_index(config_file);
92 
93  return idx;
94 }
95 
115 template <class Index, template <class, class> class Cache, class... Args>
116 std::shared_ptr<cached_index<Index, Cache>>
117  make_index(const std::string& config_file, Args&&... args)
118 {
119  return make_index<cached_index<Index, Cache>>(config_file,
120  std::forward<Args>(args)...);
121 }
122 }
123 }
124 
125 #endif
Decorator class for wrapping indexes with a cache.
Definition: cached_index.h:30
cached_index< inverted_index, caching::default_dblru_cache > dblru_inverted_index
Inverted index using default DBLRU cache.
Definition: make_index.h:29
The ModErn Text Analysis toolkit is a suite of natural language processing, classification, information retreival, data mining, and other applications of text processing.
Definition: analyzer.h:24
std::shared_ptr< Index > make_index(const std::string &config_file, Args &&...args)
Factory method for creating indexes.
Definition: make_index.h:60