spk rev 148

Add spk-sql (let make some testing with SQLite package DB to see if we faster than parsing text files
author Christophe Lincoln <pankso@slitaz.org>
date Thu Apr 24 03:09:47 2014 +0200 (2014-04-24)
parents 4b6165102b44
children a8e93994f7b5
files spk-sql
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/spk-sql	Thu Apr 24 03:09:47 2014 +0200
     1.3 @@ -0,0 +1,131 @@
     1.4 +#!/bin/sh
     1.5 +#
     1.6 +# Spk-sql - SliTaz packages DB the SQLite way. This is for testing
     1.7 +# SQLite speed to handle packages info. It may not be used to manage
     1.8 +# OS system but for pkgs.slitaz.org search engine and arm.slitaz.org
     1.9 +#
    1.10 +# Copyright (C) 2014 SliTaz GNU/Linux - BSD License
    1.11 +# Author: See AUTHORS files
    1.12 +#
    1.13 +. /usr/lib/slitaz/libspk.sh
    1.14 +
    1.15 +# Benchmarks:
    1.16 +#
    1.17 +# Listing all packages with:
    1.18 +#
    1.19 +#    spk-ls --short               : real	0m 10.92s
    1.20 +#    spk-sql list --output=html   : real	0m 1.56s
    1.21 +#
    1.22 +#
    1.23 +
    1.24 +#db="${root}${PKGS_DB}/packages.sql"
    1.25 +db="packages.sql"
    1.26 +table="mirror"
    1.27 +
    1.28 +#wok=/home/slitaz/wok
    1.29 +wok=/home/pankso/Projects/wok
    1.30 +
    1.31 +#
    1.32 +# Functions
    1.33 +#
    1.34 +
    1.35 +# Help and usage
    1.36 +usage() {
    1.37 +	name=$(basename $0)
    1.38 +	cat << EOT
    1.39 +
    1.40 +$(boldify $(gettext "Usage:")) $name [command|package] packageN
    1.41 +
    1.42 +$(gettext "SliTaz SQLite packages DB manager")
    1.43 +
    1.44 +$(boldify $(gettext "Commands:"))
    1.45 +  master       Show master record
    1.46 +  tables       List all DB tables
    1.47 +  dbi          Table database information
    1.48 +  list         List SQL DB $table table
    1.49 +  gendb        Generate a SQL DB of all packages
    1.50 +
    1.51 +$(boldify $(gettext "Options:"))
    1.52 +  --output=    Set the output format (list, html)
    1.53 +
    1.54 +EOT
    1.55 +	exit 0
    1.56 +}
    1.57 +
    1.58 +# Create the SQL database
    1.59 +create_db() {
    1.60 +	rm -f $db
    1.61 +	sqlite3 $db << EOT
    1.62 +create table $table(
    1.63 +	pkg,
    1.64 +	version,
    1.65 +	category,
    1.66 +	url,
    1.67 +	desc,
    1.68 +	deps
    1.69 +);
    1.70 +EOT
    1.71 +}
    1.72 +
    1.73 +#
    1.74 +# Handle --options
    1.75 +#
    1.76 +
    1.77 +for opt in $@
    1.78 +do
    1.79 +	case "$opt" in
    1.80 +		*usage|*help) usage ;;
    1.81 +		--count)
    1.82 +			exit 0 ;;
    1.83 +	esac
    1.84 +done
    1.85 +
    1.86 +#
    1.87 +# Handle commands
    1.88 +#
    1.89 +
    1.90 +case "$1" in
    1.91 +	"") usage ;;
    1.92 +	master) sqlite3 $db 'select * from sqlite_master' ;;
    1.93 +	tables) sqlite3 $db '.tables' ;;
    1.94 +	
    1.95 +	dbi)
    1.96 +		# Data Base Info
    1.97 +		du -sh ${db} ;;
    1.98 +	
    1.99 +	list)
   1.100 +		# Very fast listing ou fancy output: html, tcl, tabs, ...
   1.101 +		[ "$output" ] || output="list"
   1.102 +		sqlite3 ${db} << EOT
   1.103 +.mode $output
   1.104 +select * from $table;
   1.105 +EOT
   1.106 +		;;
   1.107 +	
   1.108 +	gendb)
   1.109 +		# Take long to build the +4300 pkgs DB!
   1.110 +		time=$(date +%s)
   1.111 +		echo "Initializing: $db --> $table"
   1.112 +		create_db
   1.113 +		for pkg in $(ls $wok)
   1.114 +		do
   1.115 +			echo "Inserting: $count: $pkg"
   1.116 +			. ${wok}/${pkg}/receipt
   1.117 +			sqlite3 ${db} << EOT
   1.118 +insert into $table values(
   1.119 +	"$PACAKAGE",
   1.120 +	"$VERSION",
   1.121 +	"$CATEGORY",
   1.122 +	"$WEB_SITE",
   1.123 +	"$SHORT_DESC",
   1.124 +	"$DEPENDS"
   1.125 +);
   1.126 +EOT
   1.127 +		done
   1.128 +		time=$(($(date +%s) - $time))
   1.129 +		echo -n "$nb "
   1.130 +		gettext "packages added in"
   1.131 +		echo " ${time}s ("$(date "+%Y%m%d %H:%M")")"
   1.132 +		newline ;;
   1.133 +
   1.134 +esac && exit 0