Friday, May 6, 2011

Java date iterator factory, with rules specifying how to calculate the intervals

I am looking for a Java class where I can specify a set of date rules, such as "every 3rd sunday" and "the first occurrence of a monday every second month". I want to be able to get something like an infinite iterator out of it (.next() would return the next date matching the rules set).

I think I'd be able to build it myself - but calendars are a hassle, and it feels like something similar should exist already. I hate being the one to reinvent a crappier wheel.

Is anyone aware of something like this? I have been looking at JODA, and it seems to lay the groundwork for it, but does not appear to give the full functionality I want..

From stackoverflow
  • I don't think there's any readily made iterators for joda-time or Java Calendar API for that matter, however with joda it's so easy that you should just go with it. For example after re-familiarizing myself with joda after a few months pause I made this in about 10 minutes:

    public class InstantIterator implements Iterator<Instant>,
                                            Iterable<Instant> {
    
        private Instant current;
        private final Instant original;
        private Duration skip;
        private List<Instant> skipThese;
    
        public InstantIterator(Instant startFrom, Duration skip) {
            this.current = original = startFrom;
            this.skip = skip;
            skipThese = new Vector<Instant>();
        }
    
        public boolean hasNext() {
            return true;
        }
    
        public Instant next() {
            Instant currentNext = current.toInstant();
            current = current.plus(skip);
            while (skipThese.contains(currentNext)) {
                currentNext = current.toInstant();
                current = current.plus(skip);
            }
            return currentNext;
        }
    
        public void remove() {
            skipThese.add(current.toInstant());
        }
    
        public Iterator<Instant> iterator() {
            return this;
        }
    
        public void rewind() {
            current = original.toInstant();
        }
    
        public void resetRemoved() {
            skipThese.clear();
        }
    
        public void resetIterator() {
            rewind();
            resetRemoved();
        }
    }
    

    Joda Time is awesome :-)

    Jacob Hansson : You rocked my afternoon - thanks mate!
    Esko : While not important at all, I just want to say that this is the second Iterator I've ever made which supports remove(), usually I've found it to be too cumbersome to support it.
  • My company has something very much like you want but it's for .NET so it probably won't do you any good.

  • You could have a look at Quartz which is designed for job scheduling.

How to get dynamic intervals in Dundas / Microsoft Chart Controls for WinForms?

Hi,

I'm using the Microsoft Chart Controls for .NET 3.5, and struggles with getting the chart control to support window/control resizing.

I have graphs where the X value is dates, and want the chart to display the maximum available of intervals/labels on the chart axis when I resize the window.

The closes I've come is to call this from the PrePaint event:

double interval = chart.Series[0].Points.Count / ((double)chart.Width / 90);
foreach (var area in chart.ChartAreas.Where(ca => ca.Visible))
{
    area.AxisX.Interval = interval;
}

This makes the intervals and labels fit perfectly along the X axis, but the dates are not shown correctly. This first label seems to be right (some date in 2008), but the rest of the labels along the axis are displayed as some date in 1900 instead.

Anyone know the preferred way of doing this?

From stackoverflow
  • for dates in ms charts u have to explicitly specify the minimum dates to start with. otherwise it takes 29th december 1899 as origin. u can set minimum and max dates on chart like

    chart.ChartAreas[0].AxisY.Minimum = (new DateTime(2010, 5, 1)).ToOADate();
                    chart.ChartAreas[0].AxisY.Maximum = (new DateTime(2011, 4, 1)).ToOADate();
    

    this post has some valuable information on ms charts HTH
    regards

Parsing XML with AS3

Here is my entire Script as I can't seem to figure out where the problem is.

The symptoms are that where I addChild(book) , is not the appropriate place for this to be added properly and sequentially with the thumbs as well. As a result, and to my surprise, the only way I can get these to appear so far is by writing a faulty trace statement which somehow pops them up in the top left corner. Any suggestions would be greatly appreciated! Thank you!!

var rowsW:Number;
var my_xW:Number;
var my_yW:Number;
var my_thumb_widthW:Number;
var my_thumb_heightW:Number;
var my_imagesW:XMLList;
var my_totalW:Number;

var container_mcW:MovieClip;
var preloaders_mcW:MovieClip;
var book:TextField = new TextField();
var author:TextField = new TextField();
var publisher:TextField = new TextField();
book.selectable = true;

var x_counterW:Number = 0;
var y_counterW:Number = 0;

var my_tweensW:Array = [];
var container_mc_tweenW:Tween;

var myXMLLoaderW:URLLoader = new URLLoader();

myXMLLoaderW.load(new URLRequest("WORKS.xml"));
myXMLLoaderW.addEventListener(Event.COMPLETE, processXMLW);

createContainerW();
callThumbsW();

function processXMLW(e:Event):void {
var myXMLW:XML = new XML(e.target.data);

rowsW = myXMLW.@ROWS;
my_xW = myXMLW.@XPOSITION;
my_yW = myXMLW.@YPOSITION;
my_thumb_widthW = myXMLW.@WIDTH;
my_thumb_heightW = myXMLW.@HEIGHT;
my_imagesW = myXMLW.IMAGE;
my_totalW = my_imagesW.length();

myXMLLoaderW.removeEventListener(Event.COMPLETE, processXMLW);
myXMLLoaderW = null;

}

function createContainerW():void {
container_mcW = new MovieClip();
container_mcW.x = my_xW;
container_mcW.y = my_yW;
addChild(container_mcW);


preloaders_mcW = new MovieClip();
preloaders_mcW.x = container_mcW.x;
preloaders_mcW.y = container_mcW.y;
addChild(preloaders_mcW);
}

function callThumbsW():void {
for (var i:Number = 0; i < my_totalW; i++) {

 var thumb_urlW = my_imagesW[i].@THUMB;
 book.text = my_imagesW[i].@TITLE;
 author.text = my_imagesW[i].@AUTHOR;
 publisher.text = my_imagesW[i].@PUBLISHER;

 var thumb_loaderW = new Loader();
 thumb_loaderW.load(new URLRequest(thumb_urlW));

thumb_loaderW.contentLoaderInfo.addEventListener(Event.COMPLETE,thumbLoadedW);

 thumb_loaderW.name = i;

 book.x = (40)*x_counterW;
 book.y = (my_thumb_heightW+40)*y_counterW;
 thumb_loaderW.x = (my_thumb_widthW+10)*x_counterW;
 thumb_loaderW.y = (my_thumb_heightW+10)*y_counterW;


 container_mcW.addChild(book);
 container_mcW.addChild(author);
 container_mcW.addChild(publisher);
 trace("my x equals" (book.x));

 if (y_counterW+1 < rowsW) {
  y_counterW++;
 } else {
  y_counterW = 0;
  x_counterW++;
 }
 var preloader_pbW:ProgressBar = new ProgressBar();
 preloader_pbW.source = thumb_loaderW.contentLoaderInfo;
 preloader_pbW.x = thumb_loaderW.x;
 preloader_pbW.y = thumb_loaderW.y;
 preloader_pbW.width = my_thumb_widthW;
 preloader_pbW.height = my_thumb_heightW;
 preloaders_mcW.addChild(preloader_pbW);

 preloader_pbW.addEventListener(Event.COMPLETE, donePbW);

}
}

