comparison OrthancServer/Sources/ServerIndex.cpp @ 4551:350a22c094f2 db-changes

testing replay of transactions
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 02 Mar 2021 19:36:59 +0100
parents 5b929e6b3c36
children efd90f778cd2
comparison
equal deleted inserted replaced
4540:9c0cff7a6ca2 4551:350a22c094f2
677 unsigned int threadSleep) : 677 unsigned int threadSleep) :
678 done_(false), 678 done_(false),
679 db_(db), 679 db_(db),
680 maximumStorageSize_(0), 680 maximumStorageSize_(0),
681 maximumPatients_(0), 681 maximumPatients_(0),
682 mainDicomTagsRegistry_(new MainDicomTagsRegistry) 682 mainDicomTagsRegistry_(new MainDicomTagsRegistry),
683 maxRetries_(0)
683 { 684 {
684 listener_.reset(new Listener(context)); 685 listener_.reset(new Listener(context));
685 db_.SetListener(*listener_); 686 db_.SetListener(*listener_);
686 687
687 // Initial recycling if the parameters have changed since the last 688 // Initial recycling if the parameters have changed since the last
2620 if (instancesId != NULL) 2621 if (instancesId != NULL)
2621 { 2622 {
2622 CopyListToVector(*instancesId, instancesList); 2623 CopyListToVector(*instancesId, instancesList);
2623 } 2624 }
2624 } 2625 }
2626
2627
2628
2629
2630
2631 /***
2632 ** PROTOTYPING FOR DB REFACTORING BELOW
2633 ***/
2634
2635 ServerIndex::ExpandResourceOperation::ExpandResourceOperation(const std::string& resource,
2636 ResourceType level) :
2637 found_(false),
2638 resource_(resource),
2639 level_(level)
2640 {
2641 }
2642
2643
2644 void ServerIndex::ExpandResourceOperation::Apply(ServerIndex::ReadOnlyTransaction& transaction)
2645 {
2646 found_ = transaction.LookupResource(item_, resource_, level_);
2647 }
2648
2649
2650 const Json::Value& ServerIndex::ExpandResourceOperation::GetResource() const
2651 {
2652 if (found_)
2653 {
2654 return item_;
2655 }
2656 else
2657 {
2658 throw OrthancException(ErrorCode_BadSequenceOfCalls);
2659 }
2660 }
2661
2662
2663 class ServerIndex::ReadOnlyWrapper : public IReadOnlyOperations
2664 {
2665 private:
2666 ReadOnlyFunction func_;
2667
2668 public:
2669 ReadOnlyWrapper(ReadOnlyFunction func) :
2670 func_(func)
2671 {
2672 assert(func_ != NULL);
2673 }
2674
2675 virtual void Apply(ReadOnlyTransaction& transaction)
2676 {
2677 func_(transaction);
2678 }
2679 };
2680
2681
2682 class ServerIndex::ReadWriteWrapper : public IReadWriteOperations
2683 {
2684 private:
2685 ReadWriteFunction func_;
2686
2687 public:
2688 ReadWriteWrapper(ReadWriteFunction func) :
2689 func_(func)
2690 {
2691 assert(func_ != NULL);
2692 }
2693
2694 virtual void Apply(ReadWriteTransaction& transaction)
2695 {
2696 func_(transaction);
2697 }
2698 };
2699
2700
2701 void ServerIndex::ApplyInternal(IReadOnlyOperations* readOperations,
2702 IReadWriteOperations* writeOperations)
2703 {
2704 if ((readOperations == NULL && writeOperations == NULL) ||
2705 (readOperations != NULL && writeOperations != NULL))
2706 {
2707 throw OrthancException(ErrorCode_InternalError);
2708 }
2709
2710 unsigned int count = 0;
2711
2712 for (;;)
2713 {
2714 try
2715 {
2716 if (readOperations != NULL)
2717 {
2718 ReadOnlyTransaction transaction(*this);
2719 readOperations->Apply(transaction);
2720 }
2721 else
2722 {
2723 assert(writeOperations != NULL);
2724 ReadWriteTransaction transaction(*this);
2725 writeOperations->Apply(transaction);
2726 }
2727
2728 return; // Success
2729 }
2730 catch (OrthancException& e)
2731 {
2732 if (e.GetErrorCode() == ErrorCode_DatabaseCannotSerialize)
2733 {
2734 if (count == maxRetries_)
2735 {
2736 throw;
2737 }
2738 else
2739 {
2740 count++;
2741 boost::this_thread::sleep(boost::posix_time::milliseconds(100 * count));
2742 }
2743 }
2744 else if (e.GetErrorCode() == ErrorCode_DatabaseUnavailable)
2745 {
2746 if (count == maxRetries_)
2747 {
2748 throw;
2749 }
2750 else
2751 {
2752 count++;
2753 boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
2754 }
2755 }
2756 else
2757 {
2758 throw;
2759 }
2760 }
2761 }
2762 }
2763
2764 void ServerIndex::Apply(IReadOnlyOperations& operations)
2765 {
2766 ApplyInternal(&operations, NULL);
2767 }
2768
2769 void ServerIndex::Apply(IReadWriteOperations& operations)
2770 {
2771 ApplyInternal(NULL, &operations);
2772 }
2773
2774 void ServerIndex::Apply(ReadOnlyFunction func)
2775 {
2776 ReadOnlyWrapper wrapper(func);
2777 Apply(wrapper);
2778 }
2779
2780 void ServerIndex::Apply(ReadWriteFunction func)
2781 {
2782 ReadWriteWrapper wrapper(func);
2783 Apply(wrapper);
2784 }
2625 } 2785 }