Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Completion of data series by analytical function

568211Oct 13 2007 — edited Oct 15 2007
have the pleasure of learning the benefits of analytical functions and hope to get some help

The case is as follows:
Different projects gets funds from different sources over several years, but not from each source every year.
I want to produce the cumulative sum of funds for each source for each year for each project, but so far I have not been able to do so for years without fund for a particular source.

I have used this syntax:

SUM(fund) OVER(PARTITION BY project, source ORDER BY year ROWS UNBOUNDED PRECEDING)
I have also experimented with different variations of the window clause, but without any luck.

This is the last step in a big job I have been working on for several weeks, so I would be very thankful for any help

Comments

807588
Hi 6tr6tr,

Just build a String instance with the array of bytes and then use the indexOf method to locate the specific strings :
byte[] array;
...
String text = new String(array);
String specific = "something";
if (text.indexOf(specific) >= 0) System.out.println("Found " + specific);
else System.out.println("Not found " + specific);
798635
That is probably the safest way to be encoding independent but assuming with enough memory.
807588
Chicon wrote:
Hi 6tr6tr,

Just build a String instance with the array of bytes and then use the indexOf method to locate the specific strings :
byte[] array;
...
String text = new String(array);
String specific = "something";
if (text.indexOf(specific) >= 0) System.out.println("Found " + specific);
else System.out.println("Not found " + specific);
Thanks. The problem is that after finding the string and replacing it, I need to pass it back as a byte array again. It just seems inefficient to go to from byte array to string and back again. Is this really going to be the fastest?
807588
6tr6tr wrote:
Thanks. The problem is that after finding the string and replacing it, I need to pass it back as a byte array again. It just seems inefficient to go to from byte array to string and back again. Is this really going to be the fastest?
Why do you need to go back to a byte array ? If you think about rewriting the file from which you got the data, there's no need to convert back to an array of bytes.
807588
Chicon wrote:
6tr6tr wrote:
Thanks. The problem is that after finding the string and replacing it, I need to pass it back as a byte array again. It just seems inefficient to go to from byte array to string and back again. Is this really going to be the fastest?
Why do you need to go back to a byte array ? If you think about rewriting the file from which you got the data, there's no need to convert back to an array of bytes.
It's not my choice. The API I've been given (proprietary, internal) requires that i pass the altered data as a byte array.
807588
It's not my choice. The API I've been given (proprietary, internal) requires that i pass the altered data as a byte array.
Ok. To go back to your question : the getBytes method of String is really fast.
795426
Whenever you're dealing with converting Strings to and from a stream/array of bytes, you MUST tell Java what encoding to use to do the conversion. Otherwise, your code's behavior becomes dependent on the platform it is run on.
807588
Is that going to be fast over all compared to having my strings in byte array format and comparing them to the bytes in the data I receive (and then using System.arraycopy to create a new array if replacing occurrs)? Neither one's a great option but I'd prefer to have the smallest impact possible.
DrClap
6tr6tr wrote:
Is that going to be fast over all compared to having my strings in byte array format and comparing them to the bytes in the data I receive (and then using System.arraycopy to create a new array if replacing occurrs)? Neither one's a great option but I'd prefer to have the smallest impact possible.
Try it and see.
807588
I'm inclined to agree with the previous posters -- do the simplest thing and see if it's good enough. However, if after trying that you desire something faster, I think I'd try inverting the problem: assuming your search patterns are small and the byte array your searching through is large, then I'd expect converting your search string to a byte array and writing a little routine that looks for your "pattern" byte array in the big one would give you faster results. And if you still need it faster under penalty of loss-of-job....hmmm....maybe try writing your byte array comparitor in C or C++ and call that from Java? I've never done anything like that, but I think it's possible. Not portable of course, but perhaps your system would allow for such ickiness.

M Busche
EJP
I would do it the other way: convert the byte array to a String and then do a regex search. Or else find a Java implementation of the Knuth-Morris-Pratt or Boyer-Moore algorithms.
807588
ejp wrote:
I would do it the other way: convert the byte array to a String and then do a regex search. Or else find a Java implementation of the Knuth-Morris-Pratt or Boyer-Moore algorithms.
Like this one
/**
 * Knuth-Morris-Pratt agent implementation for looking for bytes in a byte array.
 * <br>Based on<pre>
 * Algorthms in C
 * Robert Sedgewick
 * Addison Wesley
 * ISBN 0-201-514254-7</pre>
 *
 * @author Sabre
 * @see <a href="http://en.wikipedia.org/wiki/Knuth-Morris-Pratt_algorithm" target="right">Wikipedia</a>
 * <a href="http://www.ics.uci.edu/~eppstein/161/960227.html" target="right">Eppstein</a>
 */
public class ByteArrayKMP
{
    /**
     * Contructs the KMP agent to look for the given byte[] pattern.
     *
     * @param pattern the pattern to search for.
     */
    public ByteArrayKMP(byte[] pattern)
    {
        pattern_ = (byte[])pattern.clone();
        next_ = new int[pattern_.length];
        next_[0] = -1;
        for (int len = next_.length-1, i = 0, j = -1; i < len; next_[++i] = ++j)
        {
            while((j >= 0) && (pattern_ != pattern_[j]))
{
j = next_[j];
}
}
}

/**
* Searches a range of byte from a start index
* in a byte array for the pattern used in the construction
* of this agent. Returns the index of the start of the match or -1
* if the pattern is not found.
*
* @param dataToSearch the data to search.
* @param start the start point in the data
* @param length the number of byte to search.
* @return the index of the start of the match or -1 if not found.
*/
public int search(byte[] dataToSearch, int start, int length)
{
final int end = start + length;
if (end > dataToSearch.length)
throw new IllegalArgumentException("Invalid range");

int i = start;
int j = 0;
for (; (j < next_.length) && (i < end); i++, j++)
{
while ((j >= 0) && (dataToSearch[i] != pattern_[j]))
{
j = next_[j];
}
}
return (j == next_.length) ? i - next_.length : -1;
}

/**
* Searches from a start index until the end of the data
* in a byte array for the pattern used in the construction
* of this agent. Returns the index of the start of the match or -1
* if the pattern is not found.
*
* @param dataToSearch the data to search.
* @param start the start point in the data
* @return the index of the start of the match or -1 if not found.
*/
public int search(byte[] dataToSearch, int start)
{
return search(dataToSearch, start, dataToSearch.length - start);
}

/**
* Searches from the start until the end of the data
* in a byte array for the pattern used in the construction
* of this agent. Returns the index of the start of the match or -1
* if the pattern is not found.
*
* @param dataToSearch the data to search.
* @return the index of the start of the match or -1 if not found.
*/
public int search(byte[] dataToSearch)
{
return search(dataToSearch, 0);
}

private final byte[] pattern_;
private final int[] next_;
}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
1 - 12
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Nov 11 2007
Added on Oct 13 2007
9 comments
2,960 views