← Back to team overview

zeitgeist team mailing list archive

[Branch ~zeitgeist/zeitgeist/bluebird] Rev 248: implement payload insertion

 

------------------------------------------------------------
revno: 248
committer: Seif Lotfy <seif@xxxxxxxxx>
branch nick: bluebird
timestamp: Sat 2011-09-17 00:43:03 +0200
message:
  implement payload insertion
  implement payload retrieval
modified:
  src/datamodel.vala
  src/engine.vala
  src/sql.vala


--
lp:~zeitgeist/zeitgeist/bluebird
https://code.launchpad.net/~zeitgeist/zeitgeist/bluebird

Your team Zeitgeist Framework Team is subscribed to branch lp:~zeitgeist/zeitgeist/bluebird.
To unsubscribe from this branch go to https://code.launchpad.net/~zeitgeist/zeitgeist/bluebird/+edit-subscription
=== modified file 'src/datamodel.vala'
--- src/datamodel.vala	2011-09-16 22:38:49 +0000
+++ src/datamodel.vala	2011-09-16 22:43:03 +0000
@@ -378,6 +378,7 @@
             {
                 Variant payload_variant = Variant.new_from_data<ByteArray> (
                     new VariantType ("ay"), payload.data, false, payload);
+                // FIXME: somehow adding the payload_variant is not working
                 vb.add_value (payload_variant);
             }
 

=== modified file 'src/engine.vala'
--- src/engine.vala	2011-09-16 15:17:08 +0000
+++ src/engine.vala	2011-09-16 22:43:03 +0000
@@ -106,7 +106,16 @@
                     stmt.column_int (EventViewRows.ACTOR));
                 event.origin = stmt.column_text (
                     EventViewRows.EVENT_ORIGIN_URI);
-                // FIXME: load payload
+                
+                // Load payload
+                unowned uint8[] data = (uint8[]) 
+                    stmt.column_blob(EventViewRows.PAYLOAD);
+                data.length = stmt.column_bytes(EventViewRows.PAYLOAD);
+                if (data != null)
+                {
+                    event.payload = new ByteArray();
+                    event.payload.append(data);
+                }
                 events.insert (event_id, event);
             }
 
@@ -567,9 +576,6 @@
     {
         event.id = ++last_id;
 
-        // FIXME: store the payload
-        // payload_id = store_payload (event);
-
         // Make sure all the URIs, texts and storage are inserted
         {
             var uris = new GenericArray<string> ();
@@ -630,6 +636,8 @@
             }
         }
 
+        var payload_id = store_payload (event);
+
         // FIXME: Should we add something just like TableLookup but with LRU
         //        for those? Or is embedding the query faster? Needs testing!
 
@@ -648,7 +656,9 @@
             manifestations_table.get_id (event.manifestation));
         insert_stmt.bind_int64 (5, actors_table.get_id (event.actor));
         insert_stmt.bind_text (6, event.origin);
-        insert_stmt.bind_int64 (7, 0 /*payload_id*/);
+        if (payload_id == null)
+            payload_id = 0;
+        insert_stmt.bind_int64 (7, payload_id);
 
         for (int i = 0; i < event.num_subjects(); ++i)
         {
@@ -1067,7 +1077,7 @@
         return subwhere;
     }
 
-    private void handle_move_event(Event event)
+    private void handle_move_event (Event event)
     {
         for (int i = 0; i < event.subjects.length; i++)
         {
@@ -1086,7 +1096,34 @@
                 }
             }
         }
-    }
+     }
+
+     private int64? store_payload (Event event)
+     {
+        /** 
+        * TODO: Right now payloads are not unique and every event has its
+        * own one. We could optimize this to store those which are repeated
+        * for different events only once, especially considering that
+        * events cannot be modified once they've been inserted.
+        */
+        if (event.payload != null)
+        {
+            int rc;
+            //unowned string foo = (string) event.payload.data;
+            //stdout.printf("%s\n", foo);
+            unowned Sqlite.Statement payload_insertion_stmt =
+                database.payload_insertion_stmt;
+            payload_insertion_stmt.reset();
+            payload_insertion_stmt.bind_blob(1, event.payload, 
+                event.payload.data.length);
+            if ((rc = payload_insertion_stmt.step()) != Sqlite.DONE)
+                if (rc != Sqlite.CONSTRAINT)
+                    warning ("SQL error: %d, %s\n", rc, db.errmsg ());
+            return database.database.last_insert_rowid();
+        }
+        else
+            return null;
+     }
 
 }
 

=== modified file 'src/sql.vala'
--- src/sql.vala	2011-09-15 22:15:58 +0000
+++ src/sql.vala	2011-09-16 22:43:03 +0000
@@ -55,6 +55,7 @@
         public Sqlite.Statement event_insertion_stmt;
         public Sqlite.Statement id_retrieval_stmt;
         public Sqlite.Statement move_handling_stmt;
+        public Sqlite.Statement payload_insertion_stmt;
 
         // The DB should be accessible from engine for statement preperations
         //  as well as allowing extensions to add tables to it.
@@ -249,6 +250,13 @@
             """;
             rc = database.prepare_v2 (sql, -1, out move_handling_stmt);
             assert_query_success (rc, "Move handling error");
+            
+            // Payload insertion statment
+            sql = """
+                INSERT INTO payload (value) VALUES (?)
+            """;
+            rc = database.prepare_v2 (sql, -1, out payload_insertion_stmt);
+            assert_query_success (rc, "Payload insertion query error");
         }
 
         protected static void update_callback (Sqlite.Action action,