From 02e2be74f03127c0ea787488bd3690278069ecff Mon Sep 17 00:00:00 2001
From: DaNiMoTh <jjdanimoth@gmail.com>
Date: Sat, 8 Dec 2007 23:02:50 +0100
Subject: [PATCH] Make a big database file and an index

This patch allow repo-add to make a big database ( like pacman1 ),
but also allow making a smaller index file, that contains
useful informations about packages and where entries starts
on big database file.

Signed-off-by: DaNiMoTh <jjdanimoth@gmail.com>
---
 scripts/Makefile.am       |    7 +++-
 scripts/build_index.py.in |   64 +++++++++++++++++++++++++++++++++++++++++++++
 scripts/repo-add.sh.in    |   46 +++++++++++++++++++++++++++++---
 3 files changed, 111 insertions(+), 6 deletions(-)
 create mode 100644 scripts/build_index.py.in

diff --git a/scripts/Makefile.am b/scripts/Makefile.am
index 01250f5..529c213 100644
--- a/scripts/Makefile.am
+++ b/scripts/Makefile.am
@@ -9,7 +9,8 @@ bin_SCRIPTS = \
 	rankmirrors \
 	repo-add \
 	repo-remove \
-	updatesync
+	updatesync \
+	build_index
 
 if INCLUDE_ABS
 bin_SCRIPTS += abs
@@ -24,7 +25,8 @@ EXTRA_DIST = \
 	rankmirrors.py.in \
 	repo-add.sh.in \
 	repo-remove.sh.in \
-	updatesync.sh.in
+	updatesync.sh.in \
+	build_index.py.in
 
 # Files that should be removed, but which Automake does not know.
 MOSTLYCLEANFILES = $(bin_SCRIPTS) *.tmp
@@ -64,5 +66,6 @@ repo-add: $(srcdir)/repo-add.sh.in
 repo-remove: $(srcdir)/repo-remove.sh.in
 re-pacman: $(srcdir)/re-pacman.sh.in
 updatesync: $(srcdir)/updatesync.sh.in
+build_index: $(srcdir)/build_index.py.in
 
 # vim:set ts=2 sw=2 noet:
diff --git a/scripts/build_index.py.in b/scripts/build_index.py.in
new file mode 100644
index 0000000..0d9e899
--- /dev/null
+++ b/scripts/build_index.py.in
@@ -0,0 +1,64 @@
+#!/usr/bin/env python
+
+import sys
+import re
+
+debug = False
+tot_lines = sys.argv[1]
+index_file = sys.argv[2]
+txt_db = sys.argv[3]
+
+def writeIndexEntry(lines_from):
+    """
+    """
+
+    txtdb = open(txt_db, 'r')
+    count = 0;
+    
+    if lines_from != 0:
+        for j in range(0, lines_from):
+            txtdb.readline()
+
+    for i in txtdb.readlines():
+        if count == 0:
+            if debug == True:
+                print "Line: %s" % i
+            pkginfo = re.split('-', i)
+        if (i == "@@ENDS\n"):
+            count = count + 1
+            break
+        else:
+            count = count+1
+
+    if debug == True:
+        print pkginfo
+        print "count: %i" % count
+        print "lines_from: %i" % lines_from
+
+    lines_end = lines_from + int(count)
+    indexfile = open(index_file, 'a')
+    indexfile.write("%s " % pkginfo[0])
+    indexfile.write("- %s " % pkginfo[1])
+    if lines_from == 0:
+        indexfile.write("- %i " % (int(lines_from)))
+    else:
+        indexfile.write("- %i " % (int(lines_from)+1))
+    indexfile.write("- %i\n" % (int(count)+lines_from)) 
+
+    indexfile.close()
+    txtdb.close()
+
+    return lines_end
+
+def rebuildIndex(totlines):
+    """
+    """
+
+    lines = 0
+    while ( lines != int(totlines) ):
+        lines = writeIndexEntry(lines)
+    return 0
+
+
+rebuildIndex(tot_lines)
+
diff --git a/scripts/repo-add.sh.in b/scripts/repo-add.sh.in
index adcefc1..b2d67be 100644
--- a/scripts/repo-add.sh.in
+++ b/scripts/repo-add.sh.in
@@ -127,6 +127,30 @@ db_write_delta()
 	echo -e "$fromver $tover $csize $filename $md5sum" >>deltas
 } # end db_write_delta
 
