|
27 | 27 |
|
28 | 28 | #include "leveldb_terark.h"
|
29 | 29 | #include <errno.h>
|
30 |
| -#include <sys/stat.h> |
31 |
| -#include <unistd.h> |
| 30 | +#if defined(_WIN32) || defined(_WIN64) |
| 31 | +#else |
| 32 | + #include <sys/stat.h> |
| 33 | + #include <unistd.h> |
| 34 | +#endif |
32 | 35 | #include <sstream>
|
33 | 36 |
|
34 | 37 | using leveldb::Cache;
|
@@ -77,59 +80,42 @@ DB::ListColumnFamilies(
|
77 | 80 | std::vector<std::string> *column_families)
|
78 | 81 | {
|
79 | 82 | std::vector<std::string> cf;
|
80 |
| - DB *dbptr; |
81 |
| - Status status = DB::Open(options, name, &dbptr); |
82 |
| - if (!status.ok()) |
83 |
| - return status; |
84 |
| - DbImpl *db = static_cast<DbImpl *>(dbptr); |
85 |
| - OperationContext *context = db->GetContext(); |
86 |
| - WT_SESSION *session = context->GetSession(); |
87 |
| - WT_CURSOR *c; |
88 |
| - int ret = session->open_cursor(session, "metadata:", NULL, NULL, &c); |
89 |
| - if (ret != 0) |
90 |
| - goto err; |
91 |
| - c->set_key(c, "table:"); |
92 |
| - /* Position on the first table entry */ |
93 |
| - int cmp; |
94 |
| - ret = c->search_near(c, &cmp); |
95 |
| - if (ret != 0 || (cmp < 0 && (ret = c->next(c)) != 0)) |
96 |
| - goto err; |
97 |
| - /* Add entries while we are getting "table" URIs. */ |
98 |
| - for (; ret == 0; ret = c->next(c)) { |
99 |
| - const char *key; |
100 |
| - if ((ret = c->get_key(c, &key)) != 0) |
101 |
| - goto err; |
102 |
| - if (strncmp(key, "table:", strlen("table:")) != 0) |
103 |
| - break; |
104 |
| - printf("List column families: [%d] = %s\n", (int)cf.size(), key); |
105 |
| - cf.push_back(std::string(key + strlen("table:"))); |
| 83 | + fs::path dbdir = fs::path(name) / "TerarkDB"; |
| 84 | + fs::path metaPath = dbdir / "dbmeta.json"; |
| 85 | + if (!fs::exists(metaPath)) { |
| 86 | + fprintf(stderr, "ERROR: not exists: %s\n", metaPath.string().c_str()); |
| 87 | + return Status::InvalidArgument("ListColumnFamilies: dbmeta.json is missing", dbdir.string()); |
106 | 88 | }
|
107 |
| - |
108 |
| -err: delete db; |
109 |
| - /* |
110 |
| - * WT_NOTFOUND is not an error: it just means we got to the end of the |
111 |
| - * list of tables. |
112 |
| - */ |
113 |
| - if (ret == 0 || ret == WT_NOTFOUND) { |
114 |
| - *column_families = cf; |
115 |
| - ret = 0; |
| 89 | + try { |
| 90 | + terark::db::SchemaConfigPtr sconf = new terark::db::SchemaConfig(); |
| 91 | + sconf->loadJsonFile(metaPath.string()); |
| 92 | + column_families->resize(0); |
| 93 | + size_t cgNum = sconf->getColgroupNum(); |
| 94 | + for (size_t i = 0; i < cgNum; ++i) { |
| 95 | + const terark::db::Schema& schema = sconf->getColgroupSchema(i); |
| 96 | + column_families->push_back(schema.m_name); |
| 97 | + } |
| 98 | + } |
| 99 | + catch (const std::exception& ex) { |
| 100 | + return Status::InvalidArgument("ListColumnFamilies: load dbmeta.json failed", dbdir.string()); |
116 | 101 | }
|
117 |
| - return WiredTigerErrorToStatus(ret); |
118 | 102 | }
|
119 | 103 |
|
120 | 104 | Status
|
121 |
| -DB::Open(Options const &options, std::string const &name, const std::vector<ColumnFamilyDescriptor> &column_families, std::vector<ColumnFamilyHandle*> *handles, DB**dbptr) |
| 105 | +DB::Open(Options const &options, |
| 106 | + std::string const &name, |
| 107 | + const std::vector<ColumnFamilyDescriptor> &column_families, |
| 108 | + std::vector<ColumnFamilyHandle*> *handles, |
| 109 | + DB**dbptr) |
122 | 110 | {
|
123 | 111 | Status status = Open(options, name, dbptr);
|
124 | 112 | if (!status.ok())
|
125 | 113 | return status;
|
126 |
| - DbImpl *db = static_cast<DbImpl *>(*dbptr); |
127 |
| - std::vector<ColumnFamilyHandle*> cfhandles( |
128 |
| - column_families.size()); |
| 114 | + DbImpl *db = static_cast<DbImpl*>(*dbptr); |
| 115 | + std::vector<ColumnFamilyHandle*> cfhandles(column_families.size()); |
129 | 116 | for (size_t i = 0; i < column_families.size(); i++) {
|
130 | 117 | printf("Open column families: [%d] = %s\n", (int)i, column_families[i].name.c_str());
|
131 |
| - cfhandles[i] = new ColumnFamilyHandleImpl( |
132 |
| - db, column_families[i].name, (int)i); |
| 118 | + cfhandles[i] = new ColumnFamilyHandleImpl(db, column_families[i].name, i); |
133 | 119 | }
|
134 | 120 | db->SetColumns(*handles = cfhandles);
|
135 | 121 | return Status::OK();
|
|
0 commit comments