Open-Transactions  0.93.0-ge03d287
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
opentxs::OTDB::Storage Class Referenceabstract

#include <OTStorage.hpp>

Inheritance diagram for opentxs::OTDB::Storage:

Public Member Functions

EXPORT OTPackerGetPacker (PackType ePackType=OTDB_DEFAULT_PACKER)
 
virtual bool Exists (std::string strFolder, std::string oneStr="", std::string twoStr="", std::string threeStr="")=0
 
virtual int64_t FormPathString (std::string &strOutput, std::string strFolder, std::string oneStr="", std::string twoStr="", std::string threeStr="")=0
 
virtual ~Storage ()
 
EXPORT bool StoreString (std::string strContents, std::string strFolder, std::string oneStr="", std::string twoStr="", std::string threeStr="")
 
EXPORT std::string QueryString (std::string strFolder, std::string oneStr="", std::string twoStr="", std::string threeStr="")
 
EXPORT bool StorePlainString (std::string strContents, std::string strFolder, std::string oneStr="", std::string twoStr="", std::string threeStr="")
 
EXPORT std::string QueryPlainString (std::string strFolder, std::string oneStr="", std::string twoStr="", std::string threeStr="")
 
EXPORT bool StoreObject (Storable &theContents, std::string strFolder, std::string oneStr="", std::string twoStr="", std::string threeStr="")
 
EXPORT StorableQueryObject (StoredObjectType theObjectType, std::string strFolder, std::string oneStr="", std::string twoStr="", std::string threeStr="")
 
EXPORT std::string EncodeObject (Storable &theContents)
 
EXPORT StorableDecodeObject (StoredObjectType theObjectType, std::string strInput)
 
EXPORT bool EraseValueByKey (std::string strFolder, std::string oneStr="", std::string twoStr="", std::string threeStr="")
 
EXPORT StorableCreateObject (StoredObjectType eType)
 
EXPORT StorageType GetType () const
 

Static Public Member Functions

static EXPORT StorageCreate (StorageType eStorageType, PackType ePackType)
 

Protected Member Functions

 Storage ()
 
 Storage (const Storage &)
 
void SetPacker (OTPacker &thePacker)
 
virtual bool onStorePackedBuffer (PackedBuffer &theBuffer, std::string strFolder, std::string oneStr="", std::string twoStr="", std::string threeStr="")=0
 
virtual bool onQueryPackedBuffer (PackedBuffer &theBuffer, std::string strFolder, std::string oneStr="", std::string twoStr="", std::string threeStr="")=0
 
virtual bool onStorePlainString (std::string &theBuffer, std::string strFolder, std::string oneStr="", std::string twoStr="", std::string threeStr="")=0
 
virtual bool onQueryPlainString (std::string &theBuffer, std::string strFolder, std::string oneStr="", std::string twoStr="", std::string threeStr="")=0
 
virtual bool onEraseValueByKey (std::string strFolder, std::string oneStr="", std::string twoStr="", std::string threeStr="")=0
 

Detailed Description

Definition at line 610 of file OTStorage.hpp.

Constructor & Destructor Documentation

opentxs::OTDB::Storage::Storage ( )
inlineprotected

Definition at line 616 of file OTStorage.hpp.

617  : m_pPacker(nullptr)
618  {
619  }
opentxs::OTDB::Storage::Storage ( const Storage )
inlineprotected

Definition at line 621 of file OTStorage.hpp.

622  : m_pPacker(nullptr)
623  {
624  } // We don't want to copy the pointer. Let it create its own.
virtual opentxs::OTDB::Storage::~Storage ( )
inlinevirtual

Definition at line 714 of file OTStorage.hpp.

715  {
716  if (nullptr != m_pPacker) delete m_pPacker;
717  m_pPacker = nullptr;
718  }

Member Function Documentation

Storage * opentxs::OTDB::Storage::Create ( StorageType  eStorageType,
PackType  ePackType 
)
static

Definition at line 2145 of file OTStorage.cpp.