function thumbLoadedW(e:Event):void {
var my_thumbW:Loader = Loader(e.target.loader);
container_mcW.addChild(my_thumbW);

my_tweensW[Number(my_thumbW.name)]=new Tween(my_thumbW, "alpha", Strong.easeIn, 0,1,0.5, true);

my_thumbW.contentLoaderInfo.removeEventListener(Event.COMPLETE, thumbLoadedW);
}

function donePbW(e:Event):void {
var my_pbW:ProgressBar = ProgressBar(e.target);
preloaders_mcW.removeChild(my_pbW);
my_pbW.removeEventListener(Event.COMPLETE, donePbW);
}
From stackoverflow
  • It doesn't seem to me that you're loading your XML properly. You should be using a URLLoader, not a loader to load the XML. You'll then need to wait til Event.COMPLETE is trigged to parse the data and add your text fields.

    From sephiroth (http://www.sephiroth.it/tutorials/flashPHP/E4X/):

    import flash.net.URLLoader
    import flash.net.URLRequest
    import flash.xml.XML
    import flash.event.*
    import flash.error.*
    
    var mainXML:XML;
    var loader:URLLoader = new URLLoader();
    loader.addEventListener(Event.COMPLETE, onComplete);
    loader.load(new URLRequest("http://www.sephiroth.it/tutorials/flashPHP/E4X/files/test.xml"));
    
    
    function onComplete(evt:Event)
    {
        mainXML = new XML(loader.data)
        trace("xml loaded, start parsing using E4X syntax");
    }
    

    Well, now that you've massively edited your question, yes, it's obvious you already have that in there. I'd try tracing/watching loader.data before you try to parse it into an xml object to see exactly what data you're trying to parse into XML. It sounds like you're maybe getting some data you're not expecting.

    Trip : Hmm..I already had that part in there. I have reason to believe its in its sequential order. Will let you know when I find an answer. Thanks!
    Trip : Haha sorry about the massive edit, I figured out how to place code in the comments ;) . I've been doing a lot of tracing, and its definately there. Occasionally I can see a glimpse of it, but only in a failed trace statement. Wierd right? I can't imagine how this works.
    Trip : Still working on this. Its been one week so far, I havn't been able to break out anything. I have read every single url regarding parsing xml attributes, and I can not figure this out.
    quoo : Hm. Not quite sure what you mean by a failed trace. If I understand correctly, if you trace 'e.target.data' in the complete handler, you get the correct xml? If so, I'd try simplifying your code, removing the tweens for now, and just trying to place your mc's once they're filled with content. There's a lot going on in your code, maybe the xml is not what's breaking things?
    Beau Martínez : Awesome link, been looking for some definitive xml-parsing howto for as3 to no real avail.
  • The reason that my code at best only displayed one result from the XML List is because I instantiate a TextField outside of the loop that draws the var i from the XML's many nodes. To correctly display a textfield for each node, I need to instantiate the TextField for each var i. Like this :

    function callThumbsW():void {
    for (var i:Number = 0; i < my_totalW; i++) {
    
     var thumb_urlW = my_imagesW[i].thumb;
     var book = my_imagesW[i].TITLE;
    
     var textHolder:TextField = new TextField;
     container_mcW.addChild(textHolder);
    

    This causes instant gratification.

ASP.NET MVC Deployed project not working: Inheritance System.web.mvc.viewpage fails?

Hi,

i have a MVC project which runs perfectly local. When I deploy to the production server I receive a compilation error:

BC30456: 'Title' is not a member of 'ASP.views_home_index_aspx'.

It makes me think that the inheritance of System.Web.ViewPage(of T) fails somehow... The project is deployed under http://server/ProjectName. The website at http://server/ works fine when deployed....

Can somebody assist? Thanks

UPDATE
Does everybody notice: "not a member of 'ASP.views_home_index_aspx'. ?
Shouldn't this be "not a member of 'ProjectName.views_home_index_aspx'. ?

From stackoverflow
  • Have you got the MVC DLL in your projects BIN folder.

    It is installed on your machine at C:\Program Files\Microsoft ASP.NET\ASP.NET MVC 1.0\Assemblies

    EDIT: Does the class ASP.views_home_index_aspx inherit from System.Web.Mvc.ViewPage ?

    EDIT 2: Every page in a .net web application and/or MVC site is compiled into a class. The class names are generated from the location of the file. So views_home_index_aspx is the compiled class that represents your aspx file at views/home/index.aspx . It has converted the "/" and "." to underscores.

    So your error is saying that, the compiled class that your page generates does not contain the Title member you are trying to access.

    Is this a big error that your page throws with a stack trace? or do you get the error in visual studio. Can you post some code?

    Ropstah : Yes it's there. I'm using a websetup deployment project and the assembly is found as dependency so it's automatically included. :(
    Ropstah : You're getting close I think. I have /views/home/index.aspx. This page has an inherits attribute which says: "System.Web.Mvc.ViewPage(Of IEnumerable(Of Models.Project))". However I cannot find the class views_home_index_aspx anywhere....
  • Best go through these posts just in case its something you have missed.

    Ropstah : Thanks for the urls, but both sites don't mention anything related to this issue...

Concurrency between windows and Web app C# asp.net

Hi, I am wondering if there is a best practice in regards to concurrency between asp.net and a windows app using the same db? I have a db with items their quantity, any of these items can be sold with different quantities from a website shopping cart or store location. updating the item quantity when user adds the item to their shopping cart seems to be a problematic approach.

Thanks

From stackoverflow
  • not knowing the business rules regarding how an item can be reserved in a basket (such as basket expiry) can you not maintain 'reserved' and 'sold' counts?

    Ok so based on what you say. I'd think perhaps a compensation based system rather than a transaction based system for handling this. Yes you have transactions on sale but thw reserve you don't

    Sammy : The database is an SQL 2008 and is setup to be almost identical to NorthWind db, I ended up adding an extra field to the Items table "InSessionCount" which will be a nullable int. when the user "any user" checks out the item, we put the selected quantity in the InSessionCount, if the transaction is approved, we modify the quantity row else we reset the InSessionCount field to null and the order is cancelled.
  • I would suggest introducing a Gateway into your system. The Gateway would be a single application (or part of one of your existing applications: the winforms app or the web app) that acts as the intermediary to the DB for both the windows app and the web app. This way, you only have one point of failure between db requests and the db itself. Design that Gateway to manage concurrency and then you dont have to worry about other processes writing to your db.

    If i was in your position i might select the web app to be the gateway. Create a set of services/repositories to interface with the db and have the UI layer of the web app access those services. Then expose those services via web services (or WCF services) to your windows app for it to access the db instead of it accessing the db directly. This way you have decoupled your system and created a single point of entry to the data store. You'll probably solve a whole lot more problems other than concurrency with this approach.

Connection refused after some time on threaded process in tcp socket requests (c/linux)

I'm trying to make process that takes number of requests each second, on each request new thread is created. Each thread then opens socket connection to address (http port) sends HEAD requests, gets response and closes socket.
Problem I'm having comes when i put more then 3 requests per second, after some time i get error in send() part of function, i keep getting Connection Refused. If I input more requests per second i get errors earlier. If i put only 2 requests per second i don't get errors at all. I suspect that I'm running out of some resource but i can't find which.

Here is basic structure of code

//declarations

socketfd = socket(servinfo->ai_family,servinfo->ai_socktype,servinfo->ai_protocol);

arg = fcntl(socketfd, F_GETFL, NULL)) < 0);
arg |= O_NONBLOCK;
fcntl(socketfd, F_SETFL, arg)

