ジャナ・ビジネス・コンサルティング有限会社トップページへ
/*
 * Berkeley DB XML .NET API
 * 
 * Copyright (C) 2007 Jana Business Consulting Ltd. All rights reserved.
 * 
 * For more information, see http://www.janabiz.com.
 * 
 */

using JanaBiz.Db;
using JanaBiz.DbXml;
using JanaBiz.DbXml.Exceptions;

public class SimpleAdd 
{

    private static string theContainer = "simpleExampleData.dbxml";

    public static void Main(string[] args) 
    {

        string envdir = parseArguments(args);
                        
        DbEnvironment dbEnv = null;
        XmlManager mgr = null;
        XmlContainer container = null;
        
        try 
        {
            // Open an environment
            dbEnv = CreateDbEnvironment(envdir);
            
            // Open an environment and manager
            using(mgr = CreateManager(dbEnv)) 
            {

                // Open a transactional container
                XmlContainerConfig containerconfig = new XmlContainerConfig();
                containerconfig.Transactional = true;
                using(container = mgr.OpenContainer(null, theContainer,
                          containerconfig)) 
                {

                    // Start a transaction
                    using(XmlTransaction txn = mgr.CreateTransaction()) 
                    {

                        // Create an update context
                        using(XmlUpdateContext uc = mgr.CreateUpdateContext()) 
                        {
                            // Create string contents for documents.
                            string document1 = "<aDoc><title>doc1</title><color>green</color></aDoc>";
                            string document2 = "<aDoc><title>doc2</title><color>yellow</color></aDoc>";

                            // Put the document, asking DB XML to generate a name
                            XmlDocumentConfig docconfig = new XmlDocumentConfig();
                            docconfig.GenerateName = true;
                            container.PutDocument(txn, "", document1, uc, docconfig);
        
                            // Do it again for the second document
                            container.PutDocument(txn, "", document2, uc, docconfig);
        
                            // Commit the writes. This causes the container write operations
                            // to be saved to the container.
                            txn.Commit();
                        }
                    }
                }
            }
        }
        catch(DbXmlException e) 
        {
            System.Console.WriteLine("Error performing document add against " + theContainer);
            System.Console.WriteLine(e.ToString());
        }
        finally
        {
            if(container != null)
            {
                container.Close();
            }
            if(mgr != null)
            {
                mgr.Close();
            }
            if(dbEnv != null)
            {
                dbEnv.Close();
            }
        }
    }
    
    public static DbEnvironment CreateDbEnvironment(string envdir) 
    {
        DbEnvironmentConfig envconf = new DbEnvironmentConfig();
        envconf.CacheSize = 50 * 1024;
        envconf.AllowCreate = true;
        envconf.InitializeCache = true;
        envconf.Transactional = true;
        envconf.InitializeLocking = true;
        envconf.InitializeLogging = true;
        envconf.RunRecovery = true;
        envconf.ErrorWriter = System.Console.Out;
        envconf.ErrorPrefix = "PREFIX"; 
        XmlManagerConfig mgrconfig = new XmlManagerConfig();
        mgrconfig.AdoptEnvironment = true;

        DbEnvironment env = new DbEnvironment(envdir, envconf);
        
        return env;
    }

    public static XmlManager CreateManager(DbEnvironment env) 
    {
        XmlManagerConfig mgrconfig = new XmlManagerConfig();
        mgrconfig.AdoptEnvironment = true;

        try 
        {
            return new XmlManager(env, mgrconfig);
        }
        catch(System.Exception e) 
        {
            env.Close();
            throw e;
        }
    }

    private static void Usage() 
    {
        System.Console.WriteLine("This program adds a few simple XML documents to a specified container.");
        System.Console.WriteLine("You should run exampleLoadContainer before running this example.");
        System.Console.WriteLine("You are only required to pass this command the path location of the database");
        System.Console.WriteLine("environment that you specified when you loaded the examples data:");
        System.Console.WriteLine();
        System.Console.WriteLine("\t-h <dbenv directory>");
        System.Console.WriteLine("For example:");
        System.Console.WriteLine("\tsimpleAdd.exe -h examplesEnvironment");

        System.Environment.Exit(-1);
    }

    private static string parseArguments(string[] args) 
    {
        string envdir = null;
        for(int i = 0; i < args.Length; ++i) 
        {
            string arg = args[i];
            if((arg.StartsWith("-")
#if WIN32
                || arg.StartsWith("/")
#endif
                ) && arg.Length > 1) 
            {
                switch(arg[1]) 
                {
                    case 'h': 
                    {
                        ++i;
                        if(i >= args.Length) 
                        {
                            System.Console.WriteLine("Invalid option: " + arg);
                            Usage();
                        }
                        envdir = args[i];
                        break;
                    }
                    default: 
                    {
                        System.Console.WriteLine("Unknown option: " + arg);
                        Usage();
                        break;
                    }
                }
            }
            else 
            {
                System.Console.WriteLine("Too many arguments: " + arg);
                Usage();
            }
        }
        if(envdir == null) 
        {
            System.Console.WriteLine("Environment directory not found.");
            Usage();
        }
        return envdir;
    }

}