Skip to main content

Extracting collection from Sedna XML DB

This post is actually based on a kind of an epic fail story. Initially the task was just to rename a collection in Sedna XML DB. The solution is as primitive as using RENAME COLLECTION statement of Sedna Data Definition Language. But I'm probably too enthusiastic about writing Bash scripts in Linux. So I missed out single-statement solution and wrote a bunch of scripts to perform the same task via extracting-loading procedure. Anyway, it can still be quite valuable for more complex tasks like moving a collection between XML DB installations (e.g. from Production to Test environment) or merging collections. So my solution follows below.

Extracting a single file
It's always wise to modularize the code and divide a task into smaller parts. First, we need a script for extracting a single file. It need be parametrized with a file name and a collection name.

Also I address another essential problem here that is the safety of file names. It's not a common problem but we do have files named in XML DB using characters that are illegal for real file names (e.g. the right slash character '/'). It can be easily stored in Sedna XML DB though. The solution is to rename a file using current timestamp and to store a mapping to the original file name in a separate LOAD_FILE. This will be used later for loading files back to XML DB.
extract_file.sh:
#!/bin/sh
# External parameters
FILE_NAME=$1
COLLECTION_NAME=$2

TARGET_PATH=./products
LOAD_FILE=load_file.list
SEDNA_TERM=/appl/sedna/bin/se_term
XQUERY_STRING="doc(\"$FILE_NAME\",\"$COLLECTION_NAME\")"

# Creates a target path directories if needed
mkdir -p $TARGET_PATH
# Safe file name made up from a timestamp to avoid duplication
SAFE_FILE_NAME=`echo $(date +%Y%m%d%H%M%S%N).xml`

# Extracts a file from XML DB
$SEDNA_TERM -query $XQUERY_STRING -output $TARGET_PATH/$SAFE_FILE_NAME mydatabase
# Saves a mapping from a file path to a real file name
echo "'$TARGET_PATH/$SAFE_FILE_NAME' '$FILE_NAME'" >> $LOAD_FILE

Extracting a collection
Second part is writing a script for extracting a complete collection. It simply iterates over the files stored in XML DB collection and executes the above script on each file. I use the FILENAMES_XQUERY to retrieve the list of file names in collection.

Also I track the operation progress using the same approach with counter variable and print_status function that was initially used in this post.
extract_files.sh:
#!/bin/bash

# This function writes a status message to both stdout and $OUTPUT_FILE
function print_status {
  echo ">>> Extracted $counter files, time: `date`" | tee -a $OUTPUT_FILE
}

OUTPUT_FILE=extract_files.log
COLLECTION_NAME=products
SEDNA_TERM=/appl/sedna/bin/se_term
FILENAMES_XQUERY="for \$i in collection(\"products\") return document-uri(\$i)"

echo "" > $OUTPUT_FILE

counter=0
print_status

for file in `$SEDNA_TERM -query "$FILENAMES_XQUERY" mydatabase`
do
  # Exctacts a single file from collection
  ./extract_file.sh $file $COLLECTION_NAME

  let "counter = $counter + 1"
  if (( $counter % 100 == 0 )); then
    print_status
  fi
done

print_status

Loading a collection
The third part is loading files back to XML DB. It has similar iterative structure with the same tracking progress technique as above. The difference is that we iterate over lines in the LOAD_FILE. For each line file_path_and_name we execute LOAD statement to upload the file into a collection (pretty much the same code as in this post). If I needed to merge collections, I'd consider deleting existing file first or using some xquery to check whatever I want about the existing file.
load_files.sh:
#!/bin/bash

# This function writes a status message to both stdout and $OUTPUT_FILE
function print_status {
  echo ">>> Loaded $counter files, time: `date`" | tee -a $OUTPUT_FILE
}

OUTPUT_FILE=load_files.log
COLLECTION_NAME=ontoML/products
SEDNA_TERM=/appl/sedna/bin/se_term
LOAD_FILE=load_file.list

echo "" > $OUTPUT_FILE

counter=0
print_status

# Creates a collection (or shows error message if it already exists)
$SEDNA_TERM -query "CREATE COLLECTION '$COLLECTION_NAME'" mydatabase >> $OUTPUT_FILE