if((conn = connect(socketfd, servinfo->ai_addr, servinfo->ai_addrlen)) < 0)
{
 if(errno == EINPROGRESS)
 {
  do
  {
            tv.tv_sec = CONNECT_TIMEOUT;
   tv.tv_usec = 0;
   FD_ZERO(&myset);
   FD_SET(socketfd, &myset);
   if((res = select(socketfd+1, NULL, &myset, NULL, &tv) > 0)) 
   {
    if( (arg = fcntl(socketfd, F_GETFL, NULL)) < 0) { 
     perror("fcntl get 2");
    } 
    arg &= (~O_NONBLOCK); 
    if( fcntl(socketfd, F_SETFL, arg) < 0) {
     perror("fcntl set 2");
    }
    char szBuf[4096];

    std::string htmlreq = "HEAD / HTTP/1.1\r\nHost:";
    htmlreq += info->hostName;
    htmlreq += "\r\n\r\n";

    if((conn = send(socketfd,htmlreq.c_str(),htmlreq.size(),0)) == -1 && errno != EINTR)
    {
     perror("send");
     close(socketfd);
     return;
    }

    if((conn = recv(socketfd,szBuf,sizeof(szBuf)+1,0)) < 0 && errno != EINTR)
    {
     perror("recv");
     close(socketfd);
     return ;
    }

    close(socketfd);

    // do stuff with data
    break;
   }
   else
   {
                //timeout
    break;
   }
  }while(1);
 }
 else
 {
  perror("connect");
  close(socketfd);
  return; 
 }
}

I removed some error checking from start, what i get as output is "Send: Connection Refused" after some time. I'd appreciate some pointers to what part could be causing problems, platform is ubuntu linux. I'd also be glad to post other parts of code if needed. Tnx in advance.

From stackoverflow
  • The resource you're probably running out of is on the server you're connecting to. The connection is being refused by the computer you're connecting to because it's either:

    1. Configure to limit the number of connections per second ( based on some criteria )
    2. Or the server you're connecting to is under too much load for some reason and can't take any more connections.

    Since you always get the error on the third connection it could be that the server you're connecting to limits the number of connections on a per IP basis.

    Edit1

    You're trying to do a non-blocking connect? Now that I look at it closer it sounds like your problem is with the select, as in select is returning that the socket is readable before it's actually connected and then you're calling send. One of the things to watch out for on non-blocking connects is that the socket becomes both readable and writeable on error. Which means you need to check for both after select returns otherwise you may be missing whatever the actual error is and seeing the send error instead.

    This is from Stevens UNP:

    FD_ZERO(&rset);
    FD_SET(sockfd, &rset);
    wset = rset;
    tval.tv_sec = nsec;
    tval.tv_usec = 0;
    
    if ( (n = Select(sockfd+1, &rset, &wset, NULL,
         nsec ? &tval : NULL)) == 0) {
     close(sockfd);  /* timeout */
     errno = ETIMEDOUT;
     return(-1);
    }
    
    if (FD_ISSET(sockfd, &rset) || FD_ISSET(sockfd, &wset)) {
     len = sizeof(error);
     if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
      return(-1);   /* Solaris pending error */
    } else
     err_quit("select error: sockfd not set");
    
    done:
    Fcntl(sockfd, F_SETFL, flags); /* restore file status flags */
    
    if (error) {
     close(sockfd);  /* just in case */
     errno = error;
     return(-1);
    }
    return(0);
    
    Robert S. Barnes : You're not checking for readability during the select which may mean you're missing a select error, see above.
  • There are quite a few problems in your code.

    First you set the socket to non blocking. I don't understand why you do this. The connect function has an internal timeout and so won't block.

    Another problem of your code is that the first if statement will skip the instruction block if the connection immediately succeeds ! Which may happen.

    You apparently want to first send the HEAD message. There is no real need to make this one non blocking unless you expect the remote server or the network to be very slow and want a time out on it. In this case the select with non blocking socket would make sens.

    Once you send the HEAD message, you expect some data in response that you collect with the recv function. Be aware that this function call may return before the whole data sent is received. You need an independent way to determine that all the data sent has been received. Would the server close the connection ? This would detected by the recv function returning 0.

    So the recv should be wrapped into a loop where you append to received data to some buffer or a file and quit when recv returns 0. Use a non blocking socket if you want to add a timeout on this recv operation which may indeed block.

    But first try without timeouts to be sure it works at full speed without blocking as your current version.

    I suspect the initial connect is slow because of name and IP adresse resolution, and gets faster in subsequent calls because data is cached.

    Robert S. Barnes : The connect function can take a very long time to timeout ( 10 or 11 minutes on Linux iirc ) and you often don't want to wait that long. You should read up on non-blocking connects and why they're used.

How can I restore process state after a crash?

