Skip to content

Commit d8c4b6d

Browse files
committed
push code
1 parent 0a5fde3 commit d8c4b6d

29 files changed

+452
-69
lines changed

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ help:
33
@echo "make debug Build crossdb library and tool with debug"
44
@echo "make run Run crossdb tool"
55
@echo "make clean Clean build result"
6-
@echo "make install Install crossdb(lib&tool&header) to Linux & FreeBSD"
7-
@echo "make uninstall Uninstall crossdb from Linux & FreeBSD"
6+
@echo "make install Install crossdb(lib&tool&header) to Linux/FreeBSD"
7+
@echo "make uninstall Uninstall crossdb from Linux/FreeBSD"
88
@echo "make installmacos Install crossdb(lib&tool&header) to MacOS"
99
@echo "make uninstallmacos Uninstall crossdb from MacOS"
1010
@echo "make example Build and run example (need to install crossdb first)"
@@ -65,4 +65,4 @@ example:
6565

6666
.PHONY: bench
6767
bench:
68-
make -C bench/c/
68+
make -C bench/c/

README.md

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,22 @@
99

1010
> **NOTE**
1111
> This project was redesigned and rewritten from scratch.
12-
> It's still in early development stage, so please **DO NOT** use in your project now.
12+
> It's still in early development stage, so please DO NOT use in your project now.
1313
1414
# Introduction
1515

16-
**CrossDB** is a super high-performance embedded and server RDBMS.
17-
It's developed for high performance scenarios with main memory can hold whole DB.
16+
**CrossDB** is a super high-performance lightweight embedded and server SQL RDBMS.
17+
It's developed for high performance scenarios with main memory can hold whole DB.
1818

1919
## Features
2020

2121
- Support Multiple OS Platforms: Linux/Windows/MacOS/FreeBSD etc
2222
- Support Multiple CPU ARCH: X86/ARM/PPC/MIPS etc
2323
- Support OnDisk/In-memory/RamDisk/Hybrid Storage
2424
- Support Standard RDBMS model
25-
- Support MySQL like SQL
25+
- Support Standard SQL and many extensions from MySQL
2626
- Support Multiple databases
27+
- Support Embedded and Client-Server mode(TBD)
2728
- Support Primary Key and multiple Secondary Indexes
2829
- Support HASH and RBTREE(TBD) Index
2930
- Support Multi-columns Index
@@ -33,7 +34,7 @@ It's developed for high performance scenarios with main memory can hold whole DB
3334
- Support Multiple Threads and Multiple Processes Access
3435
- Support Table level read-write lock
3536
- Support Reader-Writer MVCC
36-
- Support Embedded CrossDB Shell
37+
- Support Embedded CrossDB Shell with convenient auto-completion
3738
- Support Multi-Statments APIs
3839
- Support Prepared Statments APIs
3940
- Super High Performance
@@ -42,31 +43,44 @@ It's developed for high performance scenarios with main memory can hold whole DB
4243

4344
## Use Cases
4445

45-
- You can use CrossDB In-Memory DB to manage Process Runtime Data to replace STL or hand-wrting data structures.
46+
- High-frenquency trade (OLTP)
47+
- High-peformance query
48+
- High-peformance data manamgent: You can use CrossDB OnDisk DB to store data on Disk/Flash/SDD.
49+
- High-peformance IMDB: You can use CrossDB In-Memory DB to manage Process Runtime Data to replace STL or hand-written data structures.
4650
- You can use CrossDB RamDisk DB to support Process Restartability, In-Service Software Upgrade(ISSU) easily.
47-
- You can use CrossDB OnDisk DB to store data on Disk/Flash/SDD.
4851
- You can use CrossDB to work as a super fast cache DB.
4952

5053
## Build and Install
5154

55+
### Linux/FreeBSD
56+
57+
```bash
58+
make build
59+
sudo make install
5260
```
53-
make
54-
make install
61+
62+
### MacOS
63+
64+
```bash
65+
make build
66+
sudo make installmacos
5567
```
5668

57-
## Contribution
58-
In order to keep CrossDB quality and high-performance, this project does not
59-
accept patches.
69+
### Windows
6070

