Skip to content

Commit

Permalink
Initial push for refactor new CrossDB
Browse files Browse the repository at this point in the history
  • Loading branch information
jcwangxp committed Aug 18, 2024
1 parent 75ce6bc commit 057e0b9
Show file tree
Hide file tree
Showing 67 changed files with 14,454 additions and 1,232 deletions.
19 changes: 14 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
# Change Log

## 0.5.0 <small>(2023-06-26)</small> {#0.5.0}
## 0.6.0 <small>(2024-08-15)</small>

- **Initial refactor release**

- This project was redesigned and rewritten from scratch
- Standard RDBMS model
- New APIs which can support more languages


## 0.5.0 <small>(2023-06-26)</small>

Features

Expand All @@ -12,7 +21,7 @@ Features
Bug Fixes


## 0.4.0 <small>(2023-06-20)</small> {#0.4.0}
## 0.4.0 <small>(2023-06-20)</small>

Features

Expand All @@ -23,7 +32,7 @@ Features
Bug Fixes


## 0.3.0 <small>(2023-06-13)</small> {#0.3.0}
## 0.3.0 <small>(2023-06-13)</small>

Features

Expand All @@ -35,7 +44,7 @@ Bug Fixes
- `cross_dbTblCreate` flags `CROSS_DB_RBTREE` doesn't create Primary Key Index type correctly


## 0.2.0 <small>(2023-06-07)</small> {#0.2.0}
## 0.2.0 <small>(2023-06-07)</small>

Features

Expand All @@ -45,7 +54,7 @@ Features
Bug Fixes


## 0.1.0 <small>(2023-06-03)</small> {#0.1.0}
## 0.1.0 <small>(2023-06-03)</small>

- **Initial release**

Expand Down
875 changes: 674 additions & 201 deletions LICENSE

Large diffs are not rendered by default.

67 changes: 67 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
help:
@echo "make build Build crossdb library and tool"
@echo "make debug Build crossdb library and tool with debug"
@echo "make run Run crossdb tool"
@echo "make clean Clean build result"
@echo "make install Install crossdb(lib&tool&header) to Linux & FreeBSD"
@echo "make uninstall Uninstall crossdb from Linux & FreeBSD"
@echo "make installmacos Install crossdb(lib&tool&header) to MacOS"
@echo "make uninstallmacos Uninstall crossdb from MacOS"
@echo "make example Build and run example (need to install crossdb first)"
@echo "make bench Build and run bench test (need to install crossdb first)"

.PHONY: build
build:
$(CC) -o build/libcrossdb.so -fPIC -shared -lpthread -O2 src/crossdb.c
$(CC) -o build/crossdb src/main.c -lpthread -O2
cp src/crossdb.h build/

debug:
$(CC) -o build/libcrossdb.so -fPIC -lpthread -shared -g src/crossdb.c
$(CC) -o build/crossdb src/main.c -lpthread -g
cp src/crossdb.h build/

run:
build/crossdb