What's a good way to persist state when restarting a crashed process?

I have a supervisor in an OTP application what watches several "subsystem" gen_servers.

For example, one is a "weather" subsystem that generates a new weather state every 15 minutes and handles queries for the current state of the weather. (Think the lemonade stand game)

If that gen_server crashes, I want it to be restarted, but it should be be restarted with the most recent weather state, not some arbitrary state hardcoded in init(). It wouldn't make sense for the simulation state to suddenly go from "hail storm" to "pleasant and breezy" just because of the crash.

I hesitate to use mnesia or ETS to store the state after every update because of the added complexity; is there an easier way?

From stackoverflow
  • As long as it just has to be during runtime a would suggest the usage of ETS. The value is by far greater than the complexity. The API is simple and if you're working with named tables the access is simple too. You only have to create the table before your gen_server is started by the supervisor.

    Two - more complex - alternatives:

    • Build a pair of processes, one for the job to do, one for the state maintenance. Due to the simplicity of the second one it would be really reliable.
    • A real silly one could be the exchange of the child spec of the supervisor with the current state as argument each time the state is changing. (smile) No, just kidding.
  • is there an easier way?

    when process died it sends message to supervisor that containing State of process, so you can use this value to store in supervisor (in mnesia or supervisor's state) and when your server will start (in init) it have to send sync call to supervisor to get State value. I haven't real example, but i hope it makes sense.

    Anyway i don't really see problem to store State in mnesia.

    sorry my English :)

asp.net doesn't render Sys.WebForms.PageRequestManager._initialize code

i'm using the ASP.NET 2.0 Ajax Extensions on a web site. as always, everything is fine on local but the remote web site does not use ajax calls. my local server has the ASP.NET Ajax extensions installed but the remote one doesn't. i know that i should be able to use the Ajax extensions without installing them. so in turn, i added the extensions' .dll among the web site's references but still no luck.

after my further investigation, i found out that local and remote pages have exactly the same HTML code rendered, except that the local (working) one has these lines

//<![CDATA[ Sys.WebForms.PageRequestManager._initialize('ctl00$ContentPlaceHolder1$ScriptManager1', document.getElementById('aspnetForm')); Sys.WebForms.PageRequestManager.getInstance()._updateControls(['tctl00$ContentPlaceHolder1$updReportArgs','tctl00$ContentPlaceHolder1$updReport'], ['ctl00$ContentPlaceHolder1$chkTumu','ctl00$ContentPlaceHolder1$btnGetir'], [], 90); //]]>

obviously, these are the lines of code that make callbacks possible. the question is why doesn't asp.net render these lines? what could be missing?

by the way, the ScriptResource.axd and WebResource.axd doesn't give a 404 or anything, i can see through their js codes via Firebug.

and one more thing: i'm unsure if it is related or not, but there are client-side asp.net validators on the page whose js code are not rendered either. again, those work fine locally.

for further investigation you can see the remote site here : http://www.ajitatif.com/subdomains/nazer/Raporlar/danismanbasarim.aspx

From stackoverflow
  • I had this same problem but I was adding a ScriptManager control as a child control to a custom user control. I forgot to make a call to the RenderControl method. Once I added that call, it added the aforementioned lines of javascript. So I'm not sure if you are using a user control or not, but I'm pretty sure the Render routines are not being called for your ScriptManager somehow.

  • Does the web.config for the remote web site have <xhtmlConformance mode="Legacy"/> set?

    This setting prevents partial page updates from working. Change the mode to Transitional or Strict, or remove the xhtmlConformance setting.

    http://weblogs.asp.net/scottgu/archive/2006/12/10/gotcha-don-t-use-xhtmlconformance-mode-legacy-with-asp-net-ajax.aspx

Rails nested resources

I'm trying to get my head around nested associations in Rails using ActiveResource. My example is as follows: What I have is an airport with many runways.

My show action in airports controller contains: @airport = Airport.find(params[:id])

When I call http://localhost/airports/2.xml I get that piece of XML:

<airport>
  <code>DUS</code>
  <created-at type="datetime">2009-02-12T09:39:22Z</created-at>
  <id type="integer">2</id>
  <name>Duesseldorf</name>
  <updated-at type="datetime">2009-02-12T09:39:22Z</updated-at>
</airport>

Now, I changed the action to

@airport = Airport.find(params[:id], :include => :runways)

How can I achieve that above loading above URL is giving me something like:

<airport>
  <code>FRA</code>
  <created-at type="datetime">2009-02-12T09:39:22Z</created-at>
  <id type="integer">2</id>
  <name>Frankfurt</name>
  <updated-at type="datetime">2009-02-12T09:39:22Z</updated-at>

  <runways>
    <runway>
      <id>1</id>
      <name>bumpy runway</name>
    </runway>
  </runways>

</airport>

And on top of that: If I have a client with

class Airport < ActiveResource::Base
  ..
end

and

class Runway < ActiveResource::Base
  ..
end

How can I get it to automatically load associations like:

a = Airport.find(1)
puts a.runways.length
=> 1

And (last but not least): Is there a way to store data from the client like:

a = Airport.find(1)
a.runways << Runway.find(1)
a.save

Maybe I'm really too blind, but I'm stuck... Any idea is warmly welcome.

Thanks

Matt

From stackoverflow
  • Resolved it myself finally. Wasn't aware to put the include into the render statememt:

    def show
      @airport = Airport.find(params[:id], :include => :runways)
      respond_to do |format|
        format.html # show.html.erb
        format.xml  { render :xml => @airport.to_xml(:include => :runways) }
      end
    end
    
    Stefan : Thank you. This isn't documented in many places. I've had to search for a long time.
  • The :include option for the finder specifies that it should eagerly fetch the related items from the database. The :include option for to_xml specifies that it should be included in the XML rendering.

    If the canonical XML representation includes the related objects, you can override the to_xml method to make your life a little simpler:

    class Airport
      def to_xml(options={})
        super(options.merge(:include => :runways))
      end
    end
    

    and then since render will call to_xml if you don't, your controller code can simply be

    format.xml { render :xml => @airport }
    
    Matt : thanks, makes perfect sense!

JQuery fadeIn() on DOM element creation?

How do I create a DOM element in JQuery and fade it in to show up, instead of having it show up immediately?

I try this:

var myDiv = "<div>Hello!</div>"
$("somePlace").after(myDiv).fadeIn('fast');

but this doesn't work, since the .after(myDiv) makes it popup immediately. Any solutions? Thanks!

