Search code examples
db4o

db4o - how do I get a distinct list of classes contained in a .db4o DB file?


Say I open a .db4o file. What would be the Java code (psuedo) that would generate a list of unique Java CLASSES/TYPES contained in the database?

I am sure I could write the code to do it, but I am afraid it could be quite slow. Is there a good way to do this without querying for every object in the database? Could I use some sort of index?

I'd rather not rely on stored metadata about the database, I'd rather rely on the true information about what objects are actually stored within.


Solution

  • You can use something like (C# but it can be easily converted to Java :)

    const string DatabaseFileName = "c:\\temp\\Learning.odb";
    static void Main(string[] args)
    {
        using (var db = Db4oEmbedded.OpenFile(DatabaseFileName))
        {
            var classes = db.Ext().StoredClasses();
            foreach (var @class in classes)
            {
                Console.WriteLine();
                Console.WriteLine(@class.GetName());
                foreach (var field in @class.GetStoredFields())
                {
                    Console.WriteLine("\t{1} {0}", field.GetName(), field.GetStoredType().GetName());
                }
            }
        }
    }
    

    Note that you have more interesting methods in ExtObjectContainer interface.

    Hope this helps.