+clean_db() {
+	local pack=$(echo $1 | cut -d '-' -f1)
+	local duplicated=$(cat $indexfile | grep $pack)
+	local total_line=$(cat $txtdb | wc -l)
+	local head_nline=$(echo "$duplicated" | cut -d '-' -f3)
+	local old_tail_nline=$(echo "$duplicated" | cut -d '-' -f4)
+	local tail_nline=`echo ${total_line}-${old_tail_nline} | bc`
+	DEBUG=0	
+	if [[ $DEBUG == 1 ]]; then
+		echo -en "Pack: $pack\nDuplicated: $duplicated\n"
+		echo -en "total_line: $total_line\nhead_n:$head_nline"
+		echo -en "old_tail: $old_tail_nline\ntail_nline: $tail_nline"
+	fi
+	cat $txtdb | head -n$head_nline > $txtdb.new
+	cat $txtdb | tail -n$tail_nline >> $txtdb.new
+	mv $txtdb.new $txtdb
+  total_line=$(cat $txtdb | wc -l)
+	rm ${indexfile}
+  rebuild_index ${total_line} ${indexfile} ${txtdb}
+}
+
+rebuild_index() {
+	/home/danimoth/source/pacman-new/scripts/build_index $1 $2 $3
+}
 
 # write an entry to the pacman database
 #   arg1 - path to package
@@ -138,12 +162,10 @@ db_write_entry()
 		group depend backup license replaces provides conflict \
 		_groups _depends _backups _licenses _replaces _provides _conflicts \
 		startdir
-
 	local OLDIFS="$IFS"
 	# IFS (field seperator) is only the newline character
 	IFS="
 "
-
 	# read info from the zipped package
 	local line
 	for line in $(bsdtar -xOf "$pkgfile" .PKGINFO | \
@@ -181,6 +203,7 @@ db_write_entry()
 		if [ "${existing%-*-*}" = "$pkgname" ]; then
 			msg2 "$(gettext "Removing existing package '%s'...")" "$existing"
 			rm -rf "$existing"
+			clean_db $existing
 		fi
 	done
 
@@ -215,7 +238,12 @@ db_write_entry()
 	write_list_entry "DEPENDS" "$_depends" "depends"
 	write_list_entry "CONFLICTS" "$_conflicts" "depends"
 	write_list_entry "PROVIDES" "$_provides" "depends"
-
+	
+	echo "$pkgname - $pkgver" >> $txtdb
+  cat desc >> $txtdb
+	cat depends >> $txtdb
+	echo "@@ENDS" >> $txtdb
+	
 	# create deltas entry if there are delta files
 	for delta in $startdir/$pkgname-*-*_to_*-*-$arch.delta; do
 		# This for loop also pulls in all files that start with the current package
@@ -306,7 +334,14 @@ for arg in "$@"; do
 				error "$(gettext "'%s' is not a package file, skipping")" "$arg"
 			else
 				msg "$(gettext "Adding package '%s'")" "$arg"
-
+				indexfile="$gstmpdir/index.idx"
+				txtdb="$gstmpdir/txtdb.idx"
+				if [[ ! -f $indexfile ]]; then
+					touch $indexfile
+				fi
+				if [[ ! -f $txtdb ]]; then
+					touch $txtdb
+				fi
 				if db_write_entry "$arg"; then
 					success=1
 				fi
@@ -317,6 +352,9 @@ for arg in "$@"; do
 	fi
 done
 
+total_lines=$(cat ${txtdb} | wc -l)
+rm ${indexfile}
+rebuild_index ${total_lines} ${indexfile} ${txtdb}
 # if all operations were a success, rezip database
 if [ $success -eq 1 ]; then
 	msg "$(gettext "Creating updated database file %s")" "$REPO_DB_FILE"
-- 
1.5.3.7