From stackoverflow
  • Add it with a class which is hidden at the start.

    <style>
    .hidden {
      display:none;
    }
    </style>
    
    <div class="hidden">
     Won't be seen.
    </div>
    
  • $("<div>Hello</div>").hide().appendTo("somePlace").fadeIn("fast");
    
    altCognito : +1 Sigh. Oh yeah, good point.
    Jasie : Thanks cletus! Thanks altCognito for the alternative.

dynamic control values

how to get the text of the dynamical textbox which is added to the dynamical table,which is added to the panel on the form ,that form is having the masterpage

From stackoverflow
  • Presumably your talking about ASP.NET ? Are you trying to get the value in the code behind or client side using Javascript.

    If its server side, and your <asp:Panel> is declared on the page, you could do...

    foreach(Control c in myPanel.Controls)
    {
        if(c.GetType() == typeof(TextBox))
        {
            TextBox tb = c as TextBox;
            if(tb.ID == "The ID Your Looking For")
            {
               //Do stuff with tb.Text;
            }
        }
    }
    

How do I get information from wikipedia into my application.

Hi guys I wish to get information for entries I have in my database from wikipedia like for example some stadiums and country information. I'm using Zend Framework and also how would I be able to handle queries that return multiple ambiguous entries or the like.. I would like all the help I can get here...

From stackoverflow
  • Do a simple HTTP request to the article you are looking to import. Here's a good library which might help with parsing the HTML, though there are dozens of solutions for that as well, including using the standard DOM model which is provided by php.

    <?php
    require_once "HTTP/Request.php";
    
    $req =& new HTTP_Request("http://www.yahoo.com/");
    if (!PEAR::isError($req->sendRequest())) {
        echo $req->getResponseBody();
    }
    ?>
    

    Note, you will be locked out of the site if your traffic levels are deemed too high. (If you want a HUGE number of articles, download the database)

    Ali : Whoa I didn't know that you could do that - I've downloaded the database and started a new thread on the meticulous task of importing it - thanks for the tip man :)
  • Wikipedia based on MediaWiki, that have API.

    You can try MediaWiki API on Wikipedia - http://en.wikipedia.org/w/api.php

    Documentation for MediaWiki API - http://www.mediawiki.org/wiki/API

    Ali : Thats cool but are there any tutorials or starter code to get started. Basically I don't have the exact name of the article just an entry in my database and I was wondering if I could get a corresponding article from the wikipedia.

Compile a DLL in C/C++, then call it from another program

I want to make a simple, simple DLL which exports one or two functions, then try to call it from another program... Everywhere I've looked so far, is for complicated matters, different ways of linking things together, weird problems that I haven't even begun to realize exist yet... I just want to get started, by doing something like so:

Make a DLL which exports some functions, like,

int add2(int num){
   return num + 2;
}

int mult(int num1, int num2){
   int product;
   product = num1 * num2;
   return product;
}

I'm compiling with MinGW, I'd like to do this in C, but if there's any real differences doing it in C++, I'd like to know those also. I want to know how to load that DLL into another C (and C++) program, and then call those functions from it. My goal here, after playing around with DLLs for a bit, is to make a VB front-end for C(++) code, by loading DLLs into visual basic (I have visual studio 6, I just want to make some forms and events for the objects on those forms, which call the DLL).

I need to know how to call gcc (/g++) to make it create a DLL, but also how to write (/generate) an exports file... and what I can/cannot do in a DLL (like, can I take arguments by pointer/reference from the VB front-end? Can the DLL call a theoretical function in the front-end? Or have a function take a "function pointer" (I don't even know if that's possible) from VB and call it?) I'm fairly certain I can't pass a variant to the DLL...but that's all I know really.

update again

Okay, I figured out how to compile it with gcc, to make the dll I ran

gcc -c -DBUILD_DLL dll.c
gcc -shared -o mydll.dll dll.o -Wl,--out-implib,libmessage.a

and then I had another program load it and test the functions, and it worked great, thanks so much for the advice, but I tried loading it with VB6, like this

Public Declare Function add2 Lib "C:\c\dll\mydll.dll" (num As Integer) As Integer

then I just called add2(text1.text) from a form, but it gave me a runtime error:

"Can't find DLL entry point add2 in C:\c\dll\mydll.dll"

this is the code I compiled for the DLL:

#ifdef BUILD_DLL
#define EXPORT __declspec(dllexport)
#else
#define EXPORT __declspec(dllimport)
#endif

EXPORT int __stdcall add2(int num){
  return num + 2;
}

EXPORT int __stdcall mul(int num1, int num2){
  return num1 * num2;
}

calling it from the C program like this worked, though:

#include<stdio.h>
#include<windows.h>

int main(){

  HANDLE ldll;
  int (*add2)(int);
  int (*mul)(int,int);

  ldll = LoadLibrary("mydll.dll");
  if(ldll>(void*)HINSTANCE_ERROR){
    add2 = GetProcAddress(ldll, "add2");
    mul = GetProcAddress(ldll, "mul");
    printf("add2(3): %d\nmul(4,5): %d", add2(3), mul(4,5));
  } else {
    printf("ERROR.");
  }

}

any ideas?

solved it

To solve the previous problem, I just had to compile it like so:

gcc -c -DBUILD_DLL dll.c
gcc -shared -o mydll.dll dll.o -Wl,--add-stdcall-alias

and use this API call in VB6

Public Declare Function add2 Lib "C:\c\dll\mydll" _
    (ByVal num As Integer) As Integer

I learned not to forget to specify ByVal or ByRef explicitly--I was just getting back the address of the argument I passed, it looked like, -3048.

