Understanding Distributed Postgres BDR

I made this presentation to help myself learn about distributed Postgres; specifically Postgres BDR. It includes background information necessary to understand different Postgres Distributed modes of operation. The notion of a database journal is explained in case the audience is unfamiliar.

The meat of the presentation is looking at various conflicts that will inevitably arise when using a distributed Postgres BDR. It ends with the conclusion that app writers must deal with certain conflicts and points out the user experience effect.

 

108 – Hidden Data Analysis

Method

The given files were first examined at face value by opening them with the system recommended programs (i.e. images with an image viewer, text files with a text editor, HTML files with a web-browser).  A an interesting text document mentioning guns was found.  The text document [New Text Document (3).txt] contained the name of another file [bills.xlsx].  Suspicious lines in the text document began with cell numbers corresponding to bills.xlsx file.  The image cell numbers were not accurate but the text ones were.  The cells contains the same text as the text document.

The web files were opened in a text editor and examined for any hidden HTML, none was found.  Each excel and doc were opened and any known object were examined (f5 in LibreOffice writer and LibreOffice Calc).  The bills.xlsx file was found to have image object that were not immediately visible.  Upon examining the cells that these images lived in it was apparent that there existed small images of guns scattered around the excel document in an effort to make them no hard to find.

Tastypie Inline (aka. Nested, full=True, Embedded) Relationship Dynamically via GET Parameter

I believe this tutorial to be best motivated by an example.  The example relationships below are from a conference app.  A conference app will have presenters (persons) and a schedule with multiple tracks.

api/v1/conference_app/3/?format=json

{
    "persons": [
        "/api/v1/person/35/",
        "/api/v1/person/36/"
    ],
    "resource_uri": "/api/v1/conference_app/3/",
    "summary": "test",
    "tracks": [
        "/api/v1/track/6/",
        "/api/v1/track/7/"
    ]
}

api/v1/conference_app/3/?format=json&inline=true

{
    "persons": [
        {
            "conference_id": "/api/v1/conference_app/3/",
            "description": "",
            "id": "35",
            "person_image": null,
            "person_name": "",
            "resource_uri": "/api/v1/person/35/"
        },
        {
            "conference_id": "/api/v1/conference_app/3/",
            "description": "",
            "id": "36",
            "person_image": null,
            "person_name": "",
            "resource_uri": "/api/v1/person/36/"
        }
    ],
    "resource_uri": "/api/v1/conference_app/3/",
    "summary": "test",
    "tracks": [
        {
            "conference_id": "/api/v1/conference_app/3/",
            "id": "6",
            "resource_uri": "/api/v1/track/6/",
            "schedule_entries": [],
            "track_name": "test1"
        },
        {
            "conference_id": "/api/v1/conference_app/3/",
            "id": "7",
            "resource_uri": "/api/v1/track/7/",
            "schedule_entries": [],
            "track_name": ""
        }
    ]
}

This tutorial will allow the user of an API to dynamically specify that a resource be either inlined or accessed via a resource_uri.

Tastypie is not capable of changing the fields model values dynamically.  The below model inherits from the model ToManyFields which itself inherits from the model Field.  Overriding the method dehydrate_related inherited from the model Field allows us to change the dehydration behavior based on the passed in bundle.request.

class ToManyFieldInlineToggle(fields.ToManyField):
    def dehydrate_related(self, bundle, related_resource):
        try:
            isInline = bundle.request.GET.get('inline')
            if isInline not in [ 'true', 'True', True]:
                return related_resource.get_resource_uri(bundle)
        # bundle.request is None
        except NoneType:
            pass

        # Default non-nested behavior
        bundle = related_resource.build_bundle(obj=related_resource.instance, request=bundle.request)
        return related_resource.full_dehydrate(bundle)

Then, instead of using field.ToManyField() within your models that inherit from ModelResource, use ToManyFieldInineToggle().

Ember.js Handlebars View content Inheritance + Image Upload Preview + View Object Binding

This tutorial includes: (1) inheriting a parent view’s content context in Handlebars, (2) an ajax image upload preview, and (3) object view 2-way binding.

The following fiddle is the full example.  http://jsfiddle.net/MBmUs/63/

Handlebars Content Context

Handlebar provides views the ability to specify a data context for which to interact with.  The working context is effectively a passed in argument for use by the view.  It’s common to have nested views when dealing with Handlebars and Ember.js.  Further, you often want to ‘forward’ the parent context to the nested view.  But how do you do this?  Simple…

{{#view Ember.View contentBinding="App.myModel"}}
    {{#view App.PreviewUploadImage name="logo_image" contentBinding="content"}}
        {{content.myModel_variable}}
    {{/view}}
{{/view}}

Ajax Upload Preview

Providing the user with a preview of an image they intend to upload is a useful feature.  The programmer can provide the user with feedback such as what the re-sized image will look like, how the image fits into the intended use layout, or just to remind the user what image they chose.  Traditional systems first send the image to a server then re-download the image for display.  More commonly this is made transparent to the user with Ajax technology.  We can however improve upon this processes and display the chosen image to the user without first sending it to a server using the FileReader() object.

http://jsfiddle.net/W5QA9/

<script>
function previewImg() {
    var input = $('#file-upload');
    if (input.files && input.files[0]) {
        var reader = new FileReader();
        reader.onload = function(e) {
            $('#blah').attr('src', e.target.result);
                //App.myModel.set('myModel_src', e.target.result);
        }
        reader.readAsDataURL(input.files[0]);
    }
}
</script>

<input id="file-upload" onClick="previewImg();" type="file" />
<img src="#" id="blah" />

View Object Binding

See jsfiddle at top of page.

Raspberry Pi No HDMI SSH

Update June 2, 2012: kernelcode on the raspberry pi forums has a much more elegant solution to the unknown IP address problem. The below code will write your current IP address out to a file. You can then shutdown the Raspberry Pi and remove the sd card; then read the card from your desktop and open the /home/pi/myip.txt file to view you IP address. Note that this isn’t full proof because it requires shutting down the Raspberry Pi board, but more then likely you will get the same IP address upon next boot.

ifconfig > myip.txt

Scenario

You’ve gotten your Raspberry Pi board in the mail and you want to start using it but you’re missing an HDMI cable or a HDMI capable display.  With a little blind typing, you can get open-sshserver up and going.

(This tutorial assumes that you have debian for Raspberry Pi, correctly, on an SDCARD.  If not, see <this tutorial>)

Login

Logging in blind is an easy task.  Type ‘pi’ and hit enter.  Then type ‘raspberry’ and hit enter.  Now switch to root as some of the later commands will require it.

sudo su

Packages

First you need to update your package list.

sudo apt-get update

Next you need to get the openssh-server.  Type the below command, wait a couple seconds, then type y + enter.  Now wait a minute or so while the package downloads and installs.

sudo apt-get install openssh-server

IP Address

Remember, you want to connect to your Raspberry Pi but you don’t have a display so you can’t get the ip address of the device.  The solution is to use the OK status LED to blink your IP address.  We now need to get the code that will blink the ip onto the device and run it.

wget http://bit.ly/N6aQj4 -O blink_ip.pl
perl ./blink_ip.pl

(The above url is a snapshot of the gist url https://gist.github.com/2858824)

blink_ip.pl

  • Starts with a long period of fast blinking
  • Blank for 4 seconds
  • Begin ip address display routine
    • 1-9 displayed by counting using the blinking led
      • Turn led on for .25 seconds
      • Turn led  off for .25 seconds
    • 0 is displayed by rapidly blinking the led
    • Turn led off for 2 seconds
    • Repeat