1 #ifndef DBAPI_DRIVER___MEMORY_STORE__HPP
2 #define DBAPI_DRIVER___MEMORY_STORE__HPP
3 
4 /*  $Id: memory_store.hpp 498292 2016-04-14 19:07:55Z ucko $
5  * ===========================================================================
6  *
7  *                            PUBLIC DOMAIN NOTICE
8  *               National Center for Biotechnology Information
9  *
10  *  This software/database is a "United States Government Work" under the
11  *  terms of the United States Copyright Act.  It was written as part of
12  *  the author's official duties as a United States Government employee and
13  *  thus cannot be copyrighted.  This software/database is freely available
14  *  to the public for use. The National Library of Medicine and the U.S.
15  *  Government have not placed any restriction on its use or reproduction.
16  *
17  *  Although all reasonable efforts have been taken to ensure the accuracy
18  *  and reliability of the software and data, the NLM and the U.S.
19  *  Government do not and cannot warrant the performance or results that
20  *  may be obtained by using this software or data. The NLM and the U.S.
21  *  Government disclaim all warranties, express or implied, including
22  *  warranties of performance, merchantability or fitness for any particular
23  *  purpose.
24  *
25  *  Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * File Name:  memory_store.cpp
30  *
31  * Author:  Vladimir Soussov
32  *
33  * File Description:  RAM storage
34  *
35  */
36 
37 
38 #include <corelib/ncbiobj.hpp>
39 #include <corelib/ncbi_limits.h>
40 
41 
42 BEGIN_NCBI_SCOPE
43 
44 static size_t kMax_BlobSize= (size_t) kMax_Int;
45 
46 // The storage with the sequential access
47 class C_SA_Storage : public CObject
48 {
49 public:
50     virtual size_t  Read  (void*       buff, size_t nof_bytes) = 0;
51     virtual size_t  Append(const void* buff, size_t nof_bytes) = 0;
52     virtual void    Flush (void) = 0;
53 
~C_SA_Storage()54     virtual ~C_SA_Storage() {}
55 };
56 
57 
58 // Random access storage
59 class C_RA_Storage : public C_SA_Storage
60 {
61 public:
62     enum EWhence {
63         eCurr,
64         eHead,
65         eTail
66     };
67 
68     virtual long   Seek (long offset, EWhence whence) = 0;
69     virtual size_t Tell (void) const = 0;
70     virtual size_t Write(const void* buff, size_t nof_bytes) = 0;
71 
~C_RA_Storage()72     virtual ~C_RA_Storage() {}
73 };
74 
75 
76 // Full access storage (allows insert and delete)
77 class C_FA_Storage : public C_RA_Storage
78 {
79 public:
80     virtual size_t Insert  (const void* buff, size_t nof_bytes) = 0;
81     virtual size_t Delete  (size_t nof_bytes) = 0;
82     virtual size_t Truncate(size_t nof_bytes) = 0;
83 
~C_FA_Storage()84     virtual ~C_FA_Storage() {}
85 };
86 
87 
88 
89 
90 class CMemStore : public C_FA_Storage
91 {
92 public:
CMemStore()93     CMemStore() { x_Init(); }
CMemStore(size_t block_size)94     CMemStore(size_t block_size) { x_Init((TSize) block_size); }
95     CMemStore(C_SA_Storage& storage, size_t block_size = 2048);
96 
97     ~CMemStore();
98 
99     size_t Read        (void*       buff, size_t nof_bytes);
100     size_t Peek        (void*       buff, size_t nof_bytes) const;
101     size_t PeekAt      (void*       buff, size_t start, size_t n) const;
102     size_t Append      (const void* buff, size_t nof_bytes);
103     size_t Write       (const void* buff, size_t nof_bytes);
104     size_t Insert      (const void* buff, size_t nof_bytes);
105 
106     size_t Delete      (size_t nof_bytes = kMax_BlobSize);
107     size_t Truncate    (size_t nof_bytes = kMax_BlobSize);
108 
Flush(void)109     void   Flush       (void)  { return; };
110     long   Seek        (long offset, EWhence whence);
Tell() const111     size_t Tell        () const  { return (size_t) m_Pos; }
GetDataSize() const112     size_t GetDataSize () const  { return (size_t) m_Size; }
113 
114     typedef long TSize;
115 
116 private:
117     struct SMemBlock
118     {
119         SMemBlock* next;
120         SMemBlock* prev;
121         TSize      free_space;
122         char*      body;
123     };
124 
125     TSize      m_BlockSize;
126     SMemBlock* m_First;
127     SMemBlock* m_Last;
128     SMemBlock* m_Current;
129     TSize      m_Pos;
130     TSize      m_BlockPos;
131     TSize      m_Size;
132 
133     SMemBlock* x_InsertBlock(void);
134     SMemBlock* x_AddBlock(void);
135     TSize      x_SeekHEAD(TSize offset);
136     TSize      x_SeekCURR(TSize offset);
137     TSize      x_SeekTAIL(TSize offset);
138 
x_Init(TSize block_size=2048)139     void x_Init(TSize block_size = 2048) {
140         m_BlockSize = (block_size > 16) ? block_size : 2048;
141         m_First = m_Last = m_Current = 0;
142         m_Pos = m_BlockPos = m_Size = 0;
143     };
144 };
145 
146 
147 END_NCBI_SCOPE
148 
149 
150 #endif  /* DBAPI_DRIVER___MEMORY_STORE__HPP */
151