From stackoverflow
  • The thing to watch out for when writing C++ dlls is name mangling. If you want interoperability between C and C++, you'd be better off by exporting non-mangled C-style functions from within the dll.

    You have two options to use a dll

    • Either use a lib file to link the symbols -- compile time dynamic linking
    • Use LoadLibrary() or some suitable function to load the library, retrieve a function pointer (GetProcAddress) and call it -- runtime dynamic linking

    Exporting classes will not work if you follow the second method though.

  • There is but one difference. You have to take care or name mangling win C++. But on windows you have to take care about 1) decrating the functions to be exported from the DLL 2) write a so called .def file which lists all the exported symbols.

    In Windows while compiling a DLL have have to use

    __declspec(dllexport)

    but while using it you have to write __declspec(dllimport)

    So the usual way of doing that is something like

    #ifdef BUILD_DLL
    #define EXPORT __declspec(dllexport)
    #else
    #define EXPORT __declspec(dllimport)
    #endif
    

    The naming is a bit confusing, because it is often named EXPORT.. But that's what you'll find in most of the headers somwhere. So in your case you'd write (with the above #define)

    int DLL_EXPORT add.... int DLL_EXPORT mult...

    Remember that you have to add the Preprocessor directive BUILD_DLL during building the shared library.

    Regards Friedrich

    xtofl : In here we don't use EXPORT but INEX. You could call it "SYMBOL", or something else direction-independent.
  • For VB6:

    You need to declare your C functions as __stdcall, otherwise you get "invalid calling convention" type errors. About other your questions:

    can I take arguments by pointer/reference from the VB front-end?

    Yes, use ByRef/ByVal modifiers.

    Can the DLL call a theoretical function in the front-end?

    Yes, use AddressOf statement. You need to pass function pointer to dll before.

    Or have a function take a "function pointer" (I don't even know if that's possible) from VB and call it?)

    Yes, use AddressOf statement.

    update (more questions appeared :)):

    to load it into VB, do I just do the usual method (what I would do to load winsock.ocx or some other runtime, but find my DLL instead) or do I put an API call into a module?

    You need to decaler API function in VB6 code, like next:

    Private Declare Function SHGetSpecialFolderLocation Lib "shell32" _
       (ByVal hwndOwner As Long, _
        ByVal nFolder As Long, _
        ByRef pidl As Long) As Long
    
    Jim Mack : When calling back into VB6 using AddressOf, be aware that the VB6 RT is not re-entrant, and not thread safe. If you're called back to from a different thread, you'll certainly crash if your function uses any intrinsic VB6 functions (invokes the RT again).
  • Regarding building a DLL using MinGW, here are some very brief instructions.

    First, you need to mark your functions for export, so they can be used by callers of the DLL. To do this, modify them so they look like (for example)

    __declspec( dllexport ) int add2(int num){
       return num + 2;
    }
    

    then, assuming your functions are in a file called funcs.c, you can compile them:

    gcc -shared -o mylib.dll funcs.c
    

    The -shared flag tells gcc to create a DLL.

    To check if the DLL has actually exported the functions, get hold of the free Dependency Walker tool and use it to examine the DLL.

    For a free IDE which will automate all the flags etc. needed to build DLLs, take a look at the excellent Code::Blocks, which works very well with MinGW.

    Edit: For more details on this subject, see the article Creating a MinGW DLL for Use with Visual Basic on the MinGW Wiki.

    xtofl : Checking the exports can also be done using the cmd-line tool DUMPBIN with argument /exports.
    Carson Myers : I tried the dumpbin, and realized (even though I thought I was keeping it in mind) that the names were mangled... I added Alias "add2@4" to my API call in VB, and now it calls it, but returns -3048, no matter what I call it with...
    anon : To get started, create C source files, not C++ (extension .c rather than .cpp). This will avoid most name-mangling issues.
    Carson Myers : I used C... also, I went to that blog and followed the instructions--do I need the DllMain function to call it from VB? Anyways, I compiled it to object code and then tried creating the DLL passing --add-stdcall-alias to the linker, and it said "Undefined reference to WinMain@16"...
    anon : No, you don't need DllMain, and you should never call it - it is used by the OS. If you get an undefined reference to Winmain, it thinks you are building an exe - did you remember the -shared flag?
    Carson Myers : no, I forgot it that time, haha. Well, I linked it successfully with the --add-stdcall-alias flag, and can do without the 'Alias "add2@4"' part of the API call in VB, but it's still just returning -3048... I'm passing the VB type Integer to the C type Int, and then the return value of Int is going to a textbox... any problems there?
    Carson Myers : Oh! Christ! I forgot ByVal in the API call.. It was just returning the address of the argument. When I took the argument by pointer in the DLL, it worked fine, then I remembered VB makes all the arguments ByRef by default... except for when it doesn't, anyway. Thanks for your help
  • Here is how you do it:

    In .h

    #ifdef BUILD_DLL
    #define EXPORT __declspec(dllexport)
    #else
    #define EXPORT __declspec(dllimport)
    #endif
    
    extern "C" // Only if you are using C++ rather than C
    {    
      EXPORT int __stdcall add2(int num);
      EXPORT int __stdcall mult(int num1, int num2);
    }
    

    in .cpp

    extern "C" // Only if you are using C++ rather than C
    {    
    EXPORT int __stdcall add2(int num)
    {
      return num + 2;
    }
    
    
    EXPORT int __stdcall mult(int num1, int num2)
    {
      int product;
      product = num1 * num2;
      return product;
    }
    }
    

    The macro tells your module (i.e your .cpp files) that they are providing the dll stuff to the outside world. People who incude your .h file want to import the same functions, so they sell EXPORT as telling the linker to import. You need to add BUILD_DLL to the project compile options, and you might want to rename it to something obviously specific to your project (in case a dll uses your dll).

    You might also need to create a .def file to rename the functions.aspx) and de-obfuscate the names (C/C++ mangles those names). This blog entry might be an interesting launching off point about that.

    Loading your own custom dlls is just like loading system dlls. Just ensure that the DLL is on your system path. C:\windows\ or the working dir of your application are an easy place to put your dll.

Beginning PHP development - programming stack recommendation and web site resources

I'm beginning PHP development, and I'm looking at picking brains for ideas and best practices, and also website resrouces, such as www.w3schools.com

Yes, it has to be PHP as that's the existing website technology that's being used.

I'm using Windows, although I'll be doing development on a Virtual Machine, maybe Virtual PC or Virtual Box running Windows 2000 or XP, or maybe Vista.

I'm an experienced VB6 and SQL Server developer, so I can use SQL Server as the back-end running on my host laptop, or I can use mySQL maybe. I'm thinking using SQL Server to start with would remove one layer of complexity, and allow me to concentrate on the webserver (Apache, maybe ?) and PHP and not have to worry too much about the database, as all that will be completely natural to me.

Framework recommendations, and one or two examples of what you have used them for will be appreciated.

Source and version control frameworks, tools, utilities and add-ins would also be appreciated.

I'm going to consider writing an answer to this question myself with my experiences as I get started, almost like a 'how-to' step-by-step guide so that anyone in the future who wants to do the same thing can get going even quicker.

Thanks in advance, gurus.