2146 {
2147  Storage* pStore = nullptr;
2148 
2149  switch (eStorageType) {
2150  case STORE_FILESYSTEM:
2151  pStore = StorageFS::Instantiate();
2152  OT_ASSERT(nullptr != pStore);
2153  break;
2154  // case STORE_COUCH_DB:
2155  // pStore = new StorageCouchDB; OT_ASSERT(nullptr != pStore);
2156  // break;
2157  default:
2158  otErr << "OTDB::Storage::Create: Failed: Unknown storage type.\n";
2159  break;
2160  }
2161 
2162  // IF we successfully created the storage context, now let's
2163  // try to create the packer that goes with it.
2164  // (They are created together and linked until death.)
2165 
2166  if (nullptr != pStore) {
2167  OTPacker* pPacker = OTPacker::Create(ePackType);
2168 
2169  if (nullptr == pPacker) {
2170  otErr << "OTDB::Storage::Create: Failed while creating packer.\n";
2171 
2172  // For whatever reason, we failed. Memory issues or whatever.
2173  delete pStore;
2174  pStore = nullptr;
2175  return nullptr;
2176  }
2177 
2178  // Now they're married.
2179  pStore->SetPacker(*pPacker);
2180  }
2181  else
2182  otErr << "OTDB::Storage::Create: Failed, since pStore is nullptr.\n";
2183 
2184  return pStore; // Possible to return nullptr.
2185 }
static StorageFS * Instantiate()
Definition: OTStorage.hpp:1896
#define OT_ASSERT(x)
Definition: Assert.hpp:150
static OTPacker * Create(PackType ePackType)
Definition: OTStorage.cpp:889
OTLOG_IMPORT OTLogStream otErr
Storable * opentxs::OTDB::Storage::CreateObject ( StoredObjectType  eType)

Definition at line 2128 of file OTStorage.cpp.

2129 {
2130  OTPacker* pPacker = GetPacker();
2131 
2132  if (nullptr == pPacker) {
2133  otErr << "OTDB::Storage::CreateObject: Failed, since GetPacker() "
2134  "returned nullptr.\n";
2135  return nullptr;
2136  }
2137 
2138  Storable* pStorable = Storable::Create(eType, pPacker->GetType());
2139 
2140  return pStorable; // May return nullptr.
2141 }
OTLOG_IMPORT OTLogStream otErr
EXPORT OTPacker * GetPacker(PackType ePackType=OTDB_DEFAULT_PACKER)
Definition: OTStorage.cpp:2103
static EXPORT Storable * Create(StoredObjectType eType, PackType thePackType)
Definition: OTStorage.cpp:860
Storable * opentxs::OTDB::Storage::DecodeObject ( StoredObjectType  theObjectType,
std::string  strInput 
)

Definition at line 2435 of file OTStorage.cpp.

2437 {
2438  if (strInput.size() < 1) return nullptr;
2439 
2440  OTPacker* pPacker = GetPacker();
2441 
2442  if (nullptr == pPacker) return nullptr;
2443 
2444  PackedBuffer* pBuffer = pPacker->CreateBuffer();
2445 
2446  if (nullptr == pBuffer) return nullptr;
2447 
2448  // Below this point, responsible for pBuffer.
2449 
2450  Storable* pStorable = CreateObject(theObjectType);
2451 
2452  if (nullptr == pStorable) {
2453  delete pBuffer;
2454  return nullptr;
2455  }
2456 
2457  // Below this point, responsible for pBuffer AND pStorable.
2458 
2459  OTASCIIArmor theArmor;
2460  theArmor.Set(strInput.c_str(), static_cast<uint32_t>(strInput.size()));
2461  const OTPayload thePayload(theArmor);
2462 
2463  // Put thePayload's contents into pBuffer here.
2464  //
2465  pBuffer->SetData(
2466  static_cast<const uint8_t*>(thePayload.GetPayloadPointer()),
2467  thePayload.GetSize());
2468 
2469  // Now let's unpack it and return the Storable object.
2470 
2471  const bool bUnpacked = pPacker->Unpack(*pBuffer, *pStorable);
2472 
2473  if (!bUnpacked) {
2474  delete pBuffer;
2475  delete pStorable;
2476 
2477  return nullptr;
2478  }
2479 
2480  // Success :-)
2481 
2482  // Don't want any leaks here, do we?
2483  delete pBuffer;
2484 
2485  return pStorable; // caller is responsible to delete.
2486 }
EXPORT Storable * CreateObject(StoredObjectType eType)
Definition: OTStorage.cpp:2128
EXPORT OTPacker * GetPacker(PackType ePackType=OTDB_DEFAULT_PACKER)
Definition: OTStorage.cpp:2103
std::string opentxs::OTDB::Storage::EncodeObject ( Storable theContents)