61-
If you would like to suggest a change and you include a patch as a proof-of-
62-
concept, that would be great.
71+
You need to install [MINGW64](https://www.mingw-w64.org/) to build.
72+
Then set `gcc` path to `system environment variables` `Path` and make sure `gcc` can run.
6373

64-
However, please do not be offended if we rewrite your patch from scratch.
74+
```
75+
build.bat
76+
```
77+
78+
## Contribution
6579

6680
Following contributions are welcome:
6781

6882
- Language bindings: `Python`, `Java`, `Go`, `CSharp`, `Javascript`, `PHP`, etc
6983
- Test and report bugs
7084

7185
## Want to Lean More?
72-
https://crossdb.org
86+
https://crossdb.org

bench/c/bench.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ static void ts_beg() { s_last_ts = timestamp_us (); }
1111
static void ts_end (int count)
1212
{
1313
s_last_ts = timestamp_us() - s_last_ts;
14-
printf ("Use time %uns, QPS %d\n", (uint32_t)s_last_ts, (int)((uint64_t)count*1000000/s_last_ts));
14+
printf ("Use time %uus, QPS %d\n", (uint32_t)s_last_ts, (int)((uint64_t)count*1000000/s_last_ts));
1515
}
1616

1717
int main (int argc, char **argv)
@@ -107,8 +107,8 @@ int main (int argc, char **argv)
107107
pRow = xdb_fetch_row (pRes);
108108
if (NULL != pRow) {
109109
count++;
110-
xdb_free_result (pRes);
111110
}
111+
xdb_free_result (pRes);
112112
}
113113
ts_end(LKUP_COUNT);
114114
if (count != LKUP_COUNT) {
@@ -153,4 +153,4 @@ int main (int argc, char **argv)
153153
xdb_close (pConn);
154154

155155
return 0;
156-
}
156+
}

build.cmd

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
IF not exist build (mkdir build)
2+
gcc -o build/libcrossdb.dll src/crossdb.c -fPIC -shared -lws2_32 -lpthread -static -O2 -Wl,--out-implib,build/libcrossdb.lib
3+
gcc -o build/crossdb src/main.c -lws2_32 -lpthread -static -O2
4+
copy /Y src\crossdb.h build\
5+
xcopy /ehiqy build\* c:\crossdb\

examples/c/example.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ int main (int argc, char **argv)
159159
xdb_free_result (pRes);
160160
}
161161

162+
// Embedded shell
162163
xdb_exec (pConn, "SHELL");
163164

164165
error:

src/core/xdb_cfg.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,9 @@
3636

3737
#define XDB_PATH_LEN 256
3838

39-
#endif // __CROSS_CFG_H__
39+
40+
#ifndef XDB_ENABLE_SERVER
41+
#define XDB_ENABLE_SERVER 0
42+
#endif
43+
44+
#endif // __CROSS_CFG_H__

src/core/xdb_common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ typedef int xdb_rowid;
4545
pConn->conn_res.row_data = (uintptr_t)pConn->conn_msg.msg; \
4646
pConn->conn_msg.len = snprintf(pConn->conn_msg.msg, sizeof(pConn->conn_msg.msg)-1, errmsgfmt); \
4747
pConn->conn_msg.len_type = (XDB_RET_MSG<<28) | pConn->conn_msg.len; \
48+
pConn->conn_res.data_len = pConn->conn_msg.len;
4849