From stackoverflow
  • First thing I suggest you do is read How Is PHP Done the Right Way?

    For source control, Subversion is a decent place to start.

    You will need Firefox plus Firebug. Also look at What’s in your web-developer toolbox? and Free tools to speed up web development.

    In regards to frameworks, start with Choosing the right PHP framework.

    You probably should consider Javascript frameworks too, in which case start with JavaScript frameworks and CSS frameworks: JQuery, YUI, neither, or something else? and Which Javascript framework (jQuery vs Dojo vs … )?

  • Ignore frameworks to begin with. Once you have an idea about what php is/can, you can pick a framework. But don't do it as the first thing.

    As for setup, I would strongly recommend that you use a standard stack. That means Apache and MySql. You can run it on Windows for development mode. The differences between Windows and *nix are rather small for most PHP applications.

    For revision control you should probably use SVN, as it is the de-facto standard at the moment, and is fairly easy to use. You can download TortoiseSVN for Windows, if you don't like to use the command line.

    Use PDO for database connectivity, rather than the older mysql_* functions. It's the new standard in php5. Make sure that magic-quotes are disabled, and use prepared statements/bound parameters for binding data to queries.

    altCognito : +1, though I would point out that reading something like "How is PHP done the right way" can help a lot.
  • I've used PHP extensively on the WISP (Windows, IIS, SQLServer, PHP) stack. It's a little unusual as the vast majority of PHP use is on LAMP, but personally I've found it to be rock solid.

    If an MVC framework is not absolutely essential for you I would highly recommend the Tiny But Strong template system. I've used Smarty and various other PHP templating systems, but TBS wins hands down for me as it's very tight, clean and above all works with WYSIWYG page design programs such as Dreamweaver.

    cometbill : does PHP run natively on IIS or what do I need to install as this stack might suit me for tinkering, as I could also use it for .NET development.
  • A huge plus is that PHP itself is very well documented. It's once you start using other people's code that things begin to get hairy.

Rails error "NoMethodError" - My first ruby app

Hi,

I am absolutely and totally new to rails, so the answer is probably very simple. Here goes:

My page is generating this error

NoMethodError in Tasks#new
Showing app/views/tasks/new.erb where line #3 raised:

undefined method `tasks_path' for #

Here is the view:

<% form_for(@task) do |f| %>
    <%= f.error_messages %>

    <%= f.label :description %>:
    <%= f.text_field :description %><br />

    <%= f.label :priority %>:
    <%= collection_select(:news, :priority_id, Priority.find(:all), :id, :description) %><br />

    <%= f.submit "Add Task" %>

<% end %>

The controller:

class TasksController < ApplicationController
    def index
     @all_tasks = Task.find(:all, :order => :id)
    end

    def new
     @task = Task.new
    end ...(more)

and the model:

I can't see a problem, but like I said, I'm clueless so far. Thanks!

class Task < ActiveRecord::Base
    validates_presence_of :description

    belongs_to :priority
    has_and_belongs_to_many :staff
    has_and_belongs_to_many :catagory

end
From stackoverflow
  • Do you have

    map.resources :tasks
    

    in your routes?

    doctororange : Oh, you're right on. After adding that, I was able to use form_for with @task
    Jarrod : If you've got @task = Task.new in your action, the form_for(@task) references the empty task object. :task would make your form submit to tasks/new instead of a RESTful task/ with method='post'.
  • Have you generated this example using the scaffold generator? Because if not it might be that you forgot to define the ressource url mapping in the routes.rb:

    --- a/config/routes.rb
    +++ b/config/routes.rb
    @@ -1,4 +1,6 @@
     ActionController::Routing::Routes.draw do |map|
    +  map.resources :tasks
    +
    

    Don't forget to restart webrick after you've added the route!

    reto : Also checkout 'http://www.railsbrain.com/api/rails-2.3.2/doc/index.html?a=M000295&name=resources' Hmm.. and @task should work.
  • Thanks for the answers.

    As predicted, as simple problem.

    <% form_for(@task) do |f| %>
    

    should be:

    <% form_for(:task) do |f| %>
    

    Funny how you always find the answer to a question right after you post it! Thanks again.

  • Regarding this code:

    @all_tasks = Task.find(:all, :order => :id)
    

    You don't need to specify order by id because it's the default behavior. So this should suffice.

    @all_tasks = Task.find(:all)
    

    And this can be further condensed to the following

    @all_tasks = Task.find.all
    

    Furthermore, a rails convention is to name your instance variable @tasks

    @tasks = Task.find.all
    

    Have fun with Rails.

    doctororange : Thanks. Task.find.all gives the error "Couldn't find Task without an ID" ~$ ruby --version ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux] ~$ rails --version Rails 2.3.2

Removing element from web page through firefox extension

I'm going to develop a firefox extension which adds a button beside the file input fields (the <input type="file"> tag) when a file is selected.

The file overlay.js, which contains the extension's logic, manages the "file choose" event through this method:

var xpitest = {
    ...
    onFileChosen: function(e) {
        var fileInput = e.explicitOriginalTarget;
        if(fileInput.type=="file"){
            var parentDiv = fileInput.parentNode;
            var newButton = top.window.content.document.createElement("input");
            newButton.setAttribute("type", "button");
            newButton.setAttribute("id", "Firefox.Now_button_id");
            newButton.setAttribute("value", "my button");
            newButton.setAttribute("name", "Firefox.Now_button_name");
            parentDiv.insertBefore(newButton, fileInput);
        }
    }
    ...
}

window.addEventListener("change", function(e) {xpitest.onFileChosen(e)},false);

My problem is that, everytime I choose a file, a new button is being added, see this picture:

If I select the same file more than once, no new button appears (this is correct).

As we can see, on the first file input, only one file has been selected.

On the second one I've chosen two different files, in effect two buttons have been created...

On the third, I've chosen three different files.

The correct behavior should be this:

  • when a file is chosen, create my_button beside the input field
  • if my_button exists, delete it and create another one (I need this, beacuse I should connect it to a custom event which will do something with the file name)

My question is: how can I correctly delete the button? Note that the my_button html code does not appear on page source!

Thanks

From stackoverflow
  • Pardon me if I'm thinking too simply, but couldn't you just do this?

    var button = document.getElementById('Firefox.Now_button_id')
    button.parentNode.removeChild(button)
    

    Is this what you were looking for? Feel free to correct me if I misunderstood you.

    Giancarlo : This way may remove ALL buttons, I need to remove only the button associated to the input field... I've tryed this: var oldButton = document.getElementById("Firefox.Now_button_id"); if(oldButton!=null){ parentDiv.removeChild(oldButton); } I think I should use IDs but I can't get it working...
    musicfreak : How can this remove all buttons? You should only have one button with that ID.
    Giancarlo : Of course! But this wont remove any buttons, I think because the html code for my_button does not appear on page source!
  • Solved. I set an ID for each with the following method:

    onPageLoad: function(e){
        var inputNodes = top.window.content.document.getElementsByTagName("input");       
        for(var i=0; i<inputNodes.length; i++){
        if(inputNodes[i].type=="file")
             inputNodes[i].setAttribute("id",i.toString());
        } 
    }
    

    I call this method only on page load:

    var appcontent = document.getElementById("appcontent");   // browser
    if(appcontent)
        appcontent.addEventListener("DOMContentLoaded", xpitest.onPageLoad, true);
    

    Then I've modified the onFileChosen method in this way:

    onFileChosen: function(e) {
        var fileInput = e.explicitOriginalTarget;
        if(fileInput.type=="file"){
            var parentDiv = fileInput.parentNode;         
            var buttonId = fileInput.id + "Firefox.Now_button_id";
            var oldButton = top.window.content.document.getElementById(buttonId);
            if(oldButton!=null){
                parentDiv.removeChild(oldButton);
                this.count--;
            }
            var newButton = top.window.content.document.createElement("input");
            newButton.setAttribute("type", "button");      
            newButton.setAttribute("id", buttonId);
            newButton.setAttribute("value", "my button");
            newButton.setAttribute("name", "Firefox.Now_button_name");
            parentDiv.insertBefore(newButton, fileInput);
            this.count++;
        }
    }
    

Are there any other uses for Perlin Noise besides the obvious?

I have been looking into different algorithms lately and have read quite alot about perlin noise. It seems like the only thing people use it for is to generate textures (clouds/wood grain) or to distribute trees.

What else can Perlin Noise be used for?

From stackoverflow
  • One related use is for fractal generated terrains.

  • It can be used in 4 dimensions (i.e x,y,z, time) to create volumetric clouds that appear and disappear. Add a base movement vector that varies over time and you have wind too.

  • The best treatise on Perlin noise and things you can do with it I know is in Texturing and Modelling by Ebert, but Hugo Elias put together a rather good collection of pages on noise and other related subjects some time back which is worth a look.

    I used it extensively for creating realistic-looking landscapes when I wrote a series of Landscape Visualisation programs back in the late 90 early 2000s using various forms of Perlin noise processes to handle the terrain generation. Many other programs do similar things - the wonderful Terragen for example.

    I've also used it to apply realistic noise on top of other textures, for example to add 'roughness' to a Photorealistic Textile plugin for Photoshop.

    Basically the charm of Perlin noise is that it's not random but turbulent, so in any case where you have a non-deterministic phenomenon it can be applied to give more 'natural' results. Defiantly a set or routines that any programmer should be familiar with as its use is appropriate in many circumstances where people tend to reach for a random number generator. For example using a Perlin function to derive variations in velocity of some modelled moving entity in a game (say due to wind or some such) works far better than applying random changes.

    TandemAdam : Your examples were very helpful. I am a big fan of Hugo Elias' website. Also your last paragraph explained Uses for Perlin Noise beautifully.
  • Don't forget about Worley noise too. It's a useful complement to Perlin. The paper itself is here.

    http://www.cse.ohio-state.edu/~nouanese/782/lab4/ http://www.flickr.com/photos/12739382@N04/2652571038/

    TandemAdam : Thanks for that! I had never heard of Worley noise before. I will have to have a play with it now

The strategy to get recovery from broken files?

Hi All,

Me and my colleague are trying to implement a mechanism to get recovery from broken files on an embedded equipment.

This could be happened during certain circumstances, e.g. user takes off the battery during file writing.

Orz, but now we have just one idea:

  • Create duplicated backup files, and copy them back if dangerous file i/o is not finished properly.

This is kind of stupid, as if the backup files also broken, we are just dead.

Do you have any suggestions or good articles on this?

Thanks in advance.

From stackoverflow
  • Read up on database logging and database journal files.

    A database (like Oracle) has very, very robust file writing. Do not actually use Oracle. Use their design pattern. The design pattern goes something like this. You can borrow these ideas without actually using the actual product.

    1. Your transaction (i.e., Insert) will fetch the block to be updated. Usually this is in memory cache, if not, it is read from disk to memory cache.

    2. A "before image" (or rollback segment) copy is made of the block you're about to write.

    3. You change the cache copy, write a journal entry, and queue up a DB write.

    4. You commit the change, which makes the cache change visible to other transactions.

    5. At some point, the DB writer will finalize the DB file change.

    The journal is a simple circular queue file -- the records are just a history of changes with little structure to them. It can be replicated on multiple devices.

    The DB files are more complex structures. They have a "transaction number" -- a simple sequential count of overall transactions. This is encoded in the block (two different ways) as well as written to the control file.

    A good DBA assures that the control file is replicated across devices.

    When Oracle starts up, it checks the control file(s) to find which one is likely to be correct. Others may be corrupted. Oracle checks the DB files to see which match the control file. It checks the journal to see if transactions need to be applied to get the files up to the correct transaction number.

    Of course, if it crashes while writing all of the journal copies, that transaction will be lost -- not much can be done about that. However, if it crashes after the journal entry is written, it will probably recover cleanly with no problems.

    If you lose media, and recover a backup, there's a chance that the journal file can be applied to the recovered backup file and bring it up to date. Otherwise, old journal files have to be replayed to get it up to date.

    James Anderson : Its really very unlikely that an embedded device will be running ORACLE :-) . However sqlite would definately be worth a look as its free, has a tiny footprint and is generally an excellent piece of software.
    S.Lott : The point is NOT to use Oracle. The point is to borrow their design pattern for reliability.
    tingyu : The way sqlite doing this - http://sqlite.org/atomiccommit.html
  • Depends on which OS etc. etc. but in most cases what you can do is copy to a temporary file name and as the last final step rename the files to the correct name.

    This means the (WOOPS) Window of Opertunity Of Potential S****p is confined to the interval when the renames take place.

    If the OS supports a nice directory structure and you lay out the files intelligently you can further refine this by copying the new files to a temp directory and renaming the directory so the WOOPS becomes the interval between "rename target to save" and "rename temp to target".

    This gets even better if the OS supports Soft link directories then you can "ln -s target temp". On most OSes replacing a softlink will be an "atomic" operation which will work or not work without any messy halfway states.

    All these options depend on having enough storage to keep a complete old and new copy on the file system.

    tingyu : Sorry, I don't quite understand the second option: renaming the directory. What's the pros compare with first option? Seems the interval is longer than the first one.
    James Anderson : With the second option the strategy is to group the files which are likely to change in one or two directories. As there will be fewer directories than files this should be quicker.