Definition at line 2389 of file OTStorage.cpp.

2390 {
2391  std::string strReturnValue("");
2392 
2393  OTPacker* pPacker = GetPacker();
2394 
2395  if (nullptr == pPacker) {
2396  otErr << "Storage::EncodeObject: No packer allocated.\n";
2397  return strReturnValue;
2398  }
2399 
2400  PackedBuffer* pBuffer = pPacker->Pack(theContents);
2401 
2402  if (nullptr == pBuffer) {
2403  otErr << "Storage::EncodeObject: Packing failed.\n";
2404  return strReturnValue;
2405  }
2406 
2407  // OTPackedBuffer:
2408  // virtual const uint8_t * GetData()=0;
2409  // virtual size_t GetSize()=0;
2410  //
2411  const uint32_t nNewSize = static_cast<const uint32_t>(pBuffer->GetSize());
2412  const void* pNewData = static_cast<const void*>(pBuffer->GetData());
2413 
2414  if ((nNewSize < 1) || (nullptr == pNewData)) {
2415  delete pBuffer;
2416  pBuffer = nullptr;
2417 
2418  otErr << "Storage::EncodeObject: Packing failed (2).\n";
2419  return strReturnValue;
2420  }
2421 
2422  const OTData theData(pNewData, nNewSize);
2423  const OTASCIIArmor theArmor(theData);
2424 
2425  strReturnValue.assign(theArmor.Get(), theArmor.GetLength());
2426 
2427  // Don't want any leaks here, do we?
2428  delete pBuffer;
2429 
2430  return strReturnValue;
2431 }
OTLOG_IMPORT OTLogStream otErr
EXPORT OTPacker * GetPacker(PackType ePackType=OTDB_DEFAULT_PACKER)
Definition: OTStorage.cpp:2103
bool opentxs::OTDB::Storage::EraseValueByKey ( std::string  strFolder,
std::string  oneStr = "",
std::string  twoStr = "",
std::string  threeStr = "" 
)

Definition at line 2488 of file OTStorage.cpp.

2490 {
2491  bool bSuccess = onEraseValueByKey(strFolder, oneStr, twoStr, threeStr);
2492 
2493  if (!bSuccess)
2494  otErr << "Storage::EraseValueByKey: Failed trying to erase a value "
2495  "(while calling onEraseValueByKey) \n";
2496 
2497  return bSuccess;
2498 }
virtual bool onEraseValueByKey(std::string strFolder, std::string oneStr="", std::string twoStr="", std::string threeStr="")=0
OTLOG_IMPORT OTLogStream otErr
virtual bool opentxs::OTDB::Storage::Exists ( std::string  strFolder,
std::string  oneStr = "",
std::string  twoStr = "",
std::string  threeStr = "" 
)
pure virtual

Implemented in opentxs::OTDB::StorageFS.

virtual int64_t opentxs::OTDB::Storage::FormPathString ( std::string &  strOutput,
std::string  strFolder,
std::string  oneStr = "",
std::string  twoStr = "",
std::string  threeStr = "" 
)
pure virtual

Implemented in opentxs::OTDB::StorageFS.

OTPacker * opentxs::OTDB::Storage::GetPacker ( PackType  ePackType = OTDB_DEFAULT_PACKER)

