Beispiel #1
0
        static void Main(string[] args)
        {
            Guid guid;

            // create a database
            var pathToDataDir     = "db-data";
            var fileStorageEngine = FileStorageEngineFactory.Create(pathToDataDir, SIZE_1G, MAX_ENTRIES);

            using (var db = new ObjectDB <object>(fileStorageEngine, new BinaryFormatterSerializer()))
            {
                // create
                var data = new MyData(1, "hello world");
                Console.WriteLine("inserting object: " + data);
                guid = db.Insert(data);
                Console.WriteLine("GUID of inserted object={0}", guid);

                // read
                data = db.Read <MyData>(guid);
                Console.WriteLine("read object: {0}", data);

                // update
                data.Text = "very interesting text";
                Console.WriteLine("updating object: {0}", data);
                db.Update(guid, data);
            }

            // re-open existing databse
            using (var db = new ObjectDB <object>(FileStorageEngineFactory.Open(pathToDataDir), new BinaryFormatterSerializer()))
            {
                // read after re-opening the db
                var data = db.Read <MyData>(guid);
                Console.WriteLine("object after re-opening the DB: {0}", data);

                // delete
                Console.WriteLine("deleting object");
                db.Delete(guid);

                // RecordNotFoundException after deletion
                try
                {
                    db.Read <MyData>(guid);
                }
                catch (RecordNotFoundException)
                {
                    Console.WriteLine("object was deleted");
                }
            }
        }
Beispiel #2
0
 private ObjectDB <object> OpenDB(string name)
 {
     return(new ObjectDB <object>(FileStorageEngineFactory.Open("dbs/" + name), new BinaryFormatterSerializer()));
 }