4950
#define XDB_EXPECT_BRK(expr, code, errmsgfmt...) \
5051
if (!(expr)) { \

src/core/xdb_conn.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ typedef struct xdb_conn_t {
7575
char *pNxtSql;
7676
char sql_buf[4096];
7777

78-
#ifdef XDB_ENABLE_SERVER
78+
#if (XDB_ENABLE_SERVER == 1)
7979
xdb_server_t *pServer;
8080
#endif
8181
xdb_rowset_t row_set;

src/core/xdb_db.c

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,17 +135,17 @@ xdb_create_db (xdb_stmt_db_t *pStmt)
135135
pDbm->bMemory = pStmt->bMemory;
136136

137137
xdb_stghdr_t stg_hdr = {.stg_magic = 0xE7FCFDFB, .blk_flags=1, .blk_size = sizeof(xdb_dbobj_t),
138-
.ctl_off = 0, .blk_off = XDB_OFFSET(xdb_dbHdr_t, dbobj)};
138+
.ctl_off = 0, .blk_off = XDB_OFFSET(xdb_db_t, dbobj)};
139139
pDbm->stg_mgr.pOps = pDbm->bMemory ? &s_xdb_store_mem_ops : &s_xdb_store_file_ops;
140140
pDbm->stg_mgr.pStgHdr = &stg_hdr;
141141
int rc = xdb_stg_open (&pDbm->stg_mgr, path, NULL, NULL);
142142
pDbm->lock_mode = pStmt->lock_mode;
143143

144-
xdb_dbHdr_t *pDbHdr = (xdb_dbHdr_t*)pDbm->stg_mgr.pStgHdr;
144+
xdb_db_t *pDb = (xdb_db_t*)pDbm->stg_mgr.pStgHdr;
145145
if (XDB_OK == rc) {
146-
pDbHdr->lock_mode = pStmt->lock_mode;
146+
pDb->lock_mode = pStmt->lock_mode;
147147
} else if (0 == pDbm->lock_mode) {
148-
pDbm->lock_mode = pDbHdr->lock_mode;
148+
pDbm->lock_mode = pDb->lock_mode;
149149
}
150150

151151
XDB_EXPECT (strlen(real_db_name) < sizeof (pDbm->db_path), XDB_E_PARAM, "Too long path");
@@ -299,4 +299,41 @@ xdb_gen_db_schema (xdb_dbm_t *pDbm)
299299
xdb_dump_db_schema (pDbm, file);
300300
}
301301
return 0;
302-
}
302+
}
303+
304+
XDB_STATIC int
305+
xdb_flush_db (xdb_dbm_t *pDbm, uint32_t flags)
306+
{
307+
#if (XDB_ENABLE_WAL == 1)
308+
// Switch wal if has commit, then flush tables, afterward backup wal can be recycled
309+
bool bSwitch = xdb_wal_switch (pDbm);
310+
#endif
311+
312+
// flush tables
313+
int count = XDB_OBJM_MAX(pDbm->db_objm);
314+
for (int i = 0; i < count; ++i) {
315+
xdb_tblm_t *pTblm = XDB_OBJM_GET(pDbm->db_objm, i);
316+
if (NULL != pTblm) {
317+
xdb_flush_table (pTblm, flags);
318+
}
319+
}
320+
321+
#if (XDB_ENABLE_WAL == 1)
322+
// flush backup wal if switched
323+
if (bSwitch) {
324+
xdb_wal_wrlock (pDbm);
325+
__xdb_wal_flush (pDbm->pWalmBak, false);
326+
if (pDbm->pWalm->pWal->commit_size > sizeof (xdb_wal_t)) {
327+
// Mark for next round flush
328+
pDbm->db_dirty = true;
329+
}
330+
xdb_wal_wrunlock (pDbm);
331+
332+
// fsync may take long time, so do it out of wal lock
333+
xdb_stg_sync (pDbm->pWalm, 0, 0, false);
334+
xdb_stg_sync (pDbm->pWalmBak, 0, 0, false);
335+
}
336+
#endif
337+
338+
return 0;
339+
}

src/core/xdb_db.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,38 @@
2121

2222
typedef struct xdb_dbm_t {
2323
xdb_obj_t obj;
24-
struct xdb_dbHdr_t *pDb;
2524
char db_path[XDB_PATH_LEN+1];
2625
xdb_objm_t db_objm;
2726
xdb_stgmgr_t stg_mgr;
2827
bool bMemory;
2928
bool bSysDb;
3029
xdb_lockmode_t lock_mode;
30+
bool db_dirty;
31+
32+
#ifdef XDB_EANBLE_WAL
33+
xdb_walm_t wal_mgmt1;
34+
xdb_walm_t wal_mgmt2;
35+
xdb_walm_t *pWalm;
36+
xdb_walm_t *pWalmBak;
37+
xdb_walrow_t *pWalRow;
38+
#endif
39+
40+
xdb_rwlock_t wal_lock;
3141
} xdb_dbm_t;
3242

3343
typedef struct xdb_dbobj_t {
3444
uint64_t val[8];
3545
} xdb_dbobj_t;
3646

37-
typedef struct xdb_dbHdr_t {
47+
typedef struct xdb_db_t {
3848
xdb_stghdr_t blk_hdr;
3949
uint64_t lsn;
4050
uint8_t lock_mode; // xdb_lockmode_t
4151
uint8_t rsvd[7];
4252
xdb_dbobj_t dbobj[];
43-
} xdb_dbHdr_t;
53+
} xdb_db_t;
4454

4555
XDB_STATIC xdb_dbm_t*
4656
xdb_find_db (const char *name);
4757

48-
#endif // __XDB_DB_H__
58+
#endif // __XDB_DB_H__

0 commit comments

Comments
 (0)