]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
11fdf7f2 TL |
2 | // This source code is licensed under both the GPLv2 (found in the |
3 | // COPYING file in the root directory) and Apache 2.0 License | |
4 | // (found in the LICENSE.Apache file in the root directory). | |
7c673cae FG |
5 | #pragma once |
6 | #ifndef ROCKSDB_LITE | |
7 | ||
8 | #include <deque> | |
9 | #include <map> | |
10 | #include <memory> | |
11 | #include <string> | |
12 | #include <unordered_map> | |
13 | #include <utility> | |
14 | #include <vector> | |
15 | ||
16 | #include "rocksdb/slice.h" | |
17 | ||
18 | // We use JSONDocument for DocumentDB API | |
19 | // Implementation inspired by folly::dynamic, rapidjson and fbson | |
20 | ||
21 | namespace fbson { | |
22 | class FbsonValue; | |
23 | class ObjectVal; | |
24 | template <typename T> | |
25 | class FbsonWriterT; | |
26 | class FbsonOutStream; | |
27 | typedef FbsonWriterT<FbsonOutStream> FbsonWriter; | |
28 | } // namespace fbson | |
29 | ||
30 | namespace rocksdb { | |
31 | ||
32 | // NOTE: none of this is thread-safe | |
33 | class JSONDocument { | |
34 | public: | |
35 | // return nullptr on parse failure | |
36 | static JSONDocument* ParseJSON(const char* json); | |
37 | ||
38 | enum Type { | |
39 | kNull, | |
40 | kArray, | |
41 | kBool, | |
42 | kDouble, | |
43 | kInt64, | |
44 | kObject, | |
45 | kString, | |
46 | }; | |
47 | ||
48 | /* implicit */ JSONDocument(); // null | |
49 | /* implicit */ JSONDocument(bool b); | |
50 | /* implicit */ JSONDocument(double d); | |
51 | /* implicit */ JSONDocument(int8_t i); | |
52 | /* implicit */ JSONDocument(int16_t i); | |
53 | /* implicit */ JSONDocument(int32_t i); | |
54 | /* implicit */ JSONDocument(int64_t i); | |
55 | /* implicit */ JSONDocument(const std::string& s); | |
56 | /* implicit */ JSONDocument(const char* s); | |
57 | // constructs JSONDocument of specific type with default value | |
58 | explicit JSONDocument(Type _type); | |
59 | ||
60 | JSONDocument(const JSONDocument& json_document); | |
61 | ||
62 | JSONDocument(JSONDocument&& json_document); | |
63 | ||
64 | Type type() const; | |
65 | ||
66 | // REQUIRES: IsObject() | |
67 | bool Contains(const std::string& key) const; | |
68 | // REQUIRES: IsObject() | |
69 | // Returns non-owner object | |
70 | JSONDocument operator[](const std::string& key) const; | |
71 | ||
72 | // REQUIRES: IsArray() == true || IsObject() == true | |
73 | size_t Count() const; | |
74 | ||
75 | // REQUIRES: IsArray() | |
76 | // Returns non-owner object | |
77 | JSONDocument operator[](size_t i) const; | |
78 | ||
79 | JSONDocument& operator=(JSONDocument jsonDocument); | |
80 | ||
81 | bool IsNull() const; | |
82 | bool IsArray() const; | |
83 | bool IsBool() const; | |
84 | bool IsDouble() const; | |
85 | bool IsInt64() const; | |
86 | bool IsObject() const; | |
87 | bool IsString() const; | |
88 | ||
89 | // REQUIRES: IsBool() == true | |
90 | bool GetBool() const; | |
91 | // REQUIRES: IsDouble() == true | |
92 | double GetDouble() const; | |
93 | // REQUIRES: IsInt64() == true | |
94 | int64_t GetInt64() const; | |
95 | // REQUIRES: IsString() == true | |
96 | std::string GetString() const; | |
97 | ||
98 | bool operator==(const JSONDocument& rhs) const; | |
99 | ||
100 | bool operator!=(const JSONDocument& rhs) const; | |
101 | ||
102 | JSONDocument Copy() const; | |
103 | ||
104 | bool IsOwner() const; | |
105 | ||
106 | std::string DebugString() const; | |
107 | ||
108 | private: | |
109 | class ItemsIteratorGenerator; | |
110 | ||
111 | public: | |
112 | // REQUIRES: IsObject() | |
113 | ItemsIteratorGenerator Items() const; | |
114 | ||
115 | // appends serialized object to dst | |
116 | void Serialize(std::string* dst) const; | |
117 | // returns nullptr if Slice doesn't represent valid serialized JSONDocument | |
118 | static JSONDocument* Deserialize(const Slice& src); | |
119 | ||
120 | private: | |
121 | friend class JSONDocumentBuilder; | |
122 | ||
123 | JSONDocument(fbson::FbsonValue* val, bool makeCopy); | |
124 | ||
125 | void InitFromValue(const fbson::FbsonValue* val); | |
126 | ||
127 | // iteration on objects | |
128 | class const_item_iterator { | |
129 | private: | |
130 | class Impl; | |
131 | public: | |
132 | typedef std::pair<std::string, JSONDocument> value_type; | |
133 | explicit const_item_iterator(Impl* impl); | |
134 | const_item_iterator(const_item_iterator&&); | |
135 | const_item_iterator& operator++(); | |
136 | bool operator!=(const const_item_iterator& other); | |
137 | value_type operator*(); | |
138 | ~const_item_iterator(); | |
139 | private: | |
140 | friend class ItemsIteratorGenerator; | |
141 | std::unique_ptr<Impl> it_; | |
142 | }; | |
143 | ||
144 | class ItemsIteratorGenerator { | |
145 | public: | |
146 | explicit ItemsIteratorGenerator(const fbson::ObjectVal& object); | |
147 | const_item_iterator begin() const; | |
148 | ||
149 | const_item_iterator end() const; | |
150 | ||
151 | private: | |
152 | const fbson::ObjectVal& object_; | |
153 | }; | |
154 | ||
155 | std::unique_ptr<char[]> data_; | |
156 | mutable fbson::FbsonValue* value_; | |
157 | ||
158 | // Our serialization format's first byte specifies the encoding version. That | |
159 | // way, we can easily change our format while providing backwards | |
160 | // compatibility. This constant specifies the current version of the | |
161 | // serialization format | |
162 | static const char kSerializationFormatVersion; | |
163 | }; | |
164 | ||
165 | class JSONDocumentBuilder { | |
166 | public: | |
167 | JSONDocumentBuilder(); | |
168 | ||
169 | explicit JSONDocumentBuilder(fbson::FbsonOutStream* out); | |
170 | ||
171 | void Reset(); | |
172 | ||
173 | bool WriteStartArray(); | |
174 | ||
175 | bool WriteEndArray(); | |
176 | ||
177 | bool WriteStartObject(); | |
178 | ||
179 | bool WriteEndObject(); | |
180 | ||
181 | bool WriteKeyValue(const std::string& key, const JSONDocument& value); | |
182 | ||
183 | bool WriteJSONDocument(const JSONDocument& value); | |
184 | ||
185 | JSONDocument GetJSONDocument(); | |
186 | ||
187 | ~JSONDocumentBuilder(); | |
188 | ||
189 | private: | |
190 | std::unique_ptr<fbson::FbsonWriter> writer_; | |
191 | }; | |
192 | ||
193 | } // namespace rocksdb | |
194 | ||
195 | #endif // ROCKSDB_LITE |