Definition at line 2103 of file OTStorage.cpp.

2104 {
2105  // Normally if you use Create(), the packer is created at that time.
2106  // However, in the future, coders using the API may create subclasses of
2107  // Storage through SWIG, which Create could not anticipate. This mechanism
2108  // makes sure that in those cases, the packer still gets set (on the first
2109  // Get() call), and the coder using the API still has the ability to choose
2110  // what type of packer will be used.
2111  //
2112  if (nullptr == m_pPacker) {
2113  m_pPacker = OTPacker::Create(ePackType);
2114  }
2115 
2116  return m_pPacker; // May return nullptr. (If Create call above fails.)
2117 }
static OTPacker * Create(PackType ePackType)
Definition: OTStorage.cpp:889
StorageType opentxs::OTDB::Storage::GetType ( ) const

Definition at line 2187 of file OTStorage.cpp.

2188 {
2189  // If I find the type, then I return it. Otherwise I ASSUME
2190  // that the coder using the API has subclassed Storage, and
2191  // that this is a custom Storage type invented by the API user.
2192 
2193  if (typeid(*this) == typeid(StorageFS)) return STORE_FILESYSTEM;
2194  // else if (typeid(*this) == typeid(StorageCouchDB))
2195  // return STORE_COUCH_DB;
2196  // Etc.
2197  //
2198  else
2199  return STORE_TYPE_SUBCLASS; // The Java coder using API must have
2200  // subclassed Storage himself.
2201 }
virtual bool opentxs::OTDB::Storage::onEraseValueByKey ( std::string  strFolder,
std::string  oneStr = "",
std::string  twoStr = "",
std::string  threeStr = "" 
)
protectedpure virtual

Implemented in opentxs::OTDB::StorageFS.

virtual bool opentxs::OTDB::Storage::onQueryPackedBuffer ( PackedBuffer theBuffer,
std::string  strFolder,
std::string  oneStr = "",
std::string  twoStr = "",
std::string  threeStr = "" 
)
protectedpure virtual

Implemented in opentxs::OTDB::StorageFS.

virtual bool opentxs::OTDB::Storage::onQueryPlainString ( std::string &  theBuffer,
std::string  strFolder,
std::string  oneStr = "",
std::string  twoStr = "",
std::string  threeStr = "" 
)
protectedpure virtual

Implemented in opentxs::OTDB::StorageFS.

virtual bool opentxs::OTDB::Storage::onStorePackedBuffer ( PackedBuffer theBuffer,
std::string  strFolder,
std::string  oneStr = "",
std::string  twoStr = "",
std::string  threeStr = "" 
)
protectedpure virtual

Implemented in opentxs::OTDB::StorageFS.

virtual bool opentxs::OTDB::Storage::onStorePlainString ( std::string &  theBuffer,
std::string  strFolder,
std::string  oneStr = "",
std::string  twoStr = "",
std::string  threeStr = "" 
)
protectedpure virtual

Implemented in opentxs::OTDB::StorageFS.

Storable * opentxs::OTDB::Storage::QueryObject ( StoredObjectType  theObjectType,
std::string  strFolder,
std::string  oneStr = "",
std::string  twoStr = "",
std::string  threeStr = "" 
)

Definition at line 2336 of file OTStorage.cpp.

