Skip to content

Commit eaf9328

Browse files
authored
LNX-4265 (#11)
LNX-4265
1 parent b932ab2 commit eaf9328

File tree

14 files changed

+1172
-0
lines changed

14 files changed

+1172
-0
lines changed

linux-articles/simpledb/simpledb.c

Lines changed: 609 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"100","John Doe","[email protected]","9001","Red Book","15.00"
2+
"101","Alpha Tester","[email protected]","9002","Blue Pen","2.50"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"9001","100","Red Book","15.00"
2+
"9002","101","Blue Pen","2.50"
3+
"9003","999","Green Pencil","1.00"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"9001","100","Red Book","15.00"
2+
"9002","101","Blue Pen","2.50"
3+
"9003","999","Green Pencil","1.00"
21.6 KB
Binary file not shown.
Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
=== Starting test of simpledb ===
2+
=== Using three database directories: testdb1, testdb2 and testdb3 ===
3+
4+
### 1) Testing missing arguments or invalid usage...
5+
- Attempting to run without arguments (expect usage error):
6+
Usage:
7+
./simpledb --db-path <PATH> COMMAND [ARGS...]
8+
9+
Commands:
10+
list <table>
11+
get <table> field=value
12+
save <table> field1=value1 [field2=value2 ...]
13+
delete <table> field=value
14+
15+
Options:
16+
--db-path <PATH> Required. Path to the database directory.
17+
18+
- Attempting to run with no --db-path (expect usage error):
19+
Usage:
20+
./simpledb --db-path <PATH> COMMAND [ARGS...]
21+
22+
Commands:
23+
list <table>
24+
get <table> field=value
25+
save <table> field1=value1 [field2=value2 ...]
26+
delete <table> field=value
27+
28+
Options:
29+
--db-path <PATH> Required. Path to the database directory.
30+
31+
- Attempting to run with --db-path but no command (expect usage error):
32+
Usage:
33+
./simpledb --db-path <PATH> COMMAND [ARGS...]
34+
35+
Commands:
36+
list <table>
37+
get <table> field=value
38+
save <table> field1=value1 [field2=value2 ...]
39+
delete <table> field=value
40+
41+
Options:
42+
--db-path <PATH> Required. Path to the database directory.
43+
44+
- Attempting an unknown command (expect error):
45+
Error: Unknown command 'unknowncmd'
46+
Usage:
47+
./simpledb --db-path <PATH> COMMAND [ARGS...]
48+
49+
Commands:
50+
list <table>
51+
get <table> field=value
52+
save <table> field1=value1 [field2=value2 ...]
53+
delete <table> field=value
54+
55+
Options:
56+
--db-path <PATH> Required. Path to the database directory.
57+
58+
59+
### 2) Creating and listing a simple table...
60+
- Listing 'users' in testdb1 (should be empty JSON array):
61+
- Checking the content of testdb1/users.json (should exist after first command, or created empty).
62+
total 0
63+
64+
### 3) Save new records and update existing ones...
65+
- Adding a new record with id=100 to 'users' in testdb1:
66+
{"id":"100","name":"John Doe","age":"30","email":"[email protected]"}
67+
- Listing 'users' again (should show John):
68+
{"id":"100","name":"John Doe","age":"30","email":"[email protected]"}
69+
- Updating record with id=100 (changing email):
70+
{"id":"100","name":"John Doe","age":"30","email":"[email protected]"}
71+
- Listing 'users' again (should show updated email):
72+
{"id":"100","name":"John Doe","age":"30","email":"[email protected]"}
73+
- Adding second record with id=200...
74+
{"id":"200","name":"Alice","age":"28","email":"[email protected]"}
75+
- Listing 'users' to confirm the second record...
76+
{"id":"100","name":"John Doe","age":"30","email":"[email protected]"}
77+
{"id":"200","name":"Alice","age":"28","email":"[email protected]"}
78+
79+
### 4) Get by field value...
80+
- Getting users with id=100 (should return John):
81+
{"id":"100","name":"John Doe","age":"30","email":"[email protected]"}
82+
- Getting users with [email protected] (should return Alice):
83+
{"id":"200","name":"Alice","age":"28","email":"[email protected]"}
84+
- Getting users with an unknown field (should return no records):
85+
86+
### 5) Delete records...
87+
- Deleting user with id=200:
88+
Deleted 1 record(s)
89+
- Listing users (should only have John now):
90+
{"id":"100","name":"John Doe","age":"30","email":"[email protected]"}
91+
- Trying to delete a non-existent user (id=9999) (should delete 0):
92+
Deleted 0 record(s)
93+
- Listing users again to confirm no change:
94+
{"id":"100","name":"John Doe","age":"30","email":"[email protected]"}
95+
96+
### 6) Working with multiple tables within the same database...
97+
- Creating a 'products' table in testdb1...
98+
{"id":"5001","name":"Widget","price":"19.99"}
99+
{"id":"5002","name":"Gadget","price":"29.99"}
100+
- Listing 'products':
101+
{"id":"5001","name":"Widget","price":"19.99"}
102+
{"id":"5002","name":"Gadget","price":"29.99"}
103+
- We still have 'users' table too. Listing 'users' again to check everything is intact:
104+
{"id":"100","name":"John Doe","age":"30","email":"[email protected]"}
105+
106+
### 7) Creating and using a second separate database directory (testdb2)...
107+
- Let's add a 'users' table in testdb2 with different data...
108+
{"id":"999","name":"Jane Doe","email":"[email protected]"}
109+
{"id":"999","name":"Jane Doe","email":"[email protected]"}
110+
- testdb1 and testdb2 are totally independent. Checking each directory's content:
111+
Contents of testdb1:
112+
total 8
113+
-rw-rw-r-- 1 francesco francesco 93 Jan 6 17:10 products.json
114+
-rw-rw-r-- 1 francesco francesco 73 Jan 6 17:10 users.json
115+
Contents of testdb2:
116+
total 4
117+
-rw-rw-r-- 1 francesco francesco 59 Jan 6 17:10 users.json
118+
119+
### 8) Combining simpledb with grep and jq...
120+
- Let's add a few more users to testdb1's 'users' table...
121+
{"id":"101","name":"Alpha Tester","email":"[email protected]"}
122+
{"id":"102","name":"Beta Tester","email":"[email protected]"}
123+
- Now listing all users in JSON lines format:
124+
{"id":"100","name":"John Doe","age":"30","email":"[email protected]"}
125+
{"id":"101","name":"Alpha Tester","email":"[email protected]"}
126+
{"id":"102","name":"Beta Tester","email":"[email protected]"}
127+
128+
#### 8a) Using grep to find user names containing 'Tester':
129+
{"id":"101","name":"Alpha Tester","email":"[email protected]"}
130+
{"id":"102","name":"Beta Tester","email":"[email protected]"}
131+
132+
#### 8b) Using jq to filter by email matching 'example.com':
133+
{
134+
"id": "101",
135+
"name": "Alpha Tester",
136+
"email": "[email protected]"
137+
}
138+
{
139+
"id": "102",
140+
"name": "Beta Tester",
141+
"email": "[email protected]"
142+
}
143+
144+
#### 8c) Sorting by name with jq:
145+
{
146+
"id": "101",
147+
"name": "Alpha Tester",
148+
"email": "[email protected]"
149+
}
150+
{
151+
"id": "102",
152+
"name": "Beta Tester",
153+
"email": "[email protected]"
154+
}
155+
{
156+
"id": "100",
157+
"name": "John Doe",
158+
"age": "30",
159+
"email": "[email protected]"
160+
}
161+
162+
### 9) Simulating a 'join' between tables...
163+
- Creating an 'orders' table in testdb1...
164+
{"id":"1","order_id":"9001","user_id":"100","product":"Red Book","price":"15.00"}
165+
{"id":"2","order_id":"9002","user_id":"101","product":"Blue Pen","price":"2.50"}
166+
{"id":"3","order_id":"9003","user_id":"999","product":"Green Pencil","price":"1.00"}
167+
- Listing 'orders':
168+
{"id":"1","order_id":"9001","user_id":"100","product":"Red Book","price":"15.00"}
169+
{"id":"2","order_id":"9002","user_id":"101","product":"Blue Pen","price":"2.50"}
170+
{"id":"3","order_id":"9003","user_id":"999","product":"Green Pencil","price":"1.00"}
171+
172+
#### 9a) Converting 'users' to CSV (id,name,email) => users.csv
173+
"100","John Doe","[email protected]"
174+
"101","Alpha Tester","[email protected]"
175+
"102","Beta Tester","[email protected]"
176+
177+
#### 9b) Converting 'orders' to CSV (order_id,user_id,product,price) => orders.csv
178+
"9001","100","Red Book","15.00"
179+
"9002","101","Blue Pen","2.50"
180+
"9003","999","Green Pencil","1.00"
181+
182+
#### 9c) Sort both CSV files by their key for join.
183+
- Sorted 'users':
184+
"100","John Doe","[email protected]"
185+
"101","Alpha Tester","[email protected]"
186+
"102","Beta Tester","[email protected]"
187+
- Sorted 'orders':
188+
"9001","100","Red Book","15.00"
189+
"9002","101","Blue Pen","2.50"
190+
"9003","999","Green Pencil","1.00"
191+
192+
#### 9d) Join by user_id (users.id == orders.user_id)
193+
(We have to specify that for 'users' the join field is column 1, for 'orders' it's column 2)
194+
- Result of join (joined.csv):
195+
"100","John Doe","[email protected]","9001","Red Book","15.00"
196+
"101","Alpha Tester","[email protected]","9002","Blue Pen","2.50"
197+
198+
#### 9e) Explanation:
199+
The joined.csv lines combine the user info with the order info if the IDs match.
200+
201+
### 10) Testing automatic and invalid IDs in testdb3...
202+
- Case A: Save without providing an ID at all (should auto-generate id=1).
203+
{"id":"1","name":"Bob","email":"[email protected]"}
204+
- Listing 'people' (should see Bob with id=1):
205+
{"id":"1","name":"Bob","email":"[email protected]"}
206+
207+
- Case B: Save another record without ID (auto-generate id=2).
208+
{"id":"2","name":"Alice","email":"[email protected]"}
209+
- Listing 'people' (should see Bob (id=1) and Alice (id=2)):
210+
{"id":"1","name":"Bob","email":"[email protected]"}
211+
{"id":"2","name":"Alice","email":"[email protected]"}
212+
213+
- Case C: Provide a valid positive integer ID.
214+
{"id":"10","name":"Charlie","email":"[email protected]"}
215+
- Listing 'people' (should see Bob (id=1), Alice (id=2), and Charlie (id=10)):
216+
{"id":"1","name":"Bob","email":"[email protected]"}
217+
{"id":"2","name":"Alice","email":"[email protected]"}
218+
{"id":"10","name":"Charlie","email":"[email protected]"}
219+
220+
- Case D: Provide an invalid (negative) ID, expecting an error.
221+
Error: 'id' must be a positive integer, got '-5'
222+
- Listing 'people' again (no changes expected):
223+
{"id":"1","name":"Bob","email":"[email protected]"}
224+
{"id":"2","name":"Alice","email":"[email protected]"}
225+
{"id":"10","name":"Charlie","email":"[email protected]"}
226+
227+
- Case E: Provide an invalid (non-numeric) ID, expecting an error.
228+
Error: 'id' must be a positive integer, got 'abc'
229+
- Listing 'people' again (no changes expected):
230+
{"id":"1","name":"Bob","email":"[email protected]"}
231+
{"id":"2","name":"Alice","email":"[email protected]"}
232+
{"id":"10","name":"Charlie","email":"[email protected]"}
233+
234+
### 10) End of tests for testdb3.
235+
236+
### 11) Final checks and cleanup hints...
237+
- Database directories currently exist at testdb1, testdb2 and testdb3
238+
- If you want to remove them, run: rm -rf testdb1 testdb2 testdb3
239+
- CSV and JSON files (users.csv, orders.csv, etc.) are also in the current directory.
240+
241+
=== End of test script for simpledb ===

0 commit comments

Comments
 (0)