while read file_path_and_name
do
  # Uploads a single file to collection
  $SEDNA_TERM -query "LOAD $file_path_and_name '$COLLECTION_NAME'" mydatabase >> $OUTPUT_FILE

  let "counter = $counter + 1"
  if (( $counter % 100 == 0 )); then
    print_status
  fi
done <$LOAD_FILE

print_status

Results
Finally I used a simple script to combine everything:
run.sh:
#!/bin/sh

echo "### Started extraction at `date`"
./extract_files.sh
echo "### Finished extraction, started upload at `date`"
./load_files.sh
echo "### Finished upload at `date`"
These are generated files' snippets:
extract_files.log:
>>> Extracted 0 files, time: Fri Oct 26 16:34:11 CEST 2012
>>> Extracted 100 files, time: Fri Oct 26 16:34:18 CEST 2012
>>> Extracted 200 files, time: Fri Oct 26 16:34:25 CEST 2012
...
>>> Extracted 7777 files, time: Fri Oct 26 16:43:36 CEST 2012
load_file.list:
'./products/20121026163412406104000.xml' '74HC4538D.xml'
'./products/20121026163412463021000.xml' '74HC4538D/S206.xml'
'./products/20121026163412555510000.xml' '74HC4538D/S242.xml'
...
load_files.log:
>>> Loaded 0 files, time: Fri Oct 26 16:43:36 CEST 2012
UPDATE is executed successfully
Bulk load succeeded
...
Bulk load succeeded
>>> Loaded 7777 files, time: Fri Oct 26 17:11:18 CEST 2012
That's it. At least I got a nice article for the time I wasted instead of using RENAME COLLECTION statement :)

Comments

Popular posts from this blog

DynamicReports and Spring MVC integration

This is a tutorial on how to exploit DynamicReports reporting library in an existing Spring MVC based web application. It's a continuation to the previous post where DynamicReports has been chosen as the most appropriate solution to implement an export feature in a web application (for my specific use case). The complete code won't be provided here but only the essential code snippets together with usage remarks. Also I've widely used this tutorial that describes a similar problem for an alternative reporting library.
So let's turn to the implementation description and start with a short plan of this how-to:
Adding project dependencies.Implementing the Controller part of the MVC pattern.Modifying the View part of the MVC pattern.Modifying web.xml.Adding project dependencies
I used to apply Maven Project Builder throughout my Java applications, thus the dependencies will be provided in the Maven format.

Maven project pom.xml file:
net.sourceforge.dynamicreportsdynamicrepo…

Choosing Java reporting tool - part 2

I've provided a general overview of possible solutions to get a reporting/exporting functionality in the previous post. This is the second overview of alternatives based on JasperReports reporting engine.

Since the previous part I've done the following:
Implemented a simple report using both DynamicJasper and DynamicReports to compare them from technical side.Investigated JasperServer features and tried to implement a simple report for JasperServer instance (it appeared we already have a ready licensed installation of JasperServer that makes it unreasonable to install a fresh one).
First, the comparison results of Java libraries (DynamicJasper and DynamicReports):
Both libraries suffer from poor-quality or missing Java docs but they look a bit better in DynamicJasper.Taking into account the point 1, a developer has to use online documentation and to review the code. Here the code looks definitely nicer and more readable for DynamicReports. With respect t…

Do It Yourself Java Profiling

This article is a free translation of the Russian one that is a transcript of the Russian video lecture done by Roman Elizarov at the Application Developer Days 2011 conference.
The lecturer talked about profiling of Java applications without any standalone tools. Instead, it's suggested to use internal JVM features (i.e. threaddumps, java agents, bytecode manipulation) to implement profiling quickly and efficiently. Moreover, it can be applied on Production environments with minimal overhead. This concept is called DIY or "Do It Yourself". Below the lecture's text and slides begin.
Today I'm giving a lecture "Do It Yourself Java Profiling". It's based on the real life experience that was gained during more than 10 years of developing high-loaded finance applications that work with huge amounts of data, millions currency rate changes per second and thousands of online users. As a result, we have to deal with profiling. Application profiling is an i…