2339 {
2340  OTPacker* pPacker = GetPacker();
2341 
2342  if (nullptr == pPacker) return nullptr;
2343 
2344  PackedBuffer* pBuffer = pPacker->CreateBuffer();
2345 
2346  if (nullptr == pBuffer) return nullptr;
2347 
2348  // Below this point, responsible for pBuffer.
2349 
2350  Storable* pStorable = CreateObject(theObjectType);
2351 
2352  if (nullptr == pStorable) {
2353  delete pBuffer;
2354  return nullptr;
2355  }
2356 
2357  // Below this point, responsible for pBuffer AND pStorable.
2358 
2359  bool bSuccess =
2360  onQueryPackedBuffer(*pBuffer, strFolder, oneStr, twoStr, threeStr);
2361 
2362  if (!bSuccess) {
2363  delete pBuffer;
2364  delete pStorable;
2365 
2366  return nullptr;
2367  }
2368 
2369  // We got the packed buffer back from the query!
2370  // Now let's unpack it and return the Storable object.
2371 
2372  bool bUnpacked = pPacker->Unpack(*pBuffer, *pStorable);
2373 
2374  if (!bUnpacked) {
2375  delete pBuffer;
2376  delete pStorable;
2377 
2378  return nullptr;
2379  }
2380 
2381  // Success :-)
2382 
2383  // Don't want any leaks here, do we?
2384  delete pBuffer;
2385 
2386  return pStorable; // caller is responsible to delete.
2387 }
EXPORT Storable * CreateObject(StoredObjectType eType)
Definition: OTStorage.cpp:2128
virtual bool onQueryPackedBuffer(PackedBuffer &theBuffer, std::string strFolder, std::string oneStr="", std::string twoStr="", std::string threeStr="")=0
EXPORT OTPacker * GetPacker(PackType ePackType=OTDB_DEFAULT_PACKER)
Definition: OTStorage.cpp:2103
std::string opentxs::OTDB::Storage::QueryPlainString ( std::string  strFolder,
std::string  oneStr = "",
std::string  twoStr = "",
std::string  threeStr = "" 
)

Definition at line 2290 of file OTStorage.cpp.

2292 {
2293  std::string theString("");
2294 
2295  if (!onQueryPlainString(theString, strFolder, oneStr, twoStr, threeStr))
2296  theString = "";
2297 
2298  return theString;
2299 }
virtual bool onQueryPlainString(std::string &theBuffer, std::string strFolder, std::string oneStr="", std::string twoStr="", std::string threeStr="")=0
std::string opentxs::OTDB::Storage::QueryString ( std::string  strFolder,
std::string  oneStr = "",
std::string  twoStr = "",
std::string  threeStr = "" 
)

Definition at line 2237 of file OTStorage.cpp.

2239 {
2240  std::string theString("");
2241 
2242  OTPacker* pPacker = GetPacker();
2243 
2244  if (nullptr == pPacker) return theString;
2245 
2246  PackedBuffer* pBuffer = pPacker->CreateBuffer();
2247 
2248  if (nullptr == pBuffer) return theString;
2249 
2250  // Below this point, responsible for pBuffer.
2251 
2252  bool bSuccess =
2253  onQueryPackedBuffer(*pBuffer, strFolder, oneStr, twoStr, threeStr);
2254 
2255  if (!bSuccess) {
2256  delete pBuffer;
2257  pBuffer = nullptr;
2258  return theString;
2259  }
2260 
2261  // We got the packed buffer back from the query!
2262  // Now let's unpack it and return the Storable object.
2263 
2264  bool bUnpacked = pPacker->Unpack(*pBuffer, theString);
2265 
2266  if (!bUnpacked) {
2267  delete pBuffer;
2268  theString = "";
2269  return theString;
2270  }
2271 
2272  // Success :-)
2273 
2274  // Don't want any leaks here, do we?
2275  delete pBuffer;
2276  pBuffer = nullptr;
2277 
2278  return theString;
2279 }
virtual bool onQueryPackedBuffer(PackedBuffer &theBuffer, std::string strFolder, std::string oneStr="", std::string twoStr="", std::string threeStr="")=0
EXPORT OTPacker * GetPacker(PackType ePackType=OTDB_DEFAULT_PACKER)
Definition: OTStorage.cpp:2103
void opentxs::OTDB::Storage::SetPacker ( OTPacker thePacker)
inlineprotected

Definition at line 627 of file OTStorage.hpp.

628  {
629  OT_ASSERT(nullptr == m_pPacker);
630  m_pPacker = &thePacker;
631  }
#define OT_ASSERT(x)
Definition: Assert.hpp:150
bool opentxs::OTDB::Storage::StoreObject ( Storable theContents,
std::string  strFolder,
std::string  oneStr = "",
std::string  twoStr = "",
std::string  threeStr = "" 
)

