• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 252
  • Last Modified:

Finding keys of a structure using date ranges

I have a structure with a date and time value that looks like this:

<cfoutput>
<cfset storage = structnew()>
<cfset storage["time"] = "{ts '2005-04-26 12:03:50'}">
</cfoutput>

Now I want to use structfindvalue() to find any keys where the time is 3 or more hours from the current time, how would I do this?

<cfset results = structfindvalue(storage, "<!--- what would go here? --->", "ALL")>

What would I replace that comment with inorder to find any keys where the date and time value is 3 or more hours from the current time? Any ideas?
0
andw928
Asked:
andw928
  • 3
  • 3
1 Solution
 
Renante EnteraCommented:
Hi andw928!

I just want to clarify something.  Are your comparing it with the current time only not current datetime ???

If you are looking for the keys then you will do something like this :

<cfset storage = structnew()>
<cfset storage["time1"] = "{ts '2005-04-25 16:03:50'}">
<cfset storage["time2"] = "{ts '2005-04-27 18:03:50'}">
<cfset storage["time3"] = "{ts '2005-04-28 15:03:50'}">

<cfset keyList = StructKeyList(storage)>
<cfset newList = ''>

<cfloop index="i" list="#keyList#">
  <cfif Datediff('h',now(),storage[i]) GTE 3>
    <cfset newList = ListAppend(newList,i)>
  </cfif>
</cfloop>

<cfoutput>
Keys having values 3 hours or more from the current datetime #now()# are the ff.:<br>
#ListSort(newList,'text','asc')#
</cfoutput>

:: Reminders ::
   1.  In my code, I'm comparing it with the current datetime.
   2.  If you want to compare it with current time only then replace "<cfif Datediff('h',now(),storage[i]) GTE 3>"
        with "<cfif Datediff('h',TimeFormat(now(),'H:mm:ss'),TimeFormat(storage[i],'H:mm:ss')) GTE 3>"
   3.  And of course, replace the extra label "current datetime #now#" with "current time #TimeFormat(now(),'H:mm:ss')#"

Hope this helps you.  Just try it.


Goodluck!
eNTRANCE2002 :-)
0
 
andw928Author Commented:
That would be nice, although under some testing and benchmarks, looping the code I have takes alot longer than using structfindvalue(). Is there anyway to specify a datetime range using structfindvalue? That was my question, to replace the comment in this tag, with a datetime range, if at all possible:

<cfset results = structfindvalue(storage, "<!--- what would go here? --->", "ALL")>

And if not possible, then what would be the most efficient and fastest way to remove keys that have datetime values more than 3 hours from now without looping it? I expect to have 1000-2000 keys in the structure, looping it on each page request wouldn't be efficient, plus the structure would be in the application scope, so the data would have to be locked...any ideas?

-Matt

0
 
Renante EnteraCommented:
Hmmm ... :-?  I think that could not be possible.  You could not do that using StructFindValue().

Since you have 1000-2000 keys in the structure, it would be better if you will store it in a database.  So, you will have a table that has three(3) columns - col1(for structure), col2(for key), col3(for value).

Then referring to that table, you will do something like this :
1.  Selecting records from table
     <cfquery name="qGetRecord" datasource="DSN">
       Select * From TableName
       Where col1 = 'storage'
       And col2 >= '#DateAdd('h',3,now())#'
     </cfquery>
2.  Retrieving key basing from #1 query
     <cfoutput query="qGetRecord">
       #col2#<br>
     </cfoutput>
3.  Or you want it to store in a list variable
     <cfset keyList = ValueList(qGetRecord.col2)>

Hope this makes sense.


Regards!
eNTRANCE2002 :-)
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
andw928Author Commented:
Once again, I don't know about the database, becuase with these structures, there will be a delete, insert, and a select on each page. Also, I'd have to index 2 columns, which will almost be 8-10 bytes each, non-numeric. That would cause extreme overhead.

The object of the application is to completel an extremely fast application, where the page execution time is low.

I was thinking..what if I setup a scheduled task that runs lets say every 10 or so minutes, would that be good? Would it be better? Would there be any disadvantages?

If nobody else gives me any suggestions then I'll give you the points entrance.
0
 
andw928Author Commented:
Wait, no, scheduled tasks are no good, becuase I would need the results immediately not every so and so. But, besides loops and queries, is there any fast and efficient way to do this? Becuase my data will range anywhere from 1 item to 2000. So an insert, select, delete with 2-3 indexes, requested on each page, would definetely not be efficient especially if I only have 1 item.

But, then the application structures would be a good and fast place, but they would be slower if it reaches near 2000 items, then the queries would be faster. Is there anyway I can do this to balance this out?
0
 
Renante EnteraCommented:
Hi andw928!

That's my only alternative which is using database if you will not use loops.  Actually, looping really cause the system to have low performance if many records are involved like in your case.  So, this is not adviceable.  In addition to that, you're right that scheduled task is no good too as what you have stated.  You will consider case to case basis on implementing it.

Hope this makes sense.


Regards!
eNTRANCE2002 :-)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now