0

i have a database. imagine that my database have 1.000.000.000 records or includes 100 gb datas. i want to write a program.

the program basically will send a query to database to take 10 records and will display this records on the screen. then the user will use scroll bar on the mause to change the records displayed. for example when user scrolled down program will display records between 2 and 11. if the user keep scrolling down, the records keep displaying like between 3 and 12, 4 and 13 ... also the user can scroll up.

how can i use threads in a program like that. can anyone give a general idea for it. also if i want to use a pattern, which pattern can i use and why?

note: i can also use two buttons (for up and down) instead of scroll bar.

ramazan murat
  • 1,227
  • 1
  • 10
  • 8
  • You're first worry is how to get the data from the Db in that manner. Forget the threads for now. – H H Jan 07 '11 at 21:06
  • i coded before to do it. but as i said my problem is how will i use threads and a pattern deal with threads. – ramazan murat Jan 07 '11 at 21:08
  • 1
    If you have a large number of records (10^9 is a lot) then you have to wonder if a scrollbar is of any use because the resolution mapping between the scrollbar and records is so low i.e. scrollbar has 1000px, therefore, each px represents 10^9 / 10^3 = 10^6 records. Every time you move the scrollbar it will jump a minimum of 1 million records. If you have page buttons then the user has to click the down button 10^8 times to get to the bottom. That's going to take a while :) – Tim Lloyd Jan 07 '11 at 21:09
  • its not a problem if i have a general idea that how to solve that kind of problem, after that i can display 200 records for example and when begin to scrolling i can display records 200-400,400-800 for example – ramazan murat Jan 07 '11 at 21:12
  • @ramazan You will not be able to fit 200 records on the display (you will not even fit 100), but let's go with it. Your resolution is now 50,000 per pixel. And that is if the user moves the scrollbar a single pixel. It's more like you'll move 50px - so we're now up to 2.5 million records per scroll. Scrollbars are just not so good with large numbers. – Tim Lloyd Jan 07 '11 at 21:15
  • note: i can also use two buttons (for up and down) instead of scroll bar. what about it? but again i need to know how will i use threads in a program like that? i am really not interested on the probşems about displayed records number and scroll bar etc. – ramazan murat Jan 07 '11 at 21:18
  • @ramazon If you have buttons and you have 10^9 records, then the user will have click the down button 10^8 times to get to the bottom. I suspect that they will have to instigate a covenant where generations of families will pass the baton from parent to child until eventually as the Sun redens and starts to swell just maybe they will get to the bottom of your display. – Tim Lloyd Jan 07 '11 at 21:21
  • @chibacity ok ok .. you are right.. but can i ask what do you think about Mordachai's answer? it seems to me that he is right – ramazan murat Jan 07 '11 at 21:24
  • 1
    An example how the application may look like, using Vaadin table lazy loading: http://demo.vaadin.com/sampler#TableLazyLoading – Raphael Bossek Jan 07 '11 at 21:28
  • @Raphael B. thats exactly what i want.. – ramazan murat Jan 07 '11 at 21:31
  • 1
    Vaadin is Apache 2.0 licensed (http://vaadin.com/comparison) and you get the source code for the samples. Saerch at http://demo.vaadin.com/sampler#TableLazyLoading in your browser window for __View Source__ to get a first impression about the view implementation -- go for it http://vaadin.com/download – Raphael Bossek Jan 07 '11 at 21:38

2 Answers2

1

A typical pattern would be to have one thread handle the UI. Mouse, windowing, drawing, etc.

A worker thread would be created that did the actual DB i/o. That thread would collect responses from the database & place them in a buffer (or send them piecemeal) to the UI thread, which would then display them as they came in. Alternately, you could have the UI thread query the worker for a given range of records as the user works with the UI and this would require a way for the worker to respond immediately even if it didn't have all of the needed data.

At any rate, keeping the UI responsive while the work is executed is a typical pattern.

At the DB level itself, there are many ways to break down the execution of the search within the records into multiple parallel tasks (running on independent threads or fibers) doing the actual search requested from your program.

Mordachai
  • 9,412
  • 6
  • 60
  • 112
  • thank you for your answer.. it seems a good idea.. i will try to find more information about pattern you mentioned about.. should i search it as "worker pattern"? – ramazan murat Jan 07 '11 at 21:28
  • Sorry, I'm not aware of a formal name for this pattern: it is just one that occurs commonly in GUI style applications. I have, for example, written an installer which has a worker thread that does the actual install while the UI shows status updates & remains completely responsive to the user (and can be canceled and unrolled). Similarly, I've written serial (com-port & parallel-port) transmit & receive software that do the I/O work in separate threads from the GUI. It's a very easy pattern to handle in Win32 programming (and I imagine in other OS environments as well). – Mordachai Jan 11 '11 at 18:39
0

See allso for Model View Controller (MVC), Model View Presenter (MVP) and Model View ViewModel (MVVM) patterns to seperate the visualisation, business logic and data layers.

Community
  • 1
  • 1
Raphael Bossek
  • 1,904
  • 14
  • 25