Scid  4.7.0
codec_memory.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016-2017 Fulvio Benini
3 
4  * This file is part of Scid (Shane's Chess Information Database).
5  *
6  * Scid is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation.
9  *
10  * Scid 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 Scid. If not, see <http://www.gnu.org/licenses/>.
17  *
18  */
19 
20 /** @file
21  * Implements the CodecMemory class, which represent a memory database.
22  */
23 
24 #ifndef CODEC_MEMORY_H
25 #define CODEC_MEMORY_H
26 
27 #include "codec_native.h"
28 
29 /**
30  * Manages memory databases that do not have associated files.
31  * Every open database should have a native representation in memory: to satisfy
32  * this requirement non-native codecs should be derived from this class.
33  */
34 class CodecMemory : public CodecNative<CodecMemory> {
36 
37  enum : uint64_t {
38  LIMIT_GAMEOFFSET = 1ULL << 46,
39  LIMIT_GAMELEN = 1ULL << 18,
40  LIMIT_NUMGAMES = (1ULL << 32) - 2,
41  LIMIT_UNIQUENAMES = 1ULL << 28,
42  LIMIT_NAMELEN = 255
43  };
44 
45 public: // ICodecDatabase interface
46  Codec getType() const override { return ICodecDatabase::MEMORY; }
47 
48  std::vector<std::string> getFilenames() const override {
49  return std::vector<std::string>();
50  }
51 
52  std::vector<std::pair<const char*, std::string>>
53  getExtraInfo() const override {
54  std::vector<std::pair<const char*, std::string>> res;
55  res.emplace_back("type", std::to_string(idx_->GetType()));
56  return res;
57  }
58 
59  const byte* getGameData(uint64_t offset, uint32_t length) override {
60  ASSERT(offset < v_.size());
61  ASSERT(length <= v_.size() - offset);
62  ASSERT(v_.contiguous(static_cast<size_t>(offset)) >= length);
63  return &v_[offset];
64  }
65 
66  errorT flush() override { return OK; }
67 
68  errorT dyn_open(fileModeT fMode, const char*, const Progress&, Index* idx,
69  NameBase* nb) override {
70  if (idx == 0 || nb == 0)
71  return ERROR;
72  if (fMode != FMODE_Memory)
73  return ERROR;
74  idx_ = idx;
75  nb_ = nb;
76  return OK;
77  }
78 
79 public: // CodecNative CRTP
80  /**
81  * Stores the data of a game into memory.
82  * @param src: valid pointer to a buffer that contains the game data
83  * (encoded in native format).
84  * @param length: the length of the buffer @p src (in bytes).
85  * @returns
86  * - on success, a @e std::pair containing OK and the offset of the stored
87  * data (usable to retrieve the data with getGameData()).
88  * - on failure, a @e std::pair containing an error code and 0.
89  */
90  std::pair<errorT, uint64_t> dyn_addGameData(const byte* src,
91  size_t length) {
92  ASSERT(src != 0);
93 
94  if (length >= LIMIT_GAMELEN)
95  return std::make_pair(ERROR_GameLengthLimit, 0);
96 
97  auto offset = v_.size();
98  auto capacity = v_.capacity();
99  if (capacity - offset < length) // Do not fit in the current chunk
100  offset = capacity;
101  if (offset >= LIMIT_GAMEOFFSET)
102  return std::make_pair(ERROR_OffsetLimit, 0);
103 
104  v_.resize(offset + length);
105  ASSERT(v_.contiguous(offset) >= length);
106  std::copy_n(src, length, &v_[offset]);
107  return {OK, offset};
108  }
109 
110  /**
111  * Given a name (string), retrieve the corresponding ID.
112  * The name is added to @e nb_ if do not already exists in the NameBase.
113  * @param nt: nameT type of the name to retrieve.
114  * @param name: the name to retrieve.
115  * @returns
116  * - on success, a @e std::pair containing OK and the ID.
117  * - on failure, a @e std::pair containing an error code and 0.
118  */
119  std::pair<errorT, idNumberT> dyn_addName(nameT nt, const char* name) {
120  return nb_->addName(nt, name, LIMIT_NAMELEN, LIMIT_UNIQUENAMES);
121  }
122 
123  /**
124  * Add an IndexEntry to @e idx_.
125  * @param ie: the IndexEntry object to add.
126  * @returns OK if successful or an error code.
127  */
129  auto nGames = idx_->GetNumGames();
130  if (nGames >= LIMIT_NUMGAMES)
131  return ERROR_NumGamesLimit;
132 
133  return idx_->WriteEntry(&ie, nGames);
134  }
135 
136  /**
137  * Replace an IndexEntry.
138  * @param ie: the IndexEntry with the new data.
139  * @param replaced: valid gamenumT of the game to be replaced.
140  * @returns OK if successful or an error code.
141  */
143  return idx_->WriteEntry(&ie, replaced);
144  }
145 };
146 
147 #endif
unsigned char byte
Definition: common.h:89
Manages memory databases that do not have associated files.
Definition: codec_memory.h:34
const errorT ERROR_NumGamesLimit
Definition: error.h:59
std::pair< errorT, idNumberT > addName(nameT nt, const char *name, size_t MAX_LEN, idNumberT MAX_ID)
Add a name (string) to the NameBase.
Definition: namebase.h:81
void resize(size_t count)
Definition: containers.h:149
const byte * getGameData(uint64_t offset, uint32_t length) override
Fetches the data of a game (excluding index info), encoded in native format.
Definition: codec_memory.h:59
std::vector< std::pair< const char *, std::string > > getExtraInfo() const override
Returns a vector of tag pairs containing extra information about the database (type, description, autoload, etc..)
Definition: codec_memory.h:53
const errorT OK
Definition: error.h:23
const errorT ERROR_OffsetLimit
Definition: error.h:57
#define ASSERT(f)
Definition: common.h:59
errorT dyn_open(fileModeT fMode, const char *, const Progress &, Index *idx, NameBase *nb) override
Opens/Creates a database.
Definition: codec_memory.h:68
uint GetType() const
Definition: index.h:137
size_t size() const
Definition: containers.h:169
Definition: misc.h:63
names
Definition: tablebase.tcl:257
gamenumT GetNumGames() const
Header getter functions.
Definition: index.h:136
This class stores the database&#39;s names (players, events, sites and rounds).
Definition: namebase.h:33
std::vector< std::string > getFilenames() const override
Returns the full path of the files used by the database.
Definition: codec_memory.h:48
errorT dyn_saveIndexEntry(const IndexEntry &ie, gamenumT replaced)
Replace an IndexEntry.
Definition: codec_memory.h:142
This class stores the pointers to the Index and NameBase objects used by a native codec...
Definition: codec_native.h:43
Definition: index.h:58
size_t contiguous(size_t pos) const
Definition: containers.h:138
std::pair< errorT, uint64_t > dyn_addGameData(const byte *src, size_t length)
Stores the data of a game into memory.
Definition: codec_memory.h:90
unsigned short errorT
Definition: error.h:20
Implements CodecNative, which is used as base class by native codecs.
std::pair< errorT, idNumberT > dyn_addName(nameT nt, const char *name)
Given a name (string), retrieve the corresponding ID.
Definition: codec_memory.h:119
const errorT ERROR_GameLengthLimit
Definition: error.h:58
errorT WriteEntry(const IndexEntry *ie, gamenumT idx)
WriteEntry() - modify a game in the Index.
Definition: index.cpp:160
Codec getType() const override
Returns the Codec type.
Definition: codec_memory.h:46
errorT flush() override
Writes all pending output to the files.
Definition: codec_memory.h:66
unsigned nameT
Definition: common.h:153
uint gamenumT
Definition: common.h:163
fileModeT
Definition: common.h:136
size_t capacity() const
Definition: containers.h:132
const errorT ERROR
Definition: error.h:26
errorT dyn_addIndexEntry(const IndexEntry &ie)
Add an IndexEntry to idx_.
Definition: codec_memory.h:128
Definition: indexentry.h:54