Menu

Library system

David Lazarescu

Big picture

Librum's library is split up into two parts:
- local-library
- server-library

The first part is the "local-library," which keeps track of the books that are currently downloaded, and the second part is the "server-library," which contains all of the books that the user owns.

It is important to remember that the server's library contains all of the books from the time the user was last online.
If a user adds a book while offline, it will be added to the local-library but not to the server-library until the user reconnects.

The local library

The local-library is a simple set of files that is kept up to date during Librum's runtime; it stores the books' data in JSON and adds, updates, or removes data based on the operations performed in-app.

Every user has a unique folder whose name is based on the user's email address. Inside of this folder are files which each correspond to a book. These files are named by the book's uuid with the .libmeta extension appended.

An example of such a file could look like this:


09ffa03c-5703-40a1-881a-cc08caea8251.libmeta

{
    "addedToLibrary": "17:14:07 - 01.11.2022",
    "author": "Some Author",
    "creationDate": "Friday, 29. May 2020 13:20:30 UTC",
    "creator": "",
    "currentPage": 217,
    "documentSize": "18,8 MiB",
    "filePath": "file:///home/user/path/to/book.pdf",
    "format": "pdf",
    "language": "English",
    "lastModified": "17:14:11 - 01.11.2022",
    "lastOpened": "17:23:57 - 01.11.2022",
    "pageCount": 901,
    "pagesSize": "187 x 235 mm (Portrait Custom)",
    "title": "SomeBook",
    "uuid": "09ffa03c-5703-40a1-881a-cc08caea8251"
}

In Librum this local-library is maintained by the DownloadedBooksTracker class. This class performs operations such as adding, updating and deleting books from the local-library.

The server library

The server-library refers to the books actually stored in the database, the source code is located in Librum-Server.

Books in the server-library are persistently located on the server and thus accessible from all the user's devices. When a user adds a book via the Librum client application while being connected to the internet, the book is added to both the local-library and the server-library. If the user does not have an internet connection while adding a book, the book will be added to the local-library and synced to the server-library the next time a internet connection is available.

How books are loaded

On login both the local-library (DownloadedBooksTracker) and the server-library books are requested. If the user is not connected to the internet, the books from the local library are used; otherwise, the books from the server-library are used.

During runtime, the client sends a poll-request to the server asking if there have been any changes (e.g., every 30s). If there is a change, the modifications are merged.

Merging changes

The way changes are merged is very simple, every book has a property called lastModified, if the server-library's book's lastModified is later than the the lastModified of the local-library, the local-library's book is overwritten with the one of the server-library and vice versa.