Debian Codegraph @

src/lib/object_store/DBObject.h

raw
1 /*
2 * Copyright (c) 2013 SURFnet bv
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 /*****************************************************************************
28 DBObject.h
29
30 This class represents object records in a database
31 *****************************************************************************/
32
33 #ifndef _SOFTHSM_V2_DBOBJECT_H
34 #define _SOFTHSM_V2_DBOBJECT_H
35
36 #include "config.h"
37 #include "OSAttribute.h"
38 #include "cryptoki.h"
39 #include "OSObject.h"
40 #include "ObjectStoreToken.h"
41
42 #include "MutexFactory.h"
43 #include <string>
44
45 namespace DB { class Connection; }
46
47 class DBObject : public OSObject
48 {
49 public:
50 // Constructor for creating or accessing an object, don't do anything yet.
51 DBObject(DB::Connection *connection, ObjectStoreToken *token = NULL);
52
53 // Constructor for accessing an object with an objectId known to exists
54 DBObject(DB::Connection *connection, ObjectStoreToken *token, long long objectId);
55
56 // Destructor
57 virtual ~DBObject();
58
59 // Will drop any internal references to the connection
60 void dropConnection();
61
62 // create tables to support storage of attributes for the object.
63 bool createTables();
64
65 // drop tables that support storage of attributes for the object.
66 bool dropTables();
67
68 // Find an existing object.
69 bool find(long long objectId);
70
71 // Insert a new object into the database and retrieve the object id associated with it.
72 bool insert();
73
74 // Remove an existing object from the database and reset the object id to zero.
75 bool remove();
76
77 // Object id associated with this object.
78 long long objectId();
79
80 // Check if the specified attribute exists
81 virtual bool attributeExists(CK_ATTRIBUTE_TYPE type);
82
83 // Retrieve the specified attribute
84 virtual OSAttribute getAttribute(CK_ATTRIBUTE_TYPE type);
85 virtual bool getBooleanValue(CK_ATTRIBUTE_TYPE type, bool val);
86 virtual unsigned long getUnsignedLongValue(CK_ATTRIBUTE_TYPE type, unsigned long val);
87 virtual ByteString getByteStringValue(CK_ATTRIBUTE_TYPE type);
88
89 // Retrieve the next attribute type
90 virtual CK_ATTRIBUTE_TYPE nextAttributeType(CK_ATTRIBUTE_TYPE type);
91
92 // Set the specified attribute
93 virtual bool setAttribute(CK_ATTRIBUTE_TYPE type, const OSAttribute& attribute);
94
95 // Delete the specified attribute
96 virtual bool deleteAttribute(CK_ATTRIBUTE_TYPE type);
97
98 // The validity state of the object
99 virtual bool isValid();
100
101 // Start an attribute set transaction; this method is used when - for
102 // example - a key is generated and all its attributes need to be
103 // persisted in one go.
104 //
105 // N.B.: Starting a transaction locks the object!
106 //
107 // Function returns false in case a transaction is already in progress
108 virtual bool startTransaction(Access access);
109
110 // Commit an attribute transaction; returns false if no transaction is in progress
111 virtual bool commitTransaction();
112
113 // Abort an attribute transaction; loads back the previous version of the object from disk;
114 // returns false if no transaction was in progress
115 virtual bool abortTransaction();
116
117 // Destroys the object (warning, any pointers to the object are no longer
118 // valid after this call because delete is called!)
119 virtual bool destroyObject();
120
121 private:
122 // Disable copy constructor and assignment
123 DBObject();
124 DBObject(const DBObject&);
125 DBObject & operator= (const DBObject &);
126
127 // Mutex object for thread-safeness
128 Mutex* _mutex;
129
130 DB::Connection *_connection;
131 ObjectStoreToken *_token;
132 long long _objectId;
133
134 std::map<CK_ATTRIBUTE_TYPE,OSAttribute*> _attributes;
135 std::map<CK_ATTRIBUTE_TYPE,OSAttribute*> *_transaction;
136
137 OSAttribute* getAttributeDB(CK_ATTRIBUTE_TYPE type);
138 OSAttribute* accessAttribute(CK_ATTRIBUTE_TYPE type);
139 };
140
141 #endif // !_SOFTHSM_V2_DBOBJECT_H
142