Skip to content

27 March 2007

Last week I checked in code to Beagle which lets application developers set additional metadata on already indexed files, which I first mentioned a couple of weeks ago. My hope is that applications like F-Spot (with its tags) will use these APIs to proactively index their metadata, in addition to the “old fashioned” way of Beagle pulling that information when first building its indexes.

It’s pretty easy to do this from C#, C, and Python. Here’s a C# example:

// Create an indexable object; these describe a document in Beagle
Indexable indexable = new Indexable ("file:///home/joe/test.txt");
indexable.Type = IndexableType.PropertyChange;
indexable.AddProperty (Property.NewKeyword ("mynamespace:is_awesome", "yes"));

// Create a message to send to the daemon with this information.
// The source tells it what index the existing "/home/joe/test.txt" document lives.
IndexingServiceRequest req = new IndexingServiceRequest ();
req.Source = "Files";
req.Add (indexable);

// Send it (synchronously with this API)
req.Send ();

C is a little uglier, but still pretty easy:

BeagleIndexable *indexable;
BeagleProperty *prop;
BeagleClient *client;
BeagleIndexingServiceRequest *request;

/* Create the indexable */
indexable = beagle_indexable_new ("file:///home/joe/test.txt");
beagle_indexable_set_type (indexable, BEAGLE_INDEXABLE_TYPE_PROPERTY_CHANGE);

/* Add a property */
prop = beagle_property_new (BEAGLE_PROPERTY_TYPE_KEYWORD, "mynamespace:is_awesome", "yes");
beagle_indexable_add_property (indexable, prop);

/* Create the request message */
request = beagle_indexing_service_request_new ();
beagle_indexing_service_request_set_source (request, "Files");
beagle_indexing_service_request_add (request, indexable);

/* Create a client connection to the daemon, and send the message */
client = beagle_client_new (NULL);
beagle_client_send_request (client, BEAGLE_REQUEST (request), NULL);

/* Bleh, a non-garbage collected language */
beagle_indexable_free (indexable);
/* ...other object cleanup here... */

Python is easy too, and is based on the C API, so it doesn’t take much imagination to see how it would work.