Skip to content

Commit

Permalink
Add build-pages function
Browse files Browse the repository at this point in the history
  • Loading branch information
EduardoLR10 committed Jan 13, 2024
1 parent 7914de1 commit 9c7535c
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 20 deletions.
34 changes: 20 additions & 14 deletions main.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
(submod RacketowerDB/io reader)
;(submod RacketowerDB/language parser)
(submod RacketowerDB/backend server)
RacketowerDB/util)
RacketowerDB/bplustree
RacketowerDB/util
RacketowerDB/pagination)

;; review if this is needed for eval-syntax
(dynamic-require 'RacketowerDB/ast 0)
Expand Down Expand Up @@ -90,21 +92,25 @@
`(("MODEL" . ,(stringl "Abc"))
("YEAR" . ,(integer32 2013))))

(check-local-constraints programmer-table (list row1 row2))
(hash-set! schema "CAR" car-table)
(hash-set! schema "TEST" procedure-test)
(hash-set! schema "PROGRAMMER" programmer-table)
(write-schema-to-disk schema)
(set! schema (read-schema-from-disk "schema"))
(check-local-constraints (hash-ref schema "PROGRAMMER") (list row1 row2))
(println schema)
(set! schema (write-rows-to-disk schema "PROGRAMMER" (list row1 row2)))
(println (read-table-values-from-disk schema "PROGRAMMER"))
(write-table-to-disk programmer-table "PROGRAMMER")
(let ((read-table (read-table-from-disk "PROGRAMMER")))
(hash-set! schema "PROGRAMMER" read-table)
(set! schema (write-rows-to-disk schema "PROGRAMMER" (list row1 row2)))
(println schema)))
;; (println (read-table-values-from-disk schema "PROGRAMMER"))
(define-values (pages amount-already-read) (build-pages 0 1 1 (+ 7 10 4) 0 "AGE" schema "PROGRAMMER"))
(println pages)
(println amount-already-read)
;; (check-local-constraints programmer-table (list row1 row2))
;; (hash-set! schema "CAR" car-table)
;; (hash-set! schema "TEST" procedure-test)
;; (write-schema-to-disk schema)
;; (set! schema (read-schema-from-disk "schema"))
;; (check-local-constraints (hash-ref schema "PROGRAMMER") (list row1 row2))
;; (println schema)
;; (write-table-to-disk programmer-table "PROGRAMMER")
;; (let ((read-table (read-table-from-disk "PROGRAMMER")))
;; (hash-set! schema "PROGRAMMER" read-table)
;; (set! schema (write-rows-to-disk schema "PROGRAMMER" (list row1 row2)))
;; (println schema))
(tree-test))

;;(exit-handler)
;;(server-entrypoint)
Expand Down
67 changes: 61 additions & 6 deletions pagination.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,74 @@

(require
ffi/unsafe
(only-in RacketowerDB/util checked-guard))
(only-in RacketowerDB/util checked-guard build-ndf-filename)
(submod RacketowerDB/io reader)
RacketowerDB/ast)

(provide build-pages)

(struct page [instances indexes] #:transparent
#:guard
(checked-guard
[(instances . (vectorof bytes?))
(indexes . (vectorof (cons/c exact-nonnegative-integer? exact-nonnegative-integer?)))]
[(instances . (listof (listof byte?)))
(indexes . (listof (cons/c exact-nonnegative-integer? exact-nonnegative-integer?)))]
(values instances indexes)))

(struct pager [pages tree amount-read] #:transparent
(struct pager [pages tree amount-read instancesPerPage maxPagesAtOnce] #:transparent
#:guard
(checked-guard
[(pages . (vectorof page?))
[(pages . (listof page?))
(tree . cpointer?)
(amount-read . exact-nonnegative-integer?)]
(amount-read . exact-nonnegative-integer?)
(instancesPerPage . exact-nonnegative-integer?)
(maxPagesAtOnce . exact-nonnegative-integer?)]
(values pages tree amount-read)))

(define (split-by lst n)
(if (not (empty? lst))
(cons (take lst n) (split-by (drop lst n) n))
'()))

(define (build-pages
initial-page-number
instances-per-page
page-bound
instance-size
amount-already-read
attribute-to-index
schema
entity-name)
;; TODO: Leave an error message to only work on the supported types.
(define (query data attribute-to-index)
(first (map (lambda [row] (integer32-value (hash-ref row attribute-to-index))) data)))
(define (create-indexes instances schema entity-name attribute-to-index)
(map (lambda (index instance)
(let* [(instance-bytes (list->bytes instance))
(read-content (read-table-values-from-disk schema entity-name #:source instance-bytes))
(attempt-to-find (query read-content attribute-to-index))]
;; TODO: We are missing some error checking in here
(cons attempt-to-find index)))
(range (length instances))
instances))
(define file-name (build-ndf-filename entity-name #:data? 'data))
(define content-length (file-size file-name))
(let loop [(amount-already-read amount-already-read)
(reader (open-input-file file-name #:mode 'binary))
(index initial-page-number)
(pages (list))]
(if (equal? index page-bound)
(values pages amount-already-read)
(begin
(file-position reader (* index instance-size))
(let* [(amount-to-read (if (> (* instances-per-page instance-size) (- content-length amount-already-read))
(- content-length amount-already-read)
(* instances-per-page instance-size)))
(buffer (bytes->list (read-bytes amount-to-read reader)))
(instances (take (split-by buffer instance-size) page-bound))
(indexes (create-indexes instances schema entity-name attribute-to-index))]
(loop
(+ amount-already-read amount-to-read)
reader
(+ instances-per-page index)
(append pages (list (page instances indexes)))))))))

0 comments on commit 9c7535c

Please sign in to comment.