clean:
rm -rf build/*
make -C examples/c/ clean
make -C bench/c/ clean

wall:
$(CC) -o build/libcrossdb.so -fPIC -shared -lpthread -O2 -Wall src/crossdb.c
$(CC) -o build/crossdb src/main.c -lpthread -O2 -Wall
cp src/crossdb.h build/

gdb:
$(CC) -o build/crossdb src/main.c -lpthread -g
gdb build/crossdb

install:
install -c build/libcrossdb.so /usr/lib/
install -c build/crossdb.h /usr/include/
install -c build/crossdb /usr/bin/

uninstall:
rm -rf /usr/lib/libcrossdb.so
rm -rf /usr/include/crossdb.h
rm -rf /usr/bin/crossdb

installmacos:
$(CC) -o build/libcrossdb.so -dynamiclib -lpthread -O2 src/crossdb.c
install -c build/libcrossdb.so /usr/local/lib/
install -c build/crossdb.h /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include
install -c build/crossdb /usr/local/bin/

uninstallmacos:
rm -rf /usr/local/lib/libcrossdb.so
rm -rf /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/crossdb.h
rm -rf /usr/local/bin/crossdb

example:
make -C examples/c/

.PHONY: bench
bench:
make -C bench/c/
216 changes: 47 additions & 169 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,189 +4,67 @@
</a>
</p>
<p align="center">
<strong>The Fastest Embedded Database in the world</strong>
<strong>Super High-performance Embedded and Server RDBMS</strong>
</p>

## [CrossDB vs. SQLite3 Benchmark](https://crossdb.org/blog/benchmark/crossdb-vs-sqlite3/)
> **NOTE**
> This project was redesigned and rewritten from scratch.
> It's still in early development stage, so please DO NOT use in your project now.
<a href="https://crossdb.org/blog/benchmark/crossdb-vs-sqlite3/">
<img src="https://crossdb.org/images/benchmark/crossdb-vs-sqlite-ramdisk-large.png">
</a>
# Introduction

## [CrossDB vs. CPP STL Map Benchmark](https://crossdb.org/blog/benchmark/crossdb-vs-stlmap/)
**CrossDB** is a super high-performance embedded and server RDBMS.
It's developed for high performance scenarios with main memory can hold whole DB.

<a href="https://crossdb.org/blog/benchmark/crossdb-vs-stlmap/">
<img src="https://crossdb.org/images/benchmark/crossdb-vs-stlmap-large.png">
</a>
## Features

## [CrossDB vs. CPP STL Hashmap Benchmark](https://crossdb.org/blog/benchmark/crossdb-vs-stlhmap/)
- Support Multiple OS Platforms: Linux/Windows/MacOS/FreeBSD etc
- Support Multiple CPU ARCH: X86/ARM/PPC/MIPS etc
- Support OnDisk/In-memory/RamDisk/Hybrid Storage
- Support Standard RDBMS model
- Support MySQL like SQL
- Support Multiple databases
- Support Primary Key and multiple Secondary Indexes
- Support HASH and RBTREE(TBD) Index
- Support Multi-columns Index
- Support Exact Match, Leftmost Match(TBD), Range Match(TBD)
- Support Standard ACID Transaction (begin/commit/rollback)
- Support WAL for OnDisk storage(TBD)
- Support Multiple Threads and Multiple Processes Access
- Support Table level read-write lock
- Support Reader-Writer MVCC
- Support Embedded CrossDB Shell
- Support Multi-Statments APIs
- Support Prepared Statments APIs
- Super High Performance
- Very Simple: Simple header and library file
- Zero Config: no complex config, real out-of-the-box

<a href="https://crossdb.org/blog/benchmark/crossdb-vs-stlhmap/">
<img src="https://crossdb.org/images/benchmark/crossdb-vs-stlhmap-large.png">
</a>
## Use Cases

## CrossDB CLI
- You can use CrossDB In-Memory DB to manage Process Runtime Data to replace STL or hand-wrting data structures.
- You can use CrossDB RamDisk DB to support Process Restartability, In-Service Software Upgrade(ISSU) easily.
- You can use CrossDB OnDisk DB to store data on Disk/Flash/SDD.
- You can use CrossDB to work as a super fast cache DB.

![img](https://crossdb.org/images/crossdb-cli.gif)
## Build and Install

## CrossDB Model
make
make install

![img](https://crossdb.org/images/crossdb-model.png)
## Contribution
In order to keep CrossDB quality and high-performance, this project does not
accept patches.

## Learn CrossDB in 5 Minutes
If you would like to suggest a change and you include a patch as a proof-of-
concept, that would be great.

### 🛶 Define Schema
However, please do not be offended if we rewrite your patch from scratch.

``` c linenums="1"
typedef struct {
uint32_t prefix;
uint8_t mask;
uint32_t nexthop;
uint8_t metric;
char intf[16];
uint32_t birth;
uint32_t flags;
} route_t;
Following contributions are welcome:

#undef CROSS_STRUCT_NAME
#define CROSS_STRUCT_NAME route_t
cross_field_t route_schema[] = {
CROSS_FIELD (prefix, UINT, IPv4, 0),
CROSS_FIELD (mask, UINT, DFT, 0),
CROSS_FIELD (nexthop, UINT, IPv4, 0),
CROSS_FIELD (metric, UINT, DFT, 0),
CROSS_FIELD (intf, CHAR, DFT, 0),
CROSS_FIELD (birth, UINT, TS, 0),
CROSS_FIELD (flags, UINT, HEX, 0),
CROSS_END (route_t)
};
```

### ⚙️ Create DB

``` c linenums="1"
#define CHECK(ret,str) if (ret < 0) { printf (str": %s\n", cross_errMsg(ret)); return -1; }

cross_db_h hDb;
cross_tbl_h hRtTbl;
cross_ret ret;
route_t route;
cross_rowid count;

// Create database
ret = cross_dbCreate (&hDb, "db_data/example", 0);
CHECK (ret, "Failed to create db: example");

// Create table: route (Primary Key: prefix,mask)
ret = cross_dbTblCreate (hDb, &hRtTbl, "route", route_schema, "prefix,mask", 0);
CHECK (ret, "Failed to create table: route table");

// Create index on nexthop: idx_nexthop
ret = cross_dbIdxCreate (hRtTbl, "idx_nexthop", "nexthop", 0);
CHECK (ret, "Failed to create index: idx_nexthop");
```
### ⚜️ Insert Rows
``` c linenums="1"
#define IP4ADDR(a,b,c,d) ((a)<<24|(b)<<16|(c)<<8|(d))
// Insert route 192.168.1.0/24->192.168.1.254
route.prefix = IP4ADDR(192,168,1,0);
route.mask = 24;
route.nexthop = IP4ADDR(192,168,1,254);
route.metric = 1;
route.flags = 0;
strcpy (route.intf, "eth1");
route.birth = time (NULL);
ret = cross_dbInsertRow (hRtTbl, &route, 0);
CHECK (ret, "Failed to insert route 192.168.1.0/24");
```

### 🚀 Query Rows

``` c linenums="1"
#define IP4STR(ip) ip>>24,(ip>>16)&0xff,(ip>>8)&0xff,ip&0xff

// Get single route 192.168.1.0/24 by Primary Key
route.prefix = IP4ADDR(192,168,1,0);
route.mask = 24;
ret = cross_dbGetRowByPK (hRtTbl, &route, &route, 0);
CHECK (ret, "Failed to get route 192.168.1.0/24 by Primary Key");
printf ("Get single route: %d.%d.%d.%d/%d->%d.%d.%d.%d intf: %s metric: %d flags: 0x%x\n",
IP4STR(route.prefix), route.mask, IP4STR(route.nexthop), route.intf, route.metric, route.flags);

// Get one row where nexthop=192.168.1.254
route.nexthop = IP4ADDR(192,168,1,254);
ret = cross_dbGetOneRow (hRtTbl, "nexthop", &route, &route, 0);
CHECK (ret, "Failed to get one route where nexthop=192.168.1.254");
printf ("Get one route where nexthop=192.168.1.254: "
"%d.%d.%d.%d/%d->%d.%d.%d.%d intf: %s metric: %d flags: 0x%x\n",
IP4STR(route.prefix), route.mask, IP4STR(route.nexthop), route.intf, route.metric, route.flags);
```
## 🔫 Update Rows
``` c linenums="1"
// Update single route 192.168.1.0/24 by Primary Key: set flags 0->1 metric 1->3
route.prefix = IP4ADDR(192,168,1,0);
route.mask = 24;
route.metric = 3;
route.flags = 1;
ret = cross_dbUpdRowByPK (hRtTbl, &route, "flags,metric", &route, 0);
CHECK (ret, "Failed to update route 192.168.1.0/24 by Primary Key");
// Update routes where nexthop=192.168.1.254: set flags 0->3
route.nexthop = IP4ADDR(192,168,1,254);
route.flags = 3;
count = cross_dbUpdateRows (hRtTbl, "nexthop", &route, "flags", &route, 0);
printf ("Update %d routes where nexthop=10.1.2.254\n", count);
```

## 🎡 Cursor Query

``` c linenums="1"
// Use cursor to get routes where nexthop=192.168.1.254
cross_cursor_h hCursor;
route.nexthop = IP4ADDR(192,168,1,254);
count = cross_dbQueryRows (hRtTbl, &hCursor, "nexthop", &route, 0);
printf ("Query %d routes where nexthop=192.168.1.254\n", count);
while (CROSS_OK == cross_cursorGetNextRow (hCursor, &route, 0)) {
printf (" route: %d.%d.%d.%d/%d->%d.%d.%d.%d intf: %s metric: %d flags: 0x%x\n",
IP4STR(route.prefix), route.mask, IP4STR(route.nexthop), route.intf, route.metric, route.flags);
}
cross_cursorClose (hCursor, 0);
```
## ✂️ Delete Rows
``` c linenums="1"
// Delete single route 192.168.1.0/24 by Primary Key
route.prefix = IP4ADDR(192,168,1,0);
route.mask = 24;
ret = cross_dbDelRowByPK (hRtTbl, &route, 0);
CHECK (ret, "Failed to delete route 192.168.1.0/24 by Primary Key");
// Delete routes where nexthop=192.168.1.254
route.nexthop = IP4ADDR(192,168,1,254);
count = cross_dbDeleteRows (hRtTbl, "nexthop", &route, 0);
printf ("Delete %d routes where nexthop=192.168.1.254\n", count);
```

## 🌄 Transaction

``` c linenums="1"
ret = cross_dbTransBegin (hDb, 0);
CHECK (ret, "Failed to begin transaction");
// Update single route 192.168.1.0/24 by Primary Key: set flags 0->5
route.prefix = IP4ADDR(192,168,1,0);
route.mask = 24;
route.flags = 5;
ret = cross_dbUpdRowByPK (hRtTbl, &route, "flags", &route, 0);
CHECK (ret, "Failed to update route 192.168.1.0/24 by Primary Key");
ret = cross_dbTransCommit (hDb, 0);
CHECK (ret, "Failed to commit transaction");
```
- Language bindings: `Python`, `Java`, `Go`, `CSharp`, `Javascript`, `PHP`, etc
- Test and report bugs

## Want to Lean More?
https://crossdb.org
https://crossdb.org
10 changes: 10 additions & 0 deletions bench/c/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
all:
$(CC) bench.c -O2 -lcrossdb -lpthread
./a.out

fast:
$(CC) bench.c ../../src/crossdb.c -O3 -march=native
./a.out

clean:
rm -f a.out
Loading

0 comments on commit 057e0b9

Please sign in to comment.