Skip to content

Commit ae5039f

Browse files
committed
initial commit
0 parents  commit ae5039f

31 files changed

+1508
-0
lines changed

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# java-cli-buildr-cockroachdb-single-node-without-ssl-pivot
2+
3+
## Description
4+
Creates a small database table
5+
called `dog`. This table, `dog`, has been normalized to 3NF.
6+
Two new tables have been added, `breedLookup` and `colorLookup`.
7+
Creates a new table `dog_expanded` that joins
8+
`dog`, `breedLookup` and `colorLookup`. Added clustered indexes on
9+
`dog`.breedId and `dog`.colorId. Turned `dog_expanded` into a view with an
10+
implicit index on `dog_expanded`.id. Using a case statement with the aggregate function
11+
COUNT, create a new view `breed_count`. All output normally
12+
seen in a terminal will be in `log` which will dump to the screen. The project may seem to hang but the logs from the container must be written to the project this can take up to 3 min.
13+
14+
A java buildr build, that connects to single node
15+
cockroach database without ssl.
16+
17+
## Tech stack
18+
- java
19+
- buildr
20+
- postgres drivers
21+
22+
## Docker stack
23+
- cockroachdb/cockroach:v19.2.2
24+
- vanto/apache-buildr:latest-jruby-jdk8
25+
26+
## To run
27+
`sudo ./install.sh -u`
28+
- [webui](http://localhost:8080)
29+
30+
## To stop
31+
`sudo ./install.sh -d`
32+
33+
## For help
34+
`sudo ./install.sh -h`
35+
36+
## Credit
37+
[Cockroach setup](https://github.com/s0rg/cockroach-compose)

db/init-data.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env sh
2+
3+
4+
HOST="db"
5+
DATA_SRC=/docker-entrypoint-initdb.d
6+
hold=/tmp/hold.sql
7+
8+
/cockroach/cockroach.sh init --host "$HOST" --insecure
9+
10+
# /cockroach/cockroach.sh user set testuser --host "$HOST" --insecure
11+
12+
for sql in `ls ${DATA_SRC}/*.sql`; do
13+
cat $sql >> $hold
14+
done
15+
16+
cat $hold | /cockroach/cockroach.sh sql --host $HOST --insecure
17+
18+
exit 0

db/sql/00-create-table.sql

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CREATE DATABASE animal;
2+
3+
USE animal;
4+
5+
CREATE SEQUENCE dog_id_seq INCREMENT 1 MINVALUE 1 MAXVALUE 2147483647 START 1;
6+
7+
CREATE TABLE dog (
8+
id integer DEFAULT nextval('dog_id_seq') NOT NULL,
9+
breedId integer NOT NULL,
10+
colorId integer NOT NULL
11+
);

db/sql/01-table-insert.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
INSERT INTO dog (breedId, colorId)
2+
VALUES
3+
(1, 1),
4+
(1, 2),
5+
(1, 3),
6+
(2, 2),
7+
(3, 2),
8+
(3, 3),
9+
(4, 3),
10+
(4, 4);

db/sql/02-create-table.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CREATE SEQUENCE breedLookup_id_seq INCREMENT 1 MINVALUE 1 MAXVALUE 2147483647 START 1;
2+
3+
CREATE TABLE breedLookup (
4+
id integer DEFAULT nextval('breedLookup_id_seq') NOT NULL,
5+
breed text NOT NULL
6+
);

db/sql/03-table-insert.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
INSERT INTO breedLookup (breed)
2+
VALUES
3+
('Am Bulldog'),
4+
('Blue Tick'),
5+
('Labrador'),
6+
('Gr Shepard');

db/sql/04-create-table.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CREATE SEQUENCE colorLookup_id_seq INCREMENT 1 MINVALUE 1 MAXVALUE 2147483647 START 1;
2+
3+
CREATE TABLE colorLookup (
4+
id integer DEFAULT nextval('colorLookup_id_seq') NOT NULL,
5+
color text NOT NULL
6+
);

db/sql/05-table-insert.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
INSERT INTO colorLookup (color)
2+
VALUES
3+
('White'),
4+
('Grey'),
5+
('Black'),
6+
('Brown');

db/sql/07-create-index.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CREATE INDEX IDX_breed on dog(breedId);
2+
CREATE INDEX IDX_color on dog(colorId);

db/sql/08-create-view.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CREATE VIEW dog_expanded AS (
2+
SELECT a.id, b.breed, c.color
3+
FROM dog AS a
4+
JOIN breedLookup AS b ON b.id = a.breedId
5+
JOIN colorLookup AS c ON c.id = a.colorId
6+
);

db/sql/09-create-view.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CREATE VIEW breed_count AS
2+
SELECT c.id, c.breed, CASE
3+
WHEN a.breedId BETWEEN 1 AND 5 THEN COUNT(a.breedId)
4+
ELSE COUNT(NULL)
5+
END
6+
FROM dog as a
7+
JOIN breedLookup as c ON c.id = a.breedId
8+
GROUP BY c.id, c.breed, a.breedId
9+
ORDER BY c.id;

docker-compose.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
version: '3'
2+
services:
3+
java-srv:
4+
build:
5+
context: java-srv
6+
depends_on:
7+
- db-runner
8+
links:
9+
- "db:db"
10+
volumes:
11+
- ./log:/root/log
12+
13+
db:
14+
image: cockroachdb/cockroach:v19.2.2
15+
ports:
16+
- "26257:26257"
17+
- "8080:8080"
18+
command: start-single-node --insecure
19+
20+
db-runner:
21+
image: cockroachdb/cockroach:v19.2.2
22+
command:
23+
- shell
24+
- /usr/local/bin/init-data.sh
25+
volumes:
26+
- ./db/init-data.sh:/usr/local/bin/init-data.sh:ro
27+
- ./db/sql:/docker-entrypoint-initdb.d
28+
depends_on:
29+
- db
30+
links:
31+
- "db:db"

general.log

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[2022-09-02 14:11:43 INFO]: install::setup-logging ended
2+
================
3+
[2022-09-02 14:11:43 INFO]: install::start-up started
4+
[2022-09-02 14:11:43 INFO]: install::start-up starting services
5+
[2022-09-02 14:11:43 INFO]: install::start-up ended
6+
================
7+
[2022-09-02 14:12:29 INFO]: install::root-check started
8+
[2022-09-02 14:12:29 INFO]: install::root-check ended
9+
================
10+
[2022-09-02 14:12:29 INFO]: install::docker-check started
11+
[2022-09-02 14:12:29 INFO]: install::docker-check ended
12+
================
13+
[2022-09-02 14:12:29 INFO]: install::docker-compose-check started
14+
[2022-09-02 14:12:29 INFO]: install::docker-compose-check ended
15+
================
16+
[2022-09-02 14:12:29 INFO]: install::tear-down started
17+
[2022-09-02 14:12:29 INFO]: install::tear-down starting services
18+
[2022-09-02 14:12:29 INFO]: install::tear-down ended
19+
================

install.sh

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
#!/usr/bin/env bash
2+
basefile="install"
3+
logfile="general.log"
4+
timestamp=`date '+%Y-%m-%d %H:%M:%S'`
5+
6+
if [ "$#" -ne 1 ]; then
7+
msg="[ERROR]: $basefile failed to receive enough args"
8+
echo "$msg"
9+
echo "$msg" >> $logfile
10+
exit 1
11+
fi
12+
13+
function setup-logging(){
14+
scope="setup-logging"
15+
info_base="[$timestamp INFO]: $basefile::$scope"
16+
17+
echo "$info_base started" >> $logfile
18+
19+
echo "$info_base removing old logs" >> $logfile
20+
21+
rm -f $logfile
22+
23+
echo "$info_base ended" >> $logfile
24+
25+
echo "================" >> $logfile
26+
}
27+
28+
function root-check(){
29+
scope="root-check"
30+
info_base="[$timestamp INFO]: $basefile::$scope"
31+
32+
echo "$info_base started" >> $logfile
33+
34+
#Make sure the script is running as root.
35+
if [ "$UID" -ne "0" ]; then
36+
echo "[$timestamp ERROR]: $basefile::$scope you must be root to run $0" >> $logfile
37+
echo "==================" >> $logfile
38+
echo "You must be root to run $0. Try the following"
39+
echo "sudo $0"
40+
exit 1
41+
fi
42+
43+
echo "$info_base ended" >> $logfile
44+
echo "================" >> $logfile
45+
}
46+
47+
function docker-check() {
48+
scope="docker-check"
49+
info_base="[$timestamp INFO]: $basefile::$scope"
50+
cmd=`docker -v`
51+
52+
echo "$info_base started" >> $logfile
53+
54+
if [ -z "$cmd" ]; then
55+
echo "$info_base docker not installed"
56+
echo "$info_base docker not installed" >> $logfile
57+
fi
58+
59+
echo "$info_base ended" >> $logfile
60+
echo "================" >> $logfile
61+
62+
}
63+
64+
function docker-compose-check() {
65+
scope="docker-compose-check"
66+
info_base="[$timestamp INFO]: $basefile::$scope"
67+
cmd=`docker-compose -v`
68+
69+
echo "$info_base started" >> $logfile
70+
71+
if [ -z "$cmd" ]; then
72+
echo "$info_base docker-compose not installed"
73+
echo "$info_base docker-compose not installed" >> $logfile
74+
fi
75+
76+
echo "$info_base ended" >> $logfile
77+
echo "================" >> $logfile
78+
79+
}
80+
function usage() {
81+
echo ""
82+
echo "Usage: "
83+
echo ""
84+
echo "-u: start."
85+
echo "-d: tear down."
86+
echo "-h: Display this help and exit."
87+
echo ""
88+
}
89+
function start-up(){
90+
91+
local scope="start-up"
92+
local docker_img_name=`head -n 1 README.md | sed 's/# //'`
93+
local info_base="[$timestamp INFO]: $basefile::$scope"
94+
95+
echo "$info_base started" >> $logfile
96+
97+
echo "$info_base starting services" >> $logfile
98+
99+
sudo docker-compose up --build
100+
101+
echo "$info_base ended" >> $logfile
102+
103+
echo "================" >> $logfile
104+
}
105+
function tear-down(){
106+
107+
scope="tear-down"
108+
info_base="[$timestamp INFO]: $basefile::$scope"
109+
110+
echo "$info_base started" >> $logfile
111+
112+
echo "$info_base starting services" >> $logfile
113+
114+
sudo docker-compose down \
115+
&& sudo rm -R log
116+
117+
echo "$info_base ended" >> $logfile
118+
119+
echo "================" >> $logfile
120+
}
121+
122+
root-check
123+
docker-check
124+
docker-compose-check
125+
126+
while getopts ":udh" opts; do
127+
case $opts in
128+
u)
129+
setup-logging
130+
start-up ;;
131+
d)
132+
tear-down ;;
133+
h)
134+
usage
135+
exit 0 ;;
136+
/?)
137+
usage
138+
exit 1 ;;
139+
esac
140+
done

java-srv/Dockerfile

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
FROM vanto/apache-buildr:latest-jruby-jdk8 as builder
2+
3+
WORKDIR /workspace
4+
5+
COPY bin .
6+
7+
RUN buildr compile
8+
9+
FROM alpine:edge
10+
11+
RUN adduser -D developer
12+
13+
ENV DISPLAY :0
14+
15+
RUN apk update \
16+
&& apk add openjdk11
17+
18+
RUN apk --no-cache add msttcorefonts-installer fontconfig && \
19+
update-ms-fonts && \
20+
fc-cache -f
21+
22+
USER developer
23+
24+
WORKDIR /home/developer
25+
26+
COPY --from=builder /workspace/target/classes .
27+
28+
CMD ["java", "example.Main"]

java-srv/bin/buildfile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Generated by Buildr 1.4.22, change to your liking
2+
3+
# Version number for this release
4+
VERSION_NUMBER = "1.0.0"
5+
# Group identifier for your projects
6+
GROUP = "desktop-app"
7+
COPYRIGHT = ""
8+
9+
# Specify Maven 2.0 remote repositories here, like this:
10+
11+
repositories.remote << "http://insecure.repo1.maven.org/maven2"
12+
13+
desc "The POC apache buildr project"
14+
define "desktop-app" do
15+
16+
project.version = VERSION_NUMBER
17+
project.group = GROUP
18+
manifest["Implementation-Vendor"] = COPYRIGHT
19+
compile.with "com.google.guava:guava:jar:29.0-jre",
20+
"org.postgresql:postgresql:jar:42.2.18",
21+
"log4j:log4j:jar:1.2.17",
22+
"org.slf4j:slf4j-api:jar:1.7.5",
23+
"org.slf4j:slf4j-log4j12:jar:1.7.5"
24+
package(:jar)
25+
26+
run.using :main => 'example.Main'
27+
end

0 commit comments

Comments
 (0)