Definition at line 2301 of file OTStorage.cpp.

2304 {
2305  OTPacker* pPacker = GetPacker();
2306 
2307  if (nullptr == pPacker) {
2308  otErr << "No packer allocated in Storage::StoreObject\n";
2309  return false;
2310  }
2311 
2312  PackedBuffer* pBuffer = pPacker->Pack(theContents);
2313 
2314  if (nullptr == pBuffer) {
2315  otErr << "Packing failed in Storage::StoreObject\n";
2316  return false;
2317  }
2318 
2319  bool bSuccess =
2320  onStorePackedBuffer(*pBuffer, strFolder, oneStr, twoStr, threeStr);
2321 
2322  if (!bSuccess) {
2323  otErr << "Storing failed in Storage::StoreObject (calling "
2324  "onStorePackedBuffer) \n";
2325  return false;
2326  }
2327 
2328  // Don't want any leaks here, do we?
2329  delete pBuffer;
2330 
2331  return bSuccess;
2332 }
virtual bool onStorePackedBuffer(PackedBuffer &theBuffer, std::string strFolder, std::string oneStr="", std::string twoStr="", std::string threeStr="")=0
OTLOG_IMPORT OTLogStream otErr
EXPORT OTPacker * GetPacker(PackType ePackType=OTDB_DEFAULT_PACKER)
Definition: OTStorage.cpp:2103
bool opentxs::OTDB::Storage::StorePlainString ( std::string  strContents,
std::string  strFolder,
std::string  oneStr = "",
std::string  twoStr = "",
std::string  threeStr = "" 
)

Definition at line 2283 of file OTStorage.cpp.

2286 {
2287  return onStorePlainString(strContents, strFolder, oneStr, twoStr, threeStr);
2288 }
virtual bool onStorePlainString(std::string &theBuffer, std::string strFolder, std::string oneStr="", std::string twoStr="", std::string threeStr="")=0
bool opentxs::OTDB::Storage::StoreString ( std::string  strContents,
std::string  strFolder,
std::string  oneStr = "",
std::string  twoStr = "",
std::string  threeStr = "" 
)

Definition at line 2203 of file OTStorage.cpp.

2206 {
2207  OTString ot_strFolder(strFolder), ot_oneStr(oneStr), ot_twoStr(twoStr),
2208  ot_threeStr(threeStr);
2209  OT_ASSERT_MSG(ot_strFolder.Exists(),
2210  "Storage::StoreString: strFolder is null");
2211 
2212  if (!ot_oneStr.Exists()) {
2213  OT_ASSERT_MSG((!ot_twoStr.Exists() && !ot_threeStr.Exists()),
2214  "Storage::StoreString: bad options");
2215  oneStr = strFolder;
2216  strFolder = ".";
2217  }
2218 
2219  OTPacker* pPacker = GetPacker();
2220 
2221  if (nullptr == pPacker) return false;
2222 
2223  PackedBuffer* pBuffer = pPacker->Pack(strContents);
2224 
2225  if (nullptr == pBuffer) return false;
2226 
2227  bool bSuccess =
2228  onStorePackedBuffer(*pBuffer, strFolder, oneStr, twoStr, threeStr);
2229 
2230  // Don't want any leaks here, do we?
2231  delete pBuffer;
2232  pBuffer = nullptr;
2233 
2234  return bSuccess;
2235 }
#define OT_ASSERT_MSG(x, s)
Definition: Assert.hpp:155
virtual bool onStorePackedBuffer(PackedBuffer &theBuffer, std::string strFolder, std::string oneStr="", std::string twoStr="", std::string threeStr="")=0
EXPORT OTPacker * GetPacker(PackType ePackType=OTDB_DEFAULT_PACKER)
Definition: OTStorage.cpp:2103

The documentation for this class was generated from the following files: