Skip to content

Commit

Permalink
Refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
interkosmos committed Jul 27, 2024
1 parent dfff06d commit 779fec5
Show file tree
Hide file tree
Showing 41 changed files with 598 additions and 577 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ SRC = $(SRCDIR)/dm_ansi.f90 \
$(SRCDIR)/dm_html.f90 \
$(SRCDIR)/dm_http.f90 \
$(SRCDIR)/dm_id.f90 \
$(SRCDIR)/dm_inet.f90 \
$(SRCDIR)/dm_net.f90 \
$(SRCDIR)/dm_job.f90 \
$(SRCDIR)/dm_json.f90 \
$(SRCDIR)/dm_jsonl.f90 \
Expand Down Expand Up @@ -338,7 +338,7 @@ OBJ = dm_ansi.o \
dm_html.o \
dm_http.o \
dm_id.o \
dm_inet.o \
dm_net.o \
dm_job.o \
dm_json.o \
dm_jsonl.o \
Expand Down Expand Up @@ -552,7 +552,7 @@ $(OBJ): $(SRC)
$(FC) $(FFLAGS) $(LDFLAGS) -c src/dm_hash_table.f90
$(FC) $(FFLAGS) $(LDFLAGS) -c src/dm_unit.f90
$(FC) $(FFLAGS) $(LDFLAGS) -c src/dm_id.f90
$(FC) $(FFLAGS) $(LDFLAGS) -c src/dm_inet.f90
$(FC) $(FFLAGS) $(LDFLAGS) -c src/dm_net.f90
$(FC) $(FFLAGS) $(LDFLAGS) -c src/dm_uuid.f90
$(FC) $(FFLAGS) $(LDFLAGS) -c src/dm_signal.f90
$(FC) $(FFLAGS) $(LDFLAGS) -c src/dm_system.f90
Expand Down
8 changes: 3 additions & 5 deletions app/dmapi.f90
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,11 @@ program dmapi
if (dm_is_error(rc)) call dm_stop(STOP_FAILURE)

! Run event loop.
do while (dm_fcgi_accept() == E_NONE)
do while (dm_fcgi_accept())
call dm_cgi_env(env)
call dm_cgi_router_dispatch(router, env, code)

if (code /= HTTP_OK) then
call api_error(code, dm_error_message(E_NOT_FOUND), E_NOT_FOUND)
end if
if (code == HTTP_OK) cycle
call api_error(code, dm_error_message(E_NOT_FOUND), E_NOT_FOUND)
end do

! Clean up.
Expand Down
40 changes: 22 additions & 18 deletions app/dmbackup.f90
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ program dmbackup
character(len=*), parameter :: APP_NAME = 'dmbackup'
integer, parameter :: APP_MAJOR = 0
integer, parameter :: APP_MINOR = 9
integer, parameter :: APP_PATCH = 1
integer, parameter :: APP_PATCH = 2

integer, parameter :: APP_NSTEPS = 500 !! Step size for backup API.
integer, parameter :: APP_SLEEP_TIME = 25 !! Sleep time between steps in msec.
Expand All @@ -19,7 +19,7 @@ program dmbackup
!! Command-line arguments.
character(len=FILE_PATH_LEN) :: database = ' ' !! Path to database.
character(len=FILE_PATH_LEN) :: backup = ' ' !! Path to backup.
logical :: vacuum = .false. !! VACUUM flag.
logical :: vacuum = .false. !! Vacuum flag.
logical :: wal = .false. !! WAL flag.
logical :: verbose = .false. !! Verbose flag.
end type app_type
Expand All @@ -41,11 +41,16 @@ program dmbackup
integer function backup(app) result(rc)
!! Creates database backup.
type(app_type), intent(inout) :: app
type(db_type) :: db

type(db_type) :: db

! Open database.
rc = dm_db_open(db, app%database)
if (dm_is_error(rc)) return

if (dm_is_error(rc)) then
call dm_error_out(rc, 'failed to open database')
return
end if

backup_block: block
! Use VACUUM INTO.
Expand All @@ -57,29 +62,25 @@ integer function backup(app) result(rc)
! Use SQLite backup API.
if (app%verbose) then
! Using callback.
rc = dm_db_backup(db = db, &
path = app%backup, &
wal = app%wal, &
callback = backup_handler, &
nsteps = APP_NSTEPS, &
sleep_time = APP_SLEEP_TIME)
rc = dm_db_backup(db=db, path=app%backup, wal=app%wal, callback=backup_handler, &
nsteps=APP_NSTEPS, sleep_time=APP_SLEEP_TIME)
print *
else
! No callback.
rc = dm_db_backup(db = db, &
path = app%backup, &
wal = app%wal, &
nsteps = APP_NSTEPS, &
sleep_time = APP_SLEEP_TIME)
rc = dm_db_backup(db=db, path=app%backup, wal=app%wal, nsteps=APP_NSTEPS, &
sleep_time=APP_SLEEP_TIME)
end if
end block backup_block

if (dm_is_error(dm_db_close(db))) rc = E_DB
call dm_error_out(rc, 'backup failed')
rc = dm_db_close(db)
end function backup

integer function read_args(app) result(rc)
!! Reads command-line arguments.
type(app_type), intent(out) :: app
type(arg_type) :: args(5)

type(arg_type) :: args(5)

rc = E_NONE

Expand Down Expand Up @@ -118,9 +119,12 @@ end function read_args

subroutine backup_handler(remaining, page_count)
!! Prints progess to standard output of SQLite backup API is selected.
!! The cursor is reset to the first column of the line on each
!! invokation.
integer, intent(in) :: remaining !! Pages remaining.
integer, intent(in) :: page_count !! Total count of pages.

print '("Progress: ", f5.1, " %")', 100.0 * (page_count - remaining) / page_count
write (*, '(a1, "[0GProgress: ", f5.1, " %")', advance='no') &
ASCII_ESC, 100.0 * (page_count - remaining) / page_count
end subroutine backup_handler
end program dmbackup
6 changes: 1 addition & 5 deletions app/dmexport.f90
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,7 @@ integer function export(app) result(rc)
! Open file.
if (is_file) then
rc = E_IO
open (action = 'write', &
file = trim(app%output), &
iostat = stat, &
newunit = unit, &
status = 'replace')
open (action='write', file=trim(app%output), iostat=stat, newunit=unit, status='replace')
if (stat /= 0) return
end if

Expand Down
19 changes: 11 additions & 8 deletions app/dmfs.f90
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,11 @@ integer function read_args(app) result(rc)
return
end select

app%output_type = OUTPUT_FILE
if (trim(app%output) == '-') app%output_type = OUTPUT_STDOUT
if (trim(app%output) == '-') then
app%output_type = OUTPUT_STDOUT
else
app%output_type = OUTPUT_FILE
end if
end if

rc = E_EMPTY
Expand Down Expand Up @@ -231,7 +234,7 @@ integer function read_observ(observ, node_id, sensor_id, source, debug) result(r
type(response_type), pointer :: response ! Single response in request.

integer :: delay
integer :: fu, stat
integer :: stat, unit
integer :: i, j, n
logical :: debug_

Expand Down Expand Up @@ -278,7 +281,7 @@ integer function read_observ(observ, node_id, sensor_id, source, debug) result(r
end if

! Try to open file for reading.
open (action='read', file=trim(request%request), iostat=stat, newunit=fu)
open (action='read', file=trim(request%request), iostat=stat, newunit=unit)
if (stat == 0) request%error = E_NONE

if (dm_is_error(request%error)) then
Expand All @@ -289,7 +292,7 @@ integer function read_observ(observ, node_id, sensor_id, source, debug) result(r
! Read until the request pattern matches or end is reached.
read_loop: do
rc = E_EOF
read (fu, '(a)', iostat=stat) raw
read (unit, '(a)', iostat=stat) raw
if (is_iostat_end(stat)) exit read_loop
if (stat /= 0) cycle read_loop

Expand Down Expand Up @@ -328,7 +331,7 @@ integer function read_observ(observ, node_id, sensor_id, source, debug) result(r
end do read_loop

! Close file.
close (fu)
close (unit)

request%error = rc

Expand All @@ -352,7 +355,7 @@ integer function read_observ(observ, node_id, sensor_id, source, debug) result(r
call logger%debug('next observ in ' // dm_itoa(delay / 1000) // ' sec', observ=observ)
end if

call dm_usleep(delay * 1000) ! [msec] to [us].
call dm_msleep(delay)
end do req_loop
end function read_observ

Expand Down Expand Up @@ -454,7 +457,7 @@ subroutine run(app)
delay = max(0, job%delay)
if (delay <= 0) cycle job_loop
if (debug) call logger%debug('next job in ' // dm_itoa(delay / 1000) // ' sec', observ=observ)
call dm_usleep(delay * 1000)
call dm_msleep(delay)
end do job_loop
end subroutine run

Expand Down
1 change: 1 addition & 0 deletions app/dmgrc.f90
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ integer function read_config(app) result(rc)
rc = dm_config_get(config, 'warning', app%levels(LL_WARNING )%codes)
rc = dm_config_get(config, 'error', app%levels(LL_ERROR )%codes)
rc = dm_config_get(config, 'critical', app%levels(LL_CRITICAL)%codes)
rc = dm_config_get(config, 'user', app%levels(LL_USER) %codes)

call dm_config_remove(config)
end if
Expand Down
18 changes: 9 additions & 9 deletions app/dmimport.f90
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ integer function import(app) result(rc)

type(app_type), intent(inout) :: app

integer :: er, stat, unit
integer :: error, stat, unit
integer(kind=i8) :: nrecs, nrows
logical :: exists, valid
real(kind=r8) :: dt
Expand Down Expand Up @@ -155,13 +155,14 @@ integer function import(app) result(rc)
exit read_loop
end if

! Any other error.
if (dm_is_error(rc)) then
call dm_error_out(rc, 'failed to read record in row ' // dm_itoa(nrows))
exit read_loop
end if

! Validate record but skip database insert on dry run.
if (app%dry) then
! Validate record.
select case (app%type)
case (TYPE_NODE)
valid = dm_node_valid(node)
Expand All @@ -181,7 +182,6 @@ integer function import(app) result(rc)
exit read_loop
end if

! Skip database insert on dry run.
cycle read_loop
end if

Expand Down Expand Up @@ -222,10 +222,10 @@ integer function import(app) result(rc)
if (.not. app%dry) then
! Rollback transaction on error.
if (dm_is_error(rc)) then
er = dm_db_rollback(db)
error = dm_db_rollback(db)

if (dm_is_error(er)) then
call dm_error_out(er, 'failed to roll back database transaction')
if (dm_is_error(error)) then
call dm_error_out(error, 'failed to roll back database transaction')
exit import_block
end if

Expand All @@ -239,10 +239,10 @@ integer function import(app) result(rc)
! Rollback transaction on error.
if (dm_is_error(rc)) then
call dm_error_out(rc, 'failed to commit database transaction')
er = dm_db_rollback(db)
error = dm_db_rollback(db)

if (dm_is_error(er)) then
call dm_error_out(er, 'failed to roll back database transaction')
if (dm_is_error(error)) then
call dm_error_out(error, 'failed to roll back database transaction')
exit import_block
end if

Expand Down
Loading

0 comments on commit 779fec5

Please sign in to comment.